xref: /titanic_52/usr/src/uts/common/inet/ip/ip_if.c (revision aa52b40e638e124bfdc5d2a65d512955e606cab1)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the interface control functions for IP.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strlog.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/kstat.h>
44 #include <sys/debug.h>
45 #include <sys/zone.h>
46 #include <sys/sunldi.h>
47 #include <sys/file.h>
48 
49 #include <sys/kmem.h>
50 #include <sys/systm.h>
51 #include <sys/param.h>
52 #include <sys/socket.h>
53 #include <sys/isa_defs.h>
54 #include <net/if.h>
55 #include <net/if_arp.h>
56 #include <net/if_types.h>
57 #include <net/if_dl.h>
58 #include <net/route.h>
59 #include <sys/sockio.h>
60 #include <netinet/in.h>
61 #include <netinet/ip6.h>
62 #include <netinet/icmp6.h>
63 #include <netinet/igmp_var.h>
64 #include <sys/strsun.h>
65 #include <sys/policy.h>
66 #include <sys/ethernet.h>
67 
68 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
69 #include <inet/mi.h>
70 #include <inet/nd.h>
71 #include <inet/arp.h>
72 #include <inet/mib2.h>
73 #include <inet/ip.h>
74 #include <inet/ip6.h>
75 #include <inet/ip6_asp.h>
76 #include <inet/tcp.h>
77 #include <inet/ip_multi.h>
78 #include <inet/ip_ire.h>
79 #include <inet/ip_ftable.h>
80 #include <inet/ip_rts.h>
81 #include <inet/ip_ndp.h>
82 #include <inet/ip_if.h>
83 #include <inet/ip_impl.h>
84 #include <inet/tun.h>
85 #include <inet/sctp_ip.h>
86 #include <inet/ip_netinfo.h>
87 #include <inet/mib2.h>
88 
89 #include <net/pfkeyv2.h>
90 #include <inet/ipsec_info.h>
91 #include <inet/sadb.h>
92 #include <inet/ipsec_impl.h>
93 #include <sys/iphada.h>
94 
95 
96 #include <netinet/igmp.h>
97 #include <inet/ip_listutils.h>
98 #include <inet/ipclassifier.h>
99 #include <sys/mac.h>
100 
101 #include <sys/systeminfo.h>
102 #include <sys/bootconf.h>
103 
104 #include <sys/tsol/tndb.h>
105 #include <sys/tsol/tnet.h>
106 
107 /* The character which tells where the ill_name ends */
108 #define	IPIF_SEPARATOR_CHAR	':'
109 
110 /* IP ioctl function table entry */
111 typedef struct ipft_s {
112 	int	ipft_cmd;
113 	pfi_t	ipft_pfi;
114 	int	ipft_min_size;
115 	int	ipft_flags;
116 } ipft_t;
117 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
118 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
119 
120 typedef struct ip_sock_ar_s {
121 	union {
122 		area_t	ip_sock_area;
123 		ared_t	ip_sock_ared;
124 		areq_t	ip_sock_areq;
125 	} ip_sock_ar_u;
126 	queue_t	*ip_sock_ar_q;
127 } ip_sock_ar_t;
128 
129 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
130 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
131 		    char *value, caddr_t cp, cred_t *ioc_cr);
132 
133 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
134 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
135 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
136     mblk_t *mp, boolean_t need_up);
137 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
140     queue_t *q, mblk_t *mp, boolean_t need_up);
141 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
142     mblk_t *mp, boolean_t need_up);
143 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
144     mblk_t *mp);
145 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
146     queue_t *q, mblk_t *mp, boolean_t need_up);
147 static int	ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp,
148     sin_t *sin, boolean_t x_arp_ioctl, boolean_t if_arp_ioctl);
149 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
150     int ioccmd, struct linkblk *li, boolean_t doconsist);
151 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
152 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
153 static void	ipsq_flush(ill_t *ill);
154 
155 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
156     queue_t *q, mblk_t *mp, boolean_t need_up);
157 static void	ipsq_delete(ipsq_t *);
158 
159 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
160 		    boolean_t initialize);
161 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
162 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
163 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
164 		    boolean_t isv6);
165 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
166 static void	ipif_delete_cache_ire(ire_t *, char *);
167 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
168 static void	ipif_free(ipif_t *ipif);
169 static void	ipif_free_tail(ipif_t *ipif);
170 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
171 static void	ipif_multicast_down(ipif_t *ipif);
172 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
173 static void	ipif_set_default(ipif_t *ipif);
174 static int	ipif_set_values(queue_t *q, mblk_t *mp,
175     char *interf_name, uint_t *ppa);
176 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
177     queue_t *q);
178 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
179     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
180     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
181 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
182 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
183 
184 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
185 static int	ill_arp_off(ill_t *ill);
186 static int	ill_arp_on(ill_t *ill);
187 static void	ill_delete_interface_type(ill_if_t *);
188 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
189 static void	ill_dl_down(ill_t *ill);
190 static void	ill_down(ill_t *ill);
191 static void	ill_downi(ire_t *ire, char *ill_arg);
192 static void	ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg);
193 static void	ill_down_tail(ill_t *ill);
194 static void	ill_free_mib(ill_t *ill);
195 static void	ill_glist_delete(ill_t *);
196 static boolean_t ill_has_usable_ipif(ill_t *);
197 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
198 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
199 static void	ill_phyint_free(ill_t *ill);
200 static void	ill_phyint_reinit(ill_t *ill);
201 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
202 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
203 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
204 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
205 static void	ill_stq_cache_delete(ire_t *, char *);
206 
207 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
208 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
209 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
210     in6_addr_t *);
211 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
212     ipaddr_t *);
213 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
214 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
215     in6_addr_t *);
216 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
217     ipaddr_t *);
218 
219 static void	ipif_save_ire(ipif_t *, ire_t *);
220 static void	ipif_remove_ire(ipif_t *, ire_t *);
221 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
222 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
223 
224 /*
225  * Per-ill IPsec capabilities management.
226  */
227 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
228 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
229 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
230 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
231 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
232 static void ill_capability_proto(ill_t *, int, mblk_t *);
233 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
234     boolean_t);
235 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
236 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
237 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
238 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
239 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
240 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
241 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
242 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
243     dl_capability_sub_t *);
244 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
245 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
246 static void ill_capability_lso_reset(ill_t *, mblk_t **);
247 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
248 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
249 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
250 static void	ill_capability_dls_disable(ill_t *);
251 
252 static void	illgrp_cache_delete(ire_t *, char *);
253 static void	illgrp_delete(ill_t *ill);
254 static void	illgrp_reset_schednext(ill_t *ill);
255 
256 static ill_t	*ill_prev_usesrc(ill_t *);
257 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
258 static void	ill_disband_usesrc_group(ill_t *);
259 
260 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
261 
262 /*
263  * if we go over the memory footprint limit more than once in this msec
264  * interval, we'll start pruning aggressively.
265  */
266 int ip_min_frag_prune_time = 0;
267 
268 /*
269  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
270  * and the IPsec DOI
271  */
272 #define	MAX_IPSEC_ALGS	256
273 
274 #define	BITSPERBYTE	8
275 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
276 
277 #define	IPSEC_ALG_ENABLE(algs, algid) \
278 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
279 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
280 
281 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
282 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
283 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
284 
285 typedef uint8_t ipsec_capab_elem_t;
286 
287 /*
288  * Per-algorithm parameters.  Note that at present, only encryption
289  * algorithms have variable keysize (IKE does not provide a way to negotiate
290  * auth algorithm keysize).
291  *
292  * All sizes here are in bits.
293  */
294 typedef struct
295 {
296 	uint16_t	minkeylen;
297 	uint16_t	maxkeylen;
298 } ipsec_capab_algparm_t;
299 
300 /*
301  * Per-ill capabilities.
302  */
303 struct ill_ipsec_capab_s {
304 	ipsec_capab_elem_t *encr_hw_algs;
305 	ipsec_capab_elem_t *auth_hw_algs;
306 	uint32_t algs_size;	/* size of _hw_algs in bytes */
307 	/* algorithm key lengths */
308 	ipsec_capab_algparm_t *encr_algparm;
309 	uint32_t encr_algparm_size;
310 	uint32_t encr_algparm_end;
311 };
312 
313 /*
314  * The field values are larger than strictly necessary for simple
315  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
316  */
317 static area_t	ip_area_template = {
318 	AR_ENTRY_ADD,			/* area_cmd */
319 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
320 					/* area_name_offset */
321 	/* area_name_length temporarily holds this structure length */
322 	sizeof (area_t),			/* area_name_length */
323 	IP_ARP_PROTO_TYPE,		/* area_proto */
324 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
325 	IP_ADDR_LEN,			/* area_proto_addr_length */
326 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
327 					/* area_proto_mask_offset */
328 	0,				/* area_flags */
329 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
330 					/* area_hw_addr_offset */
331 	/* Zero length hw_addr_length means 'use your idea of the address' */
332 	0				/* area_hw_addr_length */
333 };
334 
335 /*
336  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
337  * support
338  */
339 static area_t	ip6_area_template = {
340 	AR_ENTRY_ADD,			/* area_cmd */
341 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
342 					/* area_name_offset */
343 	/* area_name_length temporarily holds this structure length */
344 	sizeof (area_t),			/* area_name_length */
345 	IP_ARP_PROTO_TYPE,		/* area_proto */
346 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
347 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
348 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
349 					/* area_proto_mask_offset */
350 	0,				/* area_flags */
351 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
352 					/* area_hw_addr_offset */
353 	/* Zero length hw_addr_length means 'use your idea of the address' */
354 	0				/* area_hw_addr_length */
355 };
356 
357 static ared_t	ip_ared_template = {
358 	AR_ENTRY_DELETE,
359 	sizeof (ared_t) + IP_ADDR_LEN,
360 	sizeof (ared_t),
361 	IP_ARP_PROTO_TYPE,
362 	sizeof (ared_t),
363 	IP_ADDR_LEN
364 };
365 
366 static ared_t	ip6_ared_template = {
367 	AR_ENTRY_DELETE,
368 	sizeof (ared_t) + IPV6_ADDR_LEN,
369 	sizeof (ared_t),
370 	IP_ARP_PROTO_TYPE,
371 	sizeof (ared_t),
372 	IPV6_ADDR_LEN
373 };
374 
375 /*
376  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
377  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
378  * areq is used).
379  */
380 static areq_t	ip_areq_template = {
381 	AR_ENTRY_QUERY,			/* cmd */
382 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
383 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
384 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
385 	sizeof (areq_t),			/* target addr offset */
386 	IP_ADDR_LEN,			/* target addr_length */
387 	0,				/* flags */
388 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
389 	IP_ADDR_LEN,			/* sender addr length */
390 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
391 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
392 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
393 	/* anything else filled in by the code */
394 };
395 
396 static arc_t	ip_aru_template = {
397 	AR_INTERFACE_UP,
398 	sizeof (arc_t),		/* Name offset */
399 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
400 };
401 
402 static arc_t	ip_ard_template = {
403 	AR_INTERFACE_DOWN,
404 	sizeof (arc_t),		/* Name offset */
405 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
406 };
407 
408 static arc_t	ip_aron_template = {
409 	AR_INTERFACE_ON,
410 	sizeof (arc_t),		/* Name offset */
411 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
412 };
413 
414 static arc_t	ip_aroff_template = {
415 	AR_INTERFACE_OFF,
416 	sizeof (arc_t),		/* Name offset */
417 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
418 };
419 
420 
421 static arma_t	ip_arma_multi_template = {
422 	AR_MAPPING_ADD,
423 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
424 				/* Name offset */
425 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
426 	IP_ARP_PROTO_TYPE,
427 	sizeof (arma_t),			/* proto_addr_offset */
428 	IP_ADDR_LEN,				/* proto_addr_length */
429 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
430 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
431 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
432 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
433 	IP_MAX_HW_LEN,				/* hw_addr_length */
434 	0,					/* hw_mapping_start */
435 };
436 
437 static ipft_t	ip_ioctl_ftbl[] = {
438 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
439 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
440 		IPFT_F_NO_REPLY },
441 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
442 		IPFT_F_NO_REPLY },
443 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
444 	{ 0 }
445 };
446 
447 /* Simple ICMP IP Header Template */
448 static ipha_t icmp_ipha = {
449 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
450 };
451 
452 /* Flag descriptors for ip_ipif_report */
453 static nv_t	ipif_nv_tbl[] = {
454 	{ IPIF_UP,		"UP" },
455 	{ IPIF_BROADCAST,	"BROADCAST" },
456 	{ ILLF_DEBUG,		"DEBUG" },
457 	{ PHYI_LOOPBACK,	"LOOPBACK" },
458 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
459 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
460 	{ PHYI_RUNNING,		"RUNNING" },
461 	{ ILLF_NOARP,		"NOARP" },
462 	{ PHYI_PROMISC,		"PROMISC" },
463 	{ PHYI_ALLMULTI,	"ALLMULTI" },
464 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
465 	{ ILLF_MULTICAST,	"MULTICAST" },
466 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
467 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
468 	{ IPIF_DHCPRUNNING,	"DHCP" },
469 	{ IPIF_PRIVATE,		"PRIVATE" },
470 	{ IPIF_NOXMIT,		"NOXMIT" },
471 	{ IPIF_NOLOCAL,		"NOLOCAL" },
472 	{ IPIF_DEPRECATED,	"DEPRECATED" },
473 	{ IPIF_PREFERRED,	"PREFERRED" },
474 	{ IPIF_TEMPORARY,	"TEMPORARY" },
475 	{ IPIF_ADDRCONF,	"ADDRCONF" },
476 	{ PHYI_VIRTUAL,		"VIRTUAL" },
477 	{ ILLF_ROUTER,		"ROUTER" },
478 	{ ILLF_NONUD,		"NONUD" },
479 	{ IPIF_ANYCAST,		"ANYCAST" },
480 	{ ILLF_NORTEXCH,	"NORTEXCH" },
481 	{ ILLF_IPV4,		"IPV4" },
482 	{ ILLF_IPV6,		"IPV6" },
483 	{ IPIF_MIPRUNNING,	"MIP" },
484 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
485 	{ PHYI_FAILED,		"FAILED" },
486 	{ PHYI_STANDBY,		"STANDBY" },
487 	{ PHYI_INACTIVE,	"INACTIVE" },
488 	{ PHYI_OFFLINE,		"OFFLINE" },
489 };
490 
491 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
492 
493 static ip_m_t	ip_m_tbl[] = {
494 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
495 	    ip_ether_v6intfid },
496 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
497 	    ip_nodef_v6intfid },
498 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
499 	    ip_nodef_v6intfid },
500 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
501 	    ip_nodef_v6intfid },
502 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
503 	    ip_ether_v6intfid },
504 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
505 	    ip_ib_v6intfid },
506 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
507 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
508 	    ip_nodef_v6intfid }
509 };
510 
511 static ill_t	ill_null;		/* Empty ILL for init. */
512 char	ipif_loopback_name[] = "lo0";
513 static char *ipv4_forward_suffix = ":ip_forwarding";
514 static char *ipv6_forward_suffix = ":ip6_forwarding";
515 static	sin6_t	sin6_null;	/* Zero address for quick clears */
516 static	sin_t	sin_null;	/* Zero address for quick clears */
517 
518 /* When set search for unused ipif_seqid */
519 static ipif_t	ipif_zero;
520 
521 /*
522  * ppa arena is created after these many
523  * interfaces have been plumbed.
524  */
525 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
526 
527 /*
528  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
529  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
530  * set through platform specific code (Niagara/Ontario).
531  */
532 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
533 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
534 
535 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
536 
537 static uint_t
538 ipif_rand(ip_stack_t *ipst)
539 {
540 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
541 	    12345;
542 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
543 }
544 
545 /*
546  * Allocate per-interface mibs.
547  * Returns true if ok. False otherwise.
548  *  ipsq  may not yet be allocated (loopback case ).
549  */
550 static boolean_t
551 ill_allocate_mibs(ill_t *ill)
552 {
553 	/* Already allocated? */
554 	if (ill->ill_ip_mib != NULL) {
555 		if (ill->ill_isv6)
556 			ASSERT(ill->ill_icmp6_mib != NULL);
557 		return (B_TRUE);
558 	}
559 
560 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
561 	    KM_NOSLEEP);
562 	if (ill->ill_ip_mib == NULL) {
563 		return (B_FALSE);
564 	}
565 
566 	/* Setup static information */
567 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
568 	    sizeof (mib2_ipIfStatsEntry_t));
569 	if (ill->ill_isv6) {
570 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
571 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
572 		    sizeof (mib2_ipv6AddrEntry_t));
573 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
574 		    sizeof (mib2_ipv6RouteEntry_t));
575 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
576 		    sizeof (mib2_ipv6NetToMediaEntry_t));
577 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
578 		    sizeof (ipv6_member_t));
579 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
580 		    sizeof (ipv6_grpsrc_t));
581 	} else {
582 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
583 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
584 		    sizeof (mib2_ipAddrEntry_t));
585 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
586 		    sizeof (mib2_ipRouteEntry_t));
587 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
588 		    sizeof (mib2_ipNetToMediaEntry_t));
589 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
590 		    sizeof (ip_member_t));
591 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
592 		    sizeof (ip_grpsrc_t));
593 
594 		/*
595 		 * For a v4 ill, we are done at this point, because per ill
596 		 * icmp mibs are only used for v6.
597 		 */
598 		return (B_TRUE);
599 	}
600 
601 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
602 	    KM_NOSLEEP);
603 	if (ill->ill_icmp6_mib == NULL) {
604 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
605 		ill->ill_ip_mib = NULL;
606 		return (B_FALSE);
607 	}
608 	/* static icmp info */
609 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
610 	    sizeof (mib2_ipv6IfIcmpEntry_t);
611 	/*
612 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
613 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
614 	 * -> ill_phyint_reinit
615 	 */
616 	return (B_TRUE);
617 }
618 
619 /*
620  * Common code for preparation of ARP commands.  Two points to remember:
621  * 	1) The ill_name is tacked on at the end of the allocated space so
622  *	   the templates name_offset field must contain the total space
623  *	   to allocate less the name length.
624  *
625  *	2) The templates name_length field should contain the *template*
626  *	   length.  We use it as a parameter to bcopy() and then write
627  *	   the real ill_name_length into the name_length field of the copy.
628  * (Always called as writer.)
629  */
630 mblk_t *
631 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
632 {
633 	arc_t	*arc = (arc_t *)template;
634 	char	*cp;
635 	int	len;
636 	mblk_t	*mp;
637 	uint_t	name_length = ill->ill_name_length;
638 	uint_t	template_len = arc->arc_name_length;
639 
640 	len = arc->arc_name_offset + name_length;
641 	mp = allocb(len, BPRI_HI);
642 	if (mp == NULL)
643 		return (NULL);
644 	cp = (char *)mp->b_rptr;
645 	mp->b_wptr = (uchar_t *)&cp[len];
646 	if (template_len)
647 		bcopy(template, cp, template_len);
648 	if (len > template_len)
649 		bzero(&cp[template_len], len - template_len);
650 	mp->b_datap->db_type = M_PROTO;
651 
652 	arc = (arc_t *)cp;
653 	arc->arc_name_length = name_length;
654 	cp = (char *)arc + arc->arc_name_offset;
655 	bcopy(ill->ill_name, cp, name_length);
656 
657 	if (addr) {
658 		area_t	*area = (area_t *)mp->b_rptr;
659 
660 		cp = (char *)area + area->area_proto_addr_offset;
661 		bcopy(addr, cp, area->area_proto_addr_length);
662 		if (area->area_cmd == AR_ENTRY_ADD) {
663 			cp = (char *)area;
664 			len = area->area_proto_addr_length;
665 			if (area->area_proto_mask_offset)
666 				cp += area->area_proto_mask_offset;
667 			else
668 				cp += area->area_proto_addr_offset + len;
669 			while (len-- > 0)
670 				*cp++ = (char)~0;
671 		}
672 	}
673 	return (mp);
674 }
675 
676 mblk_t *
677 ipif_area_alloc(ipif_t *ipif)
678 {
679 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
680 	    (char *)&ipif->ipif_lcl_addr));
681 }
682 
683 mblk_t *
684 ipif_ared_alloc(ipif_t *ipif)
685 {
686 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
687 	    (char *)&ipif->ipif_lcl_addr));
688 }
689 
690 mblk_t *
691 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
692 {
693 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
694 	    (char *)&addr));
695 }
696 
697 /*
698  * Completely vaporize a lower level tap and all associated interfaces.
699  * ill_delete is called only out of ip_close when the device control
700  * stream is being closed.
701  */
702 void
703 ill_delete(ill_t *ill)
704 {
705 	ipif_t	*ipif;
706 	ill_t	*prev_ill;
707 	ip_stack_t	*ipst = ill->ill_ipst;
708 
709 	/*
710 	 * ill_delete may be forcibly entering the ipsq. The previous
711 	 * ioctl may not have completed and may need to be aborted.
712 	 * ipsq_flush takes care of it. If we don't need to enter the
713 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
714 	 * ill_delete_tail is sufficient.
715 	 */
716 	ipsq_flush(ill);
717 
718 	/*
719 	 * Nuke all interfaces.  ipif_free will take down the interface,
720 	 * remove it from the list, and free the data structure.
721 	 * Walk down the ipif list and remove the logical interfaces
722 	 * first before removing the main ipif. We can't unplumb
723 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
724 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
725 	 * POINTOPOINT.
726 	 *
727 	 * If ill_ipif was not properly initialized (i.e low on memory),
728 	 * then no interfaces to clean up. In this case just clean up the
729 	 * ill.
730 	 */
731 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
732 		ipif_free(ipif);
733 
734 	/*
735 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
736 	 * So nobody can be using this mp now. Free the mp allocated for
737 	 * honoring ILLF_NOARP
738 	 */
739 	freemsg(ill->ill_arp_on_mp);
740 	ill->ill_arp_on_mp = NULL;
741 
742 	/* Clean up msgs on pending upcalls for mrouted */
743 	reset_mrt_ill(ill);
744 
745 	/*
746 	 * ipif_free -> reset_conn_ipif will remove all multicast
747 	 * references for IPv4. For IPv6, we need to do it here as
748 	 * it points only at ills.
749 	 */
750 	reset_conn_ill(ill);
751 
752 	/*
753 	 * ill_down will arrange to blow off any IRE's dependent on this
754 	 * ILL, and shut down fragmentation reassembly.
755 	 */
756 	ill_down(ill);
757 
758 	/* Let SCTP know, so that it can remove this from its list. */
759 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
760 
761 	/*
762 	 * If an address on this ILL is being used as a source address then
763 	 * clear out the pointers in other ILLs that point to this ILL.
764 	 */
765 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
766 	if (ill->ill_usesrc_grp_next != NULL) {
767 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
768 			ill_disband_usesrc_group(ill);
769 		} else {	/* consumer of the usesrc ILL */
770 			prev_ill = ill_prev_usesrc(ill);
771 			prev_ill->ill_usesrc_grp_next =
772 			    ill->ill_usesrc_grp_next;
773 		}
774 	}
775 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
776 }
777 
778 static void
779 ipif_non_duplicate(ipif_t *ipif)
780 {
781 	ill_t *ill = ipif->ipif_ill;
782 	mutex_enter(&ill->ill_lock);
783 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
784 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
785 		ASSERT(ill->ill_ipif_dup_count > 0);
786 		ill->ill_ipif_dup_count--;
787 	}
788 	mutex_exit(&ill->ill_lock);
789 }
790 
791 /*
792  * ill_delete_tail is called from ip_modclose after all references
793  * to the closing ill are gone. The wait is done in ip_modclose
794  */
795 void
796 ill_delete_tail(ill_t *ill)
797 {
798 	mblk_t	**mpp;
799 	ipif_t	*ipif;
800 	ip_stack_t	*ipst = ill->ill_ipst;
801 
802 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
803 		ipif_non_duplicate(ipif);
804 		ipif_down_tail(ipif);
805 	}
806 
807 	ASSERT(ill->ill_ipif_dup_count == 0 &&
808 	    ill->ill_arp_down_mp == NULL &&
809 	    ill->ill_arp_del_mapping_mp == NULL);
810 
811 	/*
812 	 * If polling capability is enabled (which signifies direct
813 	 * upcall into IP and driver has ill saved as a handle),
814 	 * we need to make sure that unbind has completed before we
815 	 * let the ill disappear and driver no longer has any reference
816 	 * to this ill.
817 	 */
818 	mutex_enter(&ill->ill_lock);
819 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
820 		cv_wait(&ill->ill_cv, &ill->ill_lock);
821 	mutex_exit(&ill->ill_lock);
822 
823 	/*
824 	 * Clean up polling and soft ring capabilities
825 	 */
826 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
827 		ill_capability_dls_disable(ill);
828 
829 	if (ill->ill_net_type != IRE_LOOPBACK)
830 		qprocsoff(ill->ill_rq);
831 
832 	/*
833 	 * We do an ipsq_flush once again now. New messages could have
834 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
835 	 * could also have landed up if an ioctl thread had looked up
836 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
837 	 * enqueued the ioctl when we did the ipsq_flush last time.
838 	 */
839 	ipsq_flush(ill);
840 
841 	/*
842 	 * Free capabilities.
843 	 */
844 	if (ill->ill_ipsec_capab_ah != NULL) {
845 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
846 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
847 		ill->ill_ipsec_capab_ah = NULL;
848 	}
849 
850 	if (ill->ill_ipsec_capab_esp != NULL) {
851 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
852 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
853 		ill->ill_ipsec_capab_esp = NULL;
854 	}
855 
856 	if (ill->ill_mdt_capab != NULL) {
857 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
858 		ill->ill_mdt_capab = NULL;
859 	}
860 
861 	if (ill->ill_hcksum_capab != NULL) {
862 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
863 		ill->ill_hcksum_capab = NULL;
864 	}
865 
866 	if (ill->ill_zerocopy_capab != NULL) {
867 		kmem_free(ill->ill_zerocopy_capab,
868 		    sizeof (ill_zerocopy_capab_t));
869 		ill->ill_zerocopy_capab = NULL;
870 	}
871 
872 	if (ill->ill_lso_capab != NULL) {
873 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
874 		ill->ill_lso_capab = NULL;
875 	}
876 
877 	if (ill->ill_dls_capab != NULL) {
878 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
879 		ill->ill_dls_capab->ill_unbind_conn = NULL;
880 		kmem_free(ill->ill_dls_capab,
881 		    sizeof (ill_dls_capab_t) +
882 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
883 		ill->ill_dls_capab = NULL;
884 	}
885 
886 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
887 
888 	while (ill->ill_ipif != NULL)
889 		ipif_free_tail(ill->ill_ipif);
890 
891 	ill_down_tail(ill);
892 
893 	/*
894 	 * We have removed all references to ilm from conn and the ones joined
895 	 * within the kernel.
896 	 *
897 	 * We don't walk conns, mrts and ires because
898 	 *
899 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
900 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
901 	 *    ill references.
902 	 */
903 	ASSERT(ilm_walk_ill(ill) == 0);
904 	/*
905 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
906 	 * could free the phyint. No more reference to the phyint after this
907 	 * point.
908 	 */
909 	(void) ill_glist_delete(ill);
910 
911 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
912 	if (ill->ill_ndd_name != NULL)
913 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
914 	rw_exit(&ipst->ips_ip_g_nd_lock);
915 
916 
917 	if (ill->ill_frag_ptr != NULL) {
918 		uint_t count;
919 
920 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
921 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
922 		}
923 		mi_free(ill->ill_frag_ptr);
924 		ill->ill_frag_ptr = NULL;
925 		ill->ill_frag_hash_tbl = NULL;
926 	}
927 
928 	freemsg(ill->ill_nd_lla_mp);
929 	/* Free all retained control messages. */
930 	mpp = &ill->ill_first_mp_to_free;
931 	do {
932 		while (mpp[0]) {
933 			mblk_t  *mp;
934 			mblk_t  *mp1;
935 
936 			mp = mpp[0];
937 			mpp[0] = mp->b_next;
938 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
939 				mp1->b_next = NULL;
940 				mp1->b_prev = NULL;
941 			}
942 			freemsg(mp);
943 		}
944 	} while (mpp++ != &ill->ill_last_mp_to_free);
945 
946 	ill_free_mib(ill);
947 	/* Drop refcnt here */
948 	netstack_rele(ill->ill_ipst->ips_netstack);
949 	ill->ill_ipst = NULL;
950 
951 	ILL_TRACE_CLEANUP(ill);
952 }
953 
954 static void
955 ill_free_mib(ill_t *ill)
956 {
957 	ip_stack_t *ipst = ill->ill_ipst;
958 
959 	/*
960 	 * MIB statistics must not be lost, so when an interface
961 	 * goes away the counter values will be added to the global
962 	 * MIBs.
963 	 */
964 	if (ill->ill_ip_mib != NULL) {
965 		if (ill->ill_isv6) {
966 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
967 			    ill->ill_ip_mib);
968 		} else {
969 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
970 			    ill->ill_ip_mib);
971 		}
972 
973 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
974 		ill->ill_ip_mib = NULL;
975 	}
976 	if (ill->ill_icmp6_mib != NULL) {
977 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
978 		    ill->ill_icmp6_mib);
979 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
980 		ill->ill_icmp6_mib = NULL;
981 	}
982 }
983 
984 /*
985  * Concatenate together a physical address and a sap.
986  *
987  * Sap_lengths are interpreted as follows:
988  *   sap_length == 0	==>	no sap
989  *   sap_length > 0	==>	sap is at the head of the dlpi address
990  *   sap_length < 0	==>	sap is at the tail of the dlpi address
991  */
992 static void
993 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
994     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
995 {
996 	uint16_t sap_addr = (uint16_t)sap_src;
997 
998 	if (sap_length == 0) {
999 		if (phys_src == NULL)
1000 			bzero(dst, phys_length);
1001 		else
1002 			bcopy(phys_src, dst, phys_length);
1003 	} else if (sap_length < 0) {
1004 		if (phys_src == NULL)
1005 			bzero(dst, phys_length);
1006 		else
1007 			bcopy(phys_src, dst, phys_length);
1008 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1009 	} else {
1010 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1011 		if (phys_src == NULL)
1012 			bzero((char *)dst + sap_length, phys_length);
1013 		else
1014 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1015 	}
1016 }
1017 
1018 /*
1019  * Generate a dl_unitdata_req mblk for the device and address given.
1020  * addr_length is the length of the physical portion of the address.
1021  * If addr is NULL include an all zero address of the specified length.
1022  * TRUE? In any case, addr_length is taken to be the entire length of the
1023  * dlpi address, including the absolute value of sap_length.
1024  */
1025 mblk_t *
1026 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1027 		t_scalar_t sap_length)
1028 {
1029 	dl_unitdata_req_t *dlur;
1030 	mblk_t	*mp;
1031 	t_scalar_t	abs_sap_length;		/* absolute value */
1032 
1033 	abs_sap_length = ABS(sap_length);
1034 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1035 	    DL_UNITDATA_REQ);
1036 	if (mp == NULL)
1037 		return (NULL);
1038 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1039 	/* HACK: accomodate incompatible DLPI drivers */
1040 	if (addr_length == 8)
1041 		addr_length = 6;
1042 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1043 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1044 	dlur->dl_priority.dl_min = 0;
1045 	dlur->dl_priority.dl_max = 0;
1046 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1047 	    (uchar_t *)&dlur[1]);
1048 	return (mp);
1049 }
1050 
1051 /*
1052  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1053  * Return an error if we already have 1 or more ioctls in progress.
1054  * This is used only for non-exclusive ioctls. Currently this is used
1055  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1056  * and thus need to use ipsq_pending_mp_add.
1057  */
1058 boolean_t
1059 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1060 {
1061 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1062 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1063 	/*
1064 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1065 	 */
1066 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1067 	    (add_mp->b_datap->db_type == M_IOCTL));
1068 
1069 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1070 	/*
1071 	 * Return error if the conn has started closing. The conn
1072 	 * could have finished cleaning up the pending mp list,
1073 	 * If so we should not add another mp to the list negating
1074 	 * the cleanup.
1075 	 */
1076 	if (connp->conn_state_flags & CONN_CLOSING)
1077 		return (B_FALSE);
1078 	/*
1079 	 * Add the pending mp to the head of the list, chained by b_next.
1080 	 * Note down the conn on which the ioctl request came, in b_prev.
1081 	 * This will be used to later get the conn, when we get a response
1082 	 * on the ill queue, from some other module (typically arp)
1083 	 */
1084 	add_mp->b_next = (void *)ill->ill_pending_mp;
1085 	add_mp->b_queue = CONNP_TO_WQ(connp);
1086 	ill->ill_pending_mp = add_mp;
1087 	if (connp != NULL)
1088 		connp->conn_oper_pending_ill = ill;
1089 	return (B_TRUE);
1090 }
1091 
1092 /*
1093  * Retrieve the ill_pending_mp and return it. We have to walk the list
1094  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1095  */
1096 mblk_t *
1097 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1098 {
1099 	mblk_t	*prev = NULL;
1100 	mblk_t	*curr = NULL;
1101 	uint_t	id;
1102 	conn_t	*connp;
1103 
1104 	/*
1105 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1106 	 * up the pending mp, but it does not know the ioc_id and
1107 	 * passes in a zero for it.
1108 	 */
1109 	mutex_enter(&ill->ill_lock);
1110 	if (ioc_id != 0)
1111 		*connpp = NULL;
1112 
1113 	/* Search the list for the appropriate ioctl based on ioc_id */
1114 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1115 	    prev = curr, curr = curr->b_next) {
1116 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1117 		connp = Q_TO_CONN(curr->b_queue);
1118 		/* Match based on the ioc_id or based on the conn */
1119 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1120 			break;
1121 	}
1122 
1123 	if (curr != NULL) {
1124 		/* Unlink the mblk from the pending mp list */
1125 		if (prev != NULL) {
1126 			prev->b_next = curr->b_next;
1127 		} else {
1128 			ASSERT(ill->ill_pending_mp == curr);
1129 			ill->ill_pending_mp = curr->b_next;
1130 		}
1131 
1132 		/*
1133 		 * conn refcnt must have been bumped up at the start of
1134 		 * the ioctl. So we can safely access the conn.
1135 		 */
1136 		ASSERT(CONN_Q(curr->b_queue));
1137 		*connpp = Q_TO_CONN(curr->b_queue);
1138 		curr->b_next = NULL;
1139 		curr->b_queue = NULL;
1140 	}
1141 
1142 	mutex_exit(&ill->ill_lock);
1143 
1144 	return (curr);
1145 }
1146 
1147 /*
1148  * Add the pending mp to the list. There can be only 1 pending mp
1149  * in the list. Any exclusive ioctl that needs to wait for a response
1150  * from another module or driver needs to use this function to set
1151  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1152  * the other module/driver. This is also used while waiting for the
1153  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1154  */
1155 boolean_t
1156 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1157     int waitfor)
1158 {
1159 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1160 
1161 	ASSERT(IAM_WRITER_IPIF(ipif));
1162 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1163 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1164 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1165 	/*
1166 	 * The caller may be using a different ipif than the one passed into
1167 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1168 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1169 	 * that `ipsq_current_ipif == ipif'.
1170 	 */
1171 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1172 
1173 	/*
1174 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1175 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1176 	 */
1177 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1178 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1179 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1180 
1181 	if (connp != NULL) {
1182 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1183 		/*
1184 		 * Return error if the conn has started closing. The conn
1185 		 * could have finished cleaning up the pending mp list,
1186 		 * If so we should not add another mp to the list negating
1187 		 * the cleanup.
1188 		 */
1189 		if (connp->conn_state_flags & CONN_CLOSING)
1190 			return (B_FALSE);
1191 	}
1192 	mutex_enter(&ipsq->ipsq_lock);
1193 	ipsq->ipsq_pending_ipif = ipif;
1194 	/*
1195 	 * Note down the queue in b_queue. This will be returned by
1196 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1197 	 * the processing
1198 	 */
1199 	add_mp->b_next = NULL;
1200 	add_mp->b_queue = q;
1201 	ipsq->ipsq_pending_mp = add_mp;
1202 	ipsq->ipsq_waitfor = waitfor;
1203 
1204 	if (connp != NULL)
1205 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1206 	mutex_exit(&ipsq->ipsq_lock);
1207 	return (B_TRUE);
1208 }
1209 
1210 /*
1211  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1212  * queued in the list.
1213  */
1214 mblk_t *
1215 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1216 {
1217 	mblk_t	*curr = NULL;
1218 
1219 	mutex_enter(&ipsq->ipsq_lock);
1220 	*connpp = NULL;
1221 	if (ipsq->ipsq_pending_mp == NULL) {
1222 		mutex_exit(&ipsq->ipsq_lock);
1223 		return (NULL);
1224 	}
1225 
1226 	/* There can be only 1 such excl message */
1227 	curr = ipsq->ipsq_pending_mp;
1228 	ASSERT(curr != NULL && curr->b_next == NULL);
1229 	ipsq->ipsq_pending_ipif = NULL;
1230 	ipsq->ipsq_pending_mp = NULL;
1231 	ipsq->ipsq_waitfor = 0;
1232 	mutex_exit(&ipsq->ipsq_lock);
1233 
1234 	if (CONN_Q(curr->b_queue)) {
1235 		/*
1236 		 * This mp did a refhold on the conn, at the start of the ioctl.
1237 		 * So we can safely return a pointer to the conn to the caller.
1238 		 */
1239 		*connpp = Q_TO_CONN(curr->b_queue);
1240 	} else {
1241 		*connpp = NULL;
1242 	}
1243 	curr->b_next = NULL;
1244 	curr->b_prev = NULL;
1245 	return (curr);
1246 }
1247 
1248 /*
1249  * Cleanup the ioctl mp queued in ipsq_pending_mp
1250  * - Called in the ill_delete path
1251  * - Called in the M_ERROR or M_HANGUP path on the ill.
1252  * - Called in the conn close path.
1253  */
1254 boolean_t
1255 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1256 {
1257 	mblk_t	*mp;
1258 	ipsq_t	*ipsq;
1259 	queue_t	*q;
1260 	ipif_t	*ipif;
1261 
1262 	ASSERT(IAM_WRITER_ILL(ill));
1263 	ipsq = ill->ill_phyint->phyint_ipsq;
1264 	mutex_enter(&ipsq->ipsq_lock);
1265 	/*
1266 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1267 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1268 	 * even if it is meant for another ill, since we have to enqueue
1269 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1270 	 * If connp is non-null we are called from the conn close path.
1271 	 */
1272 	mp = ipsq->ipsq_pending_mp;
1273 	if (mp == NULL || (connp != NULL &&
1274 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1275 		mutex_exit(&ipsq->ipsq_lock);
1276 		return (B_FALSE);
1277 	}
1278 	/* Now remove from the ipsq_pending_mp */
1279 	ipsq->ipsq_pending_mp = NULL;
1280 	q = mp->b_queue;
1281 	mp->b_next = NULL;
1282 	mp->b_prev = NULL;
1283 	mp->b_queue = NULL;
1284 
1285 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1286 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1287 	if (ill->ill_move_in_progress) {
1288 		ILL_CLEAR_MOVE(ill);
1289 	} else if (ill->ill_up_ipifs) {
1290 		ill_group_cleanup(ill);
1291 	}
1292 
1293 	ipif = ipsq->ipsq_pending_ipif;
1294 	ipsq->ipsq_pending_ipif = NULL;
1295 	ipsq->ipsq_waitfor = 0;
1296 	ipsq->ipsq_current_ipif = NULL;
1297 	ipsq->ipsq_current_ioctl = 0;
1298 	mutex_exit(&ipsq->ipsq_lock);
1299 
1300 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1301 		if (connp == NULL) {
1302 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1303 		} else {
1304 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1305 			mutex_enter(&ipif->ipif_ill->ill_lock);
1306 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1307 			mutex_exit(&ipif->ipif_ill->ill_lock);
1308 		}
1309 	} else {
1310 		/*
1311 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1312 		 * be just inet_freemsg. we have to restart it
1313 		 * otherwise the thread will be stuck.
1314 		 */
1315 		inet_freemsg(mp);
1316 	}
1317 	return (B_TRUE);
1318 }
1319 
1320 /*
1321  * The ill is closing. Cleanup all the pending mps. Called exclusively
1322  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1323  * knows this ill, and hence nobody can add an mp to this list
1324  */
1325 static void
1326 ill_pending_mp_cleanup(ill_t *ill)
1327 {
1328 	mblk_t	*mp;
1329 	queue_t	*q;
1330 
1331 	ASSERT(IAM_WRITER_ILL(ill));
1332 
1333 	mutex_enter(&ill->ill_lock);
1334 	/*
1335 	 * Every mp on the pending mp list originating from an ioctl
1336 	 * added 1 to the conn refcnt, at the start of the ioctl.
1337 	 * So bump it down now.  See comments in ip_wput_nondata()
1338 	 */
1339 	while (ill->ill_pending_mp != NULL) {
1340 		mp = ill->ill_pending_mp;
1341 		ill->ill_pending_mp = mp->b_next;
1342 		mutex_exit(&ill->ill_lock);
1343 
1344 		q = mp->b_queue;
1345 		ASSERT(CONN_Q(q));
1346 		mp->b_next = NULL;
1347 		mp->b_prev = NULL;
1348 		mp->b_queue = NULL;
1349 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1350 		mutex_enter(&ill->ill_lock);
1351 	}
1352 	ill->ill_pending_ipif = NULL;
1353 
1354 	mutex_exit(&ill->ill_lock);
1355 }
1356 
1357 /*
1358  * Called in the conn close path and ill delete path
1359  */
1360 static void
1361 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1362 {
1363 	ipsq_t	*ipsq;
1364 	mblk_t	*prev;
1365 	mblk_t	*curr;
1366 	mblk_t	*next;
1367 	queue_t	*q;
1368 	mblk_t	*tmp_list = NULL;
1369 
1370 	ASSERT(IAM_WRITER_ILL(ill));
1371 	if (connp != NULL)
1372 		q = CONNP_TO_WQ(connp);
1373 	else
1374 		q = ill->ill_wq;
1375 
1376 	ipsq = ill->ill_phyint->phyint_ipsq;
1377 	/*
1378 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1379 	 * In the case of ioctl from a conn, there can be only 1 mp
1380 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1381 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1382 	 * ioctls meant for this ill form conn's are not flushed. They will
1383 	 * be processed during ipsq_exit and will not find the ill and will
1384 	 * return error.
1385 	 */
1386 	mutex_enter(&ipsq->ipsq_lock);
1387 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1388 	    curr = next) {
1389 		next = curr->b_next;
1390 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1391 			/* Unlink the mblk from the pending mp list */
1392 			if (prev != NULL) {
1393 				prev->b_next = curr->b_next;
1394 			} else {
1395 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1396 				ipsq->ipsq_xopq_mphead = curr->b_next;
1397 			}
1398 			if (ipsq->ipsq_xopq_mptail == curr)
1399 				ipsq->ipsq_xopq_mptail = prev;
1400 			/*
1401 			 * Create a temporary list and release the ipsq lock
1402 			 * New elements are added to the head of the tmp_list
1403 			 */
1404 			curr->b_next = tmp_list;
1405 			tmp_list = curr;
1406 		} else {
1407 			prev = curr;
1408 		}
1409 	}
1410 	mutex_exit(&ipsq->ipsq_lock);
1411 
1412 	while (tmp_list != NULL) {
1413 		curr = tmp_list;
1414 		tmp_list = curr->b_next;
1415 		curr->b_next = NULL;
1416 		curr->b_prev = NULL;
1417 		curr->b_queue = NULL;
1418 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1419 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1420 			    CONN_CLOSE : NO_COPYOUT, NULL);
1421 		} else {
1422 			/*
1423 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1424 			 * this can't be just inet_freemsg. we have to
1425 			 * restart it otherwise the thread will be stuck.
1426 			 */
1427 			inet_freemsg(curr);
1428 		}
1429 	}
1430 }
1431 
1432 /*
1433  * This conn has started closing. Cleanup any pending ioctl from this conn.
1434  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1435  */
1436 void
1437 conn_ioctl_cleanup(conn_t *connp)
1438 {
1439 	mblk_t *curr;
1440 	ipsq_t	*ipsq;
1441 	ill_t	*ill;
1442 	boolean_t refheld;
1443 
1444 	/*
1445 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1446 	 * ioctl has not yet started, the mp is pending in the list headed by
1447 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1448 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1449 	 * is currently executing now the mp is not queued anywhere but
1450 	 * conn_oper_pending_ill is null. The conn close will wait
1451 	 * till the conn_ref drops to zero.
1452 	 */
1453 	mutex_enter(&connp->conn_lock);
1454 	ill = connp->conn_oper_pending_ill;
1455 	if (ill == NULL) {
1456 		mutex_exit(&connp->conn_lock);
1457 		return;
1458 	}
1459 
1460 	curr = ill_pending_mp_get(ill, &connp, 0);
1461 	if (curr != NULL) {
1462 		mutex_exit(&connp->conn_lock);
1463 		CONN_DEC_REF(connp);
1464 		inet_freemsg(curr);
1465 		return;
1466 	}
1467 	/*
1468 	 * We may not be able to refhold the ill if the ill/ipif
1469 	 * is changing. But we need to make sure that the ill will
1470 	 * not vanish. So we just bump up the ill_waiter count.
1471 	 */
1472 	refheld = ill_waiter_inc(ill);
1473 	mutex_exit(&connp->conn_lock);
1474 	if (refheld) {
1475 		if (ipsq_enter(ill, B_TRUE)) {
1476 			ill_waiter_dcr(ill);
1477 			/*
1478 			 * Check whether this ioctl has started and is
1479 			 * pending now in ipsq_pending_mp. If it is not
1480 			 * found there then check whether this ioctl has
1481 			 * not even started and is in the ipsq_xopq list.
1482 			 */
1483 			if (!ipsq_pending_mp_cleanup(ill, connp))
1484 				ipsq_xopq_mp_cleanup(ill, connp);
1485 			ipsq = ill->ill_phyint->phyint_ipsq;
1486 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1487 			return;
1488 		}
1489 	}
1490 
1491 	/*
1492 	 * The ill is also closing and we could not bump up the
1493 	 * ill_waiter_count or we could not enter the ipsq. Leave
1494 	 * the cleanup to ill_delete
1495 	 */
1496 	mutex_enter(&connp->conn_lock);
1497 	while (connp->conn_oper_pending_ill != NULL)
1498 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1499 	mutex_exit(&connp->conn_lock);
1500 	if (refheld)
1501 		ill_waiter_dcr(ill);
1502 }
1503 
1504 /*
1505  * ipcl_walk function for cleaning up conn_*_ill fields.
1506  */
1507 static void
1508 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1509 {
1510 	ill_t	*ill = (ill_t *)arg;
1511 	ire_t	*ire;
1512 
1513 	mutex_enter(&connp->conn_lock);
1514 	if (connp->conn_multicast_ill == ill) {
1515 		/* Revert to late binding */
1516 		connp->conn_multicast_ill = NULL;
1517 		connp->conn_orig_multicast_ifindex = 0;
1518 	}
1519 	if (connp->conn_incoming_ill == ill)
1520 		connp->conn_incoming_ill = NULL;
1521 	if (connp->conn_outgoing_ill == ill)
1522 		connp->conn_outgoing_ill = NULL;
1523 	if (connp->conn_outgoing_pill == ill)
1524 		connp->conn_outgoing_pill = NULL;
1525 	if (connp->conn_nofailover_ill == ill)
1526 		connp->conn_nofailover_ill = NULL;
1527 	if (connp->conn_xmit_if_ill == ill)
1528 		connp->conn_xmit_if_ill = NULL;
1529 	if (connp->conn_ire_cache != NULL) {
1530 		ire = connp->conn_ire_cache;
1531 		/*
1532 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1533 		 * interface X and ipif coming from interface Y, if interface
1534 		 * X and Y are part of the same IPMPgroup. Thus whenever
1535 		 * interface X goes down, remove all references to it by
1536 		 * checking both on ire_ipif and ire_stq.
1537 		 */
1538 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1539 		    (ire->ire_type == IRE_CACHE &&
1540 		    ire->ire_stq == ill->ill_wq)) {
1541 			connp->conn_ire_cache = NULL;
1542 			mutex_exit(&connp->conn_lock);
1543 			ire_refrele_notr(ire);
1544 			return;
1545 		}
1546 	}
1547 	mutex_exit(&connp->conn_lock);
1548 
1549 }
1550 
1551 /* ARGSUSED */
1552 void
1553 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1554 {
1555 	ill_t	*ill = q->q_ptr;
1556 	ipif_t	*ipif;
1557 
1558 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1559 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1560 		ipif_non_duplicate(ipif);
1561 		ipif_down_tail(ipif);
1562 	}
1563 	ill_down_tail(ill);
1564 	freemsg(mp);
1565 	ipsq_current_finish(ipsq);
1566 }
1567 
1568 /*
1569  * ill_down_start is called when we want to down this ill and bring it up again
1570  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1571  * all interfaces, but don't tear down any plumbing.
1572  */
1573 boolean_t
1574 ill_down_start(queue_t *q, mblk_t *mp)
1575 {
1576 	ill_t	*ill = q->q_ptr;
1577 	ipif_t	*ipif;
1578 
1579 	ASSERT(IAM_WRITER_ILL(ill));
1580 
1581 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1582 		(void) ipif_down(ipif, NULL, NULL);
1583 
1584 	ill_down(ill);
1585 
1586 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1587 
1588 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1589 
1590 	/*
1591 	 * Atomically test and add the pending mp if references are active.
1592 	 */
1593 	mutex_enter(&ill->ill_lock);
1594 	if (!ill_is_quiescent(ill)) {
1595 		/* call cannot fail since `conn_t *' argument is NULL */
1596 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1597 		    mp, ILL_DOWN);
1598 		mutex_exit(&ill->ill_lock);
1599 		return (B_FALSE);
1600 	}
1601 	mutex_exit(&ill->ill_lock);
1602 	return (B_TRUE);
1603 }
1604 
1605 static void
1606 ill_down(ill_t *ill)
1607 {
1608 	ip_stack_t	*ipst = ill->ill_ipst;
1609 
1610 	/* Blow off any IREs dependent on this ILL. */
1611 	ire_walk(ill_downi, (char *)ill, ipst);
1612 
1613 	mutex_enter(&ipst->ips_ire_mrtun_lock);
1614 	if (ipst->ips_ire_mrtun_count != 0) {
1615 		mutex_exit(&ipst->ips_ire_mrtun_lock);
1616 		ire_walk_ill_mrtun(0, 0, ill_downi_mrtun_srcif,
1617 		    (char *)ill, NULL, ipst);
1618 	} else {
1619 		mutex_exit(&ipst->ips_ire_mrtun_lock);
1620 	}
1621 
1622 	/*
1623 	 * If any interface based forwarding table exists
1624 	 * Blow off the ires there dependent on this ill
1625 	 */
1626 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
1627 	if (ipst->ips_ire_srcif_table_count > 0) {
1628 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
1629 		ire_walk_srcif_table_v4(ill_downi_mrtun_srcif, (char *)ill,
1630 		    ipst);
1631 	} else {
1632 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
1633 	}
1634 
1635 	/* Remove any conn_*_ill depending on this ill */
1636 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1637 
1638 	if (ill->ill_group != NULL) {
1639 		illgrp_delete(ill);
1640 	}
1641 }
1642 
1643 static void
1644 ill_down_tail(ill_t *ill)
1645 {
1646 	int	i;
1647 
1648 	/* Destroy ill_srcif_table if it exists */
1649 	/* Lock not reqd really because nobody should be able to access */
1650 	mutex_enter(&ill->ill_lock);
1651 	if (ill->ill_srcif_table != NULL) {
1652 		ill->ill_srcif_refcnt = 0;
1653 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
1654 			rw_destroy(&ill->ill_srcif_table[i].irb_lock);
1655 		}
1656 		kmem_free(ill->ill_srcif_table,
1657 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t));
1658 		ill->ill_srcif_table = NULL;
1659 		ill->ill_srcif_refcnt = 0;
1660 		ill->ill_mrtun_refcnt = 0;
1661 	}
1662 	mutex_exit(&ill->ill_lock);
1663 }
1664 
1665 /*
1666  * ire_walk routine used to delete every IRE that depends on queues
1667  * associated with 'ill'.  (Always called as writer.)
1668  */
1669 static void
1670 ill_downi(ire_t *ire, char *ill_arg)
1671 {
1672 	ill_t	*ill = (ill_t *)ill_arg;
1673 
1674 	/*
1675 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1676 	 * interface X and ipif coming from interface Y, if interface
1677 	 * X and Y are part of the same IPMP group. Thus whenever interface
1678 	 * X goes down, remove all references to it by checking both
1679 	 * on ire_ipif and ire_stq.
1680 	 */
1681 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1682 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1683 		ire_delete(ire);
1684 	}
1685 }
1686 
1687 /*
1688  * A seperate routine for deleting revtun and srcif based routes
1689  * are needed because the ires only deleted when the interface
1690  * is unplumbed. Also these ires have ire_in_ill non-null as well.
1691  * we want to keep mobile IP specific code separate.
1692  */
1693 static void
1694 ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg)
1695 {
1696 	ill_t   *ill = (ill_t *)ill_arg;
1697 
1698 	ASSERT(ire->ire_in_ill != NULL);
1699 
1700 	if ((ire->ire_in_ill != NULL && ire->ire_in_ill == ill) ||
1701 	    (ire->ire_stq == ill->ill_wq) || (ire->ire_stq == ill->ill_rq)) {
1702 		ire_delete(ire);
1703 	}
1704 }
1705 
1706 /*
1707  * Remove ire/nce from the fastpath list.
1708  */
1709 void
1710 ill_fastpath_nack(ill_t *ill)
1711 {
1712 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1713 }
1714 
1715 /* Consume an M_IOCACK of the fastpath probe. */
1716 void
1717 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1718 {
1719 	mblk_t	*mp1 = mp;
1720 
1721 	/*
1722 	 * If this was the first attempt turn on the fastpath probing.
1723 	 */
1724 	mutex_enter(&ill->ill_lock);
1725 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1726 		ill->ill_dlpi_fastpath_state = IDS_OK;
1727 	mutex_exit(&ill->ill_lock);
1728 
1729 	/* Free the M_IOCACK mblk, hold on to the data */
1730 	mp = mp->b_cont;
1731 	freeb(mp1);
1732 	if (mp == NULL)
1733 		return;
1734 	if (mp->b_cont != NULL) {
1735 		/*
1736 		 * Update all IRE's or NCE's that are waiting for
1737 		 * fastpath update.
1738 		 */
1739 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1740 		mp1 = mp->b_cont;
1741 		freeb(mp);
1742 		mp = mp1;
1743 	} else {
1744 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1745 	}
1746 
1747 	freeb(mp);
1748 }
1749 
1750 /*
1751  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1752  * The data portion of the request is a dl_unitdata_req_t template for
1753  * what we would send downstream in the absence of a fastpath confirmation.
1754  */
1755 int
1756 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1757 {
1758 	struct iocblk	*ioc;
1759 	mblk_t	*mp;
1760 
1761 	if (dlur_mp == NULL)
1762 		return (EINVAL);
1763 
1764 	mutex_enter(&ill->ill_lock);
1765 	switch (ill->ill_dlpi_fastpath_state) {
1766 	case IDS_FAILED:
1767 		/*
1768 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1769 		 * support it.
1770 		 */
1771 		mutex_exit(&ill->ill_lock);
1772 		return (ENOTSUP);
1773 	case IDS_UNKNOWN:
1774 		/* This is the first probe */
1775 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1776 		break;
1777 	default:
1778 		break;
1779 	}
1780 	mutex_exit(&ill->ill_lock);
1781 
1782 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1783 		return (EAGAIN);
1784 
1785 	mp->b_cont = copyb(dlur_mp);
1786 	if (mp->b_cont == NULL) {
1787 		freeb(mp);
1788 		return (EAGAIN);
1789 	}
1790 
1791 	ioc = (struct iocblk *)mp->b_rptr;
1792 	ioc->ioc_count = msgdsize(mp->b_cont);
1793 
1794 	putnext(ill->ill_wq, mp);
1795 	return (0);
1796 }
1797 
1798 void
1799 ill_capability_probe(ill_t *ill)
1800 {
1801 	/*
1802 	 * Do so only if negotiation is enabled, capabilities are unknown,
1803 	 * and a capability negotiation is not already in progress.
1804 	 */
1805 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN &&
1806 	    ill->ill_dlpi_capab_state != IDS_RENEG)
1807 		return;
1808 
1809 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1810 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1811 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1812 }
1813 
1814 void
1815 ill_capability_reset(ill_t *ill)
1816 {
1817 	mblk_t *sc_mp = NULL;
1818 	mblk_t *tmp;
1819 
1820 	/*
1821 	 * Note here that we reset the state to UNKNOWN, and later send
1822 	 * down the DL_CAPABILITY_REQ without first setting the state to
1823 	 * INPROGRESS.  We do this in order to distinguish the
1824 	 * DL_CAPABILITY_ACK response which may come back in response to
1825 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1826 	 * also handle the case where the driver doesn't send us back
1827 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1828 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1829 	 * features are turned off until the state reaches IDS_OK.
1830 	 */
1831 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1832 
1833 	/*
1834 	 * Disable sub-capabilities and request a list of sub-capability
1835 	 * messages which will be sent down to the driver.  Each handler
1836 	 * allocates the corresponding dl_capability_sub_t inside an
1837 	 * mblk, and links it to the existing sc_mp mblk, or return it
1838 	 * as sc_mp if it's the first sub-capability (the passed in
1839 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1840 	 * sc_mp will be pulled-up, before passing it downstream.
1841 	 */
1842 	ill_capability_mdt_reset(ill, &sc_mp);
1843 	ill_capability_hcksum_reset(ill, &sc_mp);
1844 	ill_capability_zerocopy_reset(ill, &sc_mp);
1845 	ill_capability_ipsec_reset(ill, &sc_mp);
1846 	ill_capability_dls_reset(ill, &sc_mp);
1847 	ill_capability_lso_reset(ill, &sc_mp);
1848 
1849 	/* Nothing to send down in order to disable the capabilities? */
1850 	if (sc_mp == NULL)
1851 		return;
1852 
1853 	tmp = msgpullup(sc_mp, -1);
1854 	freemsg(sc_mp);
1855 	if ((sc_mp = tmp) == NULL) {
1856 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1857 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1858 		return;
1859 	}
1860 
1861 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1862 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1863 }
1864 
1865 /*
1866  * Request or set new-style hardware capabilities supported by DLS provider.
1867  */
1868 static void
1869 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1870 {
1871 	mblk_t *mp;
1872 	dl_capability_req_t *capb;
1873 	size_t size = 0;
1874 	uint8_t *ptr;
1875 
1876 	if (reqp != NULL)
1877 		size = MBLKL(reqp);
1878 
1879 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1880 	if (mp == NULL) {
1881 		freemsg(reqp);
1882 		return;
1883 	}
1884 	ptr = mp->b_rptr;
1885 
1886 	capb = (dl_capability_req_t *)ptr;
1887 	ptr += sizeof (dl_capability_req_t);
1888 
1889 	if (reqp != NULL) {
1890 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1891 		capb->dl_sub_length = size;
1892 		bcopy(reqp->b_rptr, ptr, size);
1893 		ptr += size;
1894 		mp->b_cont = reqp->b_cont;
1895 		freeb(reqp);
1896 	}
1897 	ASSERT(ptr == mp->b_wptr);
1898 
1899 	ill_dlpi_send(ill, mp);
1900 }
1901 
1902 static void
1903 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1904 {
1905 	dl_capab_id_t *id_ic;
1906 	uint_t sub_dl_cap = outers->dl_cap;
1907 	dl_capability_sub_t *inners;
1908 	uint8_t *capend;
1909 
1910 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1911 
1912 	/*
1913 	 * Note: range checks here are not absolutely sufficient to
1914 	 * make us robust against malformed messages sent by drivers;
1915 	 * this is in keeping with the rest of IP's dlpi handling.
1916 	 * (Remember, it's coming from something else in the kernel
1917 	 * address space)
1918 	 */
1919 
1920 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1921 	if (capend > mp->b_wptr) {
1922 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1923 		    "malformed sub-capability too long for mblk");
1924 		return;
1925 	}
1926 
1927 	id_ic = (dl_capab_id_t *)(outers + 1);
1928 
1929 	if (outers->dl_length < sizeof (*id_ic) ||
1930 	    (inners = &id_ic->id_subcap,
1931 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1932 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1933 		    "encapsulated capab type %d too long for mblk",
1934 		    inners->dl_cap);
1935 		return;
1936 	}
1937 
1938 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1939 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1940 		    "isn't as expected; pass-thru module(s) detected, "
1941 		    "discarding capability\n", inners->dl_cap));
1942 		return;
1943 	}
1944 
1945 	/* Process the encapsulated sub-capability */
1946 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1947 }
1948 
1949 /*
1950  * Process Multidata Transmit capability negotiation ack received from a
1951  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1952  * DL_CAPABILITY_ACK message.
1953  */
1954 static void
1955 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1956 {
1957 	mblk_t *nmp = NULL;
1958 	dl_capability_req_t *oc;
1959 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1960 	ill_mdt_capab_t **ill_mdt_capab;
1961 	uint_t sub_dl_cap = isub->dl_cap;
1962 	uint8_t *capend;
1963 
1964 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1965 
1966 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1967 
1968 	/*
1969 	 * Note: range checks here are not absolutely sufficient to
1970 	 * make us robust against malformed messages sent by drivers;
1971 	 * this is in keeping with the rest of IP's dlpi handling.
1972 	 * (Remember, it's coming from something else in the kernel
1973 	 * address space)
1974 	 */
1975 
1976 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1977 	if (capend > mp->b_wptr) {
1978 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1979 		    "malformed sub-capability too long for mblk");
1980 		return;
1981 	}
1982 
1983 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1984 
1985 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1986 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1987 		    "unsupported MDT sub-capability (version %d, expected %d)",
1988 		    mdt_ic->mdt_version, MDT_VERSION_2);
1989 		return;
1990 	}
1991 
1992 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1993 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1994 		    "capability isn't as expected; pass-thru module(s) "
1995 		    "detected, discarding capability\n"));
1996 		return;
1997 	}
1998 
1999 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
2000 
2001 		if (*ill_mdt_capab == NULL) {
2002 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
2003 			    KM_NOSLEEP);
2004 
2005 			if (*ill_mdt_capab == NULL) {
2006 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2007 				    "could not enable MDT version %d "
2008 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
2009 				    ill->ill_name);
2010 				return;
2011 			}
2012 		}
2013 
2014 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
2015 		    "MDT version %d (%d bytes leading, %d bytes trailing "
2016 		    "header spaces, %d max pld bufs, %d span limit)\n",
2017 		    ill->ill_name, MDT_VERSION_2,
2018 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
2019 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
2020 
2021 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
2022 		(*ill_mdt_capab)->ill_mdt_on = 1;
2023 		/*
2024 		 * Round the following values to the nearest 32-bit; ULP
2025 		 * may further adjust them to accomodate for additional
2026 		 * protocol headers.  We pass these values to ULP during
2027 		 * bind time.
2028 		 */
2029 		(*ill_mdt_capab)->ill_mdt_hdr_head =
2030 		    roundup(mdt_ic->mdt_hdr_head, 4);
2031 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
2032 		    roundup(mdt_ic->mdt_hdr_tail, 4);
2033 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
2034 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
2035 
2036 		ill->ill_capabilities |= ILL_CAPAB_MDT;
2037 	} else {
2038 		uint_t size;
2039 		uchar_t *rptr;
2040 
2041 		size = sizeof (dl_capability_req_t) +
2042 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
2043 
2044 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2045 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2046 			    "could not enable MDT for %s (ENOMEM)\n",
2047 			    ill->ill_name);
2048 			return;
2049 		}
2050 
2051 		rptr = nmp->b_rptr;
2052 		/* initialize dl_capability_req_t */
2053 		oc = (dl_capability_req_t *)nmp->b_rptr;
2054 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2055 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2056 		    sizeof (dl_capab_mdt_t);
2057 		nmp->b_rptr += sizeof (dl_capability_req_t);
2058 
2059 		/* initialize dl_capability_sub_t */
2060 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2061 		nmp->b_rptr += sizeof (*isub);
2062 
2063 		/* initialize dl_capab_mdt_t */
2064 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2065 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2066 
2067 		nmp->b_rptr = rptr;
2068 
2069 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2070 		    "to enable MDT version %d\n", ill->ill_name,
2071 		    MDT_VERSION_2));
2072 
2073 		/* set ENABLE flag */
2074 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2075 
2076 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2077 		ill_dlpi_send(ill, nmp);
2078 	}
2079 }
2080 
2081 static void
2082 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2083 {
2084 	mblk_t *mp;
2085 	dl_capab_mdt_t *mdt_subcap;
2086 	dl_capability_sub_t *dl_subcap;
2087 	int size;
2088 
2089 	if (!ILL_MDT_CAPABLE(ill))
2090 		return;
2091 
2092 	ASSERT(ill->ill_mdt_capab != NULL);
2093 	/*
2094 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2095 	 * structure since it's possible that another thread is still
2096 	 * referring to it.  The structure only gets deallocated when
2097 	 * we destroy the ill.
2098 	 */
2099 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2100 
2101 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2102 
2103 	mp = allocb(size, BPRI_HI);
2104 	if (mp == NULL) {
2105 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2106 		    "request to disable MDT\n"));
2107 		return;
2108 	}
2109 
2110 	mp->b_wptr = mp->b_rptr + size;
2111 
2112 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2113 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2114 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2115 
2116 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2117 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2118 	mdt_subcap->mdt_flags = 0;
2119 	mdt_subcap->mdt_hdr_head = 0;
2120 	mdt_subcap->mdt_hdr_tail = 0;
2121 
2122 	if (*sc_mp != NULL)
2123 		linkb(*sc_mp, mp);
2124 	else
2125 		*sc_mp = mp;
2126 }
2127 
2128 /*
2129  * Send a DL_NOTIFY_REQ to the specified ill to enable
2130  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2131  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2132  * acceleration.
2133  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2134  */
2135 static boolean_t
2136 ill_enable_promisc_notify(ill_t *ill)
2137 {
2138 	mblk_t *mp;
2139 	dl_notify_req_t *req;
2140 
2141 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2142 
2143 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2144 	if (mp == NULL)
2145 		return (B_FALSE);
2146 
2147 	req = (dl_notify_req_t *)mp->b_rptr;
2148 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2149 	    DL_NOTE_PROMISC_OFF_PHYS;
2150 
2151 	ill_dlpi_send(ill, mp);
2152 
2153 	return (B_TRUE);
2154 }
2155 
2156 
2157 /*
2158  * Allocate an IPsec capability request which will be filled by our
2159  * caller to turn on support for one or more algorithms.
2160  */
2161 static mblk_t *
2162 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2163 {
2164 	mblk_t *nmp;
2165 	dl_capability_req_t	*ocap;
2166 	dl_capab_ipsec_t	*ocip;
2167 	dl_capab_ipsec_t	*icip;
2168 	uint8_t			*ptr;
2169 	icip = (dl_capab_ipsec_t *)(isub + 1);
2170 
2171 	/*
2172 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2173 	 * PROMISC_ON/OFF notification from the provider. We need to
2174 	 * do this before enabling the algorithms to avoid leakage of
2175 	 * cleartext packets.
2176 	 */
2177 
2178 	if (!ill_enable_promisc_notify(ill))
2179 		return (NULL);
2180 
2181 	/*
2182 	 * Allocate new mblk which will contain a new capability
2183 	 * request to enable the capabilities.
2184 	 */
2185 
2186 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2187 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2188 	if (nmp == NULL)
2189 		return (NULL);
2190 
2191 	ptr = nmp->b_rptr;
2192 
2193 	/* initialize dl_capability_req_t */
2194 	ocap = (dl_capability_req_t *)ptr;
2195 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2196 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2197 	ptr += sizeof (dl_capability_req_t);
2198 
2199 	/* initialize dl_capability_sub_t */
2200 	bcopy(isub, ptr, sizeof (*isub));
2201 	ptr += sizeof (*isub);
2202 
2203 	/* initialize dl_capab_ipsec_t */
2204 	ocip = (dl_capab_ipsec_t *)ptr;
2205 	bcopy(icip, ocip, sizeof (*icip));
2206 
2207 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2208 	return (nmp);
2209 }
2210 
2211 /*
2212  * Process an IPsec capability negotiation ack received from a DLS Provider.
2213  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2214  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2215  */
2216 static void
2217 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2218 {
2219 	dl_capab_ipsec_t	*icip;
2220 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2221 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2222 	uint_t cipher, nciphers;
2223 	mblk_t *nmp;
2224 	uint_t alg_len;
2225 	boolean_t need_sadb_dump;
2226 	uint_t sub_dl_cap = isub->dl_cap;
2227 	ill_ipsec_capab_t **ill_capab;
2228 	uint64_t ill_capab_flag;
2229 	uint8_t *capend, *ciphend;
2230 	boolean_t sadb_resync;
2231 
2232 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2233 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2234 
2235 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2236 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2237 		ill_capab_flag = ILL_CAPAB_AH;
2238 	} else {
2239 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2240 		ill_capab_flag = ILL_CAPAB_ESP;
2241 	}
2242 
2243 	/*
2244 	 * If the ill capability structure exists, then this incoming
2245 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2246 	 * If this is so, then we'd need to resynchronize the SADB
2247 	 * after re-enabling the offloaded ciphers.
2248 	 */
2249 	sadb_resync = (*ill_capab != NULL);
2250 
2251 	/*
2252 	 * Note: range checks here are not absolutely sufficient to
2253 	 * make us robust against malformed messages sent by drivers;
2254 	 * this is in keeping with the rest of IP's dlpi handling.
2255 	 * (Remember, it's coming from something else in the kernel
2256 	 * address space)
2257 	 */
2258 
2259 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2260 	if (capend > mp->b_wptr) {
2261 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2262 		    "malformed sub-capability too long for mblk");
2263 		return;
2264 	}
2265 
2266 	/*
2267 	 * There are two types of acks we process here:
2268 	 * 1. acks in reply to a (first form) generic capability req
2269 	 *    (no ENABLE flag set)
2270 	 * 2. acks in reply to a ENABLE capability req.
2271 	 *    (ENABLE flag set)
2272 	 *
2273 	 * We process the subcapability passed as argument as follows:
2274 	 * 1 do initializations
2275 	 *   1.1 initialize nmp = NULL
2276 	 *   1.2 set need_sadb_dump to B_FALSE
2277 	 * 2 for each cipher in subcapability:
2278 	 *   2.1 if ENABLE flag is set:
2279 	 *	2.1.1 update per-ill ipsec capabilities info
2280 	 *	2.1.2 set need_sadb_dump to B_TRUE
2281 	 *   2.2 if ENABLE flag is not set:
2282 	 *	2.2.1 if nmp is NULL:
2283 	 *		2.2.1.1 allocate and initialize nmp
2284 	 *		2.2.1.2 init current pos in nmp
2285 	 *	2.2.2 copy current cipher to current pos in nmp
2286 	 *	2.2.3 set ENABLE flag in nmp
2287 	 *	2.2.4 update current pos
2288 	 * 3 if nmp is not equal to NULL, send enable request
2289 	 *   3.1 send capability request
2290 	 * 4 if need_sadb_dump is B_TRUE
2291 	 *   4.1 enable promiscuous on/off notifications
2292 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2293 	 *	AH or ESP SA's to interface.
2294 	 */
2295 
2296 	nmp = NULL;
2297 	oalg = NULL;
2298 	need_sadb_dump = B_FALSE;
2299 	icip = (dl_capab_ipsec_t *)(isub + 1);
2300 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2301 
2302 	nciphers = icip->cip_nciphers;
2303 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2304 
2305 	if (ciphend > capend) {
2306 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2307 		    "too many ciphers for sub-capability len");
2308 		return;
2309 	}
2310 
2311 	for (cipher = 0; cipher < nciphers; cipher++) {
2312 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2313 
2314 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2315 			/*
2316 			 * TBD: when we provide a way to disable capabilities
2317 			 * from above, need to manage the request-pending state
2318 			 * and fail if we were not expecting this ACK.
2319 			 */
2320 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2321 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2322 
2323 			/*
2324 			 * Update IPsec capabilities for this ill
2325 			 */
2326 
2327 			if (*ill_capab == NULL) {
2328 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2329 				    ("ill_capability_ipsec_ack: "
2330 				    "allocating ipsec_capab for ill\n"));
2331 				*ill_capab = ill_ipsec_capab_alloc();
2332 
2333 				if (*ill_capab == NULL) {
2334 					cmn_err(CE_WARN,
2335 					    "ill_capability_ipsec_ack: "
2336 					    "could not enable IPsec Hardware "
2337 					    "acceleration for %s (ENOMEM)\n",
2338 					    ill->ill_name);
2339 					return;
2340 				}
2341 			}
2342 
2343 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2344 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2345 
2346 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2347 				cmn_err(CE_WARN,
2348 				    "ill_capability_ipsec_ack: "
2349 				    "malformed IPsec algorithm id %d",
2350 				    ialg->alg_prim);
2351 				continue;
2352 			}
2353 
2354 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2355 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2356 				    ialg->alg_prim);
2357 			} else {
2358 				ipsec_capab_algparm_t *alp;
2359 
2360 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2361 				    ialg->alg_prim);
2362 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2363 				    ialg->alg_prim)) {
2364 					cmn_err(CE_WARN,
2365 					    "ill_capability_ipsec_ack: "
2366 					    "no space for IPsec alg id %d",
2367 					    ialg->alg_prim);
2368 					continue;
2369 				}
2370 				alp = &((*ill_capab)->encr_algparm[
2371 				    ialg->alg_prim]);
2372 				alp->minkeylen = ialg->alg_minbits;
2373 				alp->maxkeylen = ialg->alg_maxbits;
2374 			}
2375 			ill->ill_capabilities |= ill_capab_flag;
2376 			/*
2377 			 * indicate that a capability was enabled, which
2378 			 * will be used below to kick off a SADB dump
2379 			 * to the ill.
2380 			 */
2381 			need_sadb_dump = B_TRUE;
2382 		} else {
2383 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2384 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2385 			    ialg->alg_prim));
2386 
2387 			if (nmp == NULL) {
2388 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2389 				if (nmp == NULL) {
2390 					/*
2391 					 * Sending the PROMISC_ON/OFF
2392 					 * notification request failed.
2393 					 * We cannot enable the algorithms
2394 					 * since the Provider will not
2395 					 * notify IP of promiscous mode
2396 					 * changes, which could lead
2397 					 * to leakage of packets.
2398 					 */
2399 					cmn_err(CE_WARN,
2400 					    "ill_capability_ipsec_ack: "
2401 					    "could not enable IPsec Hardware "
2402 					    "acceleration for %s (ENOMEM)\n",
2403 					    ill->ill_name);
2404 					return;
2405 				}
2406 				/* ptr to current output alg specifier */
2407 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2408 			}
2409 
2410 			/*
2411 			 * Copy current alg specifier, set ENABLE
2412 			 * flag, and advance to next output alg.
2413 			 * For now we enable all IPsec capabilities.
2414 			 */
2415 			ASSERT(oalg != NULL);
2416 			bcopy(ialg, oalg, alg_len);
2417 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2418 			nmp->b_wptr += alg_len;
2419 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2420 		}
2421 
2422 		/* move to next input algorithm specifier */
2423 		ialg = (dl_capab_ipsec_alg_t *)
2424 		    ((char *)ialg + alg_len);
2425 	}
2426 
2427 	if (nmp != NULL)
2428 		/*
2429 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2430 		 * IPsec hardware acceleration.
2431 		 */
2432 		ill_dlpi_send(ill, nmp);
2433 
2434 	if (need_sadb_dump)
2435 		/*
2436 		 * An acknowledgement corresponding to a request to
2437 		 * enable acceleration was received, notify SADB.
2438 		 */
2439 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2440 }
2441 
2442 /*
2443  * Given an mblk with enough space in it, create sub-capability entries for
2444  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2445  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2446  * in preparation for the reset the DL_CAPABILITY_REQ message.
2447  */
2448 static void
2449 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2450     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2451 {
2452 	dl_capab_ipsec_t *oipsec;
2453 	dl_capab_ipsec_alg_t *oalg;
2454 	dl_capability_sub_t *dl_subcap;
2455 	int i, k;
2456 
2457 	ASSERT(nciphers > 0);
2458 	ASSERT(ill_cap != NULL);
2459 	ASSERT(mp != NULL);
2460 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2461 
2462 	/* dl_capability_sub_t for "stype" */
2463 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2464 	dl_subcap->dl_cap = stype;
2465 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2466 	mp->b_wptr += sizeof (dl_capability_sub_t);
2467 
2468 	/* dl_capab_ipsec_t for "stype" */
2469 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2470 	oipsec->cip_version = 1;
2471 	oipsec->cip_nciphers = nciphers;
2472 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2473 
2474 	/* create entries for "stype" AUTH ciphers */
2475 	for (i = 0; i < ill_cap->algs_size; i++) {
2476 		for (k = 0; k < BITSPERBYTE; k++) {
2477 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2478 				continue;
2479 
2480 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2481 			bzero((void *)oalg, sizeof (*oalg));
2482 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2483 			oalg->alg_prim = k + (BITSPERBYTE * i);
2484 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2485 		}
2486 	}
2487 	/* create entries for "stype" ENCR ciphers */
2488 	for (i = 0; i < ill_cap->algs_size; i++) {
2489 		for (k = 0; k < BITSPERBYTE; k++) {
2490 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2491 				continue;
2492 
2493 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2494 			bzero((void *)oalg, sizeof (*oalg));
2495 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2496 			oalg->alg_prim = k + (BITSPERBYTE * i);
2497 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2498 		}
2499 	}
2500 }
2501 
2502 /*
2503  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2504  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2505  * POPC instruction, but our macro is more flexible for an arbitrary length
2506  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2507  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2508  * stays that way, we can reduce the number of iterations required.
2509  */
2510 #define	COUNT_1S(val, sum) {					\
2511 	uint8_t x = val & 0xff;					\
2512 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2513 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2514 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2515 }
2516 
2517 /* ARGSUSED */
2518 static void
2519 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2520 {
2521 	mblk_t *mp;
2522 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2523 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2524 	uint64_t ill_capabilities = ill->ill_capabilities;
2525 	int ah_cnt = 0, esp_cnt = 0;
2526 	int ah_len = 0, esp_len = 0;
2527 	int i, size = 0;
2528 
2529 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2530 		return;
2531 
2532 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2533 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2534 
2535 	/* Find out the number of ciphers for AH */
2536 	if (cap_ah != NULL) {
2537 		for (i = 0; i < cap_ah->algs_size; i++) {
2538 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2539 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2540 		}
2541 		if (ah_cnt > 0) {
2542 			size += sizeof (dl_capability_sub_t) +
2543 			    sizeof (dl_capab_ipsec_t);
2544 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2545 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2546 			size += ah_len;
2547 		}
2548 	}
2549 
2550 	/* Find out the number of ciphers for ESP */
2551 	if (cap_esp != NULL) {
2552 		for (i = 0; i < cap_esp->algs_size; i++) {
2553 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2554 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2555 		}
2556 		if (esp_cnt > 0) {
2557 			size += sizeof (dl_capability_sub_t) +
2558 			    sizeof (dl_capab_ipsec_t);
2559 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2560 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2561 			size += esp_len;
2562 		}
2563 	}
2564 
2565 	if (size == 0) {
2566 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2567 		    "there's nothing to reset\n"));
2568 		return;
2569 	}
2570 
2571 	mp = allocb(size, BPRI_HI);
2572 	if (mp == NULL) {
2573 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2574 		    "request to disable IPSEC Hardware Acceleration\n"));
2575 		return;
2576 	}
2577 
2578 	/*
2579 	 * Clear the capability flags for IPSec HA but retain the ill
2580 	 * capability structures since it's possible that another thread
2581 	 * is still referring to them.  The structures only get deallocated
2582 	 * when we destroy the ill.
2583 	 *
2584 	 * Various places check the flags to see if the ill is capable of
2585 	 * hardware acceleration, and by clearing them we ensure that new
2586 	 * outbound IPSec packets are sent down encrypted.
2587 	 */
2588 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2589 
2590 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2591 	if (ah_cnt > 0) {
2592 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2593 		    cap_ah, mp);
2594 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2595 	}
2596 
2597 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2598 	if (esp_cnt > 0) {
2599 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2600 		    cap_esp, mp);
2601 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2602 	}
2603 
2604 	/*
2605 	 * At this point we've composed a bunch of sub-capabilities to be
2606 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2607 	 * by the caller.  Upon receiving this reset message, the driver
2608 	 * must stop inbound decryption (by destroying all inbound SAs)
2609 	 * and let the corresponding packets come in encrypted.
2610 	 */
2611 
2612 	if (*sc_mp != NULL)
2613 		linkb(*sc_mp, mp);
2614 	else
2615 		*sc_mp = mp;
2616 }
2617 
2618 static void
2619 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2620     boolean_t encapsulated)
2621 {
2622 	boolean_t legacy = B_FALSE;
2623 
2624 	/*
2625 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2626 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2627 	 * instructed the driver to disable its advertised capabilities,
2628 	 * so there's no point in accepting any response at this moment.
2629 	 */
2630 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2631 		return;
2632 
2633 	/*
2634 	 * Note that only the following two sub-capabilities may be
2635 	 * considered as "legacy", since their original definitions
2636 	 * do not incorporate the dl_mid_t module ID token, and hence
2637 	 * may require the use of the wrapper sub-capability.
2638 	 */
2639 	switch (subp->dl_cap) {
2640 	case DL_CAPAB_IPSEC_AH:
2641 	case DL_CAPAB_IPSEC_ESP:
2642 		legacy = B_TRUE;
2643 		break;
2644 	}
2645 
2646 	/*
2647 	 * For legacy sub-capabilities which don't incorporate a queue_t
2648 	 * pointer in their structures, discard them if we detect that
2649 	 * there are intermediate modules in between IP and the driver.
2650 	 */
2651 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2652 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2653 		    "%d discarded; %d module(s) present below IP\n",
2654 		    subp->dl_cap, ill->ill_lmod_cnt));
2655 		return;
2656 	}
2657 
2658 	switch (subp->dl_cap) {
2659 	case DL_CAPAB_IPSEC_AH:
2660 	case DL_CAPAB_IPSEC_ESP:
2661 		ill_capability_ipsec_ack(ill, mp, subp);
2662 		break;
2663 	case DL_CAPAB_MDT:
2664 		ill_capability_mdt_ack(ill, mp, subp);
2665 		break;
2666 	case DL_CAPAB_HCKSUM:
2667 		ill_capability_hcksum_ack(ill, mp, subp);
2668 		break;
2669 	case DL_CAPAB_ZEROCOPY:
2670 		ill_capability_zerocopy_ack(ill, mp, subp);
2671 		break;
2672 	case DL_CAPAB_POLL:
2673 		if (!SOFT_RINGS_ENABLED())
2674 			ill_capability_dls_ack(ill, mp, subp);
2675 		break;
2676 	case DL_CAPAB_SOFT_RING:
2677 		if (SOFT_RINGS_ENABLED())
2678 			ill_capability_dls_ack(ill, mp, subp);
2679 		break;
2680 	case DL_CAPAB_LSO:
2681 		ill_capability_lso_ack(ill, mp, subp);
2682 		break;
2683 	default:
2684 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2685 		    subp->dl_cap));
2686 	}
2687 }
2688 
2689 /*
2690  * As part of negotiating polling capability, the driver tells us
2691  * the default (or normal) blanking interval and packet threshold
2692  * (the receive timer fires if blanking interval is reached or
2693  * the packet threshold is reached).
2694  *
2695  * As part of manipulating the polling interval, we always use our
2696  * estimated interval (avg service time * number of packets queued
2697  * on the squeue) but we try to blank for a minimum of
2698  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2699  * packet threshold during this time. When we are not in polling mode
2700  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2701  * rr_min_blank_ratio but up the packet cnt by a ratio of
2702  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2703  * possible although for a shorter interval.
2704  */
2705 #define	RR_MAX_BLANK_RATIO	20
2706 #define	RR_MIN_BLANK_RATIO	10
2707 #define	RR_MAX_PKT_CNT_RATIO	3
2708 #define	RR_MIN_PKT_CNT_RATIO	3
2709 
2710 /*
2711  * These can be tuned via /etc/system.
2712  */
2713 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2714 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2715 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2716 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2717 
2718 static mac_resource_handle_t
2719 ill_ring_add(void *arg, mac_resource_t *mrp)
2720 {
2721 	ill_t			*ill = (ill_t *)arg;
2722 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2723 	ill_rx_ring_t		*rx_ring;
2724 	int			ip_rx_index;
2725 
2726 	ASSERT(mrp != NULL);
2727 	if (mrp->mr_type != MAC_RX_FIFO) {
2728 		return (NULL);
2729 	}
2730 	ASSERT(ill != NULL);
2731 	ASSERT(ill->ill_dls_capab != NULL);
2732 
2733 	mutex_enter(&ill->ill_lock);
2734 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2735 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2736 		ASSERT(rx_ring != NULL);
2737 
2738 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2739 			time_t normal_blank_time =
2740 			    mrfp->mrf_normal_blank_time;
2741 			uint_t normal_pkt_cnt =
2742 			    mrfp->mrf_normal_pkt_count;
2743 
2744 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2745 
2746 	rx_ring->rr_blank = mrfp->mrf_blank;
2747 	rx_ring->rr_handle = mrfp->mrf_arg;
2748 	rx_ring->rr_ill = ill;
2749 	rx_ring->rr_normal_blank_time = normal_blank_time;
2750 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2751 
2752 			rx_ring->rr_max_blank_time =
2753 			    normal_blank_time * rr_max_blank_ratio;
2754 			rx_ring->rr_min_blank_time =
2755 			    normal_blank_time * rr_min_blank_ratio;
2756 			rx_ring->rr_max_pkt_cnt =
2757 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2758 			rx_ring->rr_min_pkt_cnt =
2759 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2760 
2761 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2762 			mutex_exit(&ill->ill_lock);
2763 
2764 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2765 			    (int), ip_rx_index);
2766 			return ((mac_resource_handle_t)rx_ring);
2767 		}
2768 	}
2769 
2770 	/*
2771 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2772 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2773 	 * should be made configurable. Meanwhile it cause no panic because
2774 	 * driver will pass ip_input a NULL handle which will make
2775 	 * IP allocate the default squeue and Polling mode will not
2776 	 * be used for this ring.
2777 	 */
2778 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2779 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2780 
2781 	mutex_exit(&ill->ill_lock);
2782 	return (NULL);
2783 }
2784 
2785 static boolean_t
2786 ill_capability_dls_init(ill_t *ill)
2787 {
2788 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2789 	conn_t 			*connp;
2790 	size_t			sz;
2791 	ip_stack_t *ipst = ill->ill_ipst;
2792 
2793 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2794 		if (ill_dls == NULL) {
2795 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2796 			    "soft_ring enabled for ill=%s (%p) but data "
2797 			    "structs uninitialized\n", ill->ill_name,
2798 			    (void *)ill);
2799 		}
2800 		return (B_TRUE);
2801 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2802 		if (ill_dls == NULL) {
2803 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2804 			    "polling enabled for ill=%s (%p) but data "
2805 			    "structs uninitialized\n", ill->ill_name,
2806 			    (void *)ill);
2807 		}
2808 		return (B_TRUE);
2809 	}
2810 
2811 	if (ill_dls != NULL) {
2812 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2813 		/* Soft_Ring or polling is being re-enabled */
2814 
2815 		connp = ill_dls->ill_unbind_conn;
2816 		ASSERT(rx_ring != NULL);
2817 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2818 		bzero((void *)rx_ring,
2819 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2820 		ill_dls->ill_ring_tbl = rx_ring;
2821 		ill_dls->ill_unbind_conn = connp;
2822 		return (B_TRUE);
2823 	}
2824 
2825 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2826 	    ipst->ips_netstack)) == NULL)
2827 		return (B_FALSE);
2828 
2829 	sz = sizeof (ill_dls_capab_t);
2830 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2831 
2832 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2833 	if (ill_dls == NULL) {
2834 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2835 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2836 		    (void *)ill);
2837 		CONN_DEC_REF(connp);
2838 		return (B_FALSE);
2839 	}
2840 
2841 	/* Allocate space to hold ring table */
2842 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2843 	ill->ill_dls_capab = ill_dls;
2844 	ill_dls->ill_unbind_conn = connp;
2845 	return (B_TRUE);
2846 }
2847 
2848 /*
2849  * ill_capability_dls_disable: disable soft_ring and/or polling
2850  * capability. Since any of the rings might already be in use, need
2851  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2852  * direct calls if necessary.
2853  */
2854 static void
2855 ill_capability_dls_disable(ill_t *ill)
2856 {
2857 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2858 
2859 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2860 		ip_squeue_clean_all(ill);
2861 		ill_dls->ill_tx = NULL;
2862 		ill_dls->ill_tx_handle = NULL;
2863 		ill_dls->ill_dls_change_status = NULL;
2864 		ill_dls->ill_dls_bind = NULL;
2865 		ill_dls->ill_dls_unbind = NULL;
2866 	}
2867 
2868 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2869 }
2870 
2871 static void
2872 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2873     dl_capability_sub_t *isub)
2874 {
2875 	uint_t			size;
2876 	uchar_t			*rptr;
2877 	dl_capab_dls_t	dls, *odls;
2878 	ill_dls_capab_t	*ill_dls;
2879 	mblk_t			*nmp = NULL;
2880 	dl_capability_req_t	*ocap;
2881 	uint_t			sub_dl_cap = isub->dl_cap;
2882 
2883 	if (!ill_capability_dls_init(ill))
2884 		return;
2885 	ill_dls = ill->ill_dls_capab;
2886 
2887 	/* Copy locally to get the members aligned */
2888 	bcopy((void *)idls, (void *)&dls,
2889 	    sizeof (dl_capab_dls_t));
2890 
2891 	/* Get the tx function and handle from dld */
2892 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2893 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2894 
2895 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2896 		ill_dls->ill_dls_change_status =
2897 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2898 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2899 		ill_dls->ill_dls_unbind =
2900 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2901 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2902 	}
2903 
2904 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2905 	    isub->dl_length;
2906 
2907 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2908 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2909 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2910 		    ill->ill_name, (void *)ill);
2911 		return;
2912 	}
2913 
2914 	/* initialize dl_capability_req_t */
2915 	rptr = nmp->b_rptr;
2916 	ocap = (dl_capability_req_t *)rptr;
2917 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2918 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2919 	rptr += sizeof (dl_capability_req_t);
2920 
2921 	/* initialize dl_capability_sub_t */
2922 	bcopy(isub, rptr, sizeof (*isub));
2923 	rptr += sizeof (*isub);
2924 
2925 	odls = (dl_capab_dls_t *)rptr;
2926 	rptr += sizeof (dl_capab_dls_t);
2927 
2928 	/* initialize dl_capab_dls_t to be sent down */
2929 	dls.dls_rx_handle = (uintptr_t)ill;
2930 	dls.dls_rx = (uintptr_t)ip_input;
2931 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2932 
2933 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2934 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2935 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2936 		dls.dls_flags = SOFT_RING_ENABLE;
2937 	} else {
2938 		dls.dls_flags = POLL_ENABLE;
2939 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2940 		    "to enable polling\n", ill->ill_name));
2941 	}
2942 	bcopy((void *)&dls, (void *)odls,
2943 	    sizeof (dl_capab_dls_t));
2944 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2945 	/*
2946 	 * nmp points to a DL_CAPABILITY_REQ message to
2947 	 * enable either soft_ring or polling
2948 	 */
2949 	ill_dlpi_send(ill, nmp);
2950 }
2951 
2952 static void
2953 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2954 {
2955 	mblk_t *mp;
2956 	dl_capab_dls_t *idls;
2957 	dl_capability_sub_t *dl_subcap;
2958 	int size;
2959 
2960 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2961 		return;
2962 
2963 	ASSERT(ill->ill_dls_capab != NULL);
2964 
2965 	size = sizeof (*dl_subcap) + sizeof (*idls);
2966 
2967 	mp = allocb(size, BPRI_HI);
2968 	if (mp == NULL) {
2969 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2970 		    "request to disable soft_ring\n"));
2971 		return;
2972 	}
2973 
2974 	mp->b_wptr = mp->b_rptr + size;
2975 
2976 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2977 	dl_subcap->dl_length = sizeof (*idls);
2978 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2979 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2980 	else
2981 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2982 
2983 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2984 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2985 		idls->dls_flags = SOFT_RING_DISABLE;
2986 	else
2987 		idls->dls_flags = POLL_DISABLE;
2988 
2989 	if (*sc_mp != NULL)
2990 		linkb(*sc_mp, mp);
2991 	else
2992 		*sc_mp = mp;
2993 }
2994 
2995 /*
2996  * Process a soft_ring/poll capability negotiation ack received
2997  * from a DLS Provider.isub must point to the sub-capability
2998  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2999  */
3000 static void
3001 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3002 {
3003 	dl_capab_dls_t		*idls;
3004 	uint_t			sub_dl_cap = isub->dl_cap;
3005 	uint8_t			*capend;
3006 
3007 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
3008 	    sub_dl_cap == DL_CAPAB_POLL);
3009 
3010 	if (ill->ill_isv6)
3011 		return;
3012 
3013 	/*
3014 	 * Note: range checks here are not absolutely sufficient to
3015 	 * make us robust against malformed messages sent by drivers;
3016 	 * this is in keeping with the rest of IP's dlpi handling.
3017 	 * (Remember, it's coming from something else in the kernel
3018 	 * address space)
3019 	 */
3020 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3021 	if (capend > mp->b_wptr) {
3022 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
3023 		    "malformed sub-capability too long for mblk");
3024 		return;
3025 	}
3026 
3027 	/*
3028 	 * There are two types of acks we process here:
3029 	 * 1. acks in reply to a (first form) generic capability req
3030 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
3031 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
3032 	 *    capability req.
3033 	 */
3034 	idls = (dl_capab_dls_t *)(isub + 1);
3035 
3036 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
3037 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
3038 		    "capability isn't as expected; pass-thru "
3039 		    "module(s) detected, discarding capability\n"));
3040 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
3041 			/*
3042 			 * This is a capability renegotitation case.
3043 			 * The interface better be unusable at this
3044 			 * point other wise bad things will happen
3045 			 * if we disable direct calls on a running
3046 			 * and up interface.
3047 			 */
3048 			ill_capability_dls_disable(ill);
3049 		}
3050 		return;
3051 	}
3052 
3053 	switch (idls->dls_flags) {
3054 	default:
3055 		/* Disable if unknown flag */
3056 	case SOFT_RING_DISABLE:
3057 	case POLL_DISABLE:
3058 		ill_capability_dls_disable(ill);
3059 		break;
3060 	case SOFT_RING_CAPABLE:
3061 	case POLL_CAPABLE:
3062 		/*
3063 		 * If the capability was already enabled, its safe
3064 		 * to disable it first to get rid of stale information
3065 		 * and then start enabling it again.
3066 		 */
3067 		ill_capability_dls_disable(ill);
3068 		ill_capability_dls_capable(ill, idls, isub);
3069 		break;
3070 	case SOFT_RING_ENABLE:
3071 	case POLL_ENABLE:
3072 		mutex_enter(&ill->ill_lock);
3073 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3074 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3075 			ASSERT(ill->ill_dls_capab != NULL);
3076 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3077 		}
3078 		if (sub_dl_cap == DL_CAPAB_POLL &&
3079 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3080 			ASSERT(ill->ill_dls_capab != NULL);
3081 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3082 			ip1dbg(("ill_capability_dls_ack: interface %s "
3083 			    "has enabled polling\n", ill->ill_name));
3084 		}
3085 		mutex_exit(&ill->ill_lock);
3086 		break;
3087 	}
3088 }
3089 
3090 /*
3091  * Process a hardware checksum offload capability negotiation ack received
3092  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3093  * of a DL_CAPABILITY_ACK message.
3094  */
3095 static void
3096 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3097 {
3098 	dl_capability_req_t	*ocap;
3099 	dl_capab_hcksum_t	*ihck, *ohck;
3100 	ill_hcksum_capab_t	**ill_hcksum;
3101 	mblk_t			*nmp = NULL;
3102 	uint_t			sub_dl_cap = isub->dl_cap;
3103 	uint8_t			*capend;
3104 
3105 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3106 
3107 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3108 
3109 	/*
3110 	 * Note: range checks here are not absolutely sufficient to
3111 	 * make us robust against malformed messages sent by drivers;
3112 	 * this is in keeping with the rest of IP's dlpi handling.
3113 	 * (Remember, it's coming from something else in the kernel
3114 	 * address space)
3115 	 */
3116 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3117 	if (capend > mp->b_wptr) {
3118 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3119 		    "malformed sub-capability too long for mblk");
3120 		return;
3121 	}
3122 
3123 	/*
3124 	 * There are two types of acks we process here:
3125 	 * 1. acks in reply to a (first form) generic capability req
3126 	 *    (no ENABLE flag set)
3127 	 * 2. acks in reply to a ENABLE capability req.
3128 	 *    (ENABLE flag set)
3129 	 */
3130 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3131 
3132 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3133 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3134 		    "unsupported hardware checksum "
3135 		    "sub-capability (version %d, expected %d)",
3136 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3137 		return;
3138 	}
3139 
3140 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3141 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3142 		    "checksum capability isn't as expected; pass-thru "
3143 		    "module(s) detected, discarding capability\n"));
3144 		return;
3145 	}
3146 
3147 #define	CURR_HCKSUM_CAPAB				\
3148 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3149 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3150 
3151 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3152 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3153 		/* do ENABLE processing */
3154 		if (*ill_hcksum == NULL) {
3155 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3156 			    KM_NOSLEEP);
3157 
3158 			if (*ill_hcksum == NULL) {
3159 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3160 				    "could not enable hcksum version %d "
3161 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3162 				    ill->ill_name);
3163 				return;
3164 			}
3165 		}
3166 
3167 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3168 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3169 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3170 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3171 		    "has enabled hardware checksumming\n ",
3172 		    ill->ill_name));
3173 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3174 		/*
3175 		 * Enabling hardware checksum offload
3176 		 * Currently IP supports {TCP,UDP}/IPv4
3177 		 * partial and full cksum offload and
3178 		 * IPv4 header checksum offload.
3179 		 * Allocate new mblk which will
3180 		 * contain a new capability request
3181 		 * to enable hardware checksum offload.
3182 		 */
3183 		uint_t	size;
3184 		uchar_t	*rptr;
3185 
3186 		size = sizeof (dl_capability_req_t) +
3187 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3188 
3189 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3190 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3191 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3192 			    ill->ill_name);
3193 			return;
3194 		}
3195 
3196 		rptr = nmp->b_rptr;
3197 		/* initialize dl_capability_req_t */
3198 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3199 		ocap->dl_sub_offset =
3200 		    sizeof (dl_capability_req_t);
3201 		ocap->dl_sub_length =
3202 		    sizeof (dl_capability_sub_t) +
3203 		    isub->dl_length;
3204 		nmp->b_rptr += sizeof (dl_capability_req_t);
3205 
3206 		/* initialize dl_capability_sub_t */
3207 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3208 		nmp->b_rptr += sizeof (*isub);
3209 
3210 		/* initialize dl_capab_hcksum_t */
3211 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3212 		bcopy(ihck, ohck, sizeof (*ihck));
3213 
3214 		nmp->b_rptr = rptr;
3215 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3216 
3217 		/* Set ENABLE flag */
3218 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3219 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3220 
3221 		/*
3222 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3223 		 * hardware checksum acceleration.
3224 		 */
3225 		ill_dlpi_send(ill, nmp);
3226 	} else {
3227 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3228 		    "advertised %x hardware checksum capability flags\n",
3229 		    ill->ill_name, ihck->hcksum_txflags));
3230 	}
3231 }
3232 
3233 static void
3234 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3235 {
3236 	mblk_t *mp;
3237 	dl_capab_hcksum_t *hck_subcap;
3238 	dl_capability_sub_t *dl_subcap;
3239 	int size;
3240 
3241 	if (!ILL_HCKSUM_CAPABLE(ill))
3242 		return;
3243 
3244 	ASSERT(ill->ill_hcksum_capab != NULL);
3245 	/*
3246 	 * Clear the capability flag for hardware checksum offload but
3247 	 * retain the ill_hcksum_capab structure since it's possible that
3248 	 * another thread is still referring to it.  The structure only
3249 	 * gets deallocated when we destroy the ill.
3250 	 */
3251 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3252 
3253 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3254 
3255 	mp = allocb(size, BPRI_HI);
3256 	if (mp == NULL) {
3257 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3258 		    "request to disable hardware checksum offload\n"));
3259 		return;
3260 	}
3261 
3262 	mp->b_wptr = mp->b_rptr + size;
3263 
3264 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3265 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3266 	dl_subcap->dl_length = sizeof (*hck_subcap);
3267 
3268 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3269 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3270 	hck_subcap->hcksum_txflags = 0;
3271 
3272 	if (*sc_mp != NULL)
3273 		linkb(*sc_mp, mp);
3274 	else
3275 		*sc_mp = mp;
3276 }
3277 
3278 static void
3279 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3280 {
3281 	mblk_t *nmp = NULL;
3282 	dl_capability_req_t *oc;
3283 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3284 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3285 	uint_t sub_dl_cap = isub->dl_cap;
3286 	uint8_t *capend;
3287 
3288 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3289 
3290 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3291 
3292 	/*
3293 	 * Note: range checks here are not absolutely sufficient to
3294 	 * make us robust against malformed messages sent by drivers;
3295 	 * this is in keeping with the rest of IP's dlpi handling.
3296 	 * (Remember, it's coming from something else in the kernel
3297 	 * address space)
3298 	 */
3299 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3300 	if (capend > mp->b_wptr) {
3301 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3302 		    "malformed sub-capability too long for mblk");
3303 		return;
3304 	}
3305 
3306 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3307 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3308 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3309 		    "unsupported ZEROCOPY sub-capability (version %d, "
3310 		    "expected %d)", zc_ic->zerocopy_version,
3311 		    ZEROCOPY_VERSION_1);
3312 		return;
3313 	}
3314 
3315 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3316 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3317 		    "capability isn't as expected; pass-thru module(s) "
3318 		    "detected, discarding capability\n"));
3319 		return;
3320 	}
3321 
3322 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3323 		if (*ill_zerocopy_capab == NULL) {
3324 			*ill_zerocopy_capab =
3325 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3326 			    KM_NOSLEEP);
3327 
3328 			if (*ill_zerocopy_capab == NULL) {
3329 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3330 				    "could not enable Zero-copy version %d "
3331 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3332 				    ill->ill_name);
3333 				return;
3334 			}
3335 		}
3336 
3337 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3338 		    "supports Zero-copy version %d\n", ill->ill_name,
3339 		    ZEROCOPY_VERSION_1));
3340 
3341 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3342 		    zc_ic->zerocopy_version;
3343 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3344 		    zc_ic->zerocopy_flags;
3345 
3346 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3347 	} else {
3348 		uint_t size;
3349 		uchar_t *rptr;
3350 
3351 		size = sizeof (dl_capability_req_t) +
3352 		    sizeof (dl_capability_sub_t) +
3353 		    sizeof (dl_capab_zerocopy_t);
3354 
3355 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3356 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3357 			    "could not enable zerocopy for %s (ENOMEM)\n",
3358 			    ill->ill_name);
3359 			return;
3360 		}
3361 
3362 		rptr = nmp->b_rptr;
3363 		/* initialize dl_capability_req_t */
3364 		oc = (dl_capability_req_t *)rptr;
3365 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3366 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3367 		    sizeof (dl_capab_zerocopy_t);
3368 		rptr += sizeof (dl_capability_req_t);
3369 
3370 		/* initialize dl_capability_sub_t */
3371 		bcopy(isub, rptr, sizeof (*isub));
3372 		rptr += sizeof (*isub);
3373 
3374 		/* initialize dl_capab_zerocopy_t */
3375 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3376 		*zc_oc = *zc_ic;
3377 
3378 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3379 		    "to enable zero-copy version %d\n", ill->ill_name,
3380 		    ZEROCOPY_VERSION_1));
3381 
3382 		/* set VMSAFE_MEM flag */
3383 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3384 
3385 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3386 		ill_dlpi_send(ill, nmp);
3387 	}
3388 }
3389 
3390 static void
3391 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3392 {
3393 	mblk_t *mp;
3394 	dl_capab_zerocopy_t *zerocopy_subcap;
3395 	dl_capability_sub_t *dl_subcap;
3396 	int size;
3397 
3398 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3399 		return;
3400 
3401 	ASSERT(ill->ill_zerocopy_capab != NULL);
3402 	/*
3403 	 * Clear the capability flag for Zero-copy but retain the
3404 	 * ill_zerocopy_capab structure since it's possible that another
3405 	 * thread is still referring to it.  The structure only gets
3406 	 * deallocated when we destroy the ill.
3407 	 */
3408 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3409 
3410 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3411 
3412 	mp = allocb(size, BPRI_HI);
3413 	if (mp == NULL) {
3414 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3415 		    "request to disable Zero-copy\n"));
3416 		return;
3417 	}
3418 
3419 	mp->b_wptr = mp->b_rptr + size;
3420 
3421 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3422 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3423 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3424 
3425 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3426 	zerocopy_subcap->zerocopy_version =
3427 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3428 	zerocopy_subcap->zerocopy_flags = 0;
3429 
3430 	if (*sc_mp != NULL)
3431 		linkb(*sc_mp, mp);
3432 	else
3433 		*sc_mp = mp;
3434 }
3435 
3436 /*
3437  * Process Large Segment Offload capability negotiation ack received from a
3438  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3439  * DL_CAPABILITY_ACK message.
3440  */
3441 static void
3442 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3443 {
3444 	mblk_t *nmp = NULL;
3445 	dl_capability_req_t *oc;
3446 	dl_capab_lso_t *lso_ic, *lso_oc;
3447 	ill_lso_capab_t **ill_lso_capab;
3448 	uint_t sub_dl_cap = isub->dl_cap;
3449 	uint8_t *capend;
3450 
3451 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3452 
3453 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3454 
3455 	/*
3456 	 * Note: range checks here are not absolutely sufficient to
3457 	 * make us robust against malformed messages sent by drivers;
3458 	 * this is in keeping with the rest of IP's dlpi handling.
3459 	 * (Remember, it's coming from something else in the kernel
3460 	 * address space)
3461 	 */
3462 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3463 	if (capend > mp->b_wptr) {
3464 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3465 		    "malformed sub-capability too long for mblk");
3466 		return;
3467 	}
3468 
3469 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3470 
3471 	if (lso_ic->lso_version != LSO_VERSION_1) {
3472 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3473 		    "unsupported LSO sub-capability (version %d, expected %d)",
3474 		    lso_ic->lso_version, LSO_VERSION_1);
3475 		return;
3476 	}
3477 
3478 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3479 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3480 		    "capability isn't as expected; pass-thru module(s) "
3481 		    "detected, discarding capability\n"));
3482 		return;
3483 	}
3484 
3485 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3486 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3487 		if (*ill_lso_capab == NULL) {
3488 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3489 			    KM_NOSLEEP);
3490 
3491 			if (*ill_lso_capab == NULL) {
3492 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3493 				    "could not enable LSO version %d "
3494 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3495 				    ill->ill_name);
3496 				return;
3497 			}
3498 		}
3499 
3500 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3501 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3502 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3503 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3504 
3505 		ip1dbg(("ill_capability_lso_ack: interface %s "
3506 		    "has enabled LSO\n ", ill->ill_name));
3507 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3508 		uint_t size;
3509 		uchar_t *rptr;
3510 
3511 		size = sizeof (dl_capability_req_t) +
3512 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3513 
3514 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3515 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3516 			    "could not enable LSO for %s (ENOMEM)\n",
3517 			    ill->ill_name);
3518 			return;
3519 		}
3520 
3521 		rptr = nmp->b_rptr;
3522 		/* initialize dl_capability_req_t */
3523 		oc = (dl_capability_req_t *)nmp->b_rptr;
3524 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3525 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3526 		    sizeof (dl_capab_lso_t);
3527 		nmp->b_rptr += sizeof (dl_capability_req_t);
3528 
3529 		/* initialize dl_capability_sub_t */
3530 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3531 		nmp->b_rptr += sizeof (*isub);
3532 
3533 		/* initialize dl_capab_lso_t */
3534 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3535 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3536 
3537 		nmp->b_rptr = rptr;
3538 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3539 
3540 		/* set ENABLE flag */
3541 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3542 
3543 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3544 		ill_dlpi_send(ill, nmp);
3545 	} else {
3546 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3547 		    "advertised %x LSO capability flags\n",
3548 		    ill->ill_name, lso_ic->lso_flags));
3549 	}
3550 }
3551 
3552 
3553 static void
3554 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3555 {
3556 	mblk_t *mp;
3557 	dl_capab_lso_t *lso_subcap;
3558 	dl_capability_sub_t *dl_subcap;
3559 	int size;
3560 
3561 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3562 		return;
3563 
3564 	ASSERT(ill->ill_lso_capab != NULL);
3565 	/*
3566 	 * Clear the capability flag for LSO but retain the
3567 	 * ill_lso_capab structure since it's possible that another
3568 	 * thread is still referring to it.  The structure only gets
3569 	 * deallocated when we destroy the ill.
3570 	 */
3571 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3572 
3573 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3574 
3575 	mp = allocb(size, BPRI_HI);
3576 	if (mp == NULL) {
3577 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3578 		    "request to disable LSO\n"));
3579 		return;
3580 	}
3581 
3582 	mp->b_wptr = mp->b_rptr + size;
3583 
3584 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3585 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3586 	dl_subcap->dl_length = sizeof (*lso_subcap);
3587 
3588 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3589 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3590 	lso_subcap->lso_flags = 0;
3591 
3592 	if (*sc_mp != NULL)
3593 		linkb(*sc_mp, mp);
3594 	else
3595 		*sc_mp = mp;
3596 }
3597 
3598 /*
3599  * Consume a new-style hardware capabilities negotiation ack.
3600  * Called from ip_rput_dlpi_writer().
3601  */
3602 void
3603 ill_capability_ack(ill_t *ill, mblk_t *mp)
3604 {
3605 	dl_capability_ack_t *capp;
3606 	dl_capability_sub_t *subp, *endp;
3607 
3608 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3609 		ill->ill_dlpi_capab_state = IDS_OK;
3610 
3611 	capp = (dl_capability_ack_t *)mp->b_rptr;
3612 
3613 	if (capp->dl_sub_length == 0)
3614 		/* no new-style capabilities */
3615 		return;
3616 
3617 	/* make sure the driver supplied correct dl_sub_length */
3618 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3619 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3620 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3621 		return;
3622 	}
3623 
3624 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3625 	/*
3626 	 * There are sub-capabilities. Process the ones we know about.
3627 	 * Loop until we don't have room for another sub-cap header..
3628 	 */
3629 	for (subp = SC(capp, capp->dl_sub_offset),
3630 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3631 	    subp <= endp;
3632 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3633 
3634 		switch (subp->dl_cap) {
3635 		case DL_CAPAB_ID_WRAPPER:
3636 			ill_capability_id_ack(ill, mp, subp);
3637 			break;
3638 		default:
3639 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3640 			break;
3641 		}
3642 	}
3643 #undef SC
3644 }
3645 
3646 /*
3647  * This routine is called to scan the fragmentation reassembly table for
3648  * the specified ILL for any packets that are starting to smell.
3649  * dead_interval is the maximum time in seconds that will be tolerated.  It
3650  * will either be the value specified in ip_g_frag_timeout, or zero if the
3651  * ILL is shutting down and it is time to blow everything off.
3652  *
3653  * It returns the number of seconds (as a time_t) that the next frag timer
3654  * should be scheduled for, 0 meaning that the timer doesn't need to be
3655  * re-started.  Note that the method of calculating next_timeout isn't
3656  * entirely accurate since time will flow between the time we grab
3657  * current_time and the time we schedule the next timeout.  This isn't a
3658  * big problem since this is the timer for sending an ICMP reassembly time
3659  * exceeded messages, and it doesn't have to be exactly accurate.
3660  *
3661  * This function is
3662  * sometimes called as writer, although this is not required.
3663  */
3664 time_t
3665 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3666 {
3667 	ipfb_t	*ipfb;
3668 	ipfb_t	*endp;
3669 	ipf_t	*ipf;
3670 	ipf_t	*ipfnext;
3671 	mblk_t	*mp;
3672 	time_t	current_time = gethrestime_sec();
3673 	time_t	next_timeout = 0;
3674 	uint32_t	hdr_length;
3675 	mblk_t	*send_icmp_head;
3676 	mblk_t	*send_icmp_head_v6;
3677 	zoneid_t zoneid;
3678 	ip_stack_t *ipst = ill->ill_ipst;
3679 
3680 	ipfb = ill->ill_frag_hash_tbl;
3681 	if (ipfb == NULL)
3682 		return (B_FALSE);
3683 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3684 	/* Walk the frag hash table. */
3685 	for (; ipfb < endp; ipfb++) {
3686 		send_icmp_head = NULL;
3687 		send_icmp_head_v6 = NULL;
3688 		mutex_enter(&ipfb->ipfb_lock);
3689 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3690 			time_t frag_time = current_time - ipf->ipf_timestamp;
3691 			time_t frag_timeout;
3692 
3693 			if (frag_time < dead_interval) {
3694 				/*
3695 				 * There are some outstanding fragments
3696 				 * that will timeout later.  Make note of
3697 				 * the time so that we can reschedule the
3698 				 * next timeout appropriately.
3699 				 */
3700 				frag_timeout = dead_interval - frag_time;
3701 				if (next_timeout == 0 ||
3702 				    frag_timeout < next_timeout) {
3703 					next_timeout = frag_timeout;
3704 				}
3705 				break;
3706 			}
3707 			/* Time's up.  Get it out of here. */
3708 			hdr_length = ipf->ipf_nf_hdr_len;
3709 			ipfnext = ipf->ipf_hash_next;
3710 			if (ipfnext)
3711 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3712 			*ipf->ipf_ptphn = ipfnext;
3713 			mp = ipf->ipf_mp->b_cont;
3714 			for (; mp; mp = mp->b_cont) {
3715 				/* Extra points for neatness. */
3716 				IP_REASS_SET_START(mp, 0);
3717 				IP_REASS_SET_END(mp, 0);
3718 			}
3719 			mp = ipf->ipf_mp->b_cont;
3720 			ill->ill_frag_count -= ipf->ipf_count;
3721 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3722 			ipfb->ipfb_count -= ipf->ipf_count;
3723 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3724 			ipfb->ipfb_frag_pkts--;
3725 			/*
3726 			 * We do not send any icmp message from here because
3727 			 * we currently are holding the ipfb_lock for this
3728 			 * hash chain. If we try and send any icmp messages
3729 			 * from here we may end up via a put back into ip
3730 			 * trying to get the same lock, causing a recursive
3731 			 * mutex panic. Instead we build a list and send all
3732 			 * the icmp messages after we have dropped the lock.
3733 			 */
3734 			if (ill->ill_isv6) {
3735 				if (hdr_length != 0) {
3736 					mp->b_next = send_icmp_head_v6;
3737 					send_icmp_head_v6 = mp;
3738 				} else {
3739 					freemsg(mp);
3740 				}
3741 			} else {
3742 				if (hdr_length != 0) {
3743 					mp->b_next = send_icmp_head;
3744 					send_icmp_head = mp;
3745 				} else {
3746 					freemsg(mp);
3747 				}
3748 			}
3749 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3750 			freeb(ipf->ipf_mp);
3751 		}
3752 		mutex_exit(&ipfb->ipfb_lock);
3753 		/*
3754 		 * Now need to send any icmp messages that we delayed from
3755 		 * above.
3756 		 */
3757 		while (send_icmp_head_v6 != NULL) {
3758 			ip6_t *ip6h;
3759 
3760 			mp = send_icmp_head_v6;
3761 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3762 			mp->b_next = NULL;
3763 			if (mp->b_datap->db_type == M_CTL)
3764 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3765 			else
3766 				ip6h = (ip6_t *)mp->b_rptr;
3767 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3768 			    ill, ipst);
3769 			if (zoneid == ALL_ZONES) {
3770 				freemsg(mp);
3771 			} else {
3772 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3773 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3774 				    B_FALSE, zoneid, ipst);
3775 			}
3776 		}
3777 		while (send_icmp_head != NULL) {
3778 			ipaddr_t dst;
3779 
3780 			mp = send_icmp_head;
3781 			send_icmp_head = send_icmp_head->b_next;
3782 			mp->b_next = NULL;
3783 
3784 			if (mp->b_datap->db_type == M_CTL)
3785 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3786 			else
3787 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3788 
3789 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3790 			if (zoneid == ALL_ZONES) {
3791 				freemsg(mp);
3792 			} else {
3793 				icmp_time_exceeded(ill->ill_wq, mp,
3794 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3795 				    ipst);
3796 			}
3797 		}
3798 	}
3799 	/*
3800 	 * A non-dying ILL will use the return value to decide whether to
3801 	 * restart the frag timer, and for how long.
3802 	 */
3803 	return (next_timeout);
3804 }
3805 
3806 /*
3807  * This routine is called when the approximate count of mblk memory used
3808  * for the specified ILL has exceeded max_count.
3809  */
3810 void
3811 ill_frag_prune(ill_t *ill, uint_t max_count)
3812 {
3813 	ipfb_t	*ipfb;
3814 	ipf_t	*ipf;
3815 	size_t	count;
3816 
3817 	/*
3818 	 * If we are here within ip_min_frag_prune_time msecs remove
3819 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3820 	 * ill_frag_free_num_pkts.
3821 	 */
3822 	mutex_enter(&ill->ill_lock);
3823 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3824 	    (ip_min_frag_prune_time != 0 ?
3825 	    ip_min_frag_prune_time : msec_per_tick)) {
3826 
3827 		ill->ill_frag_free_num_pkts++;
3828 
3829 	} else {
3830 		ill->ill_frag_free_num_pkts = 0;
3831 	}
3832 	ill->ill_last_frag_clean_time = lbolt;
3833 	mutex_exit(&ill->ill_lock);
3834 
3835 	/*
3836 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3837 	 */
3838 	if (ill->ill_frag_free_num_pkts != 0) {
3839 		int ix;
3840 
3841 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3842 			ipfb = &ill->ill_frag_hash_tbl[ix];
3843 			mutex_enter(&ipfb->ipfb_lock);
3844 			if (ipfb->ipfb_ipf != NULL) {
3845 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3846 				    ill->ill_frag_free_num_pkts);
3847 			}
3848 			mutex_exit(&ipfb->ipfb_lock);
3849 		}
3850 	}
3851 	/*
3852 	 * While the reassembly list for this ILL is too big, prune a fragment
3853 	 * queue by age, oldest first.  Note that the per ILL count is
3854 	 * approximate, while the per frag hash bucket counts are accurate.
3855 	 */
3856 	while (ill->ill_frag_count > max_count) {
3857 		int	ix;
3858 		ipfb_t	*oipfb = NULL;
3859 		uint_t	oldest = UINT_MAX;
3860 
3861 		count = 0;
3862 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3863 			ipfb = &ill->ill_frag_hash_tbl[ix];
3864 			mutex_enter(&ipfb->ipfb_lock);
3865 			ipf = ipfb->ipfb_ipf;
3866 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3867 				oldest = ipf->ipf_gen;
3868 				oipfb = ipfb;
3869 			}
3870 			count += ipfb->ipfb_count;
3871 			mutex_exit(&ipfb->ipfb_lock);
3872 		}
3873 		/* Refresh the per ILL count */
3874 		ill->ill_frag_count = count;
3875 		if (oipfb == NULL) {
3876 			ill->ill_frag_count = 0;
3877 			break;
3878 		}
3879 		if (count <= max_count)
3880 			return;	/* Somebody beat us to it, nothing to do */
3881 		mutex_enter(&oipfb->ipfb_lock);
3882 		ipf = oipfb->ipfb_ipf;
3883 		if (ipf != NULL) {
3884 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3885 		}
3886 		mutex_exit(&oipfb->ipfb_lock);
3887 	}
3888 }
3889 
3890 /*
3891  * free 'free_cnt' fragmented packets starting at ipf.
3892  */
3893 void
3894 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3895 {
3896 	size_t	count;
3897 	mblk_t	*mp;
3898 	mblk_t	*tmp;
3899 	ipf_t **ipfp = ipf->ipf_ptphn;
3900 
3901 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3902 	ASSERT(ipfp != NULL);
3903 	ASSERT(ipf != NULL);
3904 
3905 	while (ipf != NULL && free_cnt-- > 0) {
3906 		count = ipf->ipf_count;
3907 		mp = ipf->ipf_mp;
3908 		ipf = ipf->ipf_hash_next;
3909 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3910 			IP_REASS_SET_START(tmp, 0);
3911 			IP_REASS_SET_END(tmp, 0);
3912 		}
3913 		ill->ill_frag_count -= count;
3914 		ASSERT(ipfb->ipfb_count >= count);
3915 		ipfb->ipfb_count -= count;
3916 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3917 		ipfb->ipfb_frag_pkts--;
3918 		freemsg(mp);
3919 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3920 	}
3921 
3922 	if (ipf)
3923 		ipf->ipf_ptphn = ipfp;
3924 	ipfp[0] = ipf;
3925 }
3926 
3927 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3928 	"obsolete and may be removed in a future release of Solaris.  Use " \
3929 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3930 
3931 /*
3932  * For obsolete per-interface forwarding configuration;
3933  * called in response to ND_GET.
3934  */
3935 /* ARGSUSED */
3936 static int
3937 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3938 {
3939 	ill_t *ill = (ill_t *)cp;
3940 
3941 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3942 
3943 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3944 	return (0);
3945 }
3946 
3947 /*
3948  * For obsolete per-interface forwarding configuration;
3949  * called in response to ND_SET.
3950  */
3951 /* ARGSUSED */
3952 static int
3953 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3954     cred_t *ioc_cr)
3955 {
3956 	long value;
3957 	int retval;
3958 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3959 
3960 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3961 
3962 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3963 	    value < 0 || value > 1) {
3964 		return (EINVAL);
3965 	}
3966 
3967 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3968 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3969 	rw_exit(&ipst->ips_ill_g_lock);
3970 	return (retval);
3971 }
3972 
3973 /*
3974  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3975  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3976  * up RTS_IFINFO routing socket messages for each interface whose flags we
3977  * change.
3978  */
3979 int
3980 ill_forward_set(ill_t *ill, boolean_t enable)
3981 {
3982 	ill_group_t *illgrp;
3983 	ip_stack_t	*ipst = ill->ill_ipst;
3984 
3985 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3986 
3987 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3988 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3989 		return (0);
3990 
3991 	if (IS_LOOPBACK(ill))
3992 		return (EINVAL);
3993 
3994 	/*
3995 	 * If the ill is in an IPMP group, set the forwarding policy on all
3996 	 * members of the group to the same value.
3997 	 */
3998 	illgrp = ill->ill_group;
3999 	if (illgrp != NULL) {
4000 		ill_t *tmp_ill;
4001 
4002 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
4003 		    tmp_ill = tmp_ill->ill_group_next) {
4004 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
4005 			    (enable ? "Enabling" : "Disabling"),
4006 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
4007 			    tmp_ill->ill_name));
4008 			mutex_enter(&tmp_ill->ill_lock);
4009 			if (enable)
4010 				tmp_ill->ill_flags |= ILLF_ROUTER;
4011 			else
4012 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
4013 			mutex_exit(&tmp_ill->ill_lock);
4014 			if (tmp_ill->ill_isv6)
4015 				ill_set_nce_router_flags(tmp_ill, enable);
4016 			/* Notify routing socket listeners of this change. */
4017 			ip_rts_ifmsg(tmp_ill->ill_ipif);
4018 		}
4019 	} else {
4020 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
4021 		    (enable ? "Enabling" : "Disabling"),
4022 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
4023 		mutex_enter(&ill->ill_lock);
4024 		if (enable)
4025 			ill->ill_flags |= ILLF_ROUTER;
4026 		else
4027 			ill->ill_flags &= ~ILLF_ROUTER;
4028 		mutex_exit(&ill->ill_lock);
4029 		if (ill->ill_isv6)
4030 			ill_set_nce_router_flags(ill, enable);
4031 		/* Notify routing socket listeners of this change. */
4032 		ip_rts_ifmsg(ill->ill_ipif);
4033 	}
4034 
4035 	return (0);
4036 }
4037 
4038 /*
4039  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
4040  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
4041  * set or clear.
4042  */
4043 static void
4044 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
4045 {
4046 	ipif_t *ipif;
4047 	nce_t *nce;
4048 
4049 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4050 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
4051 		if (nce != NULL) {
4052 			mutex_enter(&nce->nce_lock);
4053 			if (enable)
4054 				nce->nce_flags |= NCE_F_ISROUTER;
4055 			else
4056 				nce->nce_flags &= ~NCE_F_ISROUTER;
4057 			mutex_exit(&nce->nce_lock);
4058 			NCE_REFRELE(nce);
4059 		}
4060 	}
4061 }
4062 
4063 /*
4064  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
4065  * for this ill.  Make sure the v6/v4 question has been answered about this
4066  * ill.  The creation of this ndd variable is only for backwards compatibility.
4067  * The preferred way to control per-interface IP forwarding is through the
4068  * ILLF_ROUTER interface flag.
4069  */
4070 static int
4071 ill_set_ndd_name(ill_t *ill)
4072 {
4073 	char *suffix;
4074 	ip_stack_t	*ipst = ill->ill_ipst;
4075 
4076 	ASSERT(IAM_WRITER_ILL(ill));
4077 
4078 	if (ill->ill_isv6)
4079 		suffix = ipv6_forward_suffix;
4080 	else
4081 		suffix = ipv4_forward_suffix;
4082 
4083 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4084 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4085 	/*
4086 	 * Copies over the '\0'.
4087 	 * Note that strlen(suffix) is always bounded.
4088 	 */
4089 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4090 	    strlen(suffix) + 1);
4091 
4092 	/*
4093 	 * Use of the nd table requires holding the reader lock.
4094 	 * Modifying the nd table thru nd_load/nd_unload requires
4095 	 * the writer lock.
4096 	 */
4097 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4098 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4099 	    nd_ill_forward_set, (caddr_t)ill)) {
4100 		/*
4101 		 * If the nd_load failed, it only meant that it could not
4102 		 * allocate a new bunch of room for further NDD expansion.
4103 		 * Because of that, the ill_ndd_name will be set to 0, and
4104 		 * this interface is at the mercy of the global ip_forwarding
4105 		 * variable.
4106 		 */
4107 		rw_exit(&ipst->ips_ip_g_nd_lock);
4108 		ill->ill_ndd_name = NULL;
4109 		return (ENOMEM);
4110 	}
4111 	rw_exit(&ipst->ips_ip_g_nd_lock);
4112 	return (0);
4113 }
4114 
4115 /*
4116  * Intializes the context structure and returns the first ill in the list
4117  * cuurently start_list and end_list can have values:
4118  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4119  * IP_V4_G_HEAD		Traverse IPV4 list only.
4120  * IP_V6_G_HEAD		Traverse IPV6 list only.
4121  */
4122 
4123 /*
4124  * We don't check for CONDEMNED ills here. Caller must do that if
4125  * necessary under the ill lock.
4126  */
4127 ill_t *
4128 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4129     ip_stack_t *ipst)
4130 {
4131 	ill_if_t *ifp;
4132 	ill_t *ill;
4133 	avl_tree_t *avl_tree;
4134 
4135 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4136 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4137 
4138 	/*
4139 	 * setup the lists to search
4140 	 */
4141 	if (end_list != MAX_G_HEADS) {
4142 		ctx->ctx_current_list = start_list;
4143 		ctx->ctx_last_list = end_list;
4144 	} else {
4145 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4146 		ctx->ctx_current_list = 0;
4147 	}
4148 
4149 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4150 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4151 		if (ifp != (ill_if_t *)
4152 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4153 			avl_tree = &ifp->illif_avl_by_ppa;
4154 			ill = avl_first(avl_tree);
4155 			/*
4156 			 * ill is guaranteed to be non NULL or ifp should have
4157 			 * not existed.
4158 			 */
4159 			ASSERT(ill != NULL);
4160 			return (ill);
4161 		}
4162 		ctx->ctx_current_list++;
4163 	}
4164 
4165 	return (NULL);
4166 }
4167 
4168 /*
4169  * returns the next ill in the list. ill_first() must have been called
4170  * before calling ill_next() or bad things will happen.
4171  */
4172 
4173 /*
4174  * We don't check for CONDEMNED ills here. Caller must do that if
4175  * necessary under the ill lock.
4176  */
4177 ill_t *
4178 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4179 {
4180 	ill_if_t *ifp;
4181 	ill_t *ill;
4182 	ip_stack_t	*ipst = lastill->ill_ipst;
4183 
4184 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4185 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4186 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4187 	    AVL_AFTER)) != NULL) {
4188 		return (ill);
4189 	}
4190 
4191 	/* goto next ill_ifp in the list. */
4192 	ifp = lastill->ill_ifptr->illif_next;
4193 
4194 	/* make sure not at end of circular list */
4195 	while (ifp ==
4196 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4197 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4198 			return (NULL);
4199 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4200 	}
4201 
4202 	return (avl_first(&ifp->illif_avl_by_ppa));
4203 }
4204 
4205 /*
4206  * Check interface name for correct format which is name+ppa.
4207  * name can contain characters and digits, the right most digits
4208  * make up the ppa number. use of octal is not allowed, name must contain
4209  * a ppa, return pointer to the start of ppa.
4210  * In case of error return NULL.
4211  */
4212 static char *
4213 ill_get_ppa_ptr(char *name)
4214 {
4215 	int namelen = mi_strlen(name);
4216 
4217 	int len = namelen;
4218 
4219 	name += len;
4220 	while (len > 0) {
4221 		name--;
4222 		if (*name < '0' || *name > '9')
4223 			break;
4224 		len--;
4225 	}
4226 
4227 	/* empty string, all digits, or no trailing digits */
4228 	if (len == 0 || len == (int)namelen)
4229 		return (NULL);
4230 
4231 	name++;
4232 	/* check for attempted use of octal */
4233 	if (*name == '0' && len != (int)namelen - 1)
4234 		return (NULL);
4235 	return (name);
4236 }
4237 
4238 /*
4239  * use avl tree to locate the ill.
4240  */
4241 static ill_t *
4242 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4243     ipsq_func_t func, int *error, ip_stack_t *ipst)
4244 {
4245 	char *ppa_ptr = NULL;
4246 	int len;
4247 	uint_t ppa;
4248 	ill_t *ill = NULL;
4249 	ill_if_t *ifp;
4250 	int list;
4251 	ipsq_t *ipsq;
4252 
4253 	if (error != NULL)
4254 		*error = 0;
4255 
4256 	/*
4257 	 * get ppa ptr
4258 	 */
4259 	if (isv6)
4260 		list = IP_V6_G_HEAD;
4261 	else
4262 		list = IP_V4_G_HEAD;
4263 
4264 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4265 		if (error != NULL)
4266 			*error = ENXIO;
4267 		return (NULL);
4268 	}
4269 
4270 	len = ppa_ptr - name + 1;
4271 
4272 	ppa = stoi(&ppa_ptr);
4273 
4274 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4275 
4276 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4277 		/*
4278 		 * match is done on len - 1 as the name is not null
4279 		 * terminated it contains ppa in addition to the interface
4280 		 * name.
4281 		 */
4282 		if ((ifp->illif_name_len == len) &&
4283 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4284 			break;
4285 		} else {
4286 			ifp = ifp->illif_next;
4287 		}
4288 	}
4289 
4290 
4291 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4292 		/*
4293 		 * Even the interface type does not exist.
4294 		 */
4295 		if (error != NULL)
4296 			*error = ENXIO;
4297 		return (NULL);
4298 	}
4299 
4300 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4301 	if (ill != NULL) {
4302 		/*
4303 		 * The block comment at the start of ipif_down
4304 		 * explains the use of the macros used below
4305 		 */
4306 		GRAB_CONN_LOCK(q);
4307 		mutex_enter(&ill->ill_lock);
4308 		if (ILL_CAN_LOOKUP(ill)) {
4309 			ill_refhold_locked(ill);
4310 			mutex_exit(&ill->ill_lock);
4311 			RELEASE_CONN_LOCK(q);
4312 			return (ill);
4313 		} else if (ILL_CAN_WAIT(ill, q)) {
4314 			ipsq = ill->ill_phyint->phyint_ipsq;
4315 			mutex_enter(&ipsq->ipsq_lock);
4316 			mutex_exit(&ill->ill_lock);
4317 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4318 			mutex_exit(&ipsq->ipsq_lock);
4319 			RELEASE_CONN_LOCK(q);
4320 			*error = EINPROGRESS;
4321 			return (NULL);
4322 		}
4323 		mutex_exit(&ill->ill_lock);
4324 		RELEASE_CONN_LOCK(q);
4325 	}
4326 	if (error != NULL)
4327 		*error = ENXIO;
4328 	return (NULL);
4329 }
4330 
4331 /*
4332  * comparison function for use with avl.
4333  */
4334 static int
4335 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4336 {
4337 	uint_t ppa;
4338 	uint_t ill_ppa;
4339 
4340 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4341 
4342 	ppa = *((uint_t *)ppa_ptr);
4343 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4344 	/*
4345 	 * We want the ill with the lowest ppa to be on the
4346 	 * top.
4347 	 */
4348 	if (ill_ppa < ppa)
4349 		return (1);
4350 	if (ill_ppa > ppa)
4351 		return (-1);
4352 	return (0);
4353 }
4354 
4355 /*
4356  * remove an interface type from the global list.
4357  */
4358 static void
4359 ill_delete_interface_type(ill_if_t *interface)
4360 {
4361 	ASSERT(interface != NULL);
4362 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4363 
4364 	avl_destroy(&interface->illif_avl_by_ppa);
4365 	if (interface->illif_ppa_arena != NULL)
4366 		vmem_destroy(interface->illif_ppa_arena);
4367 
4368 	remque(interface);
4369 
4370 	mi_free(interface);
4371 }
4372 
4373 /* Defined in ip_netinfo.c */
4374 extern ddi_taskq_t	*eventq_queue_nic;
4375 
4376 /*
4377  * remove ill from the global list.
4378  */
4379 static void
4380 ill_glist_delete(ill_t *ill)
4381 {
4382 	char *nicname;
4383 	size_t nicnamelen;
4384 	hook_nic_event_t *info;
4385 	ip_stack_t	*ipst;
4386 
4387 	if (ill == NULL)
4388 		return;
4389 	ipst = ill->ill_ipst;
4390 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4391 
4392 	if (ill->ill_name != NULL) {
4393 		nicname = kmem_alloc(ill->ill_name_length, KM_NOSLEEP);
4394 		if (nicname != NULL) {
4395 			bcopy(ill->ill_name, nicname, ill->ill_name_length);
4396 			nicnamelen = ill->ill_name_length;
4397 		}
4398 	} else {
4399 		nicname = NULL;
4400 		nicnamelen = 0;
4401 	}
4402 
4403 	/*
4404 	 * If the ill was never inserted into the AVL tree
4405 	 * we skip the if branch.
4406 	 */
4407 	if (ill->ill_ifptr != NULL) {
4408 		/*
4409 		 * remove from AVL tree and free ppa number
4410 		 */
4411 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4412 
4413 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4414 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4415 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4416 		}
4417 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4418 			ill_delete_interface_type(ill->ill_ifptr);
4419 		}
4420 
4421 		/*
4422 		 * Indicate ill is no longer in the list.
4423 		 */
4424 		ill->ill_ifptr = NULL;
4425 		ill->ill_name_length = 0;
4426 		ill->ill_name[0] = '\0';
4427 		ill->ill_ppa = UINT_MAX;
4428 	}
4429 
4430 	/*
4431 	 * Run the unplumb hook after the NIC has disappeared from being
4432 	 * visible so that attempts to revalidate its existance will fail.
4433 	 *
4434 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4435 	 * that the ordering of delivered events to listeners matches the
4436 	 * order of them in the kernel.
4437 	 */
4438 	if ((info = ill->ill_nic_event_info) != NULL) {
4439 		if (info->hne_event != NE_DOWN) {
4440 			ip2dbg(("ill_glist_delete: unexpected nic event %d "
4441 			    "attached for %s\n", info->hne_event,
4442 			    ill->ill_name));
4443 			if (info->hne_data != NULL)
4444 				kmem_free(info->hne_data, info->hne_datalen);
4445 			kmem_free(info, sizeof (hook_nic_event_t));
4446 		} else {
4447 			if (ddi_taskq_dispatch(eventq_queue_nic,
4448 			    ip_ne_queue_func, (void *)info, DDI_SLEEP)
4449 			    == DDI_FAILURE) {
4450 				ip2dbg(("ill_glist_delete: ddi_taskq_dispatch "
4451 				    "failed\n"));
4452 				if (info->hne_data != NULL)
4453 					kmem_free(info->hne_data,
4454 					    info->hne_datalen);
4455 				kmem_free(info, sizeof (hook_nic_event_t));
4456 			}
4457 		}
4458 	}
4459 
4460 	/* Generate NE_UNPLUMB event for ill_name. */
4461 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
4462 	if (info != NULL) {
4463 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
4464 		info->hne_lif = 0;
4465 		info->hne_event = NE_UNPLUMB;
4466 		info->hne_data = nicname;
4467 		info->hne_datalen = nicnamelen;
4468 		info->hne_family = ill->ill_isv6 ?
4469 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
4470 	} else {
4471 		ip2dbg(("ill_glist_delete: could not attach UNPLUMB nic event "
4472 		    "information for %s (ENOMEM)\n", ill->ill_name));
4473 		if (nicname != NULL)
4474 			kmem_free(nicname, nicnamelen);
4475 	}
4476 
4477 	ill->ill_nic_event_info = info;
4478 
4479 	ill_phyint_free(ill);
4480 	rw_exit(&ipst->ips_ill_g_lock);
4481 }
4482 
4483 /*
4484  * allocate a ppa, if the number of plumbed interfaces of this type are
4485  * less than ill_no_arena do a linear search to find a unused ppa.
4486  * When the number goes beyond ill_no_arena switch to using an arena.
4487  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4488  * is the return value for an error condition, so allocation starts at one
4489  * and is decremented by one.
4490  */
4491 static int
4492 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4493 {
4494 	ill_t *tmp_ill;
4495 	uint_t start, end;
4496 	int ppa;
4497 
4498 	if (ifp->illif_ppa_arena == NULL &&
4499 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4500 		/*
4501 		 * Create an arena.
4502 		 */
4503 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4504 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4505 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4506 			/* allocate what has already been assigned */
4507 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4508 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4509 		    tmp_ill, AVL_AFTER)) {
4510 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4511 			    1,		/* size */
4512 			    1,		/* align/quantum */
4513 			    0,		/* phase */
4514 			    0,		/* nocross */
4515 			    /* minaddr */
4516 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4517 			    /* maxaddr */
4518 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4519 			    VM_NOSLEEP|VM_FIRSTFIT);
4520 			if (ppa == 0) {
4521 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4522 				    " failed while switching"));
4523 				vmem_destroy(ifp->illif_ppa_arena);
4524 				ifp->illif_ppa_arena = NULL;
4525 				break;
4526 			}
4527 		}
4528 	}
4529 
4530 	if (ifp->illif_ppa_arena != NULL) {
4531 		if (ill->ill_ppa == UINT_MAX) {
4532 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4533 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4534 			if (ppa == 0)
4535 				return (EAGAIN);
4536 			ill->ill_ppa = --ppa;
4537 		} else {
4538 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4539 			    1, 		/* size */
4540 			    1, 		/* align/quantum */
4541 			    0, 		/* phase */
4542 			    0, 		/* nocross */
4543 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4544 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4545 			    VM_NOSLEEP|VM_FIRSTFIT);
4546 			/*
4547 			 * Most likely the allocation failed because
4548 			 * the requested ppa was in use.
4549 			 */
4550 			if (ppa == 0)
4551 				return (EEXIST);
4552 		}
4553 		return (0);
4554 	}
4555 
4556 	/*
4557 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4558 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4559 	 */
4560 	if (ill->ill_ppa == UINT_MAX) {
4561 		end = UINT_MAX - 1;
4562 		start = 0;
4563 	} else {
4564 		end = start = ill->ill_ppa;
4565 	}
4566 
4567 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4568 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4569 		if (start++ >= end) {
4570 			if (ill->ill_ppa == UINT_MAX)
4571 				return (EAGAIN);
4572 			else
4573 				return (EEXIST);
4574 		}
4575 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4576 	}
4577 	ill->ill_ppa = start;
4578 	return (0);
4579 }
4580 
4581 /*
4582  * Insert ill into the list of configured ill's. Once this function completes,
4583  * the ill is globally visible and is available through lookups. More precisely
4584  * this happens after the caller drops the ill_g_lock.
4585  */
4586 static int
4587 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4588 {
4589 	ill_if_t *ill_interface;
4590 	avl_index_t where = 0;
4591 	int error;
4592 	int name_length;
4593 	int index;
4594 	boolean_t check_length = B_FALSE;
4595 	ip_stack_t	*ipst = ill->ill_ipst;
4596 
4597 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4598 
4599 	name_length = mi_strlen(name) + 1;
4600 
4601 	if (isv6)
4602 		index = IP_V6_G_HEAD;
4603 	else
4604 		index = IP_V4_G_HEAD;
4605 
4606 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4607 	/*
4608 	 * Search for interface type based on name
4609 	 */
4610 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4611 		if ((ill_interface->illif_name_len == name_length) &&
4612 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4613 			break;
4614 		}
4615 		ill_interface = ill_interface->illif_next;
4616 	}
4617 
4618 	/*
4619 	 * Interface type not found, create one.
4620 	 */
4621 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4622 
4623 		ill_g_head_t ghead;
4624 
4625 		/*
4626 		 * allocate ill_if_t structure
4627 		 */
4628 
4629 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4630 		if (ill_interface == NULL) {
4631 			return (ENOMEM);
4632 		}
4633 
4634 
4635 
4636 		(void) strcpy(ill_interface->illif_name, name);
4637 		ill_interface->illif_name_len = name_length;
4638 
4639 		avl_create(&ill_interface->illif_avl_by_ppa,
4640 		    ill_compare_ppa, sizeof (ill_t),
4641 		    offsetof(struct ill_s, ill_avl_byppa));
4642 
4643 		/*
4644 		 * link the structure in the back to maintain order
4645 		 * of configuration for ifconfig output.
4646 		 */
4647 		ghead = ipst->ips_ill_g_heads[index];
4648 		insque(ill_interface, ghead.ill_g_list_tail);
4649 
4650 	}
4651 
4652 	if (ill->ill_ppa == UINT_MAX)
4653 		check_length = B_TRUE;
4654 
4655 	error = ill_alloc_ppa(ill_interface, ill);
4656 	if (error != 0) {
4657 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4658 			ill_delete_interface_type(ill->ill_ifptr);
4659 		return (error);
4660 	}
4661 
4662 	/*
4663 	 * When the ppa is choosen by the system, check that there is
4664 	 * enough space to insert ppa. if a specific ppa was passed in this
4665 	 * check is not required as the interface name passed in will have
4666 	 * the right ppa in it.
4667 	 */
4668 	if (check_length) {
4669 		/*
4670 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4671 		 */
4672 		char buf[sizeof (uint_t) * 3];
4673 
4674 		/*
4675 		 * convert ppa to string to calculate the amount of space
4676 		 * required for it in the name.
4677 		 */
4678 		numtos(ill->ill_ppa, buf);
4679 
4680 		/* Do we have enough space to insert ppa ? */
4681 
4682 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4683 			/* Free ppa and interface type struct */
4684 			if (ill_interface->illif_ppa_arena != NULL) {
4685 				vmem_free(ill_interface->illif_ppa_arena,
4686 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4687 			}
4688 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4689 			    0) {
4690 				ill_delete_interface_type(ill->ill_ifptr);
4691 			}
4692 
4693 			return (EINVAL);
4694 		}
4695 	}
4696 
4697 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4698 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4699 
4700 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4701 	    &where);
4702 	ill->ill_ifptr = ill_interface;
4703 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4704 
4705 	ill_phyint_reinit(ill);
4706 	return (0);
4707 }
4708 
4709 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4710 static boolean_t
4711 ipsq_init(ill_t *ill)
4712 {
4713 	ipsq_t  *ipsq;
4714 
4715 	/* Init the ipsq and impicitly enter as writer */
4716 	ill->ill_phyint->phyint_ipsq =
4717 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4718 	if (ill->ill_phyint->phyint_ipsq == NULL)
4719 		return (B_FALSE);
4720 	ipsq = ill->ill_phyint->phyint_ipsq;
4721 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4722 	ill->ill_phyint->phyint_ipsq_next = NULL;
4723 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4724 	ipsq->ipsq_refs = 1;
4725 	ipsq->ipsq_writer = curthread;
4726 	ipsq->ipsq_reentry_cnt = 1;
4727 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4728 #ifdef ILL_DEBUG
4729 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4730 #endif
4731 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4732 	return (B_TRUE);
4733 }
4734 
4735 /*
4736  * ill_init is called by ip_open when a device control stream is opened.
4737  * It does a few initializations, and shoots a DL_INFO_REQ message down
4738  * to the driver.  The response is later picked up in ip_rput_dlpi and
4739  * used to set up default mechanisms for talking to the driver.  (Always
4740  * called as writer.)
4741  *
4742  * If this function returns error, ip_open will call ip_close which in
4743  * turn will call ill_delete to clean up any memory allocated here that
4744  * is not yet freed.
4745  */
4746 int
4747 ill_init(queue_t *q, ill_t *ill)
4748 {
4749 	int	count;
4750 	dl_info_req_t	*dlir;
4751 	mblk_t	*info_mp;
4752 	uchar_t *frag_ptr;
4753 
4754 	/*
4755 	 * The ill is initialized to zero by mi_alloc*(). In addition
4756 	 * some fields already contain valid values, initialized in
4757 	 * ip_open(), before we reach here.
4758 	 */
4759 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4760 
4761 	ill->ill_rq = q;
4762 	ill->ill_wq = WR(q);
4763 
4764 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4765 	    BPRI_HI);
4766 	if (info_mp == NULL)
4767 		return (ENOMEM);
4768 
4769 	/*
4770 	 * Allocate sufficient space to contain our fragment hash table and
4771 	 * the device name.
4772 	 */
4773 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4774 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4775 	if (frag_ptr == NULL) {
4776 		freemsg(info_mp);
4777 		return (ENOMEM);
4778 	}
4779 	ill->ill_frag_ptr = frag_ptr;
4780 	ill->ill_frag_free_num_pkts = 0;
4781 	ill->ill_last_frag_clean_time = 0;
4782 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4783 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4784 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4785 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4786 		    NULL, MUTEX_DEFAULT, NULL);
4787 	}
4788 
4789 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4790 	if (ill->ill_phyint == NULL) {
4791 		freemsg(info_mp);
4792 		mi_free(frag_ptr);
4793 		return (ENOMEM);
4794 	}
4795 
4796 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4797 	/*
4798 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4799 	 * at this point because of the following reason. If we can't
4800 	 * enter the ipsq at some point and cv_wait, the writer that
4801 	 * wakes us up tries to locate us using the list of all phyints
4802 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4803 	 * If we don't set it now, we risk a missed wakeup.
4804 	 */
4805 	ill->ill_phyint->phyint_illv4 = ill;
4806 	ill->ill_ppa = UINT_MAX;
4807 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4808 
4809 	if (!ipsq_init(ill)) {
4810 		freemsg(info_mp);
4811 		mi_free(frag_ptr);
4812 		mi_free(ill->ill_phyint);
4813 		return (ENOMEM);
4814 	}
4815 
4816 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4817 
4818 
4819 	/* Frag queue limit stuff */
4820 	ill->ill_frag_count = 0;
4821 	ill->ill_ipf_gen = 0;
4822 
4823 	ill->ill_global_timer = INFINITY;
4824 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4825 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4826 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4827 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4828 
4829 	/*
4830 	 * Initialize IPv6 configuration variables.  The IP module is always
4831 	 * opened as an IPv4 module.  Instead tracking down the cases where
4832 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4833 	 * here for convenience, this has no effect until the ill is set to do
4834 	 * IPv6.
4835 	 */
4836 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4837 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4838 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4839 	ill->ill_max_buf = ND_MAX_Q;
4840 	ill->ill_refcnt = 0;
4841 
4842 	/* Send down the Info Request to the driver. */
4843 	info_mp->b_datap->db_type = M_PCPROTO;
4844 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4845 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4846 	dlir->dl_primitive = DL_INFO_REQ;
4847 
4848 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4849 
4850 	qprocson(q);
4851 	ill_dlpi_send(ill, info_mp);
4852 
4853 	return (0);
4854 }
4855 
4856 /*
4857  * ill_dls_info
4858  * creates datalink socket info from the device.
4859  */
4860 int
4861 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4862 {
4863 	size_t	len;
4864 	ill_t	*ill = ipif->ipif_ill;
4865 
4866 	sdl->sdl_family = AF_LINK;
4867 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4868 	sdl->sdl_type = ill->ill_type;
4869 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4870 	len = strlen(sdl->sdl_data);
4871 	ASSERT(len < 256);
4872 	sdl->sdl_nlen = (uchar_t)len;
4873 	sdl->sdl_alen = ill->ill_phys_addr_length;
4874 	sdl->sdl_slen = 0;
4875 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4876 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4877 
4878 	return (sizeof (struct sockaddr_dl));
4879 }
4880 
4881 /*
4882  * ill_xarp_info
4883  * creates xarp info from the device.
4884  */
4885 static int
4886 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4887 {
4888 	sdl->sdl_family = AF_LINK;
4889 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4890 	sdl->sdl_type = ill->ill_type;
4891 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4892 	    sizeof (sdl->sdl_data));
4893 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4894 	sdl->sdl_alen = ill->ill_phys_addr_length;
4895 	sdl->sdl_slen = 0;
4896 	return (sdl->sdl_nlen);
4897 }
4898 
4899 static int
4900 loopback_kstat_update(kstat_t *ksp, int rw)
4901 {
4902 	kstat_named_t *kn;
4903 	netstackid_t	stackid;
4904 	netstack_t	*ns;
4905 	ip_stack_t	*ipst;
4906 
4907 	if (ksp == NULL || ksp->ks_data == NULL)
4908 		return (EIO);
4909 
4910 	if (rw == KSTAT_WRITE)
4911 		return (EACCES);
4912 
4913 	kn = KSTAT_NAMED_PTR(ksp);
4914 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4915 
4916 	ns = netstack_find_by_stackid(stackid);
4917 	if (ns == NULL)
4918 		return (-1);
4919 
4920 	ipst = ns->netstack_ip;
4921 	if (ipst == NULL) {
4922 		netstack_rele(ns);
4923 		return (-1);
4924 	}
4925 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4926 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4927 	netstack_rele(ns);
4928 	return (0);
4929 }
4930 
4931 
4932 /*
4933  * Has ifindex been plumbed already.
4934  * Compares both phyint_ifindex and phyint_group_ifindex.
4935  */
4936 static boolean_t
4937 phyint_exists(uint_t index, ip_stack_t *ipst)
4938 {
4939 	phyint_t *phyi;
4940 
4941 	ASSERT(index != 0);
4942 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4943 	/*
4944 	 * Indexes are stored in the phyint - a common structure
4945 	 * to both IPv4 and IPv6.
4946 	 */
4947 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4948 	for (; phyi != NULL;
4949 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4950 	    phyi, AVL_AFTER)) {
4951 		if (phyi->phyint_ifindex == index ||
4952 		    phyi->phyint_group_ifindex == index)
4953 			return (B_TRUE);
4954 	}
4955 	return (B_FALSE);
4956 }
4957 
4958 /* Pick a unique ifindex */
4959 boolean_t
4960 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4961 {
4962 	uint_t starting_index;
4963 
4964 	if (!ipst->ips_ill_index_wrap) {
4965 		*indexp = ipst->ips_ill_index++;
4966 		if (ipst->ips_ill_index == 0) {
4967 			/* Reached the uint_t limit Next time wrap  */
4968 			ipst->ips_ill_index_wrap = B_TRUE;
4969 		}
4970 		return (B_TRUE);
4971 	}
4972 
4973 	/*
4974 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4975 	 * at this point and don't want to call any function that attempts
4976 	 * to get the lock again.
4977 	 */
4978 	starting_index = ipst->ips_ill_index++;
4979 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4980 		if (ipst->ips_ill_index != 0 &&
4981 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4982 			/* found unused index - use it */
4983 			*indexp = ipst->ips_ill_index;
4984 			return (B_TRUE);
4985 		}
4986 	}
4987 
4988 	/*
4989 	 * all interface indicies are inuse.
4990 	 */
4991 	return (B_FALSE);
4992 }
4993 
4994 /*
4995  * Assign a unique interface index for the phyint.
4996  */
4997 static boolean_t
4998 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4999 {
5000 	ASSERT(phyi->phyint_ifindex == 0);
5001 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
5002 }
5003 
5004 /*
5005  * Return a pointer to the ill which matches the supplied name.  Note that
5006  * the ill name length includes the null termination character.  (May be
5007  * called as writer.)
5008  * If do_alloc and the interface is "lo0" it will be automatically created.
5009  * Cannot bump up reference on condemned ills. So dup detect can't be done
5010  * using this func.
5011  */
5012 ill_t *
5013 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
5014     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
5015     ip_stack_t *ipst)
5016 {
5017 	ill_t	*ill;
5018 	ipif_t	*ipif;
5019 	kstat_named_t	*kn;
5020 	boolean_t isloopback;
5021 	ipsq_t *old_ipsq;
5022 	in6_addr_t ov6addr;
5023 
5024 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
5025 
5026 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5027 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
5028 	rw_exit(&ipst->ips_ill_g_lock);
5029 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
5030 		return (ill);
5031 
5032 	/*
5033 	 * Couldn't find it.  Does this happen to be a lookup for the
5034 	 * loopback device and are we allowed to allocate it?
5035 	 */
5036 	if (!isloopback || !do_alloc)
5037 		return (NULL);
5038 
5039 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
5040 
5041 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
5042 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
5043 		rw_exit(&ipst->ips_ill_g_lock);
5044 		return (ill);
5045 	}
5046 
5047 	/* Create the loopback device on demand */
5048 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
5049 	    sizeof (ipif_loopback_name), BPRI_MED));
5050 	if (ill == NULL)
5051 		goto done;
5052 
5053 	*ill = ill_null;
5054 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
5055 	ill->ill_ipst = ipst;
5056 	netstack_hold(ipst->ips_netstack);
5057 	/*
5058 	 * For exclusive stacks we set the zoneid to zero
5059 	 * to make IP operate as if in the global zone.
5060 	 */
5061 	ill->ill_zoneid = GLOBAL_ZONEID;
5062 
5063 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
5064 	if (ill->ill_phyint == NULL)
5065 		goto done;
5066 
5067 	if (isv6)
5068 		ill->ill_phyint->phyint_illv6 = ill;
5069 	else
5070 		ill->ill_phyint->phyint_illv4 = ill;
5071 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
5072 	ill->ill_max_frag = IP_LOOPBACK_MTU;
5073 	/* Add room for tcp+ip headers */
5074 	if (isv6) {
5075 		ill->ill_isv6 = B_TRUE;
5076 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
5077 	} else {
5078 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
5079 	}
5080 	if (!ill_allocate_mibs(ill))
5081 		goto done;
5082 	ill->ill_max_mtu = ill->ill_max_frag;
5083 	/*
5084 	 * ipif_loopback_name can't be pointed at directly because its used
5085 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
5086 	 * from the glist, ill_glist_delete() sets the first character of
5087 	 * ill_name to '\0'.
5088 	 */
5089 	ill->ill_name = (char *)ill + sizeof (*ill);
5090 	(void) strcpy(ill->ill_name, ipif_loopback_name);
5091 	ill->ill_name_length = sizeof (ipif_loopback_name);
5092 	/* Set ill_name_set for ill_phyint_reinit to work properly */
5093 
5094 	ill->ill_global_timer = INFINITY;
5095 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
5096 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
5097 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
5098 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
5099 
5100 	/* No resolver here. */
5101 	ill->ill_net_type = IRE_LOOPBACK;
5102 
5103 	/* Initialize the ipsq */
5104 	if (!ipsq_init(ill))
5105 		goto done;
5106 
5107 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5108 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5109 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5110 #ifdef ILL_DEBUG
5111 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5112 #endif
5113 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5114 	if (ipif == NULL)
5115 		goto done;
5116 
5117 	ill->ill_flags = ILLF_MULTICAST;
5118 
5119 	ov6addr = ipif->ipif_v6lcl_addr;
5120 	/* Set up default loopback address and mask. */
5121 	if (!isv6) {
5122 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5123 
5124 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5125 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5126 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5127 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5128 		    ipif->ipif_v6subnet);
5129 		ill->ill_flags |= ILLF_IPV4;
5130 	} else {
5131 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5132 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5133 		ipif->ipif_v6net_mask = ipv6_all_ones;
5134 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5135 		    ipif->ipif_v6subnet);
5136 		ill->ill_flags |= ILLF_IPV6;
5137 	}
5138 
5139 	/*
5140 	 * Chain us in at the end of the ill list. hold the ill
5141 	 * before we make it globally visible. 1 for the lookup.
5142 	 */
5143 	ill->ill_refcnt = 0;
5144 	ill_refhold(ill);
5145 
5146 	ill->ill_frag_count = 0;
5147 	ill->ill_frag_free_num_pkts = 0;
5148 	ill->ill_last_frag_clean_time = 0;
5149 
5150 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5151 
5152 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5153 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5154 
5155 	/* Let SCTP know so that it can add this to its list */
5156 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5157 
5158 	/*
5159 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5160 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5161 	 * requires to be after ill_glist_insert() since we need the
5162 	 * ill_index set. Pass on ipv6_loopback as the old address.
5163 	 */
5164 	sctp_update_ipif_addr(ipif, ov6addr);
5165 
5166 	/*
5167 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5168 	 */
5169 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5170 		/* Loopback ills aren't in any IPMP group */
5171 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5172 		ipsq_delete(old_ipsq);
5173 	}
5174 
5175 	/*
5176 	 * Delay this till the ipif is allocated as ipif_allocate
5177 	 * de-references ill_phyint for getting the ifindex. We
5178 	 * can't do this before ipif_allocate because ill_phyint_reinit
5179 	 * -> phyint_assign_ifindex expects ipif to be present.
5180 	 */
5181 	mutex_enter(&ill->ill_phyint->phyint_lock);
5182 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5183 	mutex_exit(&ill->ill_phyint->phyint_lock);
5184 
5185 	if (ipst->ips_loopback_ksp == NULL) {
5186 		/* Export loopback interface statistics */
5187 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5188 		    ipif_loopback_name, "net",
5189 		    KSTAT_TYPE_NAMED, 2, 0,
5190 		    ipst->ips_netstack->netstack_stackid);
5191 		if (ipst->ips_loopback_ksp != NULL) {
5192 			ipst->ips_loopback_ksp->ks_update =
5193 			    loopback_kstat_update;
5194 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5195 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5196 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5197 			ipst->ips_loopback_ksp->ks_private =
5198 			    (void *)(uintptr_t)ipst->ips_netstack->
5199 			    netstack_stackid;
5200 			kstat_install(ipst->ips_loopback_ksp);
5201 		}
5202 	}
5203 
5204 	if (error != NULL)
5205 		*error = 0;
5206 	*did_alloc = B_TRUE;
5207 	rw_exit(&ipst->ips_ill_g_lock);
5208 	return (ill);
5209 done:
5210 	if (ill != NULL) {
5211 		if (ill->ill_phyint != NULL) {
5212 			ipsq_t	*ipsq;
5213 
5214 			ipsq = ill->ill_phyint->phyint_ipsq;
5215 			if (ipsq != NULL) {
5216 				ipsq->ipsq_ipst = NULL;
5217 				kmem_free(ipsq, sizeof (ipsq_t));
5218 			}
5219 			mi_free(ill->ill_phyint);
5220 		}
5221 		ill_free_mib(ill);
5222 		if (ill->ill_ipst != NULL)
5223 			netstack_rele(ill->ill_ipst->ips_netstack);
5224 		mi_free(ill);
5225 	}
5226 	rw_exit(&ipst->ips_ill_g_lock);
5227 	if (error != NULL)
5228 		*error = ENOMEM;
5229 	return (NULL);
5230 }
5231 
5232 /*
5233  * For IPP calls - use the ip_stack_t for global stack.
5234  */
5235 ill_t *
5236 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5237     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5238 {
5239 	ip_stack_t	*ipst;
5240 	ill_t		*ill;
5241 
5242 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5243 	if (ipst == NULL) {
5244 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5245 		return (NULL);
5246 	}
5247 
5248 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5249 	netstack_rele(ipst->ips_netstack);
5250 	return (ill);
5251 }
5252 
5253 /*
5254  * Return a pointer to the ill which matches the index and IP version type.
5255  */
5256 ill_t *
5257 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5258     ipsq_func_t func, int *err, ip_stack_t *ipst)
5259 {
5260 	ill_t	*ill;
5261 	ipsq_t  *ipsq;
5262 	phyint_t *phyi;
5263 
5264 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5265 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5266 
5267 	if (err != NULL)
5268 		*err = 0;
5269 
5270 	/*
5271 	 * Indexes are stored in the phyint - a common structure
5272 	 * to both IPv4 and IPv6.
5273 	 */
5274 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5275 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5276 	    (void *) &index, NULL);
5277 	if (phyi != NULL) {
5278 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5279 		if (ill != NULL) {
5280 			/*
5281 			 * The block comment at the start of ipif_down
5282 			 * explains the use of the macros used below
5283 			 */
5284 			GRAB_CONN_LOCK(q);
5285 			mutex_enter(&ill->ill_lock);
5286 			if (ILL_CAN_LOOKUP(ill)) {
5287 				ill_refhold_locked(ill);
5288 				mutex_exit(&ill->ill_lock);
5289 				RELEASE_CONN_LOCK(q);
5290 				rw_exit(&ipst->ips_ill_g_lock);
5291 				return (ill);
5292 			} else if (ILL_CAN_WAIT(ill, q)) {
5293 				ipsq = ill->ill_phyint->phyint_ipsq;
5294 				mutex_enter(&ipsq->ipsq_lock);
5295 				rw_exit(&ipst->ips_ill_g_lock);
5296 				mutex_exit(&ill->ill_lock);
5297 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5298 				mutex_exit(&ipsq->ipsq_lock);
5299 				RELEASE_CONN_LOCK(q);
5300 				*err = EINPROGRESS;
5301 				return (NULL);
5302 			}
5303 			RELEASE_CONN_LOCK(q);
5304 			mutex_exit(&ill->ill_lock);
5305 		}
5306 	}
5307 	rw_exit(&ipst->ips_ill_g_lock);
5308 	if (err != NULL)
5309 		*err = ENXIO;
5310 	return (NULL);
5311 }
5312 
5313 /*
5314  * Return the ifindex next in sequence after the passed in ifindex.
5315  * If there is no next ifindex for the given protocol, return 0.
5316  */
5317 uint_t
5318 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5319 {
5320 	phyint_t *phyi;
5321 	phyint_t *phyi_initial;
5322 	uint_t   ifindex;
5323 
5324 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5325 
5326 	if (index == 0) {
5327 		phyi = avl_first(
5328 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5329 	} else {
5330 		phyi = phyi_initial = avl_find(
5331 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5332 		    (void *) &index, NULL);
5333 	}
5334 
5335 	for (; phyi != NULL;
5336 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5337 	    phyi, AVL_AFTER)) {
5338 		/*
5339 		 * If we're not returning the first interface in the tree
5340 		 * and we still haven't moved past the phyint_t that
5341 		 * corresponds to index, avl_walk needs to be called again
5342 		 */
5343 		if (!((index != 0) && (phyi == phyi_initial))) {
5344 			if (isv6) {
5345 				if ((phyi->phyint_illv6) &&
5346 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5347 				    (phyi->phyint_illv6->ill_isv6 == 1))
5348 					break;
5349 			} else {
5350 				if ((phyi->phyint_illv4) &&
5351 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5352 				    (phyi->phyint_illv4->ill_isv6 == 0))
5353 					break;
5354 			}
5355 		}
5356 	}
5357 
5358 	rw_exit(&ipst->ips_ill_g_lock);
5359 
5360 	if (phyi != NULL)
5361 		ifindex = phyi->phyint_ifindex;
5362 	else
5363 		ifindex = 0;
5364 
5365 	return (ifindex);
5366 }
5367 
5368 
5369 /*
5370  * Return the ifindex for the named interface.
5371  * If there is no next ifindex for the interface, return 0.
5372  */
5373 uint_t
5374 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5375 {
5376 	phyint_t	*phyi;
5377 	avl_index_t	where = 0;
5378 	uint_t		ifindex;
5379 
5380 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5381 
5382 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5383 	    name, &where)) == NULL) {
5384 		rw_exit(&ipst->ips_ill_g_lock);
5385 		return (0);
5386 	}
5387 
5388 	ifindex = phyi->phyint_ifindex;
5389 
5390 	rw_exit(&ipst->ips_ill_g_lock);
5391 
5392 	return (ifindex);
5393 }
5394 
5395 
5396 /*
5397  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5398  * that gives a running thread a reference to the ill. This reference must be
5399  * released by the thread when it is done accessing the ill and related
5400  * objects. ill_refcnt can not be used to account for static references
5401  * such as other structures pointing to an ill. Callers must generally
5402  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5403  * or be sure that the ill is not being deleted or changing state before
5404  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5405  * ill won't change any of its critical state such as address, netmask etc.
5406  */
5407 void
5408 ill_refhold(ill_t *ill)
5409 {
5410 	mutex_enter(&ill->ill_lock);
5411 	ill->ill_refcnt++;
5412 	ILL_TRACE_REF(ill);
5413 	mutex_exit(&ill->ill_lock);
5414 }
5415 
5416 void
5417 ill_refhold_locked(ill_t *ill)
5418 {
5419 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5420 	ill->ill_refcnt++;
5421 	ILL_TRACE_REF(ill);
5422 }
5423 
5424 int
5425 ill_check_and_refhold(ill_t *ill)
5426 {
5427 	mutex_enter(&ill->ill_lock);
5428 	if (ILL_CAN_LOOKUP(ill)) {
5429 		ill_refhold_locked(ill);
5430 		mutex_exit(&ill->ill_lock);
5431 		return (0);
5432 	}
5433 	mutex_exit(&ill->ill_lock);
5434 	return (ILL_LOOKUP_FAILED);
5435 }
5436 
5437 /*
5438  * Must not be called while holding any locks. Otherwise if this is
5439  * the last reference to be released, there is a chance of recursive mutex
5440  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5441  * to restart an ioctl.
5442  */
5443 void
5444 ill_refrele(ill_t *ill)
5445 {
5446 	mutex_enter(&ill->ill_lock);
5447 	ASSERT(ill->ill_refcnt != 0);
5448 	ill->ill_refcnt--;
5449 	ILL_UNTRACE_REF(ill);
5450 	if (ill->ill_refcnt != 0) {
5451 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5452 		mutex_exit(&ill->ill_lock);
5453 		return;
5454 	}
5455 
5456 	/* Drops the ill_lock */
5457 	ipif_ill_refrele_tail(ill);
5458 }
5459 
5460 /*
5461  * Obtain a weak reference count on the ill. This reference ensures the
5462  * ill won't be freed, but the ill may change any of its critical state
5463  * such as netmask, address etc. Returns an error if the ill has started
5464  * closing.
5465  */
5466 boolean_t
5467 ill_waiter_inc(ill_t *ill)
5468 {
5469 	mutex_enter(&ill->ill_lock);
5470 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5471 		mutex_exit(&ill->ill_lock);
5472 		return (B_FALSE);
5473 	}
5474 	ill->ill_waiters++;
5475 	mutex_exit(&ill->ill_lock);
5476 	return (B_TRUE);
5477 }
5478 
5479 void
5480 ill_waiter_dcr(ill_t *ill)
5481 {
5482 	mutex_enter(&ill->ill_lock);
5483 	ill->ill_waiters--;
5484 	if (ill->ill_waiters == 0)
5485 		cv_broadcast(&ill->ill_cv);
5486 	mutex_exit(&ill->ill_lock);
5487 }
5488 
5489 /*
5490  * Named Dispatch routine to produce a formatted report on all ILLs.
5491  * This report is accessed by using the ndd utility to "get" ND variable
5492  * "ip_ill_status".
5493  */
5494 /* ARGSUSED */
5495 int
5496 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5497 {
5498 	ill_t		*ill;
5499 	ill_walk_context_t ctx;
5500 	ip_stack_t	*ipst;
5501 
5502 	ipst = CONNQ_TO_IPST(q);
5503 
5504 	(void) mi_mpprintf(mp,
5505 	    "ILL      " MI_COL_HDRPAD_STR
5506 	/*   01234567[89ABCDEF] */
5507 	    "rq       " MI_COL_HDRPAD_STR
5508 	/*   01234567[89ABCDEF] */
5509 	    "wq       " MI_COL_HDRPAD_STR
5510 	/*   01234567[89ABCDEF] */
5511 	    "upcnt mxfrg err name");
5512 	/*   12345 12345 123 xxxxxxxx  */
5513 
5514 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5515 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5516 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5517 		(void) mi_mpprintf(mp,
5518 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5519 		    "%05u %05u %03d %s",
5520 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5521 		    ill->ill_ipif_up_count,
5522 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5523 	}
5524 	rw_exit(&ipst->ips_ill_g_lock);
5525 
5526 	return (0);
5527 }
5528 
5529 /*
5530  * Named Dispatch routine to produce a formatted report on all IPIFs.
5531  * This report is accessed by using the ndd utility to "get" ND variable
5532  * "ip_ipif_status".
5533  */
5534 /* ARGSUSED */
5535 int
5536 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5537 {
5538 	char	buf1[INET6_ADDRSTRLEN];
5539 	char	buf2[INET6_ADDRSTRLEN];
5540 	char	buf3[INET6_ADDRSTRLEN];
5541 	char	buf4[INET6_ADDRSTRLEN];
5542 	char	buf5[INET6_ADDRSTRLEN];
5543 	char	buf6[INET6_ADDRSTRLEN];
5544 	char	buf[LIFNAMSIZ];
5545 	ill_t	*ill;
5546 	ipif_t	*ipif;
5547 	nv_t	*nvp;
5548 	uint64_t flags;
5549 	zoneid_t zoneid;
5550 	ill_walk_context_t ctx;
5551 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5552 
5553 	(void) mi_mpprintf(mp,
5554 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5555 	    "\tlocal address\n"
5556 	    "\tsrc address\n"
5557 	    "\tsubnet\n"
5558 	    "\tmask\n"
5559 	    "\tbroadcast\n"
5560 	    "\tp-p-dst");
5561 
5562 	ASSERT(q->q_next == NULL);
5563 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5564 
5565 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5566 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5567 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5568 		for (ipif = ill->ill_ipif; ipif != NULL;
5569 		    ipif = ipif->ipif_next) {
5570 			if (zoneid != GLOBAL_ZONEID &&
5571 			    zoneid != ipif->ipif_zoneid &&
5572 			    ipif->ipif_zoneid != ALL_ZONES)
5573 				continue;
5574 			(void) mi_mpprintf(mp,
5575 			    MI_COL_PTRFMT_STR
5576 			    "%04u %05u %u/%u/%u %s %d",
5577 			    (void *)ipif,
5578 			    ipif->ipif_metric, ipif->ipif_mtu,
5579 			    ipif->ipif_ib_pkt_count,
5580 			    ipif->ipif_ob_pkt_count,
5581 			    ipif->ipif_fo_pkt_count,
5582 			    ipif_get_name(ipif, buf, sizeof (buf)),
5583 			    ipif->ipif_zoneid);
5584 
5585 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5586 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5587 
5588 		/* Tack on text strings for any flags. */
5589 		nvp = ipif_nv_tbl;
5590 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5591 			if (nvp->nv_value & flags)
5592 				(void) mi_mpprintf_nr(mp, " %s",
5593 				    nvp->nv_name);
5594 		}
5595 		(void) mi_mpprintf(mp,
5596 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5597 		    inet_ntop(AF_INET6,
5598 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5599 		    inet_ntop(AF_INET6,
5600 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5601 		    inet_ntop(AF_INET6,
5602 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5603 		    inet_ntop(AF_INET6,
5604 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5605 		    inet_ntop(AF_INET6,
5606 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5607 		    inet_ntop(AF_INET6,
5608 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5609 		}
5610 	}
5611 	rw_exit(&ipst->ips_ill_g_lock);
5612 	return (0);
5613 }
5614 
5615 /*
5616  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5617  * driver.  We construct best guess defaults for lower level information that
5618  * we need.  If an interface is brought up without injection of any overriding
5619  * information from outside, we have to be ready to go with these defaults.
5620  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5621  * we primarely want the dl_provider_style.
5622  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5623  * at which point we assume the other part of the information is valid.
5624  */
5625 void
5626 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5627 {
5628 	uchar_t		*brdcst_addr;
5629 	uint_t		brdcst_addr_length, phys_addr_length;
5630 	t_scalar_t	sap_length;
5631 	dl_info_ack_t	*dlia;
5632 	ip_m_t		*ipm;
5633 	dl_qos_cl_sel1_t *sel1;
5634 
5635 	ASSERT(IAM_WRITER_ILL(ill));
5636 
5637 	/*
5638 	 * Till the ill is fully up ILL_CHANGING will be set and
5639 	 * the ill is not globally visible. So no need for a lock.
5640 	 */
5641 	dlia = (dl_info_ack_t *)mp->b_rptr;
5642 	ill->ill_mactype = dlia->dl_mac_type;
5643 
5644 	ipm = ip_m_lookup(dlia->dl_mac_type);
5645 	if (ipm == NULL) {
5646 		ipm = ip_m_lookup(DL_OTHER);
5647 		ASSERT(ipm != NULL);
5648 	}
5649 	ill->ill_media = ipm;
5650 
5651 	/*
5652 	 * When the new DLPI stuff is ready we'll pull lengths
5653 	 * from dlia.
5654 	 */
5655 	if (dlia->dl_version == DL_VERSION_2) {
5656 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5657 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5658 		    brdcst_addr_length);
5659 		if (brdcst_addr == NULL) {
5660 			brdcst_addr_length = 0;
5661 		}
5662 		sap_length = dlia->dl_sap_length;
5663 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5664 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5665 		    brdcst_addr_length, sap_length, phys_addr_length));
5666 	} else {
5667 		brdcst_addr_length = 6;
5668 		brdcst_addr = ip_six_byte_all_ones;
5669 		sap_length = -2;
5670 		phys_addr_length = brdcst_addr_length;
5671 	}
5672 
5673 	ill->ill_bcast_addr_length = brdcst_addr_length;
5674 	ill->ill_phys_addr_length = phys_addr_length;
5675 	ill->ill_sap_length = sap_length;
5676 	ill->ill_max_frag = dlia->dl_max_sdu;
5677 	ill->ill_max_mtu = ill->ill_max_frag;
5678 
5679 	ill->ill_type = ipm->ip_m_type;
5680 
5681 	if (!ill->ill_dlpi_style_set) {
5682 		if (dlia->dl_provider_style == DL_STYLE2)
5683 			ill->ill_needs_attach = 1;
5684 
5685 		/*
5686 		 * Allocate the first ipif on this ill. We don't delay it
5687 		 * further as ioctl handling assumes atleast one ipif to
5688 		 * be present.
5689 		 *
5690 		 * At this point we don't know whether the ill is v4 or v6.
5691 		 * We will know this whan the SIOCSLIFNAME happens and
5692 		 * the correct value for ill_isv6 will be assigned in
5693 		 * ipif_set_values(). We need to hold the ill lock and
5694 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5695 		 * the wakeup.
5696 		 */
5697 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5698 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5699 		mutex_enter(&ill->ill_lock);
5700 		ASSERT(ill->ill_dlpi_style_set == 0);
5701 		ill->ill_dlpi_style_set = 1;
5702 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5703 		cv_broadcast(&ill->ill_cv);
5704 		mutex_exit(&ill->ill_lock);
5705 		freemsg(mp);
5706 		return;
5707 	}
5708 	ASSERT(ill->ill_ipif != NULL);
5709 	/*
5710 	 * We know whether it is IPv4 or IPv6 now, as this is the
5711 	 * second DL_INFO_ACK we are recieving in response to the
5712 	 * DL_INFO_REQ sent in ipif_set_values.
5713 	 */
5714 	if (ill->ill_isv6)
5715 		ill->ill_sap = IP6_DL_SAP;
5716 	else
5717 		ill->ill_sap = IP_DL_SAP;
5718 	/*
5719 	 * Set ipif_mtu which is used to set the IRE's
5720 	 * ire_max_frag value. The driver could have sent
5721 	 * a different mtu from what it sent last time. No
5722 	 * need to call ipif_mtu_change because IREs have
5723 	 * not yet been created.
5724 	 */
5725 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5726 	/*
5727 	 * Clear all the flags that were set based on ill_bcast_addr_length
5728 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5729 	 * changed now and we need to re-evaluate.
5730 	 */
5731 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5732 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5733 
5734 	/*
5735 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5736 	 * changed now.
5737 	 */
5738 	if (ill->ill_bcast_addr_length == 0) {
5739 		if (ill->ill_resolver_mp != NULL)
5740 			freemsg(ill->ill_resolver_mp);
5741 		if (ill->ill_bcast_mp != NULL)
5742 			freemsg(ill->ill_bcast_mp);
5743 		if (ill->ill_flags & ILLF_XRESOLV)
5744 			ill->ill_net_type = IRE_IF_RESOLVER;
5745 		else
5746 			ill->ill_net_type = IRE_IF_NORESOLVER;
5747 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5748 		    ill->ill_phys_addr_length,
5749 		    ill->ill_sap,
5750 		    ill->ill_sap_length);
5751 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5752 
5753 		if (ill->ill_isv6)
5754 			/*
5755 			 * Note: xresolv interfaces will eventually need NOARP
5756 			 * set here as well, but that will require those
5757 			 * external resolvers to have some knowledge of
5758 			 * that flag and act appropriately. Not to be changed
5759 			 * at present.
5760 			 */
5761 			ill->ill_flags |= ILLF_NONUD;
5762 		else
5763 			ill->ill_flags |= ILLF_NOARP;
5764 
5765 		if (ill->ill_phys_addr_length == 0) {
5766 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5767 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5768 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5769 			} else {
5770 				/* pt-pt supports multicast. */
5771 				ill->ill_flags |= ILLF_MULTICAST;
5772 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5773 			}
5774 		}
5775 	} else {
5776 		ill->ill_net_type = IRE_IF_RESOLVER;
5777 		if (ill->ill_bcast_mp != NULL)
5778 			freemsg(ill->ill_bcast_mp);
5779 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5780 		    ill->ill_bcast_addr_length, ill->ill_sap,
5781 		    ill->ill_sap_length);
5782 		/*
5783 		 * Later detect lack of DLPI driver multicast
5784 		 * capability by catching DL_ENABMULTI errors in
5785 		 * ip_rput_dlpi.
5786 		 */
5787 		ill->ill_flags |= ILLF_MULTICAST;
5788 		if (!ill->ill_isv6)
5789 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5790 	}
5791 	/* By default an interface does not support any CoS marking */
5792 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5793 
5794 	/*
5795 	 * If we get QoS information in DL_INFO_ACK, the device supports
5796 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5797 	 */
5798 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5799 	    dlia->dl_qos_length);
5800 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5801 		ill->ill_flags |= ILLF_COS_ENABLED;
5802 	}
5803 
5804 	/* Clear any previous error indication. */
5805 	ill->ill_error = 0;
5806 	freemsg(mp);
5807 }
5808 
5809 /*
5810  * Perform various checks to verify that an address would make sense as a
5811  * local, remote, or subnet interface address.
5812  */
5813 static boolean_t
5814 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5815 {
5816 	ipaddr_t	net_mask;
5817 
5818 	/*
5819 	 * Don't allow all zeroes, all ones or experimental address, but allow
5820 	 * all ones netmask.
5821 	 */
5822 	if ((net_mask = ip_net_mask(addr)) == 0)
5823 		return (B_FALSE);
5824 	/* A given netmask overrides the "guess" netmask */
5825 	if (subnet_mask != 0)
5826 		net_mask = subnet_mask;
5827 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5828 	    (addr == (addr | ~net_mask)))) {
5829 		return (B_FALSE);
5830 	}
5831 	if (CLASSD(addr))
5832 		return (B_FALSE);
5833 
5834 	return (B_TRUE);
5835 }
5836 
5837 #define	V6_IPIF_LINKLOCAL(p)	\
5838 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5839 
5840 /*
5841  * Compare two given ipifs and check if the second one is better than
5842  * the first one using the order of preference (not taking deprecated
5843  * into acount) specified in ipif_lookup_multicast().
5844  */
5845 static boolean_t
5846 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5847 {
5848 	/* Check the least preferred first. */
5849 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5850 		/* If both ipifs are the same, use the first one. */
5851 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5852 			return (B_FALSE);
5853 		else
5854 			return (B_TRUE);
5855 	}
5856 
5857 	/* For IPv6, check for link local address. */
5858 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5859 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5860 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5861 			/* The second one is equal or less preferred. */
5862 			return (B_FALSE);
5863 		} else {
5864 			return (B_TRUE);
5865 		}
5866 	}
5867 
5868 	/* Then check for point to point interface. */
5869 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5870 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5871 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5872 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5873 			return (B_FALSE);
5874 		} else {
5875 			return (B_TRUE);
5876 		}
5877 	}
5878 
5879 	/* old_ipif is a normal interface, so no need to use the new one. */
5880 	return (B_FALSE);
5881 }
5882 
5883 /*
5884  * Find any non-virtual, not condemned, and up multicast capable interface
5885  * given an IP instance and zoneid.  Order of preference is:
5886  *
5887  * 1. normal
5888  * 1.1 normal, but deprecated
5889  * 2. point to point
5890  * 2.1 point to point, but deprecated
5891  * 3. link local
5892  * 3.1 link local, but deprecated
5893  * 4. loopback.
5894  */
5895 ipif_t *
5896 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5897 {
5898 	ill_t			*ill;
5899 	ill_walk_context_t	ctx;
5900 	ipif_t			*ipif;
5901 	ipif_t			*saved_ipif = NULL;
5902 	ipif_t			*dep_ipif = NULL;
5903 
5904 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5905 	if (isv6)
5906 		ill = ILL_START_WALK_V6(&ctx, ipst);
5907 	else
5908 		ill = ILL_START_WALK_V4(&ctx, ipst);
5909 
5910 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5911 		mutex_enter(&ill->ill_lock);
5912 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5913 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5914 			mutex_exit(&ill->ill_lock);
5915 			continue;
5916 		}
5917 		for (ipif = ill->ill_ipif; ipif != NULL;
5918 		    ipif = ipif->ipif_next) {
5919 			if (zoneid != ipif->ipif_zoneid &&
5920 			    zoneid != ALL_ZONES &&
5921 			    ipif->ipif_zoneid != ALL_ZONES) {
5922 				continue;
5923 			}
5924 			if (!(ipif->ipif_flags & IPIF_UP) ||
5925 			    !IPIF_CAN_LOOKUP(ipif)) {
5926 				continue;
5927 			}
5928 
5929 			/*
5930 			 * Found one candidate.  If it is deprecated,
5931 			 * remember it in dep_ipif.  If it is not deprecated,
5932 			 * remember it in saved_ipif.
5933 			 */
5934 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5935 				if (dep_ipif == NULL) {
5936 					dep_ipif = ipif;
5937 				} else if (ipif_comp_multi(dep_ipif, ipif,
5938 				    isv6)) {
5939 					/*
5940 					 * If the previous dep_ipif does not
5941 					 * belong to the same ill, we've done
5942 					 * a ipif_refhold() on it.  So we need
5943 					 * to release it.
5944 					 */
5945 					if (dep_ipif->ipif_ill != ill)
5946 						ipif_refrele(dep_ipif);
5947 					dep_ipif = ipif;
5948 				}
5949 				continue;
5950 			}
5951 			if (saved_ipif == NULL) {
5952 				saved_ipif = ipif;
5953 			} else {
5954 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5955 					if (saved_ipif->ipif_ill != ill)
5956 						ipif_refrele(saved_ipif);
5957 					saved_ipif = ipif;
5958 				}
5959 			}
5960 		}
5961 		/*
5962 		 * Before going to the next ill, do a ipif_refhold() on the
5963 		 * saved ones.
5964 		 */
5965 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5966 			ipif_refhold_locked(saved_ipif);
5967 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5968 			ipif_refhold_locked(dep_ipif);
5969 		mutex_exit(&ill->ill_lock);
5970 	}
5971 	rw_exit(&ipst->ips_ill_g_lock);
5972 
5973 	/*
5974 	 * If we have only the saved_ipif, return it.  But if we have both
5975 	 * saved_ipif and dep_ipif, check to see which one is better.
5976 	 */
5977 	if (saved_ipif != NULL) {
5978 		if (dep_ipif != NULL) {
5979 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5980 				ipif_refrele(saved_ipif);
5981 				return (dep_ipif);
5982 			} else {
5983 				ipif_refrele(dep_ipif);
5984 				return (saved_ipif);
5985 			}
5986 		}
5987 		return (saved_ipif);
5988 	} else {
5989 		return (dep_ipif);
5990 	}
5991 }
5992 
5993 /*
5994  * This function is called when an application does not specify an interface
5995  * to be used for multicast traffic (joining a group/sending data).  It
5996  * calls ire_lookup_multi() to look for an interface route for the
5997  * specified multicast group.  Doing this allows the administrator to add
5998  * prefix routes for multicast to indicate which interface to be used for
5999  * multicast traffic in the above scenario.  The route could be for all
6000  * multicast (224.0/4), for a single multicast group (a /32 route) or
6001  * anything in between.  If there is no such multicast route, we just find
6002  * any multicast capable interface and return it.  The returned ipif
6003  * is refhold'ed.
6004  */
6005 ipif_t *
6006 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
6007 {
6008 	ire_t			*ire;
6009 	ipif_t			*ipif;
6010 
6011 	ire = ire_lookup_multi(group, zoneid, ipst);
6012 	if (ire != NULL) {
6013 		ipif = ire->ire_ipif;
6014 		ipif_refhold(ipif);
6015 		ire_refrele(ire);
6016 		return (ipif);
6017 	}
6018 
6019 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
6020 }
6021 
6022 /*
6023  * Look for an ipif with the specified interface address and destination.
6024  * The destination address is used only for matching point-to-point interfaces.
6025  */
6026 ipif_t *
6027 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
6028     ipsq_func_t func, int *error, ip_stack_t *ipst)
6029 {
6030 	ipif_t	*ipif;
6031 	ill_t	*ill;
6032 	ill_walk_context_t ctx;
6033 	ipsq_t	*ipsq;
6034 
6035 	if (error != NULL)
6036 		*error = 0;
6037 
6038 	/*
6039 	 * First match all the point-to-point interfaces
6040 	 * before looking at non-point-to-point interfaces.
6041 	 * This is done to avoid returning non-point-to-point
6042 	 * ipif instead of unnumbered point-to-point ipif.
6043 	 */
6044 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6045 	ill = ILL_START_WALK_V4(&ctx, ipst);
6046 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6047 		GRAB_CONN_LOCK(q);
6048 		mutex_enter(&ill->ill_lock);
6049 		for (ipif = ill->ill_ipif; ipif != NULL;
6050 		    ipif = ipif->ipif_next) {
6051 			/* Allow the ipif to be down */
6052 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
6053 			    (ipif->ipif_lcl_addr == if_addr) &&
6054 			    (ipif->ipif_pp_dst_addr == dst)) {
6055 				/*
6056 				 * The block comment at the start of ipif_down
6057 				 * explains the use of the macros used below
6058 				 */
6059 				if (IPIF_CAN_LOOKUP(ipif)) {
6060 					ipif_refhold_locked(ipif);
6061 					mutex_exit(&ill->ill_lock);
6062 					RELEASE_CONN_LOCK(q);
6063 					rw_exit(&ipst->ips_ill_g_lock);
6064 					return (ipif);
6065 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6066 					ipsq = ill->ill_phyint->phyint_ipsq;
6067 					mutex_enter(&ipsq->ipsq_lock);
6068 					mutex_exit(&ill->ill_lock);
6069 					rw_exit(&ipst->ips_ill_g_lock);
6070 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6071 					    ill);
6072 					mutex_exit(&ipsq->ipsq_lock);
6073 					RELEASE_CONN_LOCK(q);
6074 					*error = EINPROGRESS;
6075 					return (NULL);
6076 				}
6077 			}
6078 		}
6079 		mutex_exit(&ill->ill_lock);
6080 		RELEASE_CONN_LOCK(q);
6081 	}
6082 	rw_exit(&ipst->ips_ill_g_lock);
6083 
6084 	/* lookup the ipif based on interface address */
6085 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
6086 	    ipst);
6087 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
6088 	return (ipif);
6089 }
6090 
6091 /*
6092  * Look for an ipif with the specified address. For point-point links
6093  * we look for matches on either the destination address and the local
6094  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6095  * is set.
6096  * Matches on a specific ill if match_ill is set.
6097  */
6098 ipif_t *
6099 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
6100     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
6101 {
6102 	ipif_t  *ipif;
6103 	ill_t   *ill;
6104 	boolean_t ptp = B_FALSE;
6105 	ipsq_t	*ipsq;
6106 	ill_walk_context_t	ctx;
6107 
6108 	if (error != NULL)
6109 		*error = 0;
6110 
6111 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6112 	/*
6113 	 * Repeat twice, first based on local addresses and
6114 	 * next time for pointopoint.
6115 	 */
6116 repeat:
6117 	ill = ILL_START_WALK_V4(&ctx, ipst);
6118 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6119 		if (match_ill != NULL && ill != match_ill) {
6120 			continue;
6121 		}
6122 		GRAB_CONN_LOCK(q);
6123 		mutex_enter(&ill->ill_lock);
6124 		for (ipif = ill->ill_ipif; ipif != NULL;
6125 		    ipif = ipif->ipif_next) {
6126 			if (zoneid != ALL_ZONES &&
6127 			    zoneid != ipif->ipif_zoneid &&
6128 			    ipif->ipif_zoneid != ALL_ZONES)
6129 				continue;
6130 			/* Allow the ipif to be down */
6131 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6132 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6133 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6134 			    (ipif->ipif_pp_dst_addr == addr))) {
6135 				/*
6136 				 * The block comment at the start of ipif_down
6137 				 * explains the use of the macros used below
6138 				 */
6139 				if (IPIF_CAN_LOOKUP(ipif)) {
6140 					ipif_refhold_locked(ipif);
6141 					mutex_exit(&ill->ill_lock);
6142 					RELEASE_CONN_LOCK(q);
6143 					rw_exit(&ipst->ips_ill_g_lock);
6144 					return (ipif);
6145 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6146 					ipsq = ill->ill_phyint->phyint_ipsq;
6147 					mutex_enter(&ipsq->ipsq_lock);
6148 					mutex_exit(&ill->ill_lock);
6149 					rw_exit(&ipst->ips_ill_g_lock);
6150 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6151 					    ill);
6152 					mutex_exit(&ipsq->ipsq_lock);
6153 					RELEASE_CONN_LOCK(q);
6154 					*error = EINPROGRESS;
6155 					return (NULL);
6156 				}
6157 			}
6158 		}
6159 		mutex_exit(&ill->ill_lock);
6160 		RELEASE_CONN_LOCK(q);
6161 	}
6162 
6163 	/* If we already did the ptp case, then we are done */
6164 	if (ptp) {
6165 		rw_exit(&ipst->ips_ill_g_lock);
6166 		if (error != NULL)
6167 			*error = ENXIO;
6168 		return (NULL);
6169 	}
6170 	ptp = B_TRUE;
6171 	goto repeat;
6172 }
6173 
6174 /*
6175  * Look for an ipif with the specified address. For point-point links
6176  * we look for matches on either the destination address and the local
6177  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6178  * is set.
6179  * Matches on a specific ill if match_ill is set.
6180  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6181  */
6182 zoneid_t
6183 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6184 {
6185 	zoneid_t zoneid;
6186 	ipif_t  *ipif;
6187 	ill_t   *ill;
6188 	boolean_t ptp = B_FALSE;
6189 	ill_walk_context_t	ctx;
6190 
6191 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6192 	/*
6193 	 * Repeat twice, first based on local addresses and
6194 	 * next time for pointopoint.
6195 	 */
6196 repeat:
6197 	ill = ILL_START_WALK_V4(&ctx, ipst);
6198 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6199 		if (match_ill != NULL && ill != match_ill) {
6200 			continue;
6201 		}
6202 		mutex_enter(&ill->ill_lock);
6203 		for (ipif = ill->ill_ipif; ipif != NULL;
6204 		    ipif = ipif->ipif_next) {
6205 			/* Allow the ipif to be down */
6206 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6207 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6208 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6209 			    (ipif->ipif_pp_dst_addr == addr)) &&
6210 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6211 				zoneid = ipif->ipif_zoneid;
6212 				mutex_exit(&ill->ill_lock);
6213 				rw_exit(&ipst->ips_ill_g_lock);
6214 				/*
6215 				 * If ipif_zoneid was ALL_ZONES then we have
6216 				 * a trusted extensions shared IP address.
6217 				 * In that case GLOBAL_ZONEID works to send.
6218 				 */
6219 				if (zoneid == ALL_ZONES)
6220 					zoneid = GLOBAL_ZONEID;
6221 				return (zoneid);
6222 			}
6223 		}
6224 		mutex_exit(&ill->ill_lock);
6225 	}
6226 
6227 	/* If we already did the ptp case, then we are done */
6228 	if (ptp) {
6229 		rw_exit(&ipst->ips_ill_g_lock);
6230 		return (ALL_ZONES);
6231 	}
6232 	ptp = B_TRUE;
6233 	goto repeat;
6234 }
6235 
6236 /*
6237  * Look for an ipif that matches the specified remote address i.e. the
6238  * ipif that would receive the specified packet.
6239  * First look for directly connected interfaces and then do a recursive
6240  * IRE lookup and pick the first ipif corresponding to the source address in the
6241  * ire.
6242  * Returns: held ipif
6243  */
6244 ipif_t *
6245 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6246 {
6247 	ipif_t	*ipif;
6248 	ire_t	*ire;
6249 	ip_stack_t	*ipst = ill->ill_ipst;
6250 
6251 	ASSERT(!ill->ill_isv6);
6252 
6253 	/*
6254 	 * Someone could be changing this ipif currently or change it
6255 	 * after we return this. Thus  a few packets could use the old
6256 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6257 	 * will atomically be updated or cleaned up with the new value
6258 	 * Thus we don't need a lock to check the flags or other attrs below.
6259 	 */
6260 	mutex_enter(&ill->ill_lock);
6261 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6262 		if (!IPIF_CAN_LOOKUP(ipif))
6263 			continue;
6264 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6265 		    ipif->ipif_zoneid != ALL_ZONES)
6266 			continue;
6267 		/* Allow the ipif to be down */
6268 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6269 			if ((ipif->ipif_pp_dst_addr == addr) ||
6270 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6271 			    ipif->ipif_lcl_addr == addr)) {
6272 				ipif_refhold_locked(ipif);
6273 				mutex_exit(&ill->ill_lock);
6274 				return (ipif);
6275 			}
6276 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6277 			ipif_refhold_locked(ipif);
6278 			mutex_exit(&ill->ill_lock);
6279 			return (ipif);
6280 		}
6281 	}
6282 	mutex_exit(&ill->ill_lock);
6283 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6284 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6285 	if (ire != NULL) {
6286 		/*
6287 		 * The callers of this function wants to know the
6288 		 * interface on which they have to send the replies
6289 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6290 		 * derived from different ills, we really don't care
6291 		 * what we return here.
6292 		 */
6293 		ipif = ire->ire_ipif;
6294 		if (ipif != NULL) {
6295 			ipif_refhold(ipif);
6296 			ire_refrele(ire);
6297 			return (ipif);
6298 		}
6299 		ire_refrele(ire);
6300 	}
6301 	/* Pick the first interface */
6302 	ipif = ipif_get_next_ipif(NULL, ill);
6303 	return (ipif);
6304 }
6305 
6306 /*
6307  * This func does not prevent refcnt from increasing. But if
6308  * the caller has taken steps to that effect, then this func
6309  * can be used to determine whether the ill has become quiescent
6310  */
6311 boolean_t
6312 ill_is_quiescent(ill_t *ill)
6313 {
6314 	ipif_t	*ipif;
6315 
6316 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6317 
6318 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6319 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6320 			return (B_FALSE);
6321 		}
6322 	}
6323 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6324 	    ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 ||
6325 	    ill->ill_mrtun_refcnt != 0) {
6326 		return (B_FALSE);
6327 	}
6328 	return (B_TRUE);
6329 }
6330 
6331 /*
6332  * This func does not prevent refcnt from increasing. But if
6333  * the caller has taken steps to that effect, then this func
6334  * can be used to determine whether the ipif has become quiescent
6335  */
6336 static boolean_t
6337 ipif_is_quiescent(ipif_t *ipif)
6338 {
6339 	ill_t *ill;
6340 
6341 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6342 
6343 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6344 		return (B_FALSE);
6345 	}
6346 
6347 	ill = ipif->ipif_ill;
6348 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6349 	    ill->ill_logical_down) {
6350 		return (B_TRUE);
6351 	}
6352 
6353 	/* This is the last ipif going down or being deleted on this ill */
6354 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6355 		return (B_FALSE);
6356 	}
6357 
6358 	return (B_TRUE);
6359 }
6360 
6361 /*
6362  * This func does not prevent refcnt from increasing. But if
6363  * the caller has taken steps to that effect, then this func
6364  * can be used to determine whether the ipifs marked with IPIF_MOVING
6365  * have become quiescent and can be moved in a failover/failback.
6366  */
6367 static ipif_t *
6368 ill_quiescent_to_move(ill_t *ill)
6369 {
6370 	ipif_t  *ipif;
6371 
6372 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6373 
6374 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6375 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6376 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6377 				return (ipif);
6378 			}
6379 		}
6380 	}
6381 	return (NULL);
6382 }
6383 
6384 /*
6385  * The ipif/ill/ire has been refreled. Do the tail processing.
6386  * Determine if the ipif or ill in question has become quiescent and if so
6387  * wakeup close and/or restart any queued pending ioctl that is waiting
6388  * for the ipif_down (or ill_down)
6389  */
6390 void
6391 ipif_ill_refrele_tail(ill_t *ill)
6392 {
6393 	mblk_t	*mp;
6394 	conn_t	*connp;
6395 	ipsq_t	*ipsq;
6396 	ipif_t	*ipif;
6397 	dl_notify_ind_t *dlindp;
6398 
6399 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6400 
6401 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6402 	    ill_is_quiescent(ill)) {
6403 		/* ill_close may be waiting */
6404 		cv_broadcast(&ill->ill_cv);
6405 	}
6406 
6407 	/* ipsq can't change because ill_lock  is held */
6408 	ipsq = ill->ill_phyint->phyint_ipsq;
6409 	if (ipsq->ipsq_waitfor == 0) {
6410 		/* Not waiting for anything, just return. */
6411 		mutex_exit(&ill->ill_lock);
6412 		return;
6413 	}
6414 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6415 	    ipsq->ipsq_pending_ipif != NULL);
6416 	/*
6417 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6418 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6419 	 * be zero for restarting an ioctl that ends up downing the ill.
6420 	 */
6421 	ipif = ipsq->ipsq_pending_ipif;
6422 	if (ipif->ipif_ill != ill) {
6423 		/* The ioctl is pending on some other ill. */
6424 		mutex_exit(&ill->ill_lock);
6425 		return;
6426 	}
6427 
6428 	switch (ipsq->ipsq_waitfor) {
6429 	case IPIF_DOWN:
6430 	case IPIF_FREE:
6431 		if (!ipif_is_quiescent(ipif)) {
6432 			mutex_exit(&ill->ill_lock);
6433 			return;
6434 		}
6435 		break;
6436 
6437 	case ILL_DOWN:
6438 	case ILL_FREE:
6439 		/*
6440 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6441 		 * waits synchronously in ip_close, and no message is queued in
6442 		 * ipsq_pending_mp at all in this case
6443 		 */
6444 		if (!ill_is_quiescent(ill)) {
6445 			mutex_exit(&ill->ill_lock);
6446 			return;
6447 		}
6448 
6449 		break;
6450 
6451 	case ILL_MOVE_OK:
6452 		if (ill_quiescent_to_move(ill) != NULL) {
6453 			mutex_exit(&ill->ill_lock);
6454 			return;
6455 		}
6456 
6457 		break;
6458 	default:
6459 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6460 		    (void *)ipsq, ipsq->ipsq_waitfor);
6461 	}
6462 
6463 	/*
6464 	 * Incr refcnt for the qwriter_ip call below which
6465 	 * does a refrele
6466 	 */
6467 	ill_refhold_locked(ill);
6468 	mutex_exit(&ill->ill_lock);
6469 
6470 	mp = ipsq_pending_mp_get(ipsq, &connp);
6471 	ASSERT(mp != NULL);
6472 
6473 	/*
6474 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6475 	 * we can only get here when the current operation decides it
6476 	 * it needs to quiesce via ipsq_pending_mp_add().
6477 	 */
6478 	switch (mp->b_datap->db_type) {
6479 	case M_PCPROTO:
6480 	case M_PROTO:
6481 		/*
6482 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6483 		 */
6484 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6485 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6486 
6487 		switch (dlindp->dl_notification) {
6488 		case DL_NOTE_PHYS_ADDR:
6489 			qwriter_ip(ill, ill->ill_rq, mp,
6490 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6491 			return;
6492 		default:
6493 			ASSERT(0);
6494 		}
6495 		break;
6496 
6497 	case M_ERROR:
6498 	case M_HANGUP:
6499 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6500 		    B_TRUE);
6501 		return;
6502 
6503 	case M_IOCTL:
6504 	case M_IOCDATA:
6505 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6506 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6507 		return;
6508 
6509 	default:
6510 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6511 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6512 	}
6513 }
6514 
6515 #ifdef ILL_DEBUG
6516 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6517 void
6518 th_trace_rrecord(th_trace_t *th_trace)
6519 {
6520 	tr_buf_t *tr_buf;
6521 	uint_t lastref;
6522 
6523 	lastref = th_trace->th_trace_lastref;
6524 	lastref++;
6525 	if (lastref == TR_BUF_MAX)
6526 		lastref = 0;
6527 	th_trace->th_trace_lastref = lastref;
6528 	tr_buf = &th_trace->th_trbuf[lastref];
6529 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
6530 }
6531 
6532 th_trace_t *
6533 th_trace_ipif_lookup(ipif_t *ipif)
6534 {
6535 	int bucket_id;
6536 	th_trace_t *th_trace;
6537 
6538 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6539 
6540 	bucket_id = IP_TR_HASH(curthread);
6541 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6542 
6543 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
6544 	    th_trace = th_trace->th_next) {
6545 		if (th_trace->th_id == curthread)
6546 			return (th_trace);
6547 	}
6548 	return (NULL);
6549 }
6550 
6551 void
6552 ipif_trace_ref(ipif_t *ipif)
6553 {
6554 	int bucket_id;
6555 	th_trace_t *th_trace;
6556 
6557 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6558 
6559 	if (ipif->ipif_trace_disable)
6560 		return;
6561 
6562 	/*
6563 	 * Attempt to locate the trace buffer for the curthread.
6564 	 * If it does not exist, then allocate a new trace buffer
6565 	 * and link it in list of trace bufs for this ipif, at the head
6566 	 */
6567 	th_trace = th_trace_ipif_lookup(ipif);
6568 	if (th_trace == NULL) {
6569 		bucket_id = IP_TR_HASH(curthread);
6570 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6571 		    KM_NOSLEEP);
6572 		if (th_trace == NULL) {
6573 			ipif->ipif_trace_disable = B_TRUE;
6574 			ipif_trace_cleanup(ipif);
6575 			return;
6576 		}
6577 		th_trace->th_id = curthread;
6578 		th_trace->th_next = ipif->ipif_trace[bucket_id];
6579 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
6580 		if (th_trace->th_next != NULL)
6581 			th_trace->th_next->th_prev = &th_trace->th_next;
6582 		ipif->ipif_trace[bucket_id] = th_trace;
6583 	}
6584 	ASSERT(th_trace->th_refcnt >= 0 &&
6585 	    th_trace->th_refcnt < TR_BUF_MAX -1);
6586 	th_trace->th_refcnt++;
6587 	th_trace_rrecord(th_trace);
6588 }
6589 
6590 void
6591 ipif_untrace_ref(ipif_t *ipif)
6592 {
6593 	th_trace_t *th_trace;
6594 
6595 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6596 
6597 	if (ipif->ipif_trace_disable)
6598 		return;
6599 	th_trace = th_trace_ipif_lookup(ipif);
6600 	ASSERT(th_trace != NULL);
6601 	ASSERT(th_trace->th_refcnt > 0);
6602 
6603 	th_trace->th_refcnt--;
6604 	th_trace_rrecord(th_trace);
6605 }
6606 
6607 th_trace_t *
6608 th_trace_ill_lookup(ill_t *ill)
6609 {
6610 	th_trace_t *th_trace;
6611 	int bucket_id;
6612 
6613 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6614 
6615 	bucket_id = IP_TR_HASH(curthread);
6616 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6617 
6618 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
6619 	    th_trace = th_trace->th_next) {
6620 		if (th_trace->th_id == curthread)
6621 			return (th_trace);
6622 	}
6623 	return (NULL);
6624 }
6625 
6626 void
6627 ill_trace_ref(ill_t *ill)
6628 {
6629 	int bucket_id;
6630 	th_trace_t *th_trace;
6631 
6632 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6633 	if (ill->ill_trace_disable)
6634 		return;
6635 	/*
6636 	 * Attempt to locate the trace buffer for the curthread.
6637 	 * If it does not exist, then allocate a new trace buffer
6638 	 * and link it in list of trace bufs for this ill, at the head
6639 	 */
6640 	th_trace = th_trace_ill_lookup(ill);
6641 	if (th_trace == NULL) {
6642 		bucket_id = IP_TR_HASH(curthread);
6643 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6644 		    KM_NOSLEEP);
6645 		if (th_trace == NULL) {
6646 			ill->ill_trace_disable = B_TRUE;
6647 			ill_trace_cleanup(ill);
6648 			return;
6649 		}
6650 		th_trace->th_id = curthread;
6651 		th_trace->th_next = ill->ill_trace[bucket_id];
6652 		th_trace->th_prev = &ill->ill_trace[bucket_id];
6653 		if (th_trace->th_next != NULL)
6654 			th_trace->th_next->th_prev = &th_trace->th_next;
6655 		ill->ill_trace[bucket_id] = th_trace;
6656 	}
6657 	ASSERT(th_trace->th_refcnt >= 0 &&
6658 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6659 
6660 	th_trace->th_refcnt++;
6661 	th_trace_rrecord(th_trace);
6662 }
6663 
6664 void
6665 ill_untrace_ref(ill_t *ill)
6666 {
6667 	th_trace_t *th_trace;
6668 
6669 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6670 
6671 	if (ill->ill_trace_disable)
6672 		return;
6673 	th_trace = th_trace_ill_lookup(ill);
6674 	ASSERT(th_trace != NULL);
6675 	ASSERT(th_trace->th_refcnt > 0);
6676 
6677 	th_trace->th_refcnt--;
6678 	th_trace_rrecord(th_trace);
6679 }
6680 
6681 /*
6682  * Verify that this thread has no refs to the ipif and free
6683  * the trace buffers
6684  */
6685 /* ARGSUSED */
6686 void
6687 ipif_thread_exit(ipif_t *ipif, void *dummy)
6688 {
6689 	th_trace_t *th_trace;
6690 
6691 	mutex_enter(&ipif->ipif_ill->ill_lock);
6692 
6693 	th_trace = th_trace_ipif_lookup(ipif);
6694 	if (th_trace == NULL) {
6695 		mutex_exit(&ipif->ipif_ill->ill_lock);
6696 		return;
6697 	}
6698 	ASSERT(th_trace->th_refcnt == 0);
6699 	/* unlink th_trace and free it */
6700 	*th_trace->th_prev = th_trace->th_next;
6701 	if (th_trace->th_next != NULL)
6702 		th_trace->th_next->th_prev = th_trace->th_prev;
6703 	th_trace->th_next = NULL;
6704 	th_trace->th_prev = NULL;
6705 	kmem_free(th_trace, sizeof (th_trace_t));
6706 
6707 	mutex_exit(&ipif->ipif_ill->ill_lock);
6708 }
6709 
6710 /*
6711  * Verify that this thread has no refs to the ill and free
6712  * the trace buffers
6713  */
6714 /* ARGSUSED */
6715 void
6716 ill_thread_exit(ill_t *ill, void *dummy)
6717 {
6718 	th_trace_t *th_trace;
6719 
6720 	mutex_enter(&ill->ill_lock);
6721 
6722 	th_trace = th_trace_ill_lookup(ill);
6723 	if (th_trace == NULL) {
6724 		mutex_exit(&ill->ill_lock);
6725 		return;
6726 	}
6727 	ASSERT(th_trace->th_refcnt == 0);
6728 	/* unlink th_trace and free it */
6729 	*th_trace->th_prev = th_trace->th_next;
6730 	if (th_trace->th_next != NULL)
6731 		th_trace->th_next->th_prev = th_trace->th_prev;
6732 	th_trace->th_next = NULL;
6733 	th_trace->th_prev = NULL;
6734 	kmem_free(th_trace, sizeof (th_trace_t));
6735 
6736 	mutex_exit(&ill->ill_lock);
6737 }
6738 #endif
6739 
6740 #ifdef ILL_DEBUG
6741 void
6742 ip_thread_exit_stack(ip_stack_t *ipst)
6743 {
6744 	ill_t	*ill;
6745 	ipif_t	*ipif;
6746 	ill_walk_context_t	ctx;
6747 
6748 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6749 	ill = ILL_START_WALK_ALL(&ctx, ipst);
6750 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6751 		for (ipif = ill->ill_ipif; ipif != NULL;
6752 		    ipif = ipif->ipif_next) {
6753 			ipif_thread_exit(ipif, NULL);
6754 		}
6755 		ill_thread_exit(ill, NULL);
6756 	}
6757 	rw_exit(&ipst->ips_ill_g_lock);
6758 
6759 	ire_walk(ire_thread_exit, NULL, ipst);
6760 	ndp_walk_common(ipst->ips_ndp4, NULL, nce_thread_exit, NULL, B_FALSE);
6761 	ndp_walk_common(ipst->ips_ndp6, NULL, nce_thread_exit, NULL, B_FALSE);
6762 }
6763 
6764 /*
6765  * This is a function which is called from thread_exit
6766  * that can be used to debug reference count issues in IP. See comment in
6767  * <inet/ip.h> on how it is used.
6768  */
6769 void
6770 ip_thread_exit(void)
6771 {
6772 	netstack_t *ns;
6773 
6774 	ns = netstack_get_current();
6775 	if (ns != NULL) {
6776 		ip_thread_exit_stack(ns->netstack_ip);
6777 		netstack_rele(ns);
6778 	}
6779 }
6780 
6781 /*
6782  * Called when ipif is unplumbed or when memory alloc fails
6783  */
6784 void
6785 ipif_trace_cleanup(ipif_t *ipif)
6786 {
6787 	int	i;
6788 	th_trace_t	*th_trace;
6789 	th_trace_t	*th_trace_next;
6790 
6791 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6792 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
6793 		    th_trace = th_trace_next) {
6794 			th_trace_next = th_trace->th_next;
6795 			kmem_free(th_trace, sizeof (th_trace_t));
6796 		}
6797 		ipif->ipif_trace[i] = NULL;
6798 	}
6799 }
6800 
6801 /*
6802  * Called when ill is unplumbed or when memory alloc fails
6803  */
6804 void
6805 ill_trace_cleanup(ill_t *ill)
6806 {
6807 	int	i;
6808 	th_trace_t	*th_trace;
6809 	th_trace_t	*th_trace_next;
6810 
6811 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6812 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6813 		    th_trace = th_trace_next) {
6814 			th_trace_next = th_trace->th_next;
6815 			kmem_free(th_trace, sizeof (th_trace_t));
6816 		}
6817 		ill->ill_trace[i] = NULL;
6818 	}
6819 }
6820 
6821 #else
6822 void ip_thread_exit(void) {}
6823 #endif
6824 
6825 void
6826 ipif_refhold_locked(ipif_t *ipif)
6827 {
6828 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6829 	ipif->ipif_refcnt++;
6830 	IPIF_TRACE_REF(ipif);
6831 }
6832 
6833 void
6834 ipif_refhold(ipif_t *ipif)
6835 {
6836 	ill_t	*ill;
6837 
6838 	ill = ipif->ipif_ill;
6839 	mutex_enter(&ill->ill_lock);
6840 	ipif->ipif_refcnt++;
6841 	IPIF_TRACE_REF(ipif);
6842 	mutex_exit(&ill->ill_lock);
6843 }
6844 
6845 /*
6846  * Must not be called while holding any locks. Otherwise if this is
6847  * the last reference to be released there is a chance of recursive mutex
6848  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6849  * to restart an ioctl.
6850  */
6851 void
6852 ipif_refrele(ipif_t *ipif)
6853 {
6854 	ill_t	*ill;
6855 
6856 	ill = ipif->ipif_ill;
6857 
6858 	mutex_enter(&ill->ill_lock);
6859 	ASSERT(ipif->ipif_refcnt != 0);
6860 	ipif->ipif_refcnt--;
6861 	IPIF_UNTRACE_REF(ipif);
6862 	if (ipif->ipif_refcnt != 0) {
6863 		mutex_exit(&ill->ill_lock);
6864 		return;
6865 	}
6866 
6867 	/* Drops the ill_lock */
6868 	ipif_ill_refrele_tail(ill);
6869 }
6870 
6871 ipif_t *
6872 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6873 {
6874 	ipif_t	*ipif;
6875 
6876 	mutex_enter(&ill->ill_lock);
6877 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6878 	    ipif != NULL; ipif = ipif->ipif_next) {
6879 		if (!IPIF_CAN_LOOKUP(ipif))
6880 			continue;
6881 		ipif_refhold_locked(ipif);
6882 		mutex_exit(&ill->ill_lock);
6883 		return (ipif);
6884 	}
6885 	mutex_exit(&ill->ill_lock);
6886 	return (NULL);
6887 }
6888 
6889 /*
6890  * TODO: make this table extendible at run time
6891  * Return a pointer to the mac type info for 'mac_type'
6892  */
6893 static ip_m_t *
6894 ip_m_lookup(t_uscalar_t mac_type)
6895 {
6896 	ip_m_t	*ipm;
6897 
6898 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6899 		if (ipm->ip_m_mac_type == mac_type)
6900 			return (ipm);
6901 	return (NULL);
6902 }
6903 
6904 /*
6905  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6906  * ipif_arg is passed in to associate it with the correct interface.
6907  * We may need to restart this operation if the ipif cannot be looked up
6908  * due to an exclusive operation that is currently in progress. The restart
6909  * entry point is specified by 'func'
6910  */
6911 int
6912 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6913     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6914     ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp,
6915     ipsq_func_t func, struct rtsa_s *sp, ip_stack_t *ipst)
6916 {
6917 	ire_t	*ire;
6918 	ire_t	*gw_ire = NULL;
6919 	ipif_t	*ipif = NULL;
6920 	boolean_t ipif_refheld = B_FALSE;
6921 	uint_t	type;
6922 	int	match_flags = MATCH_IRE_TYPE;
6923 	int	error;
6924 	tsol_gc_t *gc = NULL;
6925 	tsol_gcgrp_t *gcgrp = NULL;
6926 	boolean_t gcgrp_xtraref = B_FALSE;
6927 
6928 	ip1dbg(("ip_rt_add:"));
6929 
6930 	if (ire_arg != NULL)
6931 		*ire_arg = NULL;
6932 
6933 	/*
6934 	 * If this is the case of RTF_HOST being set, then we set the netmask
6935 	 * to all ones (regardless if one was supplied).
6936 	 */
6937 	if (flags & RTF_HOST)
6938 		mask = IP_HOST_MASK;
6939 
6940 	/*
6941 	 * Prevent routes with a zero gateway from being created (since
6942 	 * interfaces can currently be plumbed and brought up no assigned
6943 	 * address).
6944 	 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0.
6945 	 */
6946 	if (gw_addr == 0 && src_ipif == NULL)
6947 		return (ENETUNREACH);
6948 	/*
6949 	 * Get the ipif, if any, corresponding to the gw_addr
6950 	 */
6951 	if (gw_addr != 0) {
6952 		ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func,
6953 		    &error, ipst);
6954 		if (ipif != NULL) {
6955 			if (IS_VNI(ipif->ipif_ill)) {
6956 				ipif_refrele(ipif);
6957 				return (EINVAL);
6958 			}
6959 			ipif_refheld = B_TRUE;
6960 		} else if (error == EINPROGRESS) {
6961 			ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6962 			return (EINPROGRESS);
6963 		} else {
6964 			error = 0;
6965 		}
6966 	}
6967 
6968 	if (ipif != NULL) {
6969 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6970 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6971 	} else {
6972 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6973 	}
6974 
6975 	/*
6976 	 * GateD will attempt to create routes with a loopback interface
6977 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6978 	 * these routes to be added, but create them as interface routes
6979 	 * since the gateway is an interface address.
6980 	 */
6981 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6982 		flags &= ~RTF_GATEWAY;
6983 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6984 		    mask == IP_HOST_MASK) {
6985 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6986 			    ALL_ZONES, NULL, match_flags, ipst);
6987 			if (ire != NULL) {
6988 				ire_refrele(ire);
6989 				if (ipif_refheld)
6990 					ipif_refrele(ipif);
6991 				return (EEXIST);
6992 			}
6993 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6994 			    "for 0x%x\n", (void *)ipif,
6995 			    ipif->ipif_ire_type,
6996 			    ntohl(ipif->ipif_lcl_addr)));
6997 			ire = ire_create(
6998 			    (uchar_t *)&dst_addr,	/* dest address */
6999 			    (uchar_t *)&mask,		/* mask */
7000 			    (uchar_t *)&ipif->ipif_src_addr,
7001 			    NULL,			/* no gateway */
7002 			    NULL,
7003 			    &ipif->ipif_mtu,
7004 			    NULL,
7005 			    ipif->ipif_rq,		/* recv-from queue */
7006 			    NULL,			/* no send-to queue */
7007 			    ipif->ipif_ire_type,	/* LOOPBACK */
7008 			    ipif,
7009 			    NULL,
7010 			    0,
7011 			    0,
7012 			    0,
7013 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
7014 			    RTF_PRIVATE : 0,
7015 			    &ire_uinfo_null,
7016 			    NULL,
7017 			    NULL,
7018 			    ipst);
7019 
7020 			if (ire == NULL) {
7021 				if (ipif_refheld)
7022 					ipif_refrele(ipif);
7023 				return (ENOMEM);
7024 			}
7025 			error = ire_add(&ire, q, mp, func, B_FALSE);
7026 			if (error == 0)
7027 				goto save_ire;
7028 			if (ipif_refheld)
7029 				ipif_refrele(ipif);
7030 			return (error);
7031 
7032 		}
7033 	}
7034 
7035 	/*
7036 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
7037 	 * and the gateway address provided is one of the system's interface
7038 	 * addresses.  By using the routing socket interface and supplying an
7039 	 * RTA_IFP sockaddr with an interface index, an alternate method of
7040 	 * specifying an interface route to be created is available which uses
7041 	 * the interface index that specifies the outgoing interface rather than
7042 	 * the address of an outgoing interface (which may not be able to
7043 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
7044 	 * flag, routes can be specified which not only specify the next-hop to
7045 	 * be used when routing to a certain prefix, but also which outgoing
7046 	 * interface should be used.
7047 	 *
7048 	 * Previously, interfaces would have unique addresses assigned to them
7049 	 * and so the address assigned to a particular interface could be used
7050 	 * to identify a particular interface.  One exception to this was the
7051 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
7052 	 *
7053 	 * With the advent of IPv6 and its link-local addresses, this
7054 	 * restriction was relaxed and interfaces could share addresses between
7055 	 * themselves.  In fact, typically all of the link-local interfaces on
7056 	 * an IPv6 node or router will have the same link-local address.  In
7057 	 * order to differentiate between these interfaces, the use of an
7058 	 * interface index is necessary and this index can be carried inside a
7059 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
7060 	 * of using the interface index, however, is that all of the ipif's that
7061 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
7062 	 * cannot be used to differentiate between ipif's (or logical
7063 	 * interfaces) that belong to the same ill (physical interface).
7064 	 *
7065 	 * For example, in the following case involving IPv4 interfaces and
7066 	 * logical interfaces
7067 	 *
7068 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
7069 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
7070 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
7071 	 *
7072 	 * the ipif's corresponding to each of these interface routes can be
7073 	 * uniquely identified by the "gateway" (actually interface address).
7074 	 *
7075 	 * In this case involving multiple IPv6 default routes to a particular
7076 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
7077 	 * default route is of interest:
7078 	 *
7079 	 *	default		fe80::123:4567:89ab:cdef	U	if0
7080 	 *	default		fe80::123:4567:89ab:cdef	U	if1
7081 	 */
7082 
7083 	/* RTF_GATEWAY not set */
7084 	if (!(flags & RTF_GATEWAY)) {
7085 		queue_t	*stq;
7086 		queue_t	*rfq = NULL;
7087 		ill_t	*in_ill = NULL;
7088 
7089 		if (sp != NULL) {
7090 			ip2dbg(("ip_rt_add: gateway security attributes "
7091 			    "cannot be set with interface route\n"));
7092 			if (ipif_refheld)
7093 				ipif_refrele(ipif);
7094 			return (EINVAL);
7095 		}
7096 
7097 		/*
7098 		 * As the interface index specified with the RTA_IFP sockaddr is
7099 		 * the same for all ipif's off of an ill, the matching logic
7100 		 * below uses MATCH_IRE_ILL if such an index was specified.
7101 		 * This means that routes sharing the same prefix when added
7102 		 * using a RTA_IFP sockaddr must have distinct interface
7103 		 * indices (namely, they must be on distinct ill's).
7104 		 *
7105 		 * On the other hand, since the gateway address will usually be
7106 		 * different for each ipif on the system, the matching logic
7107 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
7108 		 * route.  This means that interface routes for the same prefix
7109 		 * can be created if they belong to distinct ipif's and if a
7110 		 * RTA_IFP sockaddr is not present.
7111 		 */
7112 		if (ipif_arg != NULL) {
7113 			if (ipif_refheld)  {
7114 				ipif_refrele(ipif);
7115 				ipif_refheld = B_FALSE;
7116 			}
7117 			ipif = ipif_arg;
7118 			match_flags |= MATCH_IRE_ILL;
7119 		} else {
7120 			/*
7121 			 * Check the ipif corresponding to the gw_addr
7122 			 */
7123 			if (ipif == NULL)
7124 				return (ENETUNREACH);
7125 			match_flags |= MATCH_IRE_IPIF;
7126 		}
7127 		ASSERT(ipif != NULL);
7128 		/*
7129 		 * If src_ipif is not NULL, we have to create
7130 		 * an ire with non-null ire_in_ill value
7131 		 */
7132 		if (src_ipif != NULL) {
7133 			in_ill = src_ipif->ipif_ill;
7134 		}
7135 
7136 		/*
7137 		 * We check for an existing entry at this point.
7138 		 *
7139 		 * Since a netmask isn't passed in via the ioctl interface
7140 		 * (SIOCADDRT), we don't check for a matching netmask in that
7141 		 * case.
7142 		 */
7143 		if (!ioctl_msg)
7144 			match_flags |= MATCH_IRE_MASK;
7145 		if (src_ipif != NULL) {
7146 			/* Look up in the special table */
7147 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
7148 			    ipif, src_ipif->ipif_ill, match_flags);
7149 		} else {
7150 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7151 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
7152 			    NULL, match_flags, ipst);
7153 		}
7154 		if (ire != NULL) {
7155 			ire_refrele(ire);
7156 			if (ipif_refheld)
7157 				ipif_refrele(ipif);
7158 			return (EEXIST);
7159 		}
7160 
7161 		if (src_ipif != NULL) {
7162 			/*
7163 			 * Create the special ire for the IRE table
7164 			 * which hangs out of ire_in_ill. This ire
7165 			 * is in-between IRE_CACHE and IRE_INTERFACE.
7166 			 * Thus rfq is non-NULL.
7167 			 */
7168 			rfq = ipif->ipif_rq;
7169 		}
7170 		/* Create the usual interface ires */
7171 
7172 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7173 		    ? ipif->ipif_rq : ipif->ipif_wq;
7174 
7175 		/*
7176 		 * Create a copy of the IRE_LOOPBACK,
7177 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7178 		 * the modified address and netmask.
7179 		 */
7180 		ire = ire_create(
7181 		    (uchar_t *)&dst_addr,
7182 		    (uint8_t *)&mask,
7183 		    (uint8_t *)&ipif->ipif_src_addr,
7184 		    NULL,
7185 		    NULL,
7186 		    &ipif->ipif_mtu,
7187 		    NULL,
7188 		    rfq,
7189 		    stq,
7190 		    ipif->ipif_net_type,
7191 		    ipif,
7192 		    in_ill,
7193 		    0,
7194 		    0,
7195 		    0,
7196 		    flags,
7197 		    &ire_uinfo_null,
7198 		    NULL,
7199 		    NULL,
7200 		    ipst);
7201 		if (ire == NULL) {
7202 			if (ipif_refheld)
7203 				ipif_refrele(ipif);
7204 			return (ENOMEM);
7205 		}
7206 
7207 		/*
7208 		 * Some software (for example, GateD and Sun Cluster) attempts
7209 		 * to create (what amount to) IRE_PREFIX routes with the
7210 		 * loopback address as the gateway.  This is primarily done to
7211 		 * set up prefixes with the RTF_REJECT flag set (for example,
7212 		 * when generating aggregate routes.)
7213 		 *
7214 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7215 		 * IRE_LOOPBACK, then we map the request into a
7216 		 * IRE_IF_NORESOLVER.
7217 		 *
7218 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7219 		 * routine, but rather using ire_create() directly.
7220 		 *
7221 		 */
7222 		if (ipif->ipif_net_type == IRE_LOOPBACK)
7223 			ire->ire_type = IRE_IF_NORESOLVER;
7224 
7225 		error = ire_add(&ire, q, mp, func, B_FALSE);
7226 		if (error == 0)
7227 			goto save_ire;
7228 
7229 		/*
7230 		 * In the result of failure, ire_add() will have already
7231 		 * deleted the ire in question, so there is no need to
7232 		 * do that here.
7233 		 */
7234 		if (ipif_refheld)
7235 			ipif_refrele(ipif);
7236 		return (error);
7237 	}
7238 	if (ipif_refheld) {
7239 		ipif_refrele(ipif);
7240 		ipif_refheld = B_FALSE;
7241 	}
7242 
7243 	if (src_ipif != NULL) {
7244 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
7245 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
7246 		return (EINVAL);
7247 	}
7248 	/*
7249 	 * Get an interface IRE for the specified gateway.
7250 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7251 	 * gateway, it is currently unreachable and we fail the request
7252 	 * accordingly.
7253 	 */
7254 	ipif = ipif_arg;
7255 	if (ipif_arg != NULL)
7256 		match_flags |= MATCH_IRE_ILL;
7257 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7258 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7259 	if (gw_ire == NULL)
7260 		return (ENETUNREACH);
7261 
7262 	/*
7263 	 * We create one of three types of IREs as a result of this request
7264 	 * based on the netmask.  A netmask of all ones (which is automatically
7265 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7266 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7267 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7268 	 * destination prefix.
7269 	 */
7270 	if (mask == IP_HOST_MASK)
7271 		type = IRE_HOST;
7272 	else if (mask == 0)
7273 		type = IRE_DEFAULT;
7274 	else
7275 		type = IRE_PREFIX;
7276 
7277 	/* check for a duplicate entry */
7278 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7279 	    NULL, ALL_ZONES, 0, NULL,
7280 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7281 	if (ire != NULL) {
7282 		ire_refrele(gw_ire);
7283 		ire_refrele(ire);
7284 		return (EEXIST);
7285 	}
7286 
7287 	/* Security attribute exists */
7288 	if (sp != NULL) {
7289 		tsol_gcgrp_addr_t ga;
7290 
7291 		/* find or create the gateway credentials group */
7292 		ga.ga_af = AF_INET;
7293 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7294 
7295 		/* we hold reference to it upon success */
7296 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7297 		if (gcgrp == NULL) {
7298 			ire_refrele(gw_ire);
7299 			return (ENOMEM);
7300 		}
7301 
7302 		/*
7303 		 * Create and add the security attribute to the group; a
7304 		 * reference to the group is made upon allocating a new
7305 		 * entry successfully.  If it finds an already-existing
7306 		 * entry for the security attribute in the group, it simply
7307 		 * returns it and no new reference is made to the group.
7308 		 */
7309 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7310 		if (gc == NULL) {
7311 			/* release reference held by gcgrp_lookup */
7312 			GCGRP_REFRELE(gcgrp);
7313 			ire_refrele(gw_ire);
7314 			return (ENOMEM);
7315 		}
7316 	}
7317 
7318 	/* Create the IRE. */
7319 	ire = ire_create(
7320 	    (uchar_t *)&dst_addr,		/* dest address */
7321 	    (uchar_t *)&mask,			/* mask */
7322 	    /* src address assigned by the caller? */
7323 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7324 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7325 	    (uchar_t *)&gw_addr,		/* gateway address */
7326 	    NULL,				/* no in-srcaddress */
7327 	    &gw_ire->ire_max_frag,
7328 	    NULL,				/* no src nce */
7329 	    NULL,				/* no recv-from queue */
7330 	    NULL,				/* no send-to queue */
7331 	    (ushort_t)type,			/* IRE type */
7332 	    ipif_arg,
7333 	    NULL,
7334 	    0,
7335 	    0,
7336 	    0,
7337 	    flags,
7338 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7339 	    gc,					/* security attribute */
7340 	    NULL,
7341 	    ipst);
7342 
7343 	/*
7344 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7345 	 * reference to the 'gcgrp'. We can now release the extra reference
7346 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7347 	 */
7348 	if (gcgrp_xtraref)
7349 		GCGRP_REFRELE(gcgrp);
7350 	if (ire == NULL) {
7351 		if (gc != NULL)
7352 			GC_REFRELE(gc);
7353 		ire_refrele(gw_ire);
7354 		return (ENOMEM);
7355 	}
7356 
7357 	/*
7358 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7359 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7360 	 */
7361 
7362 	/* Add the new IRE. */
7363 	error = ire_add(&ire, q, mp, func, B_FALSE);
7364 	if (error != 0) {
7365 		/*
7366 		 * In the result of failure, ire_add() will have already
7367 		 * deleted the ire in question, so there is no need to
7368 		 * do that here.
7369 		 */
7370 		ire_refrele(gw_ire);
7371 		return (error);
7372 	}
7373 
7374 	if (flags & RTF_MULTIRT) {
7375 		/*
7376 		 * Invoke the CGTP (multirouting) filtering module
7377 		 * to add the dst address in the filtering database.
7378 		 * Replicated inbound packets coming from that address
7379 		 * will be filtered to discard the duplicates.
7380 		 * It is not necessary to call the CGTP filter hook
7381 		 * when the dst address is a broadcast or multicast,
7382 		 * because an IP source address cannot be a broadcast
7383 		 * or a multicast.
7384 		 */
7385 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7386 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7387 		if (ire_dst != NULL) {
7388 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7389 			ire_refrele(ire_dst);
7390 			goto save_ire;
7391 		}
7392 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr) &&
7393 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
7394 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
7395 			    ire->ire_addr,
7396 			    ire->ire_gateway_addr,
7397 			    ire->ire_src_addr,
7398 			    gw_ire->ire_src_addr);
7399 			if (res != 0) {
7400 				ire_refrele(gw_ire);
7401 				ire_delete(ire);
7402 				return (res);
7403 			}
7404 		}
7405 	}
7406 
7407 	/*
7408 	 * Now that the prefix IRE entry has been created, delete any
7409 	 * existing gateway IRE cache entries as well as any IRE caches
7410 	 * using the gateway, and force them to be created through
7411 	 * ip_newroute.
7412 	 */
7413 	if (gc != NULL) {
7414 		ASSERT(gcgrp != NULL);
7415 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7416 	}
7417 
7418 save_ire:
7419 	if (gw_ire != NULL) {
7420 		ire_refrele(gw_ire);
7421 	}
7422 	/*
7423 	 * We do not do save_ire for the routes added with RTA_SRCIFP
7424 	 * flag. This route is only added and deleted by mipagent.
7425 	 * So, for simplicity of design, we refrain from saving
7426 	 * ires that are created with srcif value. This may change
7427 	 * in future if we find more usage of srcifp feature.
7428 	 */
7429 	if (ipif != NULL && src_ipif == NULL) {
7430 		/*
7431 		 * Save enough information so that we can recreate the IRE if
7432 		 * the interface goes down and then up.  The metrics associated
7433 		 * with the route will be saved as well when rts_setmetrics() is
7434 		 * called after the IRE has been created.  In the case where
7435 		 * memory cannot be allocated, none of this information will be
7436 		 * saved.
7437 		 */
7438 		ipif_save_ire(ipif, ire);
7439 	}
7440 	if (ioctl_msg)
7441 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7442 	if (ire_arg != NULL) {
7443 		/*
7444 		 * Store the ire that was successfully added into where ire_arg
7445 		 * points to so that callers don't have to look it up
7446 		 * themselves (but they are responsible for ire_refrele()ing
7447 		 * the ire when they are finished with it).
7448 		 */
7449 		*ire_arg = ire;
7450 	} else {
7451 		ire_refrele(ire);		/* Held in ire_add */
7452 	}
7453 	if (ipif_refheld)
7454 		ipif_refrele(ipif);
7455 	return (0);
7456 }
7457 
7458 /*
7459  * ip_rt_delete is called to delete an IPv4 route.
7460  * ipif_arg is passed in to associate it with the correct interface.
7461  * src_ipif is passed to associate the incoming interface of the packet.
7462  * We may need to restart this operation if the ipif cannot be looked up
7463  * due to an exclusive operation that is currently in progress. The restart
7464  * entry point is specified by 'func'
7465  */
7466 /* ARGSUSED4 */
7467 int
7468 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7469     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
7470     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
7471     ip_stack_t *ipst)
7472 {
7473 	ire_t	*ire = NULL;
7474 	ipif_t	*ipif;
7475 	boolean_t ipif_refheld = B_FALSE;
7476 	uint_t	type;
7477 	uint_t	match_flags = MATCH_IRE_TYPE;
7478 	int	err = 0;
7479 
7480 	ip1dbg(("ip_rt_delete:"));
7481 	/*
7482 	 * If this is the case of RTF_HOST being set, then we set the netmask
7483 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7484 	 */
7485 	if (flags & RTF_HOST) {
7486 		mask = IP_HOST_MASK;
7487 		match_flags |= MATCH_IRE_MASK;
7488 	} else if (rtm_addrs & RTA_NETMASK) {
7489 		match_flags |= MATCH_IRE_MASK;
7490 	}
7491 
7492 	/*
7493 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7494 	 * we check if the gateway address is one of our interfaces first,
7495 	 * and fall back on RTF_GATEWAY routes.
7496 	 *
7497 	 * This makes it possible to delete an original
7498 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7499 	 *
7500 	 * As the interface index specified with the RTA_IFP sockaddr is the
7501 	 * same for all ipif's off of an ill, the matching logic below uses
7502 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7503 	 * sharing the same prefix and interface index as the the route
7504 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7505 	 * is specified in the request.
7506 	 *
7507 	 * On the other hand, since the gateway address will usually be
7508 	 * different for each ipif on the system, the matching logic
7509 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7510 	 * route.  This means that interface routes for the same prefix can be
7511 	 * uniquely identified if they belong to distinct ipif's and if a
7512 	 * RTA_IFP sockaddr is not present.
7513 	 *
7514 	 * For more detail on specifying routes by gateway address and by
7515 	 * interface index, see the comments in ip_rt_add().
7516 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
7517 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
7518 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
7519 	 * succeed.
7520 	 */
7521 	if (src_ipif != NULL) {
7522 		if (ipif_arg == NULL && gw_addr != 0) {
7523 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
7524 			    q, mp, func, &err, ipst);
7525 			if (ipif_arg != NULL)
7526 				ipif_refheld = B_TRUE;
7527 		}
7528 		if (ipif_arg == NULL) {
7529 			err = (err == EINPROGRESS) ? err : ESRCH;
7530 			return (err);
7531 		}
7532 		ipif = ipif_arg;
7533 	} else {
7534 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
7535 		    q, mp, func, &err, ipst);
7536 		if (ipif != NULL)
7537 			ipif_refheld = B_TRUE;
7538 		else if (err == EINPROGRESS)
7539 			return (err);
7540 		else
7541 			err = 0;
7542 	}
7543 	if (ipif != NULL) {
7544 		if (ipif_arg != NULL) {
7545 			if (ipif_refheld) {
7546 				ipif_refrele(ipif);
7547 				ipif_refheld = B_FALSE;
7548 			}
7549 			ipif = ipif_arg;
7550 			match_flags |= MATCH_IRE_ILL;
7551 		} else {
7552 			match_flags |= MATCH_IRE_IPIF;
7553 		}
7554 		if (src_ipif != NULL) {
7555 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
7556 			    ipif, src_ipif->ipif_ill, match_flags);
7557 		} else {
7558 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7559 				ire = ire_ctable_lookup(dst_addr, 0,
7560 				    IRE_LOOPBACK, ipif, ALL_ZONES, NULL,
7561 				    match_flags, ipst);
7562 			}
7563 			if (ire == NULL) {
7564 				ire = ire_ftable_lookup(dst_addr, mask, 0,
7565 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
7566 				    NULL, match_flags, ipst);
7567 			}
7568 		}
7569 	}
7570 
7571 	if (ire == NULL) {
7572 		/*
7573 		 * At this point, the gateway address is not one of our own
7574 		 * addresses or a matching interface route was not found.  We
7575 		 * set the IRE type to lookup based on whether
7576 		 * this is a host route, a default route or just a prefix.
7577 		 *
7578 		 * If an ipif_arg was passed in, then the lookup is based on an
7579 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7580 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7581 		 * set as the route being looked up is not a traditional
7582 		 * interface route.
7583 		 * Since we do not add gateway route with srcipif, we don't
7584 		 * expect to find it either.
7585 		 */
7586 		if (src_ipif != NULL) {
7587 			if (ipif_refheld)
7588 				ipif_refrele(ipif);
7589 			return (ESRCH);
7590 		} else {
7591 			match_flags &= ~MATCH_IRE_IPIF;
7592 			match_flags |= MATCH_IRE_GW;
7593 			if (ipif_arg != NULL)
7594 				match_flags |= MATCH_IRE_ILL;
7595 			if (mask == IP_HOST_MASK)
7596 				type = IRE_HOST;
7597 			else if (mask == 0)
7598 				type = IRE_DEFAULT;
7599 			else
7600 				type = IRE_PREFIX;
7601 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
7602 			    ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags,
7603 			    ipst);
7604 		}
7605 	}
7606 
7607 	if (ipif_refheld)
7608 		ipif_refrele(ipif);
7609 
7610 	/* ipif is not refheld anymore */
7611 	if (ire == NULL)
7612 		return (ESRCH);
7613 
7614 	if (ire->ire_flags & RTF_MULTIRT) {
7615 		/*
7616 		 * Invoke the CGTP (multirouting) filtering module
7617 		 * to remove the dst address from the filtering database.
7618 		 * Packets coming from that address will no longer be
7619 		 * filtered to remove duplicates.
7620 		 */
7621 		if (ip_cgtp_filter_ops != NULL &&
7622 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
7623 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(
7624 			    ire->ire_addr, ire->ire_gateway_addr);
7625 		}
7626 		ip_cgtp_bcast_delete(ire, ipst);
7627 	}
7628 
7629 	ipif = ire->ire_ipif;
7630 	/*
7631 	 * Removing from ipif_saved_ire_mp is not necessary
7632 	 * when src_ipif being non-NULL. ip_rt_add does not
7633 	 * save the ires which src_ipif being non-NULL.
7634 	 */
7635 	if (ipif != NULL && src_ipif == NULL) {
7636 		ipif_remove_ire(ipif, ire);
7637 	}
7638 	if (ioctl_msg)
7639 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7640 	ire_delete(ire);
7641 	ire_refrele(ire);
7642 	return (err);
7643 }
7644 
7645 /*
7646  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7647  */
7648 /* ARGSUSED */
7649 int
7650 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7651     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7652 {
7653 	ipaddr_t dst_addr;
7654 	ipaddr_t gw_addr;
7655 	ipaddr_t mask;
7656 	int error = 0;
7657 	mblk_t *mp1;
7658 	struct rtentry *rt;
7659 	ipif_t *ipif = NULL;
7660 	ip_stack_t	*ipst;
7661 
7662 	ASSERT(q->q_next == NULL);
7663 	ipst = CONNQ_TO_IPST(q);
7664 
7665 	ip1dbg(("ip_siocaddrt:"));
7666 	/* Existence of mp1 verified in ip_wput_nondata */
7667 	mp1 = mp->b_cont->b_cont;
7668 	rt = (struct rtentry *)mp1->b_rptr;
7669 
7670 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7671 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7672 
7673 	/*
7674 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7675 	 * to a particular host address.  In this case, we set the netmask to
7676 	 * all ones for the particular destination address.  Otherwise,
7677 	 * determine the netmask to be used based on dst_addr and the interfaces
7678 	 * in use.
7679 	 */
7680 	if (rt->rt_flags & RTF_HOST) {
7681 		mask = IP_HOST_MASK;
7682 	} else {
7683 		/*
7684 		 * Note that ip_subnet_mask returns a zero mask in the case of
7685 		 * default (an all-zeroes address).
7686 		 */
7687 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7688 	}
7689 
7690 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7691 	    NULL, B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7692 	if (ipif != NULL)
7693 		ipif_refrele(ipif);
7694 	return (error);
7695 }
7696 
7697 /*
7698  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7699  */
7700 /* ARGSUSED */
7701 int
7702 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7703     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7704 {
7705 	ipaddr_t dst_addr;
7706 	ipaddr_t gw_addr;
7707 	ipaddr_t mask;
7708 	int error;
7709 	mblk_t *mp1;
7710 	struct rtentry *rt;
7711 	ipif_t *ipif = NULL;
7712 	ip_stack_t	*ipst;
7713 
7714 	ASSERT(q->q_next == NULL);
7715 	ipst = CONNQ_TO_IPST(q);
7716 
7717 	ip1dbg(("ip_siocdelrt:"));
7718 	/* Existence of mp1 verified in ip_wput_nondata */
7719 	mp1 = mp->b_cont->b_cont;
7720 	rt = (struct rtentry *)mp1->b_rptr;
7721 
7722 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7723 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7724 
7725 	/*
7726 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7727 	 * to a particular host address.  In this case, we set the netmask to
7728 	 * all ones for the particular destination address.  Otherwise,
7729 	 * determine the netmask to be used based on dst_addr and the interfaces
7730 	 * in use.
7731 	 */
7732 	if (rt->rt_flags & RTF_HOST) {
7733 		mask = IP_HOST_MASK;
7734 	} else {
7735 		/*
7736 		 * Note that ip_subnet_mask returns a zero mask in the case of
7737 		 * default (an all-zeroes address).
7738 		 */
7739 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7740 	}
7741 
7742 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7743 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
7744 	    B_TRUE, q, mp, ip_process_ioctl, ipst);
7745 	if (ipif != NULL)
7746 		ipif_refrele(ipif);
7747 	return (error);
7748 }
7749 
7750 /*
7751  * Enqueue the mp onto the ipsq, chained by b_next.
7752  * b_prev stores the function to be executed later, and b_queue the queue
7753  * where this mp originated.
7754  */
7755 void
7756 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7757     ill_t *pending_ill)
7758 {
7759 	conn_t	*connp = NULL;
7760 
7761 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7762 	ASSERT(func != NULL);
7763 
7764 	mp->b_queue = q;
7765 	mp->b_prev = (void *)func;
7766 	mp->b_next = NULL;
7767 
7768 	switch (type) {
7769 	case CUR_OP:
7770 		if (ipsq->ipsq_mptail != NULL) {
7771 			ASSERT(ipsq->ipsq_mphead != NULL);
7772 			ipsq->ipsq_mptail->b_next = mp;
7773 		} else {
7774 			ASSERT(ipsq->ipsq_mphead == NULL);
7775 			ipsq->ipsq_mphead = mp;
7776 		}
7777 		ipsq->ipsq_mptail = mp;
7778 		break;
7779 
7780 	case NEW_OP:
7781 		if (ipsq->ipsq_xopq_mptail != NULL) {
7782 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7783 			ipsq->ipsq_xopq_mptail->b_next = mp;
7784 		} else {
7785 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7786 			ipsq->ipsq_xopq_mphead = mp;
7787 		}
7788 		ipsq->ipsq_xopq_mptail = mp;
7789 		break;
7790 	default:
7791 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7792 	}
7793 
7794 	if (CONN_Q(q) && pending_ill != NULL) {
7795 		connp = Q_TO_CONN(q);
7796 
7797 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7798 		connp->conn_oper_pending_ill = pending_ill;
7799 	}
7800 }
7801 
7802 /*
7803  * Return the mp at the head of the ipsq. After emptying the ipsq
7804  * look at the next ioctl, if this ioctl is complete. Otherwise
7805  * return, we will resume when we complete the current ioctl.
7806  * The current ioctl will wait till it gets a response from the
7807  * driver below.
7808  */
7809 static mblk_t *
7810 ipsq_dq(ipsq_t *ipsq)
7811 {
7812 	mblk_t	*mp;
7813 
7814 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7815 
7816 	mp = ipsq->ipsq_mphead;
7817 	if (mp != NULL) {
7818 		ipsq->ipsq_mphead = mp->b_next;
7819 		if (ipsq->ipsq_mphead == NULL)
7820 			ipsq->ipsq_mptail = NULL;
7821 		mp->b_next = NULL;
7822 		return (mp);
7823 	}
7824 	if (ipsq->ipsq_current_ipif != NULL)
7825 		return (NULL);
7826 	mp = ipsq->ipsq_xopq_mphead;
7827 	if (mp != NULL) {
7828 		ipsq->ipsq_xopq_mphead = mp->b_next;
7829 		if (ipsq->ipsq_xopq_mphead == NULL)
7830 			ipsq->ipsq_xopq_mptail = NULL;
7831 		mp->b_next = NULL;
7832 		return (mp);
7833 	}
7834 	return (NULL);
7835 }
7836 
7837 /*
7838  * Enter the ipsq corresponding to ill, by waiting synchronously till
7839  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7840  * will have to drain completely before ipsq_enter returns success.
7841  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7842  * and the ipsq_exit logic will start the next enqueued ioctl after
7843  * completion of the current ioctl. If 'force' is used, we don't wait
7844  * for the enqueued ioctls. This is needed when a conn_close wants to
7845  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7846  * of an ill can also use this option. But we dont' use it currently.
7847  */
7848 #define	ENTER_SQ_WAIT_TICKS 100
7849 boolean_t
7850 ipsq_enter(ill_t *ill, boolean_t force)
7851 {
7852 	ipsq_t	*ipsq;
7853 	boolean_t waited_enough = B_FALSE;
7854 
7855 	/*
7856 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7857 	 * Since the <ill-ipsq> assocs could change while we wait for the
7858 	 * writer, it is easier to wait on a fixed global rather than try to
7859 	 * cv_wait on a changing ipsq.
7860 	 */
7861 	mutex_enter(&ill->ill_lock);
7862 	for (;;) {
7863 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7864 			mutex_exit(&ill->ill_lock);
7865 			return (B_FALSE);
7866 		}
7867 
7868 		ipsq = ill->ill_phyint->phyint_ipsq;
7869 		mutex_enter(&ipsq->ipsq_lock);
7870 		if (ipsq->ipsq_writer == NULL &&
7871 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7872 			break;
7873 		} else if (ipsq->ipsq_writer != NULL) {
7874 			mutex_exit(&ipsq->ipsq_lock);
7875 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7876 		} else {
7877 			mutex_exit(&ipsq->ipsq_lock);
7878 			if (force) {
7879 				(void) cv_timedwait(&ill->ill_cv,
7880 				    &ill->ill_lock,
7881 				    lbolt + ENTER_SQ_WAIT_TICKS);
7882 				waited_enough = B_TRUE;
7883 				continue;
7884 			} else {
7885 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7886 			}
7887 		}
7888 	}
7889 
7890 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7891 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7892 	ipsq->ipsq_writer = curthread;
7893 	ipsq->ipsq_reentry_cnt++;
7894 #ifdef ILL_DEBUG
7895 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7896 #endif
7897 	mutex_exit(&ipsq->ipsq_lock);
7898 	mutex_exit(&ill->ill_lock);
7899 	return (B_TRUE);
7900 }
7901 
7902 /*
7903  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7904  * certain critical operations like plumbing (i.e. most set ioctls),
7905  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7906  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7907  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7908  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7909  * threads executing in the ipsq. Responses from the driver pertain to the
7910  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7911  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7912  *
7913  * If a thread does not want to reenter the ipsq when it is already writer,
7914  * it must make sure that the specified reentry point to be called later
7915  * when the ipsq is empty, nor any code path starting from the specified reentry
7916  * point must never ever try to enter the ipsq again. Otherwise it can lead
7917  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7918  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7919  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7920  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7921  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7922  * ioctl if the current ioctl has completed. If the current ioctl is still
7923  * in progress it simply returns. The current ioctl could be waiting for
7924  * a response from another module (arp_ or the driver or could be waiting for
7925  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7926  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7927  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7928  * ipsq_current_ipif is clear which happens only on ioctl completion.
7929  */
7930 
7931 /*
7932  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7933  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7934  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7935  * completion.
7936  */
7937 ipsq_t *
7938 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7939     ipsq_func_t func, int type, boolean_t reentry_ok)
7940 {
7941 	ipsq_t	*ipsq;
7942 
7943 	/* Only 1 of ipif or ill can be specified */
7944 	ASSERT((ipif != NULL) ^ (ill != NULL));
7945 	if (ipif != NULL)
7946 		ill = ipif->ipif_ill;
7947 
7948 	/*
7949 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7950 	 * ipsq of an ill can't change when ill_lock is held.
7951 	 */
7952 	GRAB_CONN_LOCK(q);
7953 	mutex_enter(&ill->ill_lock);
7954 	ipsq = ill->ill_phyint->phyint_ipsq;
7955 	mutex_enter(&ipsq->ipsq_lock);
7956 
7957 	/*
7958 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7959 	 *    (Note: If the caller does not specify reentry_ok then neither
7960 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7961 	 *    again. Otherwise it can lead to an infinite loop
7962 	 * 2. Enter the ipsq if there is no current writer and this attempted
7963 	 *    entry is part of the current ioctl or operation
7964 	 * 3. Enter the ipsq if there is no current writer and this is a new
7965 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7966 	 *    empty and there is no ioctl (or operation) currently in progress
7967 	 */
7968 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7969 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7970 	    ipsq->ipsq_current_ipif == NULL))) ||
7971 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7972 		/* Success. */
7973 		ipsq->ipsq_reentry_cnt++;
7974 		ipsq->ipsq_writer = curthread;
7975 		mutex_exit(&ipsq->ipsq_lock);
7976 		mutex_exit(&ill->ill_lock);
7977 		RELEASE_CONN_LOCK(q);
7978 #ifdef ILL_DEBUG
7979 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7980 #endif
7981 		return (ipsq);
7982 	}
7983 
7984 	ipsq_enq(ipsq, q, mp, func, type, ill);
7985 
7986 	mutex_exit(&ipsq->ipsq_lock);
7987 	mutex_exit(&ill->ill_lock);
7988 	RELEASE_CONN_LOCK(q);
7989 	return (NULL);
7990 }
7991 
7992 /*
7993  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7994  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7995  * cannot be entered, the mp is queued for completion.
7996  */
7997 void
7998 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7999     boolean_t reentry_ok)
8000 {
8001 	ipsq_t	*ipsq;
8002 
8003 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
8004 
8005 	/*
8006 	 * Drop the caller's refhold on the ill.  This is safe since we either
8007 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
8008 	 * IPSQ, in which case we return without accessing ill anymore.  This
8009 	 * is needed because func needs to see the correct refcount.
8010 	 * e.g. removeif can work only then.
8011 	 */
8012 	ill_refrele(ill);
8013 	if (ipsq != NULL) {
8014 		(*func)(ipsq, q, mp, NULL);
8015 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
8016 	}
8017 }
8018 
8019 /*
8020  * If there are more than ILL_GRP_CNT ills in a group,
8021  * we use kmem alloc'd buffers, else use the stack
8022  */
8023 #define	ILL_GRP_CNT	14
8024 /*
8025  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
8026  * Called by a thread that is currently exclusive on this ipsq.
8027  */
8028 void
8029 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
8030 {
8031 	queue_t	*q;
8032 	mblk_t	*mp;
8033 	ipsq_func_t	func;
8034 	int	next;
8035 	ill_t	**ill_list = NULL;
8036 	size_t	ill_list_size = 0;
8037 	int	cnt = 0;
8038 	boolean_t need_ipsq_free = B_FALSE;
8039 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
8040 
8041 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8042 	mutex_enter(&ipsq->ipsq_lock);
8043 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
8044 	if (ipsq->ipsq_reentry_cnt != 1) {
8045 		ipsq->ipsq_reentry_cnt--;
8046 		mutex_exit(&ipsq->ipsq_lock);
8047 		return;
8048 	}
8049 
8050 	mp = ipsq_dq(ipsq);
8051 	while (mp != NULL) {
8052 again:
8053 		mutex_exit(&ipsq->ipsq_lock);
8054 		func = (ipsq_func_t)mp->b_prev;
8055 		q = (queue_t *)mp->b_queue;
8056 		mp->b_prev = NULL;
8057 		mp->b_queue = NULL;
8058 
8059 		/*
8060 		 * If 'q' is an conn queue, it is valid, since we did a
8061 		 * a refhold on the connp, at the start of the ioctl.
8062 		 * If 'q' is an ill queue, it is valid, since close of an
8063 		 * ill will clean up the 'ipsq'.
8064 		 */
8065 		(*func)(ipsq, q, mp, NULL);
8066 
8067 		mutex_enter(&ipsq->ipsq_lock);
8068 		mp = ipsq_dq(ipsq);
8069 	}
8070 
8071 	mutex_exit(&ipsq->ipsq_lock);
8072 
8073 	/*
8074 	 * Need to grab the locks in the right order. Need to
8075 	 * atomically check (under ipsq_lock) that there are no
8076 	 * messages before relinquishing the ipsq. Also need to
8077 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
8078 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
8079 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
8080 	 * to grab ill_g_lock as writer.
8081 	 */
8082 	rw_enter(&ipst->ips_ill_g_lock,
8083 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
8084 
8085 	/* ipsq_refs can't change while ill_g_lock is held as reader */
8086 	if (ipsq->ipsq_refs != 0) {
8087 		/* At most 2 ills v4/v6 per phyint */
8088 		cnt = ipsq->ipsq_refs << 1;
8089 		ill_list_size = cnt * sizeof (ill_t *);
8090 		/*
8091 		 * If memory allocation fails, we will do the split
8092 		 * the next time ipsq_exit is called for whatever reason.
8093 		 * As long as the ipsq_split flag is set the need to
8094 		 * split is remembered.
8095 		 */
8096 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
8097 		if (ill_list != NULL)
8098 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
8099 	}
8100 	mutex_enter(&ipsq->ipsq_lock);
8101 	mp = ipsq_dq(ipsq);
8102 	if (mp != NULL) {
8103 		/* oops, some message has landed up, we can't get out */
8104 		if (ill_list != NULL)
8105 			ill_unlock_ills(ill_list, cnt);
8106 		rw_exit(&ipst->ips_ill_g_lock);
8107 		if (ill_list != NULL)
8108 			kmem_free(ill_list, ill_list_size);
8109 		ill_list = NULL;
8110 		ill_list_size = 0;
8111 		cnt = 0;
8112 		goto again;
8113 	}
8114 
8115 	/*
8116 	 * Split only if no ioctl is pending and if memory alloc succeeded
8117 	 * above.
8118 	 */
8119 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
8120 	    ill_list != NULL) {
8121 		/*
8122 		 * No new ill can join this ipsq since we are holding the
8123 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
8124 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
8125 		 * If so we will retry on the next ipsq_exit.
8126 		 */
8127 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
8128 	}
8129 
8130 	/*
8131 	 * We are holding the ipsq lock, hence no new messages can
8132 	 * land up on the ipsq, and there are no messages currently.
8133 	 * Now safe to get out. Wake up waiters and relinquish ipsq
8134 	 * atomically while holding ill locks.
8135 	 */
8136 	ipsq->ipsq_writer = NULL;
8137 	ipsq->ipsq_reentry_cnt--;
8138 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
8139 #ifdef ILL_DEBUG
8140 	ipsq->ipsq_depth = 0;
8141 #endif
8142 	mutex_exit(&ipsq->ipsq_lock);
8143 	/*
8144 	 * For IPMP this should wake up all ills in this ipsq.
8145 	 * We need to hold the ill_lock while waking up waiters to
8146 	 * avoid missed wakeups. But there is no need to acquire all
8147 	 * the ill locks and then wakeup. If we have not acquired all
8148 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
8149 	 * wakes up ills one at a time after getting the right ill_lock
8150 	 */
8151 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
8152 	if (ill_list != NULL)
8153 		ill_unlock_ills(ill_list, cnt);
8154 	if (ipsq->ipsq_refs == 0)
8155 		need_ipsq_free = B_TRUE;
8156 	rw_exit(&ipst->ips_ill_g_lock);
8157 	if (ill_list != 0)
8158 		kmem_free(ill_list, ill_list_size);
8159 
8160 	if (need_ipsq_free) {
8161 		/*
8162 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
8163 		 * looked up. ipsq can be looked up only thru ill or phyint
8164 		 * and there are no ills/phyint on this ipsq.
8165 		 */
8166 		ipsq_delete(ipsq);
8167 	}
8168 	/*
8169 	 * Now start any igmp or mld timers that could not be started
8170 	 * while inside the ipsq. The timers can't be started while inside
8171 	 * the ipsq, since igmp_start_timers may need to call untimeout()
8172 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
8173 	 * there could be a deadlock since the timeout handlers
8174 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
8175 	 * wait in ipsq_enter() trying to get the ipsq.
8176 	 *
8177 	 * However there is one exception to the above. If this thread is
8178 	 * itself the igmp/mld timeout handler thread, then we don't want
8179 	 * to start any new timer until the current handler is done. The
8180 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
8181 	 * all others pass B_TRUE.
8182 	 */
8183 	if (start_igmp_timer) {
8184 		mutex_enter(&ipst->ips_igmp_timer_lock);
8185 		next = ipst->ips_igmp_deferred_next;
8186 		ipst->ips_igmp_deferred_next = INFINITY;
8187 		mutex_exit(&ipst->ips_igmp_timer_lock);
8188 
8189 		if (next != INFINITY)
8190 			igmp_start_timers(next, ipst);
8191 	}
8192 
8193 	if (start_mld_timer) {
8194 		mutex_enter(&ipst->ips_mld_timer_lock);
8195 		next = ipst->ips_mld_deferred_next;
8196 		ipst->ips_mld_deferred_next = INFINITY;
8197 		mutex_exit(&ipst->ips_mld_timer_lock);
8198 
8199 		if (next != INFINITY)
8200 			mld_start_timers(next, ipst);
8201 	}
8202 }
8203 
8204 /*
8205  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8206  * and `ioccmd'.
8207  */
8208 void
8209 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8210 {
8211 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8212 
8213 	mutex_enter(&ipsq->ipsq_lock);
8214 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8215 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8216 	ipsq->ipsq_current_ipif = ipif;
8217 	ipsq->ipsq_current_ioctl = ioccmd;
8218 	mutex_exit(&ipsq->ipsq_lock);
8219 }
8220 
8221 /*
8222  * Finish the current exclusive operation on `ipsq'.  Note that other
8223  * operations will not be able to proceed until an ipsq_exit() is done.
8224  */
8225 void
8226 ipsq_current_finish(ipsq_t *ipsq)
8227 {
8228 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8229 
8230 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8231 
8232 	/*
8233 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8234 	 * (but we're careful to never set IPIF_CHANGING in that case).
8235 	 */
8236 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8237 		mutex_enter(&ipif->ipif_ill->ill_lock);
8238 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8239 
8240 		/* Send any queued event */
8241 		ill_nic_info_dispatch(ipif->ipif_ill);
8242 		mutex_exit(&ipif->ipif_ill->ill_lock);
8243 	}
8244 
8245 	mutex_enter(&ipsq->ipsq_lock);
8246 	ASSERT(ipsq->ipsq_current_ipif != NULL);
8247 	ipsq->ipsq_current_ipif = NULL;
8248 	ipsq->ipsq_current_ioctl = 0;
8249 	mutex_exit(&ipsq->ipsq_lock);
8250 }
8251 
8252 /*
8253  * The ill is closing. Flush all messages on the ipsq that originated
8254  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8255  * for this ill since ipsq_enter could not have entered until then.
8256  * New messages can't be queued since the CONDEMNED flag is set.
8257  */
8258 static void
8259 ipsq_flush(ill_t *ill)
8260 {
8261 	queue_t	*q;
8262 	mblk_t	*prev;
8263 	mblk_t	*mp;
8264 	mblk_t	*mp_next;
8265 	ipsq_t	*ipsq;
8266 
8267 	ASSERT(IAM_WRITER_ILL(ill));
8268 	ipsq = ill->ill_phyint->phyint_ipsq;
8269 	/*
8270 	 * Flush any messages sent up by the driver.
8271 	 */
8272 	mutex_enter(&ipsq->ipsq_lock);
8273 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8274 		mp_next = mp->b_next;
8275 		q = mp->b_queue;
8276 		if (q == ill->ill_rq || q == ill->ill_wq) {
8277 			/* Remove the mp from the ipsq */
8278 			if (prev == NULL)
8279 				ipsq->ipsq_mphead = mp->b_next;
8280 			else
8281 				prev->b_next = mp->b_next;
8282 			if (ipsq->ipsq_mptail == mp) {
8283 				ASSERT(mp_next == NULL);
8284 				ipsq->ipsq_mptail = prev;
8285 			}
8286 			inet_freemsg(mp);
8287 		} else {
8288 			prev = mp;
8289 		}
8290 	}
8291 	mutex_exit(&ipsq->ipsq_lock);
8292 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8293 	ipsq_xopq_mp_cleanup(ill, NULL);
8294 	ill_pending_mp_cleanup(ill);
8295 }
8296 
8297 /* ARGSUSED */
8298 int
8299 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8300     ip_ioctl_cmd_t *ipip, void *ifreq)
8301 {
8302 	ill_t	*ill;
8303 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8304 	boolean_t isv6;
8305 	conn_t	*connp;
8306 	ip_stack_t	*ipst;
8307 
8308 	connp = Q_TO_CONN(q);
8309 	ipst = connp->conn_netstack->netstack_ip;
8310 	isv6 = connp->conn_af_isv6;
8311 	/*
8312 	 * Set original index.
8313 	 * Failover and failback move logical interfaces
8314 	 * from one physical interface to another.  The
8315 	 * original index indicates the parent of a logical
8316 	 * interface, in other words, the physical interface
8317 	 * the logical interface will be moved back to on
8318 	 * failback.
8319 	 */
8320 
8321 	/*
8322 	 * Don't allow the original index to be changed
8323 	 * for non-failover addresses, autoconfigured
8324 	 * addresses, or IPv6 link local addresses.
8325 	 */
8326 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8327 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8328 		return (EINVAL);
8329 	}
8330 	/*
8331 	 * The new original index must be in use by some
8332 	 * physical interface.
8333 	 */
8334 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8335 	    NULL, NULL, ipst);
8336 	if (ill == NULL)
8337 		return (ENXIO);
8338 	ill_refrele(ill);
8339 
8340 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8341 	/*
8342 	 * When this ipif gets failed back, don't
8343 	 * preserve the original id, as it is no
8344 	 * longer applicable.
8345 	 */
8346 	ipif->ipif_orig_ipifid = 0;
8347 	/*
8348 	 * For IPv4, change the original index of any
8349 	 * multicast addresses associated with the
8350 	 * ipif to the new value.
8351 	 */
8352 	if (!isv6) {
8353 		ilm_t *ilm;
8354 
8355 		mutex_enter(&ipif->ipif_ill->ill_lock);
8356 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8357 		    ilm = ilm->ilm_next) {
8358 			if (ilm->ilm_ipif == ipif) {
8359 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8360 			}
8361 		}
8362 		mutex_exit(&ipif->ipif_ill->ill_lock);
8363 	}
8364 	return (0);
8365 }
8366 
8367 /* ARGSUSED */
8368 int
8369 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8370     ip_ioctl_cmd_t *ipip, void *ifreq)
8371 {
8372 	struct lifreq *lifr = (struct lifreq *)ifreq;
8373 
8374 	/*
8375 	 * Get the original interface index i.e the one
8376 	 * before FAILOVER if it ever happened.
8377 	 */
8378 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8379 	return (0);
8380 }
8381 
8382 /*
8383  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8384  * refhold and return the associated ipif
8385  */
8386 int
8387 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
8388 {
8389 	boolean_t exists;
8390 	struct iftun_req *ta;
8391 	ipif_t	*ipif;
8392 	ill_t	*ill;
8393 	boolean_t isv6;
8394 	mblk_t	*mp1;
8395 	int	error;
8396 	conn_t	*connp;
8397 	ip_stack_t	*ipst;
8398 
8399 	/* Existence verified in ip_wput_nondata */
8400 	mp1 = mp->b_cont->b_cont;
8401 	ta = (struct iftun_req *)mp1->b_rptr;
8402 	/*
8403 	 * Null terminate the string to protect against buffer
8404 	 * overrun. String was generated by user code and may not
8405 	 * be trusted.
8406 	 */
8407 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8408 
8409 	connp = Q_TO_CONN(q);
8410 	isv6 = connp->conn_af_isv6;
8411 	ipst = connp->conn_netstack->netstack_ip;
8412 
8413 	/* Disallows implicit create */
8414 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8415 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8416 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8417 	if (ipif == NULL)
8418 		return (error);
8419 
8420 	if (ipif->ipif_id != 0) {
8421 		/*
8422 		 * We really don't want to set/get tunnel parameters
8423 		 * on virtual tunnel interfaces.  Only allow the
8424 		 * base tunnel to do these.
8425 		 */
8426 		ipif_refrele(ipif);
8427 		return (EINVAL);
8428 	}
8429 
8430 	/*
8431 	 * Send down to tunnel mod for ioctl processing.
8432 	 * Will finish ioctl in ip_rput_other().
8433 	 */
8434 	ill = ipif->ipif_ill;
8435 	if (ill->ill_net_type == IRE_LOOPBACK) {
8436 		ipif_refrele(ipif);
8437 		return (EOPNOTSUPP);
8438 	}
8439 
8440 	if (ill->ill_wq == NULL) {
8441 		ipif_refrele(ipif);
8442 		return (ENXIO);
8443 	}
8444 	/*
8445 	 * Mark the ioctl as coming from an IPv6 interface for
8446 	 * tun's convenience.
8447 	 */
8448 	if (ill->ill_isv6)
8449 		ta->ifta_flags |= 0x80000000;
8450 	*ipifp = ipif;
8451 	return (0);
8452 }
8453 
8454 /*
8455  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8456  * and return the associated ipif.
8457  * Return value:
8458  *	Non zero: An error has occurred. ci may not be filled out.
8459  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8460  *	a held ipif in ci.ci_ipif.
8461  */
8462 int
8463 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
8464     cmd_info_t *ci, ipsq_func_t func)
8465 {
8466 	sin_t		*sin;
8467 	sin6_t		*sin6;
8468 	char		*name;
8469 	struct ifreq    *ifr;
8470 	struct lifreq    *lifr;
8471 	ipif_t		*ipif = NULL;
8472 	ill_t		*ill;
8473 	conn_t		*connp;
8474 	boolean_t	isv6;
8475 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
8476 	boolean_t	exists;
8477 	int		err;
8478 	mblk_t		*mp1;
8479 	zoneid_t	zoneid;
8480 	ip_stack_t	*ipst;
8481 
8482 	if (q->q_next != NULL) {
8483 		ill = (ill_t *)q->q_ptr;
8484 		isv6 = ill->ill_isv6;
8485 		connp = NULL;
8486 		zoneid = ALL_ZONES;
8487 		ipst = ill->ill_ipst;
8488 	} else {
8489 		ill = NULL;
8490 		connp = Q_TO_CONN(q);
8491 		isv6 = connp->conn_af_isv6;
8492 		zoneid = connp->conn_zoneid;
8493 		if (zoneid == GLOBAL_ZONEID) {
8494 			/* global zone can access ipifs in all zones */
8495 			zoneid = ALL_ZONES;
8496 		}
8497 		ipst = connp->conn_netstack->netstack_ip;
8498 	}
8499 
8500 	/* Has been checked in ip_wput_nondata */
8501 	mp1 = mp->b_cont->b_cont;
8502 
8503 
8504 	if (cmd_type == IF_CMD) {
8505 		/* This a old style SIOC[GS]IF* command */
8506 		ifr = (struct ifreq *)mp1->b_rptr;
8507 		/*
8508 		 * Null terminate the string to protect against buffer
8509 		 * overrun. String was generated by user code and may not
8510 		 * be trusted.
8511 		 */
8512 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8513 		sin = (sin_t *)&ifr->ifr_addr;
8514 		name = ifr->ifr_name;
8515 		ci->ci_sin = sin;
8516 		ci->ci_sin6 = NULL;
8517 		ci->ci_lifr = (struct lifreq *)ifr;
8518 	} else {
8519 		/* This a new style SIOC[GS]LIF* command */
8520 		ASSERT(cmd_type == LIF_CMD);
8521 		lifr = (struct lifreq *)mp1->b_rptr;
8522 		/*
8523 		 * Null terminate the string to protect against buffer
8524 		 * overrun. String was generated by user code and may not
8525 		 * be trusted.
8526 		 */
8527 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8528 		name = lifr->lifr_name;
8529 		sin = (sin_t *)&lifr->lifr_addr;
8530 		sin6 = (sin6_t *)&lifr->lifr_addr;
8531 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
8532 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8533 			    LIFNAMSIZ);
8534 		}
8535 		ci->ci_sin = sin;
8536 		ci->ci_sin6 = sin6;
8537 		ci->ci_lifr = lifr;
8538 	}
8539 
8540 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
8541 		/*
8542 		 * The ioctl will be failed if the ioctl comes down
8543 		 * an conn stream
8544 		 */
8545 		if (ill == NULL) {
8546 			/*
8547 			 * Not an ill queue, return EINVAL same as the
8548 			 * old error code.
8549 			 */
8550 			return (ENXIO);
8551 		}
8552 		ipif = ill->ill_ipif;
8553 		ipif_refhold(ipif);
8554 	} else {
8555 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8556 		    &exists, isv6, zoneid,
8557 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8558 		    ipst);
8559 		if (ipif == NULL) {
8560 			if (err == EINPROGRESS)
8561 				return (err);
8562 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
8563 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
8564 				/*
8565 				 * Need to try both v4 and v6 since this
8566 				 * ioctl can come down either v4 or v6
8567 				 * socket. The lifreq.lifr_family passed
8568 				 * down by this ioctl is AF_UNSPEC.
8569 				 */
8570 				ipif = ipif_lookup_on_name(name,
8571 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8572 				    zoneid, (connp == NULL) ? q :
8573 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8574 				if (err == EINPROGRESS)
8575 					return (err);
8576 			}
8577 			err = 0;	/* Ensure we don't use it below */
8578 		}
8579 	}
8580 
8581 	/*
8582 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8583 	 */
8584 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
8585 		ipif_refrele(ipif);
8586 		return (ENXIO);
8587 	}
8588 
8589 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8590 	    name[0] == '\0') {
8591 		/*
8592 		 * Handle a or a SIOC?IF* with a null name
8593 		 * during plumb (on the ill queue before the I_PLINK).
8594 		 */
8595 		ipif = ill->ill_ipif;
8596 		ipif_refhold(ipif);
8597 	}
8598 
8599 	if (ipif == NULL)
8600 		return (ENXIO);
8601 
8602 	/*
8603 	 * Allow only GET operations if this ipif has been created
8604 	 * temporarily due to a MOVE operation.
8605 	 */
8606 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
8607 		ipif_refrele(ipif);
8608 		return (EINVAL);
8609 	}
8610 
8611 	ci->ci_ipif = ipif;
8612 	return (0);
8613 }
8614 
8615 /*
8616  * Return the total number of ipifs.
8617  */
8618 static uint_t
8619 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8620 {
8621 	uint_t numifs = 0;
8622 	ill_t	*ill;
8623 	ill_walk_context_t	ctx;
8624 	ipif_t	*ipif;
8625 
8626 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8627 	ill = ILL_START_WALK_V4(&ctx, ipst);
8628 
8629 	while (ill != NULL) {
8630 		for (ipif = ill->ill_ipif; ipif != NULL;
8631 		    ipif = ipif->ipif_next) {
8632 			if (ipif->ipif_zoneid == zoneid ||
8633 			    ipif->ipif_zoneid == ALL_ZONES)
8634 				numifs++;
8635 		}
8636 		ill = ill_next(&ctx, ill);
8637 	}
8638 	rw_exit(&ipst->ips_ill_g_lock);
8639 	return (numifs);
8640 }
8641 
8642 /*
8643  * Return the total number of ipifs.
8644  */
8645 static uint_t
8646 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8647 {
8648 	uint_t numifs = 0;
8649 	ill_t	*ill;
8650 	ipif_t	*ipif;
8651 	ill_walk_context_t	ctx;
8652 
8653 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8654 
8655 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8656 	if (family == AF_INET)
8657 		ill = ILL_START_WALK_V4(&ctx, ipst);
8658 	else if (family == AF_INET6)
8659 		ill = ILL_START_WALK_V6(&ctx, ipst);
8660 	else
8661 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8662 
8663 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8664 		for (ipif = ill->ill_ipif; ipif != NULL;
8665 		    ipif = ipif->ipif_next) {
8666 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8667 			    !(lifn_flags & LIFC_NOXMIT))
8668 				continue;
8669 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8670 			    !(lifn_flags & LIFC_TEMPORARY))
8671 				continue;
8672 			if (((ipif->ipif_flags &
8673 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8674 			    IPIF_DEPRECATED)) ||
8675 			    IS_LOOPBACK(ill) ||
8676 			    !(ipif->ipif_flags & IPIF_UP)) &&
8677 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8678 				continue;
8679 
8680 			if (zoneid != ipif->ipif_zoneid &&
8681 			    ipif->ipif_zoneid != ALL_ZONES &&
8682 			    (zoneid != GLOBAL_ZONEID ||
8683 			    !(lifn_flags & LIFC_ALLZONES)))
8684 				continue;
8685 
8686 			numifs++;
8687 		}
8688 	}
8689 	rw_exit(&ipst->ips_ill_g_lock);
8690 	return (numifs);
8691 }
8692 
8693 uint_t
8694 ip_get_lifsrcofnum(ill_t *ill)
8695 {
8696 	uint_t numifs = 0;
8697 	ill_t	*ill_head = ill;
8698 	ip_stack_t	*ipst = ill->ill_ipst;
8699 
8700 	/*
8701 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8702 	 * other thread may be trying to relink the ILLs in this usesrc group
8703 	 * and adjusting the ill_usesrc_grp_next pointers
8704 	 */
8705 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8706 	if ((ill->ill_usesrc_ifindex == 0) &&
8707 	    (ill->ill_usesrc_grp_next != NULL)) {
8708 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8709 		    ill = ill->ill_usesrc_grp_next)
8710 			numifs++;
8711 	}
8712 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8713 
8714 	return (numifs);
8715 }
8716 
8717 /* Null values are passed in for ipif, sin, and ifreq */
8718 /* ARGSUSED */
8719 int
8720 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8721     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8722 {
8723 	int *nump;
8724 	conn_t *connp = Q_TO_CONN(q);
8725 
8726 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8727 
8728 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8729 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8730 
8731 	*nump = ip_get_numifs(connp->conn_zoneid,
8732 	    connp->conn_netstack->netstack_ip);
8733 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8734 	return (0);
8735 }
8736 
8737 /* Null values are passed in for ipif, sin, and ifreq */
8738 /* ARGSUSED */
8739 int
8740 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8741     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8742 {
8743 	struct lifnum *lifn;
8744 	mblk_t	*mp1;
8745 	conn_t *connp = Q_TO_CONN(q);
8746 
8747 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8748 
8749 	/* Existence checked in ip_wput_nondata */
8750 	mp1 = mp->b_cont->b_cont;
8751 
8752 	lifn = (struct lifnum *)mp1->b_rptr;
8753 	switch (lifn->lifn_family) {
8754 	case AF_UNSPEC:
8755 	case AF_INET:
8756 	case AF_INET6:
8757 		break;
8758 	default:
8759 		return (EAFNOSUPPORT);
8760 	}
8761 
8762 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8763 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8764 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8765 	return (0);
8766 }
8767 
8768 /* ARGSUSED */
8769 int
8770 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8771     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8772 {
8773 	STRUCT_HANDLE(ifconf, ifc);
8774 	mblk_t *mp1;
8775 	struct iocblk *iocp;
8776 	struct ifreq *ifr;
8777 	ill_walk_context_t	ctx;
8778 	ill_t	*ill;
8779 	ipif_t	*ipif;
8780 	struct sockaddr_in *sin;
8781 	int32_t	ifclen;
8782 	zoneid_t zoneid;
8783 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8784 
8785 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8786 
8787 	ip1dbg(("ip_sioctl_get_ifconf"));
8788 	/* Existence verified in ip_wput_nondata */
8789 	mp1 = mp->b_cont->b_cont;
8790 	iocp = (struct iocblk *)mp->b_rptr;
8791 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8792 
8793 	/*
8794 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8795 	 * the user buffer address and length into which the list of struct
8796 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8797 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8798 	 * the SIOCGIFCONF operation was redefined to simply provide
8799 	 * a large output buffer into which we are supposed to jam the ifreq
8800 	 * array.  The same ioctl command code was used, despite the fact that
8801 	 * both the applications and the kernel code had to change, thus making
8802 	 * it impossible to support both interfaces.
8803 	 *
8804 	 * For reasons not good enough to try to explain, the following
8805 	 * algorithm is used for deciding what to do with one of these:
8806 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8807 	 * form with the output buffer coming down as the continuation message.
8808 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8809 	 * and we have to copy in the ifconf structure to find out how big the
8810 	 * output buffer is and where to copy out to.  Sure no problem...
8811 	 *
8812 	 */
8813 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8814 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8815 		int numifs = 0;
8816 		size_t ifc_bufsize;
8817 
8818 		/*
8819 		 * Must be (better be!) continuation of a TRANSPARENT
8820 		 * IOCTL.  We just copied in the ifconf structure.
8821 		 */
8822 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8823 		    (struct ifconf *)mp1->b_rptr);
8824 
8825 		/*
8826 		 * Allocate a buffer to hold requested information.
8827 		 *
8828 		 * If ifc_len is larger than what is needed, we only
8829 		 * allocate what we will use.
8830 		 *
8831 		 * If ifc_len is smaller than what is needed, return
8832 		 * EINVAL.
8833 		 *
8834 		 * XXX: the ill_t structure can hava 2 counters, for
8835 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8836 		 * number of interfaces for a device, so we don't need
8837 		 * to count them here...
8838 		 */
8839 		numifs = ip_get_numifs(zoneid, ipst);
8840 
8841 		ifclen = STRUCT_FGET(ifc, ifc_len);
8842 		ifc_bufsize = numifs * sizeof (struct ifreq);
8843 		if (ifc_bufsize > ifclen) {
8844 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8845 				/* old behaviour */
8846 				return (EINVAL);
8847 			} else {
8848 				ifc_bufsize = ifclen;
8849 			}
8850 		}
8851 
8852 		mp1 = mi_copyout_alloc(q, mp,
8853 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8854 		if (mp1 == NULL)
8855 			return (ENOMEM);
8856 
8857 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8858 	}
8859 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8860 	/*
8861 	 * the SIOCGIFCONF ioctl only knows about
8862 	 * IPv4 addresses, so don't try to tell
8863 	 * it about interfaces with IPv6-only
8864 	 * addresses. (Last parm 'isv6' is B_FALSE)
8865 	 */
8866 
8867 	ifr = (struct ifreq *)mp1->b_rptr;
8868 
8869 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8870 	ill = ILL_START_WALK_V4(&ctx, ipst);
8871 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8872 		for (ipif = ill->ill_ipif; ipif != NULL;
8873 		    ipif = ipif->ipif_next) {
8874 			if (zoneid != ipif->ipif_zoneid &&
8875 			    ipif->ipif_zoneid != ALL_ZONES)
8876 				continue;
8877 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8878 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8879 					/* old behaviour */
8880 					rw_exit(&ipst->ips_ill_g_lock);
8881 					return (EINVAL);
8882 				} else {
8883 					goto if_copydone;
8884 				}
8885 			}
8886 			(void) ipif_get_name(ipif,
8887 			    ifr->ifr_name,
8888 			    sizeof (ifr->ifr_name));
8889 			sin = (sin_t *)&ifr->ifr_addr;
8890 			*sin = sin_null;
8891 			sin->sin_family = AF_INET;
8892 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8893 			ifr++;
8894 		}
8895 	}
8896 if_copydone:
8897 	rw_exit(&ipst->ips_ill_g_lock);
8898 	mp1->b_wptr = (uchar_t *)ifr;
8899 
8900 	if (STRUCT_BUF(ifc) != NULL) {
8901 		STRUCT_FSET(ifc, ifc_len,
8902 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8903 	}
8904 	return (0);
8905 }
8906 
8907 /*
8908  * Get the interfaces using the address hosted on the interface passed in,
8909  * as a source adddress
8910  */
8911 /* ARGSUSED */
8912 int
8913 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8914     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8915 {
8916 	mblk_t *mp1;
8917 	ill_t	*ill, *ill_head;
8918 	ipif_t	*ipif, *orig_ipif;
8919 	int	numlifs = 0;
8920 	size_t	lifs_bufsize, lifsmaxlen;
8921 	struct	lifreq *lifr;
8922 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8923 	uint_t	ifindex;
8924 	zoneid_t zoneid;
8925 	int err = 0;
8926 	boolean_t isv6 = B_FALSE;
8927 	struct	sockaddr_in	*sin;
8928 	struct	sockaddr_in6	*sin6;
8929 	STRUCT_HANDLE(lifsrcof, lifs);
8930 	ip_stack_t		*ipst;
8931 
8932 	ipst = CONNQ_TO_IPST(q);
8933 
8934 	ASSERT(q->q_next == NULL);
8935 
8936 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8937 
8938 	/* Existence verified in ip_wput_nondata */
8939 	mp1 = mp->b_cont->b_cont;
8940 
8941 	/*
8942 	 * Must be (better be!) continuation of a TRANSPARENT
8943 	 * IOCTL.  We just copied in the lifsrcof structure.
8944 	 */
8945 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8946 	    (struct lifsrcof *)mp1->b_rptr);
8947 
8948 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8949 		return (EINVAL);
8950 
8951 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8952 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8953 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8954 	    ip_process_ioctl, &err, ipst);
8955 	if (ipif == NULL) {
8956 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8957 		    ifindex));
8958 		return (err);
8959 	}
8960 
8961 
8962 	/* Allocate a buffer to hold requested information */
8963 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8964 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8965 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8966 	/* The actual size needed is always returned in lifs_len */
8967 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8968 
8969 	/* If the amount we need is more than what is passed in, abort */
8970 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8971 		ipif_refrele(ipif);
8972 		return (0);
8973 	}
8974 
8975 	mp1 = mi_copyout_alloc(q, mp,
8976 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8977 	if (mp1 == NULL) {
8978 		ipif_refrele(ipif);
8979 		return (ENOMEM);
8980 	}
8981 
8982 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8983 	bzero(mp1->b_rptr, lifs_bufsize);
8984 
8985 	lifr = (struct lifreq *)mp1->b_rptr;
8986 
8987 	ill = ill_head = ipif->ipif_ill;
8988 	orig_ipif = ipif;
8989 
8990 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8991 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8992 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8993 
8994 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8995 	for (; (ill != NULL) && (ill != ill_head);
8996 	    ill = ill->ill_usesrc_grp_next) {
8997 
8998 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8999 			break;
9000 
9001 		ipif = ill->ill_ipif;
9002 		(void) ipif_get_name(ipif,
9003 		    lifr->lifr_name, sizeof (lifr->lifr_name));
9004 		if (ipif->ipif_isv6) {
9005 			sin6 = (sin6_t *)&lifr->lifr_addr;
9006 			*sin6 = sin6_null;
9007 			sin6->sin6_family = AF_INET6;
9008 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9009 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
9010 			    &ipif->ipif_v6net_mask);
9011 		} else {
9012 			sin = (sin_t *)&lifr->lifr_addr;
9013 			*sin = sin_null;
9014 			sin->sin_family = AF_INET;
9015 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9016 			lifr->lifr_addrlen = ip_mask_to_plen(
9017 			    ipif->ipif_net_mask);
9018 		}
9019 		lifr++;
9020 	}
9021 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
9022 	rw_exit(&ipst->ips_ill_g_lock);
9023 	ipif_refrele(orig_ipif);
9024 	mp1->b_wptr = (uchar_t *)lifr;
9025 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
9026 
9027 	return (0);
9028 }
9029 
9030 /* ARGSUSED */
9031 int
9032 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
9033     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9034 {
9035 	mblk_t *mp1;
9036 	int	list;
9037 	ill_t	*ill;
9038 	ipif_t	*ipif;
9039 	int	flags;
9040 	int	numlifs = 0;
9041 	size_t	lifc_bufsize;
9042 	struct	lifreq *lifr;
9043 	sa_family_t	family;
9044 	struct	sockaddr_in	*sin;
9045 	struct	sockaddr_in6	*sin6;
9046 	ill_walk_context_t	ctx;
9047 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9048 	int32_t	lifclen;
9049 	zoneid_t zoneid;
9050 	STRUCT_HANDLE(lifconf, lifc);
9051 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9052 
9053 	ip1dbg(("ip_sioctl_get_lifconf"));
9054 
9055 	ASSERT(q->q_next == NULL);
9056 
9057 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9058 
9059 	/* Existence verified in ip_wput_nondata */
9060 	mp1 = mp->b_cont->b_cont;
9061 
9062 	/*
9063 	 * An extended version of SIOCGIFCONF that takes an
9064 	 * additional address family and flags field.
9065 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
9066 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
9067 	 * interfaces are omitted.
9068 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
9069 	 * unless LIFC_TEMPORARY is specified.
9070 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
9071 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
9072 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
9073 	 * has priority over LIFC_NOXMIT.
9074 	 */
9075 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
9076 
9077 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
9078 		return (EINVAL);
9079 
9080 	/*
9081 	 * Must be (better be!) continuation of a TRANSPARENT
9082 	 * IOCTL.  We just copied in the lifconf structure.
9083 	 */
9084 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
9085 
9086 	family = STRUCT_FGET(lifc, lifc_family);
9087 	flags = STRUCT_FGET(lifc, lifc_flags);
9088 
9089 	switch (family) {
9090 	case AF_UNSPEC:
9091 		/*
9092 		 * walk all ILL's.
9093 		 */
9094 		list = MAX_G_HEADS;
9095 		break;
9096 	case AF_INET:
9097 		/*
9098 		 * walk only IPV4 ILL's.
9099 		 */
9100 		list = IP_V4_G_HEAD;
9101 		break;
9102 	case AF_INET6:
9103 		/*
9104 		 * walk only IPV6 ILL's.
9105 		 */
9106 		list = IP_V6_G_HEAD;
9107 		break;
9108 	default:
9109 		return (EAFNOSUPPORT);
9110 	}
9111 
9112 	/*
9113 	 * Allocate a buffer to hold requested information.
9114 	 *
9115 	 * If lifc_len is larger than what is needed, we only
9116 	 * allocate what we will use.
9117 	 *
9118 	 * If lifc_len is smaller than what is needed, return
9119 	 * EINVAL.
9120 	 */
9121 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
9122 	lifc_bufsize = numlifs * sizeof (struct lifreq);
9123 	lifclen = STRUCT_FGET(lifc, lifc_len);
9124 	if (lifc_bufsize > lifclen) {
9125 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
9126 			return (EINVAL);
9127 		else
9128 			lifc_bufsize = lifclen;
9129 	}
9130 
9131 	mp1 = mi_copyout_alloc(q, mp,
9132 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
9133 	if (mp1 == NULL)
9134 		return (ENOMEM);
9135 
9136 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
9137 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
9138 
9139 	lifr = (struct lifreq *)mp1->b_rptr;
9140 
9141 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9142 	ill = ill_first(list, list, &ctx, ipst);
9143 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9144 		for (ipif = ill->ill_ipif; ipif != NULL;
9145 		    ipif = ipif->ipif_next) {
9146 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
9147 			    !(flags & LIFC_NOXMIT))
9148 				continue;
9149 
9150 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
9151 			    !(flags & LIFC_TEMPORARY))
9152 				continue;
9153 
9154 			if (((ipif->ipif_flags &
9155 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
9156 			    IPIF_DEPRECATED)) ||
9157 			    IS_LOOPBACK(ill) ||
9158 			    !(ipif->ipif_flags & IPIF_UP)) &&
9159 			    (flags & LIFC_EXTERNAL_SOURCE))
9160 				continue;
9161 
9162 			if (zoneid != ipif->ipif_zoneid &&
9163 			    ipif->ipif_zoneid != ALL_ZONES &&
9164 			    (zoneid != GLOBAL_ZONEID ||
9165 			    !(flags & LIFC_ALLZONES)))
9166 				continue;
9167 
9168 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
9169 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
9170 					rw_exit(&ipst->ips_ill_g_lock);
9171 					return (EINVAL);
9172 				} else {
9173 					goto lif_copydone;
9174 				}
9175 			}
9176 
9177 			(void) ipif_get_name(ipif, lifr->lifr_name,
9178 			    sizeof (lifr->lifr_name));
9179 			if (ipif->ipif_isv6) {
9180 				sin6 = (sin6_t *)&lifr->lifr_addr;
9181 				*sin6 = sin6_null;
9182 				sin6->sin6_family = AF_INET6;
9183 				sin6->sin6_addr =
9184 				    ipif->ipif_v6lcl_addr;
9185 				lifr->lifr_addrlen =
9186 				    ip_mask_to_plen_v6(
9187 				    &ipif->ipif_v6net_mask);
9188 			} else {
9189 				sin = (sin_t *)&lifr->lifr_addr;
9190 				*sin = sin_null;
9191 				sin->sin_family = AF_INET;
9192 				sin->sin_addr.s_addr =
9193 				    ipif->ipif_lcl_addr;
9194 				lifr->lifr_addrlen =
9195 				    ip_mask_to_plen(
9196 				    ipif->ipif_net_mask);
9197 			}
9198 			lifr++;
9199 		}
9200 	}
9201 lif_copydone:
9202 	rw_exit(&ipst->ips_ill_g_lock);
9203 
9204 	mp1->b_wptr = (uchar_t *)lifr;
9205 	if (STRUCT_BUF(lifc) != NULL) {
9206 		STRUCT_FSET(lifc, lifc_len,
9207 		    (int)((uchar_t *)lifr - mp1->b_rptr));
9208 	}
9209 	return (0);
9210 }
9211 
9212 /* ARGSUSED */
9213 int
9214 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9215     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9216 {
9217 	ip_stack_t	*ipst;
9218 
9219 	if (q->q_next == NULL)
9220 		ipst = CONNQ_TO_IPST(q);
9221 	else
9222 		ipst = ILLQ_TO_IPST(q);
9223 
9224 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9225 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9226 	return (0);
9227 }
9228 
9229 static void
9230 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9231 {
9232 	ip6_asp_t *table;
9233 	size_t table_size;
9234 	mblk_t *data_mp;
9235 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9236 	ip_stack_t	*ipst;
9237 
9238 	if (q->q_next == NULL)
9239 		ipst = CONNQ_TO_IPST(q);
9240 	else
9241 		ipst = ILLQ_TO_IPST(q);
9242 
9243 	/* These two ioctls are I_STR only */
9244 	if (iocp->ioc_count == TRANSPARENT) {
9245 		miocnak(q, mp, 0, EINVAL);
9246 		return;
9247 	}
9248 
9249 	data_mp = mp->b_cont;
9250 	if (data_mp == NULL) {
9251 		/* The user passed us a NULL argument */
9252 		table = NULL;
9253 		table_size = iocp->ioc_count;
9254 	} else {
9255 		/*
9256 		 * The user provided a table.  The stream head
9257 		 * may have copied in the user data in chunks,
9258 		 * so make sure everything is pulled up
9259 		 * properly.
9260 		 */
9261 		if (MBLKL(data_mp) < iocp->ioc_count) {
9262 			mblk_t *new_data_mp;
9263 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9264 			    NULL) {
9265 				miocnak(q, mp, 0, ENOMEM);
9266 				return;
9267 			}
9268 			freemsg(data_mp);
9269 			data_mp = new_data_mp;
9270 			mp->b_cont = data_mp;
9271 		}
9272 		table = (ip6_asp_t *)data_mp->b_rptr;
9273 		table_size = iocp->ioc_count;
9274 	}
9275 
9276 	switch (iocp->ioc_cmd) {
9277 	case SIOCGIP6ADDRPOLICY:
9278 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9279 		if (iocp->ioc_rval == -1)
9280 			iocp->ioc_error = EINVAL;
9281 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9282 		else if (table != NULL &&
9283 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9284 			ip6_asp_t *src = table;
9285 			ip6_asp32_t *dst = (void *)table;
9286 			int count = table_size / sizeof (ip6_asp_t);
9287 			int i;
9288 
9289 			/*
9290 			 * We need to do an in-place shrink of the array
9291 			 * to match the alignment attributes of the
9292 			 * 32-bit ABI looking at it.
9293 			 */
9294 			/* LINTED: logical expression always true: op "||" */
9295 			ASSERT(sizeof (*src) > sizeof (*dst));
9296 			for (i = 1; i < count; i++)
9297 				bcopy(src + i, dst + i, sizeof (*dst));
9298 		}
9299 #endif
9300 		break;
9301 
9302 	case SIOCSIP6ADDRPOLICY:
9303 		ASSERT(mp->b_prev == NULL);
9304 		mp->b_prev = (void *)q;
9305 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9306 		/*
9307 		 * We pass in the datamodel here so that the ip6_asp_replace()
9308 		 * routine can handle converting from 32-bit to native formats
9309 		 * where necessary.
9310 		 *
9311 		 * A better way to handle this might be to convert the inbound
9312 		 * data structure here, and hang it off a new 'mp'; thus the
9313 		 * ip6_asp_replace() logic would always be dealing with native
9314 		 * format data structures..
9315 		 *
9316 		 * (An even simpler way to handle these ioctls is to just
9317 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9318 		 * and just recompile everything that depends on it.)
9319 		 */
9320 #endif
9321 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9322 		    iocp->ioc_flag & IOC_MODELS);
9323 		return;
9324 	}
9325 
9326 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9327 	qreply(q, mp);
9328 }
9329 
9330 static void
9331 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9332 {
9333 	mblk_t 		*data_mp;
9334 	struct dstinforeq	*dir;
9335 	uint8_t		*end, *cur;
9336 	in6_addr_t	*daddr, *saddr;
9337 	ipaddr_t	v4daddr;
9338 	ire_t		*ire;
9339 	char		*slabel, *dlabel;
9340 	boolean_t	isipv4;
9341 	int		match_ire;
9342 	ill_t		*dst_ill;
9343 	ipif_t		*src_ipif, *ire_ipif;
9344 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9345 	zoneid_t	zoneid;
9346 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9347 
9348 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9349 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9350 
9351 	/*
9352 	 * This ioctl is I_STR only, and must have a
9353 	 * data mblk following the M_IOCTL mblk.
9354 	 */
9355 	data_mp = mp->b_cont;
9356 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9357 		miocnak(q, mp, 0, EINVAL);
9358 		return;
9359 	}
9360 
9361 	if (MBLKL(data_mp) < iocp->ioc_count) {
9362 		mblk_t *new_data_mp;
9363 
9364 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9365 			miocnak(q, mp, 0, ENOMEM);
9366 			return;
9367 		}
9368 		freemsg(data_mp);
9369 		data_mp = new_data_mp;
9370 		mp->b_cont = data_mp;
9371 	}
9372 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9373 
9374 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9375 	    end - cur >= sizeof (struct dstinforeq);
9376 	    cur += sizeof (struct dstinforeq)) {
9377 		dir = (struct dstinforeq *)cur;
9378 		daddr = &dir->dir_daddr;
9379 		saddr = &dir->dir_saddr;
9380 
9381 		/*
9382 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9383 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9384 		 * and ipif_select_source[_v6]() do not.
9385 		 */
9386 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9387 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9388 
9389 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9390 		if (isipv4) {
9391 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9392 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9393 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9394 		} else {
9395 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9396 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9397 		}
9398 		if (ire == NULL) {
9399 			dir->dir_dreachable = 0;
9400 
9401 			/* move on to next dst addr */
9402 			continue;
9403 		}
9404 		dir->dir_dreachable = 1;
9405 
9406 		ire_ipif = ire->ire_ipif;
9407 		if (ire_ipif == NULL)
9408 			goto next_dst;
9409 
9410 		/*
9411 		 * We expect to get back an interface ire or a
9412 		 * gateway ire cache entry.  For both types, the
9413 		 * output interface is ire_ipif->ipif_ill.
9414 		 */
9415 		dst_ill = ire_ipif->ipif_ill;
9416 		dir->dir_dmactype = dst_ill->ill_mactype;
9417 
9418 		if (isipv4) {
9419 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9420 		} else {
9421 			src_ipif = ipif_select_source_v6(dst_ill,
9422 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9423 			    zoneid);
9424 		}
9425 		if (src_ipif == NULL)
9426 			goto next_dst;
9427 
9428 		*saddr = src_ipif->ipif_v6lcl_addr;
9429 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9430 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9431 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9432 		dir->dir_sdeprecated =
9433 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9434 		ipif_refrele(src_ipif);
9435 next_dst:
9436 		ire_refrele(ire);
9437 	}
9438 	miocack(q, mp, iocp->ioc_count, 0);
9439 }
9440 
9441 
9442 /*
9443  * Check if this is an address assigned to this machine.
9444  * Skips interfaces that are down by using ire checks.
9445  * Translates mapped addresses to v4 addresses and then
9446  * treats them as such, returning true if the v4 address
9447  * associated with this mapped address is configured.
9448  * Note: Applications will have to be careful what they do
9449  * with the response; use of mapped addresses limits
9450  * what can be done with the socket, especially with
9451  * respect to socket options and ioctls - neither IPv4
9452  * options nor IPv6 sticky options/ancillary data options
9453  * may be used.
9454  */
9455 /* ARGSUSED */
9456 int
9457 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9458     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9459 {
9460 	struct sioc_addrreq *sia;
9461 	sin_t *sin;
9462 	ire_t *ire;
9463 	mblk_t *mp1;
9464 	zoneid_t zoneid;
9465 	ip_stack_t	*ipst;
9466 
9467 	ip1dbg(("ip_sioctl_tmyaddr"));
9468 
9469 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9470 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9471 	ipst = CONNQ_TO_IPST(q);
9472 
9473 	/* Existence verified in ip_wput_nondata */
9474 	mp1 = mp->b_cont->b_cont;
9475 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9476 	sin = (sin_t *)&sia->sa_addr;
9477 	switch (sin->sin_family) {
9478 	case AF_INET6: {
9479 		sin6_t *sin6 = (sin6_t *)sin;
9480 
9481 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9482 			ipaddr_t v4_addr;
9483 
9484 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9485 			    v4_addr);
9486 			ire = ire_ctable_lookup(v4_addr, 0,
9487 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9488 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9489 		} else {
9490 			in6_addr_t v6addr;
9491 
9492 			v6addr = sin6->sin6_addr;
9493 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9494 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9495 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9496 		}
9497 		break;
9498 	}
9499 	case AF_INET: {
9500 		ipaddr_t v4addr;
9501 
9502 		v4addr = sin->sin_addr.s_addr;
9503 		ire = ire_ctable_lookup(v4addr, 0,
9504 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9505 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9506 		break;
9507 	}
9508 	default:
9509 		return (EAFNOSUPPORT);
9510 	}
9511 	if (ire != NULL) {
9512 		sia->sa_res = 1;
9513 		ire_refrele(ire);
9514 	} else {
9515 		sia->sa_res = 0;
9516 	}
9517 	return (0);
9518 }
9519 
9520 /*
9521  * Check if this is an address assigned on-link i.e. neighbor,
9522  * and makes sure it's reachable from the current zone.
9523  * Returns true for my addresses as well.
9524  * Translates mapped addresses to v4 addresses and then
9525  * treats them as such, returning true if the v4 address
9526  * associated with this mapped address is configured.
9527  * Note: Applications will have to be careful what they do
9528  * with the response; use of mapped addresses limits
9529  * what can be done with the socket, especially with
9530  * respect to socket options and ioctls - neither IPv4
9531  * options nor IPv6 sticky options/ancillary data options
9532  * may be used.
9533  */
9534 /* ARGSUSED */
9535 int
9536 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9537     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9538 {
9539 	struct sioc_addrreq *sia;
9540 	sin_t *sin;
9541 	mblk_t	*mp1;
9542 	ire_t *ire = NULL;
9543 	zoneid_t zoneid;
9544 	ip_stack_t	*ipst;
9545 
9546 	ip1dbg(("ip_sioctl_tonlink"));
9547 
9548 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9549 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9550 	ipst = CONNQ_TO_IPST(q);
9551 
9552 	/* Existence verified in ip_wput_nondata */
9553 	mp1 = mp->b_cont->b_cont;
9554 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9555 	sin = (sin_t *)&sia->sa_addr;
9556 
9557 	/*
9558 	 * Match addresses with a zero gateway field to avoid
9559 	 * routes going through a router.
9560 	 * Exclude broadcast and multicast addresses.
9561 	 */
9562 	switch (sin->sin_family) {
9563 	case AF_INET6: {
9564 		sin6_t *sin6 = (sin6_t *)sin;
9565 
9566 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9567 			ipaddr_t v4_addr;
9568 
9569 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9570 			    v4_addr);
9571 			if (!CLASSD(v4_addr)) {
9572 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9573 				    NULL, NULL, zoneid, NULL,
9574 				    MATCH_IRE_GW, ipst);
9575 			}
9576 		} else {
9577 			in6_addr_t v6addr;
9578 			in6_addr_t v6gw;
9579 
9580 			v6addr = sin6->sin6_addr;
9581 			v6gw = ipv6_all_zeros;
9582 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9583 				ire = ire_route_lookup_v6(&v6addr, 0,
9584 				    &v6gw, 0, NULL, NULL, zoneid,
9585 				    NULL, MATCH_IRE_GW, ipst);
9586 			}
9587 		}
9588 		break;
9589 	}
9590 	case AF_INET: {
9591 		ipaddr_t v4addr;
9592 
9593 		v4addr = sin->sin_addr.s_addr;
9594 		if (!CLASSD(v4addr)) {
9595 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9596 			    NULL, NULL, zoneid, NULL,
9597 			    MATCH_IRE_GW, ipst);
9598 		}
9599 		break;
9600 	}
9601 	default:
9602 		return (EAFNOSUPPORT);
9603 	}
9604 	sia->sa_res = 0;
9605 	if (ire != NULL) {
9606 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9607 		    IRE_LOCAL|IRE_LOOPBACK)) {
9608 			sia->sa_res = 1;
9609 		}
9610 		ire_refrele(ire);
9611 	}
9612 	return (0);
9613 }
9614 
9615 /*
9616  * TBD: implement when kernel maintaines a list of site prefixes.
9617  */
9618 /* ARGSUSED */
9619 int
9620 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9621     ip_ioctl_cmd_t *ipip, void *ifreq)
9622 {
9623 	return (ENXIO);
9624 }
9625 
9626 /* ARGSUSED */
9627 int
9628 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9629     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9630 {
9631 	ill_t  		*ill;
9632 	mblk_t		*mp1;
9633 	conn_t		*connp;
9634 	boolean_t	success;
9635 
9636 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9637 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9638 	/* ioctl comes down on an conn */
9639 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9640 	connp = Q_TO_CONN(q);
9641 
9642 	mp->b_datap->db_type = M_IOCTL;
9643 
9644 	/*
9645 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9646 	 * The original mp contains contaminated b_next values due to 'mi',
9647 	 * which is needed to do the mi_copy_done. Unfortunately if we
9648 	 * send down the original mblk itself and if we are popped due to an
9649 	 * an unplumb before the response comes back from tunnel,
9650 	 * the streamhead (which does a freemsg) will see this contaminated
9651 	 * message and the assertion in freemsg about non-null b_next/b_prev
9652 	 * will panic a DEBUG kernel.
9653 	 */
9654 	mp1 = copymsg(mp);
9655 	if (mp1 == NULL)
9656 		return (ENOMEM);
9657 
9658 	ill = ipif->ipif_ill;
9659 	mutex_enter(&connp->conn_lock);
9660 	mutex_enter(&ill->ill_lock);
9661 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9662 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9663 		    mp, 0);
9664 	} else {
9665 		success = ill_pending_mp_add(ill, connp, mp);
9666 	}
9667 	mutex_exit(&ill->ill_lock);
9668 	mutex_exit(&connp->conn_lock);
9669 
9670 	if (success) {
9671 		ip1dbg(("sending down tunparam request "));
9672 		putnext(ill->ill_wq, mp1);
9673 		return (EINPROGRESS);
9674 	} else {
9675 		/* The conn has started closing */
9676 		freemsg(mp1);
9677 		return (EINTR);
9678 	}
9679 }
9680 
9681 static int
9682 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
9683     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
9684 {
9685 	mblk_t *mp1;
9686 	mblk_t *mp2;
9687 	mblk_t *pending_mp;
9688 	ipaddr_t ipaddr;
9689 	area_t *area;
9690 	struct iocblk *iocp;
9691 	conn_t *connp;
9692 	struct arpreq *ar;
9693 	struct xarpreq *xar;
9694 	boolean_t success;
9695 	int flags, alength;
9696 	char *lladdr;
9697 	ip_stack_t	*ipst;
9698 
9699 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9700 	connp = Q_TO_CONN(q);
9701 	ipst = connp->conn_netstack->netstack_ip;
9702 
9703 	iocp = (struct iocblk *)mp->b_rptr;
9704 	/*
9705 	 * ill has already been set depending on whether
9706 	 * bsd style or interface style ioctl.
9707 	 */
9708 	ASSERT(ill != NULL);
9709 
9710 	/*
9711 	 * Is this one of the new SIOC*XARP ioctls?
9712 	 */
9713 	if (x_arp_ioctl) {
9714 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9715 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9716 		ar = NULL;
9717 
9718 		flags = xar->xarp_flags;
9719 		lladdr = LLADDR(&xar->xarp_ha);
9720 		/*
9721 		 * Validate against user's link layer address length
9722 		 * input and name and addr length limits.
9723 		 */
9724 		alength = ill->ill_phys_addr_length;
9725 		if (iocp->ioc_cmd == SIOCSXARP) {
9726 			if (alength != xar->xarp_ha.sdl_alen ||
9727 			    (alength + xar->xarp_ha.sdl_nlen >
9728 			    sizeof (xar->xarp_ha.sdl_data)))
9729 				return (EINVAL);
9730 		}
9731 	} else {
9732 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9733 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9734 		xar = NULL;
9735 
9736 		flags = ar->arp_flags;
9737 		lladdr = ar->arp_ha.sa_data;
9738 		/*
9739 		 * Theoretically, the sa_family could tell us what link
9740 		 * layer type this operation is trying to deal with. By
9741 		 * common usage AF_UNSPEC means ethernet. We'll assume
9742 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9743 		 * for now. Our new SIOC*XARP ioctls can be used more
9744 		 * generally.
9745 		 *
9746 		 * If the underlying media happens to have a non 6 byte
9747 		 * address, arp module will fail set/get, but the del
9748 		 * operation will succeed.
9749 		 */
9750 		alength = 6;
9751 		if ((iocp->ioc_cmd != SIOCDARP) &&
9752 		    (alength != ill->ill_phys_addr_length)) {
9753 			return (EINVAL);
9754 		}
9755 	}
9756 
9757 	/*
9758 	 * We are going to pass up to ARP a packet chain that looks
9759 	 * like:
9760 	 *
9761 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9762 	 *
9763 	 * Get a copy of the original IOCTL mblk to head the chain,
9764 	 * to be sent up (in mp1). Also get another copy to store
9765 	 * in the ill_pending_mp list, for matching the response
9766 	 * when it comes back from ARP.
9767 	 */
9768 	mp1 = copyb(mp);
9769 	pending_mp = copymsg(mp);
9770 	if (mp1 == NULL || pending_mp == NULL) {
9771 		if (mp1 != NULL)
9772 			freeb(mp1);
9773 		if (pending_mp != NULL)
9774 			inet_freemsg(pending_mp);
9775 		return (ENOMEM);
9776 	}
9777 
9778 	ipaddr = sin->sin_addr.s_addr;
9779 
9780 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9781 	    (caddr_t)&ipaddr);
9782 	if (mp2 == NULL) {
9783 		freeb(mp1);
9784 		inet_freemsg(pending_mp);
9785 		return (ENOMEM);
9786 	}
9787 	/* Put together the chain. */
9788 	mp1->b_cont = mp2;
9789 	mp1->b_datap->db_type = M_IOCTL;
9790 	mp2->b_cont = mp;
9791 	mp2->b_datap->db_type = M_DATA;
9792 
9793 	iocp = (struct iocblk *)mp1->b_rptr;
9794 
9795 	/*
9796 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9797 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9798 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9799 	 * ioc_count field; set ioc_count to be correct.
9800 	 */
9801 	iocp->ioc_count = MBLKL(mp1->b_cont);
9802 
9803 	/*
9804 	 * Set the proper command in the ARP message.
9805 	 * Convert the SIOC{G|S|D}ARP calls into our
9806 	 * AR_ENTRY_xxx calls.
9807 	 */
9808 	area = (area_t *)mp2->b_rptr;
9809 	switch (iocp->ioc_cmd) {
9810 	case SIOCDARP:
9811 	case SIOCDXARP:
9812 		/*
9813 		 * We defer deleting the corresponding IRE until
9814 		 * we return from arp.
9815 		 */
9816 		area->area_cmd = AR_ENTRY_DELETE;
9817 		area->area_proto_mask_offset = 0;
9818 		break;
9819 	case SIOCGARP:
9820 	case SIOCGXARP:
9821 		area->area_cmd = AR_ENTRY_SQUERY;
9822 		area->area_proto_mask_offset = 0;
9823 		break;
9824 	case SIOCSARP:
9825 	case SIOCSXARP: {
9826 		/*
9827 		 * Delete the corresponding ire to make sure IP will
9828 		 * pick up any change from arp.
9829 		 */
9830 		if (!if_arp_ioctl) {
9831 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9832 			break;
9833 		} else {
9834 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9835 			if (ipif != NULL) {
9836 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9837 				    ipst);
9838 				ipif_refrele(ipif);
9839 			}
9840 			break;
9841 		}
9842 	}
9843 	}
9844 	iocp->ioc_cmd = area->area_cmd;
9845 
9846 	/*
9847 	 * Before sending 'mp' to ARP, we have to clear the b_next
9848 	 * and b_prev. Otherwise if STREAMS encounters such a message
9849 	 * in freemsg(), (because ARP can close any time) it can cause
9850 	 * a panic. But mi code needs the b_next and b_prev values of
9851 	 * mp->b_cont, to complete the ioctl. So we store it here
9852 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9853 	 * when the response comes down from ARP.
9854 	 */
9855 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9856 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9857 	mp->b_cont->b_next = NULL;
9858 	mp->b_cont->b_prev = NULL;
9859 
9860 	mutex_enter(&connp->conn_lock);
9861 	mutex_enter(&ill->ill_lock);
9862 	/* conn has not yet started closing, hence this can't fail */
9863 	success = ill_pending_mp_add(ill, connp, pending_mp);
9864 	ASSERT(success);
9865 	mutex_exit(&ill->ill_lock);
9866 	mutex_exit(&connp->conn_lock);
9867 
9868 	/*
9869 	 * Fill in the rest of the ARP operation fields.
9870 	 */
9871 	area->area_hw_addr_length = alength;
9872 	bcopy(lladdr,
9873 	    (char *)area + area->area_hw_addr_offset,
9874 	    area->area_hw_addr_length);
9875 	/* Translate the flags. */
9876 	if (flags & ATF_PERM)
9877 		area->area_flags |= ACE_F_PERMANENT;
9878 	if (flags & ATF_PUBL)
9879 		area->area_flags |= ACE_F_PUBLISH;
9880 	if (flags & ATF_AUTHORITY)
9881 		area->area_flags |= ACE_F_AUTHORITY;
9882 
9883 	/*
9884 	 * Up to ARP it goes.  The response will come
9885 	 * back in ip_wput as an M_IOCACK message, and
9886 	 * will be handed to ip_sioctl_iocack for
9887 	 * completion.
9888 	 */
9889 	putnext(ill->ill_rq, mp1);
9890 	return (EINPROGRESS);
9891 }
9892 
9893 /* ARGSUSED */
9894 int
9895 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9896     ip_ioctl_cmd_t *ipip, void *ifreq)
9897 {
9898 	struct xarpreq *xar;
9899 	boolean_t isv6;
9900 	mblk_t	*mp1;
9901 	int	err;
9902 	conn_t	*connp;
9903 	int ifnamelen;
9904 	ire_t	*ire = NULL;
9905 	ill_t	*ill = NULL;
9906 	struct sockaddr_in *sin;
9907 	boolean_t if_arp_ioctl = B_FALSE;
9908 	ip_stack_t	*ipst;
9909 
9910 	/* ioctl comes down on an conn */
9911 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9912 	connp = Q_TO_CONN(q);
9913 	isv6 = connp->conn_af_isv6;
9914 	ipst = connp->conn_netstack->netstack_ip;
9915 
9916 	/* Existance verified in ip_wput_nondata */
9917 	mp1 = mp->b_cont->b_cont;
9918 
9919 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
9920 	xar = (struct xarpreq *)mp1->b_rptr;
9921 	sin = (sin_t *)&xar->xarp_pa;
9922 
9923 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
9924 	    (xar->xarp_pa.ss_family != AF_INET))
9925 		return (ENXIO);
9926 
9927 	ifnamelen = xar->xarp_ha.sdl_nlen;
9928 	if (ifnamelen != 0) {
9929 		char	*cptr, cval;
9930 
9931 		if (ifnamelen >= LIFNAMSIZ)
9932 			return (EINVAL);
9933 
9934 		/*
9935 		 * Instead of bcopying a bunch of bytes,
9936 		 * null-terminate the string in-situ.
9937 		 */
9938 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9939 		cval = *cptr;
9940 		*cptr = '\0';
9941 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9942 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9943 		    &err, NULL, ipst);
9944 		*cptr = cval;
9945 		if (ill == NULL)
9946 			return (err);
9947 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9948 			ill_refrele(ill);
9949 			return (ENXIO);
9950 		}
9951 
9952 		if_arp_ioctl = B_TRUE;
9953 	} else {
9954 		/*
9955 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9956 		 * as an extended BSD ioctl. The kernel uses the IP address
9957 		 * to figure out the network interface.
9958 		 */
9959 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9960 		    ipst);
9961 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9962 		    ((ill = ire_to_ill(ire)) == NULL) ||
9963 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9964 			if (ire != NULL)
9965 				ire_refrele(ire);
9966 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9967 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9968 			    NULL, MATCH_IRE_TYPE, ipst);
9969 			if ((ire == NULL) ||
9970 			    ((ill = ire_to_ill(ire)) == NULL)) {
9971 				if (ire != NULL)
9972 					ire_refrele(ire);
9973 				return (ENXIO);
9974 			}
9975 		}
9976 		ASSERT(ire != NULL && ill != NULL);
9977 	}
9978 
9979 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9980 	if (if_arp_ioctl)
9981 		ill_refrele(ill);
9982 	if (ire != NULL)
9983 		ire_refrele(ire);
9984 
9985 	return (err);
9986 }
9987 
9988 /*
9989  * ARP IOCTLs.
9990  * How does IP get in the business of fronting ARP configuration/queries?
9991  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9992  * are by tradition passed in through a datagram socket.  That lands in IP.
9993  * As it happens, this is just as well since the interface is quite crude in
9994  * that it passes in no information about protocol or hardware types, or
9995  * interface association.  After making the protocol assumption, IP is in
9996  * the position to look up the name of the ILL, which ARP will need, and
9997  * format a request that can be handled by ARP.	 The request is passed up
9998  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9999  * back a response.  ARP supports its own set of more general IOCTLs, in
10000  * case anyone is interested.
10001  */
10002 /* ARGSUSED */
10003 int
10004 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10005     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
10006 {
10007 	struct arpreq *ar;
10008 	struct sockaddr_in *sin;
10009 	ire_t	*ire;
10010 	boolean_t isv6;
10011 	mblk_t	*mp1;
10012 	int	err;
10013 	conn_t	*connp;
10014 	ill_t	*ill;
10015 	ip_stack_t	*ipst;
10016 
10017 	/* ioctl comes down on an conn */
10018 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
10019 	connp = Q_TO_CONN(q);
10020 	ipst = CONNQ_TO_IPST(q);
10021 	isv6 = connp->conn_af_isv6;
10022 	if (isv6)
10023 		return (ENXIO);
10024 
10025 	/* Existance verified in ip_wput_nondata */
10026 	mp1 = mp->b_cont->b_cont;
10027 
10028 	ar = (struct arpreq *)mp1->b_rptr;
10029 	sin = (sin_t *)&ar->arp_pa;
10030 
10031 	/*
10032 	 * We need to let ARP know on which interface the IP
10033 	 * address has an ARP mapping. In the IPMP case, a
10034 	 * simple forwarding table lookup will return the
10035 	 * IRE_IF_RESOLVER for the first interface in the group,
10036 	 * which might not be the interface on which the
10037 	 * requested IP address was resolved due to the ill
10038 	 * selection algorithm (see ip_newroute_get_dst_ill()).
10039 	 * So we do a cache table lookup first: if the IRE cache
10040 	 * entry for the IP address is still there, it will
10041 	 * contain the ill pointer for the right interface, so
10042 	 * we use that. If the cache entry has been flushed, we
10043 	 * fall back to the forwarding table lookup. This should
10044 	 * be rare enough since IRE cache entries have a longer
10045 	 * life expectancy than ARP cache entries.
10046 	 */
10047 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL, ipst);
10048 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
10049 	    ((ill = ire_to_ill(ire)) == NULL)) {
10050 		if (ire != NULL)
10051 			ire_refrele(ire);
10052 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
10053 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
10054 		    NULL, MATCH_IRE_TYPE, ipst);
10055 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
10056 			if (ire != NULL)
10057 				ire_refrele(ire);
10058 			return (ENXIO);
10059 		}
10060 	}
10061 	ASSERT(ire != NULL && ill != NULL);
10062 
10063 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
10064 	ire_refrele(ire);
10065 	return (err);
10066 }
10067 
10068 /*
10069  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
10070  * atomically set/clear the muxids. Also complete the ioctl by acking or
10071  * naking it.  Note that the code is structured such that the link type,
10072  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
10073  * its clones use the persistent link, while pppd(1M) and perhaps many
10074  * other daemons may use non-persistent link.  When combined with some
10075  * ill_t states, linking and unlinking lower streams may be used as
10076  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
10077  */
10078 /* ARGSUSED */
10079 void
10080 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
10081 {
10082 	mblk_t		*mp1, *mp2;
10083 	struct linkblk	*li;
10084 	struct ipmx_s	*ipmxp;
10085 	ill_t		*ill;
10086 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
10087 	int		err = 0;
10088 	boolean_t	entered_ipsq = B_FALSE;
10089 	boolean_t	islink;
10090 	ip_stack_t	*ipst;
10091 
10092 	if (CONN_Q(q))
10093 		ipst = CONNQ_TO_IPST(q);
10094 	else
10095 		ipst = ILLQ_TO_IPST(q);
10096 
10097 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
10098 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
10099 
10100 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
10101 
10102 	mp1 = mp->b_cont;	/* This is the linkblk info */
10103 	li = (struct linkblk *)mp1->b_rptr;
10104 
10105 	/*
10106 	 * ARP has added this special mblk, and the utility is asking us
10107 	 * to perform consistency checks, and also atomically set the
10108 	 * muxid. Ifconfig is an example.  It achieves this by using
10109 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
10110 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
10111 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
10112 	 * and other comments in this routine for more details.
10113 	 */
10114 	mp2 = mp1->b_cont;	/* This is added by ARP */
10115 
10116 	/*
10117 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
10118 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
10119 	 * get the special mblk above.  For backward compatibility, we
10120 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
10121 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
10122 	 * not atomic, and can leave the streams unplumbable if the utility
10123 	 * is interrupted before it does the SIOCSLIFMUXID.
10124 	 */
10125 	if (mp2 == NULL) {
10126 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
10127 		if (err == EINPROGRESS)
10128 			return;
10129 		goto done;
10130 	}
10131 
10132 	/*
10133 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
10134 	 * ARP has appended this last mblk to tell us whether the lower stream
10135 	 * is an arp-dev stream or an IP module stream.
10136 	 */
10137 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
10138 	if (ipmxp->ipmx_arpdev_stream) {
10139 		/*
10140 		 * The lower stream is the arp-dev stream.
10141 		 */
10142 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
10143 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
10144 		if (ill == NULL) {
10145 			if (err == EINPROGRESS)
10146 				return;
10147 			err = EINVAL;
10148 			goto done;
10149 		}
10150 
10151 		if (ipsq == NULL) {
10152 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10153 			    NEW_OP, B_TRUE);
10154 			if (ipsq == NULL) {
10155 				ill_refrele(ill);
10156 				return;
10157 			}
10158 			entered_ipsq = B_TRUE;
10159 		}
10160 		ASSERT(IAM_WRITER_ILL(ill));
10161 		ill_refrele(ill);
10162 
10163 		/*
10164 		 * To ensure consistency between IP and ARP, the following
10165 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
10166 		 * This is because the muxid's are stored in the IP stream on
10167 		 * the ill.
10168 		 *
10169 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
10170 		 * the ARP stream. On an arp-dev stream, IP checks that it is
10171 		 * not yet plinked, and it also checks that the corresponding
10172 		 * IP stream is already plinked.
10173 		 *
10174 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
10175 		 * punlinking the IP stream. IP does not allow punlink of the
10176 		 * IP stream unless the arp stream has been punlinked.
10177 		 */
10178 		if ((islink &&
10179 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
10180 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
10181 			err = EINVAL;
10182 			goto done;
10183 		}
10184 		ill->ill_arp_muxid = islink ? li->l_index : 0;
10185 	} else {
10186 		/*
10187 		 * The lower stream is probably an IP module stream.  Do
10188 		 * consistency checking.
10189 		 */
10190 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
10191 		if (err == EINPROGRESS)
10192 			return;
10193 	}
10194 done:
10195 	if (err == 0)
10196 		miocack(q, mp, 0, 0);
10197 	else
10198 		miocnak(q, mp, 0, err);
10199 
10200 	/* Conn was refheld in ip_sioctl_copyin_setup */
10201 	if (CONN_Q(q))
10202 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
10203 	if (entered_ipsq)
10204 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10205 }
10206 
10207 /*
10208  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
10209  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
10210  * module stream).  If `doconsist' is set, then do the extended consistency
10211  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
10212  * Returns zero on success, EINPROGRESS if the operation is still pending, or
10213  * an error code on failure.
10214  */
10215 static int
10216 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
10217     struct linkblk *li, boolean_t doconsist)
10218 {
10219 	ill_t  		*ill;
10220 	queue_t		*ipwq, *dwq;
10221 	const char	*name;
10222 	struct qinit	*qinfo;
10223 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
10224 
10225 	/*
10226 	 * Walk the lower stream to verify it's the IP module stream.
10227 	 * The IP module is identified by its name, wput function,
10228 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
10229 	 * (li->l_qbot) will not vanish until this ioctl completes.
10230 	 */
10231 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
10232 		qinfo = ipwq->q_qinfo;
10233 		name = qinfo->qi_minfo->mi_idname;
10234 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
10235 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
10236 			break;
10237 		}
10238 	}
10239 
10240 	/*
10241 	 * If this isn't an IP module stream, bail.
10242 	 */
10243 	if (ipwq == NULL)
10244 		return (0);
10245 
10246 	ill = ipwq->q_ptr;
10247 	ASSERT(ill != NULL);
10248 
10249 	if (ipsq == NULL) {
10250 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10251 		    NEW_OP, B_TRUE);
10252 		if (ipsq == NULL)
10253 			return (EINPROGRESS);
10254 	}
10255 	ASSERT(IAM_WRITER_ILL(ill));
10256 
10257 	if (doconsist) {
10258 		/*
10259 		 * Consistency checking requires that I_{P}LINK occurs
10260 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
10261 		 * occurs prior to clearing ill_arp_muxid.
10262 		 */
10263 		if ((islink && ill->ill_ip_muxid != 0) ||
10264 		    (!islink && ill->ill_arp_muxid != 0)) {
10265 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
10266 			return (EINVAL);
10267 		}
10268 	}
10269 
10270 	/*
10271 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10272 	 * the read queue of the module immediately below IP in the ill.
10273 	 * These are used during the capability negotiation below.
10274 	 */
10275 	ill->ill_lmod_rq = NULL;
10276 	ill->ill_lmod_cnt = 0;
10277 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10278 		ill->ill_lmod_rq = RD(dwq);
10279 		for (; dwq != NULL; dwq = dwq->q_next)
10280 			ill->ill_lmod_cnt++;
10281 	}
10282 
10283 	if (doconsist)
10284 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10285 
10286 	/*
10287 	 * If there's at least one up ipif on this ill, then we're bound to
10288 	 * the underlying driver via DLPI.  In that case, renegotiate
10289 	 * capabilities to account for any possible change in modules
10290 	 * interposed between IP and the driver.
10291 	 */
10292 	if (ill->ill_ipif_up_count > 0) {
10293 		if (islink)
10294 			ill_capability_probe(ill);
10295 		else
10296 			ill_capability_reset(ill);
10297 	}
10298 
10299 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10300 	return (0);
10301 }
10302 
10303 /*
10304  * Search the ioctl command in the ioctl tables and return a pointer
10305  * to the ioctl command information. The ioctl command tables are
10306  * static and fully populated at compile time.
10307  */
10308 ip_ioctl_cmd_t *
10309 ip_sioctl_lookup(int ioc_cmd)
10310 {
10311 	int index;
10312 	ip_ioctl_cmd_t *ipip;
10313 	ip_ioctl_cmd_t *ipip_end;
10314 
10315 	if (ioc_cmd == IPI_DONTCARE)
10316 		return (NULL);
10317 
10318 	/*
10319 	 * Do a 2 step search. First search the indexed table
10320 	 * based on the least significant byte of the ioctl cmd.
10321 	 * If we don't find a match, then search the misc table
10322 	 * serially.
10323 	 */
10324 	index = ioc_cmd & 0xFF;
10325 	if (index < ip_ndx_ioctl_count) {
10326 		ipip = &ip_ndx_ioctl_table[index];
10327 		if (ipip->ipi_cmd == ioc_cmd) {
10328 			/* Found a match in the ndx table */
10329 			return (ipip);
10330 		}
10331 	}
10332 
10333 	/* Search the misc table */
10334 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10335 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10336 		if (ipip->ipi_cmd == ioc_cmd)
10337 			/* Found a match in the misc table */
10338 			return (ipip);
10339 	}
10340 
10341 	return (NULL);
10342 }
10343 
10344 /*
10345  * Wrapper function for resuming deferred ioctl processing
10346  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10347  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10348  */
10349 /* ARGSUSED */
10350 void
10351 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10352     void *dummy_arg)
10353 {
10354 	ip_sioctl_copyin_setup(q, mp);
10355 }
10356 
10357 /*
10358  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10359  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10360  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10361  * We establish here the size of the block to be copied in.  mi_copyin
10362  * arranges for this to happen, an processing continues in ip_wput with
10363  * an M_IOCDATA message.
10364  */
10365 void
10366 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10367 {
10368 	int	copyin_size;
10369 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10370 	ip_ioctl_cmd_t *ipip;
10371 	cred_t *cr;
10372 	ip_stack_t	*ipst;
10373 
10374 	if (CONN_Q(q))
10375 		ipst = CONNQ_TO_IPST(q);
10376 	else
10377 		ipst = ILLQ_TO_IPST(q);
10378 
10379 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10380 	if (ipip == NULL) {
10381 		/*
10382 		 * The ioctl is not one we understand or own.
10383 		 * Pass it along to be processed down stream,
10384 		 * if this is a module instance of IP, else nak
10385 		 * the ioctl.
10386 		 */
10387 		if (q->q_next == NULL) {
10388 			goto nak;
10389 		} else {
10390 			putnext(q, mp);
10391 			return;
10392 		}
10393 	}
10394 
10395 	/*
10396 	 * If this is deferred, then we will do all the checks when we
10397 	 * come back.
10398 	 */
10399 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10400 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10401 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10402 		return;
10403 	}
10404 
10405 	/*
10406 	 * Only allow a very small subset of IP ioctls on this stream if
10407 	 * IP is a module and not a driver. Allowing ioctls to be processed
10408 	 * in this case may cause assert failures or data corruption.
10409 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10410 	 * ioctls allowed on an IP module stream, after which this stream
10411 	 * normally becomes a multiplexor (at which time the stream head
10412 	 * will fail all ioctls).
10413 	 */
10414 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10415 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10416 			/*
10417 			 * Pass common Streams ioctls which the IP
10418 			 * module does not own or consume along to
10419 			 * be processed down stream.
10420 			 */
10421 			putnext(q, mp);
10422 			return;
10423 		} else {
10424 			goto nak;
10425 		}
10426 	}
10427 
10428 	/* Make sure we have ioctl data to process. */
10429 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10430 		goto nak;
10431 
10432 	/*
10433 	 * Prefer dblk credential over ioctl credential; some synthesized
10434 	 * ioctls have kcred set because there's no way to crhold()
10435 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10436 	 * the framework; the caller of ioctl needs to hold the reference
10437 	 * for the duration of the call).
10438 	 */
10439 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10440 
10441 	/* Make sure normal users don't send down privileged ioctls */
10442 	if ((ipip->ipi_flags & IPI_PRIV) &&
10443 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10444 		/* We checked the privilege earlier but log it here */
10445 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10446 		return;
10447 	}
10448 
10449 	/*
10450 	 * The ioctl command tables can only encode fixed length
10451 	 * ioctl data. If the length is variable, the table will
10452 	 * encode the length as zero. Such special cases are handled
10453 	 * below in the switch.
10454 	 */
10455 	if (ipip->ipi_copyin_size != 0) {
10456 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10457 		return;
10458 	}
10459 
10460 	switch (iocp->ioc_cmd) {
10461 	case O_SIOCGIFCONF:
10462 	case SIOCGIFCONF:
10463 		/*
10464 		 * This IOCTL is hilarious.  See comments in
10465 		 * ip_sioctl_get_ifconf for the story.
10466 		 */
10467 		if (iocp->ioc_count == TRANSPARENT)
10468 			copyin_size = SIZEOF_STRUCT(ifconf,
10469 			    iocp->ioc_flag);
10470 		else
10471 			copyin_size = iocp->ioc_count;
10472 		mi_copyin(q, mp, NULL, copyin_size);
10473 		return;
10474 
10475 	case O_SIOCGLIFCONF:
10476 	case SIOCGLIFCONF:
10477 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10478 		mi_copyin(q, mp, NULL, copyin_size);
10479 		return;
10480 
10481 	case SIOCGLIFSRCOF:
10482 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10483 		mi_copyin(q, mp, NULL, copyin_size);
10484 		return;
10485 	case SIOCGIP6ADDRPOLICY:
10486 		ip_sioctl_ip6addrpolicy(q, mp);
10487 		ip6_asp_table_refrele(ipst);
10488 		return;
10489 
10490 	case SIOCSIP6ADDRPOLICY:
10491 		ip_sioctl_ip6addrpolicy(q, mp);
10492 		return;
10493 
10494 	case SIOCGDSTINFO:
10495 		ip_sioctl_dstinfo(q, mp);
10496 		ip6_asp_table_refrele(ipst);
10497 		return;
10498 
10499 	case I_PLINK:
10500 	case I_PUNLINK:
10501 	case I_LINK:
10502 	case I_UNLINK:
10503 		/*
10504 		 * We treat non-persistent link similarly as the persistent
10505 		 * link case, in terms of plumbing/unplumbing, as well as
10506 		 * dynamic re-plumbing events indicator.  See comments
10507 		 * in ip_sioctl_plink() for more.
10508 		 *
10509 		 * Request can be enqueued in the 'ipsq' while waiting
10510 		 * to become exclusive. So bump up the conn ref.
10511 		 */
10512 		if (CONN_Q(q))
10513 			CONN_INC_REF(Q_TO_CONN(q));
10514 		ip_sioctl_plink(NULL, q, mp, NULL);
10515 		return;
10516 
10517 	case ND_GET:
10518 	case ND_SET:
10519 		/*
10520 		 * Use of the nd table requires holding the reader lock.
10521 		 * Modifying the nd table thru nd_load/nd_unload requires
10522 		 * the writer lock.
10523 		 */
10524 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10525 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10526 			rw_exit(&ipst->ips_ip_g_nd_lock);
10527 
10528 			if (iocp->ioc_error)
10529 				iocp->ioc_count = 0;
10530 			mp->b_datap->db_type = M_IOCACK;
10531 			qreply(q, mp);
10532 			return;
10533 		}
10534 		rw_exit(&ipst->ips_ip_g_nd_lock);
10535 		/*
10536 		 * We don't understand this subioctl of ND_GET / ND_SET.
10537 		 * Maybe intended for some driver / module below us
10538 		 */
10539 		if (q->q_next) {
10540 			putnext(q, mp);
10541 		} else {
10542 			iocp->ioc_error = ENOENT;
10543 			mp->b_datap->db_type = M_IOCNAK;
10544 			iocp->ioc_count = 0;
10545 			qreply(q, mp);
10546 		}
10547 		return;
10548 
10549 	case IP_IOCTL:
10550 		ip_wput_ioctl(q, mp);
10551 		return;
10552 	default:
10553 		cmn_err(CE_PANIC, "should not happen ");
10554 	}
10555 nak:
10556 	if (mp->b_cont != NULL) {
10557 		freemsg(mp->b_cont);
10558 		mp->b_cont = NULL;
10559 	}
10560 	iocp->ioc_error = EINVAL;
10561 	mp->b_datap->db_type = M_IOCNAK;
10562 	iocp->ioc_count = 0;
10563 	qreply(q, mp);
10564 }
10565 
10566 /* ip_wput hands off ARP IOCTL responses to us */
10567 void
10568 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10569 {
10570 	struct arpreq *ar;
10571 	struct xarpreq *xar;
10572 	area_t	*area;
10573 	mblk_t	*area_mp;
10574 	struct iocblk *iocp;
10575 	mblk_t	*orig_ioc_mp, *tmp;
10576 	struct iocblk	*orig_iocp;
10577 	ill_t *ill;
10578 	conn_t *connp = NULL;
10579 	uint_t ioc_id;
10580 	mblk_t *pending_mp;
10581 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10582 	int *flagsp;
10583 	char *storage = NULL;
10584 	sin_t *sin;
10585 	ipaddr_t addr;
10586 	int err;
10587 	ip_stack_t *ipst;
10588 
10589 	ill = q->q_ptr;
10590 	ASSERT(ill != NULL);
10591 	ipst = ill->ill_ipst;
10592 
10593 	/*
10594 	 * We should get back from ARP a packet chain that looks like:
10595 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10596 	 */
10597 	if (!(area_mp = mp->b_cont) ||
10598 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10599 	    !(orig_ioc_mp = area_mp->b_cont) ||
10600 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10601 		freemsg(mp);
10602 		return;
10603 	}
10604 
10605 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10606 
10607 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10608 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10609 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10610 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10611 		x_arp_ioctl = B_TRUE;
10612 		xar = (struct xarpreq *)tmp->b_rptr;
10613 		sin = (sin_t *)&xar->xarp_pa;
10614 		flagsp = &xar->xarp_flags;
10615 		storage = xar->xarp_ha.sdl_data;
10616 		if (xar->xarp_ha.sdl_nlen != 0)
10617 			ifx_arp_ioctl = B_TRUE;
10618 	} else {
10619 		ar = (struct arpreq *)tmp->b_rptr;
10620 		sin = (sin_t *)&ar->arp_pa;
10621 		flagsp = &ar->arp_flags;
10622 		storage = ar->arp_ha.sa_data;
10623 	}
10624 
10625 	iocp = (struct iocblk *)mp->b_rptr;
10626 
10627 	/*
10628 	 * Pick out the originating queue based on the ioc_id.
10629 	 */
10630 	ioc_id = iocp->ioc_id;
10631 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10632 	if (pending_mp == NULL) {
10633 		ASSERT(connp == NULL);
10634 		inet_freemsg(mp);
10635 		return;
10636 	}
10637 	ASSERT(connp != NULL);
10638 	q = CONNP_TO_WQ(connp);
10639 
10640 	/* Uncouple the internally generated IOCTL from the original one */
10641 	area = (area_t *)area_mp->b_rptr;
10642 	area_mp->b_cont = NULL;
10643 
10644 	/*
10645 	 * Restore the b_next and b_prev used by mi code. This is needed
10646 	 * to complete the ioctl using mi* functions. We stored them in
10647 	 * the pending mp prior to sending the request to ARP.
10648 	 */
10649 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10650 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10651 	inet_freemsg(pending_mp);
10652 
10653 	/*
10654 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10655 	 * Catch the case where there is an IRE_CACHE by no entry in the
10656 	 * arp table.
10657 	 */
10658 	addr = sin->sin_addr.s_addr;
10659 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10660 		ire_t			*ire;
10661 		dl_unitdata_req_t	*dlup;
10662 		mblk_t			*llmp;
10663 		int			addr_len;
10664 		ill_t			*ipsqill = NULL;
10665 
10666 		if (ifx_arp_ioctl) {
10667 			/*
10668 			 * There's no need to lookup the ill, since
10669 			 * we've already done that when we started
10670 			 * processing the ioctl and sent the message
10671 			 * to ARP on that ill.  So use the ill that
10672 			 * is stored in q->q_ptr.
10673 			 */
10674 			ipsqill = ill;
10675 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10676 			    ipsqill->ill_ipif, ALL_ZONES,
10677 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10678 		} else {
10679 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10680 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10681 			if (ire != NULL)
10682 				ipsqill = ire_to_ill(ire);
10683 		}
10684 
10685 		if ((x_arp_ioctl) && (ipsqill != NULL))
10686 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10687 
10688 		if (ire != NULL) {
10689 			/*
10690 			 * Since the ire obtained from cachetable is used for
10691 			 * mac addr copying below, treat an incomplete ire as if
10692 			 * as if we never found it.
10693 			 */
10694 			if (ire->ire_nce != NULL &&
10695 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10696 				ire_refrele(ire);
10697 				ire = NULL;
10698 				ipsqill = NULL;
10699 				goto errack;
10700 			}
10701 			*flagsp = ATF_INUSE;
10702 			llmp = (ire->ire_nce != NULL ?
10703 			    ire->ire_nce->nce_res_mp : NULL);
10704 			if (llmp != NULL && ipsqill != NULL) {
10705 				uchar_t *macaddr;
10706 
10707 				addr_len = ipsqill->ill_phys_addr_length;
10708 				if (x_arp_ioctl && ((addr_len +
10709 				    ipsqill->ill_name_length) >
10710 				    sizeof (xar->xarp_ha.sdl_data))) {
10711 					ire_refrele(ire);
10712 					freemsg(mp);
10713 					ip_ioctl_finish(q, orig_ioc_mp,
10714 					    EINVAL, NO_COPYOUT, NULL);
10715 					return;
10716 				}
10717 				*flagsp |= ATF_COM;
10718 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10719 				if (ipsqill->ill_sap_length < 0)
10720 					macaddr = llmp->b_rptr +
10721 					    dlup->dl_dest_addr_offset;
10722 				else
10723 					macaddr = llmp->b_rptr +
10724 					    dlup->dl_dest_addr_offset +
10725 					    ipsqill->ill_sap_length;
10726 				/*
10727 				 * For SIOCGARP, MAC address length
10728 				 * validation has already been done
10729 				 * before the ioctl was issued to ARP to
10730 				 * allow it to progress only on 6 byte
10731 				 * addressable (ethernet like) media. Thus
10732 				 * the mac address copying can not overwrite
10733 				 * the sa_data area below.
10734 				 */
10735 				bcopy(macaddr, storage, addr_len);
10736 			}
10737 			/* Ditch the internal IOCTL. */
10738 			freemsg(mp);
10739 			ire_refrele(ire);
10740 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10741 			return;
10742 		}
10743 	}
10744 
10745 	/*
10746 	 * Delete the coresponding IRE_CACHE if any.
10747 	 * Reset the error if there was one (in case there was no entry
10748 	 * in arp.)
10749 	 */
10750 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10751 		ipif_t *ipintf = NULL;
10752 
10753 		if (ifx_arp_ioctl) {
10754 			/*
10755 			 * There's no need to lookup the ill, since
10756 			 * we've already done that when we started
10757 			 * processing the ioctl and sent the message
10758 			 * to ARP on that ill.  So use the ill that
10759 			 * is stored in q->q_ptr.
10760 			 */
10761 			ipintf = ill->ill_ipif;
10762 		}
10763 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10764 			/*
10765 			 * The address in "addr" may be an entry for a
10766 			 * router. If that's true, then any off-net
10767 			 * IRE_CACHE entries that go through the router
10768 			 * with address "addr" must be clobbered. Use
10769 			 * ire_walk to achieve this goal.
10770 			 */
10771 			if (ifx_arp_ioctl)
10772 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10773 				    ire_delete_cache_gw, (char *)&addr, ill);
10774 			else
10775 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10776 				    ALL_ZONES, ipst);
10777 			iocp->ioc_error = 0;
10778 		}
10779 	}
10780 errack:
10781 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10782 		err = iocp->ioc_error;
10783 		freemsg(mp);
10784 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10785 		return;
10786 	}
10787 
10788 	/*
10789 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10790 	 * the area_t into the struct {x}arpreq.
10791 	 */
10792 	if (x_arp_ioctl) {
10793 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10794 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10795 		    sizeof (xar->xarp_ha.sdl_data)) {
10796 			freemsg(mp);
10797 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10798 			    NULL);
10799 			return;
10800 		}
10801 	}
10802 	*flagsp = ATF_INUSE;
10803 	if (area->area_flags & ACE_F_PERMANENT)
10804 		*flagsp |= ATF_PERM;
10805 	if (area->area_flags & ACE_F_PUBLISH)
10806 		*flagsp |= ATF_PUBL;
10807 	if (area->area_flags & ACE_F_AUTHORITY)
10808 		*flagsp |= ATF_AUTHORITY;
10809 	if (area->area_hw_addr_length != 0) {
10810 		*flagsp |= ATF_COM;
10811 		/*
10812 		 * For SIOCGARP, MAC address length validation has
10813 		 * already been done before the ioctl was issued to ARP
10814 		 * to allow it to progress only on 6 byte addressable
10815 		 * (ethernet like) media. Thus the mac address copying
10816 		 * can not overwrite the sa_data area below.
10817 		 */
10818 		bcopy((char *)area + area->area_hw_addr_offset,
10819 		    storage, area->area_hw_addr_length);
10820 	}
10821 
10822 	/* Ditch the internal IOCTL. */
10823 	freemsg(mp);
10824 	/* Complete the original. */
10825 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10826 }
10827 
10828 /*
10829  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10830  * interface) create the next available logical interface for this
10831  * physical interface.
10832  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10833  * ipif with the specified name.
10834  *
10835  * If the address family is not AF_UNSPEC then set the address as well.
10836  *
10837  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10838  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10839  *
10840  * Executed as a writer on the ill or ill group.
10841  * So no lock is needed to traverse the ipif chain, or examine the
10842  * phyint flags.
10843  */
10844 /* ARGSUSED */
10845 int
10846 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10847     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10848 {
10849 	mblk_t	*mp1;
10850 	struct lifreq *lifr;
10851 	boolean_t	isv6;
10852 	boolean_t	exists;
10853 	char 	*name;
10854 	char	*endp;
10855 	char	*cp;
10856 	int	namelen;
10857 	ipif_t	*ipif;
10858 	long	id;
10859 	ipsq_t	*ipsq;
10860 	ill_t	*ill;
10861 	sin_t	*sin;
10862 	int	err = 0;
10863 	boolean_t found_sep = B_FALSE;
10864 	conn_t	*connp;
10865 	zoneid_t zoneid;
10866 	int	orig_ifindex = 0;
10867 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10868 
10869 	ASSERT(q->q_next == NULL);
10870 	ip1dbg(("ip_sioctl_addif\n"));
10871 	/* Existence of mp1 has been checked in ip_wput_nondata */
10872 	mp1 = mp->b_cont->b_cont;
10873 	/*
10874 	 * Null terminate the string to protect against buffer
10875 	 * overrun. String was generated by user code and may not
10876 	 * be trusted.
10877 	 */
10878 	lifr = (struct lifreq *)mp1->b_rptr;
10879 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10880 	name = lifr->lifr_name;
10881 	ASSERT(CONN_Q(q));
10882 	connp = Q_TO_CONN(q);
10883 	isv6 = connp->conn_af_isv6;
10884 	zoneid = connp->conn_zoneid;
10885 	namelen = mi_strlen(name);
10886 	if (namelen == 0)
10887 		return (EINVAL);
10888 
10889 	exists = B_FALSE;
10890 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10891 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10892 		/*
10893 		 * Allow creating lo0 using SIOCLIFADDIF.
10894 		 * can't be any other writer thread. So can pass null below
10895 		 * for the last 4 args to ipif_lookup_name.
10896 		 */
10897 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10898 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10899 		/* Prevent any further action */
10900 		if (ipif == NULL) {
10901 			return (ENOBUFS);
10902 		} else if (!exists) {
10903 			/* We created the ipif now and as writer */
10904 			ipif_refrele(ipif);
10905 			return (0);
10906 		} else {
10907 			ill = ipif->ipif_ill;
10908 			ill_refhold(ill);
10909 			ipif_refrele(ipif);
10910 		}
10911 	} else {
10912 		/* Look for a colon in the name. */
10913 		endp = &name[namelen];
10914 		for (cp = endp; --cp > name; ) {
10915 			if (*cp == IPIF_SEPARATOR_CHAR) {
10916 				found_sep = B_TRUE;
10917 				/*
10918 				 * Reject any non-decimal aliases for plumbing
10919 				 * of logical interfaces. Aliases with leading
10920 				 * zeroes are also rejected as they introduce
10921 				 * ambiguity in the naming of the interfaces.
10922 				 * Comparing with "0" takes care of all such
10923 				 * cases.
10924 				 */
10925 				if ((strncmp("0", cp+1, 1)) == 0)
10926 					return (EINVAL);
10927 
10928 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10929 				    id <= 0 || *endp != '\0') {
10930 					return (EINVAL);
10931 				}
10932 				*cp = '\0';
10933 				break;
10934 			}
10935 		}
10936 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10937 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10938 		if (found_sep)
10939 			*cp = IPIF_SEPARATOR_CHAR;
10940 		if (ill == NULL)
10941 			return (err);
10942 	}
10943 
10944 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10945 	    B_TRUE);
10946 
10947 	/*
10948 	 * Release the refhold due to the lookup, now that we are excl
10949 	 * or we are just returning
10950 	 */
10951 	ill_refrele(ill);
10952 
10953 	if (ipsq == NULL)
10954 		return (EINPROGRESS);
10955 
10956 	/*
10957 	 * If the interface is failed, inactive or offlined, look for a working
10958 	 * interface in the ill group and create the ipif there. If we can't
10959 	 * find a good interface, create the ipif anyway so that in.mpathd can
10960 	 * move it to the first repaired interface.
10961 	 */
10962 	if ((ill->ill_phyint->phyint_flags &
10963 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10964 	    ill->ill_phyint->phyint_groupname_len != 0) {
10965 		phyint_t *phyi;
10966 		char *groupname = ill->ill_phyint->phyint_groupname;
10967 
10968 		/*
10969 		 * We're looking for a working interface, but it doesn't matter
10970 		 * if it's up or down; so instead of following the group lists,
10971 		 * we look at each physical interface and compare the groupname.
10972 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10973 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10974 		 * Otherwise we create the ipif on the failed interface.
10975 		 */
10976 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10977 		phyi = avl_first(&ipst->ips_phyint_g_list->
10978 		    phyint_list_avl_by_index);
10979 		for (; phyi != NULL;
10980 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10981 		    phyint_list_avl_by_index,
10982 		    phyi, AVL_AFTER)) {
10983 			if (phyi->phyint_groupname_len == 0)
10984 				continue;
10985 			ASSERT(phyi->phyint_groupname != NULL);
10986 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10987 			    !(phyi->phyint_flags &
10988 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10989 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10990 			    (phyi->phyint_illv4 != NULL))) {
10991 				break;
10992 			}
10993 		}
10994 		rw_exit(&ipst->ips_ill_g_lock);
10995 
10996 		if (phyi != NULL) {
10997 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10998 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10999 			    phyi->phyint_illv4);
11000 		}
11001 	}
11002 
11003 	/*
11004 	 * We are now exclusive on the ipsq, so an ill move will be serialized
11005 	 * before or after us.
11006 	 */
11007 	ASSERT(IAM_WRITER_ILL(ill));
11008 	ASSERT(ill->ill_move_in_progress == B_FALSE);
11009 
11010 	if (found_sep && orig_ifindex == 0) {
11011 		/* Now see if there is an IPIF with this unit number. */
11012 		for (ipif = ill->ill_ipif; ipif != NULL;
11013 		    ipif = ipif->ipif_next) {
11014 			if (ipif->ipif_id == id) {
11015 				err = EEXIST;
11016 				goto done;
11017 			}
11018 		}
11019 	}
11020 
11021 	/*
11022 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
11023 	 * of lo0. We never come here when we plumb lo0:0. It
11024 	 * happens in ipif_lookup_on_name.
11025 	 * The specified unit number is ignored when we create the ipif on a
11026 	 * different interface. However, we save it in ipif_orig_ipifid below so
11027 	 * that the ipif fails back to the right position.
11028 	 */
11029 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
11030 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
11031 		err = ENOBUFS;
11032 		goto done;
11033 	}
11034 
11035 	/* Return created name with ioctl */
11036 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
11037 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
11038 	ip1dbg(("created %s\n", lifr->lifr_name));
11039 
11040 	/* Set address */
11041 	sin = (sin_t *)&lifr->lifr_addr;
11042 	if (sin->sin_family != AF_UNSPEC) {
11043 		err = ip_sioctl_addr(ipif, sin, q, mp,
11044 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
11045 	}
11046 
11047 	/* Set ifindex and unit number for failback */
11048 	if (err == 0 && orig_ifindex != 0) {
11049 		ipif->ipif_orig_ifindex = orig_ifindex;
11050 		if (found_sep) {
11051 			ipif->ipif_orig_ipifid = id;
11052 		}
11053 	}
11054 
11055 done:
11056 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
11057 	return (err);
11058 }
11059 
11060 /*
11061  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
11062  * interface) delete it based on the IP address (on this physical interface).
11063  * Otherwise delete it based on the ipif_id.
11064  * Also, special handling to allow a removeif of lo0.
11065  */
11066 /* ARGSUSED */
11067 int
11068 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11069     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11070 {
11071 	conn_t		*connp;
11072 	ill_t		*ill = ipif->ipif_ill;
11073 	boolean_t	 success;
11074 	ip_stack_t	*ipst;
11075 
11076 	ipst = CONNQ_TO_IPST(q);
11077 
11078 	ASSERT(q->q_next == NULL);
11079 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
11080 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11081 	ASSERT(IAM_WRITER_IPIF(ipif));
11082 
11083 	connp = Q_TO_CONN(q);
11084 	/*
11085 	 * Special case for unplumbing lo0 (the loopback physical interface).
11086 	 * If unplumbing lo0, the incoming address structure has been
11087 	 * initialized to all zeros. When unplumbing lo0, all its logical
11088 	 * interfaces must be removed too.
11089 	 *
11090 	 * Note that this interface may be called to remove a specific
11091 	 * loopback logical interface (eg, lo0:1). But in that case
11092 	 * ipif->ipif_id != 0 so that the code path for that case is the
11093 	 * same as any other interface (meaning it skips the code directly
11094 	 * below).
11095 	 */
11096 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11097 		if (sin->sin_family == AF_UNSPEC &&
11098 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
11099 			/*
11100 			 * Mark it condemned. No new ref. will be made to ill.
11101 			 */
11102 			mutex_enter(&ill->ill_lock);
11103 			ill->ill_state_flags |= ILL_CONDEMNED;
11104 			for (ipif = ill->ill_ipif; ipif != NULL;
11105 			    ipif = ipif->ipif_next) {
11106 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
11107 			}
11108 			mutex_exit(&ill->ill_lock);
11109 
11110 			ipif = ill->ill_ipif;
11111 			/* unplumb the loopback interface */
11112 			ill_delete(ill);
11113 			mutex_enter(&connp->conn_lock);
11114 			mutex_enter(&ill->ill_lock);
11115 			ASSERT(ill->ill_group == NULL);
11116 
11117 			/* Are any references to this ill active */
11118 			if (ill_is_quiescent(ill)) {
11119 				mutex_exit(&ill->ill_lock);
11120 				mutex_exit(&connp->conn_lock);
11121 				ill_delete_tail(ill);
11122 				mi_free(ill);
11123 				return (0);
11124 			}
11125 			success = ipsq_pending_mp_add(connp, ipif,
11126 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
11127 			mutex_exit(&connp->conn_lock);
11128 			mutex_exit(&ill->ill_lock);
11129 			if (success)
11130 				return (EINPROGRESS);
11131 			else
11132 				return (EINTR);
11133 		}
11134 	}
11135 
11136 	/*
11137 	 * We are exclusive on the ipsq, so an ill move will be serialized
11138 	 * before or after us.
11139 	 */
11140 	ASSERT(ill->ill_move_in_progress == B_FALSE);
11141 
11142 	if (ipif->ipif_id == 0) {
11143 		/* Find based on address */
11144 		if (ipif->ipif_isv6) {
11145 			sin6_t *sin6;
11146 
11147 			if (sin->sin_family != AF_INET6)
11148 				return (EAFNOSUPPORT);
11149 
11150 			sin6 = (sin6_t *)sin;
11151 			/* We are a writer, so we should be able to lookup */
11152 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11153 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
11154 			if (ipif == NULL) {
11155 				/*
11156 				 * Maybe the address in on another interface in
11157 				 * the same IPMP group? We check this below.
11158 				 */
11159 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11160 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
11161 				    ipst);
11162 			}
11163 		} else {
11164 			ipaddr_t addr;
11165 
11166 			if (sin->sin_family != AF_INET)
11167 				return (EAFNOSUPPORT);
11168 
11169 			addr = sin->sin_addr.s_addr;
11170 			/* We are a writer, so we should be able to lookup */
11171 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
11172 			    NULL, NULL, NULL, ipst);
11173 			if (ipif == NULL) {
11174 				/*
11175 				 * Maybe the address in on another interface in
11176 				 * the same IPMP group? We check this below.
11177 				 */
11178 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
11179 				    NULL, NULL, NULL, NULL, ipst);
11180 			}
11181 		}
11182 		if (ipif == NULL) {
11183 			return (EADDRNOTAVAIL);
11184 		}
11185 		/*
11186 		 * When the address to be removed is hosted on a different
11187 		 * interface, we check if the interface is in the same IPMP
11188 		 * group as the specified one; if so we proceed with the
11189 		 * removal.
11190 		 * ill->ill_group is NULL when the ill is down, so we have to
11191 		 * compare the group names instead.
11192 		 */
11193 		if (ipif->ipif_ill != ill &&
11194 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
11195 		    ill->ill_phyint->phyint_groupname_len == 0 ||
11196 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
11197 		    ill->ill_phyint->phyint_groupname) != 0)) {
11198 			ipif_refrele(ipif);
11199 			return (EADDRNOTAVAIL);
11200 		}
11201 
11202 		/* This is a writer */
11203 		ipif_refrele(ipif);
11204 	}
11205 
11206 	/*
11207 	 * Can not delete instance zero since it is tied to the ill.
11208 	 */
11209 	if (ipif->ipif_id == 0)
11210 		return (EBUSY);
11211 
11212 	mutex_enter(&ill->ill_lock);
11213 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
11214 	mutex_exit(&ill->ill_lock);
11215 
11216 	ipif_free(ipif);
11217 
11218 	mutex_enter(&connp->conn_lock);
11219 	mutex_enter(&ill->ill_lock);
11220 
11221 	/* Are any references to this ipif active */
11222 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
11223 		mutex_exit(&ill->ill_lock);
11224 		mutex_exit(&connp->conn_lock);
11225 		ipif_non_duplicate(ipif);
11226 		ipif_down_tail(ipif);
11227 		ipif_free_tail(ipif);
11228 		return (0);
11229 	}
11230 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
11231 	    IPIF_FREE);
11232 	mutex_exit(&ill->ill_lock);
11233 	mutex_exit(&connp->conn_lock);
11234 	if (success)
11235 		return (EINPROGRESS);
11236 	else
11237 		return (EINTR);
11238 }
11239 
11240 /*
11241  * Restart the removeif ioctl. The refcnt has gone down to 0.
11242  * The ipif is already condemned. So can't find it thru lookups.
11243  */
11244 /* ARGSUSED */
11245 int
11246 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11247     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11248 {
11249 	ill_t *ill;
11250 
11251 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11252 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11253 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11254 		ill = ipif->ipif_ill;
11255 		ASSERT(IAM_WRITER_ILL(ill));
11256 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
11257 		    (ill->ill_state_flags & IPIF_CONDEMNED));
11258 		ill_delete_tail(ill);
11259 		mi_free(ill);
11260 		return (0);
11261 	}
11262 
11263 	ill = ipif->ipif_ill;
11264 	ASSERT(IAM_WRITER_IPIF(ipif));
11265 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11266 
11267 	ipif_non_duplicate(ipif);
11268 	ipif_down_tail(ipif);
11269 	ipif_free_tail(ipif);
11270 
11271 	ILL_UNMARK_CHANGING(ill);
11272 	return (0);
11273 }
11274 
11275 /*
11276  * Set the local interface address.
11277  * Allow an address of all zero when the interface is down.
11278  */
11279 /* ARGSUSED */
11280 int
11281 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11282     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11283 {
11284 	int err = 0;
11285 	in6_addr_t v6addr;
11286 	boolean_t need_up = B_FALSE;
11287 
11288 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11289 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11290 
11291 	ASSERT(IAM_WRITER_IPIF(ipif));
11292 
11293 	if (ipif->ipif_isv6) {
11294 		sin6_t *sin6;
11295 		ill_t *ill;
11296 		phyint_t *phyi;
11297 
11298 		if (sin->sin_family != AF_INET6)
11299 			return (EAFNOSUPPORT);
11300 
11301 		sin6 = (sin6_t *)sin;
11302 		v6addr = sin6->sin6_addr;
11303 		ill = ipif->ipif_ill;
11304 		phyi = ill->ill_phyint;
11305 
11306 		/*
11307 		 * Enforce that true multicast interfaces have a link-local
11308 		 * address for logical unit 0.
11309 		 */
11310 		if (ipif->ipif_id == 0 &&
11311 		    (ill->ill_flags & ILLF_MULTICAST) &&
11312 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11313 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11314 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11315 			return (EADDRNOTAVAIL);
11316 		}
11317 
11318 		/*
11319 		 * up interfaces shouldn't have the unspecified address
11320 		 * unless they also have the IPIF_NOLOCAL flags set and
11321 		 * have a subnet assigned.
11322 		 */
11323 		if ((ipif->ipif_flags & IPIF_UP) &&
11324 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11325 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11326 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11327 			return (EADDRNOTAVAIL);
11328 		}
11329 
11330 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11331 			return (EADDRNOTAVAIL);
11332 	} else {
11333 		ipaddr_t addr;
11334 
11335 		if (sin->sin_family != AF_INET)
11336 			return (EAFNOSUPPORT);
11337 
11338 		addr = sin->sin_addr.s_addr;
11339 
11340 		/* Allow 0 as the local address. */
11341 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11342 			return (EADDRNOTAVAIL);
11343 
11344 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11345 	}
11346 
11347 
11348 	/*
11349 	 * Even if there is no change we redo things just to rerun
11350 	 * ipif_set_default.
11351 	 */
11352 	if (ipif->ipif_flags & IPIF_UP) {
11353 		/*
11354 		 * Setting a new local address, make sure
11355 		 * we have net and subnet bcast ire's for
11356 		 * the old address if we need them.
11357 		 */
11358 		if (!ipif->ipif_isv6)
11359 			ipif_check_bcast_ires(ipif);
11360 		/*
11361 		 * If the interface is already marked up,
11362 		 * we call ipif_down which will take care
11363 		 * of ditching any IREs that have been set
11364 		 * up based on the old interface address.
11365 		 */
11366 		err = ipif_logical_down(ipif, q, mp);
11367 		if (err == EINPROGRESS)
11368 			return (err);
11369 		ipif_down_tail(ipif);
11370 		need_up = 1;
11371 	}
11372 
11373 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11374 	return (err);
11375 }
11376 
11377 int
11378 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11379     boolean_t need_up)
11380 {
11381 	in6_addr_t v6addr;
11382 	in6_addr_t ov6addr;
11383 	ipaddr_t addr;
11384 	sin6_t	*sin6;
11385 	int	sinlen;
11386 	int	err = 0;
11387 	ill_t	*ill = ipif->ipif_ill;
11388 	boolean_t need_dl_down;
11389 	boolean_t need_arp_down;
11390 	struct iocblk *iocp;
11391 
11392 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11393 
11394 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11395 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11396 	ASSERT(IAM_WRITER_IPIF(ipif));
11397 
11398 	/* Must cancel any pending timer before taking the ill_lock */
11399 	if (ipif->ipif_recovery_id != 0)
11400 		(void) untimeout(ipif->ipif_recovery_id);
11401 	ipif->ipif_recovery_id = 0;
11402 
11403 	if (ipif->ipif_isv6) {
11404 		sin6 = (sin6_t *)sin;
11405 		v6addr = sin6->sin6_addr;
11406 		sinlen = sizeof (struct sockaddr_in6);
11407 	} else {
11408 		addr = sin->sin_addr.s_addr;
11409 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11410 		sinlen = sizeof (struct sockaddr_in);
11411 	}
11412 	mutex_enter(&ill->ill_lock);
11413 	ov6addr = ipif->ipif_v6lcl_addr;
11414 	ipif->ipif_v6lcl_addr = v6addr;
11415 	sctp_update_ipif_addr(ipif, ov6addr);
11416 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11417 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11418 	} else {
11419 		ipif->ipif_v6src_addr = v6addr;
11420 	}
11421 	ipif->ipif_addr_ready = 0;
11422 
11423 	/*
11424 	 * If the interface was previously marked as a duplicate, then since
11425 	 * we've now got a "new" address, it should no longer be considered a
11426 	 * duplicate -- even if the "new" address is the same as the old one.
11427 	 * Note that if all ipifs are down, we may have a pending ARP down
11428 	 * event to handle.  This is because we want to recover from duplicates
11429 	 * and thus delay tearing down ARP until the duplicates have been
11430 	 * removed or disabled.
11431 	 */
11432 	need_dl_down = need_arp_down = B_FALSE;
11433 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11434 		need_arp_down = !need_up;
11435 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11436 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11437 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11438 			need_dl_down = B_TRUE;
11439 		}
11440 	}
11441 
11442 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11443 	    !ill->ill_is_6to4tun) {
11444 		queue_t *wqp = ill->ill_wq;
11445 
11446 		/*
11447 		 * The local address of this interface is a 6to4 address,
11448 		 * check if this interface is in fact a 6to4 tunnel or just
11449 		 * an interface configured with a 6to4 address.  We are only
11450 		 * interested in the former.
11451 		 */
11452 		if (wqp != NULL) {
11453 			while ((wqp->q_next != NULL) &&
11454 			    (wqp->q_next->q_qinfo != NULL) &&
11455 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11456 
11457 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11458 				    == TUN6TO4_MODID) {
11459 					/* set for use in IP */
11460 					ill->ill_is_6to4tun = 1;
11461 					break;
11462 				}
11463 				wqp = wqp->q_next;
11464 			}
11465 		}
11466 	}
11467 
11468 	ipif_set_default(ipif);
11469 
11470 	/*
11471 	 * When publishing an interface address change event, we only notify
11472 	 * the event listeners of the new address.  It is assumed that if they
11473 	 * actively care about the addresses assigned that they will have
11474 	 * already discovered the previous address assigned (if there was one.)
11475 	 *
11476 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11477 	 */
11478 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11479 		hook_nic_event_t *info;
11480 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
11481 			ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d "
11482 			    "attached for %s\n", info->hne_event,
11483 			    ill->ill_name));
11484 			if (info->hne_data != NULL)
11485 				kmem_free(info->hne_data, info->hne_datalen);
11486 			kmem_free(info, sizeof (hook_nic_event_t));
11487 		}
11488 
11489 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
11490 		if (info != NULL) {
11491 			ip_stack_t	*ipst = ill->ill_ipst;
11492 
11493 			info->hne_nic =
11494 			    ipif->ipif_ill->ill_phyint->phyint_hook_ifindex;
11495 			info->hne_lif = MAP_IPIF_ID(ipif->ipif_id);
11496 			info->hne_event = NE_ADDRESS_CHANGE;
11497 			info->hne_family = ipif->ipif_isv6 ?
11498 			    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
11499 			info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP);
11500 			if (info->hne_data != NULL) {
11501 				info->hne_datalen = sinlen;
11502 				bcopy(sin, info->hne_data, sinlen);
11503 			} else {
11504 				ip2dbg(("ip_sioctl_addr_tail: could not attach "
11505 				    "address information for ADDRESS_CHANGE nic"
11506 				    " event of %s (ENOMEM)\n",
11507 				    ipif->ipif_ill->ill_name));
11508 				kmem_free(info, sizeof (hook_nic_event_t));
11509 			}
11510 		} else
11511 			ip2dbg(("ip_sioctl_addr_tail: could not attach "
11512 			    "ADDRESS_CHANGE nic event information for %s "
11513 			    "(ENOMEM)\n", ipif->ipif_ill->ill_name));
11514 
11515 		ipif->ipif_ill->ill_nic_event_info = info;
11516 	}
11517 
11518 	mutex_exit(&ill->ill_lock);
11519 
11520 	if (need_up) {
11521 		/*
11522 		 * Now bring the interface back up.  If this
11523 		 * is the only IPIF for the ILL, ipif_up
11524 		 * will have to re-bind to the device, so
11525 		 * we may get back EINPROGRESS, in which
11526 		 * case, this IOCTL will get completed in
11527 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11528 		 */
11529 		err = ipif_up(ipif, q, mp);
11530 	}
11531 
11532 	if (need_dl_down)
11533 		ill_dl_down(ill);
11534 	if (need_arp_down)
11535 		ipif_arp_down(ipif);
11536 
11537 	return (err);
11538 }
11539 
11540 
11541 /*
11542  * Restart entry point to restart the address set operation after the
11543  * refcounts have dropped to zero.
11544  */
11545 /* ARGSUSED */
11546 int
11547 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11548     ip_ioctl_cmd_t *ipip, void *ifreq)
11549 {
11550 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11551 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11552 	ASSERT(IAM_WRITER_IPIF(ipif));
11553 	ipif_down_tail(ipif);
11554 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11555 }
11556 
11557 /* ARGSUSED */
11558 int
11559 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11560     ip_ioctl_cmd_t *ipip, void *if_req)
11561 {
11562 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11563 	struct lifreq *lifr = (struct lifreq *)if_req;
11564 
11565 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11566 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11567 	/*
11568 	 * The net mask and address can't change since we have a
11569 	 * reference to the ipif. So no lock is necessary.
11570 	 */
11571 	if (ipif->ipif_isv6) {
11572 		*sin6 = sin6_null;
11573 		sin6->sin6_family = AF_INET6;
11574 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11575 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11576 		lifr->lifr_addrlen =
11577 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11578 	} else {
11579 		*sin = sin_null;
11580 		sin->sin_family = AF_INET;
11581 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11582 		if (ipip->ipi_cmd_type == LIF_CMD) {
11583 			lifr->lifr_addrlen =
11584 			    ip_mask_to_plen(ipif->ipif_net_mask);
11585 		}
11586 	}
11587 	return (0);
11588 }
11589 
11590 /*
11591  * Set the destination address for a pt-pt interface.
11592  */
11593 /* ARGSUSED */
11594 int
11595 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11596     ip_ioctl_cmd_t *ipip, void *if_req)
11597 {
11598 	int err = 0;
11599 	in6_addr_t v6addr;
11600 	boolean_t need_up = B_FALSE;
11601 
11602 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11603 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11604 	ASSERT(IAM_WRITER_IPIF(ipif));
11605 
11606 	if (ipif->ipif_isv6) {
11607 		sin6_t *sin6;
11608 
11609 		if (sin->sin_family != AF_INET6)
11610 			return (EAFNOSUPPORT);
11611 
11612 		sin6 = (sin6_t *)sin;
11613 		v6addr = sin6->sin6_addr;
11614 
11615 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11616 			return (EADDRNOTAVAIL);
11617 	} else {
11618 		ipaddr_t addr;
11619 
11620 		if (sin->sin_family != AF_INET)
11621 			return (EAFNOSUPPORT);
11622 
11623 		addr = sin->sin_addr.s_addr;
11624 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11625 			return (EADDRNOTAVAIL);
11626 
11627 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11628 	}
11629 
11630 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11631 		return (0);	/* No change */
11632 
11633 	if (ipif->ipif_flags & IPIF_UP) {
11634 		/*
11635 		 * If the interface is already marked up,
11636 		 * we call ipif_down which will take care
11637 		 * of ditching any IREs that have been set
11638 		 * up based on the old pp dst address.
11639 		 */
11640 		err = ipif_logical_down(ipif, q, mp);
11641 		if (err == EINPROGRESS)
11642 			return (err);
11643 		ipif_down_tail(ipif);
11644 		need_up = B_TRUE;
11645 	}
11646 	/*
11647 	 * could return EINPROGRESS. If so ioctl will complete in
11648 	 * ip_rput_dlpi_writer
11649 	 */
11650 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11651 	return (err);
11652 }
11653 
11654 static int
11655 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11656     boolean_t need_up)
11657 {
11658 	in6_addr_t v6addr;
11659 	ill_t	*ill = ipif->ipif_ill;
11660 	int	err = 0;
11661 	boolean_t need_dl_down;
11662 	boolean_t need_arp_down;
11663 
11664 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11665 	    ipif->ipif_id, (void *)ipif));
11666 
11667 	/* Must cancel any pending timer before taking the ill_lock */
11668 	if (ipif->ipif_recovery_id != 0)
11669 		(void) untimeout(ipif->ipif_recovery_id);
11670 	ipif->ipif_recovery_id = 0;
11671 
11672 	if (ipif->ipif_isv6) {
11673 		sin6_t *sin6;
11674 
11675 		sin6 = (sin6_t *)sin;
11676 		v6addr = sin6->sin6_addr;
11677 	} else {
11678 		ipaddr_t addr;
11679 
11680 		addr = sin->sin_addr.s_addr;
11681 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11682 	}
11683 	mutex_enter(&ill->ill_lock);
11684 	/* Set point to point destination address. */
11685 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11686 		/*
11687 		 * Allow this as a means of creating logical
11688 		 * pt-pt interfaces on top of e.g. an Ethernet.
11689 		 * XXX Undocumented HACK for testing.
11690 		 * pt-pt interfaces are created with NUD disabled.
11691 		 */
11692 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11693 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11694 		if (ipif->ipif_isv6)
11695 			ill->ill_flags |= ILLF_NONUD;
11696 	}
11697 
11698 	/*
11699 	 * If the interface was previously marked as a duplicate, then since
11700 	 * we've now got a "new" address, it should no longer be considered a
11701 	 * duplicate -- even if the "new" address is the same as the old one.
11702 	 * Note that if all ipifs are down, we may have a pending ARP down
11703 	 * event to handle.
11704 	 */
11705 	need_dl_down = need_arp_down = B_FALSE;
11706 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11707 		need_arp_down = !need_up;
11708 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11709 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11710 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11711 			need_dl_down = B_TRUE;
11712 		}
11713 	}
11714 
11715 	/* Set the new address. */
11716 	ipif->ipif_v6pp_dst_addr = v6addr;
11717 	/* Make sure subnet tracks pp_dst */
11718 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11719 	mutex_exit(&ill->ill_lock);
11720 
11721 	if (need_up) {
11722 		/*
11723 		 * Now bring the interface back up.  If this
11724 		 * is the only IPIF for the ILL, ipif_up
11725 		 * will have to re-bind to the device, so
11726 		 * we may get back EINPROGRESS, in which
11727 		 * case, this IOCTL will get completed in
11728 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11729 		 */
11730 		err = ipif_up(ipif, q, mp);
11731 	}
11732 
11733 	if (need_dl_down)
11734 		ill_dl_down(ill);
11735 
11736 	if (need_arp_down)
11737 		ipif_arp_down(ipif);
11738 	return (err);
11739 }
11740 
11741 /*
11742  * Restart entry point to restart the dstaddress set operation after the
11743  * refcounts have dropped to zero.
11744  */
11745 /* ARGSUSED */
11746 int
11747 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11748     ip_ioctl_cmd_t *ipip, void *ifreq)
11749 {
11750 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11751 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11752 	ipif_down_tail(ipif);
11753 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11754 }
11755 
11756 /* ARGSUSED */
11757 int
11758 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11759     ip_ioctl_cmd_t *ipip, void *if_req)
11760 {
11761 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11762 
11763 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11764 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11765 	/*
11766 	 * Get point to point destination address. The addresses can't
11767 	 * change since we hold a reference to the ipif.
11768 	 */
11769 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11770 		return (EADDRNOTAVAIL);
11771 
11772 	if (ipif->ipif_isv6) {
11773 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11774 		*sin6 = sin6_null;
11775 		sin6->sin6_family = AF_INET6;
11776 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11777 	} else {
11778 		*sin = sin_null;
11779 		sin->sin_family = AF_INET;
11780 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11781 	}
11782 	return (0);
11783 }
11784 
11785 /*
11786  * part of ipmp, make this func return the active/inactive state and
11787  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11788  */
11789 /*
11790  * This function either sets or clears the IFF_INACTIVE flag.
11791  *
11792  * As long as there are some addresses or multicast memberships on the
11793  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11794  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11795  * will be used for outbound packets.
11796  *
11797  * Caller needs to verify the validity of setting IFF_INACTIVE.
11798  */
11799 static void
11800 phyint_inactive(phyint_t *phyi)
11801 {
11802 	ill_t *ill_v4;
11803 	ill_t *ill_v6;
11804 	ipif_t *ipif;
11805 	ilm_t *ilm;
11806 
11807 	ill_v4 = phyi->phyint_illv4;
11808 	ill_v6 = phyi->phyint_illv6;
11809 
11810 	/*
11811 	 * No need for a lock while traversing the list since iam
11812 	 * a writer
11813 	 */
11814 	if (ill_v4 != NULL) {
11815 		ASSERT(IAM_WRITER_ILL(ill_v4));
11816 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11817 		    ipif = ipif->ipif_next) {
11818 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11819 				mutex_enter(&phyi->phyint_lock);
11820 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11821 				mutex_exit(&phyi->phyint_lock);
11822 				return;
11823 			}
11824 		}
11825 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11826 		    ilm = ilm->ilm_next) {
11827 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11828 				mutex_enter(&phyi->phyint_lock);
11829 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11830 				mutex_exit(&phyi->phyint_lock);
11831 				return;
11832 			}
11833 		}
11834 	}
11835 	if (ill_v6 != NULL) {
11836 		ill_v6 = phyi->phyint_illv6;
11837 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11838 		    ipif = ipif->ipif_next) {
11839 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11840 				mutex_enter(&phyi->phyint_lock);
11841 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11842 				mutex_exit(&phyi->phyint_lock);
11843 				return;
11844 			}
11845 		}
11846 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11847 		    ilm = ilm->ilm_next) {
11848 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11849 				mutex_enter(&phyi->phyint_lock);
11850 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11851 				mutex_exit(&phyi->phyint_lock);
11852 				return;
11853 			}
11854 		}
11855 	}
11856 	mutex_enter(&phyi->phyint_lock);
11857 	phyi->phyint_flags |= PHYI_INACTIVE;
11858 	mutex_exit(&phyi->phyint_lock);
11859 }
11860 
11861 /*
11862  * This function is called only when the phyint flags change. Currently
11863  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11864  * that we can select a good ill.
11865  */
11866 static void
11867 ip_redo_nomination(phyint_t *phyi)
11868 {
11869 	ill_t *ill_v4;
11870 
11871 	ill_v4 = phyi->phyint_illv4;
11872 
11873 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11874 		ASSERT(IAM_WRITER_ILL(ill_v4));
11875 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11876 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11877 	}
11878 }
11879 
11880 /*
11881  * Heuristic to check if ill is INACTIVE.
11882  * Checks if ill has an ipif with an usable ip address.
11883  *
11884  * Return values:
11885  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11886  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11887  */
11888 static boolean_t
11889 ill_is_inactive(ill_t *ill)
11890 {
11891 	ipif_t *ipif;
11892 
11893 	/* Check whether it is in an IPMP group */
11894 	if (ill->ill_phyint->phyint_groupname == NULL)
11895 		return (B_FALSE);
11896 
11897 	if (ill->ill_ipif_up_count == 0)
11898 		return (B_TRUE);
11899 
11900 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11901 		uint64_t flags = ipif->ipif_flags;
11902 
11903 		/*
11904 		 * This ipif is usable if it is IPIF_UP and not a
11905 		 * dedicated test address.  A dedicated test address
11906 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11907 		 * (note in particular that V6 test addresses are
11908 		 * link-local data addresses and thus are marked
11909 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11910 		 */
11911 		if ((flags & IPIF_UP) &&
11912 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11913 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11914 			return (B_FALSE);
11915 	}
11916 	return (B_TRUE);
11917 }
11918 
11919 /*
11920  * Set interface flags.
11921  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11922  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11923  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11924  *
11925  * NOTE : We really don't enforce that ipif_id zero should be used
11926  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11927  *	  is because applications generally does SICGLIFFLAGS and
11928  *	  ORs in the new flags (that affects the logical) and does a
11929  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11930  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11931  *	  flags that will be turned on is correct with respect to
11932  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11933  */
11934 /* ARGSUSED */
11935 int
11936 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11937     ip_ioctl_cmd_t *ipip, void *if_req)
11938 {
11939 	uint64_t turn_on;
11940 	uint64_t turn_off;
11941 	int	err;
11942 	boolean_t need_up = B_FALSE;
11943 	phyint_t *phyi;
11944 	ill_t *ill;
11945 	uint64_t intf_flags;
11946 	boolean_t phyint_flags_modified = B_FALSE;
11947 	uint64_t flags;
11948 	struct ifreq *ifr;
11949 	struct lifreq *lifr;
11950 	boolean_t set_linklocal = B_FALSE;
11951 	boolean_t zero_source = B_FALSE;
11952 	ip_stack_t *ipst;
11953 
11954 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11955 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11956 
11957 	ASSERT(IAM_WRITER_IPIF(ipif));
11958 
11959 	ill = ipif->ipif_ill;
11960 	phyi = ill->ill_phyint;
11961 	ipst = ill->ill_ipst;
11962 
11963 	if (ipip->ipi_cmd_type == IF_CMD) {
11964 		ifr = (struct ifreq *)if_req;
11965 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11966 	} else {
11967 		lifr = (struct lifreq *)if_req;
11968 		flags = lifr->lifr_flags;
11969 	}
11970 
11971 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11972 
11973 	/*
11974 	 * Has the flags been set correctly till now ?
11975 	 */
11976 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11977 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11978 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11979 	/*
11980 	 * Compare the new flags to the old, and partition
11981 	 * into those coming on and those going off.
11982 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11983 	 */
11984 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11985 		flags |= intf_flags & ~0xFFFF;
11986 
11987 	/*
11988 	 * First check which bits will change and then which will
11989 	 * go on and off
11990 	 */
11991 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11992 	if (!turn_on)
11993 		return (0);	/* No change */
11994 
11995 	turn_off = intf_flags & turn_on;
11996 	turn_on ^= turn_off;
11997 	err = 0;
11998 
11999 	/*
12000 	 * Don't allow any bits belonging to the logical interface
12001 	 * to be set or cleared on the replacement ipif that was
12002 	 * created temporarily during a MOVE.
12003 	 */
12004 	if (ipif->ipif_replace_zero &&
12005 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
12006 		return (EINVAL);
12007 	}
12008 
12009 	/*
12010 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
12011 	 * IPv6 interfaces.
12012 	 */
12013 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
12014 		return (EINVAL);
12015 
12016 	/*
12017 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
12018 	 */
12019 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
12020 		return (EINVAL);
12021 
12022 	/*
12023 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
12024 	 * interfaces.  It makes no sense in that context.
12025 	 */
12026 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
12027 		return (EINVAL);
12028 
12029 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
12030 		zero_source = B_TRUE;
12031 
12032 	/*
12033 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
12034 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
12035 	 * If the link local address isn't set, and can be set, it will get
12036 	 * set later on in this function.
12037 	 */
12038 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
12039 	    (flags & IFF_UP) && !zero_source &&
12040 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
12041 		if (ipif_cant_setlinklocal(ipif))
12042 			return (EINVAL);
12043 		set_linklocal = B_TRUE;
12044 	}
12045 
12046 	/*
12047 	 * ILL cannot be part of a usesrc group and and IPMP group at the
12048 	 * same time. No need to grab ill_g_usesrc_lock here, see
12049 	 * synchronization notes in ip.c
12050 	 */
12051 	if (turn_on & PHYI_STANDBY &&
12052 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
12053 		return (EINVAL);
12054 	}
12055 
12056 	/*
12057 	 * If we modify physical interface flags, we'll potentially need to
12058 	 * send up two routing socket messages for the changes (one for the
12059 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
12060 	 */
12061 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
12062 		phyint_flags_modified = B_TRUE;
12063 
12064 	/*
12065 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
12066 	 * we need to flush the IRE_CACHES belonging to this ill.
12067 	 * We handle this case here without doing the DOWN/UP dance
12068 	 * like it is done for other flags. If some other flags are
12069 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
12070 	 * below will handle it by bringing it down and then
12071 	 * bringing it UP.
12072 	 */
12073 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
12074 		ill_t *ill_v4, *ill_v6;
12075 
12076 		ill_v4 = phyi->phyint_illv4;
12077 		ill_v6 = phyi->phyint_illv6;
12078 
12079 		/*
12080 		 * First set the INACTIVE flag if needed. Then delete the ires.
12081 		 * ire_add will atomically prevent creating new IRE_CACHEs
12082 		 * unless hidden flag is set.
12083 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
12084 		 */
12085 		if ((turn_on & PHYI_FAILED) &&
12086 		    ((intf_flags & PHYI_STANDBY) ||
12087 		    !ipst->ips_ipmp_enable_failback)) {
12088 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
12089 			phyi->phyint_flags &= ~PHYI_INACTIVE;
12090 		}
12091 		if ((turn_off & PHYI_FAILED) &&
12092 		    ((intf_flags & PHYI_STANDBY) ||
12093 		    (!ipst->ips_ipmp_enable_failback &&
12094 		    ill_is_inactive(ill)))) {
12095 			phyint_inactive(phyi);
12096 		}
12097 
12098 		if (turn_on & PHYI_STANDBY) {
12099 			/*
12100 			 * We implicitly set INACTIVE only when STANDBY is set.
12101 			 * INACTIVE is also set on non-STANDBY phyint when user
12102 			 * disables FAILBACK using configuration file.
12103 			 * Do not allow STANDBY to be set on such INACTIVE
12104 			 * phyint
12105 			 */
12106 			if (phyi->phyint_flags & PHYI_INACTIVE)
12107 				return (EINVAL);
12108 			if (!(phyi->phyint_flags & PHYI_FAILED))
12109 				phyint_inactive(phyi);
12110 		}
12111 		if (turn_off & PHYI_STANDBY) {
12112 			if (ipst->ips_ipmp_enable_failback) {
12113 				/*
12114 				 * Reset PHYI_INACTIVE.
12115 				 */
12116 				phyi->phyint_flags &= ~PHYI_INACTIVE;
12117 			} else if (ill_is_inactive(ill) &&
12118 			    !(phyi->phyint_flags & PHYI_FAILED)) {
12119 				/*
12120 				 * Need to set INACTIVE, when user sets
12121 				 * STANDBY on a non-STANDBY phyint and
12122 				 * later resets STANDBY
12123 				 */
12124 				phyint_inactive(phyi);
12125 			}
12126 		}
12127 		/*
12128 		 * We should always send up a message so that the
12129 		 * daemons come to know of it. Note that the zeroth
12130 		 * interface can be down and the check below for IPIF_UP
12131 		 * will not make sense as we are actually setting
12132 		 * a phyint flag here. We assume that the ipif used
12133 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
12134 		 * send up any message for non-zero ipifs).
12135 		 */
12136 		phyint_flags_modified = B_TRUE;
12137 
12138 		if (ill_v4 != NULL) {
12139 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
12140 			    IRE_CACHE, ill_stq_cache_delete,
12141 			    (char *)ill_v4, ill_v4);
12142 			illgrp_reset_schednext(ill_v4);
12143 		}
12144 		if (ill_v6 != NULL) {
12145 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
12146 			    IRE_CACHE, ill_stq_cache_delete,
12147 			    (char *)ill_v6, ill_v6);
12148 			illgrp_reset_schednext(ill_v6);
12149 		}
12150 	}
12151 
12152 	/*
12153 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
12154 	 * status of the interface and, if the interface is part of an IPMP
12155 	 * group, all other interfaces that are part of the same IPMP
12156 	 * group.
12157 	 */
12158 	if ((turn_on | turn_off) & ILLF_ROUTER)
12159 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
12160 
12161 	/*
12162 	 * If the interface is not UP and we are not going to
12163 	 * bring it UP, record the flags and return. When the
12164 	 * interface comes UP later, the right actions will be
12165 	 * taken.
12166 	 */
12167 	if (!(ipif->ipif_flags & IPIF_UP) &&
12168 	    !(turn_on & IPIF_UP)) {
12169 		/* Record new flags in their respective places. */
12170 		mutex_enter(&ill->ill_lock);
12171 		mutex_enter(&ill->ill_phyint->phyint_lock);
12172 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12173 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12174 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12175 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12176 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12177 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12178 		mutex_exit(&ill->ill_lock);
12179 		mutex_exit(&ill->ill_phyint->phyint_lock);
12180 
12181 		/*
12182 		 * We do the broadcast and nomination here rather
12183 		 * than waiting for a FAILOVER/FAILBACK to happen. In
12184 		 * the case of FAILBACK from INACTIVE standby to the
12185 		 * interface that has been repaired, PHYI_FAILED has not
12186 		 * been cleared yet. If there are only two interfaces in
12187 		 * that group, all we have is a FAILED and INACTIVE
12188 		 * interface. If we do the nomination soon after a failback,
12189 		 * the broadcast nomination code would select the
12190 		 * INACTIVE interface for receiving broadcasts as FAILED is
12191 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
12192 		 * receive broadcast packets, we need to redo nomination
12193 		 * when the FAILED is cleared here. Thus, in general we
12194 		 * always do the nomination here for FAILED, STANDBY
12195 		 * and OFFLINE.
12196 		 */
12197 		if (((turn_on | turn_off) &
12198 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
12199 			ip_redo_nomination(phyi);
12200 		}
12201 		if (phyint_flags_modified) {
12202 			if (phyi->phyint_illv4 != NULL) {
12203 				ip_rts_ifmsg(phyi->phyint_illv4->
12204 				    ill_ipif);
12205 			}
12206 			if (phyi->phyint_illv6 != NULL) {
12207 				ip_rts_ifmsg(phyi->phyint_illv6->
12208 				    ill_ipif);
12209 			}
12210 		}
12211 		return (0);
12212 	} else if (set_linklocal || zero_source) {
12213 		mutex_enter(&ill->ill_lock);
12214 		if (set_linklocal)
12215 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
12216 		if (zero_source)
12217 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
12218 		mutex_exit(&ill->ill_lock);
12219 	}
12220 
12221 	/*
12222 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
12223 	 * or point-to-point interfaces with an unspecified destination. We do
12224 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
12225 	 * have a subnet assigned, which is how in.ndpd currently manages its
12226 	 * onlink prefix list when no addresses are configured with those
12227 	 * prefixes.
12228 	 */
12229 	if (ipif->ipif_isv6 &&
12230 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
12231 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
12232 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
12233 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12234 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
12235 		return (EINVAL);
12236 	}
12237 
12238 	/*
12239 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
12240 	 * from being brought up.
12241 	 */
12242 	if (!ipif->ipif_isv6 &&
12243 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12244 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
12245 		return (EINVAL);
12246 	}
12247 
12248 	/*
12249 	 * The only flag changes that we currently take specific action on
12250 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
12251 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
12252 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
12253 	 * the flags and bringing it back up again.
12254 	 */
12255 	if ((turn_on|turn_off) &
12256 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
12257 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
12258 		/*
12259 		 * Taking this ipif down, make sure we have
12260 		 * valid net and subnet bcast ire's for other
12261 		 * logical interfaces, if we need them.
12262 		 */
12263 		if (!ipif->ipif_isv6)
12264 			ipif_check_bcast_ires(ipif);
12265 
12266 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
12267 		    !(turn_off & IPIF_UP)) {
12268 			need_up = B_TRUE;
12269 			if (ipif->ipif_flags & IPIF_UP)
12270 				ill->ill_logical_down = 1;
12271 			turn_on &= ~IPIF_UP;
12272 		}
12273 		err = ipif_down(ipif, q, mp);
12274 		ip1dbg(("ipif_down returns %d err ", err));
12275 		if (err == EINPROGRESS)
12276 			return (err);
12277 		ipif_down_tail(ipif);
12278 	}
12279 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12280 }
12281 
12282 static int
12283 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12284     boolean_t need_up)
12285 {
12286 	ill_t	*ill;
12287 	phyint_t *phyi;
12288 	uint64_t turn_on;
12289 	uint64_t turn_off;
12290 	uint64_t intf_flags;
12291 	boolean_t phyint_flags_modified = B_FALSE;
12292 	int	err = 0;
12293 	boolean_t set_linklocal = B_FALSE;
12294 	boolean_t zero_source = B_FALSE;
12295 
12296 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12297 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12298 
12299 	ASSERT(IAM_WRITER_IPIF(ipif));
12300 
12301 	ill = ipif->ipif_ill;
12302 	phyi = ill->ill_phyint;
12303 
12304 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12305 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12306 
12307 	turn_off = intf_flags & turn_on;
12308 	turn_on ^= turn_off;
12309 
12310 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12311 		phyint_flags_modified = B_TRUE;
12312 
12313 	/*
12314 	 * Now we change the flags. Track current value of
12315 	 * other flags in their respective places.
12316 	 */
12317 	mutex_enter(&ill->ill_lock);
12318 	mutex_enter(&phyi->phyint_lock);
12319 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12320 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12321 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12322 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12323 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12324 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12325 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12326 		set_linklocal = B_TRUE;
12327 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12328 	}
12329 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12330 		zero_source = B_TRUE;
12331 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12332 	}
12333 	mutex_exit(&ill->ill_lock);
12334 	mutex_exit(&phyi->phyint_lock);
12335 
12336 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12337 		ip_redo_nomination(phyi);
12338 
12339 	if (set_linklocal)
12340 		(void) ipif_setlinklocal(ipif);
12341 
12342 	if (zero_source)
12343 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12344 	else
12345 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12346 
12347 	if (need_up) {
12348 		/*
12349 		 * XXX ipif_up really does not know whether a phyint flags
12350 		 * was modified or not. So, it sends up information on
12351 		 * only one routing sockets message. As we don't bring up
12352 		 * the interface and also set STANDBY/FAILED simultaneously
12353 		 * it should be okay.
12354 		 */
12355 		err = ipif_up(ipif, q, mp);
12356 	} else {
12357 		/*
12358 		 * Make sure routing socket sees all changes to the flags.
12359 		 * ipif_up_done* handles this when we use ipif_up.
12360 		 */
12361 		if (phyint_flags_modified) {
12362 			if (phyi->phyint_illv4 != NULL) {
12363 				ip_rts_ifmsg(phyi->phyint_illv4->
12364 				    ill_ipif);
12365 			}
12366 			if (phyi->phyint_illv6 != NULL) {
12367 				ip_rts_ifmsg(phyi->phyint_illv6->
12368 				    ill_ipif);
12369 			}
12370 		} else {
12371 			ip_rts_ifmsg(ipif);
12372 		}
12373 		/*
12374 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12375 		 * this in need_up case.
12376 		 */
12377 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12378 	}
12379 	return (err);
12380 }
12381 
12382 /*
12383  * Restart entry point to restart the flags restart operation after the
12384  * refcounts have dropped to zero.
12385  */
12386 /* ARGSUSED */
12387 int
12388 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12389     ip_ioctl_cmd_t *ipip, void *if_req)
12390 {
12391 	int	err;
12392 	struct ifreq *ifr = (struct ifreq *)if_req;
12393 	struct lifreq *lifr = (struct lifreq *)if_req;
12394 
12395 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12396 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12397 
12398 	ipif_down_tail(ipif);
12399 	if (ipip->ipi_cmd_type == IF_CMD) {
12400 		/*
12401 		 * Since ip_sioctl_flags expects an int and ifr_flags
12402 		 * is a short we need to cast ifr_flags into an int
12403 		 * to avoid having sign extension cause bits to get
12404 		 * set that should not be.
12405 		 */
12406 		err = ip_sioctl_flags_tail(ipif,
12407 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12408 		    q, mp, B_TRUE);
12409 	} else {
12410 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12411 		    q, mp, B_TRUE);
12412 	}
12413 	return (err);
12414 }
12415 
12416 /*
12417  * Can operate on either a module or a driver queue.
12418  */
12419 /* ARGSUSED */
12420 int
12421 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12422     ip_ioctl_cmd_t *ipip, void *if_req)
12423 {
12424 	/*
12425 	 * Has the flags been set correctly till now ?
12426 	 */
12427 	ill_t *ill = ipif->ipif_ill;
12428 	phyint_t *phyi = ill->ill_phyint;
12429 
12430 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12431 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12432 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12433 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12434 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12435 
12436 	/*
12437 	 * Need a lock since some flags can be set even when there are
12438 	 * references to the ipif.
12439 	 */
12440 	mutex_enter(&ill->ill_lock);
12441 	if (ipip->ipi_cmd_type == IF_CMD) {
12442 		struct ifreq *ifr = (struct ifreq *)if_req;
12443 
12444 		/* Get interface flags (low 16 only). */
12445 		ifr->ifr_flags = ((ipif->ipif_flags |
12446 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12447 	} else {
12448 		struct lifreq *lifr = (struct lifreq *)if_req;
12449 
12450 		/* Get interface flags. */
12451 		lifr->lifr_flags = ipif->ipif_flags |
12452 		    ill->ill_flags | phyi->phyint_flags;
12453 	}
12454 	mutex_exit(&ill->ill_lock);
12455 	return (0);
12456 }
12457 
12458 /* ARGSUSED */
12459 int
12460 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12461     ip_ioctl_cmd_t *ipip, void *if_req)
12462 {
12463 	int mtu;
12464 	int ip_min_mtu;
12465 	struct ifreq	*ifr;
12466 	struct lifreq *lifr;
12467 	ire_t	*ire;
12468 	ip_stack_t *ipst;
12469 
12470 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12471 	    ipif->ipif_id, (void *)ipif));
12472 	if (ipip->ipi_cmd_type == IF_CMD) {
12473 		ifr = (struct ifreq *)if_req;
12474 		mtu = ifr->ifr_metric;
12475 	} else {
12476 		lifr = (struct lifreq *)if_req;
12477 		mtu = lifr->lifr_mtu;
12478 	}
12479 
12480 	if (ipif->ipif_isv6)
12481 		ip_min_mtu = IPV6_MIN_MTU;
12482 	else
12483 		ip_min_mtu = IP_MIN_MTU;
12484 
12485 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12486 		return (EINVAL);
12487 
12488 	/*
12489 	 * Change the MTU size in all relevant ire's.
12490 	 * Mtu change Vs. new ire creation - protocol below.
12491 	 * First change ipif_mtu and the ire_max_frag of the
12492 	 * interface ire. Then do an ire walk and change the
12493 	 * ire_max_frag of all affected ires. During ire_add
12494 	 * under the bucket lock, set the ire_max_frag of the
12495 	 * new ire being created from the ipif/ire from which
12496 	 * it is being derived. If an mtu change happens after
12497 	 * the ire is added, the new ire will be cleaned up.
12498 	 * Conversely if the mtu change happens before the ire
12499 	 * is added, ire_add will see the new value of the mtu.
12500 	 */
12501 	ipif->ipif_mtu = mtu;
12502 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12503 
12504 	if (ipif->ipif_isv6)
12505 		ire = ipif_to_ire_v6(ipif);
12506 	else
12507 		ire = ipif_to_ire(ipif);
12508 	if (ire != NULL) {
12509 		ire->ire_max_frag = ipif->ipif_mtu;
12510 		ire_refrele(ire);
12511 	}
12512 	ipst = ipif->ipif_ill->ill_ipst;
12513 	if (ipif->ipif_flags & IPIF_UP) {
12514 		if (ipif->ipif_isv6)
12515 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12516 			    ipst);
12517 		else
12518 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12519 			    ipst);
12520 	}
12521 	/* Update the MTU in SCTP's list */
12522 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12523 	return (0);
12524 }
12525 
12526 /* Get interface MTU. */
12527 /* ARGSUSED */
12528 int
12529 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12530 	ip_ioctl_cmd_t *ipip, void *if_req)
12531 {
12532 	struct ifreq	*ifr;
12533 	struct lifreq	*lifr;
12534 
12535 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12536 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12537 	if (ipip->ipi_cmd_type == IF_CMD) {
12538 		ifr = (struct ifreq *)if_req;
12539 		ifr->ifr_metric = ipif->ipif_mtu;
12540 	} else {
12541 		lifr = (struct lifreq *)if_req;
12542 		lifr->lifr_mtu = ipif->ipif_mtu;
12543 	}
12544 	return (0);
12545 }
12546 
12547 /* Set interface broadcast address. */
12548 /* ARGSUSED2 */
12549 int
12550 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12551 	ip_ioctl_cmd_t *ipip, void *if_req)
12552 {
12553 	ipaddr_t addr;
12554 	ire_t	*ire;
12555 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12556 
12557 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12558 	    ipif->ipif_id));
12559 
12560 	ASSERT(IAM_WRITER_IPIF(ipif));
12561 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12562 		return (EADDRNOTAVAIL);
12563 
12564 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12565 
12566 	if (sin->sin_family != AF_INET)
12567 		return (EAFNOSUPPORT);
12568 
12569 	addr = sin->sin_addr.s_addr;
12570 	if (ipif->ipif_flags & IPIF_UP) {
12571 		/*
12572 		 * If we are already up, make sure the new
12573 		 * broadcast address makes sense.  If it does,
12574 		 * there should be an IRE for it already.
12575 		 * Don't match on ipif, only on the ill
12576 		 * since we are sharing these now. Don't use
12577 		 * MATCH_IRE_ILL_GROUP as we are looking for
12578 		 * the broadcast ire on this ill and each ill
12579 		 * in the group has its own broadcast ire.
12580 		 */
12581 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12582 		    ipif, ALL_ZONES, NULL,
12583 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12584 		if (ire == NULL) {
12585 			return (EINVAL);
12586 		} else {
12587 			ire_refrele(ire);
12588 		}
12589 	}
12590 	/*
12591 	 * Changing the broadcast addr for this ipif.
12592 	 * Make sure we have valid net and subnet bcast
12593 	 * ire's for other logical interfaces, if needed.
12594 	 */
12595 	if (addr != ipif->ipif_brd_addr)
12596 		ipif_check_bcast_ires(ipif);
12597 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12598 	return (0);
12599 }
12600 
12601 /* Get interface broadcast address. */
12602 /* ARGSUSED */
12603 int
12604 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12605     ip_ioctl_cmd_t *ipip, void *if_req)
12606 {
12607 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12608 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12609 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12610 		return (EADDRNOTAVAIL);
12611 
12612 	/* IPIF_BROADCAST not possible with IPv6 */
12613 	ASSERT(!ipif->ipif_isv6);
12614 	*sin = sin_null;
12615 	sin->sin_family = AF_INET;
12616 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12617 	return (0);
12618 }
12619 
12620 /*
12621  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12622  */
12623 /* ARGSUSED */
12624 int
12625 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12626     ip_ioctl_cmd_t *ipip, void *if_req)
12627 {
12628 	int err = 0;
12629 	in6_addr_t v6mask;
12630 
12631 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12632 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12633 
12634 	ASSERT(IAM_WRITER_IPIF(ipif));
12635 
12636 	if (ipif->ipif_isv6) {
12637 		sin6_t *sin6;
12638 
12639 		if (sin->sin_family != AF_INET6)
12640 			return (EAFNOSUPPORT);
12641 
12642 		sin6 = (sin6_t *)sin;
12643 		v6mask = sin6->sin6_addr;
12644 	} else {
12645 		ipaddr_t mask;
12646 
12647 		if (sin->sin_family != AF_INET)
12648 			return (EAFNOSUPPORT);
12649 
12650 		mask = sin->sin_addr.s_addr;
12651 		V4MASK_TO_V6(mask, v6mask);
12652 	}
12653 
12654 	/*
12655 	 * No big deal if the interface isn't already up, or the mask
12656 	 * isn't really changing, or this is pt-pt.
12657 	 */
12658 	if (!(ipif->ipif_flags & IPIF_UP) ||
12659 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12660 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12661 		ipif->ipif_v6net_mask = v6mask;
12662 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12663 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12664 			    ipif->ipif_v6net_mask,
12665 			    ipif->ipif_v6subnet);
12666 		}
12667 		return (0);
12668 	}
12669 	/*
12670 	 * Make sure we have valid net and subnet broadcast ire's
12671 	 * for the old netmask, if needed by other logical interfaces.
12672 	 */
12673 	if (!ipif->ipif_isv6)
12674 		ipif_check_bcast_ires(ipif);
12675 
12676 	err = ipif_logical_down(ipif, q, mp);
12677 	if (err == EINPROGRESS)
12678 		return (err);
12679 	ipif_down_tail(ipif);
12680 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12681 	return (err);
12682 }
12683 
12684 static int
12685 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12686 {
12687 	in6_addr_t v6mask;
12688 	int err = 0;
12689 
12690 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12691 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12692 
12693 	if (ipif->ipif_isv6) {
12694 		sin6_t *sin6;
12695 
12696 		sin6 = (sin6_t *)sin;
12697 		v6mask = sin6->sin6_addr;
12698 	} else {
12699 		ipaddr_t mask;
12700 
12701 		mask = sin->sin_addr.s_addr;
12702 		V4MASK_TO_V6(mask, v6mask);
12703 	}
12704 
12705 	ipif->ipif_v6net_mask = v6mask;
12706 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12707 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12708 		    ipif->ipif_v6subnet);
12709 	}
12710 	err = ipif_up(ipif, q, mp);
12711 
12712 	if (err == 0 || err == EINPROGRESS) {
12713 		/*
12714 		 * The interface must be DL_BOUND if this packet has to
12715 		 * go out on the wire. Since we only go through a logical
12716 		 * down and are bound with the driver during an internal
12717 		 * down/up that is satisfied.
12718 		 */
12719 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12720 			/* Potentially broadcast an address mask reply. */
12721 			ipif_mask_reply(ipif);
12722 		}
12723 	}
12724 	return (err);
12725 }
12726 
12727 /* ARGSUSED */
12728 int
12729 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12730     ip_ioctl_cmd_t *ipip, void *if_req)
12731 {
12732 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12733 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12734 	ipif_down_tail(ipif);
12735 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12736 }
12737 
12738 /* Get interface net mask. */
12739 /* ARGSUSED */
12740 int
12741 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12742     ip_ioctl_cmd_t *ipip, void *if_req)
12743 {
12744 	struct lifreq *lifr = (struct lifreq *)if_req;
12745 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12746 
12747 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12748 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12749 
12750 	/*
12751 	 * net mask can't change since we have a reference to the ipif.
12752 	 */
12753 	if (ipif->ipif_isv6) {
12754 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12755 		*sin6 = sin6_null;
12756 		sin6->sin6_family = AF_INET6;
12757 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12758 		lifr->lifr_addrlen =
12759 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12760 	} else {
12761 		*sin = sin_null;
12762 		sin->sin_family = AF_INET;
12763 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12764 		if (ipip->ipi_cmd_type == LIF_CMD) {
12765 			lifr->lifr_addrlen =
12766 			    ip_mask_to_plen(ipif->ipif_net_mask);
12767 		}
12768 	}
12769 	return (0);
12770 }
12771 
12772 /* ARGSUSED */
12773 int
12774 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12775     ip_ioctl_cmd_t *ipip, void *if_req)
12776 {
12777 
12778 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12779 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12780 	/*
12781 	 * Set interface metric.  We don't use this for
12782 	 * anything but we keep track of it in case it is
12783 	 * important to routing applications or such.
12784 	 */
12785 	if (ipip->ipi_cmd_type == IF_CMD) {
12786 		struct ifreq    *ifr;
12787 
12788 		ifr = (struct ifreq *)if_req;
12789 		ipif->ipif_metric = ifr->ifr_metric;
12790 	} else {
12791 		struct lifreq   *lifr;
12792 
12793 		lifr = (struct lifreq *)if_req;
12794 		ipif->ipif_metric = lifr->lifr_metric;
12795 	}
12796 	return (0);
12797 }
12798 
12799 
12800 /* ARGSUSED */
12801 int
12802 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12803     ip_ioctl_cmd_t *ipip, void *if_req)
12804 {
12805 
12806 	/* Get interface metric. */
12807 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12808 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12809 	if (ipip->ipi_cmd_type == IF_CMD) {
12810 		struct ifreq    *ifr;
12811 
12812 		ifr = (struct ifreq *)if_req;
12813 		ifr->ifr_metric = ipif->ipif_metric;
12814 	} else {
12815 		struct lifreq   *lifr;
12816 
12817 		lifr = (struct lifreq *)if_req;
12818 		lifr->lifr_metric = ipif->ipif_metric;
12819 	}
12820 
12821 	return (0);
12822 }
12823 
12824 /* ARGSUSED */
12825 int
12826 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12827     ip_ioctl_cmd_t *ipip, void *if_req)
12828 {
12829 
12830 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12831 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12832 	/*
12833 	 * Set the muxid returned from I_PLINK.
12834 	 */
12835 	if (ipip->ipi_cmd_type == IF_CMD) {
12836 		struct ifreq *ifr = (struct ifreq *)if_req;
12837 
12838 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12839 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12840 	} else {
12841 		struct lifreq *lifr = (struct lifreq *)if_req;
12842 
12843 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12844 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12845 	}
12846 	return (0);
12847 }
12848 
12849 /* ARGSUSED */
12850 int
12851 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12852     ip_ioctl_cmd_t *ipip, void *if_req)
12853 {
12854 
12855 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12856 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12857 	/*
12858 	 * Get the muxid saved in ill for I_PUNLINK.
12859 	 */
12860 	if (ipip->ipi_cmd_type == IF_CMD) {
12861 		struct ifreq *ifr = (struct ifreq *)if_req;
12862 
12863 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12864 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12865 	} else {
12866 		struct lifreq *lifr = (struct lifreq *)if_req;
12867 
12868 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12869 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12870 	}
12871 	return (0);
12872 }
12873 
12874 /*
12875  * Set the subnet prefix. Does not modify the broadcast address.
12876  */
12877 /* ARGSUSED */
12878 int
12879 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12880     ip_ioctl_cmd_t *ipip, void *if_req)
12881 {
12882 	int err = 0;
12883 	in6_addr_t v6addr;
12884 	in6_addr_t v6mask;
12885 	boolean_t need_up = B_FALSE;
12886 	int addrlen;
12887 
12888 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12889 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12890 
12891 	ASSERT(IAM_WRITER_IPIF(ipif));
12892 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12893 
12894 	if (ipif->ipif_isv6) {
12895 		sin6_t *sin6;
12896 
12897 		if (sin->sin_family != AF_INET6)
12898 			return (EAFNOSUPPORT);
12899 
12900 		sin6 = (sin6_t *)sin;
12901 		v6addr = sin6->sin6_addr;
12902 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12903 			return (EADDRNOTAVAIL);
12904 	} else {
12905 		ipaddr_t addr;
12906 
12907 		if (sin->sin_family != AF_INET)
12908 			return (EAFNOSUPPORT);
12909 
12910 		addr = sin->sin_addr.s_addr;
12911 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12912 			return (EADDRNOTAVAIL);
12913 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12914 		/* Add 96 bits */
12915 		addrlen += IPV6_ABITS - IP_ABITS;
12916 	}
12917 
12918 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12919 		return (EINVAL);
12920 
12921 	/* Check if bits in the address is set past the mask */
12922 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12923 		return (EINVAL);
12924 
12925 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12926 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12927 		return (0);	/* No change */
12928 
12929 	if (ipif->ipif_flags & IPIF_UP) {
12930 		/*
12931 		 * If the interface is already marked up,
12932 		 * we call ipif_down which will take care
12933 		 * of ditching any IREs that have been set
12934 		 * up based on the old interface address.
12935 		 */
12936 		err = ipif_logical_down(ipif, q, mp);
12937 		if (err == EINPROGRESS)
12938 			return (err);
12939 		ipif_down_tail(ipif);
12940 		need_up = B_TRUE;
12941 	}
12942 
12943 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12944 	return (err);
12945 }
12946 
12947 static int
12948 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12949     queue_t *q, mblk_t *mp, boolean_t need_up)
12950 {
12951 	ill_t	*ill = ipif->ipif_ill;
12952 	int	err = 0;
12953 
12954 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12955 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12956 
12957 	/* Set the new address. */
12958 	mutex_enter(&ill->ill_lock);
12959 	ipif->ipif_v6net_mask = v6mask;
12960 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12961 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12962 		    ipif->ipif_v6subnet);
12963 	}
12964 	mutex_exit(&ill->ill_lock);
12965 
12966 	if (need_up) {
12967 		/*
12968 		 * Now bring the interface back up.  If this
12969 		 * is the only IPIF for the ILL, ipif_up
12970 		 * will have to re-bind to the device, so
12971 		 * we may get back EINPROGRESS, in which
12972 		 * case, this IOCTL will get completed in
12973 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12974 		 */
12975 		err = ipif_up(ipif, q, mp);
12976 		if (err == EINPROGRESS)
12977 			return (err);
12978 	}
12979 	return (err);
12980 }
12981 
12982 /* ARGSUSED */
12983 int
12984 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12985     ip_ioctl_cmd_t *ipip, void *if_req)
12986 {
12987 	int	addrlen;
12988 	in6_addr_t v6addr;
12989 	in6_addr_t v6mask;
12990 	struct lifreq *lifr = (struct lifreq *)if_req;
12991 
12992 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12993 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12994 	ipif_down_tail(ipif);
12995 
12996 	addrlen = lifr->lifr_addrlen;
12997 	if (ipif->ipif_isv6) {
12998 		sin6_t *sin6;
12999 
13000 		sin6 = (sin6_t *)sin;
13001 		v6addr = sin6->sin6_addr;
13002 	} else {
13003 		ipaddr_t addr;
13004 
13005 		addr = sin->sin_addr.s_addr;
13006 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
13007 		addrlen += IPV6_ABITS - IP_ABITS;
13008 	}
13009 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
13010 
13011 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
13012 }
13013 
13014 /* ARGSUSED */
13015 int
13016 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13017     ip_ioctl_cmd_t *ipip, void *if_req)
13018 {
13019 	struct lifreq *lifr = (struct lifreq *)if_req;
13020 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
13021 
13022 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
13023 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13024 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
13025 
13026 	if (ipif->ipif_isv6) {
13027 		*sin6 = sin6_null;
13028 		sin6->sin6_family = AF_INET6;
13029 		sin6->sin6_addr = ipif->ipif_v6subnet;
13030 		lifr->lifr_addrlen =
13031 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
13032 	} else {
13033 		*sin = sin_null;
13034 		sin->sin_family = AF_INET;
13035 		sin->sin_addr.s_addr = ipif->ipif_subnet;
13036 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
13037 	}
13038 	return (0);
13039 }
13040 
13041 /*
13042  * Set the IPv6 address token.
13043  */
13044 /* ARGSUSED */
13045 int
13046 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13047     ip_ioctl_cmd_t *ipi, void *if_req)
13048 {
13049 	ill_t *ill = ipif->ipif_ill;
13050 	int err;
13051 	in6_addr_t v6addr;
13052 	in6_addr_t v6mask;
13053 	boolean_t need_up = B_FALSE;
13054 	int i;
13055 	sin6_t *sin6 = (sin6_t *)sin;
13056 	struct lifreq *lifr = (struct lifreq *)if_req;
13057 	int addrlen;
13058 
13059 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
13060 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13061 	ASSERT(IAM_WRITER_IPIF(ipif));
13062 
13063 	addrlen = lifr->lifr_addrlen;
13064 	/* Only allow for logical unit zero i.e. not on "le0:17" */
13065 	if (ipif->ipif_id != 0)
13066 		return (EINVAL);
13067 
13068 	if (!ipif->ipif_isv6)
13069 		return (EINVAL);
13070 
13071 	if (addrlen > IPV6_ABITS)
13072 		return (EINVAL);
13073 
13074 	v6addr = sin6->sin6_addr;
13075 
13076 	/*
13077 	 * The length of the token is the length from the end.  To get
13078 	 * the proper mask for this, compute the mask of the bits not
13079 	 * in the token; ie. the prefix, and then xor to get the mask.
13080 	 */
13081 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
13082 		return (EINVAL);
13083 	for (i = 0; i < 4; i++) {
13084 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
13085 	}
13086 
13087 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
13088 	    ill->ill_token_length == addrlen)
13089 		return (0);	/* No change */
13090 
13091 	if (ipif->ipif_flags & IPIF_UP) {
13092 		err = ipif_logical_down(ipif, q, mp);
13093 		if (err == EINPROGRESS)
13094 			return (err);
13095 		ipif_down_tail(ipif);
13096 		need_up = B_TRUE;
13097 	}
13098 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
13099 	return (err);
13100 }
13101 
13102 static int
13103 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
13104     mblk_t *mp, boolean_t need_up)
13105 {
13106 	in6_addr_t v6addr;
13107 	in6_addr_t v6mask;
13108 	ill_t	*ill = ipif->ipif_ill;
13109 	int	i;
13110 	int	err = 0;
13111 
13112 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
13113 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13114 	v6addr = sin6->sin6_addr;
13115 	/*
13116 	 * The length of the token is the length from the end.  To get
13117 	 * the proper mask for this, compute the mask of the bits not
13118 	 * in the token; ie. the prefix, and then xor to get the mask.
13119 	 */
13120 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
13121 	for (i = 0; i < 4; i++)
13122 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
13123 
13124 	mutex_enter(&ill->ill_lock);
13125 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
13126 	ill->ill_token_length = addrlen;
13127 	mutex_exit(&ill->ill_lock);
13128 
13129 	if (need_up) {
13130 		/*
13131 		 * Now bring the interface back up.  If this
13132 		 * is the only IPIF for the ILL, ipif_up
13133 		 * will have to re-bind to the device, so
13134 		 * we may get back EINPROGRESS, in which
13135 		 * case, this IOCTL will get completed in
13136 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
13137 		 */
13138 		err = ipif_up(ipif, q, mp);
13139 		if (err == EINPROGRESS)
13140 			return (err);
13141 	}
13142 	return (err);
13143 }
13144 
13145 /* ARGSUSED */
13146 int
13147 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13148     ip_ioctl_cmd_t *ipi, void *if_req)
13149 {
13150 	ill_t *ill;
13151 	sin6_t *sin6 = (sin6_t *)sin;
13152 	struct lifreq *lifr = (struct lifreq *)if_req;
13153 
13154 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
13155 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13156 	if (ipif->ipif_id != 0)
13157 		return (EINVAL);
13158 
13159 	ill = ipif->ipif_ill;
13160 	if (!ill->ill_isv6)
13161 		return (ENXIO);
13162 
13163 	*sin6 = sin6_null;
13164 	sin6->sin6_family = AF_INET6;
13165 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
13166 	sin6->sin6_addr = ill->ill_token;
13167 	lifr->lifr_addrlen = ill->ill_token_length;
13168 	return (0);
13169 }
13170 
13171 /*
13172  * Set (hardware) link specific information that might override
13173  * what was acquired through the DL_INFO_ACK.
13174  * The logic is as follows.
13175  *
13176  * become exclusive
13177  * set CHANGING flag
13178  * change mtu on affected IREs
13179  * clear CHANGING flag
13180  *
13181  * An ire add that occurs before the CHANGING flag is set will have its mtu
13182  * changed by the ip_sioctl_lnkinfo.
13183  *
13184  * During the time the CHANGING flag is set, no new ires will be added to the
13185  * bucket, and ire add will fail (due the CHANGING flag).
13186  *
13187  * An ire add that occurs after the CHANGING flag is set will have the right mtu
13188  * before it is added to the bucket.
13189  *
13190  * Obviously only 1 thread can set the CHANGING flag and we need to become
13191  * exclusive to set the flag.
13192  */
13193 /* ARGSUSED */
13194 int
13195 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13196     ip_ioctl_cmd_t *ipi, void *if_req)
13197 {
13198 	ill_t		*ill = ipif->ipif_ill;
13199 	ipif_t		*nipif;
13200 	int		ip_min_mtu;
13201 	boolean_t	mtu_walk = B_FALSE;
13202 	struct lifreq	*lifr = (struct lifreq *)if_req;
13203 	lif_ifinfo_req_t *lir;
13204 	ire_t		*ire;
13205 
13206 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
13207 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13208 	lir = &lifr->lifr_ifinfo;
13209 	ASSERT(IAM_WRITER_IPIF(ipif));
13210 
13211 	/* Only allow for logical unit zero i.e. not on "le0:17" */
13212 	if (ipif->ipif_id != 0)
13213 		return (EINVAL);
13214 
13215 	/* Set interface MTU. */
13216 	if (ipif->ipif_isv6)
13217 		ip_min_mtu = IPV6_MIN_MTU;
13218 	else
13219 		ip_min_mtu = IP_MIN_MTU;
13220 
13221 	/*
13222 	 * Verify values before we set anything. Allow zero to
13223 	 * mean unspecified.
13224 	 */
13225 	if (lir->lir_maxmtu != 0 &&
13226 	    (lir->lir_maxmtu > ill->ill_max_frag ||
13227 	    lir->lir_maxmtu < ip_min_mtu))
13228 		return (EINVAL);
13229 	if (lir->lir_reachtime != 0 &&
13230 	    lir->lir_reachtime > ND_MAX_REACHTIME)
13231 		return (EINVAL);
13232 	if (lir->lir_reachretrans != 0 &&
13233 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
13234 		return (EINVAL);
13235 
13236 	mutex_enter(&ill->ill_lock);
13237 	ill->ill_state_flags |= ILL_CHANGING;
13238 	for (nipif = ill->ill_ipif; nipif != NULL;
13239 	    nipif = nipif->ipif_next) {
13240 		nipif->ipif_state_flags |= IPIF_CHANGING;
13241 	}
13242 
13243 	mutex_exit(&ill->ill_lock);
13244 
13245 	if (lir->lir_maxmtu != 0) {
13246 		ill->ill_max_mtu = lir->lir_maxmtu;
13247 		ill->ill_mtu_userspecified = 1;
13248 		mtu_walk = B_TRUE;
13249 	}
13250 
13251 	if (lir->lir_reachtime != 0)
13252 		ill->ill_reachable_time = lir->lir_reachtime;
13253 
13254 	if (lir->lir_reachretrans != 0)
13255 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
13256 
13257 	ill->ill_max_hops = lir->lir_maxhops;
13258 
13259 	ill->ill_max_buf = ND_MAX_Q;
13260 
13261 	if (mtu_walk) {
13262 		/*
13263 		 * Set the MTU on all ipifs associated with this ill except
13264 		 * for those whose MTU was fixed via SIOCSLIFMTU.
13265 		 */
13266 		for (nipif = ill->ill_ipif; nipif != NULL;
13267 		    nipif = nipif->ipif_next) {
13268 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
13269 				continue;
13270 
13271 			nipif->ipif_mtu = ill->ill_max_mtu;
13272 
13273 			if (!(nipif->ipif_flags & IPIF_UP))
13274 				continue;
13275 
13276 			if (nipif->ipif_isv6)
13277 				ire = ipif_to_ire_v6(nipif);
13278 			else
13279 				ire = ipif_to_ire(nipif);
13280 			if (ire != NULL) {
13281 				ire->ire_max_frag = ipif->ipif_mtu;
13282 				ire_refrele(ire);
13283 			}
13284 			if (ill->ill_isv6) {
13285 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13286 				    ipif_mtu_change, (char *)nipif,
13287 				    ill);
13288 			} else {
13289 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13290 				    ipif_mtu_change, (char *)nipif,
13291 				    ill);
13292 			}
13293 		}
13294 	}
13295 
13296 	mutex_enter(&ill->ill_lock);
13297 	for (nipif = ill->ill_ipif; nipif != NULL;
13298 	    nipif = nipif->ipif_next) {
13299 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13300 	}
13301 	ILL_UNMARK_CHANGING(ill);
13302 	mutex_exit(&ill->ill_lock);
13303 
13304 	return (0);
13305 }
13306 
13307 /* ARGSUSED */
13308 int
13309 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13310     ip_ioctl_cmd_t *ipi, void *if_req)
13311 {
13312 	struct lif_ifinfo_req *lir;
13313 	ill_t *ill = ipif->ipif_ill;
13314 
13315 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13316 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13317 	if (ipif->ipif_id != 0)
13318 		return (EINVAL);
13319 
13320 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13321 	lir->lir_maxhops = ill->ill_max_hops;
13322 	lir->lir_reachtime = ill->ill_reachable_time;
13323 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13324 	lir->lir_maxmtu = ill->ill_max_mtu;
13325 
13326 	return (0);
13327 }
13328 
13329 /*
13330  * Return best guess as to the subnet mask for the specified address.
13331  * Based on the subnet masks for all the configured interfaces.
13332  *
13333  * We end up returning a zero mask in the case of default, multicast or
13334  * experimental.
13335  */
13336 static ipaddr_t
13337 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13338 {
13339 	ipaddr_t net_mask;
13340 	ill_t	*ill;
13341 	ipif_t	*ipif;
13342 	ill_walk_context_t ctx;
13343 	ipif_t	*fallback_ipif = NULL;
13344 
13345 	net_mask = ip_net_mask(addr);
13346 	if (net_mask == 0) {
13347 		*ipifp = NULL;
13348 		return (0);
13349 	}
13350 
13351 	/* Let's check to see if this is maybe a local subnet route. */
13352 	/* this function only applies to IPv4 interfaces */
13353 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13354 	ill = ILL_START_WALK_V4(&ctx, ipst);
13355 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13356 		mutex_enter(&ill->ill_lock);
13357 		for (ipif = ill->ill_ipif; ipif != NULL;
13358 		    ipif = ipif->ipif_next) {
13359 			if (!IPIF_CAN_LOOKUP(ipif))
13360 				continue;
13361 			if (!(ipif->ipif_flags & IPIF_UP))
13362 				continue;
13363 			if ((ipif->ipif_subnet & net_mask) ==
13364 			    (addr & net_mask)) {
13365 				/*
13366 				 * Don't trust pt-pt interfaces if there are
13367 				 * other interfaces.
13368 				 */
13369 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13370 					if (fallback_ipif == NULL) {
13371 						ipif_refhold_locked(ipif);
13372 						fallback_ipif = ipif;
13373 					}
13374 					continue;
13375 				}
13376 
13377 				/*
13378 				 * Fine. Just assume the same net mask as the
13379 				 * directly attached subnet interface is using.
13380 				 */
13381 				ipif_refhold_locked(ipif);
13382 				mutex_exit(&ill->ill_lock);
13383 				rw_exit(&ipst->ips_ill_g_lock);
13384 				if (fallback_ipif != NULL)
13385 					ipif_refrele(fallback_ipif);
13386 				*ipifp = ipif;
13387 				return (ipif->ipif_net_mask);
13388 			}
13389 		}
13390 		mutex_exit(&ill->ill_lock);
13391 	}
13392 	rw_exit(&ipst->ips_ill_g_lock);
13393 
13394 	*ipifp = fallback_ipif;
13395 	return ((fallback_ipif != NULL) ?
13396 	    fallback_ipif->ipif_net_mask : net_mask);
13397 }
13398 
13399 /*
13400  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13401  */
13402 static void
13403 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13404 {
13405 	IOCP	iocp;
13406 	ipft_t	*ipft;
13407 	ipllc_t	*ipllc;
13408 	mblk_t	*mp1;
13409 	cred_t	*cr;
13410 	int	error = 0;
13411 	conn_t	*connp;
13412 
13413 	ip1dbg(("ip_wput_ioctl"));
13414 	iocp = (IOCP)mp->b_rptr;
13415 	mp1 = mp->b_cont;
13416 	if (mp1 == NULL) {
13417 		iocp->ioc_error = EINVAL;
13418 		mp->b_datap->db_type = M_IOCNAK;
13419 		iocp->ioc_count = 0;
13420 		qreply(q, mp);
13421 		return;
13422 	}
13423 
13424 	/*
13425 	 * These IOCTLs provide various control capabilities to
13426 	 * upstream agents such as ULPs and processes.	There
13427 	 * are currently two such IOCTLs implemented.  They
13428 	 * are used by TCP to provide update information for
13429 	 * existing IREs and to forcibly delete an IRE for a
13430 	 * host that is not responding, thereby forcing an
13431 	 * attempt at a new route.
13432 	 */
13433 	iocp->ioc_error = EINVAL;
13434 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13435 		goto done;
13436 
13437 	ipllc = (ipllc_t *)mp1->b_rptr;
13438 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13439 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13440 			break;
13441 	}
13442 	/*
13443 	 * prefer credential from mblk over ioctl;
13444 	 * see ip_sioctl_copyin_setup
13445 	 */
13446 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13447 
13448 	/*
13449 	 * Refhold the conn in case the request gets queued up in some lookup
13450 	 */
13451 	ASSERT(CONN_Q(q));
13452 	connp = Q_TO_CONN(q);
13453 	CONN_INC_REF(connp);
13454 	if (ipft->ipft_pfi &&
13455 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13456 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13457 		error = (*ipft->ipft_pfi)(q,
13458 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13459 	}
13460 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13461 		/*
13462 		 * CONN_OPER_PENDING_DONE happens in the function called
13463 		 * through ipft_pfi above.
13464 		 */
13465 		return;
13466 	}
13467 
13468 	CONN_OPER_PENDING_DONE(connp);
13469 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13470 		freemsg(mp);
13471 		return;
13472 	}
13473 	iocp->ioc_error = error;
13474 
13475 done:
13476 	mp->b_datap->db_type = M_IOCACK;
13477 	if (iocp->ioc_error)
13478 		iocp->ioc_count = 0;
13479 	qreply(q, mp);
13480 }
13481 
13482 /*
13483  * Lookup an ipif using the sequence id (ipif_seqid)
13484  */
13485 ipif_t *
13486 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13487 {
13488 	ipif_t *ipif;
13489 
13490 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13491 
13492 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13493 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13494 			return (ipif);
13495 	}
13496 	return (NULL);
13497 }
13498 
13499 /*
13500  * Assign a unique id for the ipif. This is used later when we send
13501  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13502  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13503  * IRE is added, we verify that ipif has not disappeared.
13504  */
13505 
13506 static void
13507 ipif_assign_seqid(ipif_t *ipif)
13508 {
13509 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13510 
13511 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13512 }
13513 
13514 /*
13515  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13516  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13517  * be inserted into the first space available in the list. The value of
13518  * ipif_id will then be set to the appropriate value for its position.
13519  */
13520 static int
13521 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13522 {
13523 	ill_t *ill;
13524 	ipif_t *tipif;
13525 	ipif_t **tipifp;
13526 	int id;
13527 	ip_stack_t	*ipst;
13528 
13529 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13530 	    IAM_WRITER_IPIF(ipif));
13531 
13532 	ill = ipif->ipif_ill;
13533 	ASSERT(ill != NULL);
13534 	ipst = ill->ill_ipst;
13535 
13536 	/*
13537 	 * In the case of lo0:0 we already hold the ill_g_lock.
13538 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13539 	 * ipif_insert. Another such caller is ipif_move.
13540 	 */
13541 	if (acquire_g_lock)
13542 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13543 	if (acquire_ill_lock)
13544 		mutex_enter(&ill->ill_lock);
13545 	id = ipif->ipif_id;
13546 	tipifp = &(ill->ill_ipif);
13547 	if (id == -1) {	/* need to find a real id */
13548 		id = 0;
13549 		while ((tipif = *tipifp) != NULL) {
13550 			ASSERT(tipif->ipif_id >= id);
13551 			if (tipif->ipif_id != id)
13552 				break; /* non-consecutive id */
13553 			id++;
13554 			tipifp = &(tipif->ipif_next);
13555 		}
13556 		/* limit number of logical interfaces */
13557 		if (id >= ipst->ips_ip_addrs_per_if) {
13558 			if (acquire_ill_lock)
13559 				mutex_exit(&ill->ill_lock);
13560 			if (acquire_g_lock)
13561 				rw_exit(&ipst->ips_ill_g_lock);
13562 			return (-1);
13563 		}
13564 		ipif->ipif_id = id; /* assign new id */
13565 	} else if (id < ipst->ips_ip_addrs_per_if) {
13566 		/* we have a real id; insert ipif in the right place */
13567 		while ((tipif = *tipifp) != NULL) {
13568 			ASSERT(tipif->ipif_id != id);
13569 			if (tipif->ipif_id > id)
13570 				break; /* found correct location */
13571 			tipifp = &(tipif->ipif_next);
13572 		}
13573 	} else {
13574 		if (acquire_ill_lock)
13575 			mutex_exit(&ill->ill_lock);
13576 		if (acquire_g_lock)
13577 			rw_exit(&ipst->ips_ill_g_lock);
13578 		return (-1);
13579 	}
13580 
13581 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13582 
13583 	ipif->ipif_next = tipif;
13584 	*tipifp = ipif;
13585 	if (acquire_ill_lock)
13586 		mutex_exit(&ill->ill_lock);
13587 	if (acquire_g_lock)
13588 		rw_exit(&ipst->ips_ill_g_lock);
13589 	return (0);
13590 }
13591 
13592 static void
13593 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13594 {
13595 	ipif_t	**ipifp;
13596 	ill_t	*ill = ipif->ipif_ill;
13597 
13598 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13599 	if (acquire_ill_lock)
13600 		mutex_enter(&ill->ill_lock);
13601 	else
13602 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13603 
13604 	ipifp = &ill->ill_ipif;
13605 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13606 		if (*ipifp == ipif) {
13607 			*ipifp = ipif->ipif_next;
13608 			break;
13609 		}
13610 	}
13611 
13612 	if (acquire_ill_lock)
13613 		mutex_exit(&ill->ill_lock);
13614 }
13615 
13616 /*
13617  * Allocate and initialize a new interface control structure.  (Always
13618  * called as writer.)
13619  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13620  * is not part of the global linked list of ills. ipif_seqid is unique
13621  * in the system and to preserve the uniqueness, it is assigned only
13622  * when ill becomes part of the global list. At that point ill will
13623  * have a name. If it doesn't get assigned here, it will get assigned
13624  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13625  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13626  * the interface flags or any other information from the DL_INFO_ACK for
13627  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13628  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13629  * second DL_INFO_ACK comes in from the driver.
13630  */
13631 static ipif_t *
13632 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13633 {
13634 	ipif_t	*ipif;
13635 	phyint_t *phyi;
13636 
13637 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13638 	    ill->ill_name, id, (void *)ill));
13639 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13640 
13641 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13642 		return (NULL);
13643 	*ipif = ipif_zero;	/* start clean */
13644 
13645 	ipif->ipif_ill = ill;
13646 	ipif->ipif_id = id;	/* could be -1 */
13647 	/*
13648 	 * Inherit the zoneid from the ill; for the shared stack instance
13649 	 * this is always the global zone
13650 	 */
13651 	ipif->ipif_zoneid = ill->ill_zoneid;
13652 
13653 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13654 
13655 	ipif->ipif_refcnt = 0;
13656 	ipif->ipif_saved_ire_cnt = 0;
13657 
13658 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13659 		mi_free(ipif);
13660 		return (NULL);
13661 	}
13662 	/* -1 id should have been replaced by real id */
13663 	id = ipif->ipif_id;
13664 	ASSERT(id >= 0);
13665 
13666 	if (ill->ill_name[0] != '\0')
13667 		ipif_assign_seqid(ipif);
13668 
13669 	/*
13670 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13671 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13672 	 * ioctl sets ipif_orig_ipifid to zero.
13673 	 */
13674 	ipif->ipif_orig_ipifid = id;
13675 
13676 	/*
13677 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13678 	 * The ipif is still not up and can't be looked up until the
13679 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13680 	 */
13681 	mutex_enter(&ill->ill_lock);
13682 	mutex_enter(&ill->ill_phyint->phyint_lock);
13683 	/*
13684 	 * Set the running flag when logical interface zero is created.
13685 	 * For subsequent logical interfaces, a DLPI link down
13686 	 * notification message may have cleared the running flag to
13687 	 * indicate the link is down, so we shouldn't just blindly set it.
13688 	 */
13689 	if (id == 0)
13690 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13691 	ipif->ipif_ire_type = ire_type;
13692 	phyi = ill->ill_phyint;
13693 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13694 
13695 	if (ipif->ipif_isv6) {
13696 		ill->ill_flags |= ILLF_IPV6;
13697 	} else {
13698 		ipaddr_t inaddr_any = INADDR_ANY;
13699 
13700 		ill->ill_flags |= ILLF_IPV4;
13701 
13702 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13703 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13704 		    &ipif->ipif_v6lcl_addr);
13705 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13706 		    &ipif->ipif_v6src_addr);
13707 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13708 		    &ipif->ipif_v6subnet);
13709 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13710 		    &ipif->ipif_v6net_mask);
13711 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13712 		    &ipif->ipif_v6brd_addr);
13713 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13714 		    &ipif->ipif_v6pp_dst_addr);
13715 	}
13716 
13717 	/*
13718 	 * Don't set the interface flags etc. now, will do it in
13719 	 * ip_ll_subnet_defaults.
13720 	 */
13721 	if (!initialize) {
13722 		mutex_exit(&ill->ill_lock);
13723 		mutex_exit(&ill->ill_phyint->phyint_lock);
13724 		return (ipif);
13725 	}
13726 	ipif->ipif_mtu = ill->ill_max_mtu;
13727 
13728 	if (ill->ill_bcast_addr_length != 0) {
13729 		/*
13730 		 * Later detect lack of DLPI driver multicast
13731 		 * capability by catching DL_ENABMULTI errors in
13732 		 * ip_rput_dlpi.
13733 		 */
13734 		ill->ill_flags |= ILLF_MULTICAST;
13735 		if (!ipif->ipif_isv6)
13736 			ipif->ipif_flags |= IPIF_BROADCAST;
13737 	} else {
13738 		if (ill->ill_net_type != IRE_LOOPBACK) {
13739 			if (ipif->ipif_isv6)
13740 				/*
13741 				 * Note: xresolv interfaces will eventually need
13742 				 * NOARP set here as well, but that will require
13743 				 * those external resolvers to have some
13744 				 * knowledge of that flag and act appropriately.
13745 				 * Not to be changed at present.
13746 				 */
13747 				ill->ill_flags |= ILLF_NONUD;
13748 			else
13749 				ill->ill_flags |= ILLF_NOARP;
13750 		}
13751 		if (ill->ill_phys_addr_length == 0) {
13752 			if (ill->ill_media &&
13753 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13754 				ipif->ipif_flags |= IPIF_NOXMIT;
13755 				phyi->phyint_flags |= PHYI_VIRTUAL;
13756 			} else {
13757 				/* pt-pt supports multicast. */
13758 				ill->ill_flags |= ILLF_MULTICAST;
13759 				if (ill->ill_net_type == IRE_LOOPBACK) {
13760 					phyi->phyint_flags |=
13761 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13762 				} else {
13763 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13764 				}
13765 			}
13766 		}
13767 	}
13768 	mutex_exit(&ill->ill_lock);
13769 	mutex_exit(&ill->ill_phyint->phyint_lock);
13770 	return (ipif);
13771 }
13772 
13773 /*
13774  * If appropriate, send a message up to the resolver delete the entry
13775  * for the address of this interface which is going out of business.
13776  * (Always called as writer).
13777  *
13778  * NOTE : We need to check for NULL mps as some of the fields are
13779  *	  initialized only for some interface types. See ipif_resolver_up()
13780  *	  for details.
13781  */
13782 void
13783 ipif_arp_down(ipif_t *ipif)
13784 {
13785 	mblk_t	*mp;
13786 	ill_t	*ill = ipif->ipif_ill;
13787 
13788 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13789 	ASSERT(IAM_WRITER_IPIF(ipif));
13790 
13791 	/* Delete the mapping for the local address */
13792 	mp = ipif->ipif_arp_del_mp;
13793 	if (mp != NULL) {
13794 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13795 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13796 		putnext(ill->ill_rq, mp);
13797 		ipif->ipif_arp_del_mp = NULL;
13798 	}
13799 
13800 	/*
13801 	 * If this is the last ipif that is going down and there are no
13802 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13803 	 * clean up ARP completely.
13804 	 */
13805 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13806 
13807 		/* Send up AR_INTERFACE_DOWN message */
13808 		mp = ill->ill_arp_down_mp;
13809 		if (mp != NULL) {
13810 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13811 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13812 			    ipif->ipif_id));
13813 			putnext(ill->ill_rq, mp);
13814 			ill->ill_arp_down_mp = NULL;
13815 		}
13816 
13817 		/* Tell ARP to delete the multicast mappings */
13818 		mp = ill->ill_arp_del_mapping_mp;
13819 		if (mp != NULL) {
13820 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13821 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13822 			    ipif->ipif_id));
13823 			putnext(ill->ill_rq, mp);
13824 			ill->ill_arp_del_mapping_mp = NULL;
13825 		}
13826 	}
13827 }
13828 
13829 /*
13830  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13831  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13832  * that it wants the add_mp allocated in this function to be returned
13833  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13834  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13835  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13836  * as it does a ipif_arp_down after calling this function - which will
13837  * remove what we add here.
13838  *
13839  * Returns -1 on failures and 0 on success.
13840  */
13841 int
13842 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13843 {
13844 	mblk_t	*del_mp = NULL;
13845 	mblk_t *add_mp = NULL;
13846 	mblk_t *mp;
13847 	ill_t	*ill = ipif->ipif_ill;
13848 	phyint_t *phyi = ill->ill_phyint;
13849 	ipaddr_t addr, mask, extract_mask = 0;
13850 	arma_t	*arma;
13851 	uint8_t *maddr, *bphys_addr;
13852 	uint32_t hw_start;
13853 	dl_unitdata_req_t *dlur;
13854 
13855 	ASSERT(IAM_WRITER_IPIF(ipif));
13856 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13857 		return (0);
13858 
13859 	/*
13860 	 * Delete the existing mapping from ARP. Normally ipif_down
13861 	 * -> ipif_arp_down should send this up to ARP. The only
13862 	 * reason we would find this when we are switching from
13863 	 * Multicast to Broadcast where we did not do a down.
13864 	 */
13865 	mp = ill->ill_arp_del_mapping_mp;
13866 	if (mp != NULL) {
13867 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13868 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13869 		putnext(ill->ill_rq, mp);
13870 		ill->ill_arp_del_mapping_mp = NULL;
13871 	}
13872 
13873 	if (arp_add_mapping_mp != NULL)
13874 		*arp_add_mapping_mp = NULL;
13875 
13876 	/*
13877 	 * Check that the address is not to long for the constant
13878 	 * length reserved in the template arma_t.
13879 	 */
13880 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13881 		return (-1);
13882 
13883 	/* Add mapping mblk */
13884 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13885 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13886 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13887 	    (caddr_t)&addr);
13888 	if (add_mp == NULL)
13889 		return (-1);
13890 	arma = (arma_t *)add_mp->b_rptr;
13891 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13892 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13893 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13894 
13895 	/*
13896 	 * Determine the broadcast address.
13897 	 */
13898 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13899 	if (ill->ill_sap_length < 0)
13900 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13901 	else
13902 		bphys_addr = (uchar_t *)dlur +
13903 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13904 	/*
13905 	 * Check PHYI_MULTI_BCAST and length of physical
13906 	 * address to determine if we use the mapping or the
13907 	 * broadcast address.
13908 	 */
13909 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13910 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13911 		    bphys_addr, maddr, &hw_start, &extract_mask))
13912 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13913 
13914 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13915 	    (ill->ill_flags & ILLF_MULTICAST)) {
13916 		/* Make sure this will not match the "exact" entry. */
13917 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13918 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13919 		    (caddr_t)&addr);
13920 		if (del_mp == NULL) {
13921 			freemsg(add_mp);
13922 			return (-1);
13923 		}
13924 		bcopy(&extract_mask, (char *)arma +
13925 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13926 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13927 			/* Use link-layer broadcast address for MULTI_BCAST */
13928 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13929 			ip2dbg(("ipif_arp_setup_multicast: adding"
13930 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13931 		} else {
13932 			arma->arma_hw_mapping_start = hw_start;
13933 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13934 			    " ARP setup for %s\n", ill->ill_name));
13935 		}
13936 	} else {
13937 		freemsg(add_mp);
13938 		ASSERT(del_mp == NULL);
13939 		/* It is neither MULTICAST nor MULTI_BCAST */
13940 		return (0);
13941 	}
13942 	ASSERT(add_mp != NULL && del_mp != NULL);
13943 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13944 	ill->ill_arp_del_mapping_mp = del_mp;
13945 	if (arp_add_mapping_mp != NULL) {
13946 		/* The caller just wants the mblks allocated */
13947 		*arp_add_mapping_mp = add_mp;
13948 	} else {
13949 		/* The caller wants us to send it to arp */
13950 		putnext(ill->ill_rq, add_mp);
13951 	}
13952 	return (0);
13953 }
13954 
13955 /*
13956  * Get the resolver set up for a new interface address.
13957  * (Always called as writer.)
13958  * Called both for IPv4 and IPv6 interfaces,
13959  * though it only sets up the resolver for v6
13960  * if it's an xresolv interface (one using an external resolver).
13961  * Honors ILLF_NOARP.
13962  * The enumerated value res_act is used to tune the behavior.
13963  * If set to Res_act_initial, then we set up all the resolver
13964  * structures for a new interface.  If set to Res_act_move, then
13965  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13966  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13967  * asynchronous hardware address change notification.  If set to
13968  * Res_act_defend, then we tell ARP that it needs to send a single
13969  * gratuitous message in defense of the address.
13970  * Returns error on failure.
13971  */
13972 int
13973 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13974 {
13975 	caddr_t	addr;
13976 	mblk_t	*arp_up_mp = NULL;
13977 	mblk_t	*arp_down_mp = NULL;
13978 	mblk_t	*arp_add_mp = NULL;
13979 	mblk_t	*arp_del_mp = NULL;
13980 	mblk_t	*arp_add_mapping_mp = NULL;
13981 	mblk_t	*arp_del_mapping_mp = NULL;
13982 	ill_t	*ill = ipif->ipif_ill;
13983 	uchar_t	*area_p = NULL;
13984 	uchar_t	*ared_p = NULL;
13985 	int	err = ENOMEM;
13986 	boolean_t was_dup;
13987 
13988 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13989 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13990 	ASSERT(IAM_WRITER_IPIF(ipif));
13991 
13992 	was_dup = B_FALSE;
13993 	if (res_act == Res_act_initial) {
13994 		ipif->ipif_addr_ready = 0;
13995 		/*
13996 		 * We're bringing an interface up here.  There's no way that we
13997 		 * should need to shut down ARP now.
13998 		 */
13999 		mutex_enter(&ill->ill_lock);
14000 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
14001 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
14002 			ill->ill_ipif_dup_count--;
14003 			was_dup = B_TRUE;
14004 		}
14005 		mutex_exit(&ill->ill_lock);
14006 	}
14007 	if (ipif->ipif_recovery_id != 0)
14008 		(void) untimeout(ipif->ipif_recovery_id);
14009 	ipif->ipif_recovery_id = 0;
14010 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
14011 		ipif->ipif_addr_ready = 1;
14012 		return (0);
14013 	}
14014 	/* NDP will set the ipif_addr_ready flag when it's ready */
14015 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
14016 		return (0);
14017 
14018 	if (ill->ill_isv6) {
14019 		/*
14020 		 * External resolver for IPv6
14021 		 */
14022 		ASSERT(res_act == Res_act_initial);
14023 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
14024 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
14025 			area_p = (uchar_t *)&ip6_area_template;
14026 			ared_p = (uchar_t *)&ip6_ared_template;
14027 		}
14028 	} else {
14029 		/*
14030 		 * IPv4 arp case. If the ARP stream has already started
14031 		 * closing, fail this request for ARP bringup. Else
14032 		 * record the fact that an ARP bringup is pending.
14033 		 */
14034 		mutex_enter(&ill->ill_lock);
14035 		if (ill->ill_arp_closing) {
14036 			mutex_exit(&ill->ill_lock);
14037 			err = EINVAL;
14038 			goto failed;
14039 		} else {
14040 			if (ill->ill_ipif_up_count == 0 &&
14041 			    ill->ill_ipif_dup_count == 0 && !was_dup)
14042 				ill->ill_arp_bringup_pending = 1;
14043 			mutex_exit(&ill->ill_lock);
14044 		}
14045 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
14046 			addr = (caddr_t)&ipif->ipif_lcl_addr;
14047 			area_p = (uchar_t *)&ip_area_template;
14048 			ared_p = (uchar_t *)&ip_ared_template;
14049 		}
14050 	}
14051 
14052 	/*
14053 	 * Add an entry for the local address in ARP only if it
14054 	 * is not UNNUMBERED and the address is not INADDR_ANY.
14055 	 */
14056 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
14057 		area_t *area;
14058 
14059 		/* Now ask ARP to publish our address. */
14060 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
14061 		if (arp_add_mp == NULL)
14062 			goto failed;
14063 		area = (area_t *)arp_add_mp->b_rptr;
14064 		if (res_act != Res_act_initial) {
14065 			/*
14066 			 * Copy the new hardware address and length into
14067 			 * arp_add_mp to be sent to ARP.
14068 			 */
14069 			area->area_hw_addr_length = ill->ill_phys_addr_length;
14070 			bcopy(ill->ill_phys_addr,
14071 			    ((char *)area + area->area_hw_addr_offset),
14072 			    area->area_hw_addr_length);
14073 		}
14074 
14075 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
14076 		    ACE_F_MYADDR;
14077 
14078 		if (res_act == Res_act_defend) {
14079 			area->area_flags |= ACE_F_DEFEND;
14080 			/*
14081 			 * If we're just defending our address now, then
14082 			 * there's no need to set up ARP multicast mappings.
14083 			 * The publish command is enough.
14084 			 */
14085 			goto done;
14086 		}
14087 
14088 		if (res_act != Res_act_initial)
14089 			goto arp_setup_multicast;
14090 
14091 		/*
14092 		 * Allocate an ARP deletion message so we know we can tell ARP
14093 		 * when the interface goes down.
14094 		 */
14095 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
14096 		if (arp_del_mp == NULL)
14097 			goto failed;
14098 
14099 	} else {
14100 		if (res_act != Res_act_initial)
14101 			goto done;
14102 	}
14103 	/*
14104 	 * Need to bring up ARP or setup multicast mapping only
14105 	 * when the first interface is coming UP.
14106 	 */
14107 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
14108 	    was_dup) {
14109 		goto done;
14110 	}
14111 
14112 	/*
14113 	 * Allocate an ARP down message (to be saved) and an ARP up
14114 	 * message.
14115 	 */
14116 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
14117 	if (arp_down_mp == NULL)
14118 		goto failed;
14119 
14120 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
14121 	if (arp_up_mp == NULL)
14122 		goto failed;
14123 
14124 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
14125 		goto done;
14126 
14127 arp_setup_multicast:
14128 	/*
14129 	 * Setup the multicast mappings. This function initializes
14130 	 * ill_arp_del_mapping_mp also. This does not need to be done for
14131 	 * IPv6.
14132 	 */
14133 	if (!ill->ill_isv6) {
14134 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
14135 		if (err != 0)
14136 			goto failed;
14137 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
14138 		ASSERT(arp_add_mapping_mp != NULL);
14139 	}
14140 
14141 done:
14142 	if (arp_del_mp != NULL) {
14143 		ASSERT(ipif->ipif_arp_del_mp == NULL);
14144 		ipif->ipif_arp_del_mp = arp_del_mp;
14145 	}
14146 	if (arp_down_mp != NULL) {
14147 		ASSERT(ill->ill_arp_down_mp == NULL);
14148 		ill->ill_arp_down_mp = arp_down_mp;
14149 	}
14150 	if (arp_del_mapping_mp != NULL) {
14151 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
14152 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
14153 	}
14154 	if (arp_up_mp != NULL) {
14155 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
14156 		    ill->ill_name, ipif->ipif_id));
14157 		putnext(ill->ill_rq, arp_up_mp);
14158 	}
14159 	if (arp_add_mp != NULL) {
14160 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
14161 		    ill->ill_name, ipif->ipif_id));
14162 		/*
14163 		 * If it's an extended ARP implementation, then we'll wait to
14164 		 * hear that DAD has finished before using the interface.
14165 		 */
14166 		if (!ill->ill_arp_extend)
14167 			ipif->ipif_addr_ready = 1;
14168 		putnext(ill->ill_rq, arp_add_mp);
14169 	} else {
14170 		ipif->ipif_addr_ready = 1;
14171 	}
14172 	if (arp_add_mapping_mp != NULL) {
14173 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
14174 		    ill->ill_name, ipif->ipif_id));
14175 		putnext(ill->ill_rq, arp_add_mapping_mp);
14176 	}
14177 	if (res_act != Res_act_initial)
14178 		return (0);
14179 
14180 	if (ill->ill_flags & ILLF_NOARP)
14181 		err = ill_arp_off(ill);
14182 	else
14183 		err = ill_arp_on(ill);
14184 	if (err != 0) {
14185 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
14186 		freemsg(ipif->ipif_arp_del_mp);
14187 		freemsg(ill->ill_arp_down_mp);
14188 		freemsg(ill->ill_arp_del_mapping_mp);
14189 		ipif->ipif_arp_del_mp = NULL;
14190 		ill->ill_arp_down_mp = NULL;
14191 		ill->ill_arp_del_mapping_mp = NULL;
14192 		return (err);
14193 	}
14194 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
14195 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
14196 
14197 failed:
14198 	ip1dbg(("ipif_resolver_up: FAILED\n"));
14199 	freemsg(arp_add_mp);
14200 	freemsg(arp_del_mp);
14201 	freemsg(arp_add_mapping_mp);
14202 	freemsg(arp_up_mp);
14203 	freemsg(arp_down_mp);
14204 	ill->ill_arp_bringup_pending = 0;
14205 	return (err);
14206 }
14207 
14208 /*
14209  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
14210  * just gone back up.
14211  */
14212 static void
14213 ipif_arp_start_dad(ipif_t *ipif)
14214 {
14215 	ill_t *ill = ipif->ipif_ill;
14216 	mblk_t *arp_add_mp;
14217 	area_t *area;
14218 
14219 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
14220 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14221 	    ipif->ipif_lcl_addr == INADDR_ANY ||
14222 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
14223 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
14224 		/*
14225 		 * If we can't contact ARP for some reason, that's not really a
14226 		 * problem.  Just send out the routing socket notification that
14227 		 * DAD completion would have done, and continue.
14228 		 */
14229 		ipif_mask_reply(ipif);
14230 		ip_rts_ifmsg(ipif);
14231 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14232 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14233 		ipif->ipif_addr_ready = 1;
14234 		return;
14235 	}
14236 
14237 	/* Setting the 'unverified' flag restarts DAD */
14238 	area = (area_t *)arp_add_mp->b_rptr;
14239 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
14240 	    ACE_F_UNVERIFIED;
14241 	putnext(ill->ill_rq, arp_add_mp);
14242 }
14243 
14244 static void
14245 ipif_ndp_start_dad(ipif_t *ipif)
14246 {
14247 	nce_t *nce;
14248 
14249 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
14250 	if (nce == NULL)
14251 		return;
14252 
14253 	if (!ndp_restart_dad(nce)) {
14254 		/*
14255 		 * If we can't restart DAD for some reason, that's not really a
14256 		 * problem.  Just send out the routing socket notification that
14257 		 * DAD completion would have done, and continue.
14258 		 */
14259 		ip_rts_ifmsg(ipif);
14260 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14261 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14262 		ipif->ipif_addr_ready = 1;
14263 	}
14264 	NCE_REFRELE(nce);
14265 }
14266 
14267 /*
14268  * Restart duplicate address detection on all interfaces on the given ill.
14269  *
14270  * This is called when an interface transitions from down to up
14271  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
14272  *
14273  * Note that since the underlying physical link has transitioned, we must cause
14274  * at least one routing socket message to be sent here, either via DAD
14275  * completion or just by default on the first ipif.  (If we don't do this, then
14276  * in.mpathd will see long delays when doing link-based failure recovery.)
14277  */
14278 void
14279 ill_restart_dad(ill_t *ill, boolean_t went_up)
14280 {
14281 	ipif_t *ipif;
14282 
14283 	if (ill == NULL)
14284 		return;
14285 
14286 	/*
14287 	 * If layer two doesn't support duplicate address detection, then just
14288 	 * send the routing socket message now and be done with it.
14289 	 */
14290 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14291 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14292 		ip_rts_ifmsg(ill->ill_ipif);
14293 		return;
14294 	}
14295 
14296 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14297 		if (went_up) {
14298 			if (ipif->ipif_flags & IPIF_UP) {
14299 				if (ill->ill_isv6)
14300 					ipif_ndp_start_dad(ipif);
14301 				else
14302 					ipif_arp_start_dad(ipif);
14303 			} else if (ill->ill_isv6 &&
14304 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14305 				/*
14306 				 * For IPv4, the ARP module itself will
14307 				 * automatically start the DAD process when it
14308 				 * sees DL_NOTE_LINK_UP.  We respond to the
14309 				 * AR_CN_READY at the completion of that task.
14310 				 * For IPv6, we must kick off the bring-up
14311 				 * process now.
14312 				 */
14313 				ndp_do_recovery(ipif);
14314 			} else {
14315 				/*
14316 				 * Unfortunately, the first ipif is "special"
14317 				 * and represents the underlying ill in the
14318 				 * routing socket messages.  Thus, when this
14319 				 * one ipif is down, we must still notify so
14320 				 * that the user knows the IFF_RUNNING status
14321 				 * change.  (If the first ipif is up, then
14322 				 * we'll handle eventual routing socket
14323 				 * notification via DAD completion.)
14324 				 */
14325 				if (ipif == ill->ill_ipif)
14326 					ip_rts_ifmsg(ill->ill_ipif);
14327 			}
14328 		} else {
14329 			/*
14330 			 * After link down, we'll need to send a new routing
14331 			 * message when the link comes back, so clear
14332 			 * ipif_addr_ready.
14333 			 */
14334 			ipif->ipif_addr_ready = 0;
14335 		}
14336 	}
14337 
14338 	/*
14339 	 * If we've torn down links, then notify the user right away.
14340 	 */
14341 	if (!went_up)
14342 		ip_rts_ifmsg(ill->ill_ipif);
14343 }
14344 
14345 /*
14346  * Wakeup all threads waiting to enter the ipsq, and sleeping
14347  * on any of the ills in this ipsq. The ill_lock of the ill
14348  * must be held so that waiters don't miss wakeups
14349  */
14350 static void
14351 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14352 {
14353 	phyint_t *phyint;
14354 
14355 	phyint = ipsq->ipsq_phyint_list;
14356 	while (phyint != NULL) {
14357 		if (phyint->phyint_illv4) {
14358 			if (!caller_holds_lock)
14359 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14360 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14361 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14362 			if (!caller_holds_lock)
14363 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14364 		}
14365 		if (phyint->phyint_illv6) {
14366 			if (!caller_holds_lock)
14367 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14368 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14369 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14370 			if (!caller_holds_lock)
14371 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14372 		}
14373 		phyint = phyint->phyint_ipsq_next;
14374 	}
14375 }
14376 
14377 static ipsq_t *
14378 ipsq_create(char *groupname, ip_stack_t *ipst)
14379 {
14380 	ipsq_t	*ipsq;
14381 
14382 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14383 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14384 	if (ipsq == NULL) {
14385 		return (NULL);
14386 	}
14387 
14388 	if (groupname != NULL)
14389 		(void) strcpy(ipsq->ipsq_name, groupname);
14390 	else
14391 		ipsq->ipsq_name[0] = '\0';
14392 
14393 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14394 	ipsq->ipsq_flags |= IPSQ_GROUP;
14395 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14396 	ipst->ips_ipsq_g_head = ipsq;
14397 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14398 	return (ipsq);
14399 }
14400 
14401 /*
14402  * Return an ipsq correspoding to the groupname. If 'create' is true
14403  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14404  * uniquely with an IPMP group. However during IPMP groupname operations,
14405  * multiple IPMP groups may be associated with a single ipsq. But no
14406  * IPMP group can be associated with more than 1 ipsq at any time.
14407  * For example
14408  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14409  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14410  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14411  *
14412  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14413  * status shown below during the execution of the above command.
14414  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14415  *
14416  * After the completion of the above groupname command we return to the stable
14417  * state shown below.
14418  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14419  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14420  *
14421  * Because of the above, we don't search based on the ipsq_name since that
14422  * would miss the correct ipsq during certain windows as shown above.
14423  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14424  * natural state.
14425  */
14426 static ipsq_t *
14427 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14428     ip_stack_t *ipst)
14429 {
14430 	ipsq_t	*ipsq;
14431 	int	group_len;
14432 	phyint_t *phyint;
14433 
14434 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14435 
14436 	group_len = strlen(groupname);
14437 	ASSERT(group_len != 0);
14438 	group_len++;
14439 
14440 	for (ipsq = ipst->ips_ipsq_g_head;
14441 	    ipsq != NULL;
14442 	    ipsq = ipsq->ipsq_next) {
14443 		/*
14444 		 * When an ipsq is being split, and ill_split_ipsq
14445 		 * calls this function, we exclude it from being considered.
14446 		 */
14447 		if (ipsq == exclude_ipsq)
14448 			continue;
14449 
14450 		/*
14451 		 * Compare against the ipsq_name. The groupname change happens
14452 		 * in 2 phases. The 1st phase merges the from group into
14453 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14454 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14455 		 * ipsq_name. At this point the phyint_groupname has not been
14456 		 * updated.
14457 		 */
14458 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14459 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14460 			/*
14461 			 * Verify that an ipmp groupname is exactly
14462 			 * part of 1 ipsq and is not found in any other
14463 			 * ipsq.
14464 			 */
14465 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14466 			    NULL);
14467 			return (ipsq);
14468 		}
14469 
14470 		/*
14471 		 * Comparison against ipsq_name alone is not sufficient.
14472 		 * In the case when groups are currently being
14473 		 * merged, the ipsq could hold other IPMP groups temporarily.
14474 		 * so we walk the phyint list and compare against the
14475 		 * phyint_groupname as well.
14476 		 */
14477 		phyint = ipsq->ipsq_phyint_list;
14478 		while (phyint != NULL) {
14479 			if ((group_len == phyint->phyint_groupname_len) &&
14480 			    (bcmp(phyint->phyint_groupname, groupname,
14481 			    group_len) == 0)) {
14482 				/*
14483 				 * Verify that an ipmp groupname is exactly
14484 				 * part of 1 ipsq and is not found in any other
14485 				 * ipsq.
14486 				 */
14487 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14488 				    ipst) == NULL);
14489 				return (ipsq);
14490 			}
14491 			phyint = phyint->phyint_ipsq_next;
14492 		}
14493 	}
14494 	if (create)
14495 		ipsq = ipsq_create(groupname, ipst);
14496 	return (ipsq);
14497 }
14498 
14499 static void
14500 ipsq_delete(ipsq_t *ipsq)
14501 {
14502 	ipsq_t *nipsq;
14503 	ipsq_t *pipsq = NULL;
14504 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14505 
14506 	/*
14507 	 * We don't hold the ipsq lock, but we are sure no new
14508 	 * messages can land up, since the ipsq_refs is zero.
14509 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14510 	 * is associated with this ipsq. (Lookups are based on ill_name
14511 	 * or phyint_groupname)
14512 	 */
14513 	ASSERT(ipsq->ipsq_refs == 0);
14514 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14515 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14516 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14517 		/*
14518 		 * This is not the ipsq of an IPMP group.
14519 		 */
14520 		ipsq->ipsq_ipst = NULL;
14521 		kmem_free(ipsq, sizeof (ipsq_t));
14522 		return;
14523 	}
14524 
14525 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14526 
14527 	/*
14528 	 * Locate the ipsq  before we can remove it from
14529 	 * the singly linked list of ipsq's.
14530 	 */
14531 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14532 	    nipsq = nipsq->ipsq_next) {
14533 		if (nipsq == ipsq) {
14534 			break;
14535 		}
14536 		pipsq = nipsq;
14537 	}
14538 
14539 	ASSERT(nipsq == ipsq);
14540 
14541 	/* unlink ipsq from the list */
14542 	if (pipsq != NULL)
14543 		pipsq->ipsq_next = ipsq->ipsq_next;
14544 	else
14545 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14546 	ipsq->ipsq_ipst = NULL;
14547 	kmem_free(ipsq, sizeof (ipsq_t));
14548 	rw_exit(&ipst->ips_ill_g_lock);
14549 }
14550 
14551 static void
14552 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14553     queue_t *q)
14554 {
14555 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14556 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14557 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14558 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14559 	ASSERT(current_mp != NULL);
14560 
14561 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14562 	    NEW_OP, NULL);
14563 
14564 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14565 	    new_ipsq->ipsq_xopq_mphead != NULL);
14566 
14567 	/*
14568 	 * move from old ipsq to the new ipsq.
14569 	 */
14570 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14571 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14572 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14573 
14574 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14575 }
14576 
14577 void
14578 ill_group_cleanup(ill_t *ill)
14579 {
14580 	ill_t *ill_v4;
14581 	ill_t *ill_v6;
14582 	ipif_t *ipif;
14583 
14584 	ill_v4 = ill->ill_phyint->phyint_illv4;
14585 	ill_v6 = ill->ill_phyint->phyint_illv6;
14586 
14587 	if (ill_v4 != NULL) {
14588 		mutex_enter(&ill_v4->ill_lock);
14589 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14590 		    ipif = ipif->ipif_next) {
14591 			IPIF_UNMARK_MOVING(ipif);
14592 		}
14593 		ill_v4->ill_up_ipifs = B_FALSE;
14594 		mutex_exit(&ill_v4->ill_lock);
14595 	}
14596 
14597 	if (ill_v6 != NULL) {
14598 		mutex_enter(&ill_v6->ill_lock);
14599 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14600 		    ipif = ipif->ipif_next) {
14601 			IPIF_UNMARK_MOVING(ipif);
14602 		}
14603 		ill_v6->ill_up_ipifs = B_FALSE;
14604 		mutex_exit(&ill_v6->ill_lock);
14605 	}
14606 }
14607 /*
14608  * This function is called when an ill has had a change in its group status
14609  * to bring up all the ipifs that were up before the change.
14610  */
14611 int
14612 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14613 {
14614 	ipif_t *ipif;
14615 	ill_t *ill_v4;
14616 	ill_t *ill_v6;
14617 	ill_t *from_ill;
14618 	int err = 0;
14619 
14620 
14621 	ASSERT(IAM_WRITER_ILL(ill));
14622 
14623 	/*
14624 	 * Except for ipif_state_flags and ill_state_flags the other
14625 	 * fields of the ipif/ill that are modified below are protected
14626 	 * implicitly since we are a writer. We would have tried to down
14627 	 * even an ipif that was already down, in ill_down_ipifs. So we
14628 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14629 	 */
14630 	ill_v4 = ill->ill_phyint->phyint_illv4;
14631 	ill_v6 = ill->ill_phyint->phyint_illv6;
14632 	if (ill_v4 != NULL) {
14633 		ill_v4->ill_up_ipifs = B_TRUE;
14634 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14635 		    ipif = ipif->ipif_next) {
14636 			mutex_enter(&ill_v4->ill_lock);
14637 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14638 			IPIF_UNMARK_MOVING(ipif);
14639 			mutex_exit(&ill_v4->ill_lock);
14640 			if (ipif->ipif_was_up) {
14641 				if (!(ipif->ipif_flags & IPIF_UP))
14642 					err = ipif_up(ipif, q, mp);
14643 				ipif->ipif_was_up = B_FALSE;
14644 				if (err != 0) {
14645 					/*
14646 					 * Can there be any other error ?
14647 					 */
14648 					ASSERT(err == EINPROGRESS);
14649 					return (err);
14650 				}
14651 			}
14652 		}
14653 		mutex_enter(&ill_v4->ill_lock);
14654 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14655 		mutex_exit(&ill_v4->ill_lock);
14656 		ill_v4->ill_up_ipifs = B_FALSE;
14657 		if (ill_v4->ill_move_in_progress) {
14658 			ASSERT(ill_v4->ill_move_peer != NULL);
14659 			ill_v4->ill_move_in_progress = B_FALSE;
14660 			from_ill = ill_v4->ill_move_peer;
14661 			from_ill->ill_move_in_progress = B_FALSE;
14662 			from_ill->ill_move_peer = NULL;
14663 			mutex_enter(&from_ill->ill_lock);
14664 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14665 			mutex_exit(&from_ill->ill_lock);
14666 			if (ill_v6 == NULL) {
14667 				if (from_ill->ill_phyint->phyint_flags &
14668 				    PHYI_STANDBY) {
14669 					phyint_inactive(from_ill->ill_phyint);
14670 				}
14671 				if (ill_v4->ill_phyint->phyint_flags &
14672 				    PHYI_STANDBY) {
14673 					phyint_inactive(ill_v4->ill_phyint);
14674 				}
14675 			}
14676 			ill_v4->ill_move_peer = NULL;
14677 		}
14678 	}
14679 
14680 	if (ill_v6 != NULL) {
14681 		ill_v6->ill_up_ipifs = B_TRUE;
14682 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14683 		    ipif = ipif->ipif_next) {
14684 			mutex_enter(&ill_v6->ill_lock);
14685 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14686 			IPIF_UNMARK_MOVING(ipif);
14687 			mutex_exit(&ill_v6->ill_lock);
14688 			if (ipif->ipif_was_up) {
14689 				if (!(ipif->ipif_flags & IPIF_UP))
14690 					err = ipif_up(ipif, q, mp);
14691 				ipif->ipif_was_up = B_FALSE;
14692 				if (err != 0) {
14693 					/*
14694 					 * Can there be any other error ?
14695 					 */
14696 					ASSERT(err == EINPROGRESS);
14697 					return (err);
14698 				}
14699 			}
14700 		}
14701 		mutex_enter(&ill_v6->ill_lock);
14702 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14703 		mutex_exit(&ill_v6->ill_lock);
14704 		ill_v6->ill_up_ipifs = B_FALSE;
14705 		if (ill_v6->ill_move_in_progress) {
14706 			ASSERT(ill_v6->ill_move_peer != NULL);
14707 			ill_v6->ill_move_in_progress = B_FALSE;
14708 			from_ill = ill_v6->ill_move_peer;
14709 			from_ill->ill_move_in_progress = B_FALSE;
14710 			from_ill->ill_move_peer = NULL;
14711 			mutex_enter(&from_ill->ill_lock);
14712 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14713 			mutex_exit(&from_ill->ill_lock);
14714 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14715 				phyint_inactive(from_ill->ill_phyint);
14716 			}
14717 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14718 				phyint_inactive(ill_v6->ill_phyint);
14719 			}
14720 			ill_v6->ill_move_peer = NULL;
14721 		}
14722 	}
14723 	return (0);
14724 }
14725 
14726 /*
14727  * bring down all the approriate ipifs.
14728  */
14729 /* ARGSUSED */
14730 static void
14731 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14732 {
14733 	ipif_t *ipif;
14734 
14735 	ASSERT(IAM_WRITER_ILL(ill));
14736 
14737 	/*
14738 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14739 	 * are modified below are protected implicitly since we are a writer
14740 	 */
14741 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14742 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14743 			continue;
14744 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14745 			/*
14746 			 * We go through the ipif_down logic even if the ipif
14747 			 * is already down, since routes can be added based
14748 			 * on down ipifs. Going through ipif_down once again
14749 			 * will delete any IREs created based on these routes.
14750 			 */
14751 			if (ipif->ipif_flags & IPIF_UP)
14752 				ipif->ipif_was_up = B_TRUE;
14753 			/*
14754 			 * If called with chk_nofailover true ipif is moving.
14755 			 */
14756 			mutex_enter(&ill->ill_lock);
14757 			if (chk_nofailover) {
14758 				ipif->ipif_state_flags |=
14759 				    IPIF_MOVING | IPIF_CHANGING;
14760 			} else {
14761 				ipif->ipif_state_flags |= IPIF_CHANGING;
14762 			}
14763 			mutex_exit(&ill->ill_lock);
14764 			/*
14765 			 * Need to re-create net/subnet bcast ires if
14766 			 * they are dependent on ipif.
14767 			 */
14768 			if (!ipif->ipif_isv6)
14769 				ipif_check_bcast_ires(ipif);
14770 			(void) ipif_logical_down(ipif, NULL, NULL);
14771 			ipif_non_duplicate(ipif);
14772 			ipif_down_tail(ipif);
14773 		}
14774 	}
14775 }
14776 
14777 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14778 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14779 	(ipsq)->ipsq_refs++;				\
14780 }
14781 
14782 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14783 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14784 	(ipsq)->ipsq_refs--;				\
14785 	if ((ipsq)->ipsq_refs == 0)				\
14786 		(ipsq)->ipsq_name[0] = '\0'; 		\
14787 }
14788 
14789 /*
14790  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14791  * new_ipsq.
14792  */
14793 static void
14794 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14795 {
14796 	phyint_t *phyint;
14797 	phyint_t *next_phyint;
14798 
14799 	/*
14800 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14801 	 * writer and the ill_lock of the ill in question. Also the dest
14802 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14803 	 */
14804 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14805 
14806 	phyint = cur_ipsq->ipsq_phyint_list;
14807 	cur_ipsq->ipsq_phyint_list = NULL;
14808 	while (phyint != NULL) {
14809 		next_phyint = phyint->phyint_ipsq_next;
14810 		IPSQ_DEC_REF(cur_ipsq, ipst);
14811 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14812 		new_ipsq->ipsq_phyint_list = phyint;
14813 		IPSQ_INC_REF(new_ipsq, ipst);
14814 		phyint->phyint_ipsq = new_ipsq;
14815 		phyint = next_phyint;
14816 	}
14817 }
14818 
14819 #define	SPLIT_SUCCESS		0
14820 #define	SPLIT_NOT_NEEDED	1
14821 #define	SPLIT_FAILED		2
14822 
14823 int
14824 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14825     ip_stack_t *ipst)
14826 {
14827 	ipsq_t *newipsq = NULL;
14828 
14829 	/*
14830 	 * Assertions denote pre-requisites for changing the ipsq of
14831 	 * a phyint
14832 	 */
14833 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14834 	/*
14835 	 * <ill-phyint> assocs can't change while ill_g_lock
14836 	 * is held as writer. See ill_phyint_reinit()
14837 	 */
14838 	ASSERT(phyint->phyint_illv4 == NULL ||
14839 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14840 	ASSERT(phyint->phyint_illv6 == NULL ||
14841 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14842 
14843 	if ((phyint->phyint_groupname_len !=
14844 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14845 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14846 	    phyint->phyint_groupname_len) != 0)) {
14847 		/*
14848 		 * Once we fail in creating a new ipsq due to memory shortage,
14849 		 * don't attempt to create new ipsq again, based on another
14850 		 * phyint, since we want all phyints belonging to an IPMP group
14851 		 * to be in the same ipsq even in the event of mem alloc fails.
14852 		 */
14853 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14854 		    cur_ipsq, ipst);
14855 		if (newipsq == NULL) {
14856 			/* Memory allocation failure */
14857 			return (SPLIT_FAILED);
14858 		} else {
14859 			/* ipsq_refs protected by ill_g_lock (writer) */
14860 			IPSQ_DEC_REF(cur_ipsq, ipst);
14861 			phyint->phyint_ipsq = newipsq;
14862 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14863 			newipsq->ipsq_phyint_list = phyint;
14864 			IPSQ_INC_REF(newipsq, ipst);
14865 			return (SPLIT_SUCCESS);
14866 		}
14867 	}
14868 	return (SPLIT_NOT_NEEDED);
14869 }
14870 
14871 /*
14872  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14873  * to do this split
14874  */
14875 static int
14876 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14877 {
14878 	ipsq_t *newipsq;
14879 
14880 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14881 	/*
14882 	 * <ill-phyint> assocs can't change while ill_g_lock
14883 	 * is held as writer. See ill_phyint_reinit()
14884 	 */
14885 
14886 	ASSERT(phyint->phyint_illv4 == NULL ||
14887 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14888 	ASSERT(phyint->phyint_illv6 == NULL ||
14889 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14890 
14891 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14892 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14893 		/*
14894 		 * ipsq_init failed due to no memory
14895 		 * caller will use the same ipsq
14896 		 */
14897 		return (SPLIT_FAILED);
14898 	}
14899 
14900 	/* ipsq_ref is protected by ill_g_lock (writer) */
14901 	IPSQ_DEC_REF(cur_ipsq, ipst);
14902 
14903 	/*
14904 	 * This is a new ipsq that is unknown to the world.
14905 	 * So we don't need to hold ipsq_lock,
14906 	 */
14907 	newipsq = phyint->phyint_ipsq;
14908 	newipsq->ipsq_writer = NULL;
14909 	newipsq->ipsq_reentry_cnt--;
14910 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14911 #ifdef ILL_DEBUG
14912 	newipsq->ipsq_depth = 0;
14913 #endif
14914 
14915 	return (SPLIT_SUCCESS);
14916 }
14917 
14918 /*
14919  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14920  * ipsq's representing their individual groups or themselves. Return
14921  * whether split needs to be retried again later.
14922  */
14923 static boolean_t
14924 ill_split_ipsq(ipsq_t *cur_ipsq)
14925 {
14926 	phyint_t *phyint;
14927 	phyint_t *next_phyint;
14928 	int	error;
14929 	boolean_t need_retry = B_FALSE;
14930 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14931 
14932 	phyint = cur_ipsq->ipsq_phyint_list;
14933 	cur_ipsq->ipsq_phyint_list = NULL;
14934 	while (phyint != NULL) {
14935 		next_phyint = phyint->phyint_ipsq_next;
14936 		/*
14937 		 * 'created' will tell us whether the callee actually
14938 		 * created an ipsq. Lack of memory may force the callee
14939 		 * to return without creating an ipsq.
14940 		 */
14941 		if (phyint->phyint_groupname == NULL) {
14942 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14943 		} else {
14944 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14945 			    need_retry, ipst);
14946 		}
14947 
14948 		switch (error) {
14949 		case SPLIT_FAILED:
14950 			need_retry = B_TRUE;
14951 			/* FALLTHRU */
14952 		case SPLIT_NOT_NEEDED:
14953 			/*
14954 			 * Keep it on the list.
14955 			 */
14956 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14957 			cur_ipsq->ipsq_phyint_list = phyint;
14958 			break;
14959 		case SPLIT_SUCCESS:
14960 			break;
14961 		default:
14962 			ASSERT(0);
14963 		}
14964 
14965 		phyint = next_phyint;
14966 	}
14967 	return (need_retry);
14968 }
14969 
14970 /*
14971  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14972  * and return the ills in the list. This list will be
14973  * needed to unlock all the ills later on by the caller.
14974  * The <ill-ipsq> associations could change between the
14975  * lock and unlock. Hence the unlock can't traverse the
14976  * ipsq to get the list of ills.
14977  */
14978 static int
14979 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14980 {
14981 	int	cnt = 0;
14982 	phyint_t	*phyint;
14983 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14984 
14985 	/*
14986 	 * The caller holds ill_g_lock to ensure that the ill memberships
14987 	 * of the ipsq don't change
14988 	 */
14989 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14990 
14991 	phyint = ipsq->ipsq_phyint_list;
14992 	while (phyint != NULL) {
14993 		if (phyint->phyint_illv4 != NULL) {
14994 			ASSERT(cnt < list_max);
14995 			list[cnt++] = phyint->phyint_illv4;
14996 		}
14997 		if (phyint->phyint_illv6 != NULL) {
14998 			ASSERT(cnt < list_max);
14999 			list[cnt++] = phyint->phyint_illv6;
15000 		}
15001 		phyint = phyint->phyint_ipsq_next;
15002 	}
15003 	ill_lock_ills(list, cnt);
15004 	return (cnt);
15005 }
15006 
15007 void
15008 ill_lock_ills(ill_t **list, int cnt)
15009 {
15010 	int	i;
15011 
15012 	if (cnt > 1) {
15013 		boolean_t try_again;
15014 		do {
15015 			try_again = B_FALSE;
15016 			for (i = 0; i < cnt - 1; i++) {
15017 				if (list[i] < list[i + 1]) {
15018 					ill_t	*tmp;
15019 
15020 					/* swap the elements */
15021 					tmp = list[i];
15022 					list[i] = list[i + 1];
15023 					list[i + 1] = tmp;
15024 					try_again = B_TRUE;
15025 				}
15026 			}
15027 		} while (try_again);
15028 	}
15029 
15030 	for (i = 0; i < cnt; i++) {
15031 		if (i == 0) {
15032 			if (list[i] != NULL)
15033 				mutex_enter(&list[i]->ill_lock);
15034 			else
15035 				return;
15036 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
15037 			mutex_enter(&list[i]->ill_lock);
15038 		}
15039 	}
15040 }
15041 
15042 void
15043 ill_unlock_ills(ill_t **list, int cnt)
15044 {
15045 	int	i;
15046 
15047 	for (i = 0; i < cnt; i++) {
15048 		if ((i == 0) && (list[i] != NULL)) {
15049 			mutex_exit(&list[i]->ill_lock);
15050 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
15051 			mutex_exit(&list[i]->ill_lock);
15052 		}
15053 	}
15054 }
15055 
15056 /*
15057  * Merge all the ills from 1 ipsq group into another ipsq group.
15058  * The source ipsq group is specified by the ipsq associated with
15059  * 'from_ill'. The destination ipsq group is specified by the ipsq
15060  * associated with 'to_ill' or 'groupname' respectively.
15061  * Note that ipsq itself does not have a reference count mechanism
15062  * and functions don't look up an ipsq and pass it around. Instead
15063  * functions pass around an ill or groupname, and the ipsq is looked
15064  * up from the ill or groupname and the required operation performed
15065  * atomically with the lookup on the ipsq.
15066  */
15067 static int
15068 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
15069     queue_t *q)
15070 {
15071 	ipsq_t *old_ipsq;
15072 	ipsq_t *new_ipsq;
15073 	ill_t	**ill_list;
15074 	int	cnt;
15075 	size_t	ill_list_size;
15076 	boolean_t became_writer_on_new_sq = B_FALSE;
15077 	ip_stack_t	*ipst = from_ill->ill_ipst;
15078 
15079 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
15080 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
15081 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
15082 
15083 	/*
15084 	 * Need to hold ill_g_lock as writer and also the ill_lock to
15085 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
15086 	 * ipsq_lock to prevent new messages from landing on an ipsq.
15087 	 */
15088 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15089 
15090 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
15091 	if (groupname != NULL)
15092 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
15093 	else {
15094 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
15095 	}
15096 
15097 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
15098 
15099 	/*
15100 	 * both groups are on the same ipsq.
15101 	 */
15102 	if (old_ipsq == new_ipsq) {
15103 		rw_exit(&ipst->ips_ill_g_lock);
15104 		return (0);
15105 	}
15106 
15107 	cnt = old_ipsq->ipsq_refs << 1;
15108 	ill_list_size = cnt * sizeof (ill_t *);
15109 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
15110 	if (ill_list == NULL) {
15111 		rw_exit(&ipst->ips_ill_g_lock);
15112 		return (ENOMEM);
15113 	}
15114 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
15115 
15116 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
15117 	mutex_enter(&new_ipsq->ipsq_lock);
15118 	if ((new_ipsq->ipsq_writer == NULL &&
15119 	    new_ipsq->ipsq_current_ipif == NULL) ||
15120 	    (new_ipsq->ipsq_writer == curthread)) {
15121 		new_ipsq->ipsq_writer = curthread;
15122 		new_ipsq->ipsq_reentry_cnt++;
15123 		became_writer_on_new_sq = B_TRUE;
15124 	}
15125 
15126 	/*
15127 	 * We are holding ill_g_lock as writer and all the ill locks of
15128 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
15129 	 * message can land up on the old ipsq even though we don't hold the
15130 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
15131 	 */
15132 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
15133 
15134 	/*
15135 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
15136 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
15137 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
15138 	 */
15139 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
15140 
15141 	/*
15142 	 * Mark the new ipsq as needing a split since it is currently
15143 	 * being shared by more than 1 IPMP group. The split will
15144 	 * occur at the end of ipsq_exit
15145 	 */
15146 	new_ipsq->ipsq_split = B_TRUE;
15147 
15148 	/* Now release all the locks */
15149 	mutex_exit(&new_ipsq->ipsq_lock);
15150 	ill_unlock_ills(ill_list, cnt);
15151 	rw_exit(&ipst->ips_ill_g_lock);
15152 
15153 	kmem_free(ill_list, ill_list_size);
15154 
15155 	/*
15156 	 * If we succeeded in becoming writer on the new ipsq, then
15157 	 * drain the new ipsq and start processing  all enqueued messages
15158 	 * including the current ioctl we are processing which is either
15159 	 * a set groupname or failover/failback.
15160 	 */
15161 	if (became_writer_on_new_sq)
15162 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
15163 
15164 	/*
15165 	 * syncq has been changed and all the messages have been moved.
15166 	 */
15167 	mutex_enter(&old_ipsq->ipsq_lock);
15168 	old_ipsq->ipsq_current_ipif = NULL;
15169 	old_ipsq->ipsq_current_ioctl = 0;
15170 	mutex_exit(&old_ipsq->ipsq_lock);
15171 	return (EINPROGRESS);
15172 }
15173 
15174 /*
15175  * Delete and add the loopback copy and non-loopback copy of
15176  * the BROADCAST ire corresponding to ill and addr. Used to
15177  * group broadcast ires together when ill becomes part of
15178  * a group.
15179  *
15180  * This function is also called when ill is leaving the group
15181  * so that the ires belonging to the group gets re-grouped.
15182  */
15183 static void
15184 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
15185 {
15186 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
15187 	ire_t **ire_ptpn = &ire_head;
15188 	ip_stack_t	*ipst = ill->ill_ipst;
15189 
15190 	/*
15191 	 * The loopback and non-loopback IREs are inserted in the order in which
15192 	 * they're found, on the basis that they are correctly ordered (loopback
15193 	 * first).
15194 	 */
15195 	for (;;) {
15196 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15197 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15198 		if (ire == NULL)
15199 			break;
15200 
15201 		/*
15202 		 * we are passing in KM_SLEEP because it is not easy to
15203 		 * go back to a sane state in case of memory failure.
15204 		 */
15205 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
15206 		ASSERT(nire != NULL);
15207 		bzero(nire, sizeof (ire_t));
15208 		/*
15209 		 * Don't use ire_max_frag directly since we don't
15210 		 * hold on to 'ire' until we add the new ire 'nire' and
15211 		 * we don't want the new ire to have a dangling reference
15212 		 * to 'ire'. The ire_max_frag of a broadcast ire must
15213 		 * be in sync with the ipif_mtu of the associate ipif.
15214 		 * For eg. this happens as a result of SIOCSLIFNAME,
15215 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
15216 		 * the driver. A change in ire_max_frag triggered as
15217 		 * as a result of path mtu discovery, or due to an
15218 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
15219 		 * route change -mtu command does not apply to broadcast ires.
15220 		 *
15221 		 * XXX We need a recovery strategy here if ire_init fails
15222 		 */
15223 		if (ire_init(nire,
15224 		    (uchar_t *)&ire->ire_addr,
15225 		    (uchar_t *)&ire->ire_mask,
15226 		    (uchar_t *)&ire->ire_src_addr,
15227 		    (uchar_t *)&ire->ire_gateway_addr,
15228 		    (uchar_t *)&ire->ire_in_src_addr,
15229 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
15230 		    &ire->ire_ipif->ipif_mtu,
15231 		    ire->ire_nce,
15232 		    ire->ire_rfq,
15233 		    ire->ire_stq,
15234 		    ire->ire_type,
15235 		    ire->ire_ipif,
15236 		    ire->ire_in_ill,
15237 		    ire->ire_cmask,
15238 		    ire->ire_phandle,
15239 		    ire->ire_ihandle,
15240 		    ire->ire_flags,
15241 		    &ire->ire_uinfo,
15242 		    NULL,
15243 		    NULL,
15244 		    ipst) == NULL) {
15245 			cmn_err(CE_PANIC, "ire_init() failed");
15246 		}
15247 		ire_delete(ire);
15248 		ire_refrele(ire);
15249 
15250 		/*
15251 		 * The newly created IREs are inserted at the tail of the list
15252 		 * starting with ire_head. As we've just allocated them no one
15253 		 * knows about them so it's safe.
15254 		 */
15255 		*ire_ptpn = nire;
15256 		ire_ptpn = &nire->ire_next;
15257 	}
15258 
15259 	for (nire = ire_head; nire != NULL; nire = nire_next) {
15260 		int error;
15261 		ire_t *oire;
15262 		/* unlink the IRE from our list before calling ire_add() */
15263 		nire_next = nire->ire_next;
15264 		nire->ire_next = NULL;
15265 
15266 		/* ire_add adds the ire at the right place in the list */
15267 		oire = nire;
15268 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
15269 		ASSERT(error == 0);
15270 		ASSERT(oire == nire);
15271 		ire_refrele(nire);	/* Held in ire_add */
15272 	}
15273 }
15274 
15275 /*
15276  * This function is usually called when an ill is inserted in
15277  * a group and all the ipifs are already UP. As all the ipifs
15278  * are already UP, the broadcast ires have already been created
15279  * and been inserted. But, ire_add_v4 would not have grouped properly.
15280  * We need to re-group for the benefit of ip_wput_ire which
15281  * expects BROADCAST ires to be grouped properly to avoid sending
15282  * more than one copy of the broadcast packet per group.
15283  *
15284  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
15285  *	  because when ipif_up_done ends up calling this, ires have
15286  *        already been added before illgrp_insert i.e before ill_group
15287  *	  has been initialized.
15288  */
15289 static void
15290 ill_group_bcast_for_xmit(ill_t *ill)
15291 {
15292 	ill_group_t *illgrp;
15293 	ipif_t *ipif;
15294 	ipaddr_t addr;
15295 	ipaddr_t net_mask;
15296 	ipaddr_t subnet_netmask;
15297 
15298 	illgrp = ill->ill_group;
15299 
15300 	/*
15301 	 * This function is called even when an ill is deleted from
15302 	 * the group. Hence, illgrp could be null.
15303 	 */
15304 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15305 		return;
15306 
15307 	/*
15308 	 * Delete all the BROADCAST ires matching this ill and add
15309 	 * them back. This time, ire_add_v4 should take care of
15310 	 * grouping them with others because ill is part of the
15311 	 * group.
15312 	 */
15313 	ill_bcast_delete_and_add(ill, 0);
15314 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15315 
15316 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15317 
15318 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15319 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15320 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15321 		} else {
15322 			net_mask = htonl(IN_CLASSA_NET);
15323 		}
15324 		addr = net_mask & ipif->ipif_subnet;
15325 		ill_bcast_delete_and_add(ill, addr);
15326 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15327 
15328 		subnet_netmask = ipif->ipif_net_mask;
15329 		addr = ipif->ipif_subnet;
15330 		ill_bcast_delete_and_add(ill, addr);
15331 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15332 	}
15333 }
15334 
15335 /*
15336  * This function is called from illgrp_delete when ill is being deleted
15337  * from the group.
15338  *
15339  * As ill is not there in the group anymore, any address belonging
15340  * to this ill should be cleared of IRE_MARK_NORECV.
15341  */
15342 static void
15343 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15344 {
15345 	ire_t *ire;
15346 	irb_t *irb;
15347 	ip_stack_t	*ipst = ill->ill_ipst;
15348 
15349 	ASSERT(ill->ill_group == NULL);
15350 
15351 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15352 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15353 
15354 	if (ire != NULL) {
15355 		/*
15356 		 * IPMP and plumbing operations are serialized on the ipsq, so
15357 		 * no one will insert or delete a broadcast ire under our feet.
15358 		 */
15359 		irb = ire->ire_bucket;
15360 		rw_enter(&irb->irb_lock, RW_READER);
15361 		ire_refrele(ire);
15362 
15363 		for (; ire != NULL; ire = ire->ire_next) {
15364 			if (ire->ire_addr != addr)
15365 				break;
15366 			if (ire_to_ill(ire) != ill)
15367 				continue;
15368 
15369 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15370 			ire->ire_marks &= ~IRE_MARK_NORECV;
15371 		}
15372 		rw_exit(&irb->irb_lock);
15373 	}
15374 }
15375 
15376 /*
15377  * This function must be called only after the broadcast ires
15378  * have been grouped together. For a given address addr, nominate
15379  * only one of the ires whose interface is not FAILED or OFFLINE.
15380  *
15381  * This is also called when an ipif goes down, so that we can nominate
15382  * a different ire with the same address for receiving.
15383  */
15384 static void
15385 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15386 {
15387 	irb_t *irb;
15388 	ire_t *ire;
15389 	ire_t *ire1;
15390 	ire_t *save_ire;
15391 	ire_t **irep = NULL;
15392 	boolean_t first = B_TRUE;
15393 	ire_t *clear_ire = NULL;
15394 	ire_t *start_ire = NULL;
15395 	ire_t	*new_lb_ire;
15396 	ire_t	*new_nlb_ire;
15397 	boolean_t new_lb_ire_used = B_FALSE;
15398 	boolean_t new_nlb_ire_used = B_FALSE;
15399 	uint64_t match_flags;
15400 	uint64_t phyi_flags;
15401 	boolean_t fallback = B_FALSE;
15402 	uint_t	max_frag;
15403 
15404 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15405 	    NULL, MATCH_IRE_TYPE, ipst);
15406 	/*
15407 	 * We may not be able to find some ires if a previous
15408 	 * ire_create failed. This happens when an ipif goes
15409 	 * down and we are unable to create BROADCAST ires due
15410 	 * to memory failure. Thus, we have to check for NULL
15411 	 * below. This should handle the case for LOOPBACK,
15412 	 * POINTOPOINT and interfaces with some POINTOPOINT
15413 	 * logicals for which there are no BROADCAST ires.
15414 	 */
15415 	if (ire == NULL)
15416 		return;
15417 	/*
15418 	 * Currently IRE_BROADCASTS are deleted when an ipif
15419 	 * goes down which runs exclusively. Thus, setting
15420 	 * IRE_MARK_RCVD should not race with ire_delete marking
15421 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15422 	 * be consistent with other parts of the code that walks
15423 	 * a given bucket.
15424 	 */
15425 	save_ire = ire;
15426 	irb = ire->ire_bucket;
15427 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15428 	if (new_lb_ire == NULL) {
15429 		ire_refrele(ire);
15430 		return;
15431 	}
15432 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15433 	if (new_nlb_ire == NULL) {
15434 		ire_refrele(ire);
15435 		kmem_cache_free(ire_cache, new_lb_ire);
15436 		return;
15437 	}
15438 	IRB_REFHOLD(irb);
15439 	rw_enter(&irb->irb_lock, RW_WRITER);
15440 	/*
15441 	 * Get to the first ire matching the address and the
15442 	 * group. If the address does not match we are done
15443 	 * as we could not find the IRE. If the address matches
15444 	 * we should get to the first one matching the group.
15445 	 */
15446 	while (ire != NULL) {
15447 		if (ire->ire_addr != addr ||
15448 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15449 			break;
15450 		}
15451 		ire = ire->ire_next;
15452 	}
15453 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15454 	start_ire = ire;
15455 redo:
15456 	while (ire != NULL && ire->ire_addr == addr &&
15457 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15458 		/*
15459 		 * The first ire for any address within a group
15460 		 * should always be the one with IRE_MARK_NORECV cleared
15461 		 * so that ip_wput_ire can avoid searching for one.
15462 		 * Note down the insertion point which will be used
15463 		 * later.
15464 		 */
15465 		if (first && (irep == NULL))
15466 			irep = ire->ire_ptpn;
15467 		/*
15468 		 * PHYI_FAILED is set when the interface fails.
15469 		 * This interface might have become good, but the
15470 		 * daemon has not yet detected. We should still
15471 		 * not receive on this. PHYI_OFFLINE should never
15472 		 * be picked as this has been offlined and soon
15473 		 * be removed.
15474 		 */
15475 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15476 		if (phyi_flags & PHYI_OFFLINE) {
15477 			ire->ire_marks |= IRE_MARK_NORECV;
15478 			ire = ire->ire_next;
15479 			continue;
15480 		}
15481 		if (phyi_flags & match_flags) {
15482 			ire->ire_marks |= IRE_MARK_NORECV;
15483 			ire = ire->ire_next;
15484 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15485 			    PHYI_INACTIVE) {
15486 				fallback = B_TRUE;
15487 			}
15488 			continue;
15489 		}
15490 		if (first) {
15491 			/*
15492 			 * We will move this to the front of the list later
15493 			 * on.
15494 			 */
15495 			clear_ire = ire;
15496 			ire->ire_marks &= ~IRE_MARK_NORECV;
15497 		} else {
15498 			ire->ire_marks |= IRE_MARK_NORECV;
15499 		}
15500 		first = B_FALSE;
15501 		ire = ire->ire_next;
15502 	}
15503 	/*
15504 	 * If we never nominated anybody, try nominating at least
15505 	 * an INACTIVE, if we found one. Do it only once though.
15506 	 */
15507 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15508 	    fallback) {
15509 		match_flags = PHYI_FAILED;
15510 		ire = start_ire;
15511 		irep = NULL;
15512 		goto redo;
15513 	}
15514 	ire_refrele(save_ire);
15515 
15516 	/*
15517 	 * irep non-NULL indicates that we entered the while loop
15518 	 * above. If clear_ire is at the insertion point, we don't
15519 	 * have to do anything. clear_ire will be NULL if all the
15520 	 * interfaces are failed.
15521 	 *
15522 	 * We cannot unlink and reinsert the ire at the right place
15523 	 * in the list since there can be other walkers of this bucket.
15524 	 * Instead we delete and recreate the ire
15525 	 */
15526 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15527 		ire_t *clear_ire_stq = NULL;
15528 
15529 		bzero(new_lb_ire, sizeof (ire_t));
15530 		/* XXX We need a recovery strategy here. */
15531 		if (ire_init(new_lb_ire,
15532 		    (uchar_t *)&clear_ire->ire_addr,
15533 		    (uchar_t *)&clear_ire->ire_mask,
15534 		    (uchar_t *)&clear_ire->ire_src_addr,
15535 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15536 		    (uchar_t *)&clear_ire->ire_in_src_addr,
15537 		    &clear_ire->ire_max_frag,
15538 		    NULL, /* let ire_nce_init derive the resolver info */
15539 		    clear_ire->ire_rfq,
15540 		    clear_ire->ire_stq,
15541 		    clear_ire->ire_type,
15542 		    clear_ire->ire_ipif,
15543 		    clear_ire->ire_in_ill,
15544 		    clear_ire->ire_cmask,
15545 		    clear_ire->ire_phandle,
15546 		    clear_ire->ire_ihandle,
15547 		    clear_ire->ire_flags,
15548 		    &clear_ire->ire_uinfo,
15549 		    NULL,
15550 		    NULL,
15551 		    ipst) == NULL)
15552 			cmn_err(CE_PANIC, "ire_init() failed");
15553 		if (clear_ire->ire_stq == NULL) {
15554 			ire_t *ire_next = clear_ire->ire_next;
15555 			if (ire_next != NULL &&
15556 			    ire_next->ire_stq != NULL &&
15557 			    ire_next->ire_addr == clear_ire->ire_addr &&
15558 			    ire_next->ire_ipif->ipif_ill ==
15559 			    clear_ire->ire_ipif->ipif_ill) {
15560 				clear_ire_stq = ire_next;
15561 
15562 				bzero(new_nlb_ire, sizeof (ire_t));
15563 				/* XXX We need a recovery strategy here. */
15564 				if (ire_init(new_nlb_ire,
15565 				    (uchar_t *)&clear_ire_stq->ire_addr,
15566 				    (uchar_t *)&clear_ire_stq->ire_mask,
15567 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15568 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15569 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
15570 				    &clear_ire_stq->ire_max_frag,
15571 				    NULL,
15572 				    clear_ire_stq->ire_rfq,
15573 				    clear_ire_stq->ire_stq,
15574 				    clear_ire_stq->ire_type,
15575 				    clear_ire_stq->ire_ipif,
15576 				    clear_ire_stq->ire_in_ill,
15577 				    clear_ire_stq->ire_cmask,
15578 				    clear_ire_stq->ire_phandle,
15579 				    clear_ire_stq->ire_ihandle,
15580 				    clear_ire_stq->ire_flags,
15581 				    &clear_ire_stq->ire_uinfo,
15582 				    NULL,
15583 				    NULL,
15584 				    ipst) == NULL)
15585 					cmn_err(CE_PANIC, "ire_init() failed");
15586 			}
15587 		}
15588 
15589 		/*
15590 		 * Delete the ire. We can't call ire_delete() since
15591 		 * we are holding the bucket lock. We can't release the
15592 		 * bucket lock since we can't allow irep to change. So just
15593 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15594 		 * ire from the list and do the refrele.
15595 		 */
15596 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15597 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15598 
15599 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15600 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15601 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15602 		}
15603 
15604 		/*
15605 		 * Also take care of otherfields like ib/ob pkt count
15606 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15607 		 */
15608 
15609 		/* Set the max_frag before adding the ire */
15610 		max_frag = *new_lb_ire->ire_max_fragp;
15611 		new_lb_ire->ire_max_fragp = NULL;
15612 		new_lb_ire->ire_max_frag = max_frag;
15613 
15614 		/* Add the new ire's. Insert at *irep */
15615 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15616 		ire1 = *irep;
15617 		if (ire1 != NULL)
15618 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15619 		new_lb_ire->ire_next = ire1;
15620 		/* Link the new one in. */
15621 		new_lb_ire->ire_ptpn = irep;
15622 		membar_producer();
15623 		*irep = new_lb_ire;
15624 		new_lb_ire_used = B_TRUE;
15625 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15626 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15627 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15628 
15629 		if (clear_ire_stq != NULL) {
15630 			/* Set the max_frag before adding the ire */
15631 			max_frag = *new_nlb_ire->ire_max_fragp;
15632 			new_nlb_ire->ire_max_fragp = NULL;
15633 			new_nlb_ire->ire_max_frag = max_frag;
15634 
15635 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15636 			irep = &new_lb_ire->ire_next;
15637 			/* Add the new ire. Insert at *irep */
15638 			ire1 = *irep;
15639 			if (ire1 != NULL)
15640 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15641 			new_nlb_ire->ire_next = ire1;
15642 			/* Link the new one in. */
15643 			new_nlb_ire->ire_ptpn = irep;
15644 			membar_producer();
15645 			*irep = new_nlb_ire;
15646 			new_nlb_ire_used = B_TRUE;
15647 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15648 			    ire_stats_inserted);
15649 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15650 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15651 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15652 		}
15653 	}
15654 	rw_exit(&irb->irb_lock);
15655 	if (!new_lb_ire_used)
15656 		kmem_cache_free(ire_cache, new_lb_ire);
15657 	if (!new_nlb_ire_used)
15658 		kmem_cache_free(ire_cache, new_nlb_ire);
15659 	IRB_REFRELE(irb);
15660 }
15661 
15662 /*
15663  * Whenever an ipif goes down we have to renominate a different
15664  * broadcast ire to receive. Whenever an ipif comes up, we need
15665  * to make sure that we have only one nominated to receive.
15666  */
15667 static void
15668 ipif_renominate_bcast(ipif_t *ipif)
15669 {
15670 	ill_t *ill = ipif->ipif_ill;
15671 	ipaddr_t subnet_addr;
15672 	ipaddr_t net_addr;
15673 	ipaddr_t net_mask = 0;
15674 	ipaddr_t subnet_netmask;
15675 	ipaddr_t addr;
15676 	ill_group_t *illgrp;
15677 	ip_stack_t	*ipst = ill->ill_ipst;
15678 
15679 	illgrp = ill->ill_group;
15680 	/*
15681 	 * If this is the last ipif going down, it might take
15682 	 * the ill out of the group. In that case ipif_down ->
15683 	 * illgrp_delete takes care of doing the nomination.
15684 	 * ipif_down does not call for this case.
15685 	 */
15686 	ASSERT(illgrp != NULL);
15687 
15688 	/* There could not have been any ires associated with this */
15689 	if (ipif->ipif_subnet == 0)
15690 		return;
15691 
15692 	ill_mark_bcast(illgrp, 0, ipst);
15693 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15694 
15695 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15696 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15697 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15698 	} else {
15699 		net_mask = htonl(IN_CLASSA_NET);
15700 	}
15701 	addr = net_mask & ipif->ipif_subnet;
15702 	ill_mark_bcast(illgrp, addr, ipst);
15703 
15704 	net_addr = ~net_mask | addr;
15705 	ill_mark_bcast(illgrp, net_addr, ipst);
15706 
15707 	subnet_netmask = ipif->ipif_net_mask;
15708 	addr = ipif->ipif_subnet;
15709 	ill_mark_bcast(illgrp, addr, ipst);
15710 
15711 	subnet_addr = ~subnet_netmask | addr;
15712 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15713 }
15714 
15715 /*
15716  * Whenever we form or delete ill groups, we need to nominate one set of
15717  * BROADCAST ires for receiving in the group.
15718  *
15719  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15720  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15721  *    for ill_ipif_up_count to be non-zero. This is the only case where
15722  *    ill_ipif_up_count is zero and we would still find the ires.
15723  *
15724  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15725  *    ipif is UP and we just have to do the nomination.
15726  *
15727  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15728  *    from the group. So, we have to do the nomination.
15729  *
15730  * Because of (3), there could be just one ill in the group. But we have
15731  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15732  * Thus, this function does not optimize when there is only one ill as
15733  * it is not correct for (3).
15734  */
15735 static void
15736 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15737 {
15738 	ill_t *ill;
15739 	ipif_t *ipif;
15740 	ipaddr_t subnet_addr;
15741 	ipaddr_t prev_subnet_addr = 0;
15742 	ipaddr_t net_addr;
15743 	ipaddr_t prev_net_addr = 0;
15744 	ipaddr_t net_mask = 0;
15745 	ipaddr_t subnet_netmask;
15746 	ipaddr_t addr;
15747 	ip_stack_t	*ipst;
15748 
15749 	/*
15750 	 * When the last memeber is leaving, there is nothing to
15751 	 * nominate.
15752 	 */
15753 	if (illgrp->illgrp_ill_count == 0) {
15754 		ASSERT(illgrp->illgrp_ill == NULL);
15755 		return;
15756 	}
15757 
15758 	ill = illgrp->illgrp_ill;
15759 	ASSERT(!ill->ill_isv6);
15760 	ipst = ill->ill_ipst;
15761 	/*
15762 	 * We assume that ires with same address and belonging to the
15763 	 * same group, has been grouped together. Nominating a *single*
15764 	 * ill in the group for sending and receiving broadcast is done
15765 	 * by making sure that the first BROADCAST ire (which will be
15766 	 * the one returned by ire_ctable_lookup for ip_rput and the
15767 	 * one that will be used in ip_wput_ire) will be the one that
15768 	 * will not have IRE_MARK_NORECV set.
15769 	 *
15770 	 * 1) ip_rput checks and discards packets received on ires marked
15771 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15772 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15773 	 *    first ire in the group for every broadcast address in the group.
15774 	 *    ip_rput will accept packets only on the first ire i.e only
15775 	 *    one copy of the ill.
15776 	 *
15777 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15778 	 *    packet for the whole group. It needs to send out on the ill
15779 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15780 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15781 	 *    the copy echoed back on other port where the ire is not marked
15782 	 *    with IRE_MARK_NORECV.
15783 	 *
15784 	 * Note that we just need to have the first IRE either loopback or
15785 	 * non-loopback (either of them may not exist if ire_create failed
15786 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15787 	 * always hit the first one and hence will always accept one copy.
15788 	 *
15789 	 * We have a broadcast ire per ill for all the unique prefixes
15790 	 * hosted on that ill. As we don't have a way of knowing the
15791 	 * unique prefixes on a given ill and hence in the whole group,
15792 	 * we just call ill_mark_bcast on all the prefixes that exist
15793 	 * in the group. For the common case of one prefix, the code
15794 	 * below optimizes by remebering the last address used for
15795 	 * markng. In the case of multiple prefixes, this will still
15796 	 * optimize depending the order of prefixes.
15797 	 *
15798 	 * The only unique address across the whole group is 0.0.0.0 and
15799 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15800 	 * the first ire in the bucket for receiving and disables the
15801 	 * others.
15802 	 */
15803 	ill_mark_bcast(illgrp, 0, ipst);
15804 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15805 	for (; ill != NULL; ill = ill->ill_group_next) {
15806 
15807 		for (ipif = ill->ill_ipif; ipif != NULL;
15808 		    ipif = ipif->ipif_next) {
15809 
15810 			if (!(ipif->ipif_flags & IPIF_UP) ||
15811 			    ipif->ipif_subnet == 0) {
15812 				continue;
15813 			}
15814 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15815 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15816 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15817 			} else {
15818 				net_mask = htonl(IN_CLASSA_NET);
15819 			}
15820 			addr = net_mask & ipif->ipif_subnet;
15821 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15822 				ill_mark_bcast(illgrp, addr, ipst);
15823 				net_addr = ~net_mask | addr;
15824 				ill_mark_bcast(illgrp, net_addr, ipst);
15825 			}
15826 			prev_net_addr = addr;
15827 
15828 			subnet_netmask = ipif->ipif_net_mask;
15829 			addr = ipif->ipif_subnet;
15830 			if (prev_subnet_addr == 0 ||
15831 			    prev_subnet_addr != addr) {
15832 				ill_mark_bcast(illgrp, addr, ipst);
15833 				subnet_addr = ~subnet_netmask | addr;
15834 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15835 			}
15836 			prev_subnet_addr = addr;
15837 		}
15838 	}
15839 }
15840 
15841 /*
15842  * This function is called while forming ill groups.
15843  *
15844  * Currently, we handle only allmulti groups. We want to join
15845  * allmulti on only one of the ills in the groups. In future,
15846  * when we have link aggregation, we may have to join normal
15847  * multicast groups on multiple ills as switch does inbound load
15848  * balancing. Following are the functions that calls this
15849  * function :
15850  *
15851  * 1) ill_recover_multicast : Interface is coming back UP.
15852  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15853  *    will call ill_recover_multicast to recover all the multicast
15854  *    groups. We need to make sure that only one member is joined
15855  *    in the ill group.
15856  *
15857  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15858  *    Somebody is joining allmulti. We need to make sure that only one
15859  *    member is joined in the group.
15860  *
15861  * 3) illgrp_insert : If allmulti has already joined, we need to make
15862  *    sure that only one member is joined in the group.
15863  *
15864  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15865  *    allmulti who we have nominated. We need to pick someother ill.
15866  *
15867  * 5) illgrp_delete : The ill we nominated is leaving the group,
15868  *    we need to pick a new ill to join the group.
15869  *
15870  * For (1), (2), (5) - we just have to check whether there is
15871  * a good ill joined in the group. If we could not find any ills
15872  * joined the group, we should join.
15873  *
15874  * For (4), the one that was nominated to receive, left the group.
15875  * There could be nobody joined in the group when this function is
15876  * called.
15877  *
15878  * For (3) - we need to explicitly check whether there are multiple
15879  * ills joined in the group.
15880  *
15881  * For simplicity, we don't differentiate any of the above cases. We
15882  * just leave the group if it is joined on any of them and join on
15883  * the first good ill.
15884  */
15885 int
15886 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15887 {
15888 	ilm_t *ilm;
15889 	ill_t *ill;
15890 	ill_t *fallback_inactive_ill = NULL;
15891 	ill_t *fallback_failed_ill = NULL;
15892 	int ret = 0;
15893 
15894 	/*
15895 	 * Leave the allmulti on all the ills and start fresh.
15896 	 */
15897 	for (ill = illgrp->illgrp_ill; ill != NULL;
15898 	    ill = ill->ill_group_next) {
15899 		if (ill->ill_join_allmulti)
15900 			(void) ip_leave_allmulti(ill->ill_ipif);
15901 	}
15902 
15903 	/*
15904 	 * Choose a good ill. Fallback to inactive or failed if
15905 	 * none available. We need to fallback to FAILED in the
15906 	 * case where we have 2 interfaces in a group - where
15907 	 * one of them is failed and another is a good one and
15908 	 * the good one (not marked inactive) is leaving the group.
15909 	 */
15910 	ret = 0;
15911 	for (ill = illgrp->illgrp_ill; ill != NULL;
15912 	    ill = ill->ill_group_next) {
15913 		/* Never pick an offline interface */
15914 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15915 			continue;
15916 
15917 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15918 			fallback_failed_ill = ill;
15919 			continue;
15920 		}
15921 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15922 			fallback_inactive_ill = ill;
15923 			continue;
15924 		}
15925 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15926 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15927 				ret = ip_join_allmulti(ill->ill_ipif);
15928 				/*
15929 				 * ip_join_allmulti can fail because of memory
15930 				 * failures. So, make sure we join at least
15931 				 * on one ill.
15932 				 */
15933 				if (ill->ill_join_allmulti)
15934 					return (0);
15935 			}
15936 		}
15937 	}
15938 	if (ret != 0) {
15939 		/*
15940 		 * If we tried nominating above and failed to do so,
15941 		 * return error. We might have tried multiple times.
15942 		 * But, return the latest error.
15943 		 */
15944 		return (ret);
15945 	}
15946 	if ((ill = fallback_inactive_ill) != NULL) {
15947 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15948 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15949 				ret = ip_join_allmulti(ill->ill_ipif);
15950 				return (ret);
15951 			}
15952 		}
15953 	} else if ((ill = fallback_failed_ill) != NULL) {
15954 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15955 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15956 				ret = ip_join_allmulti(ill->ill_ipif);
15957 				return (ret);
15958 			}
15959 		}
15960 	}
15961 	return (0);
15962 }
15963 
15964 /*
15965  * This function is called from illgrp_delete after it is
15966  * deleted from the group to reschedule responsibilities
15967  * to a different ill.
15968  */
15969 static void
15970 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15971 {
15972 	ilm_t	*ilm;
15973 	ipif_t	*ipif;
15974 	ipaddr_t subnet_addr;
15975 	ipaddr_t net_addr;
15976 	ipaddr_t net_mask = 0;
15977 	ipaddr_t subnet_netmask;
15978 	ipaddr_t addr;
15979 	ip_stack_t *ipst = ill->ill_ipst;
15980 
15981 	ASSERT(ill->ill_group == NULL);
15982 	/*
15983 	 * Broadcast Responsibility:
15984 	 *
15985 	 * 1. If this ill has been nominated for receiving broadcast
15986 	 * packets, we need to find a new one. Before we find a new
15987 	 * one, we need to re-group the ires that are part of this new
15988 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15989 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15990 	 * thing for us.
15991 	 *
15992 	 * 2. If this ill was not nominated for receiving broadcast
15993 	 * packets, we need to clear the IRE_MARK_NORECV flag
15994 	 * so that we continue to send up broadcast packets.
15995 	 */
15996 	if (!ill->ill_isv6) {
15997 		/*
15998 		 * Case 1 above : No optimization here. Just redo the
15999 		 * nomination.
16000 		 */
16001 		ill_group_bcast_for_xmit(ill);
16002 		ill_nominate_bcast_rcv(illgrp);
16003 
16004 		/*
16005 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
16006 		 */
16007 		ill_clear_bcast_mark(ill, 0);
16008 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
16009 
16010 		for (ipif = ill->ill_ipif; ipif != NULL;
16011 		    ipif = ipif->ipif_next) {
16012 
16013 			if (!(ipif->ipif_flags & IPIF_UP) ||
16014 			    ipif->ipif_subnet == 0) {
16015 				continue;
16016 			}
16017 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16018 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16019 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
16020 			} else {
16021 				net_mask = htonl(IN_CLASSA_NET);
16022 			}
16023 			addr = net_mask & ipif->ipif_subnet;
16024 			ill_clear_bcast_mark(ill, addr);
16025 
16026 			net_addr = ~net_mask | addr;
16027 			ill_clear_bcast_mark(ill, net_addr);
16028 
16029 			subnet_netmask = ipif->ipif_net_mask;
16030 			addr = ipif->ipif_subnet;
16031 			ill_clear_bcast_mark(ill, addr);
16032 
16033 			subnet_addr = ~subnet_netmask | addr;
16034 			ill_clear_bcast_mark(ill, subnet_addr);
16035 		}
16036 	}
16037 
16038 	/*
16039 	 * Multicast Responsibility.
16040 	 *
16041 	 * If we have joined allmulti on this one, find a new member
16042 	 * in the group to join allmulti. As this ill is already part
16043 	 * of allmulti, we don't have to join on this one.
16044 	 *
16045 	 * If we have not joined allmulti on this one, there is no
16046 	 * responsibility to handoff. But we need to take new
16047 	 * responsibility i.e, join allmulti on this one if we need
16048 	 * to.
16049 	 */
16050 	if (ill->ill_join_allmulti) {
16051 		(void) ill_nominate_mcast_rcv(illgrp);
16052 	} else {
16053 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16054 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16055 				(void) ip_join_allmulti(ill->ill_ipif);
16056 				break;
16057 			}
16058 		}
16059 	}
16060 
16061 	/*
16062 	 * We intentionally do the flushing of IRE_CACHES only matching
16063 	 * on the ill and not on groups. Note that we are already deleted
16064 	 * from the group.
16065 	 *
16066 	 * This will make sure that all IRE_CACHES whose stq is pointing
16067 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
16068 	 * deleted and IRE_CACHES that are not pointing at this ill will
16069 	 * be left alone.
16070 	 */
16071 	if (ill->ill_isv6) {
16072 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16073 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
16074 	} else {
16075 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16076 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
16077 	}
16078 
16079 	/*
16080 	 * Some conn may have cached one of the IREs deleted above. By removing
16081 	 * the ire reference, we clean up the extra reference to the ill held in
16082 	 * ire->ire_stq.
16083 	 */
16084 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
16085 
16086 	/*
16087 	 * Re-do source address selection for all the members in the
16088 	 * group, if they borrowed source address from one of the ipifs
16089 	 * in this ill.
16090 	 */
16091 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
16092 		if (ill->ill_isv6) {
16093 			ipif_update_other_ipifs_v6(ipif, illgrp);
16094 		} else {
16095 			ipif_update_other_ipifs(ipif, illgrp);
16096 		}
16097 	}
16098 }
16099 
16100 /*
16101  * Delete the ill from the group. The caller makes sure that it is
16102  * in a group and it okay to delete from the group. So, we always
16103  * delete here.
16104  */
16105 static void
16106 illgrp_delete(ill_t *ill)
16107 {
16108 	ill_group_t *illgrp;
16109 	ill_group_t *tmpg;
16110 	ill_t *tmp_ill;
16111 	ip_stack_t	*ipst = ill->ill_ipst;
16112 
16113 	/*
16114 	 * Reset illgrp_ill_schednext if it was pointing at us.
16115 	 * We need to do this before we set ill_group to NULL.
16116 	 */
16117 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16118 	mutex_enter(&ill->ill_lock);
16119 
16120 	illgrp_reset_schednext(ill);
16121 
16122 	illgrp = ill->ill_group;
16123 
16124 	/* Delete the ill from illgrp. */
16125 	if (illgrp->illgrp_ill == ill) {
16126 		illgrp->illgrp_ill = ill->ill_group_next;
16127 	} else {
16128 		tmp_ill = illgrp->illgrp_ill;
16129 		while (tmp_ill->ill_group_next != ill) {
16130 			tmp_ill = tmp_ill->ill_group_next;
16131 			ASSERT(tmp_ill != NULL);
16132 		}
16133 		tmp_ill->ill_group_next = ill->ill_group_next;
16134 	}
16135 	ill->ill_group = NULL;
16136 	ill->ill_group_next = NULL;
16137 
16138 	illgrp->illgrp_ill_count--;
16139 	mutex_exit(&ill->ill_lock);
16140 	rw_exit(&ipst->ips_ill_g_lock);
16141 
16142 	/*
16143 	 * As this ill is leaving the group, we need to hand off
16144 	 * the responsibilities to the other ills in the group, if
16145 	 * this ill had some responsibilities.
16146 	 */
16147 
16148 	ill_handoff_responsibility(ill, illgrp);
16149 
16150 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16151 
16152 	if (illgrp->illgrp_ill_count == 0) {
16153 
16154 		ASSERT(illgrp->illgrp_ill == NULL);
16155 		if (ill->ill_isv6) {
16156 			if (illgrp == ipst->ips_illgrp_head_v6) {
16157 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
16158 			} else {
16159 				tmpg = ipst->ips_illgrp_head_v6;
16160 				while (tmpg->illgrp_next != illgrp) {
16161 					tmpg = tmpg->illgrp_next;
16162 					ASSERT(tmpg != NULL);
16163 				}
16164 				tmpg->illgrp_next = illgrp->illgrp_next;
16165 			}
16166 		} else {
16167 			if (illgrp == ipst->ips_illgrp_head_v4) {
16168 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
16169 			} else {
16170 				tmpg = ipst->ips_illgrp_head_v4;
16171 				while (tmpg->illgrp_next != illgrp) {
16172 					tmpg = tmpg->illgrp_next;
16173 					ASSERT(tmpg != NULL);
16174 				}
16175 				tmpg->illgrp_next = illgrp->illgrp_next;
16176 			}
16177 		}
16178 		mutex_destroy(&illgrp->illgrp_lock);
16179 		mi_free(illgrp);
16180 	}
16181 	rw_exit(&ipst->ips_ill_g_lock);
16182 
16183 	/*
16184 	 * Even though the ill is out of the group its not necessary
16185 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
16186 	 * We will split the ipsq when phyint_groupname is set to NULL.
16187 	 */
16188 
16189 	/*
16190 	 * Send a routing sockets message if we are deleting from
16191 	 * groups with names.
16192 	 */
16193 	if (ill->ill_phyint->phyint_groupname_len != 0)
16194 		ip_rts_ifmsg(ill->ill_ipif);
16195 }
16196 
16197 /*
16198  * Re-do source address selection. This is normally called when
16199  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
16200  * ipif comes up.
16201  */
16202 void
16203 ill_update_source_selection(ill_t *ill)
16204 {
16205 	ipif_t *ipif;
16206 
16207 	ASSERT(IAM_WRITER_ILL(ill));
16208 
16209 	if (ill->ill_group != NULL)
16210 		ill = ill->ill_group->illgrp_ill;
16211 
16212 	for (; ill != NULL; ill = ill->ill_group_next) {
16213 		for (ipif = ill->ill_ipif; ipif != NULL;
16214 		    ipif = ipif->ipif_next) {
16215 			if (ill->ill_isv6)
16216 				ipif_recreate_interface_routes_v6(NULL, ipif);
16217 			else
16218 				ipif_recreate_interface_routes(NULL, ipif);
16219 		}
16220 	}
16221 }
16222 
16223 /*
16224  * Insert ill in a group headed by illgrp_head. The caller can either
16225  * pass a groupname in which case we search for a group with the
16226  * same name to insert in or pass a group to insert in. This function
16227  * would only search groups with names.
16228  *
16229  * NOTE : The caller should make sure that there is at least one ipif
16230  *	  UP on this ill so that illgrp_scheduler can pick this ill
16231  *	  for outbound packets. If ill_ipif_up_count is zero, we have
16232  *	  already sent a DL_UNBIND to the driver and we don't want to
16233  *	  send anymore packets. We don't assert for ipif_up_count
16234  *	  to be greater than zero, because ipif_up_done wants to call
16235  *	  this function before bumping up the ipif_up_count. See
16236  *	  ipif_up_done() for details.
16237  */
16238 int
16239 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
16240     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
16241 {
16242 	ill_group_t *illgrp;
16243 	ill_t *prev_ill;
16244 	phyint_t *phyi;
16245 	ip_stack_t	*ipst = ill->ill_ipst;
16246 
16247 	ASSERT(ill->ill_group == NULL);
16248 
16249 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16250 	mutex_enter(&ill->ill_lock);
16251 
16252 	if (groupname != NULL) {
16253 		/*
16254 		 * Look for a group with a matching groupname to insert.
16255 		 */
16256 		for (illgrp = *illgrp_head; illgrp != NULL;
16257 		    illgrp = illgrp->illgrp_next) {
16258 
16259 			ill_t *tmp_ill;
16260 
16261 			/*
16262 			 * If we have an ill_group_t in the list which has
16263 			 * no ill_t assigned then we must be in the process of
16264 			 * removing this group. We skip this as illgrp_delete()
16265 			 * will remove it from the list.
16266 			 */
16267 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
16268 				ASSERT(illgrp->illgrp_ill_count == 0);
16269 				continue;
16270 			}
16271 
16272 			ASSERT(tmp_ill->ill_phyint != NULL);
16273 			phyi = tmp_ill->ill_phyint;
16274 			/*
16275 			 * Look at groups which has names only.
16276 			 */
16277 			if (phyi->phyint_groupname_len == 0)
16278 				continue;
16279 			/*
16280 			 * Names are stored in the phyint common to both
16281 			 * IPv4 and IPv6.
16282 			 */
16283 			if (mi_strcmp(phyi->phyint_groupname,
16284 			    groupname) == 0) {
16285 				break;
16286 			}
16287 		}
16288 	} else {
16289 		/*
16290 		 * If the caller passes in a NULL "grp_to_insert", we
16291 		 * allocate one below and insert this singleton.
16292 		 */
16293 		illgrp = grp_to_insert;
16294 	}
16295 
16296 	ill->ill_group_next = NULL;
16297 
16298 	if (illgrp == NULL) {
16299 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16300 		if (illgrp == NULL) {
16301 			return (ENOMEM);
16302 		}
16303 		illgrp->illgrp_next = *illgrp_head;
16304 		*illgrp_head = illgrp;
16305 		illgrp->illgrp_ill = ill;
16306 		illgrp->illgrp_ill_count = 1;
16307 		ill->ill_group = illgrp;
16308 		/*
16309 		 * Used in illgrp_scheduler to protect multiple threads
16310 		 * from traversing the list.
16311 		 */
16312 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16313 	} else {
16314 		ASSERT(ill->ill_net_type ==
16315 		    illgrp->illgrp_ill->ill_net_type);
16316 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16317 
16318 		/* Insert ill at tail of this group */
16319 		prev_ill = illgrp->illgrp_ill;
16320 		while (prev_ill->ill_group_next != NULL)
16321 			prev_ill = prev_ill->ill_group_next;
16322 		prev_ill->ill_group_next = ill;
16323 		ill->ill_group = illgrp;
16324 		illgrp->illgrp_ill_count++;
16325 		/*
16326 		 * Inherit group properties. Currently only forwarding
16327 		 * is the property we try to keep the same with all the
16328 		 * ills. When there are more, we will abstract this into
16329 		 * a function.
16330 		 */
16331 		ill->ill_flags &= ~ILLF_ROUTER;
16332 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16333 	}
16334 	mutex_exit(&ill->ill_lock);
16335 	rw_exit(&ipst->ips_ill_g_lock);
16336 
16337 	/*
16338 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16339 	 *    may be zero as it has not yet been bumped. But the ires
16340 	 *    have already been added. So, we do the nomination here
16341 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16342 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16343 	 *    ill_ipif_up_count here while nominating broadcast ires for
16344 	 *    receive.
16345 	 *
16346 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16347 	 *    to group them properly as ire_add() has already happened
16348 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16349 	 *    case, we need to do it here anyway.
16350 	 */
16351 	if (!ill->ill_isv6) {
16352 		ill_group_bcast_for_xmit(ill);
16353 		ill_nominate_bcast_rcv(illgrp);
16354 	}
16355 
16356 	if (!ipif_is_coming_up) {
16357 		/*
16358 		 * When ipif_up_done() calls this function, the multicast
16359 		 * groups have not been joined yet. So, there is no point in
16360 		 * nomination. ip_join_allmulti will handle groups when
16361 		 * ill_recover_multicast is called from ipif_up_done() later.
16362 		 */
16363 		(void) ill_nominate_mcast_rcv(illgrp);
16364 		/*
16365 		 * ipif_up_done calls ill_update_source_selection
16366 		 * anyway. Moreover, we don't want to re-create
16367 		 * interface routes while ipif_up_done() still has reference
16368 		 * to them. Refer to ipif_up_done() for more details.
16369 		 */
16370 		ill_update_source_selection(ill);
16371 	}
16372 
16373 	/*
16374 	 * Send a routing sockets message if we are inserting into
16375 	 * groups with names.
16376 	 */
16377 	if (groupname != NULL)
16378 		ip_rts_ifmsg(ill->ill_ipif);
16379 	return (0);
16380 }
16381 
16382 /*
16383  * Return the first phyint matching the groupname. There could
16384  * be more than one when there are ill groups.
16385  *
16386  * If 'usable' is set, then we exclude ones that are marked with any of
16387  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16388  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16389  * emulation of ipmp.
16390  */
16391 phyint_t *
16392 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16393 {
16394 	phyint_t *phyi;
16395 
16396 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16397 	/*
16398 	 * Group names are stored in the phyint - a common structure
16399 	 * to both IPv4 and IPv6.
16400 	 */
16401 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16402 	for (; phyi != NULL;
16403 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16404 	    phyi, AVL_AFTER)) {
16405 		if (phyi->phyint_groupname_len == 0)
16406 			continue;
16407 		/*
16408 		 * Skip the ones that should not be used since the callers
16409 		 * sometime use this for sending packets.
16410 		 */
16411 		if (usable && (phyi->phyint_flags &
16412 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16413 			continue;
16414 
16415 		ASSERT(phyi->phyint_groupname != NULL);
16416 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16417 			return (phyi);
16418 	}
16419 	return (NULL);
16420 }
16421 
16422 
16423 /*
16424  * Return the first usable phyint matching the group index. By 'usable'
16425  * we exclude ones that are marked ununsable with any of
16426  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16427  *
16428  * Used only for the ipmp/netinfo emulation of ipmp.
16429  */
16430 phyint_t *
16431 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16432 {
16433 	phyint_t *phyi;
16434 
16435 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16436 
16437 	if (!ipst->ips_ipmp_hook_emulation)
16438 		return (NULL);
16439 
16440 	/*
16441 	 * Group indicies are stored in the phyint - a common structure
16442 	 * to both IPv4 and IPv6.
16443 	 */
16444 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16445 	for (; phyi != NULL;
16446 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16447 	    phyi, AVL_AFTER)) {
16448 		/* Ignore the ones that do not have a group */
16449 		if (phyi->phyint_groupname_len == 0)
16450 			continue;
16451 
16452 		ASSERT(phyi->phyint_group_ifindex != 0);
16453 		/*
16454 		 * Skip the ones that should not be used since the callers
16455 		 * sometime use this for sending packets.
16456 		 */
16457 		if (phyi->phyint_flags &
16458 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16459 			continue;
16460 		if (phyi->phyint_group_ifindex == group_ifindex)
16461 			return (phyi);
16462 	}
16463 	return (NULL);
16464 }
16465 
16466 
16467 /*
16468  * MT notes on creation and deletion of IPMP groups
16469  *
16470  * Creation and deletion of IPMP groups introduce the need to merge or
16471  * split the associated serialization objects i.e the ipsq's. Normally all
16472  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16473  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16474  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16475  * is a need to change the <ill-ipsq> association and we have to operate on both
16476  * the source and destination IPMP groups. For eg. attempting to set the
16477  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16478  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16479  * source or destination IPMP group are mapped to a single ipsq for executing
16480  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16481  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16482  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16483  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16484  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16485  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16486  *
16487  * In the above example the ioctl handling code locates the current ipsq of hme0
16488  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16489  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16490  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16491  * the destination ipsq. If the destination ipsq is not busy, it also enters
16492  * the destination ipsq exclusively. Now the actual groupname setting operation
16493  * can proceed. If the destination ipsq is busy, the operation is enqueued
16494  * on the destination (merged) ipsq and will be handled in the unwind from
16495  * ipsq_exit.
16496  *
16497  * To prevent other threads accessing the ill while the group name change is
16498  * in progres, we bring down the ipifs which also removes the ill from the
16499  * group. The group is changed in phyint and when the first ipif on the ill
16500  * is brought up, the ill is inserted into the right IPMP group by
16501  * illgrp_insert.
16502  */
16503 /* ARGSUSED */
16504 int
16505 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16506     ip_ioctl_cmd_t *ipip, void *ifreq)
16507 {
16508 	int i;
16509 	char *tmp;
16510 	int namelen;
16511 	ill_t *ill = ipif->ipif_ill;
16512 	ill_t *ill_v4, *ill_v6;
16513 	int err = 0;
16514 	phyint_t *phyi;
16515 	phyint_t *phyi_tmp;
16516 	struct lifreq *lifr;
16517 	mblk_t	*mp1;
16518 	char *groupname;
16519 	ipsq_t *ipsq;
16520 	ip_stack_t	*ipst = ill->ill_ipst;
16521 
16522 	ASSERT(IAM_WRITER_IPIF(ipif));
16523 
16524 	/* Existance verified in ip_wput_nondata */
16525 	mp1 = mp->b_cont->b_cont;
16526 	lifr = (struct lifreq *)mp1->b_rptr;
16527 	groupname = lifr->lifr_groupname;
16528 
16529 	if (ipif->ipif_id != 0)
16530 		return (EINVAL);
16531 
16532 	phyi = ill->ill_phyint;
16533 	ASSERT(phyi != NULL);
16534 
16535 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16536 		return (EINVAL);
16537 
16538 	tmp = groupname;
16539 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16540 		;
16541 
16542 	if (i == LIFNAMSIZ) {
16543 		/* no null termination */
16544 		return (EINVAL);
16545 	}
16546 
16547 	/*
16548 	 * Calculate the namelen exclusive of the null
16549 	 * termination character.
16550 	 */
16551 	namelen = tmp - groupname;
16552 
16553 	ill_v4 = phyi->phyint_illv4;
16554 	ill_v6 = phyi->phyint_illv6;
16555 
16556 	/*
16557 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16558 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16559 	 * synchronization notes in ip.c
16560 	 */
16561 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16562 		return (EINVAL);
16563 	}
16564 
16565 	/*
16566 	 * mark the ill as changing.
16567 	 * this should queue all new requests on the syncq.
16568 	 */
16569 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16570 
16571 	if (ill_v4 != NULL)
16572 		ill_v4->ill_state_flags |= ILL_CHANGING;
16573 	if (ill_v6 != NULL)
16574 		ill_v6->ill_state_flags |= ILL_CHANGING;
16575 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16576 
16577 	if (namelen == 0) {
16578 		/*
16579 		 * Null string means remove this interface from the
16580 		 * existing group.
16581 		 */
16582 		if (phyi->phyint_groupname_len == 0) {
16583 			/*
16584 			 * Never was in a group.
16585 			 */
16586 			err = 0;
16587 			goto done;
16588 		}
16589 
16590 		/*
16591 		 * IPv4 or IPv6 may be temporarily out of the group when all
16592 		 * the ipifs are down. Thus, we need to check for ill_group to
16593 		 * be non-NULL.
16594 		 */
16595 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16596 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16597 			mutex_enter(&ill_v4->ill_lock);
16598 			if (!ill_is_quiescent(ill_v4)) {
16599 				/*
16600 				 * ipsq_pending_mp_add will not fail since
16601 				 * connp is NULL
16602 				 */
16603 				(void) ipsq_pending_mp_add(NULL,
16604 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16605 				mutex_exit(&ill_v4->ill_lock);
16606 				err = EINPROGRESS;
16607 				goto done;
16608 			}
16609 			mutex_exit(&ill_v4->ill_lock);
16610 		}
16611 
16612 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16613 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16614 			mutex_enter(&ill_v6->ill_lock);
16615 			if (!ill_is_quiescent(ill_v6)) {
16616 				(void) ipsq_pending_mp_add(NULL,
16617 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16618 				mutex_exit(&ill_v6->ill_lock);
16619 				err = EINPROGRESS;
16620 				goto done;
16621 			}
16622 			mutex_exit(&ill_v6->ill_lock);
16623 		}
16624 
16625 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16626 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16627 		mutex_enter(&phyi->phyint_lock);
16628 		ASSERT(phyi->phyint_groupname != NULL);
16629 		mi_free(phyi->phyint_groupname);
16630 		phyi->phyint_groupname = NULL;
16631 		phyi->phyint_groupname_len = 0;
16632 
16633 		/* Restore the ifindex used to be the per interface one */
16634 		phyi->phyint_group_ifindex = 0;
16635 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16636 		mutex_exit(&phyi->phyint_lock);
16637 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16638 		rw_exit(&ipst->ips_ill_g_lock);
16639 		err = ill_up_ipifs(ill, q, mp);
16640 
16641 		/*
16642 		 * set the split flag so that the ipsq can be split
16643 		 */
16644 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16645 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16646 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16647 
16648 	} else {
16649 		if (phyi->phyint_groupname_len != 0) {
16650 			ASSERT(phyi->phyint_groupname != NULL);
16651 			/* Are we inserting in the same group ? */
16652 			if (mi_strcmp(groupname,
16653 			    phyi->phyint_groupname) == 0) {
16654 				err = 0;
16655 				goto done;
16656 			}
16657 		}
16658 
16659 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16660 		/*
16661 		 * Merge ipsq for the group's.
16662 		 * This check is here as multiple groups/ills might be
16663 		 * sharing the same ipsq.
16664 		 * If we have to merege than the operation is restarted
16665 		 * on the new ipsq.
16666 		 */
16667 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16668 		if (phyi->phyint_ipsq != ipsq) {
16669 			rw_exit(&ipst->ips_ill_g_lock);
16670 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16671 			goto done;
16672 		}
16673 		/*
16674 		 * Running exclusive on new ipsq.
16675 		 */
16676 
16677 		ASSERT(ipsq != NULL);
16678 		ASSERT(ipsq->ipsq_writer == curthread);
16679 
16680 		/*
16681 		 * Check whether the ill_type and ill_net_type matches before
16682 		 * we allocate any memory so that the cleanup is easier.
16683 		 *
16684 		 * We can't group dissimilar ones as we can't load spread
16685 		 * packets across the group because of potential link-level
16686 		 * header differences.
16687 		 */
16688 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16689 		if (phyi_tmp != NULL) {
16690 			if ((ill_v4 != NULL &&
16691 			    phyi_tmp->phyint_illv4 != NULL) &&
16692 			    ((ill_v4->ill_net_type !=
16693 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16694 			    (ill_v4->ill_type !=
16695 			    phyi_tmp->phyint_illv4->ill_type))) {
16696 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16697 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16698 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16699 				rw_exit(&ipst->ips_ill_g_lock);
16700 				return (EINVAL);
16701 			}
16702 			if ((ill_v6 != NULL &&
16703 			    phyi_tmp->phyint_illv6 != NULL) &&
16704 			    ((ill_v6->ill_net_type !=
16705 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16706 			    (ill_v6->ill_type !=
16707 			    phyi_tmp->phyint_illv6->ill_type))) {
16708 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16709 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16710 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16711 				rw_exit(&ipst->ips_ill_g_lock);
16712 				return (EINVAL);
16713 			}
16714 		}
16715 
16716 		rw_exit(&ipst->ips_ill_g_lock);
16717 
16718 		/*
16719 		 * bring down all v4 ipifs.
16720 		 */
16721 		if (ill_v4 != NULL) {
16722 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16723 		}
16724 
16725 		/*
16726 		 * bring down all v6 ipifs.
16727 		 */
16728 		if (ill_v6 != NULL) {
16729 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16730 		}
16731 
16732 		/*
16733 		 * make sure all ipifs are down and there are no active
16734 		 * references. Call to ipsq_pending_mp_add will not fail
16735 		 * since connp is NULL.
16736 		 */
16737 		if (ill_v4 != NULL) {
16738 			mutex_enter(&ill_v4->ill_lock);
16739 			if (!ill_is_quiescent(ill_v4)) {
16740 				(void) ipsq_pending_mp_add(NULL,
16741 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16742 				mutex_exit(&ill_v4->ill_lock);
16743 				err = EINPROGRESS;
16744 				goto done;
16745 			}
16746 			mutex_exit(&ill_v4->ill_lock);
16747 		}
16748 
16749 		if (ill_v6 != NULL) {
16750 			mutex_enter(&ill_v6->ill_lock);
16751 			if (!ill_is_quiescent(ill_v6)) {
16752 				(void) ipsq_pending_mp_add(NULL,
16753 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16754 				mutex_exit(&ill_v6->ill_lock);
16755 				err = EINPROGRESS;
16756 				goto done;
16757 			}
16758 			mutex_exit(&ill_v6->ill_lock);
16759 		}
16760 
16761 		/*
16762 		 * allocate including space for null terminator
16763 		 * before we insert.
16764 		 */
16765 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16766 		if (tmp == NULL)
16767 			return (ENOMEM);
16768 
16769 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16770 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16771 		mutex_enter(&phyi->phyint_lock);
16772 		if (phyi->phyint_groupname_len != 0) {
16773 			ASSERT(phyi->phyint_groupname != NULL);
16774 			mi_free(phyi->phyint_groupname);
16775 		}
16776 
16777 		/*
16778 		 * setup the new group name.
16779 		 */
16780 		phyi->phyint_groupname = tmp;
16781 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16782 		phyi->phyint_groupname_len = namelen + 1;
16783 
16784 		if (ipst->ips_ipmp_hook_emulation) {
16785 			/*
16786 			 * If the group already exists we use the existing
16787 			 * group_ifindex, otherwise we pick a new index here.
16788 			 */
16789 			if (phyi_tmp != NULL) {
16790 				phyi->phyint_group_ifindex =
16791 				    phyi_tmp->phyint_group_ifindex;
16792 			} else {
16793 				/* XXX We need a recovery strategy here. */
16794 				if (!ip_assign_ifindex(
16795 				    &phyi->phyint_group_ifindex, ipst))
16796 					cmn_err(CE_PANIC,
16797 					    "ip_assign_ifindex() failed");
16798 			}
16799 		}
16800 		/*
16801 		 * Select whether the netinfo and hook use the per-interface
16802 		 * or per-group ifindex.
16803 		 */
16804 		if (ipst->ips_ipmp_hook_emulation)
16805 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16806 		else
16807 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16808 
16809 		if (ipst->ips_ipmp_hook_emulation &&
16810 		    phyi_tmp != NULL) {
16811 			/* First phyint in group - group PLUMB event */
16812 			ill_nic_info_plumb(ill, B_TRUE);
16813 		}
16814 		mutex_exit(&phyi->phyint_lock);
16815 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16816 		rw_exit(&ipst->ips_ill_g_lock);
16817 
16818 		err = ill_up_ipifs(ill, q, mp);
16819 	}
16820 
16821 done:
16822 	/*
16823 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16824 	 */
16825 	if (err != EINPROGRESS) {
16826 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16827 		if (ill_v4 != NULL)
16828 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16829 		if (ill_v6 != NULL)
16830 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16831 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16832 	}
16833 	return (err);
16834 }
16835 
16836 /* ARGSUSED */
16837 int
16838 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16839     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16840 {
16841 	ill_t *ill;
16842 	phyint_t *phyi;
16843 	struct lifreq *lifr;
16844 	mblk_t	*mp1;
16845 
16846 	/* Existence verified in ip_wput_nondata */
16847 	mp1 = mp->b_cont->b_cont;
16848 	lifr = (struct lifreq *)mp1->b_rptr;
16849 	ill = ipif->ipif_ill;
16850 	phyi = ill->ill_phyint;
16851 
16852 	lifr->lifr_groupname[0] = '\0';
16853 	/*
16854 	 * ill_group may be null if all the interfaces
16855 	 * are down. But still, the phyint should always
16856 	 * hold the name.
16857 	 */
16858 	if (phyi->phyint_groupname_len != 0) {
16859 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16860 		    phyi->phyint_groupname_len);
16861 	}
16862 
16863 	return (0);
16864 }
16865 
16866 
16867 typedef struct conn_move_s {
16868 	ill_t	*cm_from_ill;
16869 	ill_t	*cm_to_ill;
16870 	int	cm_ifindex;
16871 } conn_move_t;
16872 
16873 /*
16874  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16875  */
16876 static void
16877 conn_move(conn_t *connp, caddr_t arg)
16878 {
16879 	conn_move_t *connm;
16880 	int ifindex;
16881 	int i;
16882 	ill_t *from_ill;
16883 	ill_t *to_ill;
16884 	ilg_t *ilg;
16885 	ilm_t *ret_ilm;
16886 
16887 	connm = (conn_move_t *)arg;
16888 	ifindex = connm->cm_ifindex;
16889 	from_ill = connm->cm_from_ill;
16890 	to_ill = connm->cm_to_ill;
16891 
16892 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16893 
16894 	/* All multicast fields protected by conn_lock */
16895 	mutex_enter(&connp->conn_lock);
16896 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16897 	if ((connp->conn_outgoing_ill == from_ill) &&
16898 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16899 		connp->conn_outgoing_ill = to_ill;
16900 		connp->conn_incoming_ill = to_ill;
16901 	}
16902 
16903 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16904 
16905 	if ((connp->conn_multicast_ill == from_ill) &&
16906 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16907 		connp->conn_multicast_ill = connm->cm_to_ill;
16908 	}
16909 
16910 	/* Change IP_XMIT_IF associations */
16911 	if ((connp->conn_xmit_if_ill == from_ill) &&
16912 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
16913 		connp->conn_xmit_if_ill = to_ill;
16914 	}
16915 	/*
16916 	 * Change the ilg_ill to point to the new one. This assumes
16917 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16918 	 * has been told to receive packets on this interface.
16919 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16920 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16921 	 * some ilms may not have moved. We check to see whether
16922 	 * the ilms have moved to to_ill. We can't check on from_ill
16923 	 * as in the process of moving, we could have split an ilm
16924 	 * in to two - which has the same orig_ifindex and v6group.
16925 	 *
16926 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16927 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16928 	 */
16929 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16930 		ilg = &connp->conn_ilg[i];
16931 		if ((ilg->ilg_ill == from_ill) &&
16932 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16933 			/* ifindex != 0 indicates failback */
16934 			if (ifindex != 0) {
16935 				connp->conn_ilg[i].ilg_ill = to_ill;
16936 				continue;
16937 			}
16938 
16939 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16940 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16941 			    connp->conn_zoneid);
16942 
16943 			if (ret_ilm != NULL)
16944 				connp->conn_ilg[i].ilg_ill = to_ill;
16945 		}
16946 	}
16947 	mutex_exit(&connp->conn_lock);
16948 }
16949 
16950 static void
16951 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16952 {
16953 	conn_move_t connm;
16954 	ip_stack_t	*ipst = from_ill->ill_ipst;
16955 
16956 	connm.cm_from_ill = from_ill;
16957 	connm.cm_to_ill = to_ill;
16958 	connm.cm_ifindex = ifindex;
16959 
16960 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16961 }
16962 
16963 /*
16964  * ilm has been moved from from_ill to to_ill.
16965  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16966  * appropriately.
16967  *
16968  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16969  *	  the code there de-references ipif_ill to get the ill to
16970  *	  send multicast requests. It does not work as ipif is on its
16971  *	  move and already moved when this function is called.
16972  *	  Thus, we need to use from_ill and to_ill send down multicast
16973  *	  requests.
16974  */
16975 static void
16976 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16977 {
16978 	ipif_t *ipif;
16979 	ilm_t *ilm;
16980 
16981 	/*
16982 	 * See whether we need to send down DL_ENABMULTI_REQ on
16983 	 * to_ill as ilm has just been added.
16984 	 */
16985 	ASSERT(IAM_WRITER_ILL(to_ill));
16986 	ASSERT(IAM_WRITER_ILL(from_ill));
16987 
16988 	ILM_WALKER_HOLD(to_ill);
16989 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16990 
16991 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16992 			continue;
16993 		/*
16994 		 * no locks held, ill/ipif cannot dissappear as long
16995 		 * as we are writer.
16996 		 */
16997 		ipif = to_ill->ill_ipif;
16998 		/*
16999 		 * No need to hold any lock as we are the writer and this
17000 		 * can only be changed by a writer.
17001 		 */
17002 		ilm->ilm_is_new = B_FALSE;
17003 
17004 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
17005 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
17006 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
17007 			    "resolver\n"));
17008 			continue;		/* Must be IRE_IF_NORESOLVER */
17009 		}
17010 
17011 
17012 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
17013 			ip1dbg(("ilm_send_multicast_reqs: "
17014 			    "to_ill MULTI_BCAST\n"));
17015 			goto from;
17016 		}
17017 
17018 		if (to_ill->ill_isv6)
17019 			mld_joingroup(ilm);
17020 		else
17021 			igmp_joingroup(ilm);
17022 
17023 		if (to_ill->ill_ipif_up_count == 0) {
17024 			/*
17025 			 * Nobody there. All multicast addresses will be
17026 			 * re-joined when we get the DL_BIND_ACK bringing the
17027 			 * interface up.
17028 			 */
17029 			ilm->ilm_notify_driver = B_FALSE;
17030 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
17031 			goto from;
17032 		}
17033 
17034 		/*
17035 		 * For allmulti address, we want to join on only one interface.
17036 		 * Checking for ilm_numentries_v6 is not correct as you may
17037 		 * find an ilm with zero address on to_ill, but we may not
17038 		 * have nominated to_ill for receiving. Thus, if we have
17039 		 * nominated from_ill (ill_join_allmulti is set), nominate
17040 		 * only if to_ill is not already nominated (to_ill normally
17041 		 * should not have been nominated if "from_ill" has already
17042 		 * been nominated. As we don't prevent failovers from happening
17043 		 * across groups, we don't assert).
17044 		 */
17045 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
17046 			/*
17047 			 * There is no need to hold ill locks as we are
17048 			 * writer on both ills and when ill_join_allmulti
17049 			 * is changed the thread is always a writer.
17050 			 */
17051 			if (from_ill->ill_join_allmulti &&
17052 			    !to_ill->ill_join_allmulti) {
17053 				(void) ip_join_allmulti(to_ill->ill_ipif);
17054 			}
17055 		} else if (ilm->ilm_notify_driver) {
17056 
17057 			/*
17058 			 * This is a newly moved ilm so we need to tell the
17059 			 * driver about the new group. There can be more than
17060 			 * one ilm's for the same group in the list each with a
17061 			 * different orig_ifindex. We have to inform the driver
17062 			 * once. In ilm_move_v[4,6] we only set the flag
17063 			 * ilm_notify_driver for the first ilm.
17064 			 */
17065 
17066 			(void) ip_ll_send_enabmulti_req(to_ill,
17067 			    &ilm->ilm_v6addr);
17068 		}
17069 
17070 		ilm->ilm_notify_driver = B_FALSE;
17071 
17072 		/*
17073 		 * See whether we need to send down DL_DISABMULTI_REQ on
17074 		 * from_ill as ilm has just been removed.
17075 		 */
17076 from:
17077 		ipif = from_ill->ill_ipif;
17078 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
17079 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
17080 			ip1dbg(("ilm_send_multicast_reqs: "
17081 			    "from_ill not resolver\n"));
17082 			continue;		/* Must be IRE_IF_NORESOLVER */
17083 		}
17084 
17085 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
17086 			ip1dbg(("ilm_send_multicast_reqs: "
17087 			    "from_ill MULTI_BCAST\n"));
17088 			continue;
17089 		}
17090 
17091 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
17092 			if (from_ill->ill_join_allmulti)
17093 				(void) ip_leave_allmulti(from_ill->ill_ipif);
17094 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
17095 			(void) ip_ll_send_disabmulti_req(from_ill,
17096 			    &ilm->ilm_v6addr);
17097 		}
17098 	}
17099 	ILM_WALKER_RELE(to_ill);
17100 }
17101 
17102 /*
17103  * This function is called when all multicast memberships needs
17104  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
17105  * called only once unlike the IPv4 counterpart where it is called after
17106  * every logical interface is moved. The reason is due to multicast
17107  * memberships are joined using an interface address in IPv4 while in
17108  * IPv6, interface index is used.
17109  */
17110 static void
17111 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
17112 {
17113 	ilm_t	*ilm;
17114 	ilm_t	*ilm_next;
17115 	ilm_t	*new_ilm;
17116 	ilm_t	**ilmp;
17117 	int	count;
17118 	char buf[INET6_ADDRSTRLEN];
17119 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
17120 	ip_stack_t	*ipst = from_ill->ill_ipst;
17121 
17122 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17123 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17124 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17125 
17126 	if (ifindex == 0) {
17127 		/*
17128 		 * Form the solicited node mcast address which is used later.
17129 		 */
17130 		ipif_t *ipif;
17131 
17132 		ipif = from_ill->ill_ipif;
17133 		ASSERT(ipif->ipif_id == 0);
17134 
17135 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
17136 	}
17137 
17138 	ilmp = &from_ill->ill_ilm;
17139 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17140 		ilm_next = ilm->ilm_next;
17141 
17142 		if (ilm->ilm_flags & ILM_DELETED) {
17143 			ilmp = &ilm->ilm_next;
17144 			continue;
17145 		}
17146 
17147 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
17148 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
17149 		ASSERT(ilm->ilm_orig_ifindex != 0);
17150 		if (ilm->ilm_orig_ifindex == ifindex) {
17151 			/*
17152 			 * We are failing back multicast memberships.
17153 			 * If the same ilm exists in to_ill, it means somebody
17154 			 * has joined the same group there e.g. ff02::1
17155 			 * is joined within the kernel when the interfaces
17156 			 * came UP.
17157 			 */
17158 			ASSERT(ilm->ilm_ipif == NULL);
17159 			if (new_ilm != NULL) {
17160 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17161 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17162 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17163 					new_ilm->ilm_is_new = B_TRUE;
17164 				}
17165 			} else {
17166 				/*
17167 				 * check if we can just move the ilm
17168 				 */
17169 				if (from_ill->ill_ilm_walker_cnt != 0) {
17170 					/*
17171 					 * We have walkers we cannot move
17172 					 * the ilm, so allocate a new ilm,
17173 					 * this (old) ilm will be marked
17174 					 * ILM_DELETED at the end of the loop
17175 					 * and will be freed when the
17176 					 * last walker exits.
17177 					 */
17178 					new_ilm = (ilm_t *)mi_zalloc
17179 					    (sizeof (ilm_t));
17180 					if (new_ilm == NULL) {
17181 						ip0dbg(("ilm_move_v6: "
17182 						    "FAILBACK of IPv6"
17183 						    " multicast address %s : "
17184 						    "from %s to"
17185 						    " %s failed : ENOMEM \n",
17186 						    inet_ntop(AF_INET6,
17187 						    &ilm->ilm_v6addr, buf,
17188 						    sizeof (buf)),
17189 						    from_ill->ill_name,
17190 						    to_ill->ill_name));
17191 
17192 							ilmp = &ilm->ilm_next;
17193 							continue;
17194 					}
17195 					*new_ilm = *ilm;
17196 					/*
17197 					 * we don't want new_ilm linked to
17198 					 * ilm's filter list.
17199 					 */
17200 					new_ilm->ilm_filter = NULL;
17201 				} else {
17202 					/*
17203 					 * No walkers we can move the ilm.
17204 					 * lets take it out of the list.
17205 					 */
17206 					*ilmp = ilm->ilm_next;
17207 					ilm->ilm_next = NULL;
17208 					new_ilm = ilm;
17209 				}
17210 
17211 				/*
17212 				 * if this is the first ilm for the group
17213 				 * set ilm_notify_driver so that we notify the
17214 				 * driver in ilm_send_multicast_reqs.
17215 				 */
17216 				if (ilm_lookup_ill_v6(to_ill,
17217 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17218 					new_ilm->ilm_notify_driver = B_TRUE;
17219 
17220 				new_ilm->ilm_ill = to_ill;
17221 				/* Add to the to_ill's list */
17222 				new_ilm->ilm_next = to_ill->ill_ilm;
17223 				to_ill->ill_ilm = new_ilm;
17224 				/*
17225 				 * set the flag so that mld_joingroup is
17226 				 * called in ilm_send_multicast_reqs().
17227 				 */
17228 				new_ilm->ilm_is_new = B_TRUE;
17229 			}
17230 			goto bottom;
17231 		} else if (ifindex != 0) {
17232 			/*
17233 			 * If this is FAILBACK (ifindex != 0) and the ifindex
17234 			 * has not matched above, look at the next ilm.
17235 			 */
17236 			ilmp = &ilm->ilm_next;
17237 			continue;
17238 		}
17239 		/*
17240 		 * If we are here, it means ifindex is 0. Failover
17241 		 * everything.
17242 		 *
17243 		 * We need to handle solicited node mcast address
17244 		 * and all_nodes mcast address differently as they
17245 		 * are joined witin the kenrel (ipif_multicast_up)
17246 		 * and potentially from the userland. We are called
17247 		 * after the ipifs of from_ill has been moved.
17248 		 * If we still find ilms on ill with solicited node
17249 		 * mcast address or all_nodes mcast address, it must
17250 		 * belong to the UP interface that has not moved e.g.
17251 		 * ipif_id 0 with the link local prefix does not move.
17252 		 * We join this on the new ill accounting for all the
17253 		 * userland memberships so that applications don't
17254 		 * see any failure.
17255 		 *
17256 		 * We need to make sure that we account only for the
17257 		 * solicited node and all node multicast addresses
17258 		 * that was brought UP on these. In the case of
17259 		 * a failover from A to B, we might have ilms belonging
17260 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
17261 		 * for the membership from the userland. If we are failing
17262 		 * over from B to C now, we will find the ones belonging
17263 		 * to A on B. These don't account for the ill_ipif_up_count.
17264 		 * They just move from B to C. The check below on
17265 		 * ilm_orig_ifindex ensures that.
17266 		 */
17267 		if ((ilm->ilm_orig_ifindex ==
17268 		    from_ill->ill_phyint->phyint_ifindex) &&
17269 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
17270 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
17271 		    &ilm->ilm_v6addr))) {
17272 			ASSERT(ilm->ilm_refcnt > 0);
17273 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
17274 			/*
17275 			 * For indentation reasons, we are not using a
17276 			 * "else" here.
17277 			 */
17278 			if (count == 0) {
17279 				ilmp = &ilm->ilm_next;
17280 				continue;
17281 			}
17282 			ilm->ilm_refcnt -= count;
17283 			if (new_ilm != NULL) {
17284 				/*
17285 				 * Can find one with the same
17286 				 * ilm_orig_ifindex, if we are failing
17287 				 * over to a STANDBY. This happens
17288 				 * when somebody wants to join a group
17289 				 * on a STANDBY interface and we
17290 				 * internally join on a different one.
17291 				 * If we had joined on from_ill then, a
17292 				 * failover now will find a new ilm
17293 				 * with this index.
17294 				 */
17295 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17296 				    " new ilm on %s, group address %s\n",
17297 				    to_ill->ill_name,
17298 				    inet_ntop(AF_INET6,
17299 				    &ilm->ilm_v6addr, buf,
17300 				    sizeof (buf))));
17301 				new_ilm->ilm_refcnt += count;
17302 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17303 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17304 					new_ilm->ilm_is_new = B_TRUE;
17305 				}
17306 			} else {
17307 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17308 				if (new_ilm == NULL) {
17309 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17310 					    " multicast address %s : from %s to"
17311 					    " %s failed : ENOMEM \n",
17312 					    inet_ntop(AF_INET6,
17313 					    &ilm->ilm_v6addr, buf,
17314 					    sizeof (buf)), from_ill->ill_name,
17315 					    to_ill->ill_name));
17316 					ilmp = &ilm->ilm_next;
17317 					continue;
17318 				}
17319 				*new_ilm = *ilm;
17320 				new_ilm->ilm_filter = NULL;
17321 				new_ilm->ilm_refcnt = count;
17322 				new_ilm->ilm_timer = INFINITY;
17323 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17324 				new_ilm->ilm_is_new = B_TRUE;
17325 				/*
17326 				 * If the to_ill has not joined this
17327 				 * group we need to tell the driver in
17328 				 * ill_send_multicast_reqs.
17329 				 */
17330 				if (ilm_lookup_ill_v6(to_ill,
17331 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17332 					new_ilm->ilm_notify_driver = B_TRUE;
17333 
17334 				new_ilm->ilm_ill = to_ill;
17335 				/* Add to the to_ill's list */
17336 				new_ilm->ilm_next = to_ill->ill_ilm;
17337 				to_ill->ill_ilm = new_ilm;
17338 				ASSERT(new_ilm->ilm_ipif == NULL);
17339 			}
17340 			if (ilm->ilm_refcnt == 0) {
17341 				goto bottom;
17342 			} else {
17343 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17344 				CLEAR_SLIST(new_ilm->ilm_filter);
17345 				ilmp = &ilm->ilm_next;
17346 			}
17347 			continue;
17348 		} else {
17349 			/*
17350 			 * ifindex = 0 means, move everything pointing at
17351 			 * from_ill. We are doing this becuase ill has
17352 			 * either FAILED or became INACTIVE.
17353 			 *
17354 			 * As we would like to move things later back to
17355 			 * from_ill, we want to retain the identity of this
17356 			 * ilm. Thus, we don't blindly increment the reference
17357 			 * count on the ilms matching the address alone. We
17358 			 * need to match on the ilm_orig_index also. new_ilm
17359 			 * was obtained by matching ilm_orig_index also.
17360 			 */
17361 			if (new_ilm != NULL) {
17362 				/*
17363 				 * This is possible only if a previous restore
17364 				 * was incomplete i.e restore to
17365 				 * ilm_orig_ifindex left some ilms because
17366 				 * of some failures. Thus when we are failing
17367 				 * again, we might find our old friends there.
17368 				 */
17369 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17370 				    " on %s, group address %s\n",
17371 				    to_ill->ill_name,
17372 				    inet_ntop(AF_INET6,
17373 				    &ilm->ilm_v6addr, buf,
17374 				    sizeof (buf))));
17375 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17376 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17377 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17378 					new_ilm->ilm_is_new = B_TRUE;
17379 				}
17380 			} else {
17381 				if (from_ill->ill_ilm_walker_cnt != 0) {
17382 					new_ilm = (ilm_t *)
17383 					    mi_zalloc(sizeof (ilm_t));
17384 					if (new_ilm == NULL) {
17385 						ip0dbg(("ilm_move_v6: "
17386 						    "FAILOVER of IPv6"
17387 						    " multicast address %s : "
17388 						    "from %s to"
17389 						    " %s failed : ENOMEM \n",
17390 						    inet_ntop(AF_INET6,
17391 						    &ilm->ilm_v6addr, buf,
17392 						    sizeof (buf)),
17393 						    from_ill->ill_name,
17394 						    to_ill->ill_name));
17395 
17396 							ilmp = &ilm->ilm_next;
17397 							continue;
17398 					}
17399 					*new_ilm = *ilm;
17400 					new_ilm->ilm_filter = NULL;
17401 				} else {
17402 					*ilmp = ilm->ilm_next;
17403 					new_ilm = ilm;
17404 				}
17405 				/*
17406 				 * If the to_ill has not joined this
17407 				 * group we need to tell the driver in
17408 				 * ill_send_multicast_reqs.
17409 				 */
17410 				if (ilm_lookup_ill_v6(to_ill,
17411 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17412 					new_ilm->ilm_notify_driver = B_TRUE;
17413 
17414 				/* Add to the to_ill's list */
17415 				new_ilm->ilm_next = to_ill->ill_ilm;
17416 				to_ill->ill_ilm = new_ilm;
17417 				ASSERT(ilm->ilm_ipif == NULL);
17418 				new_ilm->ilm_ill = to_ill;
17419 				new_ilm->ilm_is_new = B_TRUE;
17420 			}
17421 
17422 		}
17423 
17424 bottom:
17425 		/*
17426 		 * Revert multicast filter state to (EXCLUDE, NULL).
17427 		 * new_ilm->ilm_is_new should already be set if needed.
17428 		 */
17429 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17430 		CLEAR_SLIST(new_ilm->ilm_filter);
17431 		/*
17432 		 * We allocated/got a new ilm, free the old one.
17433 		 */
17434 		if (new_ilm != ilm) {
17435 			if (from_ill->ill_ilm_walker_cnt == 0) {
17436 				*ilmp = ilm->ilm_next;
17437 				ilm->ilm_next = NULL;
17438 				FREE_SLIST(ilm->ilm_filter);
17439 				FREE_SLIST(ilm->ilm_pendsrcs);
17440 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17441 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17442 				mi_free((char *)ilm);
17443 			} else {
17444 				ilm->ilm_flags |= ILM_DELETED;
17445 				from_ill->ill_ilm_cleanup_reqd = 1;
17446 				ilmp = &ilm->ilm_next;
17447 			}
17448 		}
17449 	}
17450 }
17451 
17452 /*
17453  * Move all the multicast memberships to to_ill. Called when
17454  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17455  * different from IPv6 counterpart as multicast memberships are associated
17456  * with ills in IPv6. This function is called after every ipif is moved
17457  * unlike IPv6, where it is moved only once.
17458  */
17459 static void
17460 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17461 {
17462 	ilm_t	*ilm;
17463 	ilm_t	*ilm_next;
17464 	ilm_t	*new_ilm;
17465 	ilm_t	**ilmp;
17466 	ip_stack_t	*ipst = from_ill->ill_ipst;
17467 
17468 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17469 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17470 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17471 
17472 	ilmp = &from_ill->ill_ilm;
17473 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17474 		ilm_next = ilm->ilm_next;
17475 
17476 		if (ilm->ilm_flags & ILM_DELETED) {
17477 			ilmp = &ilm->ilm_next;
17478 			continue;
17479 		}
17480 
17481 		ASSERT(ilm->ilm_ipif != NULL);
17482 
17483 		if (ilm->ilm_ipif != ipif) {
17484 			ilmp = &ilm->ilm_next;
17485 			continue;
17486 		}
17487 
17488 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17489 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17490 			new_ilm = ilm_lookup_ipif(ipif,
17491 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17492 			if (new_ilm != NULL) {
17493 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17494 				/*
17495 				 * We still need to deal with the from_ill.
17496 				 */
17497 				new_ilm->ilm_is_new = B_TRUE;
17498 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17499 				CLEAR_SLIST(new_ilm->ilm_filter);
17500 				goto delete_ilm;
17501 			}
17502 			/*
17503 			 * If we could not find one e.g. ipif is
17504 			 * still down on to_ill, we add this ilm
17505 			 * on ill_new to preserve the reference
17506 			 * count.
17507 			 */
17508 		}
17509 		/*
17510 		 * When ipifs move, ilms always move with it
17511 		 * to the NEW ill. Thus we should never be
17512 		 * able to find ilm till we really move it here.
17513 		 */
17514 		ASSERT(ilm_lookup_ipif(ipif,
17515 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17516 
17517 		if (from_ill->ill_ilm_walker_cnt != 0) {
17518 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17519 			if (new_ilm == NULL) {
17520 				char buf[INET6_ADDRSTRLEN];
17521 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17522 				    " multicast address %s : "
17523 				    "from %s to"
17524 				    " %s failed : ENOMEM \n",
17525 				    inet_ntop(AF_INET,
17526 				    &ilm->ilm_v6addr, buf,
17527 				    sizeof (buf)),
17528 				    from_ill->ill_name,
17529 				    to_ill->ill_name));
17530 
17531 				ilmp = &ilm->ilm_next;
17532 				continue;
17533 			}
17534 			*new_ilm = *ilm;
17535 			/* We don't want new_ilm linked to ilm's filter list */
17536 			new_ilm->ilm_filter = NULL;
17537 		} else {
17538 			/* Remove from the list */
17539 			*ilmp = ilm->ilm_next;
17540 			new_ilm = ilm;
17541 		}
17542 
17543 		/*
17544 		 * If we have never joined this group on the to_ill
17545 		 * make sure we tell the driver.
17546 		 */
17547 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17548 		    ALL_ZONES) == NULL)
17549 			new_ilm->ilm_notify_driver = B_TRUE;
17550 
17551 		/* Add to the to_ill's list */
17552 		new_ilm->ilm_next = to_ill->ill_ilm;
17553 		to_ill->ill_ilm = new_ilm;
17554 		new_ilm->ilm_is_new = B_TRUE;
17555 
17556 		/*
17557 		 * Revert multicast filter state to (EXCLUDE, NULL)
17558 		 */
17559 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17560 		CLEAR_SLIST(new_ilm->ilm_filter);
17561 
17562 		/*
17563 		 * Delete only if we have allocated a new ilm.
17564 		 */
17565 		if (new_ilm != ilm) {
17566 delete_ilm:
17567 			if (from_ill->ill_ilm_walker_cnt == 0) {
17568 				/* Remove from the list */
17569 				*ilmp = ilm->ilm_next;
17570 				ilm->ilm_next = NULL;
17571 				FREE_SLIST(ilm->ilm_filter);
17572 				FREE_SLIST(ilm->ilm_pendsrcs);
17573 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17574 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17575 				mi_free((char *)ilm);
17576 			} else {
17577 				ilm->ilm_flags |= ILM_DELETED;
17578 				from_ill->ill_ilm_cleanup_reqd = 1;
17579 				ilmp = &ilm->ilm_next;
17580 			}
17581 		}
17582 	}
17583 }
17584 
17585 static uint_t
17586 ipif_get_id(ill_t *ill, uint_t id)
17587 {
17588 	uint_t	unit;
17589 	ipif_t	*tipif;
17590 	boolean_t found = B_FALSE;
17591 	ip_stack_t	*ipst = ill->ill_ipst;
17592 
17593 	/*
17594 	 * During failback, we want to go back to the same id
17595 	 * instead of the smallest id so that the original
17596 	 * configuration is maintained. id is non-zero in that
17597 	 * case.
17598 	 */
17599 	if (id != 0) {
17600 		/*
17601 		 * While failing back, if we still have an ipif with
17602 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17603 		 * as soon as we return from this function. It was
17604 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17605 		 * we can choose the smallest id. Thus we return zero
17606 		 * in that case ignoring the hint.
17607 		 */
17608 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17609 			return (0);
17610 		for (tipif = ill->ill_ipif; tipif != NULL;
17611 		    tipif = tipif->ipif_next) {
17612 			if (tipif->ipif_id == id) {
17613 				found = B_TRUE;
17614 				break;
17615 			}
17616 		}
17617 		/*
17618 		 * If somebody already plumbed another logical
17619 		 * with the same id, we won't be able to find it.
17620 		 */
17621 		if (!found)
17622 			return (id);
17623 	}
17624 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17625 		found = B_FALSE;
17626 		for (tipif = ill->ill_ipif; tipif != NULL;
17627 		    tipif = tipif->ipif_next) {
17628 			if (tipif->ipif_id == unit) {
17629 				found = B_TRUE;
17630 				break;
17631 			}
17632 		}
17633 		if (!found)
17634 			break;
17635 	}
17636 	return (unit);
17637 }
17638 
17639 /* ARGSUSED */
17640 static int
17641 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17642     ipif_t **rep_ipif_ptr)
17643 {
17644 	ill_t	*from_ill;
17645 	ipif_t	*rep_ipif;
17646 	uint_t	unit;
17647 	int err = 0;
17648 	ipif_t	*to_ipif;
17649 	struct iocblk	*iocp;
17650 	boolean_t failback_cmd;
17651 	boolean_t remove_ipif;
17652 	int	rc;
17653 	ip_stack_t	*ipst;
17654 
17655 	ASSERT(IAM_WRITER_ILL(to_ill));
17656 	ASSERT(IAM_WRITER_IPIF(ipif));
17657 
17658 	iocp = (struct iocblk *)mp->b_rptr;
17659 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17660 	remove_ipif = B_FALSE;
17661 
17662 	from_ill = ipif->ipif_ill;
17663 	ipst = from_ill->ill_ipst;
17664 
17665 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17666 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17667 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17668 
17669 	/*
17670 	 * Don't move LINK LOCAL addresses as they are tied to
17671 	 * physical interface.
17672 	 */
17673 	if (from_ill->ill_isv6 &&
17674 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17675 		ipif->ipif_was_up = B_FALSE;
17676 		IPIF_UNMARK_MOVING(ipif);
17677 		return (0);
17678 	}
17679 
17680 	/*
17681 	 * We set the ipif_id to maximum so that the search for
17682 	 * ipif_id will pick the lowest number i.e 0 in the
17683 	 * following 2 cases :
17684 	 *
17685 	 * 1) We have a replacement ipif at the head of to_ill.
17686 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17687 	 *    on to_ill and hence the MOVE might fail. We want to
17688 	 *    remove it only if we could move the ipif. Thus, by
17689 	 *    setting it to the MAX value, we make the search in
17690 	 *    ipif_get_id return the zeroth id.
17691 	 *
17692 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17693 	 *    we might just have a zero address plumbed on the ipif
17694 	 *    with zero id in the case of IPv4. We remove that while
17695 	 *    doing the failback. We want to remove it only if we
17696 	 *    could move the ipif. Thus, by setting it to the MAX
17697 	 *    value, we make the search in ipif_get_id return the
17698 	 *    zeroth id.
17699 	 *
17700 	 * Both (1) and (2) are done only when when we are moving
17701 	 * an ipif (either due to failover/failback) which originally
17702 	 * belonged to this interface i.e the ipif_orig_ifindex is
17703 	 * the same as to_ill's ifindex. This is needed so that
17704 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17705 	 * from B -> A (B is being removed from the group) and
17706 	 * FAILBACK from A -> B restores the original configuration.
17707 	 * Without the check for orig_ifindex, the second FAILOVER
17708 	 * could make the ipif belonging to B replace the A's zeroth
17709 	 * ipif and the subsequent failback re-creating the replacement
17710 	 * ipif again.
17711 	 *
17712 	 * NOTE : We created the replacement ipif when we did a
17713 	 * FAILOVER (See below). We could check for FAILBACK and
17714 	 * then look for replacement ipif to be removed. But we don't
17715 	 * want to do that because we wan't to allow the possibility
17716 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17717 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17718 	 * from B -> A.
17719 	 */
17720 	to_ipif = to_ill->ill_ipif;
17721 	if ((to_ill->ill_phyint->phyint_ifindex ==
17722 	    ipif->ipif_orig_ifindex) &&
17723 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17724 		ASSERT(to_ipif->ipif_id == 0);
17725 		remove_ipif = B_TRUE;
17726 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17727 	}
17728 	/*
17729 	 * Find the lowest logical unit number on the to_ill.
17730 	 * If we are failing back, try to get the original id
17731 	 * rather than the lowest one so that the original
17732 	 * configuration is maintained.
17733 	 *
17734 	 * XXX need a better scheme for this.
17735 	 */
17736 	if (failback_cmd) {
17737 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17738 	} else {
17739 		unit = ipif_get_id(to_ill, 0);
17740 	}
17741 
17742 	/* Reset back to zero in case we fail below */
17743 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17744 		to_ipif->ipif_id = 0;
17745 
17746 	if (unit == ipst->ips_ip_addrs_per_if) {
17747 		ipif->ipif_was_up = B_FALSE;
17748 		IPIF_UNMARK_MOVING(ipif);
17749 		return (EINVAL);
17750 	}
17751 
17752 	/*
17753 	 * ipif is ready to move from "from_ill" to "to_ill".
17754 	 *
17755 	 * 1) If we are moving ipif with id zero, create a
17756 	 *    replacement ipif for this ipif on from_ill. If this fails
17757 	 *    fail the MOVE operation.
17758 	 *
17759 	 * 2) Remove the replacement ipif on to_ill if any.
17760 	 *    We could remove the replacement ipif when we are moving
17761 	 *    the ipif with id zero. But what if somebody already
17762 	 *    unplumbed it ? Thus we always remove it if it is present.
17763 	 *    We want to do it only if we are sure we are going to
17764 	 *    move the ipif to to_ill which is why there are no
17765 	 *    returns due to error till ipif is linked to to_ill.
17766 	 *    Note that the first ipif that we failback will always
17767 	 *    be zero if it is present.
17768 	 */
17769 	if (ipif->ipif_id == 0) {
17770 		ipaddr_t inaddr_any = INADDR_ANY;
17771 
17772 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17773 		if (rep_ipif == NULL) {
17774 			ipif->ipif_was_up = B_FALSE;
17775 			IPIF_UNMARK_MOVING(ipif);
17776 			return (ENOMEM);
17777 		}
17778 		*rep_ipif = ipif_zero;
17779 		/*
17780 		 * Before we put the ipif on the list, store the addresses
17781 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17782 		 * assumes so. This logic is not any different from what
17783 		 * ipif_allocate does.
17784 		 */
17785 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17786 		    &rep_ipif->ipif_v6lcl_addr);
17787 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17788 		    &rep_ipif->ipif_v6src_addr);
17789 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17790 		    &rep_ipif->ipif_v6subnet);
17791 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17792 		    &rep_ipif->ipif_v6net_mask);
17793 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17794 		    &rep_ipif->ipif_v6brd_addr);
17795 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17796 		    &rep_ipif->ipif_v6pp_dst_addr);
17797 		/*
17798 		 * We mark IPIF_NOFAILOVER so that this can never
17799 		 * move.
17800 		 */
17801 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17802 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17803 		rep_ipif->ipif_replace_zero = B_TRUE;
17804 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17805 		    MUTEX_DEFAULT, NULL);
17806 		rep_ipif->ipif_id = 0;
17807 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17808 		rep_ipif->ipif_ill = from_ill;
17809 		rep_ipif->ipif_orig_ifindex =
17810 		    from_ill->ill_phyint->phyint_ifindex;
17811 		/* Insert at head */
17812 		rep_ipif->ipif_next = from_ill->ill_ipif;
17813 		from_ill->ill_ipif = rep_ipif;
17814 		/*
17815 		 * We don't really care to let apps know about
17816 		 * this interface.
17817 		 */
17818 	}
17819 
17820 	if (remove_ipif) {
17821 		/*
17822 		 * We set to a max value above for this case to get
17823 		 * id zero. ASSERT that we did get one.
17824 		 */
17825 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17826 		rep_ipif = to_ipif;
17827 		to_ill->ill_ipif = rep_ipif->ipif_next;
17828 		rep_ipif->ipif_next = NULL;
17829 		/*
17830 		 * If some apps scanned and find this interface,
17831 		 * it is time to let them know, so that they can
17832 		 * delete it.
17833 		 */
17834 
17835 		*rep_ipif_ptr = rep_ipif;
17836 	}
17837 
17838 	/* Get it out of the ILL interface list. */
17839 	ipif_remove(ipif, B_FALSE);
17840 
17841 	/* Assign the new ill */
17842 	ipif->ipif_ill = to_ill;
17843 	ipif->ipif_id = unit;
17844 	/* id has already been checked */
17845 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17846 	ASSERT(rc == 0);
17847 	/* Let SCTP update its list */
17848 	sctp_move_ipif(ipif, from_ill, to_ill);
17849 	/*
17850 	 * Handle the failover and failback of ipif_t between
17851 	 * ill_t that have differing maximum mtu values.
17852 	 */
17853 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17854 		if (ipif->ipif_saved_mtu == 0) {
17855 			/*
17856 			 * As this ipif_t is moving to an ill_t
17857 			 * that has a lower ill_max_mtu, its
17858 			 * ipif_mtu needs to be saved so it can
17859 			 * be restored during failback or during
17860 			 * failover to an ill_t which has a
17861 			 * higher ill_max_mtu.
17862 			 */
17863 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17864 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17865 		} else {
17866 			/*
17867 			 * The ipif_t is, once again, moving to
17868 			 * an ill_t that has a lower maximum mtu
17869 			 * value.
17870 			 */
17871 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17872 		}
17873 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17874 	    ipif->ipif_saved_mtu != 0) {
17875 		/*
17876 		 * The mtu of this ipif_t had to be reduced
17877 		 * during an earlier failover; this is an
17878 		 * opportunity for it to be increased (either as
17879 		 * part of another failover or a failback).
17880 		 */
17881 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17882 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17883 			ipif->ipif_saved_mtu = 0;
17884 		} else {
17885 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17886 		}
17887 	}
17888 
17889 	/*
17890 	 * We preserve all the other fields of the ipif including
17891 	 * ipif_saved_ire_mp. The routes that are saved here will
17892 	 * be recreated on the new interface and back on the old
17893 	 * interface when we move back.
17894 	 */
17895 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17896 
17897 	return (err);
17898 }
17899 
17900 static int
17901 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17902     int ifindex, ipif_t **rep_ipif_ptr)
17903 {
17904 	ipif_t *mipif;
17905 	ipif_t *ipif_next;
17906 	int err;
17907 
17908 	/*
17909 	 * We don't really try to MOVE back things if some of the
17910 	 * operations fail. The daemon will take care of moving again
17911 	 * later on.
17912 	 */
17913 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17914 		ipif_next = mipif->ipif_next;
17915 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17916 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17917 
17918 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17919 
17920 			/*
17921 			 * When the MOVE fails, it is the job of the
17922 			 * application to take care of this properly
17923 			 * i.e try again if it is ENOMEM.
17924 			 */
17925 			if (mipif->ipif_ill != from_ill) {
17926 				/*
17927 				 * ipif has moved.
17928 				 *
17929 				 * Move the multicast memberships associated
17930 				 * with this ipif to the new ill. For IPv6, we
17931 				 * do it once after all the ipifs are moved
17932 				 * (in ill_move) as they are not associated
17933 				 * with ipifs.
17934 				 *
17935 				 * We need to move the ilms as the ipif has
17936 				 * already been moved to a new ill even
17937 				 * in the case of errors. Neither
17938 				 * ilm_free(ipif) will find the ilm
17939 				 * when somebody unplumbs this ipif nor
17940 				 * ilm_delete(ilm) will be able to find the
17941 				 * ilm, if we don't move now.
17942 				 */
17943 				if (!from_ill->ill_isv6)
17944 					ilm_move_v4(from_ill, to_ill, mipif);
17945 			}
17946 
17947 			if (err != 0)
17948 				return (err);
17949 		}
17950 	}
17951 	return (0);
17952 }
17953 
17954 static int
17955 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17956 {
17957 	int ifindex;
17958 	int err;
17959 	struct iocblk	*iocp;
17960 	ipif_t	*ipif;
17961 	ipif_t *rep_ipif_ptr = NULL;
17962 	ipif_t	*from_ipif = NULL;
17963 	boolean_t check_rep_if = B_FALSE;
17964 	ip_stack_t	*ipst = from_ill->ill_ipst;
17965 
17966 	iocp = (struct iocblk *)mp->b_rptr;
17967 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17968 		/*
17969 		 * Move everything pointing at from_ill to to_ill.
17970 		 * We acheive this by passing in 0 as ifindex.
17971 		 */
17972 		ifindex = 0;
17973 	} else {
17974 		/*
17975 		 * Move everything pointing at from_ill whose original
17976 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17977 		 * We acheive this by passing in ifindex rather than 0.
17978 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17979 		 */
17980 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17981 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17982 	}
17983 
17984 	/*
17985 	 * Determine if there is at least one ipif that would move from
17986 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17987 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17988 	 * the move, in which case we need to quiesce the replacement ipif also.
17989 	 */
17990 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17991 	    from_ipif = from_ipif->ipif_next) {
17992 		if (((ifindex == 0) ||
17993 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17994 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17995 			check_rep_if = B_TRUE;
17996 			break;
17997 		}
17998 	}
17999 
18000 
18001 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
18002 
18003 	GRAB_ILL_LOCKS(from_ill, to_ill);
18004 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
18005 		(void) ipsq_pending_mp_add(NULL, ipif, q,
18006 		    mp, ILL_MOVE_OK);
18007 		RELEASE_ILL_LOCKS(from_ill, to_ill);
18008 		return (EINPROGRESS);
18009 	}
18010 
18011 	/* Check if the replacement ipif is quiescent to delete */
18012 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
18013 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
18014 		to_ill->ill_ipif->ipif_state_flags |=
18015 		    IPIF_MOVING | IPIF_CHANGING;
18016 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
18017 			(void) ipsq_pending_mp_add(NULL, ipif, q,
18018 			    mp, ILL_MOVE_OK);
18019 			RELEASE_ILL_LOCKS(from_ill, to_ill);
18020 			return (EINPROGRESS);
18021 		}
18022 	}
18023 	RELEASE_ILL_LOCKS(from_ill, to_ill);
18024 
18025 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
18026 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
18027 	GRAB_ILL_LOCKS(from_ill, to_ill);
18028 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
18029 
18030 	/* ilm_move is done inside ipif_move for IPv4 */
18031 	if (err == 0 && from_ill->ill_isv6)
18032 		ilm_move_v6(from_ill, to_ill, ifindex);
18033 
18034 	RELEASE_ILL_LOCKS(from_ill, to_ill);
18035 	rw_exit(&ipst->ips_ill_g_lock);
18036 
18037 	/*
18038 	 * send rts messages and multicast messages.
18039 	 */
18040 	if (rep_ipif_ptr != NULL) {
18041 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
18042 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
18043 			rep_ipif_ptr->ipif_recovery_id = 0;
18044 		}
18045 		ip_rts_ifmsg(rep_ipif_ptr);
18046 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
18047 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
18048 		mi_free(rep_ipif_ptr);
18049 	}
18050 
18051 	conn_move_ill(from_ill, to_ill, ifindex);
18052 
18053 	return (err);
18054 }
18055 
18056 /*
18057  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
18058  * Also checks for the validity of the arguments.
18059  * Note: We are already exclusive inside the from group.
18060  * It is upto the caller to release refcnt on the to_ill's.
18061  */
18062 static int
18063 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
18064     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
18065 {
18066 	int dst_index;
18067 	ipif_t *ipif_v4, *ipif_v6;
18068 	struct lifreq *lifr;
18069 	mblk_t *mp1;
18070 	boolean_t exists;
18071 	sin_t	*sin;
18072 	int	err = 0;
18073 	ip_stack_t	*ipst;
18074 
18075 	if (CONN_Q(q))
18076 		ipst = CONNQ_TO_IPST(q);
18077 	else
18078 		ipst = ILLQ_TO_IPST(q);
18079 
18080 
18081 	if ((mp1 = mp->b_cont) == NULL)
18082 		return (EPROTO);
18083 
18084 	if ((mp1 = mp1->b_cont) == NULL)
18085 		return (EPROTO);
18086 
18087 	lifr = (struct lifreq *)mp1->b_rptr;
18088 	sin = (sin_t *)&lifr->lifr_addr;
18089 
18090 	/*
18091 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
18092 	 * specific operations.
18093 	 */
18094 	if (sin->sin_family != AF_UNSPEC)
18095 		return (EINVAL);
18096 
18097 	/*
18098 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
18099 	 * NULLs for the last 4 args and we know the lookup won't fail
18100 	 * with EINPROGRESS.
18101 	 */
18102 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
18103 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
18104 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
18105 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
18106 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
18107 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
18108 
18109 	if (ipif_v4 == NULL && ipif_v6 == NULL)
18110 		return (ENXIO);
18111 
18112 	if (ipif_v4 != NULL) {
18113 		ASSERT(ipif_v4->ipif_refcnt != 0);
18114 		if (ipif_v4->ipif_id != 0) {
18115 			err = EINVAL;
18116 			goto done;
18117 		}
18118 
18119 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
18120 		*ill_from_v4 = ipif_v4->ipif_ill;
18121 	}
18122 
18123 	if (ipif_v6 != NULL) {
18124 		ASSERT(ipif_v6->ipif_refcnt != 0);
18125 		if (ipif_v6->ipif_id != 0) {
18126 			err = EINVAL;
18127 			goto done;
18128 		}
18129 
18130 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
18131 		*ill_from_v6 = ipif_v6->ipif_ill;
18132 	}
18133 
18134 	err = 0;
18135 	dst_index = lifr->lifr_movetoindex;
18136 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
18137 	    q, mp, ip_process_ioctl, &err, ipst);
18138 	if (err != 0) {
18139 		/*
18140 		 * There could be only v6.
18141 		 */
18142 		if (err != ENXIO)
18143 			goto done;
18144 		err = 0;
18145 	}
18146 
18147 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
18148 	    q, mp, ip_process_ioctl, &err, ipst);
18149 	if (err != 0) {
18150 		if (err != ENXIO)
18151 			goto done;
18152 		if (*ill_to_v4 == NULL) {
18153 			err = ENXIO;
18154 			goto done;
18155 		}
18156 		err = 0;
18157 	}
18158 
18159 	/*
18160 	 * If we have something to MOVE i.e "from" not NULL,
18161 	 * "to" should be non-NULL.
18162 	 */
18163 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
18164 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
18165 		err = EINVAL;
18166 	}
18167 
18168 done:
18169 	if (ipif_v4 != NULL)
18170 		ipif_refrele(ipif_v4);
18171 	if (ipif_v6 != NULL)
18172 		ipif_refrele(ipif_v6);
18173 	return (err);
18174 }
18175 
18176 /*
18177  * FAILOVER and FAILBACK are modelled as MOVE operations.
18178  *
18179  * We don't check whether the MOVE is within the same group or
18180  * not, because this ioctl can be used as a generic mechanism
18181  * to failover from interface A to B, though things will function
18182  * only if they are really part of the same group. Moreover,
18183  * all ipifs may be down and hence temporarily out of the group.
18184  *
18185  * ipif's that need to be moved are first brought down; V4 ipifs are brought
18186  * down first and then V6.  For each we wait for the ipif's to become quiescent.
18187  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
18188  * have been deleted and there are no active references. Once quiescent the
18189  * ipif's are moved and brought up on the new ill.
18190  *
18191  * Normally the source ill and destination ill belong to the same IPMP group
18192  * and hence the same ipsq_t. In the event they don't belong to the same
18193  * same group the two ipsq's are first merged into one ipsq - that of the
18194  * to_ill. The multicast memberships on the source and destination ill cannot
18195  * change during the move operation since multicast joins/leaves also have to
18196  * execute on the same ipsq and are hence serialized.
18197  */
18198 /* ARGSUSED */
18199 int
18200 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18201     ip_ioctl_cmd_t *ipip, void *ifreq)
18202 {
18203 	ill_t *ill_to_v4 = NULL;
18204 	ill_t *ill_to_v6 = NULL;
18205 	ill_t *ill_from_v4 = NULL;
18206 	ill_t *ill_from_v6 = NULL;
18207 	int err = 0;
18208 
18209 	/*
18210 	 * setup from and to ill's, we can get EINPROGRESS only for
18211 	 * to_ill's.
18212 	 */
18213 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
18214 	    &ill_to_v4, &ill_to_v6);
18215 
18216 	if (err != 0) {
18217 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
18218 		goto done;
18219 	}
18220 
18221 	/*
18222 	 * nothing to do.
18223 	 */
18224 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
18225 		goto done;
18226 	}
18227 
18228 	/*
18229 	 * nothing to do.
18230 	 */
18231 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
18232 		goto done;
18233 	}
18234 
18235 	/*
18236 	 * Mark the ill as changing.
18237 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
18238 	 * in ill_up_ipifs in case of error they are cleared below.
18239 	 */
18240 
18241 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18242 	if (ill_from_v4 != NULL)
18243 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
18244 	if (ill_from_v6 != NULL)
18245 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
18246 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18247 
18248 	/*
18249 	 * Make sure that both src and dst are
18250 	 * in the same syncq group. If not make it happen.
18251 	 * We are not holding any locks because we are the writer
18252 	 * on the from_ipsq and we will hold locks in ill_merge_groups
18253 	 * to protect to_ipsq against changing.
18254 	 */
18255 	if (ill_from_v4 != NULL) {
18256 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
18257 		    ill_to_v4->ill_phyint->phyint_ipsq) {
18258 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
18259 			    NULL, mp, q);
18260 			goto err_ret;
18261 
18262 		}
18263 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18264 	} else {
18265 
18266 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18267 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18268 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18269 			    NULL, mp, q);
18270 			goto err_ret;
18271 
18272 		}
18273 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18274 	}
18275 
18276 	/*
18277 	 * Now that the ipsq's have been merged and we are the writer
18278 	 * lets mark to_ill as changing as well.
18279 	 */
18280 
18281 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18282 	if (ill_to_v4 != NULL)
18283 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18284 	if (ill_to_v6 != NULL)
18285 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18286 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18287 
18288 	/*
18289 	 * Its ok for us to proceed with the move even if
18290 	 * ill_pending_mp is non null on one of the from ill's as the reply
18291 	 * should not be looking at the ipif, it should only care about the
18292 	 * ill itself.
18293 	 */
18294 
18295 	/*
18296 	 * lets move ipv4 first.
18297 	 */
18298 	if (ill_from_v4 != NULL) {
18299 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18300 		ill_from_v4->ill_move_in_progress = B_TRUE;
18301 		ill_to_v4->ill_move_in_progress = B_TRUE;
18302 		ill_to_v4->ill_move_peer = ill_from_v4;
18303 		ill_from_v4->ill_move_peer = ill_to_v4;
18304 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18305 	}
18306 
18307 	/*
18308 	 * Now lets move ipv6.
18309 	 */
18310 	if (err == 0 && ill_from_v6 != NULL) {
18311 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18312 		ill_from_v6->ill_move_in_progress = B_TRUE;
18313 		ill_to_v6->ill_move_in_progress = B_TRUE;
18314 		ill_to_v6->ill_move_peer = ill_from_v6;
18315 		ill_from_v6->ill_move_peer = ill_to_v6;
18316 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18317 	}
18318 
18319 err_ret:
18320 	/*
18321 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18322 	 * moved to become quiescent.
18323 	 */
18324 	if (err == EINPROGRESS) {
18325 		goto done;
18326 	}
18327 
18328 	/*
18329 	 * if err is set ill_up_ipifs will not be called
18330 	 * lets clear the flags.
18331 	 */
18332 
18333 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18334 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18335 	/*
18336 	 * Some of the clearing may be redundant. But it is simple
18337 	 * not making any extra checks.
18338 	 */
18339 	if (ill_from_v6 != NULL) {
18340 		ill_from_v6->ill_move_in_progress = B_FALSE;
18341 		ill_from_v6->ill_move_peer = NULL;
18342 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18343 	}
18344 	if (ill_from_v4 != NULL) {
18345 		ill_from_v4->ill_move_in_progress = B_FALSE;
18346 		ill_from_v4->ill_move_peer = NULL;
18347 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18348 	}
18349 	if (ill_to_v6 != NULL) {
18350 		ill_to_v6->ill_move_in_progress = B_FALSE;
18351 		ill_to_v6->ill_move_peer = NULL;
18352 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18353 	}
18354 	if (ill_to_v4 != NULL) {
18355 		ill_to_v4->ill_move_in_progress = B_FALSE;
18356 		ill_to_v4->ill_move_peer = NULL;
18357 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18358 	}
18359 
18360 	/*
18361 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18362 	 * Do this always to maintain proper state i.e even in case of errors.
18363 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18364 	 * we need not call on both v4 and v6 interfaces.
18365 	 */
18366 	if (ill_from_v4 != NULL) {
18367 		if ((ill_from_v4->ill_phyint->phyint_flags &
18368 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18369 			phyint_inactive(ill_from_v4->ill_phyint);
18370 		}
18371 	} else if (ill_from_v6 != NULL) {
18372 		if ((ill_from_v6->ill_phyint->phyint_flags &
18373 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18374 			phyint_inactive(ill_from_v6->ill_phyint);
18375 		}
18376 	}
18377 
18378 	if (ill_to_v4 != NULL) {
18379 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18380 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18381 		}
18382 	} else if (ill_to_v6 != NULL) {
18383 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18384 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18385 		}
18386 	}
18387 
18388 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18389 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18390 
18391 no_err:
18392 	/*
18393 	 * lets bring the interfaces up on the to_ill.
18394 	 */
18395 	if (err == 0) {
18396 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18397 		    q, mp);
18398 	}
18399 
18400 	if (err == 0) {
18401 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18402 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18403 
18404 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18405 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18406 	}
18407 done:
18408 
18409 	if (ill_to_v4 != NULL) {
18410 		ill_refrele(ill_to_v4);
18411 	}
18412 	if (ill_to_v6 != NULL) {
18413 		ill_refrele(ill_to_v6);
18414 	}
18415 
18416 	return (err);
18417 }
18418 
18419 static void
18420 ill_dl_down(ill_t *ill)
18421 {
18422 	/*
18423 	 * The ill is down; unbind but stay attached since we're still
18424 	 * associated with a PPA. If we have negotiated DLPI capabilites
18425 	 * with the data link service provider (IDS_OK) then reset them.
18426 	 * The interval between unbinding and rebinding is potentially
18427 	 * unbounded hence we cannot assume things will be the same.
18428 	 * The DLPI capabilities will be probed again when the data link
18429 	 * is brought up.
18430 	 */
18431 	mblk_t	*mp = ill->ill_unbind_mp;
18432 	hook_nic_event_t *info;
18433 
18434 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18435 
18436 	ill->ill_unbind_mp = NULL;
18437 	if (mp != NULL) {
18438 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18439 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18440 		    ill->ill_name));
18441 		mutex_enter(&ill->ill_lock);
18442 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18443 		mutex_exit(&ill->ill_lock);
18444 		if (ill->ill_dlpi_capab_state == IDS_OK)
18445 			ill_capability_reset(ill);
18446 		ill_dlpi_send(ill, mp);
18447 	}
18448 
18449 	/*
18450 	 * Toss all of our multicast memberships.  We could keep them, but
18451 	 * then we'd have to do bookkeeping of any joins and leaves performed
18452 	 * by the application while the the interface is down (we can't just
18453 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18454 	 * on a downed interface).
18455 	 */
18456 	ill_leave_multicast(ill);
18457 
18458 	mutex_enter(&ill->ill_lock);
18459 
18460 	ill->ill_dl_up = 0;
18461 
18462 	if ((info = ill->ill_nic_event_info) != NULL) {
18463 		ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n",
18464 		    info->hne_event, ill->ill_name));
18465 		if (info->hne_data != NULL)
18466 			kmem_free(info->hne_data, info->hne_datalen);
18467 		kmem_free(info, sizeof (hook_nic_event_t));
18468 	}
18469 
18470 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
18471 	if (info != NULL) {
18472 		ip_stack_t	*ipst = ill->ill_ipst;
18473 
18474 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
18475 		info->hne_lif = 0;
18476 		info->hne_event = NE_DOWN;
18477 		info->hne_data = NULL;
18478 		info->hne_datalen = 0;
18479 		info->hne_family = ill->ill_isv6 ?
18480 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18481 	} else
18482 		ip2dbg(("ill_dl_down: could not attach DOWN nic event "
18483 		    "information for %s (ENOMEM)\n", ill->ill_name));
18484 
18485 	ill->ill_nic_event_info = info;
18486 
18487 	mutex_exit(&ill->ill_lock);
18488 }
18489 
18490 static void
18491 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18492 {
18493 	union DL_primitives *dlp;
18494 	t_uscalar_t prim;
18495 
18496 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18497 
18498 	dlp = (union DL_primitives *)mp->b_rptr;
18499 	prim = dlp->dl_primitive;
18500 
18501 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18502 	    dlpi_prim_str(prim), prim, ill->ill_name));
18503 
18504 	switch (prim) {
18505 	case DL_PHYS_ADDR_REQ:
18506 	{
18507 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18508 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18509 		break;
18510 	}
18511 	case DL_BIND_REQ:
18512 		mutex_enter(&ill->ill_lock);
18513 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18514 		mutex_exit(&ill->ill_lock);
18515 		break;
18516 	}
18517 
18518 	/*
18519 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18520 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18521 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18522 	 */
18523 	mutex_enter(&ill->ill_lock);
18524 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18525 	    (prim == DL_UNBIND_REQ)) {
18526 		ill->ill_dlpi_pending = prim;
18527 	}
18528 	mutex_exit(&ill->ill_lock);
18529 
18530 	putnext(ill->ill_wq, mp);
18531 }
18532 
18533 /*
18534  * Helper function for ill_dlpi_send().
18535  */
18536 /* ARGSUSED */
18537 static void
18538 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18539 {
18540 	ill_dlpi_send((ill_t *)q->q_ptr, mp);
18541 }
18542 
18543 /*
18544  * Send a DLPI control message to the driver but make sure there
18545  * is only one outstanding message. Uses ill_dlpi_pending to tell
18546  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18547  * when an ACK or a NAK is received to process the next queued message.
18548  */
18549 void
18550 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18551 {
18552 	mblk_t **mpp;
18553 
18554 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18555 
18556 	/*
18557 	 * To ensure that any DLPI requests for current exclusive operation
18558 	 * are always completely sent before any DLPI messages for other
18559 	 * operations, require writer access before enqueuing.
18560 	 */
18561 	if (!IAM_WRITER_ILL(ill)) {
18562 		ill_refhold(ill);
18563 		/* qwriter_ip() does the ill_refrele() */
18564 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18565 		    NEW_OP, B_TRUE);
18566 		return;
18567 	}
18568 
18569 	mutex_enter(&ill->ill_lock);
18570 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18571 		/* Must queue message. Tail insertion */
18572 		mpp = &ill->ill_dlpi_deferred;
18573 		while (*mpp != NULL)
18574 			mpp = &((*mpp)->b_next);
18575 
18576 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18577 		    ill->ill_name));
18578 
18579 		*mpp = mp;
18580 		mutex_exit(&ill->ill_lock);
18581 		return;
18582 	}
18583 	mutex_exit(&ill->ill_lock);
18584 	ill_dlpi_dispatch(ill, mp);
18585 }
18586 
18587 /*
18588  * Send all deferred DLPI messages without waiting for their ACKs.
18589  */
18590 void
18591 ill_dlpi_send_deferred(ill_t *ill)
18592 {
18593 	mblk_t *mp, *nextmp;
18594 
18595 	/*
18596 	 * Clear ill_dlpi_pending so that the message is not queued in
18597 	 * ill_dlpi_send().
18598 	 */
18599 	mutex_enter(&ill->ill_lock);
18600 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18601 	mp = ill->ill_dlpi_deferred;
18602 	ill->ill_dlpi_deferred = NULL;
18603 	mutex_exit(&ill->ill_lock);
18604 
18605 	for (; mp != NULL; mp = nextmp) {
18606 		nextmp = mp->b_next;
18607 		mp->b_next = NULL;
18608 		ill_dlpi_send(ill, mp);
18609 	}
18610 }
18611 
18612 /*
18613  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18614  */
18615 boolean_t
18616 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18617 {
18618 	t_uscalar_t prim_pending;
18619 
18620 	mutex_enter(&ill->ill_lock);
18621 	prim_pending = ill->ill_dlpi_pending;
18622 	mutex_exit(&ill->ill_lock);
18623 
18624 	/*
18625 	 * During teardown, ill_dlpi_send_deferred() will send requests
18626 	 * without waiting; don't bother printing any warnings in that case.
18627 	 */
18628 	if (!(ill->ill_flags & ILL_CONDEMNED) && prim_pending != prim) {
18629 		if (prim_pending == DL_PRIM_INVAL) {
18630 			(void) mi_strlog(ill->ill_rq, 1,
18631 			    SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received "
18632 			    "unsolicited ack for %s on %s\n",
18633 			    dlpi_prim_str(prim), ill->ill_name);
18634 		} else {
18635 			(void) mi_strlog(ill->ill_rq, 1,
18636 			    SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received "
18637 			    "unexpected ack for %s on %s (expecting %s)\n",
18638 			    dlpi_prim_str(prim), ill->ill_name,
18639 			    dlpi_prim_str(prim_pending));
18640 		}
18641 	}
18642 	return (prim_pending == prim);
18643 }
18644 
18645 /*
18646  * Called when an DLPI control message has been acked or nacked to
18647  * send down the next queued message (if any).
18648  */
18649 void
18650 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18651 {
18652 	mblk_t *mp;
18653 
18654 	ASSERT(IAM_WRITER_ILL(ill));
18655 	mutex_enter(&ill->ill_lock);
18656 
18657 	ASSERT(prim != DL_PRIM_INVAL);
18658 	ASSERT(ill->ill_dlpi_pending == prim);
18659 
18660 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18661 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18662 
18663 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18664 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18665 		cv_signal(&ill->ill_cv);
18666 		mutex_exit(&ill->ill_lock);
18667 		return;
18668 	}
18669 
18670 	ill->ill_dlpi_deferred = mp->b_next;
18671 	mp->b_next = NULL;
18672 	mutex_exit(&ill->ill_lock);
18673 
18674 	ill_dlpi_dispatch(ill, mp);
18675 }
18676 
18677 void
18678 conn_delete_ire(conn_t *connp, caddr_t arg)
18679 {
18680 	ipif_t	*ipif = (ipif_t *)arg;
18681 	ire_t	*ire;
18682 
18683 	/*
18684 	 * Look at the cached ires on conns which has pointers to ipifs.
18685 	 * We just call ire_refrele which clears up the reference
18686 	 * to ire. Called when a conn closes. Also called from ipif_free
18687 	 * to cleanup indirect references to the stale ipif via the cached ire.
18688 	 */
18689 	mutex_enter(&connp->conn_lock);
18690 	ire = connp->conn_ire_cache;
18691 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18692 		connp->conn_ire_cache = NULL;
18693 		mutex_exit(&connp->conn_lock);
18694 		IRE_REFRELE_NOTR(ire);
18695 		return;
18696 	}
18697 	mutex_exit(&connp->conn_lock);
18698 
18699 }
18700 
18701 /*
18702  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18703  * of IREs. Those IREs may have been previously cached in the conn structure.
18704  * This ipcl_walk() walker function releases all references to such IREs based
18705  * on the condemned flag.
18706  */
18707 /* ARGSUSED */
18708 void
18709 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18710 {
18711 	ire_t	*ire;
18712 
18713 	mutex_enter(&connp->conn_lock);
18714 	ire = connp->conn_ire_cache;
18715 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18716 		connp->conn_ire_cache = NULL;
18717 		mutex_exit(&connp->conn_lock);
18718 		IRE_REFRELE_NOTR(ire);
18719 		return;
18720 	}
18721 	mutex_exit(&connp->conn_lock);
18722 }
18723 
18724 /*
18725  * Take down a specific interface, but don't lose any information about it.
18726  * Also delete interface from its interface group (ifgrp).
18727  * (Always called as writer.)
18728  * This function goes through the down sequence even if the interface is
18729  * already down. There are 2 reasons.
18730  * a. Currently we permit interface routes that depend on down interfaces
18731  *    to be added. This behaviour itself is questionable. However it appears
18732  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18733  *    time. We go thru the cleanup in order to remove these routes.
18734  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18735  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18736  *    down, but we need to cleanup i.e. do ill_dl_down and
18737  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18738  *
18739  * IP-MT notes:
18740  *
18741  * Model of reference to interfaces.
18742  *
18743  * The following members in ipif_t track references to the ipif.
18744  *	int     ipif_refcnt;    Active reference count
18745  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18746  * The following members in ill_t track references to the ill.
18747  *	int             ill_refcnt;     active refcnt
18748  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18749  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18750  *
18751  * Reference to an ipif or ill can be obtained in any of the following ways.
18752  *
18753  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18754  * Pointers to ipif / ill from other data structures viz ire and conn.
18755  * Implicit reference to the ipif / ill by holding a reference to the ire.
18756  *
18757  * The ipif/ill lookup functions return a reference held ipif / ill.
18758  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18759  * This is a purely dynamic reference count associated with threads holding
18760  * references to the ipif / ill. Pointers from other structures do not
18761  * count towards this reference count.
18762  *
18763  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18764  * ipif/ill. This is incremented whenever a new ire is created referencing the
18765  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18766  * actually added to the ire hash table. The count is decremented in
18767  * ire_inactive where the ire is destroyed.
18768  *
18769  * nce's reference ill's thru nce_ill and the count of nce's associated with
18770  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18771  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18772  * table. Similarly it is decremented in ndp_inactive() where the nce
18773  * is destroyed.
18774  *
18775  * Flow of ioctls involving interface down/up
18776  *
18777  * The following is the sequence of an attempt to set some critical flags on an
18778  * up interface.
18779  * ip_sioctl_flags
18780  * ipif_down
18781  * wait for ipif to be quiescent
18782  * ipif_down_tail
18783  * ip_sioctl_flags_tail
18784  *
18785  * All set ioctls that involve down/up sequence would have a skeleton similar
18786  * to the above. All the *tail functions are called after the refcounts have
18787  * dropped to the appropriate values.
18788  *
18789  * The mechanism to quiesce an ipif is as follows.
18790  *
18791  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18792  * on the ipif. Callers either pass a flag requesting wait or the lookup
18793  *  functions will return NULL.
18794  *
18795  * Delete all ires referencing this ipif
18796  *
18797  * Any thread attempting to do an ipif_refhold on an ipif that has been
18798  * obtained thru a cached pointer will first make sure that
18799  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18800  * increment the refcount.
18801  *
18802  * The above guarantees that the ipif refcount will eventually come down to
18803  * zero and the ipif will quiesce, once all threads that currently hold a
18804  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18805  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18806  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
18807  * drop to zero.
18808  *
18809  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18810  *
18811  * Threads trying to lookup an ipif or ill can pass a flag requesting
18812  * wait and restart if the ipif / ill cannot be looked up currently.
18813  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18814  * failure if the ipif is currently undergoing an exclusive operation, and
18815  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18816  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18817  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18818  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18819  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18820  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18821  * until we release the ipsq_lock, even though the the ill/ipif state flags
18822  * can change after we drop the ill_lock.
18823  *
18824  * An attempt to send out a packet using an ipif that is currently
18825  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18826  * operation and restart it later when the exclusive condition on the ipif ends.
18827  * This is an example of not passing the wait flag to the lookup functions. For
18828  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18829  * out a multicast packet on that ipif will fail while the ipif is
18830  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18831  * currently IPIF_CHANGING will also fail.
18832  */
18833 int
18834 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18835 {
18836 	ill_t		*ill = ipif->ipif_ill;
18837 	phyint_t	*phyi;
18838 	conn_t		*connp;
18839 	boolean_t	success;
18840 	boolean_t	ipif_was_up = B_FALSE;
18841 	ip_stack_t	*ipst = ill->ill_ipst;
18842 
18843 	ASSERT(IAM_WRITER_IPIF(ipif));
18844 
18845 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18846 
18847 	if (ipif->ipif_flags & IPIF_UP) {
18848 		mutex_enter(&ill->ill_lock);
18849 		ipif->ipif_flags &= ~IPIF_UP;
18850 		ASSERT(ill->ill_ipif_up_count > 0);
18851 		--ill->ill_ipif_up_count;
18852 		mutex_exit(&ill->ill_lock);
18853 		ipif_was_up = B_TRUE;
18854 		/* Update status in SCTP's list */
18855 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18856 	}
18857 
18858 	/*
18859 	 * Blow away memberships we established in ipif_multicast_up().
18860 	 */
18861 	ipif_multicast_down(ipif);
18862 
18863 	/*
18864 	 * Remove from the mapping for __sin6_src_id. We insert only
18865 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18866 	 * stored as mapped addresses, we need to check for mapped
18867 	 * INADDR_ANY also.
18868 	 */
18869 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18870 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18871 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18872 		int err;
18873 
18874 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18875 		    ipif->ipif_zoneid, ipst);
18876 		if (err != 0) {
18877 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18878 		}
18879 	}
18880 
18881 	/*
18882 	 * Before we delete the ill from the group (if any), we need
18883 	 * to make sure that we delete all the routes dependent on
18884 	 * this and also any ipifs dependent on this ipif for
18885 	 * source address. We need to do before we delete from
18886 	 * the group because
18887 	 *
18888 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18889 	 *
18890 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18891 	 *    for re-doing source address selection. Note that
18892 	 *    ipif_select_source[_v6] called from
18893 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18894 	 *    because we have already marked down here i.e cleared
18895 	 *    IPIF_UP.
18896 	 */
18897 	if (ipif->ipif_isv6) {
18898 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18899 		    ipst);
18900 	} else {
18901 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18902 		    ipst);
18903 	}
18904 
18905 	/*
18906 	 * Need to add these also to be saved and restored when the
18907 	 * ipif is brought down and up
18908 	 */
18909 	mutex_enter(&ipst->ips_ire_mrtun_lock);
18910 	if (ipst->ips_ire_mrtun_count != 0) {
18911 		mutex_exit(&ipst->ips_ire_mrtun_lock);
18912 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
18913 		    (char *)ipif, NULL, ipst);
18914 	} else {
18915 		mutex_exit(&ipst->ips_ire_mrtun_lock);
18916 	}
18917 
18918 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
18919 	if (ipst->ips_ire_srcif_table_count > 0) {
18920 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
18921 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif,
18922 		    ipst);
18923 	} else {
18924 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
18925 	}
18926 
18927 	/*
18928 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18929 	 * ires have been deleted above. Otherwise a thread could end up
18930 	 * caching an ire in a conn after we have finished the cleanup of the
18931 	 * conn. The caching is done after making sure that the ire is not yet
18932 	 * condemned. Also documented in the block comment above ip_output
18933 	 */
18934 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18935 	/* Also, delete the ires cached in SCTP */
18936 	sctp_ire_cache_flush(ipif);
18937 
18938 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
18939 	nattymod_clean_ipif(ipif);
18940 
18941 	/*
18942 	 * Update any other ipifs which have used "our" local address as
18943 	 * a source address. This entails removing and recreating IRE_INTERFACE
18944 	 * entries for such ipifs.
18945 	 */
18946 	if (ipif->ipif_isv6)
18947 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18948 	else
18949 		ipif_update_other_ipifs(ipif, ill->ill_group);
18950 
18951 	if (ipif_was_up) {
18952 		/*
18953 		 * Check whether it is last ipif to leave this group.
18954 		 * If this is the last ipif to leave, we should remove
18955 		 * this ill from the group as ipif_select_source will not
18956 		 * be able to find any useful ipifs if this ill is selected
18957 		 * for load balancing.
18958 		 *
18959 		 * For nameless groups, we should call ifgrp_delete if this
18960 		 * belongs to some group. As this ipif is going down, we may
18961 		 * need to reconstruct groups.
18962 		 */
18963 		phyi = ill->ill_phyint;
18964 		/*
18965 		 * If the phyint_groupname_len is 0, it may or may not
18966 		 * be in the nameless group. If the phyint_groupname_len is
18967 		 * not 0, then this ill should be part of some group.
18968 		 * As we always insert this ill in the group if
18969 		 * phyint_groupname_len is not zero when the first ipif
18970 		 * comes up (in ipif_up_done), it should be in a group
18971 		 * when the namelen is not 0.
18972 		 *
18973 		 * NOTE : When we delete the ill from the group,it will
18974 		 * blow away all the IRE_CACHES pointing either at this ipif or
18975 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18976 		 * should be pointing at this ill.
18977 		 */
18978 		ASSERT(phyi->phyint_groupname_len == 0 ||
18979 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18980 
18981 		if (phyi->phyint_groupname_len != 0) {
18982 			if (ill->ill_ipif_up_count == 0)
18983 				illgrp_delete(ill);
18984 		}
18985 
18986 		/*
18987 		 * If we have deleted some of the broadcast ires associated
18988 		 * with this ipif, we need to re-nominate somebody else if
18989 		 * the ires that we deleted were the nominated ones.
18990 		 */
18991 		if (ill->ill_group != NULL && !ill->ill_isv6)
18992 			ipif_renominate_bcast(ipif);
18993 	}
18994 
18995 	/*
18996 	 * neighbor-discovery or arp entries for this interface.
18997 	 */
18998 	ipif_ndp_down(ipif);
18999 
19000 	/*
19001 	 * If mp is NULL the caller will wait for the appropriate refcnt.
19002 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
19003 	 * and ill_delete -> ipif_free -> ipif_down
19004 	 */
19005 	if (mp == NULL) {
19006 		ASSERT(q == NULL);
19007 		return (0);
19008 	}
19009 
19010 	if (CONN_Q(q)) {
19011 		connp = Q_TO_CONN(q);
19012 		mutex_enter(&connp->conn_lock);
19013 	} else {
19014 		connp = NULL;
19015 	}
19016 	mutex_enter(&ill->ill_lock);
19017 	/*
19018 	 * Are there any ire's pointing to this ipif that are still active ?
19019 	 * If this is the last ipif going down, are there any ire's pointing
19020 	 * to this ill that are still active ?
19021 	 */
19022 	if (ipif_is_quiescent(ipif)) {
19023 		mutex_exit(&ill->ill_lock);
19024 		if (connp != NULL)
19025 			mutex_exit(&connp->conn_lock);
19026 		return (0);
19027 	}
19028 
19029 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
19030 	    ill->ill_name, (void *)ill));
19031 	/*
19032 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
19033 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
19034 	 * which in turn is called by the last refrele on the ipif/ill/ire.
19035 	 */
19036 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
19037 	if (!success) {
19038 		/* The conn is closing. So just return */
19039 		ASSERT(connp != NULL);
19040 		mutex_exit(&ill->ill_lock);
19041 		mutex_exit(&connp->conn_lock);
19042 		return (EINTR);
19043 	}
19044 
19045 	mutex_exit(&ill->ill_lock);
19046 	if (connp != NULL)
19047 		mutex_exit(&connp->conn_lock);
19048 	return (EINPROGRESS);
19049 }
19050 
19051 void
19052 ipif_down_tail(ipif_t *ipif)
19053 {
19054 	ill_t	*ill = ipif->ipif_ill;
19055 
19056 	/*
19057 	 * Skip any loopback interface (null wq).
19058 	 * If this is the last logical interface on the ill
19059 	 * have ill_dl_down tell the driver we are gone (unbind)
19060 	 * Note that lun 0 can ipif_down even though
19061 	 * there are other logical units that are up.
19062 	 * This occurs e.g. when we change a "significant" IFF_ flag.
19063 	 */
19064 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
19065 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
19066 	    ill->ill_dl_up) {
19067 		ill_dl_down(ill);
19068 	}
19069 	ill->ill_logical_down = 0;
19070 
19071 	/*
19072 	 * Have to be after removing the routes in ipif_down_delete_ire.
19073 	 */
19074 	if (ipif->ipif_isv6) {
19075 		if (ill->ill_flags & ILLF_XRESOLV)
19076 			ipif_arp_down(ipif);
19077 	} else {
19078 		ipif_arp_down(ipif);
19079 	}
19080 
19081 	ip_rts_ifmsg(ipif);
19082 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
19083 }
19084 
19085 /*
19086  * Bring interface logically down without bringing the physical interface
19087  * down e.g. when the netmask is changed. This avoids long lasting link
19088  * negotiations between an ethernet interface and a certain switches.
19089  */
19090 static int
19091 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
19092 {
19093 	/*
19094 	 * The ill_logical_down flag is a transient flag. It is set here
19095 	 * and is cleared once the down has completed in ipif_down_tail.
19096 	 * This flag does not indicate whether the ill stream is in the
19097 	 * DL_BOUND state with the driver. Instead this flag is used by
19098 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
19099 	 * the driver. The state of the ill stream i.e. whether it is
19100 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
19101 	 */
19102 	ipif->ipif_ill->ill_logical_down = 1;
19103 	return (ipif_down(ipif, q, mp));
19104 }
19105 
19106 /*
19107  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
19108  * If the usesrc client ILL is already part of a usesrc group or not,
19109  * in either case a ire_stq with the matching usesrc client ILL will
19110  * locate the IRE's that need to be deleted. We want IREs to be created
19111  * with the new source address.
19112  */
19113 static void
19114 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
19115 {
19116 	ill_t	*ucill = (ill_t *)ill_arg;
19117 
19118 	ASSERT(IAM_WRITER_ILL(ucill));
19119 
19120 	if (ire->ire_stq == NULL)
19121 		return;
19122 
19123 	if ((ire->ire_type == IRE_CACHE) &&
19124 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
19125 		ire_delete(ire);
19126 }
19127 
19128 /*
19129  * ire_walk routine to delete every IRE dependent on the interface
19130  * address that is going down.	(Always called as writer.)
19131  * Works for both v4 and v6.
19132  * In addition for checking for ire_ipif matches it also checks for
19133  * IRE_CACHE entries which have the same source address as the
19134  * disappearing ipif since ipif_select_source might have picked
19135  * that source. Note that ipif_down/ipif_update_other_ipifs takes
19136  * care of any IRE_INTERFACE with the disappearing source address.
19137  */
19138 static void
19139 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
19140 {
19141 	ipif_t	*ipif = (ipif_t *)ipif_arg;
19142 	ill_t *ire_ill;
19143 	ill_t *ipif_ill;
19144 
19145 	ASSERT(IAM_WRITER_IPIF(ipif));
19146 	if (ire->ire_ipif == NULL)
19147 		return;
19148 
19149 	/*
19150 	 * For IPv4, we derive source addresses for an IRE from ipif's
19151 	 * belonging to the same IPMP group as the IRE's outgoing
19152 	 * interface.  If an IRE's outgoing interface isn't in the
19153 	 * same IPMP group as a particular ipif, then that ipif
19154 	 * couldn't have been used as a source address for this IRE.
19155 	 *
19156 	 * For IPv6, source addresses are only restricted to the IPMP group
19157 	 * if the IRE is for a link-local address or a multicast address.
19158 	 * Otherwise, source addresses for an IRE can be chosen from
19159 	 * interfaces other than the the outgoing interface for that IRE.
19160 	 *
19161 	 * For source address selection details, see ipif_select_source()
19162 	 * and ipif_select_source_v6().
19163 	 */
19164 	if (ire->ire_ipversion == IPV4_VERSION ||
19165 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
19166 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
19167 		ire_ill = ire->ire_ipif->ipif_ill;
19168 		ipif_ill = ipif->ipif_ill;
19169 
19170 		if (ire_ill->ill_group != ipif_ill->ill_group) {
19171 			return;
19172 		}
19173 	}
19174 
19175 
19176 	if (ire->ire_ipif != ipif) {
19177 		/*
19178 		 * Look for a matching source address.
19179 		 */
19180 		if (ire->ire_type != IRE_CACHE)
19181 			return;
19182 		if (ipif->ipif_flags & IPIF_NOLOCAL)
19183 			return;
19184 
19185 		if (ire->ire_ipversion == IPV4_VERSION) {
19186 			if (ire->ire_src_addr != ipif->ipif_src_addr)
19187 				return;
19188 		} else {
19189 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
19190 			    &ipif->ipif_v6lcl_addr))
19191 				return;
19192 		}
19193 		ire_delete(ire);
19194 		return;
19195 	}
19196 	/*
19197 	 * ire_delete() will do an ire_flush_cache which will delete
19198 	 * all ire_ipif matches
19199 	 */
19200 	ire_delete(ire);
19201 }
19202 
19203 /*
19204  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
19205  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
19206  * 2) when an interface is brought up or down (on that ill).
19207  * This ensures that the IRE_CACHE entries don't retain stale source
19208  * address selection results.
19209  */
19210 void
19211 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
19212 {
19213 	ill_t	*ill = (ill_t *)ill_arg;
19214 	ill_t	*ipif_ill;
19215 
19216 	ASSERT(IAM_WRITER_ILL(ill));
19217 	/*
19218 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19219 	 * Hence this should be IRE_CACHE.
19220 	 */
19221 	ASSERT(ire->ire_type == IRE_CACHE);
19222 
19223 	/*
19224 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
19225 	 * We are only interested in IRE_CACHES that has borrowed
19226 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
19227 	 * for which we need to look at ire_ipif->ipif_ill match
19228 	 * with ill.
19229 	 */
19230 	ASSERT(ire->ire_ipif != NULL);
19231 	ipif_ill = ire->ire_ipif->ipif_ill;
19232 	if (ipif_ill == ill || (ill->ill_group != NULL &&
19233 	    ipif_ill->ill_group == ill->ill_group)) {
19234 		ire_delete(ire);
19235 	}
19236 }
19237 
19238 /*
19239  * Delete all the ire whose stq references ill_arg.
19240  */
19241 static void
19242 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
19243 {
19244 	ill_t	*ill = (ill_t *)ill_arg;
19245 	ill_t	*ire_ill;
19246 
19247 	ASSERT(IAM_WRITER_ILL(ill));
19248 	/*
19249 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19250 	 * Hence this should be IRE_CACHE.
19251 	 */
19252 	ASSERT(ire->ire_type == IRE_CACHE);
19253 
19254 	/*
19255 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19256 	 * matches ill. We are only interested in IRE_CACHES that
19257 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19258 	 * filtering here.
19259 	 */
19260 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19261 
19262 	if (ire_ill == ill)
19263 		ire_delete(ire);
19264 }
19265 
19266 /*
19267  * This is called when an ill leaves the group. We want to delete
19268  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19269  * pointing at ill.
19270  */
19271 static void
19272 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19273 {
19274 	ill_t	*ill = (ill_t *)ill_arg;
19275 
19276 	ASSERT(IAM_WRITER_ILL(ill));
19277 	ASSERT(ill->ill_group == NULL);
19278 	/*
19279 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19280 	 * Hence this should be IRE_CACHE.
19281 	 */
19282 	ASSERT(ire->ire_type == IRE_CACHE);
19283 	/*
19284 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19285 	 * matches ill. We are interested in both.
19286 	 */
19287 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19288 	    (ire->ire_ipif->ipif_ill == ill));
19289 
19290 	ire_delete(ire);
19291 }
19292 
19293 /*
19294  * Initiate deallocate of an IPIF. Always called as writer. Called by
19295  * ill_delete or ip_sioctl_removeif.
19296  */
19297 static void
19298 ipif_free(ipif_t *ipif)
19299 {
19300 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19301 
19302 	ASSERT(IAM_WRITER_IPIF(ipif));
19303 
19304 	if (ipif->ipif_recovery_id != 0)
19305 		(void) untimeout(ipif->ipif_recovery_id);
19306 	ipif->ipif_recovery_id = 0;
19307 
19308 	/* Remove conn references */
19309 	reset_conn_ipif(ipif);
19310 
19311 	/*
19312 	 * Make sure we have valid net and subnet broadcast ire's for the
19313 	 * other ipif's which share them with this ipif.
19314 	 */
19315 	if (!ipif->ipif_isv6)
19316 		ipif_check_bcast_ires(ipif);
19317 
19318 	/*
19319 	 * Take down the interface. We can be called either from ill_delete
19320 	 * or from ip_sioctl_removeif.
19321 	 */
19322 	(void) ipif_down(ipif, NULL, NULL);
19323 
19324 	/*
19325 	 * Now that the interface is down, there's no chance it can still
19326 	 * become a duplicate.  Cancel any timer that may have been set while
19327 	 * tearing down.
19328 	 */
19329 	if (ipif->ipif_recovery_id != 0)
19330 		(void) untimeout(ipif->ipif_recovery_id);
19331 	ipif->ipif_recovery_id = 0;
19332 
19333 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19334 	/* Remove pointers to this ill in the multicast routing tables */
19335 	reset_mrt_vif_ipif(ipif);
19336 	rw_exit(&ipst->ips_ill_g_lock);
19337 }
19338 
19339 /*
19340  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19341  * also ill_move().
19342  */
19343 static void
19344 ipif_free_tail(ipif_t *ipif)
19345 {
19346 	mblk_t	*mp;
19347 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19348 
19349 	/*
19350 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19351 	 */
19352 	mutex_enter(&ipif->ipif_saved_ire_lock);
19353 	mp = ipif->ipif_saved_ire_mp;
19354 	ipif->ipif_saved_ire_mp = NULL;
19355 	mutex_exit(&ipif->ipif_saved_ire_lock);
19356 	freemsg(mp);
19357 
19358 	/*
19359 	 * Need to hold both ill_g_lock and ill_lock while
19360 	 * inserting or removing an ipif from the linked list
19361 	 * of ipifs hanging off the ill.
19362 	 */
19363 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19364 	/*
19365 	 * Remove all IPv4 multicast memberships on the interface now.
19366 	 * IPv6 is not handled here as the multicast memberships are
19367 	 * tied to the ill rather than the ipif.
19368 	 */
19369 	ilm_free(ipif);
19370 
19371 	/*
19372 	 * Since we held the ill_g_lock while doing the ilm_free above,
19373 	 * we can assert the ilms were really deleted and not just marked
19374 	 * ILM_DELETED.
19375 	 */
19376 	ASSERT(ilm_walk_ipif(ipif) == 0);
19377 
19378 	IPIF_TRACE_CLEANUP(ipif);
19379 
19380 	/* Ask SCTP to take it out of it list */
19381 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19382 
19383 	/* Get it out of the ILL interface list. */
19384 	ipif_remove(ipif, B_TRUE);
19385 	rw_exit(&ipst->ips_ill_g_lock);
19386 
19387 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19388 
19389 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19390 	ASSERT(ipif->ipif_recovery_id == 0);
19391 
19392 	/* Free the memory. */
19393 	mi_free(ipif);
19394 }
19395 
19396 /*
19397  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
19398  * "ill_name" otherwise.
19399  */
19400 char *
19401 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19402 {
19403 	char	lbuf[32];
19404 	char	*name;
19405 	size_t	name_len;
19406 
19407 	buf[0] = '\0';
19408 	if (!ipif)
19409 		return (buf);
19410 	name = ipif->ipif_ill->ill_name;
19411 	name_len = ipif->ipif_ill->ill_name_length;
19412 	if (ipif->ipif_id != 0) {
19413 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19414 		    ipif->ipif_id);
19415 		name = lbuf;
19416 		name_len = mi_strlen(name) + 1;
19417 	}
19418 	len -= 1;
19419 	buf[len] = '\0';
19420 	len = MIN(len, name_len);
19421 	bcopy(name, buf, len);
19422 	return (buf);
19423 }
19424 
19425 /*
19426  * Find an IPIF based on the name passed in.  Names can be of the
19427  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19428  * The <phys> string can have forms like <dev><#> (e.g., le0),
19429  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19430  * When there is no colon, the implied unit id is zero. <phys> must
19431  * correspond to the name of an ILL.  (May be called as writer.)
19432  */
19433 static ipif_t *
19434 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19435     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19436     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19437 {
19438 	char	*cp;
19439 	char	*endp;
19440 	long	id;
19441 	ill_t	*ill;
19442 	ipif_t	*ipif;
19443 	uint_t	ire_type;
19444 	boolean_t did_alloc = B_FALSE;
19445 	ipsq_t	*ipsq;
19446 
19447 	if (error != NULL)
19448 		*error = 0;
19449 
19450 	/*
19451 	 * If the caller wants to us to create the ipif, make sure we have a
19452 	 * valid zoneid
19453 	 */
19454 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19455 
19456 	if (namelen == 0) {
19457 		if (error != NULL)
19458 			*error = ENXIO;
19459 		return (NULL);
19460 	}
19461 
19462 	*exists = B_FALSE;
19463 	/* Look for a colon in the name. */
19464 	endp = &name[namelen];
19465 	for (cp = endp; --cp > name; ) {
19466 		if (*cp == IPIF_SEPARATOR_CHAR)
19467 			break;
19468 	}
19469 
19470 	if (*cp == IPIF_SEPARATOR_CHAR) {
19471 		/*
19472 		 * Reject any non-decimal aliases for logical
19473 		 * interfaces. Aliases with leading zeroes
19474 		 * are also rejected as they introduce ambiguity
19475 		 * in the naming of the interfaces.
19476 		 * In order to confirm with existing semantics,
19477 		 * and to not break any programs/script relying
19478 		 * on that behaviour, if<0>:0 is considered to be
19479 		 * a valid interface.
19480 		 *
19481 		 * If alias has two or more digits and the first
19482 		 * is zero, fail.
19483 		 */
19484 		if (&cp[2] < endp && cp[1] == '0')
19485 			return (NULL);
19486 	}
19487 
19488 	if (cp <= name) {
19489 		cp = endp;
19490 	} else {
19491 		*cp = '\0';
19492 	}
19493 
19494 	/*
19495 	 * Look up the ILL, based on the portion of the name
19496 	 * before the slash. ill_lookup_on_name returns a held ill.
19497 	 * Temporary to check whether ill exists already. If so
19498 	 * ill_lookup_on_name will clear it.
19499 	 */
19500 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19501 	    q, mp, func, error, &did_alloc, ipst);
19502 	if (cp != endp)
19503 		*cp = IPIF_SEPARATOR_CHAR;
19504 	if (ill == NULL)
19505 		return (NULL);
19506 
19507 	/* Establish the unit number in the name. */
19508 	id = 0;
19509 	if (cp < endp && *endp == '\0') {
19510 		/* If there was a colon, the unit number follows. */
19511 		cp++;
19512 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19513 			ill_refrele(ill);
19514 			if (error != NULL)
19515 				*error = ENXIO;
19516 			return (NULL);
19517 		}
19518 	}
19519 
19520 	GRAB_CONN_LOCK(q);
19521 	mutex_enter(&ill->ill_lock);
19522 	/* Now see if there is an IPIF with this unit number. */
19523 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19524 		if (ipif->ipif_id == id) {
19525 			if (zoneid != ALL_ZONES &&
19526 			    zoneid != ipif->ipif_zoneid &&
19527 			    ipif->ipif_zoneid != ALL_ZONES) {
19528 				mutex_exit(&ill->ill_lock);
19529 				RELEASE_CONN_LOCK(q);
19530 				ill_refrele(ill);
19531 				if (error != NULL)
19532 					*error = ENXIO;
19533 				return (NULL);
19534 			}
19535 			/*
19536 			 * The block comment at the start of ipif_down
19537 			 * explains the use of the macros used below
19538 			 */
19539 			if (IPIF_CAN_LOOKUP(ipif)) {
19540 				ipif_refhold_locked(ipif);
19541 				mutex_exit(&ill->ill_lock);
19542 				if (!did_alloc)
19543 					*exists = B_TRUE;
19544 				/*
19545 				 * Drop locks before calling ill_refrele
19546 				 * since it can potentially call into
19547 				 * ipif_ill_refrele_tail which can end up
19548 				 * in trying to acquire any lock.
19549 				 */
19550 				RELEASE_CONN_LOCK(q);
19551 				ill_refrele(ill);
19552 				return (ipif);
19553 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19554 				ipsq = ill->ill_phyint->phyint_ipsq;
19555 				mutex_enter(&ipsq->ipsq_lock);
19556 				mutex_exit(&ill->ill_lock);
19557 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19558 				mutex_exit(&ipsq->ipsq_lock);
19559 				RELEASE_CONN_LOCK(q);
19560 				ill_refrele(ill);
19561 				*error = EINPROGRESS;
19562 				return (NULL);
19563 			}
19564 		}
19565 	}
19566 	RELEASE_CONN_LOCK(q);
19567 
19568 	if (!do_alloc) {
19569 		mutex_exit(&ill->ill_lock);
19570 		ill_refrele(ill);
19571 		if (error != NULL)
19572 			*error = ENXIO;
19573 		return (NULL);
19574 	}
19575 
19576 	/*
19577 	 * If none found, atomically allocate and return a new one.
19578 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19579 	 * to support "receive only" use of lo0:1 etc. as is still done
19580 	 * below as an initial guess.
19581 	 * However, this is now likely to be overriden later in ipif_up_done()
19582 	 * when we know for sure what address has been configured on the
19583 	 * interface, since we might have more than one loopback interface
19584 	 * with a loopback address, e.g. in the case of zones, and all the
19585 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19586 	 */
19587 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19588 		ire_type = IRE_LOOPBACK;
19589 	else
19590 		ire_type = IRE_LOCAL;
19591 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19592 	if (ipif != NULL)
19593 		ipif_refhold_locked(ipif);
19594 	else if (error != NULL)
19595 		*error = ENOMEM;
19596 	mutex_exit(&ill->ill_lock);
19597 	ill_refrele(ill);
19598 	return (ipif);
19599 }
19600 
19601 /*
19602  * This routine is called whenever a new address comes up on an ipif.  If
19603  * we are configured to respond to address mask requests, then we are supposed
19604  * to broadcast an address mask reply at this time.  This routine is also
19605  * called if we are already up, but a netmask change is made.  This is legal
19606  * but might not make the system manager very popular.	(May be called
19607  * as writer.)
19608  */
19609 void
19610 ipif_mask_reply(ipif_t *ipif)
19611 {
19612 	icmph_t	*icmph;
19613 	ipha_t	*ipha;
19614 	mblk_t	*mp;
19615 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19616 
19617 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19618 
19619 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19620 		return;
19621 
19622 	/* ICMP mask reply is IPv4 only */
19623 	ASSERT(!ipif->ipif_isv6);
19624 	/* ICMP mask reply is not for a loopback interface */
19625 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19626 
19627 	mp = allocb(REPLY_LEN, BPRI_HI);
19628 	if (mp == NULL)
19629 		return;
19630 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19631 
19632 	ipha = (ipha_t *)mp->b_rptr;
19633 	bzero(ipha, REPLY_LEN);
19634 	*ipha = icmp_ipha;
19635 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19636 	ipha->ipha_src = ipif->ipif_src_addr;
19637 	ipha->ipha_dst = ipif->ipif_brd_addr;
19638 	ipha->ipha_length = htons(REPLY_LEN);
19639 	ipha->ipha_ident = 0;
19640 
19641 	icmph = (icmph_t *)&ipha[1];
19642 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19643 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19644 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19645 
19646 	put(ipif->ipif_wq, mp);
19647 
19648 #undef	REPLY_LEN
19649 }
19650 
19651 /*
19652  * When the mtu in the ipif changes, we call this routine through ire_walk
19653  * to update all the relevant IREs.
19654  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19655  */
19656 static void
19657 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19658 {
19659 	ipif_t *ipif = (ipif_t *)ipif_arg;
19660 
19661 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19662 		return;
19663 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19664 }
19665 
19666 /*
19667  * When the mtu in the ill changes, we call this routine through ire_walk
19668  * to update all the relevant IREs.
19669  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19670  */
19671 void
19672 ill_mtu_change(ire_t *ire, char *ill_arg)
19673 {
19674 	ill_t	*ill = (ill_t *)ill_arg;
19675 
19676 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19677 		return;
19678 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19679 }
19680 
19681 /*
19682  * Join the ipif specific multicast groups.
19683  * Must be called after a mapping has been set up in the resolver.  (Always
19684  * called as writer.)
19685  */
19686 void
19687 ipif_multicast_up(ipif_t *ipif)
19688 {
19689 	int err, index;
19690 	ill_t *ill;
19691 
19692 	ASSERT(IAM_WRITER_IPIF(ipif));
19693 
19694 	ill = ipif->ipif_ill;
19695 	index = ill->ill_phyint->phyint_ifindex;
19696 
19697 	ip1dbg(("ipif_multicast_up\n"));
19698 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19699 		return;
19700 
19701 	if (ipif->ipif_isv6) {
19702 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19703 			return;
19704 
19705 		/* Join the all hosts multicast address */
19706 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19707 		/*
19708 		 * Passing B_TRUE means we have to join the multicast
19709 		 * membership on this interface even though this is
19710 		 * FAILED. If we join on a different one in the group,
19711 		 * we will not be able to delete the membership later
19712 		 * as we currently don't track where we join when we
19713 		 * join within the kernel unlike applications where
19714 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19715 		 * for more on this.
19716 		 */
19717 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19718 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19719 		if (err != 0) {
19720 			ip0dbg(("ipif_multicast_up: "
19721 			    "all_hosts_mcast failed %d\n",
19722 			    err));
19723 			return;
19724 		}
19725 		/*
19726 		 * Enable multicast for the solicited node multicast address
19727 		 */
19728 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19729 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19730 
19731 			ipv6_multi.s6_addr32[3] |=
19732 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19733 
19734 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19735 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19736 			    NULL);
19737 			if (err != 0) {
19738 				ip0dbg(("ipif_multicast_up: solicited MC"
19739 				    " failed %d\n", err));
19740 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19741 				    ill, ill->ill_phyint->phyint_ifindex,
19742 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19743 				return;
19744 			}
19745 		}
19746 	} else {
19747 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19748 			return;
19749 
19750 		/* Join the all hosts multicast address */
19751 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19752 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19753 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19754 		if (err) {
19755 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19756 			return;
19757 		}
19758 	}
19759 	ipif->ipif_multicast_up = 1;
19760 }
19761 
19762 /*
19763  * Blow away any multicast groups that we joined in ipif_multicast_up().
19764  * (Explicit memberships are blown away in ill_leave_multicast() when the
19765  * ill is brought down.)
19766  */
19767 static void
19768 ipif_multicast_down(ipif_t *ipif)
19769 {
19770 	int err;
19771 
19772 	ASSERT(IAM_WRITER_IPIF(ipif));
19773 
19774 	ip1dbg(("ipif_multicast_down\n"));
19775 	if (!ipif->ipif_multicast_up)
19776 		return;
19777 
19778 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19779 
19780 	if (!ipif->ipif_isv6) {
19781 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19782 		    B_TRUE);
19783 		if (err != 0)
19784 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19785 
19786 		ipif->ipif_multicast_up = 0;
19787 		return;
19788 	}
19789 
19790 	/*
19791 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19792 	 * we should look for ilms on this ill rather than the ones that have
19793 	 * been failed over here.  They are here temporarily. As
19794 	 * ipif_multicast_up has joined on this ill, we should delete only
19795 	 * from this ill.
19796 	 */
19797 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19798 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19799 	    B_TRUE, B_TRUE);
19800 	if (err != 0) {
19801 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19802 		    err));
19803 	}
19804 	/*
19805 	 * Disable multicast for the solicited node multicast address
19806 	 */
19807 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19808 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19809 
19810 		ipv6_multi.s6_addr32[3] |=
19811 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19812 
19813 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19814 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19815 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19816 
19817 		if (err != 0) {
19818 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19819 			    err));
19820 		}
19821 	}
19822 
19823 	ipif->ipif_multicast_up = 0;
19824 }
19825 
19826 /*
19827  * Used when an interface comes up to recreate any extra routes on this
19828  * interface.
19829  */
19830 static ire_t **
19831 ipif_recover_ire(ipif_t *ipif)
19832 {
19833 	mblk_t	*mp;
19834 	ire_t	**ipif_saved_irep;
19835 	ire_t	**irep;
19836 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19837 
19838 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19839 	    ipif->ipif_id));
19840 
19841 	mutex_enter(&ipif->ipif_saved_ire_lock);
19842 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19843 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19844 	if (ipif_saved_irep == NULL) {
19845 		mutex_exit(&ipif->ipif_saved_ire_lock);
19846 		return (NULL);
19847 	}
19848 
19849 	irep = ipif_saved_irep;
19850 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19851 		ire_t		*ire;
19852 		queue_t		*rfq;
19853 		queue_t		*stq;
19854 		ifrt_t		*ifrt;
19855 		uchar_t		*src_addr;
19856 		uchar_t		*gateway_addr;
19857 		ushort_t	type;
19858 
19859 		/*
19860 		 * When the ire was initially created and then added in
19861 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19862 		 * in the case of a traditional interface route, or as one of
19863 		 * the IRE_OFFSUBNET types (with the exception of
19864 		 * IRE_HOST types ire which is created by icmp_redirect() and
19865 		 * which we don't need to save or recover).  In the case where
19866 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19867 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19868 		 * to satisfy software like GateD and Sun Cluster which creates
19869 		 * routes using the the loopback interface's address as a
19870 		 * gateway.
19871 		 *
19872 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19873 		 * ire_create() will be called in the same way here as
19874 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19875 		 * the route looks like a traditional interface route (where
19876 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19877 		 * the saved ifrt->ifrt_type.  This means that in the case where
19878 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19879 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19880 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19881 		 */
19882 		ifrt = (ifrt_t *)mp->b_rptr;
19883 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19884 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19885 			rfq = NULL;
19886 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19887 			    ? ipif->ipif_rq : ipif->ipif_wq;
19888 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19889 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19890 			    : (uint8_t *)&ipif->ipif_src_addr;
19891 			gateway_addr = NULL;
19892 			type = ipif->ipif_net_type;
19893 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19894 			/* Recover multiroute broadcast IRE. */
19895 			rfq = ipif->ipif_rq;
19896 			stq = ipif->ipif_wq;
19897 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19898 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19899 			    : (uint8_t *)&ipif->ipif_src_addr;
19900 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19901 			type = ifrt->ifrt_type;
19902 		} else {
19903 			rfq = NULL;
19904 			stq = NULL;
19905 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19906 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19907 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19908 			type = ifrt->ifrt_type;
19909 		}
19910 
19911 		/*
19912 		 * Create a copy of the IRE with the saved address and netmask.
19913 		 */
19914 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19915 		    "0x%x/0x%x\n",
19916 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19917 		    ntohl(ifrt->ifrt_addr),
19918 		    ntohl(ifrt->ifrt_mask)));
19919 		ire = ire_create(
19920 		    (uint8_t *)&ifrt->ifrt_addr,
19921 		    (uint8_t *)&ifrt->ifrt_mask,
19922 		    src_addr,
19923 		    gateway_addr,
19924 		    NULL,
19925 		    &ifrt->ifrt_max_frag,
19926 		    NULL,
19927 		    rfq,
19928 		    stq,
19929 		    type,
19930 		    ipif,
19931 		    NULL,
19932 		    0,
19933 		    0,
19934 		    0,
19935 		    ifrt->ifrt_flags,
19936 		    &ifrt->ifrt_iulp_info,
19937 		    NULL,
19938 		    NULL,
19939 		    ipst);
19940 
19941 		if (ire == NULL) {
19942 			mutex_exit(&ipif->ipif_saved_ire_lock);
19943 			kmem_free(ipif_saved_irep,
19944 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19945 			return (NULL);
19946 		}
19947 
19948 		/*
19949 		 * Some software (for example, GateD and Sun Cluster) attempts
19950 		 * to create (what amount to) IRE_PREFIX routes with the
19951 		 * loopback address as the gateway.  This is primarily done to
19952 		 * set up prefixes with the RTF_REJECT flag set (for example,
19953 		 * when generating aggregate routes.)
19954 		 *
19955 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19956 		 * IRE_LOOPBACK, then we map the request into a
19957 		 * IRE_IF_NORESOLVER.
19958 		 */
19959 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19960 			ire->ire_type = IRE_IF_NORESOLVER;
19961 		/*
19962 		 * ire held by ire_add, will be refreled' towards the
19963 		 * the end of ipif_up_done
19964 		 */
19965 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19966 		*irep = ire;
19967 		irep++;
19968 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19969 	}
19970 	mutex_exit(&ipif->ipif_saved_ire_lock);
19971 	return (ipif_saved_irep);
19972 }
19973 
19974 /*
19975  * Used to set the netmask and broadcast address to default values when the
19976  * interface is brought up.  (Always called as writer.)
19977  */
19978 static void
19979 ipif_set_default(ipif_t *ipif)
19980 {
19981 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19982 
19983 	if (!ipif->ipif_isv6) {
19984 		/*
19985 		 * Interface holds an IPv4 address. Default
19986 		 * mask is the natural netmask.
19987 		 */
19988 		if (!ipif->ipif_net_mask) {
19989 			ipaddr_t	v4mask;
19990 
19991 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19992 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19993 		}
19994 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19995 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19996 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19997 		} else {
19998 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19999 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
20000 		}
20001 		/*
20002 		 * NOTE: SunOS 4.X does this even if the broadcast address
20003 		 * has been already set thus we do the same here.
20004 		 */
20005 		if (ipif->ipif_flags & IPIF_BROADCAST) {
20006 			ipaddr_t	v4addr;
20007 
20008 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
20009 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
20010 		}
20011 	} else {
20012 		/*
20013 		 * Interface holds an IPv6-only address.  Default
20014 		 * mask is all-ones.
20015 		 */
20016 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
20017 			ipif->ipif_v6net_mask = ipv6_all_ones;
20018 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20019 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20020 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
20021 		} else {
20022 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
20023 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
20024 		}
20025 	}
20026 }
20027 
20028 /*
20029  * Return 0 if this address can be used as local address without causing
20030  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
20031  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
20032  * Special checks are needed to allow the same IPv6 link-local address
20033  * on different ills.
20034  * TODO: allowing the same site-local address on different ill's.
20035  */
20036 int
20037 ip_addr_availability_check(ipif_t *new_ipif)
20038 {
20039 	in6_addr_t our_v6addr;
20040 	ill_t *ill;
20041 	ipif_t *ipif;
20042 	ill_walk_context_t ctx;
20043 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
20044 
20045 	ASSERT(IAM_WRITER_IPIF(new_ipif));
20046 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
20047 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
20048 
20049 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
20050 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
20051 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
20052 		return (0);
20053 
20054 	our_v6addr = new_ipif->ipif_v6lcl_addr;
20055 
20056 	if (new_ipif->ipif_isv6)
20057 		ill = ILL_START_WALK_V6(&ctx, ipst);
20058 	else
20059 		ill = ILL_START_WALK_V4(&ctx, ipst);
20060 
20061 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
20062 		for (ipif = ill->ill_ipif; ipif != NULL;
20063 		    ipif = ipif->ipif_next) {
20064 			if ((ipif == new_ipif) ||
20065 			    !(ipif->ipif_flags & IPIF_UP) ||
20066 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
20067 				continue;
20068 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
20069 			    &our_v6addr)) {
20070 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
20071 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
20072 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
20073 					ipif->ipif_flags |= IPIF_UNNUMBERED;
20074 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
20075 				    new_ipif->ipif_ill != ill)
20076 					continue;
20077 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
20078 				    new_ipif->ipif_ill != ill)
20079 					continue;
20080 				else if (new_ipif->ipif_zoneid !=
20081 				    ipif->ipif_zoneid &&
20082 				    ipif->ipif_zoneid != ALL_ZONES &&
20083 				    IS_LOOPBACK(ill))
20084 					continue;
20085 				else if (new_ipif->ipif_ill == ill)
20086 					return (EADDRINUSE);
20087 				else
20088 					return (EADDRNOTAVAIL);
20089 			}
20090 		}
20091 	}
20092 
20093 	return (0);
20094 }
20095 
20096 /*
20097  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
20098  * IREs for the ipif.
20099  * When the routine returns EINPROGRESS then mp has been consumed and
20100  * the ioctl will be acked from ip_rput_dlpi.
20101  */
20102 static int
20103 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
20104 {
20105 	ill_t	*ill = ipif->ipif_ill;
20106 	boolean_t isv6 = ipif->ipif_isv6;
20107 	int	err = 0;
20108 	boolean_t success;
20109 
20110 	ASSERT(IAM_WRITER_IPIF(ipif));
20111 
20112 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
20113 
20114 	/* Shouldn't get here if it is already up. */
20115 	if (ipif->ipif_flags & IPIF_UP)
20116 		return (EALREADY);
20117 
20118 	/* Skip arp/ndp for any loopback interface. */
20119 	if (ill->ill_wq != NULL) {
20120 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
20121 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
20122 
20123 		if (!ill->ill_dl_up) {
20124 			/*
20125 			 * ill_dl_up is not yet set. i.e. we are yet to
20126 			 * DL_BIND with the driver and this is the first
20127 			 * logical interface on the ill to become "up".
20128 			 * Tell the driver to get going (via DL_BIND_REQ).
20129 			 * Note that changing "significant" IFF_ flags
20130 			 * address/netmask etc cause a down/up dance, but
20131 			 * does not cause an unbind (DL_UNBIND) with the driver
20132 			 */
20133 			return (ill_dl_up(ill, ipif, mp, q));
20134 		}
20135 
20136 		/*
20137 		 * ipif_resolver_up may end up sending an
20138 		 * AR_INTERFACE_UP message to ARP, which would, in
20139 		 * turn send a DLPI message to the driver. ioctls are
20140 		 * serialized and so we cannot send more than one
20141 		 * interface up message at a time. If ipif_resolver_up
20142 		 * does send an interface up message to ARP, we get
20143 		 * EINPROGRESS and we will complete in ip_arp_done.
20144 		 */
20145 
20146 		ASSERT(connp != NULL || !CONN_Q(q));
20147 		ASSERT(ipsq->ipsq_pending_mp == NULL);
20148 		if (connp != NULL)
20149 			mutex_enter(&connp->conn_lock);
20150 		mutex_enter(&ill->ill_lock);
20151 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20152 		mutex_exit(&ill->ill_lock);
20153 		if (connp != NULL)
20154 			mutex_exit(&connp->conn_lock);
20155 		if (!success)
20156 			return (EINTR);
20157 
20158 		/*
20159 		 * Crank up IPv6 neighbor discovery
20160 		 * Unlike ARP, this should complete when
20161 		 * ipif_ndp_up returns. However, for
20162 		 * ILLF_XRESOLV interfaces we also send a
20163 		 * AR_INTERFACE_UP to the external resolver.
20164 		 * That ioctl will complete in ip_rput.
20165 		 */
20166 		if (isv6) {
20167 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr);
20168 			if (err != 0) {
20169 				if (err != EINPROGRESS)
20170 					mp = ipsq_pending_mp_get(ipsq, &connp);
20171 				return (err);
20172 			}
20173 		}
20174 		/* Now, ARP */
20175 		err = ipif_resolver_up(ipif, Res_act_initial);
20176 		if (err == EINPROGRESS) {
20177 			/* We will complete it in ip_arp_done */
20178 			return (err);
20179 		}
20180 		mp = ipsq_pending_mp_get(ipsq, &connp);
20181 		ASSERT(mp != NULL);
20182 		if (err != 0)
20183 			return (err);
20184 	} else {
20185 		/*
20186 		 * Interfaces without underlying hardware don't do duplicate
20187 		 * address detection.
20188 		 */
20189 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
20190 		ipif->ipif_addr_ready = 1;
20191 	}
20192 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
20193 }
20194 
20195 /*
20196  * Perform a bind for the physical device.
20197  * When the routine returns EINPROGRESS then mp has been consumed and
20198  * the ioctl will be acked from ip_rput_dlpi.
20199  * Allocate an unbind message and save it until ipif_down.
20200  */
20201 static int
20202 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
20203 {
20204 	areq_t	*areq;
20205 	mblk_t	*areq_mp = NULL;
20206 	mblk_t	*bind_mp = NULL;
20207 	mblk_t	*unbind_mp = NULL;
20208 	conn_t	*connp;
20209 	boolean_t success;
20210 	uint16_t sap_addr;
20211 
20212 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
20213 	ASSERT(IAM_WRITER_ILL(ill));
20214 	ASSERT(mp != NULL);
20215 
20216 	/* Create a resolver cookie for ARP */
20217 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
20218 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
20219 		if (areq_mp == NULL)
20220 			return (ENOMEM);
20221 
20222 		freemsg(ill->ill_resolver_mp);
20223 		ill->ill_resolver_mp = areq_mp;
20224 		areq = (areq_t *)areq_mp->b_rptr;
20225 		sap_addr = ill->ill_sap;
20226 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
20227 	}
20228 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
20229 	    DL_BIND_REQ);
20230 	if (bind_mp == NULL)
20231 		goto bad;
20232 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
20233 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
20234 
20235 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
20236 	if (unbind_mp == NULL)
20237 		goto bad;
20238 
20239 	/*
20240 	 * Record state needed to complete this operation when the
20241 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
20242 	 */
20243 	ASSERT(WR(q)->q_next == NULL);
20244 	connp = Q_TO_CONN(q);
20245 
20246 	mutex_enter(&connp->conn_lock);
20247 	mutex_enter(&ipif->ipif_ill->ill_lock);
20248 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20249 	mutex_exit(&ipif->ipif_ill->ill_lock);
20250 	mutex_exit(&connp->conn_lock);
20251 	if (!success)
20252 		goto bad;
20253 
20254 	/*
20255 	 * Save the unbind message for ill_dl_down(); it will be consumed when
20256 	 * the interface goes down.
20257 	 */
20258 	ASSERT(ill->ill_unbind_mp == NULL);
20259 	ill->ill_unbind_mp = unbind_mp;
20260 
20261 	ill_dlpi_send(ill, bind_mp);
20262 	/* Send down link-layer capabilities probe if not already done. */
20263 	ill_capability_probe(ill);
20264 
20265 	/*
20266 	 * Sysid used to rely on the fact that netboots set domainname
20267 	 * and the like. Now that miniroot boots aren't strictly netboots
20268 	 * and miniroot network configuration is driven from userland
20269 	 * these things still need to be set. This situation can be detected
20270 	 * by comparing the interface being configured here to the one
20271 	 * dhcack was set to reference by the boot loader. Once sysid is
20272 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20273 	 */
20274 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
20275 	    (strcmp(ill->ill_name, dhcack) == 0) &&
20276 	    (strlen(srpc_domain) == 0)) {
20277 		if (dhcpinit() != 0)
20278 			cmn_err(CE_WARN, "no cached dhcp response");
20279 	}
20280 
20281 	/*
20282 	 * This operation will complete in ip_rput_dlpi with either
20283 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20284 	 */
20285 	return (EINPROGRESS);
20286 bad:
20287 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20288 	/*
20289 	 * We don't have to check for possible removal from illgrp
20290 	 * as we have not yet inserted in illgrp. For groups
20291 	 * without names, this ipif is still not UP and hence
20292 	 * this could not have possibly had any influence in forming
20293 	 * groups.
20294 	 */
20295 
20296 	freemsg(bind_mp);
20297 	freemsg(unbind_mp);
20298 	return (ENOMEM);
20299 }
20300 
20301 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20302 
20303 /*
20304  * DLPI and ARP is up.
20305  * Create all the IREs associated with an interface bring up multicast.
20306  * Set the interface flag and finish other initialization
20307  * that potentially had to be differed to after DL_BIND_ACK.
20308  */
20309 int
20310 ipif_up_done(ipif_t *ipif)
20311 {
20312 	ire_t	*ire_array[20];
20313 	ire_t	**irep = ire_array;
20314 	ire_t	**irep1;
20315 	ipaddr_t net_mask = 0;
20316 	ipaddr_t subnet_mask, route_mask;
20317 	ill_t	*ill = ipif->ipif_ill;
20318 	queue_t	*stq;
20319 	ipif_t	 *src_ipif;
20320 	ipif_t   *tmp_ipif;
20321 	boolean_t	flush_ire_cache = B_TRUE;
20322 	int	err = 0;
20323 	phyint_t *phyi;
20324 	ire_t	**ipif_saved_irep = NULL;
20325 	int ipif_saved_ire_cnt;
20326 	int	cnt;
20327 	boolean_t	src_ipif_held = B_FALSE;
20328 	boolean_t	ire_added = B_FALSE;
20329 	boolean_t	loopback = B_FALSE;
20330 	ip_stack_t	*ipst = ill->ill_ipst;
20331 
20332 	ip1dbg(("ipif_up_done(%s:%u)\n",
20333 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20334 	/* Check if this is a loopback interface */
20335 	if (ipif->ipif_ill->ill_wq == NULL)
20336 		loopback = B_TRUE;
20337 
20338 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20339 	/*
20340 	 * If all other interfaces for this ill are down or DEPRECATED,
20341 	 * or otherwise unsuitable for source address selection, remove
20342 	 * any IRE_CACHE entries for this ill to make sure source
20343 	 * address selection gets to take this new ipif into account.
20344 	 * No need to hold ill_lock while traversing the ipif list since
20345 	 * we are writer
20346 	 */
20347 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20348 	    tmp_ipif = tmp_ipif->ipif_next) {
20349 		if (((tmp_ipif->ipif_flags &
20350 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20351 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20352 		    (tmp_ipif == ipif))
20353 			continue;
20354 		/* first useable pre-existing interface */
20355 		flush_ire_cache = B_FALSE;
20356 		break;
20357 	}
20358 	if (flush_ire_cache)
20359 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20360 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20361 
20362 	/*
20363 	 * Figure out which way the send-to queue should go.  Only
20364 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20365 	 * should show up here.
20366 	 */
20367 	switch (ill->ill_net_type) {
20368 	case IRE_IF_RESOLVER:
20369 		stq = ill->ill_rq;
20370 		break;
20371 	case IRE_IF_NORESOLVER:
20372 	case IRE_LOOPBACK:
20373 		stq = ill->ill_wq;
20374 		break;
20375 	default:
20376 		return (EINVAL);
20377 	}
20378 
20379 	if (IS_LOOPBACK(ill)) {
20380 		/*
20381 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20382 		 * ipif_lookup_on_name(), but in the case of zones we can have
20383 		 * several loopback addresses on lo0. So all the interfaces with
20384 		 * loopback addresses need to be marked IRE_LOOPBACK.
20385 		 */
20386 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20387 		    htonl(INADDR_LOOPBACK))
20388 			ipif->ipif_ire_type = IRE_LOOPBACK;
20389 		else
20390 			ipif->ipif_ire_type = IRE_LOCAL;
20391 	}
20392 
20393 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20394 		/*
20395 		 * Can't use our source address. Select a different
20396 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20397 		 */
20398 		src_ipif = ipif_select_source(ipif->ipif_ill,
20399 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20400 		if (src_ipif == NULL)
20401 			src_ipif = ipif;	/* Last resort */
20402 		else
20403 			src_ipif_held = B_TRUE;
20404 	} else {
20405 		src_ipif = ipif;
20406 	}
20407 
20408 	/* Create all the IREs associated with this interface */
20409 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20410 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20411 
20412 		/*
20413 		 * If we're on a labeled system then make sure that zone-
20414 		 * private addresses have proper remote host database entries.
20415 		 */
20416 		if (is_system_labeled() &&
20417 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20418 		    !tsol_check_interface_address(ipif))
20419 			return (EINVAL);
20420 
20421 		/* Register the source address for __sin6_src_id */
20422 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20423 		    ipif->ipif_zoneid, ipst);
20424 		if (err != 0) {
20425 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20426 			return (err);
20427 		}
20428 
20429 		/* If the interface address is set, create the local IRE. */
20430 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20431 		    (void *)ipif,
20432 		    ipif->ipif_ire_type,
20433 		    ntohl(ipif->ipif_lcl_addr)));
20434 		*irep++ = ire_create(
20435 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20436 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20437 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20438 		    NULL,				/* no gateway */
20439 		    NULL,
20440 		    &ip_loopback_mtuplus,		/* max frag size */
20441 		    NULL,
20442 		    ipif->ipif_rq,			/* recv-from queue */
20443 		    NULL,				/* no send-to queue */
20444 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20445 		    ipif,
20446 		    NULL,
20447 		    0,
20448 		    0,
20449 		    0,
20450 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20451 		    RTF_PRIVATE : 0,
20452 		    &ire_uinfo_null,
20453 		    NULL,
20454 		    NULL,
20455 		    ipst);
20456 	} else {
20457 		ip1dbg((
20458 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20459 		    ipif->ipif_ire_type,
20460 		    ntohl(ipif->ipif_lcl_addr),
20461 		    (uint_t)ipif->ipif_flags));
20462 	}
20463 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20464 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20465 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20466 	} else {
20467 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20468 	}
20469 
20470 	subnet_mask = ipif->ipif_net_mask;
20471 
20472 	/*
20473 	 * If mask was not specified, use natural netmask of
20474 	 * interface address. Also, store this mask back into the
20475 	 * ipif struct.
20476 	 */
20477 	if (subnet_mask == 0) {
20478 		subnet_mask = net_mask;
20479 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20480 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20481 		    ipif->ipif_v6subnet);
20482 	}
20483 
20484 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20485 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20486 	    ipif->ipif_subnet != INADDR_ANY) {
20487 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20488 
20489 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20490 			route_mask = IP_HOST_MASK;
20491 		} else {
20492 			route_mask = subnet_mask;
20493 		}
20494 
20495 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20496 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20497 		    (void *)ipif, (void *)ill,
20498 		    ill->ill_net_type,
20499 		    ntohl(ipif->ipif_subnet)));
20500 		*irep++ = ire_create(
20501 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20502 		    (uchar_t *)&route_mask,		/* mask */
20503 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20504 		    NULL,				/* no gateway */
20505 		    NULL,
20506 		    &ipif->ipif_mtu,			/* max frag */
20507 		    NULL,
20508 		    NULL,				/* no recv queue */
20509 		    stq,				/* send-to queue */
20510 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20511 		    ipif,
20512 		    NULL,
20513 		    0,
20514 		    0,
20515 		    0,
20516 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20517 		    &ire_uinfo_null,
20518 		    NULL,
20519 		    NULL,
20520 		    ipst);
20521 	}
20522 
20523 	/*
20524 	 * Create any necessary broadcast IREs.
20525 	 */
20526 	if ((ipif->ipif_subnet != INADDR_ANY) &&
20527 	    (ipif->ipif_flags & IPIF_BROADCAST))
20528 		irep = ipif_create_bcast_ires(ipif, irep);
20529 
20530 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20531 
20532 	/* If an earlier ire_create failed, get out now */
20533 	for (irep1 = irep; irep1 > ire_array; ) {
20534 		irep1--;
20535 		if (*irep1 == NULL) {
20536 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20537 			err = ENOMEM;
20538 			goto bad;
20539 		}
20540 	}
20541 
20542 	/*
20543 	 * Need to atomically check for ip_addr_availablity_check
20544 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20545 	 * from group also.The ill_g_lock is grabbed as reader
20546 	 * just to make sure no new ills or new ipifs are being added
20547 	 * to the system while we are checking the uniqueness of addresses.
20548 	 */
20549 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20550 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20551 	/* Mark it up, and increment counters. */
20552 	ipif->ipif_flags |= IPIF_UP;
20553 	ill->ill_ipif_up_count++;
20554 	err = ip_addr_availability_check(ipif);
20555 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20556 	rw_exit(&ipst->ips_ill_g_lock);
20557 
20558 	if (err != 0) {
20559 		/*
20560 		 * Our address may already be up on the same ill. In this case,
20561 		 * the ARP entry for our ipif replaced the one for the other
20562 		 * ipif. So we don't want to delete it (otherwise the other ipif
20563 		 * would be unable to send packets).
20564 		 * ip_addr_availability_check() identifies this case for us and
20565 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20566 		 * which is the expected error code.
20567 		 */
20568 		if (err == EADDRINUSE) {
20569 			freemsg(ipif->ipif_arp_del_mp);
20570 			ipif->ipif_arp_del_mp = NULL;
20571 			err = EADDRNOTAVAIL;
20572 		}
20573 		ill->ill_ipif_up_count--;
20574 		ipif->ipif_flags &= ~IPIF_UP;
20575 		goto bad;
20576 	}
20577 
20578 	/*
20579 	 * Add in all newly created IREs.  ire_create_bcast() has
20580 	 * already checked for duplicates of the IRE_BROADCAST type.
20581 	 * We want to add before we call ifgrp_insert which wants
20582 	 * to know whether IRE_IF_RESOLVER exists or not.
20583 	 *
20584 	 * NOTE : We refrele the ire though we may branch to "bad"
20585 	 *	  later on where we do ire_delete. This is okay
20586 	 *	  because nobody can delete it as we are running
20587 	 *	  exclusively.
20588 	 */
20589 	for (irep1 = irep; irep1 > ire_array; ) {
20590 		irep1--;
20591 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20592 		/*
20593 		 * refheld by ire_add. refele towards the end of the func
20594 		 */
20595 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20596 	}
20597 	ire_added = B_TRUE;
20598 	/*
20599 	 * Form groups if possible.
20600 	 *
20601 	 * If we are supposed to be in a ill_group with a name, insert it
20602 	 * now as we know that at least one ipif is UP. Otherwise form
20603 	 * nameless groups.
20604 	 *
20605 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20606 	 * this ipif into the appropriate interface group, or create a
20607 	 * new one. If this is already in a nameless group, we try to form
20608 	 * a bigger group looking at other ills potentially sharing this
20609 	 * ipif's prefix.
20610 	 */
20611 	phyi = ill->ill_phyint;
20612 	if (phyi->phyint_groupname_len != 0) {
20613 		ASSERT(phyi->phyint_groupname != NULL);
20614 		if (ill->ill_ipif_up_count == 1) {
20615 			ASSERT(ill->ill_group == NULL);
20616 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20617 			    phyi->phyint_groupname, NULL, B_TRUE);
20618 			if (err != 0) {
20619 				ip1dbg(("ipif_up_done: illgrp allocation "
20620 				    "failed, error %d\n", err));
20621 				goto bad;
20622 			}
20623 		}
20624 		ASSERT(ill->ill_group != NULL);
20625 	}
20626 
20627 	/*
20628 	 * When this is part of group, we need to make sure that
20629 	 * any broadcast ires created because of this ipif coming
20630 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20631 	 * so that we don't receive duplicate broadcast packets.
20632 	 */
20633 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20634 		ipif_renominate_bcast(ipif);
20635 
20636 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20637 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20638 	ipif_saved_irep = ipif_recover_ire(ipif);
20639 
20640 	if (!loopback) {
20641 		/*
20642 		 * If the broadcast address has been set, make sure it makes
20643 		 * sense based on the interface address.
20644 		 * Only match on ill since we are sharing broadcast addresses.
20645 		 */
20646 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20647 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20648 			ire_t	*ire;
20649 
20650 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20651 			    IRE_BROADCAST, ipif, ALL_ZONES,
20652 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20653 
20654 			if (ire == NULL) {
20655 				/*
20656 				 * If there isn't a matching broadcast IRE,
20657 				 * revert to the default for this netmask.
20658 				 */
20659 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20660 				mutex_enter(&ipif->ipif_ill->ill_lock);
20661 				ipif_set_default(ipif);
20662 				mutex_exit(&ipif->ipif_ill->ill_lock);
20663 			} else {
20664 				ire_refrele(ire);
20665 			}
20666 		}
20667 
20668 	}
20669 
20670 	/* This is the first interface on this ill */
20671 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20672 		/*
20673 		 * Need to recover all multicast memberships in the driver.
20674 		 * This had to be deferred until we had attached.
20675 		 */
20676 		ill_recover_multicast(ill);
20677 	}
20678 	/* Join the allhosts multicast address */
20679 	ipif_multicast_up(ipif);
20680 
20681 	if (!loopback) {
20682 		/*
20683 		 * See whether anybody else would benefit from the
20684 		 * new ipif that we added. We call this always rather
20685 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20686 		 * ipif is for the benefit of illgrp_insert (done above)
20687 		 * which does not do source address selection as it does
20688 		 * not want to re-create interface routes that we are
20689 		 * having reference to it here.
20690 		 */
20691 		ill_update_source_selection(ill);
20692 	}
20693 
20694 	for (irep1 = irep; irep1 > ire_array; ) {
20695 		irep1--;
20696 		if (*irep1 != NULL) {
20697 			/* was held in ire_add */
20698 			ire_refrele(*irep1);
20699 		}
20700 	}
20701 
20702 	cnt = ipif_saved_ire_cnt;
20703 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20704 		if (*irep1 != NULL) {
20705 			/* was held in ire_add */
20706 			ire_refrele(*irep1);
20707 		}
20708 	}
20709 
20710 	if (!loopback && ipif->ipif_addr_ready) {
20711 		/* Broadcast an address mask reply. */
20712 		ipif_mask_reply(ipif);
20713 	}
20714 	if (ipif_saved_irep != NULL) {
20715 		kmem_free(ipif_saved_irep,
20716 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20717 	}
20718 	if (src_ipif_held)
20719 		ipif_refrele(src_ipif);
20720 
20721 	/*
20722 	 * This had to be deferred until we had bound.  Tell routing sockets and
20723 	 * others that this interface is up if it looks like the address has
20724 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20725 	 * duplicate address detection to do its thing.
20726 	 */
20727 	if (ipif->ipif_addr_ready) {
20728 		ip_rts_ifmsg(ipif);
20729 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20730 		/* Let SCTP update the status for this ipif */
20731 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20732 	}
20733 	return (0);
20734 
20735 bad:
20736 	ip1dbg(("ipif_up_done: FAILED \n"));
20737 	/*
20738 	 * We don't have to bother removing from ill groups because
20739 	 *
20740 	 * 1) For groups with names, we insert only when the first ipif
20741 	 *    comes up. In that case if it fails, it will not be in any
20742 	 *    group. So, we need not try to remove for that case.
20743 	 *
20744 	 * 2) For groups without names, either we tried to insert ipif_ill
20745 	 *    in a group as singleton or found some other group to become
20746 	 *    a bigger group. For the former, if it fails we don't have
20747 	 *    anything to do as ipif_ill is not in the group and for the
20748 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20749 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20750 	 */
20751 	while (irep > ire_array) {
20752 		irep--;
20753 		if (*irep != NULL) {
20754 			ire_delete(*irep);
20755 			if (ire_added)
20756 				ire_refrele(*irep);
20757 		}
20758 	}
20759 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20760 
20761 	if (ipif_saved_irep != NULL) {
20762 		kmem_free(ipif_saved_irep,
20763 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20764 	}
20765 	if (src_ipif_held)
20766 		ipif_refrele(src_ipif);
20767 
20768 	ipif_arp_down(ipif);
20769 	return (err);
20770 }
20771 
20772 /*
20773  * Turn off the ARP with the ILLF_NOARP flag.
20774  */
20775 static int
20776 ill_arp_off(ill_t *ill)
20777 {
20778 	mblk_t	*arp_off_mp = NULL;
20779 	mblk_t	*arp_on_mp = NULL;
20780 
20781 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20782 
20783 	ASSERT(IAM_WRITER_ILL(ill));
20784 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20785 
20786 	/*
20787 	 * If the on message is still around we've already done
20788 	 * an arp_off without doing an arp_on thus there is no
20789 	 * work needed.
20790 	 */
20791 	if (ill->ill_arp_on_mp != NULL)
20792 		return (0);
20793 
20794 	/*
20795 	 * Allocate an ARP on message (to be saved) and an ARP off message
20796 	 */
20797 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20798 	if (!arp_off_mp)
20799 		return (ENOMEM);
20800 
20801 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20802 	if (!arp_on_mp)
20803 		goto failed;
20804 
20805 	ASSERT(ill->ill_arp_on_mp == NULL);
20806 	ill->ill_arp_on_mp = arp_on_mp;
20807 
20808 	/* Send an AR_INTERFACE_OFF request */
20809 	putnext(ill->ill_rq, arp_off_mp);
20810 	return (0);
20811 failed:
20812 
20813 	if (arp_off_mp)
20814 		freemsg(arp_off_mp);
20815 	return (ENOMEM);
20816 }
20817 
20818 /*
20819  * Turn on ARP by turning off the ILLF_NOARP flag.
20820  */
20821 static int
20822 ill_arp_on(ill_t *ill)
20823 {
20824 	mblk_t	*mp;
20825 
20826 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20827 
20828 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20829 
20830 	ASSERT(IAM_WRITER_ILL(ill));
20831 	/*
20832 	 * Send an AR_INTERFACE_ON request if we have already done
20833 	 * an arp_off (which allocated the message).
20834 	 */
20835 	if (ill->ill_arp_on_mp != NULL) {
20836 		mp = ill->ill_arp_on_mp;
20837 		ill->ill_arp_on_mp = NULL;
20838 		putnext(ill->ill_rq, mp);
20839 	}
20840 	return (0);
20841 }
20842 
20843 /*
20844  * Called after either deleting ill from the group or when setting
20845  * FAILED or STANDBY on the interface.
20846  */
20847 static void
20848 illgrp_reset_schednext(ill_t *ill)
20849 {
20850 	ill_group_t *illgrp;
20851 	ill_t *save_ill;
20852 
20853 	ASSERT(IAM_WRITER_ILL(ill));
20854 	/*
20855 	 * When called from illgrp_delete, ill_group will be non-NULL.
20856 	 * But when called from ip_sioctl_flags, it could be NULL if
20857 	 * somebody is setting FAILED/INACTIVE on some interface which
20858 	 * is not part of a group.
20859 	 */
20860 	illgrp = ill->ill_group;
20861 	if (illgrp == NULL)
20862 		return;
20863 	if (illgrp->illgrp_ill_schednext != ill)
20864 		return;
20865 
20866 	illgrp->illgrp_ill_schednext = NULL;
20867 	save_ill = ill;
20868 	/*
20869 	 * Choose a good ill to be the next one for
20870 	 * outbound traffic. As the flags FAILED/STANDBY is
20871 	 * not yet marked when called from ip_sioctl_flags,
20872 	 * we check for ill separately.
20873 	 */
20874 	for (ill = illgrp->illgrp_ill; ill != NULL;
20875 	    ill = ill->ill_group_next) {
20876 		if ((ill != save_ill) &&
20877 		    !(ill->ill_phyint->phyint_flags &
20878 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20879 			illgrp->illgrp_ill_schednext = ill;
20880 			return;
20881 		}
20882 	}
20883 }
20884 
20885 /*
20886  * Given an ill, find the next ill in the group to be scheduled.
20887  * (This should be called by ip_newroute() before ire_create().)
20888  * The passed in ill may be pulled out of the group, after we have picked
20889  * up a different outgoing ill from the same group. However ire add will
20890  * atomically check this.
20891  */
20892 ill_t *
20893 illgrp_scheduler(ill_t *ill)
20894 {
20895 	ill_t *retill;
20896 	ill_group_t *illgrp;
20897 	int illcnt;
20898 	int i;
20899 	uint64_t flags;
20900 	ip_stack_t	*ipst = ill->ill_ipst;
20901 
20902 	/*
20903 	 * We don't use a lock to check for the ill_group. If this ill
20904 	 * is currently being inserted we may end up just returning this
20905 	 * ill itself. That is ok.
20906 	 */
20907 	if (ill->ill_group == NULL) {
20908 		ill_refhold(ill);
20909 		return (ill);
20910 	}
20911 
20912 	/*
20913 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20914 	 * a set of stable ills. No ill can be added or deleted or change
20915 	 * group while we hold the reader lock.
20916 	 */
20917 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20918 	if ((illgrp = ill->ill_group) == NULL) {
20919 		rw_exit(&ipst->ips_ill_g_lock);
20920 		ill_refhold(ill);
20921 		return (ill);
20922 	}
20923 
20924 	illcnt = illgrp->illgrp_ill_count;
20925 	mutex_enter(&illgrp->illgrp_lock);
20926 	retill = illgrp->illgrp_ill_schednext;
20927 
20928 	if (retill == NULL)
20929 		retill = illgrp->illgrp_ill;
20930 
20931 	/*
20932 	 * We do a circular search beginning at illgrp_ill_schednext
20933 	 * or illgrp_ill. We don't check the flags against the ill lock
20934 	 * since it can change anytime. The ire creation will be atomic
20935 	 * and will fail if the ill is FAILED or OFFLINE.
20936 	 */
20937 	for (i = 0; i < illcnt; i++) {
20938 		flags = retill->ill_phyint->phyint_flags;
20939 
20940 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20941 		    ILL_CAN_LOOKUP(retill)) {
20942 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20943 			ill_refhold(retill);
20944 			break;
20945 		}
20946 		retill = retill->ill_group_next;
20947 		if (retill == NULL)
20948 			retill = illgrp->illgrp_ill;
20949 	}
20950 	mutex_exit(&illgrp->illgrp_lock);
20951 	rw_exit(&ipst->ips_ill_g_lock);
20952 
20953 	return (i == illcnt ? NULL : retill);
20954 }
20955 
20956 /*
20957  * Checks for availbility of a usable source address (if there is one) when the
20958  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20959  * this selection is done regardless of the destination.
20960  */
20961 boolean_t
20962 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20963 {
20964 	uint_t	ifindex;
20965 	ipif_t	*ipif = NULL;
20966 	ill_t	*uill;
20967 	boolean_t isv6;
20968 	ip_stack_t	*ipst = ill->ill_ipst;
20969 
20970 	ASSERT(ill != NULL);
20971 
20972 	isv6 = ill->ill_isv6;
20973 	ifindex = ill->ill_usesrc_ifindex;
20974 	if (ifindex != 0) {
20975 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20976 		    NULL, ipst);
20977 		if (uill == NULL)
20978 			return (NULL);
20979 		mutex_enter(&uill->ill_lock);
20980 		for (ipif = uill->ill_ipif; ipif != NULL;
20981 		    ipif = ipif->ipif_next) {
20982 			if (!IPIF_CAN_LOOKUP(ipif))
20983 				continue;
20984 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20985 				continue;
20986 			if (!(ipif->ipif_flags & IPIF_UP))
20987 				continue;
20988 			if (ipif->ipif_zoneid != zoneid)
20989 				continue;
20990 			if ((isv6 &&
20991 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20992 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20993 				continue;
20994 			mutex_exit(&uill->ill_lock);
20995 			ill_refrele(uill);
20996 			return (B_TRUE);
20997 		}
20998 		mutex_exit(&uill->ill_lock);
20999 		ill_refrele(uill);
21000 	}
21001 	return (B_FALSE);
21002 }
21003 
21004 /*
21005  * Determine the best source address given a destination address and an ill.
21006  * Prefers non-deprecated over deprecated but will return a deprecated
21007  * address if there is no other choice. If there is a usable source address
21008  * on the interface pointed to by ill_usesrc_ifindex then that is given
21009  * first preference.
21010  *
21011  * Returns NULL if there is no suitable source address for the ill.
21012  * This only occurs when there is no valid source address for the ill.
21013  */
21014 ipif_t *
21015 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
21016 {
21017 	ipif_t *ipif;
21018 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
21019 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
21020 	int index = 0;
21021 	boolean_t wrapped = B_FALSE;
21022 	boolean_t same_subnet_only = B_FALSE;
21023 	boolean_t ipif_same_found, ipif_other_found;
21024 	boolean_t specific_found;
21025 	ill_t	*till, *usill = NULL;
21026 	tsol_tpc_t *src_rhtp, *dst_rhtp;
21027 	ip_stack_t	*ipst = ill->ill_ipst;
21028 
21029 	if (ill->ill_usesrc_ifindex != 0) {
21030 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
21031 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
21032 		if (usill != NULL)
21033 			ill = usill;	/* Select source from usesrc ILL */
21034 		else
21035 			return (NULL);
21036 	}
21037 
21038 	/*
21039 	 * If we're dealing with an unlabeled destination on a labeled system,
21040 	 * make sure that we ignore source addresses that are incompatible with
21041 	 * the destination's default label.  That destination's default label
21042 	 * must dominate the minimum label on the source address.
21043 	 */
21044 	dst_rhtp = NULL;
21045 	if (is_system_labeled()) {
21046 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
21047 		if (dst_rhtp == NULL)
21048 			return (NULL);
21049 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
21050 			TPC_RELE(dst_rhtp);
21051 			dst_rhtp = NULL;
21052 		}
21053 	}
21054 
21055 	/*
21056 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
21057 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
21058 	 * After selecting the right ipif, under ill_lock make sure ipif is
21059 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
21060 	 * we retry. Inside the loop we still need to check for CONDEMNED,
21061 	 * but not under a lock.
21062 	 */
21063 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21064 
21065 retry:
21066 	till = ill;
21067 	ipif_arr[0] = NULL;
21068 
21069 	if (till->ill_group != NULL)
21070 		till = till->ill_group->illgrp_ill;
21071 
21072 	/*
21073 	 * Choose one good source address from each ill across the group.
21074 	 * If possible choose a source address in the same subnet as
21075 	 * the destination address.
21076 	 *
21077 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
21078 	 * This is okay because of the following.
21079 	 *
21080 	 *    If PHYI_FAILED is set and we still have non-deprecated
21081 	 *    addresses, it means the addresses have not yet been
21082 	 *    failed over to a different interface. We potentially
21083 	 *    select them to create IRE_CACHES, which will be later
21084 	 *    flushed when the addresses move over.
21085 	 *
21086 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
21087 	 *    addresses, it means either the user has configured them
21088 	 *    or PHYI_INACTIVE has not been cleared after the addresses
21089 	 *    been moved over. For the former, in.mpathd does a failover
21090 	 *    when the interface becomes INACTIVE and hence we should
21091 	 *    not find them. Once INACTIVE is set, we don't allow them
21092 	 *    to create logical interfaces anymore. For the latter, a
21093 	 *    flush will happen when INACTIVE is cleared which will
21094 	 *    flush the IRE_CACHES.
21095 	 *
21096 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
21097 	 *    over soon. We potentially select them to create IRE_CACHEs,
21098 	 *    which will be later flushed when the addresses move over.
21099 	 *
21100 	 * NOTE : As ipif_select_source is called to borrow source address
21101 	 * for an ipif that is part of a group, source address selection
21102 	 * will be re-done whenever the group changes i.e either an
21103 	 * insertion/deletion in the group.
21104 	 *
21105 	 * Fill ipif_arr[] with source addresses, using these rules:
21106 	 *
21107 	 *	1. At most one source address from a given ill ends up
21108 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
21109 	 *	   associated with a given ill ends up in ipif_arr[].
21110 	 *
21111 	 *	2. If there is at least one non-deprecated ipif in the
21112 	 *	   IPMP group with a source address on the same subnet as
21113 	 *	   our destination, then fill ipif_arr[] only with
21114 	 *	   source addresses on the same subnet as our destination.
21115 	 *	   Note that because of (1), only the first
21116 	 *	   non-deprecated ipif found with a source address
21117 	 *	   matching the destination ends up in ipif_arr[].
21118 	 *
21119 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
21120 	 *	   addresses not in the same subnet as our destination.
21121 	 *	   Again, because of (1), only the first off-subnet source
21122 	 *	   address will be chosen.
21123 	 *
21124 	 *	4. If there are no non-deprecated ipifs, then just use
21125 	 *	   the source address associated with the last deprecated
21126 	 *	   one we find that happens to be on the same subnet,
21127 	 *	   otherwise the first one not in the same subnet.
21128 	 */
21129 	specific_found = B_FALSE;
21130 	for (; till != NULL; till = till->ill_group_next) {
21131 		ipif_same_found = B_FALSE;
21132 		ipif_other_found = B_FALSE;
21133 		for (ipif = till->ill_ipif; ipif != NULL;
21134 		    ipif = ipif->ipif_next) {
21135 			if (!IPIF_CAN_LOOKUP(ipif))
21136 				continue;
21137 			/* Always skip NOLOCAL and ANYCAST interfaces */
21138 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
21139 				continue;
21140 			if (!(ipif->ipif_flags & IPIF_UP) ||
21141 			    !ipif->ipif_addr_ready)
21142 				continue;
21143 			if (ipif->ipif_zoneid != zoneid &&
21144 			    ipif->ipif_zoneid != ALL_ZONES)
21145 				continue;
21146 			/*
21147 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
21148 			 * but are not valid as source addresses.
21149 			 */
21150 			if (ipif->ipif_lcl_addr == INADDR_ANY)
21151 				continue;
21152 
21153 			/*
21154 			 * Check compatibility of local address for
21155 			 * destination's default label if we're on a labeled
21156 			 * system.  Incompatible addresses can't be used at
21157 			 * all.
21158 			 */
21159 			if (dst_rhtp != NULL) {
21160 				boolean_t incompat;
21161 
21162 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
21163 				    IPV4_VERSION, B_FALSE);
21164 				if (src_rhtp == NULL)
21165 					continue;
21166 				incompat =
21167 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
21168 				    src_rhtp->tpc_tp.tp_doi !=
21169 				    dst_rhtp->tpc_tp.tp_doi ||
21170 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
21171 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
21172 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
21173 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
21174 				TPC_RELE(src_rhtp);
21175 				if (incompat)
21176 					continue;
21177 			}
21178 
21179 			/*
21180 			 * We prefer not to use all all-zones addresses, if we
21181 			 * can avoid it, as they pose problems with unlabeled
21182 			 * destinations.
21183 			 */
21184 			if (ipif->ipif_zoneid != ALL_ZONES) {
21185 				if (!specific_found &&
21186 				    (!same_subnet_only ||
21187 				    (ipif->ipif_net_mask & dst) ==
21188 				    ipif->ipif_subnet)) {
21189 					index = 0;
21190 					specific_found = B_TRUE;
21191 					ipif_other_found = B_FALSE;
21192 				}
21193 			} else {
21194 				if (specific_found)
21195 					continue;
21196 			}
21197 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
21198 				if (ipif_dep == NULL ||
21199 				    (ipif->ipif_net_mask & dst) ==
21200 				    ipif->ipif_subnet)
21201 					ipif_dep = ipif;
21202 				continue;
21203 			}
21204 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
21205 				/* found a source address in the same subnet */
21206 				if (!same_subnet_only) {
21207 					same_subnet_only = B_TRUE;
21208 					index = 0;
21209 				}
21210 				ipif_same_found = B_TRUE;
21211 			} else {
21212 				if (same_subnet_only || ipif_other_found)
21213 					continue;
21214 				ipif_other_found = B_TRUE;
21215 			}
21216 			ipif_arr[index++] = ipif;
21217 			if (index == MAX_IPIF_SELECT_SOURCE) {
21218 				wrapped = B_TRUE;
21219 				index = 0;
21220 			}
21221 			if (ipif_same_found)
21222 				break;
21223 		}
21224 	}
21225 
21226 	if (ipif_arr[0] == NULL) {
21227 		ipif = ipif_dep;
21228 	} else {
21229 		if (wrapped)
21230 			index = MAX_IPIF_SELECT_SOURCE;
21231 		ipif = ipif_arr[ipif_rand(ipst) % index];
21232 		ASSERT(ipif != NULL);
21233 	}
21234 
21235 	if (ipif != NULL) {
21236 		mutex_enter(&ipif->ipif_ill->ill_lock);
21237 		if (!IPIF_CAN_LOOKUP(ipif)) {
21238 			mutex_exit(&ipif->ipif_ill->ill_lock);
21239 			goto retry;
21240 		}
21241 		ipif_refhold_locked(ipif);
21242 		mutex_exit(&ipif->ipif_ill->ill_lock);
21243 	}
21244 
21245 	rw_exit(&ipst->ips_ill_g_lock);
21246 	if (usill != NULL)
21247 		ill_refrele(usill);
21248 	if (dst_rhtp != NULL)
21249 		TPC_RELE(dst_rhtp);
21250 
21251 #ifdef DEBUG
21252 	if (ipif == NULL) {
21253 		char buf1[INET6_ADDRSTRLEN];
21254 
21255 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
21256 		    ill->ill_name,
21257 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
21258 	} else {
21259 		char buf1[INET6_ADDRSTRLEN];
21260 		char buf2[INET6_ADDRSTRLEN];
21261 
21262 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
21263 		    ipif->ipif_ill->ill_name,
21264 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21265 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21266 		    buf2, sizeof (buf2))));
21267 	}
21268 #endif /* DEBUG */
21269 	return (ipif);
21270 }
21271 
21272 
21273 /*
21274  * If old_ipif is not NULL, see if ipif was derived from old
21275  * ipif and if so, recreate the interface route by re-doing
21276  * source address selection. This happens when ipif_down ->
21277  * ipif_update_other_ipifs calls us.
21278  *
21279  * If old_ipif is NULL, just redo the source address selection
21280  * if needed. This happens when illgrp_insert or ipif_up_done
21281  * calls us.
21282  */
21283 static void
21284 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21285 {
21286 	ire_t *ire;
21287 	ire_t *ipif_ire;
21288 	queue_t *stq;
21289 	ipif_t *nipif;
21290 	ill_t *ill;
21291 	boolean_t need_rele = B_FALSE;
21292 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21293 
21294 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21295 	ASSERT(IAM_WRITER_IPIF(ipif));
21296 
21297 	ill = ipif->ipif_ill;
21298 	if (!(ipif->ipif_flags &
21299 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21300 		/*
21301 		 * Can't possibly have borrowed the source
21302 		 * from old_ipif.
21303 		 */
21304 		return;
21305 	}
21306 
21307 	/*
21308 	 * Is there any work to be done? No work if the address
21309 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21310 	 * ipif_select_source() does not borrow addresses from
21311 	 * NOLOCAL and ANYCAST interfaces).
21312 	 */
21313 	if ((old_ipif != NULL) &&
21314 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21315 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21316 	    (old_ipif->ipif_flags &
21317 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21318 		return;
21319 	}
21320 
21321 	/*
21322 	 * Perform the same checks as when creating the
21323 	 * IRE_INTERFACE in ipif_up_done.
21324 	 */
21325 	if (!(ipif->ipif_flags & IPIF_UP))
21326 		return;
21327 
21328 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21329 	    (ipif->ipif_subnet == INADDR_ANY))
21330 		return;
21331 
21332 	ipif_ire = ipif_to_ire(ipif);
21333 	if (ipif_ire == NULL)
21334 		return;
21335 
21336 	/*
21337 	 * We know that ipif uses some other source for its
21338 	 * IRE_INTERFACE. Is it using the source of this
21339 	 * old_ipif?
21340 	 */
21341 	if (old_ipif != NULL &&
21342 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21343 		ire_refrele(ipif_ire);
21344 		return;
21345 	}
21346 	if (ip_debug > 2) {
21347 		/* ip1dbg */
21348 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21349 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21350 	}
21351 
21352 	stq = ipif_ire->ire_stq;
21353 
21354 	/*
21355 	 * Can't use our source address. Select a different
21356 	 * source address for the IRE_INTERFACE.
21357 	 */
21358 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21359 	if (nipif == NULL) {
21360 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21361 		nipif = ipif;
21362 	} else {
21363 		need_rele = B_TRUE;
21364 	}
21365 
21366 	ire = ire_create(
21367 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21368 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21369 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21370 	    NULL,				/* no gateway */
21371 	    NULL,
21372 	    &ipif->ipif_mtu,			/* max frag */
21373 	    NULL,				/* no src nce */
21374 	    NULL,				/* no recv from queue */
21375 	    stq,				/* send-to queue */
21376 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21377 	    ipif,
21378 	    NULL,
21379 	    0,
21380 	    0,
21381 	    0,
21382 	    0,
21383 	    &ire_uinfo_null,
21384 	    NULL,
21385 	    NULL,
21386 	    ipst);
21387 
21388 	if (ire != NULL) {
21389 		ire_t *ret_ire;
21390 		int error;
21391 
21392 		/*
21393 		 * We don't need ipif_ire anymore. We need to delete
21394 		 * before we add so that ire_add does not detect
21395 		 * duplicates.
21396 		 */
21397 		ire_delete(ipif_ire);
21398 		ret_ire = ire;
21399 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21400 		ASSERT(error == 0);
21401 		ASSERT(ire == ret_ire);
21402 		/* Held in ire_add */
21403 		ire_refrele(ret_ire);
21404 	}
21405 	/*
21406 	 * Either we are falling through from above or could not
21407 	 * allocate a replacement.
21408 	 */
21409 	ire_refrele(ipif_ire);
21410 	if (need_rele)
21411 		ipif_refrele(nipif);
21412 }
21413 
21414 /*
21415  * This old_ipif is going away.
21416  *
21417  * Determine if any other ipif's is using our address as
21418  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21419  * IPIF_DEPRECATED).
21420  * Find the IRE_INTERFACE for such ipifs and recreate them
21421  * to use an different source address following the rules in
21422  * ipif_up_done.
21423  *
21424  * This function takes an illgrp as an argument so that illgrp_delete
21425  * can call this to update source address even after deleting the
21426  * old_ipif->ipif_ill from the ill group.
21427  */
21428 static void
21429 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21430 {
21431 	ipif_t *ipif;
21432 	ill_t *ill;
21433 	char	buf[INET6_ADDRSTRLEN];
21434 
21435 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21436 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21437 
21438 	ill = old_ipif->ipif_ill;
21439 
21440 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21441 	    ill->ill_name,
21442 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21443 	    buf, sizeof (buf))));
21444 	/*
21445 	 * If this part of a group, look at all ills as ipif_select_source
21446 	 * borrows source address across all the ills in the group.
21447 	 */
21448 	if (illgrp != NULL)
21449 		ill = illgrp->illgrp_ill;
21450 
21451 	for (; ill != NULL; ill = ill->ill_group_next) {
21452 		for (ipif = ill->ill_ipif; ipif != NULL;
21453 		    ipif = ipif->ipif_next) {
21454 
21455 			if (ipif == old_ipif)
21456 				continue;
21457 
21458 			ipif_recreate_interface_routes(old_ipif, ipif);
21459 		}
21460 	}
21461 }
21462 
21463 /* ARGSUSED */
21464 int
21465 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21466 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21467 {
21468 	/*
21469 	 * ill_phyint_reinit merged the v4 and v6 into a single
21470 	 * ipsq. Could also have become part of a ipmp group in the
21471 	 * process, and we might not have been able to complete the
21472 	 * operation in ipif_set_values, if we could not become
21473 	 * exclusive.  If so restart it here.
21474 	 */
21475 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21476 }
21477 
21478 
21479 /*
21480  * Can operate on either a module or a driver queue.
21481  * Returns an error if not a module queue.
21482  */
21483 /* ARGSUSED */
21484 int
21485 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21486     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21487 {
21488 	queue_t		*q1 = q;
21489 	char 		*cp;
21490 	char		interf_name[LIFNAMSIZ];
21491 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21492 
21493 	if (q->q_next == NULL) {
21494 		ip1dbg((
21495 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21496 		return (EINVAL);
21497 	}
21498 
21499 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21500 		return (EALREADY);
21501 
21502 	do {
21503 		q1 = q1->q_next;
21504 	} while (q1->q_next);
21505 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21506 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21507 
21508 	/*
21509 	 * Here we are not going to delay the ioack until after
21510 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21511 	 * original ioctl message before sending the requests.
21512 	 */
21513 	return (ipif_set_values(q, mp, interf_name, &ppa));
21514 }
21515 
21516 /* ARGSUSED */
21517 int
21518 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21519     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21520 {
21521 	return (ENXIO);
21522 }
21523 
21524 /*
21525  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21526  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21527  * ire_check_and_create_bcast()).
21528  */
21529 static ire_t **
21530 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21531 {
21532 	ipaddr_t addr;
21533 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21534 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21535 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21536 
21537 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21538 
21539 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21540 
21541 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21542 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21543 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21544 
21545 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21546 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21547 
21548 	/*
21549 	 * For backward compatibility, we create net broadcast IREs based on
21550 	 * the old "IP address class system", since some old machines only
21551 	 * respond to these class derived net broadcast.  However, we must not
21552 	 * create these net broadcast IREs if the subnetmask is shorter than
21553 	 * the IP address class based derived netmask.  Otherwise, we may
21554 	 * create a net broadcast address which is the same as an IP address
21555 	 * on the subnet -- and then TCP will refuse to talk to that address.
21556 	 */
21557 	if (netmask < subnetmask) {
21558 		addr = netmask & ipif->ipif_subnet;
21559 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21560 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21561 		    flags);
21562 	}
21563 
21564 	/*
21565 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21566 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21567 	 * created.  Creating these broadcast IREs will only create confusion
21568 	 * as `addr' will be the same as the IP address.
21569 	 */
21570 	if (subnetmask != 0xFFFFFFFF) {
21571 		addr = ipif->ipif_subnet;
21572 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21573 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21574 		    irep, flags);
21575 	}
21576 
21577 	return (irep);
21578 }
21579 
21580 /*
21581  * Broadcast IRE info structure used in the functions below.  Since we
21582  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21583  */
21584 typedef struct bcast_ireinfo {
21585 	uchar_t		bi_type;	/* BCAST_* value from below */
21586 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21587 			bi_needrep:1,	/* do we need to replace it? */
21588 			bi_haverep:1,	/* have we replaced it? */
21589 			bi_pad:5;
21590 	ipaddr_t	bi_addr;	/* IRE address */
21591 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21592 } bcast_ireinfo_t;
21593 
21594 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21595 
21596 /*
21597  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21598  * return B_TRUE if it should immediately be used to recreate the IRE.
21599  */
21600 static boolean_t
21601 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21602 {
21603 	ipaddr_t addr;
21604 
21605 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21606 
21607 	switch (bireinfop->bi_type) {
21608 	case BCAST_NET:
21609 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21610 		if (addr != bireinfop->bi_addr)
21611 			return (B_FALSE);
21612 		break;
21613 	case BCAST_SUBNET:
21614 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21615 			return (B_FALSE);
21616 		break;
21617 	}
21618 
21619 	bireinfop->bi_needrep = 1;
21620 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21621 		if (bireinfop->bi_backup == NULL)
21622 			bireinfop->bi_backup = ipif;
21623 		return (B_FALSE);
21624 	}
21625 	return (B_TRUE);
21626 }
21627 
21628 /*
21629  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21630  * them ala ire_check_and_create_bcast().
21631  */
21632 static ire_t **
21633 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21634 {
21635 	ipaddr_t mask, addr;
21636 
21637 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21638 
21639 	addr = bireinfop->bi_addr;
21640 	irep = ire_create_bcast(ipif, addr, irep);
21641 
21642 	switch (bireinfop->bi_type) {
21643 	case BCAST_NET:
21644 		mask = ip_net_mask(ipif->ipif_subnet);
21645 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21646 		break;
21647 	case BCAST_SUBNET:
21648 		mask = ipif->ipif_net_mask;
21649 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21650 		break;
21651 	}
21652 
21653 	bireinfop->bi_haverep = 1;
21654 	return (irep);
21655 }
21656 
21657 /*
21658  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21659  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21660  * that are going away are still needed.  If so, have ipif_create_bcast()
21661  * recreate them (except for the deprecated case, as explained below).
21662  */
21663 static ire_t **
21664 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21665     ire_t **irep)
21666 {
21667 	int i;
21668 	ipif_t *ipif;
21669 
21670 	ASSERT(!ill->ill_isv6);
21671 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21672 		/*
21673 		 * Skip this ipif if it's (a) the one being taken down, (b)
21674 		 * not in the same zone, or (c) has no valid local address.
21675 		 */
21676 		if (ipif == test_ipif ||
21677 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21678 		    ipif->ipif_subnet == 0 ||
21679 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21680 		    (IPIF_UP|IPIF_BROADCAST))
21681 			continue;
21682 
21683 		/*
21684 		 * For each dying IRE that hasn't yet been replaced, see if
21685 		 * `ipif' needs it and whether the IRE should be recreated on
21686 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21687 		 * will return B_FALSE even if `ipif' needs the IRE on the
21688 		 * hopes that we'll later find a needy non-deprecated ipif.
21689 		 * However, the ipif is recorded in bi_backup for possible
21690 		 * subsequent use by ipif_check_bcast_ires().
21691 		 */
21692 		for (i = 0; i < BCAST_COUNT; i++) {
21693 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21694 				continue;
21695 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21696 				continue;
21697 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21698 		}
21699 
21700 		/*
21701 		 * If we've replaced all of the broadcast IREs that are going
21702 		 * to be taken down, we know we're done.
21703 		 */
21704 		for (i = 0; i < BCAST_COUNT; i++) {
21705 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21706 				break;
21707 		}
21708 		if (i == BCAST_COUNT)
21709 			break;
21710 	}
21711 	return (irep);
21712 }
21713 
21714 /*
21715  * Check if `test_ipif' (which is going away) is associated with any existing
21716  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21717  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21718  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21719  *
21720  * This is necessary because broadcast IREs are shared.  In particular, a
21721  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21722  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21723  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21724  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21725  * same zone, they will share the same set of broadcast IREs.
21726  *
21727  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21728  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21729  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21730  */
21731 static void
21732 ipif_check_bcast_ires(ipif_t *test_ipif)
21733 {
21734 	ill_t		*ill = test_ipif->ipif_ill;
21735 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21736 	ire_t		**irep1, **irep = &ire_array[0];
21737 	uint_t 		i, willdie;
21738 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21739 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21740 
21741 	ASSERT(!test_ipif->ipif_isv6);
21742 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21743 
21744 	/*
21745 	 * No broadcast IREs for the LOOPBACK interface
21746 	 * or others such as point to point and IPIF_NOXMIT.
21747 	 */
21748 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21749 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21750 		return;
21751 
21752 	bzero(bireinfo, sizeof (bireinfo));
21753 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21754 	bireinfo[0].bi_addr = 0;
21755 
21756 	bireinfo[1].bi_type = BCAST_ALLONES;
21757 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21758 
21759 	bireinfo[2].bi_type = BCAST_NET;
21760 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21761 
21762 	if (test_ipif->ipif_net_mask != 0)
21763 		mask = test_ipif->ipif_net_mask;
21764 	bireinfo[3].bi_type = BCAST_SUBNET;
21765 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21766 
21767 	/*
21768 	 * Figure out what (if any) broadcast IREs will die as a result of
21769 	 * `test_ipif' going away.  If none will die, we're done.
21770 	 */
21771 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21772 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21773 		    test_ipif, ALL_ZONES, NULL,
21774 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21775 		if (ire != NULL) {
21776 			willdie++;
21777 			bireinfo[i].bi_willdie = 1;
21778 			ire_refrele(ire);
21779 		}
21780 	}
21781 
21782 	if (willdie == 0)
21783 		return;
21784 
21785 	/*
21786 	 * Walk through all the ipifs that will be affected by the dying IREs,
21787 	 * and recreate the IREs as necessary.
21788 	 */
21789 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21790 
21791 	/*
21792 	 * Scan through the set of broadcast IREs and see if there are any
21793 	 * that we need to replace that have not yet been replaced.  If so,
21794 	 * replace them using the appropriate backup ipif.
21795 	 */
21796 	for (i = 0; i < BCAST_COUNT; i++) {
21797 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21798 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21799 			    &bireinfo[i], irep);
21800 	}
21801 
21802 	/*
21803 	 * If we can't create all of them, don't add any of them.  (Code in
21804 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21805 	 * non-loopback copy and loopback copy for a given address.)
21806 	 */
21807 	for (irep1 = irep; irep1 > ire_array; ) {
21808 		irep1--;
21809 		if (*irep1 == NULL) {
21810 			ip0dbg(("ipif_check_bcast_ires: can't create "
21811 			    "IRE_BROADCAST, memory allocation failure\n"));
21812 			while (irep > ire_array) {
21813 				irep--;
21814 				if (*irep != NULL)
21815 					ire_delete(*irep);
21816 			}
21817 			return;
21818 		}
21819 	}
21820 
21821 	for (irep1 = irep; irep1 > ire_array; ) {
21822 		irep1--;
21823 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21824 			ire_refrele(*irep1);		/* Held in ire_add */
21825 	}
21826 }
21827 
21828 /*
21829  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21830  * from lifr_flags and the name from lifr_name.
21831  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21832  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21833  * Returns EINPROGRESS when mp has been consumed by queueing it on
21834  * ill_pending_mp and the ioctl will complete in ip_rput.
21835  *
21836  * Can operate on either a module or a driver queue.
21837  * Returns an error if not a module queue.
21838  */
21839 /* ARGSUSED */
21840 int
21841 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21842     ip_ioctl_cmd_t *ipip, void *if_req)
21843 {
21844 	int	err;
21845 	ill_t	*ill;
21846 	struct lifreq *lifr = (struct lifreq *)if_req;
21847 
21848 	ASSERT(ipif != NULL);
21849 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21850 
21851 	if (q->q_next == NULL) {
21852 		ip1dbg((
21853 		    "if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21854 		return (EINVAL);
21855 	}
21856 
21857 	ill = (ill_t *)q->q_ptr;
21858 	/*
21859 	 * If we are not writer on 'q' then this interface exists already
21860 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
21861 	 * So return EALREADY
21862 	 */
21863 	if (ill != ipif->ipif_ill)
21864 		return (EALREADY);
21865 
21866 	if (ill->ill_name[0] != '\0')
21867 		return (EALREADY);
21868 
21869 	/*
21870 	 * Set all the flags. Allows all kinds of override. Provide some
21871 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21872 	 * unless there is either multicast/broadcast support in the driver
21873 	 * or it is a pt-pt link.
21874 	 */
21875 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21876 		/* Meaningless to IP thus don't allow them to be set. */
21877 		ip1dbg(("ip_setname: EINVAL 1\n"));
21878 		return (EINVAL);
21879 	}
21880 	/*
21881 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21882 	 * ill_bcast_addr_length info.
21883 	 */
21884 	if (!ill->ill_needs_attach &&
21885 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21886 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21887 	    ill->ill_bcast_addr_length == 0)) {
21888 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21889 		ip1dbg(("ip_setname: EINVAL 2\n"));
21890 		return (EINVAL);
21891 	}
21892 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21893 	    ((lifr->lifr_flags & IFF_IPV6) ||
21894 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21895 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21896 		ip1dbg(("ip_setname: EINVAL 3\n"));
21897 		return (EINVAL);
21898 	}
21899 	if (lifr->lifr_flags & IFF_UP) {
21900 		/* Can only be set with SIOCSLIFFLAGS */
21901 		ip1dbg(("ip_setname: EINVAL 4\n"));
21902 		return (EINVAL);
21903 	}
21904 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21905 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21906 		ip1dbg(("ip_setname: EINVAL 5\n"));
21907 		return (EINVAL);
21908 	}
21909 	/*
21910 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21911 	 */
21912 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21913 	    !(lifr->lifr_flags & IFF_IPV6) &&
21914 	    !(ipif->ipif_isv6)) {
21915 		ip1dbg(("ip_setname: EINVAL 6\n"));
21916 		return (EINVAL);
21917 	}
21918 
21919 	/*
21920 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21921 	 * we have all the flags here. So, we assign rather than we OR.
21922 	 * We can't OR the flags here because we don't want to set
21923 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21924 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21925 	 * on lifr_flags value here.
21926 	 */
21927 	/*
21928 	 * This ill has not been inserted into the global list.
21929 	 * So we are still single threaded and don't need any lock
21930 	 */
21931 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
21932 	    ~IFF_DUPLICATE;
21933 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21934 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21935 
21936 	/* We started off as V4. */
21937 	if (ill->ill_flags & ILLF_IPV6) {
21938 		ill->ill_phyint->phyint_illv6 = ill;
21939 		ill->ill_phyint->phyint_illv4 = NULL;
21940 	}
21941 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
21942 	return (err);
21943 }
21944 
21945 /* ARGSUSED */
21946 int
21947 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21948     ip_ioctl_cmd_t *ipip, void *if_req)
21949 {
21950 	/*
21951 	 * ill_phyint_reinit merged the v4 and v6 into a single
21952 	 * ipsq. Could also have become part of a ipmp group in the
21953 	 * process, and we might not have been able to complete the
21954 	 * slifname in ipif_set_values, if we could not become
21955 	 * exclusive.  If so restart it here
21956 	 */
21957 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21958 }
21959 
21960 /*
21961  * Return a pointer to the ipif which matches the index, IP version type and
21962  * zoneid.
21963  */
21964 ipif_t *
21965 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21966     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21967 {
21968 	ill_t	*ill;
21969 	ipsq_t  *ipsq;
21970 	phyint_t *phyi;
21971 	ipif_t	*ipif;
21972 
21973 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21974 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21975 
21976 	if (err != NULL)
21977 		*err = 0;
21978 
21979 	/*
21980 	 * Indexes are stored in the phyint - a common structure
21981 	 * to both IPv4 and IPv6.
21982 	 */
21983 
21984 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21985 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
21986 	    (void *) &index, NULL);
21987 	if (phyi != NULL) {
21988 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
21989 		if (ill == NULL) {
21990 			rw_exit(&ipst->ips_ill_g_lock);
21991 			if (err != NULL)
21992 				*err = ENXIO;
21993 			return (NULL);
21994 		}
21995 		GRAB_CONN_LOCK(q);
21996 		mutex_enter(&ill->ill_lock);
21997 		if (ILL_CAN_LOOKUP(ill)) {
21998 			for (ipif = ill->ill_ipif; ipif != NULL;
21999 			    ipif = ipif->ipif_next) {
22000 				if (IPIF_CAN_LOOKUP(ipif) &&
22001 				    (zoneid == ALL_ZONES ||
22002 				    zoneid == ipif->ipif_zoneid ||
22003 				    ipif->ipif_zoneid == ALL_ZONES)) {
22004 					ipif_refhold_locked(ipif);
22005 					mutex_exit(&ill->ill_lock);
22006 					RELEASE_CONN_LOCK(q);
22007 					rw_exit(&ipst->ips_ill_g_lock);
22008 					return (ipif);
22009 				}
22010 			}
22011 		} else if (ILL_CAN_WAIT(ill, q)) {
22012 			ipsq = ill->ill_phyint->phyint_ipsq;
22013 			mutex_enter(&ipsq->ipsq_lock);
22014 			rw_exit(&ipst->ips_ill_g_lock);
22015 			mutex_exit(&ill->ill_lock);
22016 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
22017 			mutex_exit(&ipsq->ipsq_lock);
22018 			RELEASE_CONN_LOCK(q);
22019 			*err = EINPROGRESS;
22020 			return (NULL);
22021 		}
22022 		mutex_exit(&ill->ill_lock);
22023 		RELEASE_CONN_LOCK(q);
22024 	}
22025 	rw_exit(&ipst->ips_ill_g_lock);
22026 	if (err != NULL)
22027 		*err = ENXIO;
22028 	return (NULL);
22029 }
22030 
22031 typedef struct conn_change_s {
22032 	uint_t cc_old_ifindex;
22033 	uint_t cc_new_ifindex;
22034 } conn_change_t;
22035 
22036 /*
22037  * ipcl_walk function for changing interface index.
22038  */
22039 static void
22040 conn_change_ifindex(conn_t *connp, caddr_t arg)
22041 {
22042 	conn_change_t *connc;
22043 	uint_t old_ifindex;
22044 	uint_t new_ifindex;
22045 	int i;
22046 	ilg_t *ilg;
22047 
22048 	connc = (conn_change_t *)arg;
22049 	old_ifindex = connc->cc_old_ifindex;
22050 	new_ifindex = connc->cc_new_ifindex;
22051 
22052 	if (connp->conn_orig_bound_ifindex == old_ifindex)
22053 		connp->conn_orig_bound_ifindex = new_ifindex;
22054 
22055 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
22056 		connp->conn_orig_multicast_ifindex = new_ifindex;
22057 
22058 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
22059 		connp->conn_orig_xmit_ifindex = new_ifindex;
22060 
22061 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
22062 		ilg = &connp->conn_ilg[i];
22063 		if (ilg->ilg_orig_ifindex == old_ifindex)
22064 			ilg->ilg_orig_ifindex = new_ifindex;
22065 	}
22066 }
22067 
22068 /*
22069  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
22070  * to new_index if it matches the old_index.
22071  *
22072  * Failovers typically happen within a group of ills. But somebody
22073  * can remove an ill from the group after a failover happened. If
22074  * we are setting the ifindex after this, we potentially need to
22075  * look at all the ills rather than just the ones in the group.
22076  * We cut down the work by looking at matching ill_net_types
22077  * and ill_types as we could not possibly grouped them together.
22078  */
22079 static void
22080 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
22081 {
22082 	ill_t *ill;
22083 	ipif_t *ipif;
22084 	uint_t old_ifindex;
22085 	uint_t new_ifindex;
22086 	ilm_t *ilm;
22087 	ill_walk_context_t ctx;
22088 	ip_stack_t	*ipst = ill_orig->ill_ipst;
22089 
22090 	old_ifindex = connc->cc_old_ifindex;
22091 	new_ifindex = connc->cc_new_ifindex;
22092 
22093 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
22094 	ill = ILL_START_WALK_ALL(&ctx, ipst);
22095 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
22096 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
22097 		    (ill_orig->ill_type != ill->ill_type)) {
22098 			continue;
22099 		}
22100 		for (ipif = ill->ill_ipif; ipif != NULL;
22101 		    ipif = ipif->ipif_next) {
22102 			if (ipif->ipif_orig_ifindex == old_ifindex)
22103 				ipif->ipif_orig_ifindex = new_ifindex;
22104 		}
22105 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
22106 			if (ilm->ilm_orig_ifindex == old_ifindex)
22107 				ilm->ilm_orig_ifindex = new_ifindex;
22108 		}
22109 	}
22110 	rw_exit(&ipst->ips_ill_g_lock);
22111 }
22112 
22113 /*
22114  * We first need to ensure that the new index is unique, and
22115  * then carry the change across both v4 and v6 ill representation
22116  * of the physical interface.
22117  */
22118 /* ARGSUSED */
22119 int
22120 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22121     ip_ioctl_cmd_t *ipip, void *ifreq)
22122 {
22123 	ill_t		*ill;
22124 	ill_t		*ill_other;
22125 	phyint_t	*phyi;
22126 	int		old_index;
22127 	conn_change_t	connc;
22128 	struct ifreq	*ifr = (struct ifreq *)ifreq;
22129 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22130 	uint_t	index;
22131 	ill_t	*ill_v4;
22132 	ill_t	*ill_v6;
22133 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22134 
22135 	if (ipip->ipi_cmd_type == IF_CMD)
22136 		index = ifr->ifr_index;
22137 	else
22138 		index = lifr->lifr_index;
22139 
22140 	/*
22141 	 * Only allow on physical interface. Also, index zero is illegal.
22142 	 *
22143 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
22144 	 *
22145 	 * 1) If PHYI_FAILED is set, a failover could have happened which
22146 	 *    implies a possible failback might have to happen. As failback
22147 	 *    depends on the old index, we should fail setting the index.
22148 	 *
22149 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
22150 	 *    any addresses or multicast memberships are failed over to
22151 	 *    a non-STANDBY interface. As failback depends on the old
22152 	 *    index, we should fail setting the index for this case also.
22153 	 *
22154 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
22155 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
22156 	 */
22157 	ill = ipif->ipif_ill;
22158 	phyi = ill->ill_phyint;
22159 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
22160 	    ipif->ipif_id != 0 || index == 0) {
22161 		return (EINVAL);
22162 	}
22163 	old_index = phyi->phyint_ifindex;
22164 
22165 	/* If the index is not changing, no work to do */
22166 	if (old_index == index)
22167 		return (0);
22168 
22169 	/*
22170 	 * Use ill_lookup_on_ifindex to determine if the
22171 	 * new index is unused and if so allow the change.
22172 	 */
22173 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
22174 	    ipst);
22175 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
22176 	    ipst);
22177 	if (ill_v6 != NULL || ill_v4 != NULL) {
22178 		if (ill_v4 != NULL)
22179 			ill_refrele(ill_v4);
22180 		if (ill_v6 != NULL)
22181 			ill_refrele(ill_v6);
22182 		return (EBUSY);
22183 	}
22184 
22185 	/*
22186 	 * The new index is unused. Set it in the phyint.
22187 	 * Locate the other ill so that we can send a routing
22188 	 * sockets message.
22189 	 */
22190 	if (ill->ill_isv6) {
22191 		ill_other = phyi->phyint_illv4;
22192 	} else {
22193 		ill_other = phyi->phyint_illv6;
22194 	}
22195 
22196 	phyi->phyint_ifindex = index;
22197 
22198 	/* Update SCTP's ILL list */
22199 	sctp_ill_reindex(ill, old_index);
22200 
22201 	connc.cc_old_ifindex = old_index;
22202 	connc.cc_new_ifindex = index;
22203 	ip_change_ifindex(ill, &connc);
22204 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
22205 
22206 	/* Send the routing sockets message */
22207 	ip_rts_ifmsg(ipif);
22208 	if (ill_other != NULL)
22209 		ip_rts_ifmsg(ill_other->ill_ipif);
22210 
22211 	return (0);
22212 }
22213 
22214 /* ARGSUSED */
22215 int
22216 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22217     ip_ioctl_cmd_t *ipip, void *ifreq)
22218 {
22219 	struct ifreq	*ifr = (struct ifreq *)ifreq;
22220 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22221 
22222 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
22223 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22224 	/* Get the interface index */
22225 	if (ipip->ipi_cmd_type == IF_CMD) {
22226 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22227 	} else {
22228 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22229 	}
22230 	return (0);
22231 }
22232 
22233 /* ARGSUSED */
22234 int
22235 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22236     ip_ioctl_cmd_t *ipip, void *ifreq)
22237 {
22238 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22239 
22240 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
22241 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22242 	/* Get the interface zone */
22243 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22244 	lifr->lifr_zoneid = ipif->ipif_zoneid;
22245 	return (0);
22246 }
22247 
22248 /*
22249  * Set the zoneid of an interface.
22250  */
22251 /* ARGSUSED */
22252 int
22253 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22254     ip_ioctl_cmd_t *ipip, void *ifreq)
22255 {
22256 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22257 	int err = 0;
22258 	boolean_t need_up = B_FALSE;
22259 	zone_t *zptr;
22260 	zone_status_t status;
22261 	zoneid_t zoneid;
22262 
22263 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22264 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
22265 		if (!is_system_labeled())
22266 			return (ENOTSUP);
22267 		zoneid = GLOBAL_ZONEID;
22268 	}
22269 
22270 	/* cannot assign instance zero to a non-global zone */
22271 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
22272 		return (ENOTSUP);
22273 
22274 	/*
22275 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
22276 	 * the event of a race with the zone shutdown processing, since IP
22277 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22278 	 * interface will be cleaned up even if the zone is shut down
22279 	 * immediately after the status check. If the interface can't be brought
22280 	 * down right away, and the zone is shut down before the restart
22281 	 * function is called, we resolve the possible races by rechecking the
22282 	 * zone status in the restart function.
22283 	 */
22284 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22285 		return (EINVAL);
22286 	status = zone_status_get(zptr);
22287 	zone_rele(zptr);
22288 
22289 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22290 		return (EINVAL);
22291 
22292 	if (ipif->ipif_flags & IPIF_UP) {
22293 		/*
22294 		 * If the interface is already marked up,
22295 		 * we call ipif_down which will take care
22296 		 * of ditching any IREs that have been set
22297 		 * up based on the old interface address.
22298 		 */
22299 		err = ipif_logical_down(ipif, q, mp);
22300 		if (err == EINPROGRESS)
22301 			return (err);
22302 		ipif_down_tail(ipif);
22303 		need_up = B_TRUE;
22304 	}
22305 
22306 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22307 	return (err);
22308 }
22309 
22310 static int
22311 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22312     queue_t *q, mblk_t *mp, boolean_t need_up)
22313 {
22314 	int	err = 0;
22315 	ip_stack_t	*ipst;
22316 
22317 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22318 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22319 
22320 	if (CONN_Q(q))
22321 		ipst = CONNQ_TO_IPST(q);
22322 	else
22323 		ipst = ILLQ_TO_IPST(q);
22324 
22325 	/*
22326 	 * For exclusive stacks we don't allow a different zoneid than
22327 	 * global.
22328 	 */
22329 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22330 	    zoneid != GLOBAL_ZONEID)
22331 		return (EINVAL);
22332 
22333 	/* Set the new zone id. */
22334 	ipif->ipif_zoneid = zoneid;
22335 
22336 	/* Update sctp list */
22337 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22338 
22339 	if (need_up) {
22340 		/*
22341 		 * Now bring the interface back up.  If this
22342 		 * is the only IPIF for the ILL, ipif_up
22343 		 * will have to re-bind to the device, so
22344 		 * we may get back EINPROGRESS, in which
22345 		 * case, this IOCTL will get completed in
22346 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22347 		 */
22348 		err = ipif_up(ipif, q, mp);
22349 	}
22350 	return (err);
22351 }
22352 
22353 /* ARGSUSED */
22354 int
22355 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22356     ip_ioctl_cmd_t *ipip, void *if_req)
22357 {
22358 	struct lifreq *lifr = (struct lifreq *)if_req;
22359 	zoneid_t zoneid;
22360 	zone_t *zptr;
22361 	zone_status_t status;
22362 
22363 	ASSERT(ipif->ipif_id != 0);
22364 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22365 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22366 		zoneid = GLOBAL_ZONEID;
22367 
22368 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22369 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22370 
22371 	/*
22372 	 * We recheck the zone status to resolve the following race condition:
22373 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22374 	 * 2) hme0:1 is up and can't be brought down right away;
22375 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22376 	 * 3) zone "myzone" is halted; the zone status switches to
22377 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22378 	 * the interfaces to remove - hme0:1 is not returned because it's not
22379 	 * yet in "myzone", so it won't be removed;
22380 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22381 	 * status check here, we would have hme0:1 in "myzone" after it's been
22382 	 * destroyed.
22383 	 * Note that if the status check fails, we need to bring the interface
22384 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22385 	 * ipif_up_done[_v6]().
22386 	 */
22387 	status = ZONE_IS_UNINITIALIZED;
22388 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22389 		status = zone_status_get(zptr);
22390 		zone_rele(zptr);
22391 	}
22392 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22393 		if (ipif->ipif_isv6) {
22394 			(void) ipif_up_done_v6(ipif);
22395 		} else {
22396 			(void) ipif_up_done(ipif);
22397 		}
22398 		return (EINVAL);
22399 	}
22400 
22401 	ipif_down_tail(ipif);
22402 
22403 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22404 	    B_TRUE));
22405 }
22406 
22407 /* ARGSUSED */
22408 int
22409 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22410 	ip_ioctl_cmd_t *ipip, void *ifreq)
22411 {
22412 	struct lifreq	*lifr = ifreq;
22413 
22414 	ASSERT(q->q_next == NULL);
22415 	ASSERT(CONN_Q(q));
22416 
22417 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22418 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22419 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22420 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22421 
22422 	return (0);
22423 }
22424 
22425 
22426 /* Find the previous ILL in this usesrc group */
22427 static ill_t *
22428 ill_prev_usesrc(ill_t *uill)
22429 {
22430 	ill_t *ill;
22431 
22432 	for (ill = uill->ill_usesrc_grp_next;
22433 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22434 	    ill = ill->ill_usesrc_grp_next)
22435 		/* do nothing */;
22436 	return (ill);
22437 }
22438 
22439 /*
22440  * Release all members of the usesrc group. This routine is called
22441  * from ill_delete when the interface being unplumbed is the
22442  * group head.
22443  */
22444 static void
22445 ill_disband_usesrc_group(ill_t *uill)
22446 {
22447 	ill_t *next_ill, *tmp_ill;
22448 	ip_stack_t	*ipst = uill->ill_ipst;
22449 
22450 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22451 	next_ill = uill->ill_usesrc_grp_next;
22452 
22453 	do {
22454 		ASSERT(next_ill != NULL);
22455 		tmp_ill = next_ill->ill_usesrc_grp_next;
22456 		ASSERT(tmp_ill != NULL);
22457 		next_ill->ill_usesrc_grp_next = NULL;
22458 		next_ill->ill_usesrc_ifindex = 0;
22459 		next_ill = tmp_ill;
22460 	} while (next_ill->ill_usesrc_ifindex != 0);
22461 	uill->ill_usesrc_grp_next = NULL;
22462 }
22463 
22464 /*
22465  * Remove the client usesrc ILL from the list and relink to a new list
22466  */
22467 int
22468 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22469 {
22470 	ill_t *ill, *tmp_ill;
22471 	ip_stack_t	*ipst = ucill->ill_ipst;
22472 
22473 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22474 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22475 
22476 	/*
22477 	 * Check if the usesrc client ILL passed in is not already
22478 	 * in use as a usesrc ILL i.e one whose source address is
22479 	 * in use OR a usesrc ILL is not already in use as a usesrc
22480 	 * client ILL
22481 	 */
22482 	if ((ucill->ill_usesrc_ifindex == 0) ||
22483 	    (uill->ill_usesrc_ifindex != 0)) {
22484 		return (-1);
22485 	}
22486 
22487 	ill = ill_prev_usesrc(ucill);
22488 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22489 
22490 	/* Remove from the current list */
22491 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22492 		/* Only two elements in the list */
22493 		ASSERT(ill->ill_usesrc_ifindex == 0);
22494 		ill->ill_usesrc_grp_next = NULL;
22495 	} else {
22496 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22497 	}
22498 
22499 	if (ifindex == 0) {
22500 		ucill->ill_usesrc_ifindex = 0;
22501 		ucill->ill_usesrc_grp_next = NULL;
22502 		return (0);
22503 	}
22504 
22505 	ucill->ill_usesrc_ifindex = ifindex;
22506 	tmp_ill = uill->ill_usesrc_grp_next;
22507 	uill->ill_usesrc_grp_next = ucill;
22508 	ucill->ill_usesrc_grp_next =
22509 	    (tmp_ill != NULL) ? tmp_ill : uill;
22510 	return (0);
22511 }
22512 
22513 /*
22514  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22515  * ip.c for locking details.
22516  */
22517 /* ARGSUSED */
22518 int
22519 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22520     ip_ioctl_cmd_t *ipip, void *ifreq)
22521 {
22522 	struct lifreq *lifr = (struct lifreq *)ifreq;
22523 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22524 	    ill_flag_changed = B_FALSE;
22525 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22526 	int err = 0, ret;
22527 	uint_t ifindex;
22528 	phyint_t *us_phyint, *us_cli_phyint;
22529 	ipsq_t *ipsq = NULL;
22530 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22531 
22532 	ASSERT(IAM_WRITER_IPIF(ipif));
22533 	ASSERT(q->q_next == NULL);
22534 	ASSERT(CONN_Q(q));
22535 
22536 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22537 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22538 
22539 	ASSERT(us_cli_phyint != NULL);
22540 
22541 	/*
22542 	 * If the client ILL is being used for IPMP, abort.
22543 	 * Note, this can be done before ipsq_try_enter since we are already
22544 	 * exclusive on this ILL
22545 	 */
22546 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22547 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22548 		return (EINVAL);
22549 	}
22550 
22551 	ifindex = lifr->lifr_index;
22552 	if (ifindex == 0) {
22553 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22554 			/* non usesrc group interface, nothing to reset */
22555 			return (0);
22556 		}
22557 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22558 		/* valid reset request */
22559 		reset_flg = B_TRUE;
22560 	}
22561 
22562 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22563 	    ip_process_ioctl, &err, ipst);
22564 
22565 	if (usesrc_ill == NULL) {
22566 		return (err);
22567 	}
22568 
22569 	/*
22570 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22571 	 * group nor can either of the interfaces be used for standy. So
22572 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22573 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22574 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22575 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22576 	 * the usesrc_cli_ill
22577 	 */
22578 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22579 	    NEW_OP, B_TRUE);
22580 	if (ipsq == NULL) {
22581 		err = EINPROGRESS;
22582 		/* Operation enqueued on the ipsq of the usesrc ILL */
22583 		goto done;
22584 	}
22585 
22586 	/* Check if the usesrc_ill is used for IPMP */
22587 	us_phyint = usesrc_ill->ill_phyint;
22588 	if ((us_phyint->phyint_groupname != NULL) ||
22589 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22590 		err = EINVAL;
22591 		goto done;
22592 	}
22593 
22594 	/*
22595 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22596 	 * already a client then return EINVAL
22597 	 */
22598 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22599 		err = EINVAL;
22600 		goto done;
22601 	}
22602 
22603 	/*
22604 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22605 	 * be then this is a duplicate operation.
22606 	 */
22607 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22608 		err = 0;
22609 		goto done;
22610 	}
22611 
22612 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22613 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22614 	    usesrc_ill->ill_isv6));
22615 
22616 	/*
22617 	 * The next step ensures that no new ires will be created referencing
22618 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22619 	 * we go through an ire walk deleting all ire caches that reference
22620 	 * the client ill. New ires referencing the client ill that are added
22621 	 * to the ire table before the ILL_CHANGING flag is set, will be
22622 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22623 	 * the client ill while the ILL_CHANGING flag is set will be failed
22624 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22625 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22626 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22627 	 * belong to the same usesrc group.
22628 	 */
22629 	mutex_enter(&usesrc_cli_ill->ill_lock);
22630 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22631 	mutex_exit(&usesrc_cli_ill->ill_lock);
22632 	ill_flag_changed = B_TRUE;
22633 
22634 	if (ipif->ipif_isv6)
22635 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22636 		    ALL_ZONES, ipst);
22637 	else
22638 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22639 		    ALL_ZONES, ipst);
22640 
22641 	/*
22642 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22643 	 * and the ill_usesrc_ifindex fields
22644 	 */
22645 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22646 
22647 	if (reset_flg) {
22648 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22649 		if (ret != 0) {
22650 			err = EINVAL;
22651 		}
22652 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22653 		goto done;
22654 	}
22655 
22656 	/*
22657 	 * Four possibilities to consider:
22658 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22659 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22660 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22661 	 * 4. Both are part of their respective usesrc groups
22662 	 */
22663 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22664 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22665 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22666 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22667 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22668 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22669 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22670 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22671 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22672 		/* Insert at head of list */
22673 		usesrc_cli_ill->ill_usesrc_grp_next =
22674 		    usesrc_ill->ill_usesrc_grp_next;
22675 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22676 	} else {
22677 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22678 		    ifindex);
22679 		if (ret != 0)
22680 			err = EINVAL;
22681 	}
22682 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22683 
22684 done:
22685 	if (ill_flag_changed) {
22686 		mutex_enter(&usesrc_cli_ill->ill_lock);
22687 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22688 		mutex_exit(&usesrc_cli_ill->ill_lock);
22689 	}
22690 	if (ipsq != NULL)
22691 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22692 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22693 	ill_refrele(usesrc_ill);
22694 	return (err);
22695 }
22696 
22697 /*
22698  * comparison function used by avl.
22699  */
22700 static int
22701 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22702 {
22703 
22704 	uint_t index;
22705 
22706 	ASSERT(phyip != NULL && index_ptr != NULL);
22707 
22708 	index = *((uint_t *)index_ptr);
22709 	/*
22710 	 * let the phyint with the lowest index be on top.
22711 	 */
22712 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22713 		return (1);
22714 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22715 		return (-1);
22716 	return (0);
22717 }
22718 
22719 /*
22720  * comparison function used by avl.
22721  */
22722 static int
22723 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22724 {
22725 	ill_t *ill;
22726 	int res = 0;
22727 
22728 	ASSERT(phyip != NULL && name_ptr != NULL);
22729 
22730 	if (((phyint_t *)phyip)->phyint_illv4)
22731 		ill = ((phyint_t *)phyip)->phyint_illv4;
22732 	else
22733 		ill = ((phyint_t *)phyip)->phyint_illv6;
22734 	ASSERT(ill != NULL);
22735 
22736 	res = strcmp(ill->ill_name, (char *)name_ptr);
22737 	if (res > 0)
22738 		return (1);
22739 	else if (res < 0)
22740 		return (-1);
22741 	return (0);
22742 }
22743 /*
22744  * This function is called from ill_delete when the ill is being
22745  * unplumbed. We remove the reference from the phyint and we also
22746  * free the phyint when there are no more references to it.
22747  */
22748 static void
22749 ill_phyint_free(ill_t *ill)
22750 {
22751 	phyint_t *phyi;
22752 	phyint_t *next_phyint;
22753 	ipsq_t *cur_ipsq;
22754 	ip_stack_t	*ipst = ill->ill_ipst;
22755 
22756 	ASSERT(ill->ill_phyint != NULL);
22757 
22758 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22759 	phyi = ill->ill_phyint;
22760 	ill->ill_phyint = NULL;
22761 	/*
22762 	 * ill_init allocates a phyint always to store the copy
22763 	 * of flags relevant to phyint. At that point in time, we could
22764 	 * not assign the name and hence phyint_illv4/v6 could not be
22765 	 * initialized. Later in ipif_set_values, we assign the name to
22766 	 * the ill, at which point in time we assign phyint_illv4/v6.
22767 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22768 	 */
22769 	if (ill->ill_flags & ILLF_IPV6) {
22770 		phyi->phyint_illv6 = NULL;
22771 	} else {
22772 		phyi->phyint_illv4 = NULL;
22773 	}
22774 	/*
22775 	 * ipif_down removes it from the group when the last ipif goes
22776 	 * down.
22777 	 */
22778 	ASSERT(ill->ill_group == NULL);
22779 
22780 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22781 		return;
22782 
22783 	/*
22784 	 * Make sure this phyint was put in the list.
22785 	 */
22786 	if (phyi->phyint_ifindex > 0) {
22787 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22788 		    phyi);
22789 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22790 		    phyi);
22791 	}
22792 	/*
22793 	 * remove phyint from the ipsq list.
22794 	 */
22795 	cur_ipsq = phyi->phyint_ipsq;
22796 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22797 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22798 	} else {
22799 		next_phyint = cur_ipsq->ipsq_phyint_list;
22800 		while (next_phyint != NULL) {
22801 			if (next_phyint->phyint_ipsq_next == phyi) {
22802 				next_phyint->phyint_ipsq_next =
22803 				    phyi->phyint_ipsq_next;
22804 				break;
22805 			}
22806 			next_phyint = next_phyint->phyint_ipsq_next;
22807 		}
22808 		ASSERT(next_phyint != NULL);
22809 	}
22810 	IPSQ_DEC_REF(cur_ipsq, ipst);
22811 
22812 	if (phyi->phyint_groupname_len != 0) {
22813 		ASSERT(phyi->phyint_groupname != NULL);
22814 		mi_free(phyi->phyint_groupname);
22815 	}
22816 	mi_free(phyi);
22817 }
22818 
22819 /*
22820  * Attach the ill to the phyint structure which can be shared by both
22821  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22822  * function is called from ipif_set_values and ill_lookup_on_name (for
22823  * loopback) where we know the name of the ill. We lookup the ill and if
22824  * there is one present already with the name use that phyint. Otherwise
22825  * reuse the one allocated by ill_init.
22826  */
22827 static void
22828 ill_phyint_reinit(ill_t *ill)
22829 {
22830 	boolean_t isv6 = ill->ill_isv6;
22831 	phyint_t *phyi_old;
22832 	phyint_t *phyi;
22833 	avl_index_t where = 0;
22834 	ill_t	*ill_other = NULL;
22835 	ipsq_t	*ipsq;
22836 	ip_stack_t	*ipst = ill->ill_ipst;
22837 
22838 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22839 
22840 	phyi_old = ill->ill_phyint;
22841 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22842 	    phyi_old->phyint_illv6 == NULL));
22843 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22844 	    phyi_old->phyint_illv4 == NULL));
22845 	ASSERT(phyi_old->phyint_ifindex == 0);
22846 
22847 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22848 	    ill->ill_name, &where);
22849 
22850 	/*
22851 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22852 	 *    the global list of ills. So no other thread could have located
22853 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22854 	 * 2. Now locate the other protocol instance of this ill.
22855 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22856 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22857 	 *    of neither ill can change.
22858 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22859 	 *    other ill.
22860 	 * 5. Release all locks.
22861 	 */
22862 
22863 	/*
22864 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22865 	 * we are initializing IPv4.
22866 	 */
22867 	if (phyi != NULL) {
22868 		ill_other = (isv6) ? phyi->phyint_illv4 :
22869 		    phyi->phyint_illv6;
22870 		ASSERT(ill_other->ill_phyint != NULL);
22871 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22872 		    (!isv6 && ill_other->ill_isv6));
22873 		GRAB_ILL_LOCKS(ill, ill_other);
22874 		/*
22875 		 * We are potentially throwing away phyint_flags which
22876 		 * could be different from the one that we obtain from
22877 		 * ill_other->ill_phyint. But it is okay as we are assuming
22878 		 * that the state maintained within IP is correct.
22879 		 */
22880 		mutex_enter(&phyi->phyint_lock);
22881 		if (isv6) {
22882 			ASSERT(phyi->phyint_illv6 == NULL);
22883 			phyi->phyint_illv6 = ill;
22884 		} else {
22885 			ASSERT(phyi->phyint_illv4 == NULL);
22886 			phyi->phyint_illv4 = ill;
22887 		}
22888 		/*
22889 		 * This is a new ill, currently undergoing SLIFNAME
22890 		 * So we could not have joined an IPMP group until now.
22891 		 */
22892 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22893 		    phyi_old->phyint_groupname == NULL);
22894 
22895 		/*
22896 		 * This phyi_old is going away. Decref ipsq_refs and
22897 		 * assert it is zero. The ipsq itself will be freed in
22898 		 * ipsq_exit
22899 		 */
22900 		ipsq = phyi_old->phyint_ipsq;
22901 		IPSQ_DEC_REF(ipsq, ipst);
22902 		ASSERT(ipsq->ipsq_refs == 0);
22903 		/* Get the singleton phyint out of the ipsq list */
22904 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22905 		ipsq->ipsq_phyint_list = NULL;
22906 		phyi_old->phyint_illv4 = NULL;
22907 		phyi_old->phyint_illv6 = NULL;
22908 		mi_free(phyi_old);
22909 	} else {
22910 		mutex_enter(&ill->ill_lock);
22911 		/*
22912 		 * We don't need to acquire any lock, since
22913 		 * the ill is not yet visible globally  and we
22914 		 * have not yet released the ill_g_lock.
22915 		 */
22916 		phyi = phyi_old;
22917 		mutex_enter(&phyi->phyint_lock);
22918 		/* XXX We need a recovery strategy here. */
22919 		if (!phyint_assign_ifindex(phyi, ipst))
22920 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22921 
22922 		/* No IPMP group yet, thus the hook uses the ifindex */
22923 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22924 
22925 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22926 		    (void *)phyi, where);
22927 
22928 		(void) avl_find(&ipst->ips_phyint_g_list->
22929 		    phyint_list_avl_by_index,
22930 		    &phyi->phyint_ifindex, &where);
22931 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22932 		    (void *)phyi, where);
22933 	}
22934 
22935 	/*
22936 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22937 	 * pending mp is not affected because that is per ill basis.
22938 	 */
22939 	ill->ill_phyint = phyi;
22940 
22941 	/*
22942 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22943 	 * We do this here as when the first ipif was allocated,
22944 	 * ipif_allocate does not know the right interface index.
22945 	 */
22946 
22947 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22948 	/*
22949 	 * Now that the phyint's ifindex has been assigned, complete the
22950 	 * remaining
22951 	 */
22952 
22953 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22954 	if (ill->ill_isv6) {
22955 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22956 		    ill->ill_phyint->phyint_ifindex;
22957 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22958 	} else {
22959 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22960 	}
22961 
22962 	/*
22963 	 * Generate an event within the hooks framework to indicate that
22964 	 * a new interface has just been added to IP.  For this event to
22965 	 * be generated, the network interface must, at least, have an
22966 	 * ifindex assigned to it.
22967 	 *
22968 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22969 	 * that the ordering of delivered events to listeners matches the
22970 	 * order of them in the kernel.
22971 	 *
22972 	 * This function could be called from ill_lookup_on_name. In that case
22973 	 * the interface is loopback "lo", which will not generate a NIC event.
22974 	 */
22975 	if (ill->ill_name_length <= 2 ||
22976 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22977 		/*
22978 		 * Generate nic plumb event for ill_name even if
22979 		 * ipmp_hook_emulation is set. That avoids generating events
22980 		 * for the ill_names should ipmp_hook_emulation be turned on
22981 		 * later.
22982 		 */
22983 		ill_nic_info_plumb(ill, B_FALSE);
22984 	}
22985 	RELEASE_ILL_LOCKS(ill, ill_other);
22986 	mutex_exit(&phyi->phyint_lock);
22987 }
22988 
22989 /*
22990  * Allocate a NE_PLUMB nic info event and store in the ill.
22991  * If 'group' is set we do it for the group name, otherwise the ill name.
22992  * It will be sent when we leave the ipsq.
22993  */
22994 void
22995 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22996 {
22997 	phyint_t	*phyi = ill->ill_phyint;
22998 	ip_stack_t	*ipst = ill->ill_ipst;
22999 	hook_nic_event_t *info;
23000 	char		*name;
23001 	int		namelen;
23002 
23003 	ASSERT(MUTEX_HELD(&ill->ill_lock));
23004 
23005 	if ((info = ill->ill_nic_event_info) != NULL) {
23006 		ip2dbg(("ill_nic_info_plumb: unexpected nic event %d "
23007 		    "attached for %s\n", info->hne_event,
23008 		    ill->ill_name));
23009 		if (info->hne_data != NULL)
23010 			kmem_free(info->hne_data, info->hne_datalen);
23011 		kmem_free(info, sizeof (hook_nic_event_t));
23012 		ill->ill_nic_event_info = NULL;
23013 	}
23014 
23015 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
23016 	if (info == NULL) {
23017 		ip2dbg(("ill_nic_info_plumb: could not attach PLUMB nic "
23018 		    "event information for %s (ENOMEM)\n",
23019 		    ill->ill_name));
23020 		return;
23021 	}
23022 
23023 	if (group) {
23024 		ASSERT(phyi->phyint_groupname_len != 0);
23025 		namelen = phyi->phyint_groupname_len;
23026 		name = phyi->phyint_groupname;
23027 	} else {
23028 		namelen = ill->ill_name_length;
23029 		name = ill->ill_name;
23030 	}
23031 
23032 	info->hne_nic = phyi->phyint_hook_ifindex;
23033 	info->hne_lif = 0;
23034 	info->hne_event = NE_PLUMB;
23035 	info->hne_family = ill->ill_isv6 ?
23036 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
23037 
23038 	info->hne_data = kmem_alloc(namelen, KM_NOSLEEP);
23039 	if (info->hne_data != NULL) {
23040 		info->hne_datalen = namelen;
23041 		bcopy(name, info->hne_data, info->hne_datalen);
23042 	} else {
23043 		ip2dbg(("ill_nic_info_plumb: could not attach "
23044 		    "name information for PLUMB nic event "
23045 		    "of %s (ENOMEM)\n", name));
23046 		kmem_free(info, sizeof (hook_nic_event_t));
23047 		info = NULL;
23048 	}
23049 	ill->ill_nic_event_info = info;
23050 }
23051 
23052 /*
23053  * Unhook the nic event message from the ill and enqueue it
23054  * into the nic event taskq.
23055  */
23056 void
23057 ill_nic_info_dispatch(ill_t *ill)
23058 {
23059 	hook_nic_event_t *info;
23060 
23061 	ASSERT(MUTEX_HELD(&ill->ill_lock));
23062 
23063 	if ((info = ill->ill_nic_event_info) != NULL) {
23064 		if (ddi_taskq_dispatch(eventq_queue_nic,
23065 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
23066 			ip2dbg(("ill_nic_info_dispatch: "
23067 			    "ddi_taskq_dispatch failed\n"));
23068 			if (info->hne_data != NULL)
23069 				kmem_free(info->hne_data, info->hne_datalen);
23070 			kmem_free(info, sizeof (hook_nic_event_t));
23071 		}
23072 		ill->ill_nic_event_info = NULL;
23073 	}
23074 }
23075 
23076 /*
23077  * Notify any downstream modules of the name of this interface.
23078  * An M_IOCTL is used even though we don't expect a successful reply.
23079  * Any reply message from the driver (presumably an M_IOCNAK) will
23080  * eventually get discarded somewhere upstream.  The message format is
23081  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
23082  * to IP.
23083  */
23084 static void
23085 ip_ifname_notify(ill_t *ill, queue_t *q)
23086 {
23087 	mblk_t *mp1, *mp2;
23088 	struct iocblk *iocp;
23089 	struct lifreq *lifr;
23090 
23091 	mp1 = mkiocb(SIOCSLIFNAME);
23092 	if (mp1 == NULL)
23093 		return;
23094 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
23095 	if (mp2 == NULL) {
23096 		freeb(mp1);
23097 		return;
23098 	}
23099 
23100 	mp1->b_cont = mp2;
23101 	iocp = (struct iocblk *)mp1->b_rptr;
23102 	iocp->ioc_count = sizeof (struct lifreq);
23103 
23104 	lifr = (struct lifreq *)mp2->b_rptr;
23105 	mp2->b_wptr += sizeof (struct lifreq);
23106 	bzero(lifr, sizeof (struct lifreq));
23107 
23108 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
23109 	lifr->lifr_ppa = ill->ill_ppa;
23110 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
23111 
23112 	putnext(q, mp1);
23113 }
23114 
23115 static int
23116 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
23117 {
23118 	int err;
23119 	ip_stack_t	*ipst = ill->ill_ipst;
23120 
23121 	/* Set the obsolete NDD per-interface forwarding name. */
23122 	err = ill_set_ndd_name(ill);
23123 	if (err != 0) {
23124 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
23125 		    err);
23126 	}
23127 
23128 	/* Tell downstream modules where they are. */
23129 	ip_ifname_notify(ill, q);
23130 
23131 	/*
23132 	 * ill_dl_phys returns EINPROGRESS in the usual case.
23133 	 * Error cases are ENOMEM ...
23134 	 */
23135 	err = ill_dl_phys(ill, ipif, mp, q);
23136 
23137 	/*
23138 	 * If there is no IRE expiration timer running, get one started.
23139 	 * igmp and mld timers will be triggered by the first multicast
23140 	 */
23141 	if (ipst->ips_ip_ire_expire_id == 0) {
23142 		/*
23143 		 * acquire the lock and check again.
23144 		 */
23145 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
23146 		if (ipst->ips_ip_ire_expire_id == 0) {
23147 			ipst->ips_ip_ire_expire_id = timeout(
23148 			    ip_trash_timer_expire, ipst,
23149 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
23150 		}
23151 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
23152 	}
23153 
23154 	if (ill->ill_isv6) {
23155 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
23156 		if (ipst->ips_mld_slowtimeout_id == 0) {
23157 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
23158 			    (void *)ipst,
23159 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
23160 		}
23161 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
23162 	} else {
23163 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
23164 		if (ipst->ips_igmp_slowtimeout_id == 0) {
23165 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
23166 			    (void *)ipst,
23167 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
23168 		}
23169 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
23170 	}
23171 
23172 	return (err);
23173 }
23174 
23175 /*
23176  * Common routine for ppa and ifname setting. Should be called exclusive.
23177  *
23178  * Returns EINPROGRESS when mp has been consumed by queueing it on
23179  * ill_pending_mp and the ioctl will complete in ip_rput.
23180  *
23181  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
23182  * the new name and new ppa in lifr_name and lifr_ppa respectively.
23183  * For SLIFNAME, we pass these values back to the userland.
23184  */
23185 static int
23186 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
23187 {
23188 	ill_t	*ill;
23189 	ipif_t	*ipif;
23190 	ipsq_t	*ipsq;
23191 	char	*ppa_ptr;
23192 	char	*old_ptr;
23193 	char	old_char;
23194 	int	error;
23195 	ip_stack_t	*ipst;
23196 
23197 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
23198 	ASSERT(q->q_next != NULL);
23199 	ASSERT(interf_name != NULL);
23200 
23201 	ill = (ill_t *)q->q_ptr;
23202 	ipst = ill->ill_ipst;
23203 
23204 	ASSERT(ill->ill_ipst != NULL);
23205 	ASSERT(ill->ill_name[0] == '\0');
23206 	ASSERT(IAM_WRITER_ILL(ill));
23207 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
23208 	ASSERT(ill->ill_ppa == UINT_MAX);
23209 
23210 	/* The ppa is sent down by ifconfig or is chosen */
23211 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
23212 		return (EINVAL);
23213 	}
23214 
23215 	/*
23216 	 * make sure ppa passed in is same as ppa in the name.
23217 	 * This check is not made when ppa == UINT_MAX in that case ppa
23218 	 * in the name could be anything. System will choose a ppa and
23219 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
23220 	 */
23221 	if (*new_ppa_ptr != UINT_MAX) {
23222 		/* stoi changes the pointer */
23223 		old_ptr = ppa_ptr;
23224 		/*
23225 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
23226 		 * (they don't have an externally visible ppa).  We assign one
23227 		 * here so that we can manage the interface.  Note that in
23228 		 * the past this value was always 0 for DLPI 1 drivers.
23229 		 */
23230 		if (*new_ppa_ptr == 0)
23231 			*new_ppa_ptr = stoi(&old_ptr);
23232 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
23233 			return (EINVAL);
23234 	}
23235 	/*
23236 	 * terminate string before ppa
23237 	 * save char at that location.
23238 	 */
23239 	old_char = ppa_ptr[0];
23240 	ppa_ptr[0] = '\0';
23241 
23242 	ill->ill_ppa = *new_ppa_ptr;
23243 	/*
23244 	 * Finish as much work now as possible before calling ill_glist_insert
23245 	 * which makes the ill globally visible and also merges it with the
23246 	 * other protocol instance of this phyint. The remaining work is
23247 	 * done after entering the ipsq which may happen sometime later.
23248 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
23249 	 */
23250 	ipif = ill->ill_ipif;
23251 
23252 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
23253 	ipif_assign_seqid(ipif);
23254 
23255 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
23256 		ill->ill_flags |= ILLF_IPV4;
23257 
23258 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
23259 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
23260 
23261 	if (ill->ill_flags & ILLF_IPV6) {
23262 
23263 		ill->ill_isv6 = B_TRUE;
23264 		if (ill->ill_rq != NULL) {
23265 			ill->ill_rq->q_qinfo = &rinit_ipv6;
23266 			ill->ill_wq->q_qinfo = &winit_ipv6;
23267 		}
23268 
23269 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
23270 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
23271 		ipif->ipif_v6src_addr = ipv6_all_zeros;
23272 		ipif->ipif_v6subnet = ipv6_all_zeros;
23273 		ipif->ipif_v6net_mask = ipv6_all_zeros;
23274 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
23275 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
23276 		/*
23277 		 * point-to-point or Non-mulicast capable
23278 		 * interfaces won't do NUD unless explicitly
23279 		 * configured to do so.
23280 		 */
23281 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
23282 		    !(ill->ill_flags & ILLF_MULTICAST)) {
23283 			ill->ill_flags |= ILLF_NONUD;
23284 		}
23285 		/* Make sure IPv4 specific flag is not set on IPv6 if */
23286 		if (ill->ill_flags & ILLF_NOARP) {
23287 			/*
23288 			 * Note: xresolv interfaces will eventually need
23289 			 * NOARP set here as well, but that will require
23290 			 * those external resolvers to have some
23291 			 * knowledge of that flag and act appropriately.
23292 			 * Not to be changed at present.
23293 			 */
23294 			ill->ill_flags &= ~ILLF_NOARP;
23295 		}
23296 		/*
23297 		 * Set the ILLF_ROUTER flag according to the global
23298 		 * IPv6 forwarding policy.
23299 		 */
23300 		if (ipst->ips_ipv6_forward != 0)
23301 			ill->ill_flags |= ILLF_ROUTER;
23302 	} else if (ill->ill_flags & ILLF_IPV4) {
23303 		ill->ill_isv6 = B_FALSE;
23304 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
23305 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
23306 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
23307 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
23308 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
23309 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
23310 		/*
23311 		 * Set the ILLF_ROUTER flag according to the global
23312 		 * IPv4 forwarding policy.
23313 		 */
23314 		if (ipst->ips_ip_g_forward != 0)
23315 			ill->ill_flags |= ILLF_ROUTER;
23316 	}
23317 
23318 	ASSERT(ill->ill_phyint != NULL);
23319 
23320 	/*
23321 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23322 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23323 	 */
23324 	if (!ill_allocate_mibs(ill))
23325 		return (ENOMEM);
23326 
23327 	/*
23328 	 * Pick a default sap until we get the DL_INFO_ACK back from
23329 	 * the driver.
23330 	 */
23331 	if (ill->ill_sap == 0) {
23332 		if (ill->ill_isv6)
23333 			ill->ill_sap  = IP6_DL_SAP;
23334 		else
23335 			ill->ill_sap  = IP_DL_SAP;
23336 	}
23337 
23338 	ill->ill_ifname_pending = 1;
23339 	ill->ill_ifname_pending_err = 0;
23340 
23341 	ill_refhold(ill);
23342 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23343 	if ((error = ill_glist_insert(ill, interf_name,
23344 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23345 		ill->ill_ppa = UINT_MAX;
23346 		ill->ill_name[0] = '\0';
23347 		/*
23348 		 * undo null termination done above.
23349 		 */
23350 		ppa_ptr[0] = old_char;
23351 		rw_exit(&ipst->ips_ill_g_lock);
23352 		ill_refrele(ill);
23353 		return (error);
23354 	}
23355 
23356 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23357 
23358 	/*
23359 	 * When we return the buffer pointed to by interf_name should contain
23360 	 * the same name as in ill_name.
23361 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23362 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23363 	 * so copy full name and update the ppa ptr.
23364 	 * When ppa passed in != UINT_MAX all values are correct just undo
23365 	 * null termination, this saves a bcopy.
23366 	 */
23367 	if (*new_ppa_ptr == UINT_MAX) {
23368 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23369 		*new_ppa_ptr = ill->ill_ppa;
23370 	} else {
23371 		/*
23372 		 * undo null termination done above.
23373 		 */
23374 		ppa_ptr[0] = old_char;
23375 	}
23376 
23377 	/* Let SCTP know about this ILL */
23378 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23379 
23380 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23381 	    B_TRUE);
23382 
23383 	rw_exit(&ipst->ips_ill_g_lock);
23384 	ill_refrele(ill);
23385 	if (ipsq == NULL)
23386 		return (EINPROGRESS);
23387 
23388 	/*
23389 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23390 	 */
23391 	if (ipsq->ipsq_current_ipif == NULL)
23392 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23393 	else
23394 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23395 
23396 	error = ipif_set_values_tail(ill, ipif, mp, q);
23397 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23398 	if (error != 0 && error != EINPROGRESS) {
23399 		/*
23400 		 * restore previous values
23401 		 */
23402 		ill->ill_isv6 = B_FALSE;
23403 	}
23404 	return (error);
23405 }
23406 
23407 
23408 void
23409 ipif_init(ip_stack_t *ipst)
23410 {
23411 	hrtime_t hrt;
23412 	int i;
23413 
23414 	/*
23415 	 * Can't call drv_getparm here as it is too early in the boot.
23416 	 * As we use ipif_src_random just for picking a different
23417 	 * source address everytime, this need not be really random.
23418 	 */
23419 	hrt = gethrtime();
23420 	ipst->ips_ipif_src_random =
23421 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23422 
23423 	for (i = 0; i < MAX_G_HEADS; i++) {
23424 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23425 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23426 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23427 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23428 	}
23429 
23430 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23431 	    ill_phyint_compare_index,
23432 	    sizeof (phyint_t),
23433 	    offsetof(struct phyint, phyint_avl_by_index));
23434 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23435 	    ill_phyint_compare_name,
23436 	    sizeof (phyint_t),
23437 	    offsetof(struct phyint, phyint_avl_by_name));
23438 }
23439 
23440 /*
23441  * This is called by ip_rt_add when src_addr value is other than zero.
23442  * src_addr signifies the source address of the incoming packet. For
23443  * reverse tunnel route we need to create a source addr based routing
23444  * table. This routine creates ip_mrtun_table if it's empty and then
23445  * it adds the route entry hashed by source address. It verifies that
23446  * the outgoing interface is always a non-resolver interface (tunnel).
23447  */
23448 int
23449 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
23450     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func,
23451     ip_stack_t *ipst)
23452 {
23453 	ire_t   *ire;
23454 	ire_t	*save_ire;
23455 	ipif_t  *ipif;
23456 	ill_t   *in_ill = NULL;
23457 	ill_t	*out_ill;
23458 	queue_t	*stq;
23459 	mblk_t	*dlureq_mp;
23460 	int	error;
23461 
23462 	if (ire_arg != NULL)
23463 		*ire_arg = NULL;
23464 	ASSERT(in_src_addr != INADDR_ANY);
23465 
23466 	ipif = ipif_arg;
23467 	if (ipif != NULL) {
23468 		out_ill = ipif->ipif_ill;
23469 	} else {
23470 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
23471 		return (EINVAL);
23472 	}
23473 
23474 	if (src_ipif == NULL) {
23475 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
23476 		return (EINVAL);
23477 	}
23478 	in_ill = src_ipif->ipif_ill;
23479 
23480 	/*
23481 	 * Check for duplicates. We don't need to
23482 	 * match out_ill, because the uniqueness of
23483 	 * a route is only dependent on src_addr and
23484 	 * in_ill.
23485 	 */
23486 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
23487 	if (ire != NULL) {
23488 		ire_refrele(ire);
23489 		return (EEXIST);
23490 	}
23491 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
23492 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
23493 		    ipif->ipif_net_type));
23494 		return (EINVAL);
23495 	}
23496 
23497 	stq = ipif->ipif_wq;
23498 	ASSERT(stq != NULL);
23499 
23500 	/*
23501 	 * The outgoing interface must be non-resolver
23502 	 * interface.
23503 	 */
23504 	dlureq_mp = ill_dlur_gen(NULL,
23505 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
23506 	    out_ill->ill_sap_length);
23507 
23508 	if (dlureq_mp == NULL) {
23509 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
23510 		return (ENOMEM);
23511 	}
23512 
23513 	/* Create the IRE. */
23514 
23515 	ire = ire_create(
23516 	    NULL,				/* Zero dst addr */
23517 	    NULL,				/* Zero mask */
23518 	    NULL,				/* Zero gateway addr */
23519 	    NULL,				/* Zero ipif_src addr */
23520 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
23521 	    &ipif->ipif_mtu,
23522 	    NULL,
23523 	    NULL,				/* rfq */
23524 	    stq,
23525 	    IRE_MIPRTUN,
23526 	    ipif,
23527 	    in_ill,
23528 	    0,
23529 	    0,
23530 	    0,
23531 	    flags,
23532 	    &ire_uinfo_null,
23533 	    NULL,
23534 	    NULL,
23535 	    ipst);
23536 
23537 	if (ire == NULL) {
23538 		freeb(dlureq_mp);
23539 		return (ENOMEM);
23540 	}
23541 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
23542 	    ire->ire_type));
23543 	save_ire = ire;
23544 	ASSERT(save_ire != NULL);
23545 	error = ire_add_mrtun(&ire, q, mp, func);
23546 	/*
23547 	 * If ire_add_mrtun() failed, the ire passed in was freed
23548 	 * so there is no need to do so here.
23549 	 */
23550 	if (error != 0) {
23551 		return (error);
23552 	}
23553 
23554 	/* Duplicate check */
23555 	if (ire != save_ire) {
23556 		/* route already exists by now */
23557 		ire_refrele(ire);
23558 		return (EEXIST);
23559 	}
23560 
23561 	if (ire_arg != NULL) {
23562 		/*
23563 		 * Store the ire that was just added. the caller
23564 		 * ip_rts_request responsible for doing ire_refrele()
23565 		 * on it.
23566 		 */
23567 		*ire_arg = ire;
23568 	} else {
23569 		ire_refrele(ire);	/* held in ire_add_mrtun */
23570 	}
23571 
23572 	return (0);
23573 }
23574 
23575 /*
23576  * It is called by ip_rt_delete() only when mipagent requests to delete
23577  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
23578  */
23579 
23580 int
23581 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
23582 {
23583 	ire_t   *ire = NULL;
23584 
23585 	if (in_src_addr == INADDR_ANY)
23586 		return (EINVAL);
23587 	if (src_ipif == NULL)
23588 		return (EINVAL);
23589 
23590 	/* search if this route exists in the ip_mrtun_table */
23591 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
23592 	if (ire == NULL) {
23593 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
23594 		return (ESRCH);
23595 	}
23596 	ire_delete(ire);
23597 	ire_refrele(ire);
23598 	return (0);
23599 }
23600 
23601 /*
23602  * Lookup the ipif corresponding to the onlink destination address. For
23603  * point-to-point interfaces, it matches with remote endpoint destination
23604  * address. For point-to-multipoint interfaces it only tries to match the
23605  * destination with the interface's subnet address. The longest, most specific
23606  * match is found to take care of such rare network configurations like -
23607  * le0: 129.146.1.1/16
23608  * le1: 129.146.2.2/24
23609  * It is used only by SO_DONTROUTE at the moment.
23610  */
23611 ipif_t *
23612 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23613 {
23614 	ipif_t	*ipif, *best_ipif;
23615 	ill_t	*ill;
23616 	ill_walk_context_t ctx;
23617 
23618 	ASSERT(zoneid != ALL_ZONES);
23619 	best_ipif = NULL;
23620 
23621 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23622 	ill = ILL_START_WALK_V4(&ctx, ipst);
23623 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23624 		mutex_enter(&ill->ill_lock);
23625 		for (ipif = ill->ill_ipif; ipif != NULL;
23626 		    ipif = ipif->ipif_next) {
23627 			if (!IPIF_CAN_LOOKUP(ipif))
23628 				continue;
23629 			if (ipif->ipif_zoneid != zoneid &&
23630 			    ipif->ipif_zoneid != ALL_ZONES)
23631 				continue;
23632 			/*
23633 			 * Point-to-point case. Look for exact match with
23634 			 * destination address.
23635 			 */
23636 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23637 				if (ipif->ipif_pp_dst_addr == addr) {
23638 					ipif_refhold_locked(ipif);
23639 					mutex_exit(&ill->ill_lock);
23640 					rw_exit(&ipst->ips_ill_g_lock);
23641 					if (best_ipif != NULL)
23642 						ipif_refrele(best_ipif);
23643 					return (ipif);
23644 				}
23645 			} else if (ipif->ipif_subnet == (addr &
23646 			    ipif->ipif_net_mask)) {
23647 				/*
23648 				 * Point-to-multipoint case. Looping through to
23649 				 * find the most specific match. If there are
23650 				 * multiple best match ipif's then prefer ipif's
23651 				 * that are UP. If there is only one best match
23652 				 * ipif and it is DOWN we must still return it.
23653 				 */
23654 				if ((best_ipif == NULL) ||
23655 				    (ipif->ipif_net_mask >
23656 				    best_ipif->ipif_net_mask) ||
23657 				    ((ipif->ipif_net_mask ==
23658 				    best_ipif->ipif_net_mask) &&
23659 				    ((ipif->ipif_flags & IPIF_UP) &&
23660 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23661 					ipif_refhold_locked(ipif);
23662 					mutex_exit(&ill->ill_lock);
23663 					rw_exit(&ipst->ips_ill_g_lock);
23664 					if (best_ipif != NULL)
23665 						ipif_refrele(best_ipif);
23666 					best_ipif = ipif;
23667 					rw_enter(&ipst->ips_ill_g_lock,
23668 					    RW_READER);
23669 					mutex_enter(&ill->ill_lock);
23670 				}
23671 			}
23672 		}
23673 		mutex_exit(&ill->ill_lock);
23674 	}
23675 	rw_exit(&ipst->ips_ill_g_lock);
23676 	return (best_ipif);
23677 }
23678 
23679 
23680 /*
23681  * Save enough information so that we can recreate the IRE if
23682  * the interface goes down and then up.
23683  */
23684 static void
23685 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23686 {
23687 	mblk_t	*save_mp;
23688 
23689 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23690 	if (save_mp != NULL) {
23691 		ifrt_t	*ifrt;
23692 
23693 		save_mp->b_wptr += sizeof (ifrt_t);
23694 		ifrt = (ifrt_t *)save_mp->b_rptr;
23695 		bzero(ifrt, sizeof (ifrt_t));
23696 		ifrt->ifrt_type = ire->ire_type;
23697 		ifrt->ifrt_addr = ire->ire_addr;
23698 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23699 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23700 		ifrt->ifrt_mask = ire->ire_mask;
23701 		ifrt->ifrt_flags = ire->ire_flags;
23702 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23703 		mutex_enter(&ipif->ipif_saved_ire_lock);
23704 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23705 		ipif->ipif_saved_ire_mp = save_mp;
23706 		ipif->ipif_saved_ire_cnt++;
23707 		mutex_exit(&ipif->ipif_saved_ire_lock);
23708 	}
23709 }
23710 
23711 
23712 static void
23713 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23714 {
23715 	mblk_t	**mpp;
23716 	mblk_t	*mp;
23717 	ifrt_t	*ifrt;
23718 
23719 	/* Remove from ipif_saved_ire_mp list if it is there */
23720 	mutex_enter(&ipif->ipif_saved_ire_lock);
23721 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23722 	    mpp = &(*mpp)->b_cont) {
23723 		/*
23724 		 * On a given ipif, the triple of address, gateway and
23725 		 * mask is unique for each saved IRE (in the case of
23726 		 * ordinary interface routes, the gateway address is
23727 		 * all-zeroes).
23728 		 */
23729 		mp = *mpp;
23730 		ifrt = (ifrt_t *)mp->b_rptr;
23731 		if (ifrt->ifrt_addr == ire->ire_addr &&
23732 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23733 		    ifrt->ifrt_mask == ire->ire_mask) {
23734 			*mpp = mp->b_cont;
23735 			ipif->ipif_saved_ire_cnt--;
23736 			freeb(mp);
23737 			break;
23738 		}
23739 	}
23740 	mutex_exit(&ipif->ipif_saved_ire_lock);
23741 }
23742 
23743 
23744 /*
23745  * IP multirouting broadcast routes handling
23746  * Append CGTP broadcast IREs to regular ones created
23747  * at ifconfig time.
23748  */
23749 static void
23750 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23751 {
23752 	ire_t *ire_prim;
23753 
23754 	ASSERT(ire != NULL);
23755 	ASSERT(ire_dst != NULL);
23756 
23757 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23758 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23759 	if (ire_prim != NULL) {
23760 		/*
23761 		 * We are in the special case of broadcasts for
23762 		 * CGTP. We add an IRE_BROADCAST that holds
23763 		 * the RTF_MULTIRT flag, the destination
23764 		 * address of ire_dst and the low level
23765 		 * info of ire_prim. In other words, CGTP
23766 		 * broadcast is added to the redundant ipif.
23767 		 */
23768 		ipif_t *ipif_prim;
23769 		ire_t  *bcast_ire;
23770 
23771 		ipif_prim = ire_prim->ire_ipif;
23772 
23773 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23774 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23775 		    (void *)ire_dst, (void *)ire_prim,
23776 		    (void *)ipif_prim));
23777 
23778 		bcast_ire = ire_create(
23779 		    (uchar_t *)&ire->ire_addr,
23780 		    (uchar_t *)&ip_g_all_ones,
23781 		    (uchar_t *)&ire_dst->ire_src_addr,
23782 		    (uchar_t *)&ire->ire_gateway_addr,
23783 		    NULL,
23784 		    &ipif_prim->ipif_mtu,
23785 		    NULL,
23786 		    ipif_prim->ipif_rq,
23787 		    ipif_prim->ipif_wq,
23788 		    IRE_BROADCAST,
23789 		    ipif_prim,
23790 		    NULL,
23791 		    0,
23792 		    0,
23793 		    0,
23794 		    ire->ire_flags,
23795 		    &ire_uinfo_null,
23796 		    NULL,
23797 		    NULL,
23798 		    ipst);
23799 
23800 		if (bcast_ire != NULL) {
23801 
23802 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23803 			    B_FALSE) == 0) {
23804 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23805 				    "added bcast_ire %p\n",
23806 				    (void *)bcast_ire));
23807 
23808 				ipif_save_ire(bcast_ire->ire_ipif,
23809 				    bcast_ire);
23810 				ire_refrele(bcast_ire);
23811 			}
23812 		}
23813 		ire_refrele(ire_prim);
23814 	}
23815 }
23816 
23817 
23818 /*
23819  * IP multirouting broadcast routes handling
23820  * Remove the broadcast ire
23821  */
23822 static void
23823 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23824 {
23825 	ire_t *ire_dst;
23826 
23827 	ASSERT(ire != NULL);
23828 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23829 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23830 	if (ire_dst != NULL) {
23831 		ire_t *ire_prim;
23832 
23833 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23834 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23835 		if (ire_prim != NULL) {
23836 			ipif_t *ipif_prim;
23837 			ire_t  *bcast_ire;
23838 
23839 			ipif_prim = ire_prim->ire_ipif;
23840 
23841 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23842 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23843 			    (void *)ire_dst, (void *)ire_prim,
23844 			    (void *)ipif_prim));
23845 
23846 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23847 			    ire->ire_gateway_addr,
23848 			    IRE_BROADCAST,
23849 			    ipif_prim, ALL_ZONES,
23850 			    NULL,
23851 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23852 			    MATCH_IRE_MASK, ipst);
23853 
23854 			if (bcast_ire != NULL) {
23855 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23856 				    "looked up bcast_ire %p\n",
23857 				    (void *)bcast_ire));
23858 				ipif_remove_ire(bcast_ire->ire_ipif,
23859 				    bcast_ire);
23860 				ire_delete(bcast_ire);
23861 			}
23862 			ire_refrele(ire_prim);
23863 		}
23864 		ire_refrele(ire_dst);
23865 	}
23866 }
23867 
23868 /*
23869  * IPsec hardware acceleration capabilities related functions.
23870  */
23871 
23872 /*
23873  * Free a per-ill IPsec capabilities structure.
23874  */
23875 static void
23876 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23877 {
23878 	if (capab->auth_hw_algs != NULL)
23879 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23880 	if (capab->encr_hw_algs != NULL)
23881 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23882 	if (capab->encr_algparm != NULL)
23883 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23884 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23885 }
23886 
23887 /*
23888  * Allocate a new per-ill IPsec capabilities structure. This structure
23889  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23890  * an array which specifies, for each algorithm, whether this algorithm
23891  * is supported by the ill or not.
23892  */
23893 static ill_ipsec_capab_t *
23894 ill_ipsec_capab_alloc(void)
23895 {
23896 	ill_ipsec_capab_t *capab;
23897 	uint_t nelems;
23898 
23899 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23900 	if (capab == NULL)
23901 		return (NULL);
23902 
23903 	/* we need one bit per algorithm */
23904 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23905 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23906 
23907 	/* allocate memory to store algorithm flags */
23908 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23909 	if (capab->encr_hw_algs == NULL)
23910 		goto nomem;
23911 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23912 	if (capab->auth_hw_algs == NULL)
23913 		goto nomem;
23914 	/*
23915 	 * Leave encr_algparm NULL for now since we won't need it half
23916 	 * the time
23917 	 */
23918 	return (capab);
23919 
23920 nomem:
23921 	ill_ipsec_capab_free(capab);
23922 	return (NULL);
23923 }
23924 
23925 /*
23926  * Resize capability array.  Since we're exclusive, this is OK.
23927  */
23928 static boolean_t
23929 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23930 {
23931 	ipsec_capab_algparm_t *nalp, *oalp;
23932 	uint32_t olen, nlen;
23933 
23934 	oalp = capab->encr_algparm;
23935 	olen = capab->encr_algparm_size;
23936 
23937 	if (oalp != NULL) {
23938 		if (algid < capab->encr_algparm_end)
23939 			return (B_TRUE);
23940 	}
23941 
23942 	nlen = (algid + 1) * sizeof (*nalp);
23943 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23944 	if (nalp == NULL)
23945 		return (B_FALSE);
23946 
23947 	if (oalp != NULL) {
23948 		bcopy(oalp, nalp, olen);
23949 		kmem_free(oalp, olen);
23950 	}
23951 	capab->encr_algparm = nalp;
23952 	capab->encr_algparm_size = nlen;
23953 	capab->encr_algparm_end = algid + 1;
23954 
23955 	return (B_TRUE);
23956 }
23957 
23958 /*
23959  * Compare the capabilities of the specified ill with the protocol
23960  * and algorithms specified by the SA passed as argument.
23961  * If they match, returns B_TRUE, B_FALSE if they do not match.
23962  *
23963  * The ill can be passed as a pointer to it, or by specifying its index
23964  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23965  *
23966  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23967  * packet is eligible for hardware acceleration, and by
23968  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23969  * to a particular ill.
23970  */
23971 boolean_t
23972 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23973     ipsa_t *sa, netstack_t *ns)
23974 {
23975 	boolean_t sa_isv6;
23976 	uint_t algid;
23977 	struct ill_ipsec_capab_s *cpp;
23978 	boolean_t need_refrele = B_FALSE;
23979 	ip_stack_t	*ipst = ns->netstack_ip;
23980 
23981 	if (ill == NULL) {
23982 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23983 		    NULL, NULL, NULL, ipst);
23984 		if (ill == NULL) {
23985 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23986 			return (B_FALSE);
23987 		}
23988 		need_refrele = B_TRUE;
23989 	}
23990 
23991 	/*
23992 	 * Use the address length specified by the SA to determine
23993 	 * if it corresponds to a IPv6 address, and fail the matching
23994 	 * if the isv6 flag passed as argument does not match.
23995 	 * Note: this check is used for SADB capability checking before
23996 	 * sending SA information to an ill.
23997 	 */
23998 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23999 	if (sa_isv6 != ill_isv6)
24000 		/* protocol mismatch */
24001 		goto done;
24002 
24003 	/*
24004 	 * Check if the ill supports the protocol, algorithm(s) and
24005 	 * key size(s) specified by the SA, and get the pointers to
24006 	 * the algorithms supported by the ill.
24007 	 */
24008 	switch (sa->ipsa_type) {
24009 
24010 	case SADB_SATYPE_ESP:
24011 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
24012 			/* ill does not support ESP acceleration */
24013 			goto done;
24014 		cpp = ill->ill_ipsec_capab_esp;
24015 		algid = sa->ipsa_auth_alg;
24016 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
24017 			goto done;
24018 		algid = sa->ipsa_encr_alg;
24019 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
24020 			goto done;
24021 		if (algid < cpp->encr_algparm_end) {
24022 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
24023 			if (sa->ipsa_encrkeybits < alp->minkeylen)
24024 				goto done;
24025 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
24026 				goto done;
24027 		}
24028 		break;
24029 
24030 	case SADB_SATYPE_AH:
24031 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
24032 			/* ill does not support AH acceleration */
24033 			goto done;
24034 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
24035 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
24036 			goto done;
24037 		break;
24038 	}
24039 
24040 	if (need_refrele)
24041 		ill_refrele(ill);
24042 	return (B_TRUE);
24043 done:
24044 	if (need_refrele)
24045 		ill_refrele(ill);
24046 	return (B_FALSE);
24047 }
24048 
24049 
24050 /*
24051  * Add a new ill to the list of IPsec capable ills.
24052  * Called from ill_capability_ipsec_ack() when an ACK was received
24053  * indicating that IPsec hardware processing was enabled for an ill.
24054  *
24055  * ill must point to the ill for which acceleration was enabled.
24056  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
24057  */
24058 static void
24059 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
24060 {
24061 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
24062 	uint_t sa_type;
24063 	uint_t ipproto;
24064 	ip_stack_t	*ipst = ill->ill_ipst;
24065 
24066 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
24067 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
24068 
24069 	switch (dl_cap) {
24070 	case DL_CAPAB_IPSEC_AH:
24071 		sa_type = SADB_SATYPE_AH;
24072 		ills = &ipst->ips_ipsec_capab_ills_ah;
24073 		ipproto = IPPROTO_AH;
24074 		break;
24075 	case DL_CAPAB_IPSEC_ESP:
24076 		sa_type = SADB_SATYPE_ESP;
24077 		ills = &ipst->ips_ipsec_capab_ills_esp;
24078 		ipproto = IPPROTO_ESP;
24079 		break;
24080 	}
24081 
24082 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
24083 
24084 	/*
24085 	 * Add ill index to list of hardware accelerators. If
24086 	 * already in list, do nothing.
24087 	 */
24088 	for (cur_ill = *ills; cur_ill != NULL &&
24089 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
24090 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
24091 		;
24092 
24093 	if (cur_ill == NULL) {
24094 		/* if this is a new entry for this ill */
24095 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
24096 		if (new_ill == NULL) {
24097 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24098 			return;
24099 		}
24100 
24101 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
24102 		new_ill->ill_isv6 = ill->ill_isv6;
24103 		new_ill->next = *ills;
24104 		*ills = new_ill;
24105 	} else if (!sadb_resync) {
24106 		/* not resync'ing SADB and an entry exists for this ill */
24107 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24108 		return;
24109 	}
24110 
24111 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24112 
24113 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
24114 		/*
24115 		 * IPsec module for protocol loaded, initiate dump
24116 		 * of the SADB to this ill.
24117 		 */
24118 		sadb_ill_download(ill, sa_type);
24119 }
24120 
24121 /*
24122  * Remove an ill from the list of IPsec capable ills.
24123  */
24124 static void
24125 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
24126 {
24127 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
24128 	ip_stack_t	*ipst = ill->ill_ipst;
24129 
24130 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
24131 	    dl_cap == DL_CAPAB_IPSEC_ESP);
24132 
24133 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
24134 	    &ipst->ips_ipsec_capab_ills_esp;
24135 
24136 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
24137 
24138 	prev_ill = NULL;
24139 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
24140 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
24141 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
24142 		;
24143 	if (cur_ill == NULL) {
24144 		/* entry not found */
24145 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24146 		return;
24147 	}
24148 	if (prev_ill == NULL) {
24149 		/* entry at front of list */
24150 		*ills = NULL;
24151 	} else {
24152 		prev_ill->next = cur_ill->next;
24153 	}
24154 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
24155 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24156 }
24157 
24158 /*
24159  * Called by SADB to send a DL_CONTROL_REQ message to every ill
24160  * supporting the specified IPsec protocol acceleration.
24161  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
24162  * We free the mblk and, if sa is non-null, release the held referece.
24163  */
24164 void
24165 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
24166     netstack_t *ns)
24167 {
24168 	ipsec_capab_ill_t *ici, *cur_ici;
24169 	ill_t *ill;
24170 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
24171 	ip_stack_t	*ipst = ns->netstack_ip;
24172 
24173 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
24174 	    ipst->ips_ipsec_capab_ills_esp;
24175 
24176 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
24177 
24178 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
24179 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
24180 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
24181 
24182 		/*
24183 		 * Handle the case where the ill goes away while the SADB is
24184 		 * attempting to send messages.  If it's going away, it's
24185 		 * nuking its shadow SADB, so we don't care..
24186 		 */
24187 
24188 		if (ill == NULL)
24189 			continue;
24190 
24191 		if (sa != NULL) {
24192 			/*
24193 			 * Make sure capabilities match before
24194 			 * sending SA to ill.
24195 			 */
24196 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
24197 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
24198 				ill_refrele(ill);
24199 				continue;
24200 			}
24201 
24202 			mutex_enter(&sa->ipsa_lock);
24203 			sa->ipsa_flags |= IPSA_F_HW;
24204 			mutex_exit(&sa->ipsa_lock);
24205 		}
24206 
24207 		/*
24208 		 * Copy template message, and add it to the front
24209 		 * of the mblk ship list. We want to avoid holding
24210 		 * the ipsec_capab_ills_lock while sending the
24211 		 * message to the ills.
24212 		 *
24213 		 * The b_next and b_prev are temporarily used
24214 		 * to build a list of mblks to be sent down, and to
24215 		 * save the ill to which they must be sent.
24216 		 */
24217 		nmp = copymsg(mp);
24218 		if (nmp == NULL) {
24219 			ill_refrele(ill);
24220 			continue;
24221 		}
24222 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
24223 		nmp->b_next = mp_ship_list;
24224 		mp_ship_list = nmp;
24225 		nmp->b_prev = (mblk_t *)ill;
24226 	}
24227 
24228 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24229 
24230 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
24231 		/* restore the mblk to a sane state */
24232 		next_mp = nmp->b_next;
24233 		nmp->b_next = NULL;
24234 		ill = (ill_t *)nmp->b_prev;
24235 		nmp->b_prev = NULL;
24236 
24237 		ill_dlpi_send(ill, nmp);
24238 		ill_refrele(ill);
24239 	}
24240 
24241 	if (sa != NULL)
24242 		IPSA_REFRELE(sa);
24243 	freemsg(mp);
24244 }
24245 
24246 /*
24247  * Derive an interface id from the link layer address.
24248  * Knows about IEEE 802 and IEEE EUI-64 mappings.
24249  */
24250 static boolean_t
24251 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24252 {
24253 	char		*addr;
24254 
24255 	if (phys_length != ETHERADDRL)
24256 		return (B_FALSE);
24257 
24258 	/* Form EUI-64 like address */
24259 	addr = (char *)&v6addr->s6_addr32[2];
24260 	bcopy((char *)phys_addr, addr, 3);
24261 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
24262 	addr[3] = (char)0xff;
24263 	addr[4] = (char)0xfe;
24264 	bcopy((char *)phys_addr + 3, addr + 5, 3);
24265 	return (B_TRUE);
24266 }
24267 
24268 /* ARGSUSED */
24269 static boolean_t
24270 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24271 {
24272 	return (B_FALSE);
24273 }
24274 
24275 /* ARGSUSED */
24276 static boolean_t
24277 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
24278     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
24279 {
24280 	/*
24281 	 * Multicast address mappings used over Ethernet/802.X.
24282 	 * This address is used as a base for mappings.
24283 	 */
24284 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
24285 	    0x00, 0x00, 0x00};
24286 
24287 	/*
24288 	 * Extract low order 32 bits from IPv6 multicast address.
24289 	 * Or that into the link layer address, starting from the
24290 	 * second byte.
24291 	 */
24292 	*hw_start = 2;
24293 	v6_extract_mask->s6_addr32[0] = 0;
24294 	v6_extract_mask->s6_addr32[1] = 0;
24295 	v6_extract_mask->s6_addr32[2] = 0;
24296 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24297 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
24298 	return (B_TRUE);
24299 }
24300 
24301 /*
24302  * Indicate by return value whether multicast is supported. If not,
24303  * this code should not touch/change any parameters.
24304  */
24305 /* ARGSUSED */
24306 static boolean_t
24307 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24308     uint32_t *hw_start, ipaddr_t *extract_mask)
24309 {
24310 	/*
24311 	 * Multicast address mappings used over Ethernet/802.X.
24312 	 * This address is used as a base for mappings.
24313 	 */
24314 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
24315 	    0x00, 0x00, 0x00 };
24316 
24317 	if (phys_length != ETHERADDRL)
24318 		return (B_FALSE);
24319 
24320 	*extract_mask = htonl(0x007fffff);
24321 	*hw_start = 2;
24322 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
24323 	return (B_TRUE);
24324 }
24325 
24326 /*
24327  * Derive IPoIB interface id from the link layer address.
24328  */
24329 static boolean_t
24330 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24331 {
24332 	char		*addr;
24333 
24334 	if (phys_length != 20)
24335 		return (B_FALSE);
24336 	addr = (char *)&v6addr->s6_addr32[2];
24337 	bcopy(phys_addr + 12, addr, 8);
24338 	/*
24339 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
24340 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
24341 	 * rules. In these cases, the IBA considers these GUIDs to be in
24342 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
24343 	 * required; vendors are required not to assign global EUI-64's
24344 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
24345 	 * of the interface identifier. Whether the GUID is in modified
24346 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
24347 	 * bit set to 1.
24348 	 */
24349 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
24350 	return (B_TRUE);
24351 }
24352 
24353 /*
24354  * Note on mapping from multicast IP addresses to IPoIB multicast link
24355  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
24356  * The format of an IPoIB multicast address is:
24357  *
24358  *  4 byte QPN      Scope Sign.  Pkey
24359  * +--------------------------------------------+
24360  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
24361  * +--------------------------------------------+
24362  *
24363  * The Scope and Pkey components are properties of the IBA port and
24364  * network interface. They can be ascertained from the broadcast address.
24365  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
24366  */
24367 
24368 static boolean_t
24369 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
24370     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
24371 {
24372 	/*
24373 	 * Base IPoIB IPv6 multicast address used for mappings.
24374 	 * Does not contain the IBA scope/Pkey values.
24375 	 */
24376 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24377 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
24378 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24379 
24380 	/*
24381 	 * Extract low order 80 bits from IPv6 multicast address.
24382 	 * Or that into the link layer address, starting from the
24383 	 * sixth byte.
24384 	 */
24385 	*hw_start = 6;
24386 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
24387 
24388 	/*
24389 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24390 	 */
24391 	*(maddr + 5) = *(bphys_addr + 5);
24392 	*(maddr + 8) = *(bphys_addr + 8);
24393 	*(maddr + 9) = *(bphys_addr + 9);
24394 
24395 	v6_extract_mask->s6_addr32[0] = 0;
24396 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
24397 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
24398 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24399 	return (B_TRUE);
24400 }
24401 
24402 static boolean_t
24403 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24404     uint32_t *hw_start, ipaddr_t *extract_mask)
24405 {
24406 	/*
24407 	 * Base IPoIB IPv4 multicast address used for mappings.
24408 	 * Does not contain the IBA scope/Pkey values.
24409 	 */
24410 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24411 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
24412 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24413 
24414 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
24415 		return (B_FALSE);
24416 
24417 	/*
24418 	 * Extract low order 28 bits from IPv4 multicast address.
24419 	 * Or that into the link layer address, starting from the
24420 	 * sixteenth byte.
24421 	 */
24422 	*extract_mask = htonl(0x0fffffff);
24423 	*hw_start = 16;
24424 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
24425 
24426 	/*
24427 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24428 	 */
24429 	*(maddr + 5) = *(bphys_addr + 5);
24430 	*(maddr + 8) = *(bphys_addr + 8);
24431 	*(maddr + 9) = *(bphys_addr + 9);
24432 	return (B_TRUE);
24433 }
24434 
24435 /*
24436  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
24437  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
24438  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
24439  * the link-local address is preferred.
24440  */
24441 boolean_t
24442 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24443 {
24444 	ipif_t	*ipif;
24445 	ipif_t	*maybe_ipif = NULL;
24446 
24447 	mutex_enter(&ill->ill_lock);
24448 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24449 		mutex_exit(&ill->ill_lock);
24450 		if (ipifp != NULL)
24451 			*ipifp = NULL;
24452 		return (B_FALSE);
24453 	}
24454 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24455 		if (!IPIF_CAN_LOOKUP(ipif))
24456 			continue;
24457 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
24458 		    ipif->ipif_zoneid != ALL_ZONES)
24459 			continue;
24460 		if ((ipif->ipif_flags & flags) != flags)
24461 			continue;
24462 
24463 		if (ipifp == NULL) {
24464 			mutex_exit(&ill->ill_lock);
24465 			ASSERT(maybe_ipif == NULL);
24466 			return (B_TRUE);
24467 		}
24468 		if (!ill->ill_isv6 ||
24469 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
24470 			ipif_refhold_locked(ipif);
24471 			mutex_exit(&ill->ill_lock);
24472 			*ipifp = ipif;
24473 			return (B_TRUE);
24474 		}
24475 		if (maybe_ipif == NULL)
24476 			maybe_ipif = ipif;
24477 	}
24478 	if (ipifp != NULL) {
24479 		if (maybe_ipif != NULL)
24480 			ipif_refhold_locked(maybe_ipif);
24481 		*ipifp = maybe_ipif;
24482 	}
24483 	mutex_exit(&ill->ill_lock);
24484 	return (maybe_ipif != NULL);
24485 }
24486 
24487 /*
24488  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24489  */
24490 boolean_t
24491 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24492 {
24493 	ill_t *illg;
24494 	ip_stack_t	*ipst = ill->ill_ipst;
24495 
24496 	/*
24497 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24498 	 */
24499 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24500 		return (B_TRUE);
24501 	}
24502 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24503 	if (ill->ill_group == NULL) {
24504 		/* ill not in a group */
24505 		rw_exit(&ipst->ips_ill_g_lock);
24506 		return (B_FALSE);
24507 	}
24508 
24509 	/*
24510 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24511 	 * group. We need to look for an ipif in the zone on all the ills in the
24512 	 * group.
24513 	 */
24514 	illg = ill->ill_group->illgrp_ill;
24515 	do {
24516 		/*
24517 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24518 		 * that it's not there.
24519 		 */
24520 		if (illg != ill &&
24521 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24522 			break;
24523 		}
24524 	} while ((illg = illg->ill_group_next) != NULL);
24525 	rw_exit(&ipst->ips_ill_g_lock);
24526 	return (illg != NULL);
24527 }
24528 
24529 /*
24530  * Check if this ill is only being used to send ICMP probes for IPMP
24531  */
24532 boolean_t
24533 ill_is_probeonly(ill_t *ill)
24534 {
24535 	/*
24536 	 * Check if the interface is FAILED, or INACTIVE
24537 	 */
24538 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24539 		return (B_TRUE);
24540 
24541 	return (B_FALSE);
24542 }
24543 
24544 /*
24545  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24546  * If a pointer to an ipif_t is returned then the caller will need to do
24547  * an ill_refrele().
24548  *
24549  * If there is no real interface which matches the ifindex, then it looks
24550  * for a group that has a matching index. In the case of a group match the
24551  * lifidx must be zero. We don't need emulate the logical interfaces
24552  * since IP Filter's use of netinfo doesn't use that.
24553  */
24554 ipif_t *
24555 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24556     ip_stack_t *ipst)
24557 {
24558 	ipif_t *ipif;
24559 	ill_t *ill;
24560 
24561 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24562 	    ipst);
24563 
24564 	if (ill == NULL) {
24565 		/* Fallback to group names only if hook_emulation set */
24566 		if (!ipst->ips_ipmp_hook_emulation)
24567 			return (NULL);
24568 
24569 		if (lifidx != 0)
24570 			return (NULL);
24571 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24572 		if (ill == NULL)
24573 			return (NULL);
24574 	}
24575 
24576 	mutex_enter(&ill->ill_lock);
24577 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24578 		mutex_exit(&ill->ill_lock);
24579 		ill_refrele(ill);
24580 		return (NULL);
24581 	}
24582 
24583 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24584 		if (!IPIF_CAN_LOOKUP(ipif))
24585 			continue;
24586 		if (lifidx == ipif->ipif_id) {
24587 			ipif_refhold_locked(ipif);
24588 			break;
24589 		}
24590 	}
24591 
24592 	mutex_exit(&ill->ill_lock);
24593 	ill_refrele(ill);
24594 	return (ipif);
24595 }
24596 
24597 /*
24598  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24599  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24600  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24601  * for details.
24602  */
24603 void
24604 ill_fastpath_flush(ill_t *ill)
24605 {
24606 	ip_stack_t *ipst = ill->ill_ipst;
24607 
24608 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24609 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24610 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24611 }
24612 
24613 /*
24614  * Set the physical address information for `ill' to the contents of the
24615  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24616  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24617  * EINPROGRESS will be returned.
24618  */
24619 int
24620 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24621 {
24622 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24623 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24624 
24625 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24626 
24627 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24628 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24629 		/* Changing DL_IPV6_TOKEN is not yet supported */
24630 		return (0);
24631 	}
24632 
24633 	/*
24634 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24635 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24636 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24637 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24638 	 */
24639 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24640 		freemsg(mp);
24641 		return (ENOMEM);
24642 	}
24643 
24644 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24645 
24646 	/*
24647 	 * If we can quiesce the ill, then set the address.  If not, then
24648 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24649 	 */
24650 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24651 	mutex_enter(&ill->ill_lock);
24652 	if (!ill_is_quiescent(ill)) {
24653 		/* call cannot fail since `conn_t *' argument is NULL */
24654 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24655 		    mp, ILL_DOWN);
24656 		mutex_exit(&ill->ill_lock);
24657 		return (EINPROGRESS);
24658 	}
24659 	mutex_exit(&ill->ill_lock);
24660 
24661 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24662 	return (0);
24663 }
24664 
24665 /*
24666  * Once the ill associated with `q' has quiesced, set its physical address
24667  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24668  * are passed (linked by b_cont), since we sometimes need to save two distinct
24669  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24670  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24671  * is quiesced, we know any stale IREs with the old address information have
24672  * already been removed, so we don't need to call ill_fastpath_flush().
24673  */
24674 /* ARGSUSED */
24675 static void
24676 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24677 {
24678 	ill_t		*ill = q->q_ptr;
24679 	mblk_t		*addrmp2 = unlinkb(addrmp);
24680 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24681 	uint_t		addrlen, addroff;
24682 
24683 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24684 
24685 	addroff	= dlindp->dl_addr_offset;
24686 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24687 
24688 	switch (dlindp->dl_data) {
24689 	case DL_IPV6_LINK_LAYER_ADDR:
24690 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24691 		freemsg(addrmp2);
24692 		break;
24693 
24694 	case DL_CURR_PHYS_ADDR:
24695 		freemsg(ill->ill_phys_addr_mp);
24696 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24697 		ill->ill_phys_addr_mp = addrmp;
24698 		ill->ill_phys_addr_length = addrlen;
24699 
24700 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24701 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24702 		else
24703 			freemsg(addrmp2);
24704 		break;
24705 	default:
24706 		ASSERT(0);
24707 	}
24708 
24709 	/*
24710 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24711 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24712 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24713 	 * brought up.
24714 	 */
24715 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24716 		ipsq_current_finish(ipsq);
24717 }
24718 
24719 /*
24720  * Helper routine for setting the ill_nd_lla fields.
24721  */
24722 void
24723 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24724 {
24725 	freemsg(ill->ill_nd_lla_mp);
24726 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24727 	ill->ill_nd_lla_mp = ndmp;
24728 	ill->ill_nd_lla_len = addrlen;
24729 }
24730 
24731 major_t IP_MAJ;
24732 #define	IP	"ip"
24733 
24734 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24735 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24736 
24737 /*
24738  * Issue REMOVEIF ioctls to have the loopback interfaces
24739  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24740  * the former going away when the user-level processes in the zone
24741  * are killed  * and the latter are cleaned up by the stream head
24742  * str_stack_shutdown callback that undoes all I_PLINKs.
24743  */
24744 void
24745 ip_loopback_cleanup(ip_stack_t *ipst)
24746 {
24747 	int error;
24748 	ldi_handle_t	lh = NULL;
24749 	ldi_ident_t	li = NULL;
24750 	int		rval;
24751 	cred_t		*cr;
24752 	struct strioctl iocb;
24753 	struct lifreq	lifreq;
24754 
24755 	IP_MAJ = ddi_name_to_major(IP);
24756 
24757 #ifdef NS_DEBUG
24758 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24759 	    ipst->ips_netstack->netstack_stackid);
24760 #endif
24761 
24762 	bzero(&lifreq, sizeof (lifreq));
24763 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24764 
24765 	error = ldi_ident_from_major(IP_MAJ, &li);
24766 	if (error) {
24767 #ifdef DEBUG
24768 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24769 		    error);
24770 #endif
24771 		return;
24772 	}
24773 
24774 	cr = zone_get_kcred(netstackid_to_zoneid(
24775 	    ipst->ips_netstack->netstack_stackid));
24776 	ASSERT(cr != NULL);
24777 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24778 	if (error) {
24779 #ifdef DEBUG
24780 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24781 		    error);
24782 #endif
24783 		goto out;
24784 	}
24785 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24786 	iocb.ic_timout = 15;
24787 	iocb.ic_len = sizeof (lifreq);
24788 	iocb.ic_dp = (char *)&lifreq;
24789 
24790 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24791 	/* LINTED - statement has no consequent */
24792 	if (error) {
24793 #ifdef NS_DEBUG
24794 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24795 		    "UDP6 error %d\n", error);
24796 #endif
24797 	}
24798 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24799 	lh = NULL;
24800 
24801 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24802 	if (error) {
24803 #ifdef NS_DEBUG
24804 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24805 		    error);
24806 #endif
24807 		goto out;
24808 	}
24809 
24810 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24811 	iocb.ic_timout = 15;
24812 	iocb.ic_len = sizeof (lifreq);
24813 	iocb.ic_dp = (char *)&lifreq;
24814 
24815 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24816 	/* LINTED - statement has no consequent */
24817 	if (error) {
24818 #ifdef NS_DEBUG
24819 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24820 		    "UDP error %d\n", error);
24821 #endif
24822 	}
24823 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24824 	lh = NULL;
24825 
24826 out:
24827 	/* Close layered handles */
24828 	if (lh)
24829 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24830 	if (li)
24831 		ldi_ident_release(li);
24832 
24833 	crfree(cr);
24834 }
24835