xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
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 ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
150 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
151 static void	ipsq_flush(ill_t *ill);
152 static void	ipsq_clean_all(ill_t *ill);
153 static void	ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring);
154 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
155     queue_t *q, mblk_t *mp, boolean_t need_up);
156 static void	ipsq_delete(ipsq_t *);
157 
158 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
159 		    boolean_t initialize);
160 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
161 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
162 static void	ipif_delete_cache_ire(ire_t *, char *);
163 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
164 static void	ipif_free(ipif_t *ipif);
165 static void	ipif_free_tail(ipif_t *ipif);
166 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
167 static void	ipif_multicast_down(ipif_t *ipif);
168 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
169 static void	ipif_set_default(ipif_t *ipif);
170 static int	ipif_set_values(queue_t *q, mblk_t *mp,
171     char *interf_name, uint_t *ppa);
172 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
173     queue_t *q);
174 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
175     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
176     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
177 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
178 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
179 
180 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
181 static int	ill_arp_off(ill_t *ill);
182 static int	ill_arp_on(ill_t *ill);
183 static void	ill_delete_interface_type(ill_if_t *);
184 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
185 static void	ill_dl_down(ill_t *ill);
186 static void	ill_down(ill_t *ill);
187 static void	ill_downi(ire_t *ire, char *ill_arg);
188 static void	ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg);
189 static void	ill_down_tail(ill_t *ill);
190 static void	ill_free_mib(ill_t *ill);
191 static void	ill_glist_delete(ill_t *);
192 static boolean_t ill_has_usable_ipif(ill_t *);
193 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
194 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
195 static void	ill_phyint_free(ill_t *ill);
196 static void	ill_phyint_reinit(ill_t *ill);
197 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
198 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
199 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
200 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
201 static void	ill_stq_cache_delete(ire_t *, char *);
202 
203 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
204 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
205 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
206     in6_addr_t *);
207 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     ipaddr_t *);
209 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
210 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
211     in6_addr_t *);
212 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
213     ipaddr_t *);
214 
215 static void	ipif_save_ire(ipif_t *, ire_t *);
216 static void	ipif_remove_ire(ipif_t *, ire_t *);
217 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
218 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
219 
220 /*
221  * Per-ill IPsec capabilities management.
222  */
223 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
224 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
225 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
226 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
227 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
228 static void ill_capability_proto(ill_t *, int, mblk_t *);
229 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
230     boolean_t);
231 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
232 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
233 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
234 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
235 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
236 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
237 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
238 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
239     dl_capability_sub_t *);
240 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
241 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
242 static void ill_capability_lso_reset(ill_t *, mblk_t **);
243 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
244 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
245 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
246 static void	ill_capability_dls_disable(ill_t *);
247 
248 static void	illgrp_cache_delete(ire_t *, char *);
249 static void	illgrp_delete(ill_t *ill);
250 static void	illgrp_reset_schednext(ill_t *ill);
251 
252 static ill_t	*ill_prev_usesrc(ill_t *);
253 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
254 static void	ill_disband_usesrc_group(ill_t *);
255 
256 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
257 
258 /*
259  * if we go over the memory footprint limit more than once in this msec
260  * interval, we'll start pruning aggressively.
261  */
262 int ip_min_frag_prune_time = 0;
263 
264 /*
265  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
266  * and the IPsec DOI
267  */
268 #define	MAX_IPSEC_ALGS	256
269 
270 #define	BITSPERBYTE	8
271 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
272 
273 #define	IPSEC_ALG_ENABLE(algs, algid) \
274 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
275 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
276 
277 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
278 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
279 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
280 
281 typedef uint8_t ipsec_capab_elem_t;
282 
283 /*
284  * Per-algorithm parameters.  Note that at present, only encryption
285  * algorithms have variable keysize (IKE does not provide a way to negotiate
286  * auth algorithm keysize).
287  *
288  * All sizes here are in bits.
289  */
290 typedef struct
291 {
292 	uint16_t	minkeylen;
293 	uint16_t	maxkeylen;
294 } ipsec_capab_algparm_t;
295 
296 /*
297  * Per-ill capabilities.
298  */
299 struct ill_ipsec_capab_s {
300 	ipsec_capab_elem_t *encr_hw_algs;
301 	ipsec_capab_elem_t *auth_hw_algs;
302 	uint32_t algs_size;	/* size of _hw_algs in bytes */
303 	/* algorithm key lengths */
304 	ipsec_capab_algparm_t *encr_algparm;
305 	uint32_t encr_algparm_size;
306 	uint32_t encr_algparm_end;
307 };
308 
309 /*
310  * The field values are larger than strictly necessary for simple
311  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
312  */
313 static area_t	ip_area_template = {
314 	AR_ENTRY_ADD,			/* area_cmd */
315 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
316 					/* area_name_offset */
317 	/* area_name_length temporarily holds this structure length */
318 	sizeof (area_t),			/* area_name_length */
319 	IP_ARP_PROTO_TYPE,		/* area_proto */
320 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
321 	IP_ADDR_LEN,			/* area_proto_addr_length */
322 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
323 					/* area_proto_mask_offset */
324 	0,				/* area_flags */
325 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
326 					/* area_hw_addr_offset */
327 	/* Zero length hw_addr_length means 'use your idea of the address' */
328 	0				/* area_hw_addr_length */
329 };
330 
331 /*
332  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
333  * support
334  */
335 static area_t	ip6_area_template = {
336 	AR_ENTRY_ADD,			/* area_cmd */
337 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
338 					/* area_name_offset */
339 	/* area_name_length temporarily holds this structure length */
340 	sizeof (area_t),			/* area_name_length */
341 	IP_ARP_PROTO_TYPE,		/* area_proto */
342 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
343 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
344 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
345 					/* area_proto_mask_offset */
346 	0,				/* area_flags */
347 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
348 					/* area_hw_addr_offset */
349 	/* Zero length hw_addr_length means 'use your idea of the address' */
350 	0				/* area_hw_addr_length */
351 };
352 
353 static ared_t	ip_ared_template = {
354 	AR_ENTRY_DELETE,
355 	sizeof (ared_t) + IP_ADDR_LEN,
356 	sizeof (ared_t),
357 	IP_ARP_PROTO_TYPE,
358 	sizeof (ared_t),
359 	IP_ADDR_LEN
360 };
361 
362 static ared_t	ip6_ared_template = {
363 	AR_ENTRY_DELETE,
364 	sizeof (ared_t) + IPV6_ADDR_LEN,
365 	sizeof (ared_t),
366 	IP_ARP_PROTO_TYPE,
367 	sizeof (ared_t),
368 	IPV6_ADDR_LEN
369 };
370 
371 /*
372  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
373  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
374  * areq is used).
375  */
376 static areq_t	ip_areq_template = {
377 	AR_ENTRY_QUERY,			/* cmd */
378 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
379 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
380 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
381 	sizeof (areq_t),			/* target addr offset */
382 	IP_ADDR_LEN,			/* target addr_length */
383 	0,				/* flags */
384 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
385 	IP_ADDR_LEN,			/* sender addr length */
386 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
387 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
388 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
389 	/* anything else filled in by the code */
390 };
391 
392 static arc_t	ip_aru_template = {
393 	AR_INTERFACE_UP,
394 	sizeof (arc_t),		/* Name offset */
395 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
396 };
397 
398 static arc_t	ip_ard_template = {
399 	AR_INTERFACE_DOWN,
400 	sizeof (arc_t),		/* Name offset */
401 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
402 };
403 
404 static arc_t	ip_aron_template = {
405 	AR_INTERFACE_ON,
406 	sizeof (arc_t),		/* Name offset */
407 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
408 };
409 
410 static arc_t	ip_aroff_template = {
411 	AR_INTERFACE_OFF,
412 	sizeof (arc_t),		/* Name offset */
413 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
414 };
415 
416 
417 static arma_t	ip_arma_multi_template = {
418 	AR_MAPPING_ADD,
419 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
420 				/* Name offset */
421 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
422 	IP_ARP_PROTO_TYPE,
423 	sizeof (arma_t),			/* proto_addr_offset */
424 	IP_ADDR_LEN,				/* proto_addr_length */
425 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
426 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
427 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
428 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
429 	IP_MAX_HW_LEN,				/* hw_addr_length */
430 	0,					/* hw_mapping_start */
431 };
432 
433 static ipft_t	ip_ioctl_ftbl[] = {
434 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
435 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
436 		IPFT_F_NO_REPLY },
437 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
438 		IPFT_F_NO_REPLY },
439 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
440 	{ 0 }
441 };
442 
443 /* Simple ICMP IP Header Template */
444 static ipha_t icmp_ipha = {
445 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
446 };
447 
448 /* Flag descriptors for ip_ipif_report */
449 static nv_t	ipif_nv_tbl[] = {
450 	{ IPIF_UP,		"UP" },
451 	{ IPIF_BROADCAST,	"BROADCAST" },
452 	{ ILLF_DEBUG,		"DEBUG" },
453 	{ PHYI_LOOPBACK,	"LOOPBACK" },
454 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
455 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
456 	{ PHYI_RUNNING,		"RUNNING" },
457 	{ ILLF_NOARP,		"NOARP" },
458 	{ PHYI_PROMISC,		"PROMISC" },
459 	{ PHYI_ALLMULTI,	"ALLMULTI" },
460 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
461 	{ ILLF_MULTICAST,	"MULTICAST" },
462 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
463 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
464 	{ IPIF_DHCPRUNNING,	"DHCP" },
465 	{ IPIF_PRIVATE,		"PRIVATE" },
466 	{ IPIF_NOXMIT,		"NOXMIT" },
467 	{ IPIF_NOLOCAL,		"NOLOCAL" },
468 	{ IPIF_DEPRECATED,	"DEPRECATED" },
469 	{ IPIF_PREFERRED,	"PREFERRED" },
470 	{ IPIF_TEMPORARY,	"TEMPORARY" },
471 	{ IPIF_ADDRCONF,	"ADDRCONF" },
472 	{ PHYI_VIRTUAL,		"VIRTUAL" },
473 	{ ILLF_ROUTER,		"ROUTER" },
474 	{ ILLF_NONUD,		"NONUD" },
475 	{ IPIF_ANYCAST,		"ANYCAST" },
476 	{ ILLF_NORTEXCH,	"NORTEXCH" },
477 	{ ILLF_IPV4,		"IPV4" },
478 	{ ILLF_IPV6,		"IPV6" },
479 	{ IPIF_MIPRUNNING,	"MIP" },
480 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
481 	{ PHYI_FAILED,		"FAILED" },
482 	{ PHYI_STANDBY,		"STANDBY" },
483 	{ PHYI_INACTIVE,	"INACTIVE" },
484 	{ PHYI_OFFLINE,		"OFFLINE" },
485 };
486 
487 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
488 
489 static ip_m_t	ip_m_tbl[] = {
490 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
491 	    ip_ether_v6intfid },
492 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
493 	    ip_nodef_v6intfid },
494 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
495 	    ip_nodef_v6intfid },
496 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
497 	    ip_nodef_v6intfid },
498 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
499 	    ip_ether_v6intfid },
500 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
501 	    ip_ib_v6intfid },
502 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
503 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
504 	    ip_nodef_v6intfid }
505 };
506 
507 static ill_t	ill_null;		/* Empty ILL for init. */
508 char	ipif_loopback_name[] = "lo0";
509 static char *ipv4_forward_suffix = ":ip_forwarding";
510 static char *ipv6_forward_suffix = ":ip6_forwarding";
511 static	sin6_t	sin6_null;	/* Zero address for quick clears */
512 static	sin_t	sin_null;	/* Zero address for quick clears */
513 
514 /* When set search for unused ipif_seqid */
515 static ipif_t	ipif_zero;
516 
517 /*
518  * ppa arena is created after these many
519  * interfaces have been plumbed.
520  */
521 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
522 
523 /*
524  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
525  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
526  * set through platform specific code (Niagara/Ontario).
527  */
528 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
529 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
530 
531 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
532 
533 static uint_t
534 ipif_rand(ip_stack_t *ipst)
535 {
536 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
537 	    12345;
538 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
539 }
540 
541 /*
542  * Allocate per-interface mibs.
543  * Returns true if ok. False otherwise.
544  *  ipsq  may not yet be allocated (loopback case ).
545  */
546 static boolean_t
547 ill_allocate_mibs(ill_t *ill)
548 {
549 	/* Already allocated? */
550 	if (ill->ill_ip_mib != NULL) {
551 		if (ill->ill_isv6)
552 			ASSERT(ill->ill_icmp6_mib != NULL);
553 		return (B_TRUE);
554 	}
555 
556 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
557 	    KM_NOSLEEP);
558 	if (ill->ill_ip_mib == NULL) {
559 		return (B_FALSE);
560 	}
561 
562 	/* Setup static information */
563 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
564 	    sizeof (mib2_ipIfStatsEntry_t));
565 	if (ill->ill_isv6) {
566 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
567 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
568 		    sizeof (mib2_ipv6AddrEntry_t));
569 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
570 		    sizeof (mib2_ipv6RouteEntry_t));
571 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
572 		    sizeof (mib2_ipv6NetToMediaEntry_t));
573 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
574 		    sizeof (ipv6_member_t));
575 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
576 		    sizeof (ipv6_grpsrc_t));
577 	} else {
578 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
579 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
580 		    sizeof (mib2_ipAddrEntry_t));
581 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
582 		    sizeof (mib2_ipRouteEntry_t));
583 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
584 		    sizeof (mib2_ipNetToMediaEntry_t));
585 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
586 		    sizeof (ip_member_t));
587 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
588 		    sizeof (ip_grpsrc_t));
589 
590 		/*
591 		 * For a v4 ill, we are done at this point, because per ill
592 		 * icmp mibs are only used for v6.
593 		 */
594 		return (B_TRUE);
595 	}
596 
597 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
598 	    KM_NOSLEEP);
599 	if (ill->ill_icmp6_mib == NULL) {
600 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
601 		ill->ill_ip_mib = NULL;
602 		return (B_FALSE);
603 	}
604 	/* static icmp info */
605 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
606 	    sizeof (mib2_ipv6IfIcmpEntry_t);
607 	/*
608 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
609 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
610 	 * -> ill_phyint_reinit
611 	 */
612 	return (B_TRUE);
613 }
614 
615 /*
616  * Common code for preparation of ARP commands.  Two points to remember:
617  * 	1) The ill_name is tacked on at the end of the allocated space so
618  *	   the templates name_offset field must contain the total space
619  *	   to allocate less the name length.
620  *
621  *	2) The templates name_length field should contain the *template*
622  *	   length.  We use it as a parameter to bcopy() and then write
623  *	   the real ill_name_length into the name_length field of the copy.
624  * (Always called as writer.)
625  */
626 mblk_t *
627 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
628 {
629 	arc_t	*arc = (arc_t *)template;
630 	char	*cp;
631 	int	len;
632 	mblk_t	*mp;
633 	uint_t	name_length = ill->ill_name_length;
634 	uint_t	template_len = arc->arc_name_length;
635 
636 	len = arc->arc_name_offset + name_length;
637 	mp = allocb(len, BPRI_HI);
638 	if (mp == NULL)
639 		return (NULL);
640 	cp = (char *)mp->b_rptr;
641 	mp->b_wptr = (uchar_t *)&cp[len];
642 	if (template_len)
643 		bcopy(template, cp, template_len);
644 	if (len > template_len)
645 		bzero(&cp[template_len], len - template_len);
646 	mp->b_datap->db_type = M_PROTO;
647 
648 	arc = (arc_t *)cp;
649 	arc->arc_name_length = name_length;
650 	cp = (char *)arc + arc->arc_name_offset;
651 	bcopy(ill->ill_name, cp, name_length);
652 
653 	if (addr) {
654 		area_t	*area = (area_t *)mp->b_rptr;
655 
656 		cp = (char *)area + area->area_proto_addr_offset;
657 		bcopy(addr, cp, area->area_proto_addr_length);
658 		if (area->area_cmd == AR_ENTRY_ADD) {
659 			cp = (char *)area;
660 			len = area->area_proto_addr_length;
661 			if (area->area_proto_mask_offset)
662 				cp += area->area_proto_mask_offset;
663 			else
664 				cp += area->area_proto_addr_offset + len;
665 			while (len-- > 0)
666 				*cp++ = (char)~0;
667 		}
668 	}
669 	return (mp);
670 }
671 
672 mblk_t *
673 ipif_area_alloc(ipif_t *ipif)
674 {
675 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
676 	    (char *)&ipif->ipif_lcl_addr));
677 }
678 
679 mblk_t *
680 ipif_ared_alloc(ipif_t *ipif)
681 {
682 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
683 	    (char *)&ipif->ipif_lcl_addr));
684 }
685 
686 mblk_t *
687 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
688 {
689 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
690 	    (char *)&addr));
691 }
692 
693 /*
694  * Completely vaporize a lower level tap and all associated interfaces.
695  * ill_delete is called only out of ip_close when the device control
696  * stream is being closed.
697  */
698 void
699 ill_delete(ill_t *ill)
700 {
701 	ipif_t	*ipif;
702 	ill_t	*prev_ill;
703 	ip_stack_t	*ipst = ill->ill_ipst;
704 
705 	/*
706 	 * ill_delete may be forcibly entering the ipsq. The previous
707 	 * ioctl may not have completed and may need to be aborted.
708 	 * ipsq_flush takes care of it. If we don't need to enter the
709 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
710 	 * ill_delete_tail is sufficient.
711 	 */
712 	ipsq_flush(ill);
713 
714 	/*
715 	 * Nuke all interfaces.  ipif_free will take down the interface,
716 	 * remove it from the list, and free the data structure.
717 	 * Walk down the ipif list and remove the logical interfaces
718 	 * first before removing the main ipif. We can't unplumb
719 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
720 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
721 	 * POINTOPOINT.
722 	 *
723 	 * If ill_ipif was not properly initialized (i.e low on memory),
724 	 * then no interfaces to clean up. In this case just clean up the
725 	 * ill.
726 	 */
727 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
728 		ipif_free(ipif);
729 
730 	/*
731 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
732 	 * So nobody can be using this mp now. Free the mp allocated for
733 	 * honoring ILLF_NOARP
734 	 */
735 	freemsg(ill->ill_arp_on_mp);
736 	ill->ill_arp_on_mp = NULL;
737 
738 	/* Clean up msgs on pending upcalls for mrouted */
739 	reset_mrt_ill(ill);
740 
741 	/*
742 	 * ipif_free -> reset_conn_ipif will remove all multicast
743 	 * references for IPv4. For IPv6, we need to do it here as
744 	 * it points only at ills.
745 	 */
746 	reset_conn_ill(ill);
747 
748 	/*
749 	 * ill_down will arrange to blow off any IRE's dependent on this
750 	 * ILL, and shut down fragmentation reassembly.
751 	 */
752 	ill_down(ill);
753 
754 	/* Let SCTP know, so that it can remove this from its list. */
755 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
756 
757 	/*
758 	 * If an address on this ILL is being used as a source address then
759 	 * clear out the pointers in other ILLs that point to this ILL.
760 	 */
761 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
762 	if (ill->ill_usesrc_grp_next != NULL) {
763 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
764 			ill_disband_usesrc_group(ill);
765 		} else {	/* consumer of the usesrc ILL */
766 			prev_ill = ill_prev_usesrc(ill);
767 			prev_ill->ill_usesrc_grp_next =
768 			    ill->ill_usesrc_grp_next;
769 		}
770 	}
771 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
772 }
773 
774 static void
775 ipif_non_duplicate(ipif_t *ipif)
776 {
777 	ill_t *ill = ipif->ipif_ill;
778 	mutex_enter(&ill->ill_lock);
779 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
780 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
781 		ASSERT(ill->ill_ipif_dup_count > 0);
782 		ill->ill_ipif_dup_count--;
783 	}
784 	mutex_exit(&ill->ill_lock);
785 }
786 
787 /*
788  * Send all deferred messages without waiting for their ACKs.
789  */
790 void
791 ill_send_all_deferred_mp(ill_t *ill)
792 {
793 	mblk_t *mp, *next;
794 
795 	/*
796 	 * Clear ill_dlpi_pending so that the message is not queued in
797 	 * ill_dlpi_send().
798 	 */
799 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
800 
801 	for (mp = ill->ill_dlpi_deferred; mp != NULL; mp = next) {
802 		next = mp->b_next;
803 		mp->b_next = NULL;
804 		ill_dlpi_send(ill, mp);
805 	}
806 	ill->ill_dlpi_deferred = NULL;
807 }
808 
809 /*
810  * ill_delete_tail is called from ip_modclose after all references
811  * to the closing ill are gone. The wait is done in ip_modclose
812  */
813 void
814 ill_delete_tail(ill_t *ill)
815 {
816 	mblk_t	**mpp;
817 	ipif_t	*ipif;
818 	ip_stack_t	*ipst = ill->ill_ipst;
819 
820 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
821 		ipif_non_duplicate(ipif);
822 		ipif_down_tail(ipif);
823 	}
824 
825 	ASSERT(ill->ill_ipif_dup_count == 0 &&
826 	    ill->ill_arp_down_mp == NULL &&
827 	    ill->ill_arp_del_mapping_mp == NULL);
828 
829 	/*
830 	 * If polling capability is enabled (which signifies direct
831 	 * upcall into IP and driver has ill saved as a handle),
832 	 * we need to make sure that unbind has completed before we
833 	 * let the ill disappear and driver no longer has any reference
834 	 * to this ill.
835 	 */
836 	mutex_enter(&ill->ill_lock);
837 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
838 		cv_wait(&ill->ill_cv, &ill->ill_lock);
839 	mutex_exit(&ill->ill_lock);
840 
841 	/*
842 	 * Clean up polling and soft ring capabilities
843 	 */
844 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
845 		ill_capability_dls_disable(ill);
846 
847 	/*
848 	 * Send the detach if there's one to send (i.e., if we're above a
849 	 * style 2 DLPI driver).
850 	 */
851 	if (ill->ill_detach_mp != NULL) {
852 		ill_dlpi_send(ill, ill->ill_detach_mp);
853 		ill->ill_detach_mp = NULL;
854 	}
855 
856 	if (ill->ill_net_type != IRE_LOOPBACK)
857 		qprocsoff(ill->ill_rq);
858 
859 	/*
860 	 * We do an ipsq_flush once again now. New messages could have
861 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
862 	 * could also have landed up if an ioctl thread had looked up
863 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
864 	 * enqueued the ioctl when we did the ipsq_flush last time.
865 	 */
866 	ipsq_flush(ill);
867 
868 	/*
869 	 * Free capabilities.
870 	 */
871 	if (ill->ill_ipsec_capab_ah != NULL) {
872 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
873 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
874 		ill->ill_ipsec_capab_ah = NULL;
875 	}
876 
877 	if (ill->ill_ipsec_capab_esp != NULL) {
878 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
879 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
880 		ill->ill_ipsec_capab_esp = NULL;
881 	}
882 
883 	if (ill->ill_mdt_capab != NULL) {
884 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
885 		ill->ill_mdt_capab = NULL;
886 	}
887 
888 	if (ill->ill_hcksum_capab != NULL) {
889 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
890 		ill->ill_hcksum_capab = NULL;
891 	}
892 
893 	if (ill->ill_zerocopy_capab != NULL) {
894 		kmem_free(ill->ill_zerocopy_capab,
895 		    sizeof (ill_zerocopy_capab_t));
896 		ill->ill_zerocopy_capab = NULL;
897 	}
898 
899 	if (ill->ill_lso_capab != NULL) {
900 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
901 		ill->ill_lso_capab = NULL;
902 	}
903 
904 	if (ill->ill_dls_capab != NULL) {
905 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
906 		ill->ill_dls_capab->ill_unbind_conn = NULL;
907 		kmem_free(ill->ill_dls_capab,
908 		    sizeof (ill_dls_capab_t) +
909 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
910 		ill->ill_dls_capab = NULL;
911 	}
912 
913 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
914 
915 	while (ill->ill_ipif != NULL)
916 		ipif_free_tail(ill->ill_ipif);
917 
918 	ill_down_tail(ill);
919 
920 	/*
921 	 * We have removed all references to ilm from conn and the ones joined
922 	 * within the kernel.
923 	 *
924 	 * We don't walk conns, mrts and ires because
925 	 *
926 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
927 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
928 	 *    ill references.
929 	 */
930 	ASSERT(ilm_walk_ill(ill) == 0);
931 	/*
932 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
933 	 * could free the phyint. No more reference to the phyint after this
934 	 * point.
935 	 */
936 	(void) ill_glist_delete(ill);
937 
938 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
939 	if (ill->ill_ndd_name != NULL)
940 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
941 	rw_exit(&ipst->ips_ip_g_nd_lock);
942 
943 
944 	if (ill->ill_frag_ptr != NULL) {
945 		uint_t count;
946 
947 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
948 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
949 		}
950 		mi_free(ill->ill_frag_ptr);
951 		ill->ill_frag_ptr = NULL;
952 		ill->ill_frag_hash_tbl = NULL;
953 	}
954 
955 	freemsg(ill->ill_nd_lla_mp);
956 	/* Free all retained control messages. */
957 	mpp = &ill->ill_first_mp_to_free;
958 	do {
959 		while (mpp[0]) {
960 			mblk_t  *mp;
961 			mblk_t  *mp1;
962 
963 			mp = mpp[0];
964 			mpp[0] = mp->b_next;
965 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
966 				mp1->b_next = NULL;
967 				mp1->b_prev = NULL;
968 			}
969 			freemsg(mp);
970 		}
971 	} while (mpp++ != &ill->ill_last_mp_to_free);
972 
973 	ill_free_mib(ill);
974 	/* Drop refcnt here */
975 	netstack_rele(ill->ill_ipst->ips_netstack);
976 	ill->ill_ipst = NULL;
977 
978 	ILL_TRACE_CLEANUP(ill);
979 }
980 
981 static void
982 ill_free_mib(ill_t *ill)
983 {
984 	ip_stack_t *ipst = ill->ill_ipst;
985 
986 	/*
987 	 * MIB statistics must not be lost, so when an interface
988 	 * goes away the counter values will be added to the global
989 	 * MIBs.
990 	 */
991 	if (ill->ill_ip_mib != NULL) {
992 		if (ill->ill_isv6) {
993 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
994 			    ill->ill_ip_mib);
995 		} else {
996 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
997 			    ill->ill_ip_mib);
998 		}
999 
1000 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
1001 		ill->ill_ip_mib = NULL;
1002 	}
1003 	if (ill->ill_icmp6_mib != NULL) {
1004 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
1005 		    ill->ill_icmp6_mib);
1006 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
1007 		ill->ill_icmp6_mib = NULL;
1008 	}
1009 }
1010 
1011 /*
1012  * Concatenate together a physical address and a sap.
1013  *
1014  * Sap_lengths are interpreted as follows:
1015  *   sap_length == 0	==>	no sap
1016  *   sap_length > 0	==>	sap is at the head of the dlpi address
1017  *   sap_length < 0	==>	sap is at the tail of the dlpi address
1018  */
1019 static void
1020 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
1021     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
1022 {
1023 	uint16_t sap_addr = (uint16_t)sap_src;
1024 
1025 	if (sap_length == 0) {
1026 		if (phys_src == NULL)
1027 			bzero(dst, phys_length);
1028 		else
1029 			bcopy(phys_src, dst, phys_length);
1030 	} else if (sap_length < 0) {
1031 		if (phys_src == NULL)
1032 			bzero(dst, phys_length);
1033 		else
1034 			bcopy(phys_src, dst, phys_length);
1035 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1036 	} else {
1037 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1038 		if (phys_src == NULL)
1039 			bzero((char *)dst + sap_length, phys_length);
1040 		else
1041 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1042 	}
1043 }
1044 
1045 /*
1046  * Generate a dl_unitdata_req mblk for the device and address given.
1047  * addr_length is the length of the physical portion of the address.
1048  * If addr is NULL include an all zero address of the specified length.
1049  * TRUE? In any case, addr_length is taken to be the entire length of the
1050  * dlpi address, including the absolute value of sap_length.
1051  */
1052 mblk_t *
1053 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1054 		t_scalar_t sap_length)
1055 {
1056 	dl_unitdata_req_t *dlur;
1057 	mblk_t	*mp;
1058 	t_scalar_t	abs_sap_length;		/* absolute value */
1059 
1060 	abs_sap_length = ABS(sap_length);
1061 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1062 		DL_UNITDATA_REQ);
1063 	if (mp == NULL)
1064 		return (NULL);
1065 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1066 	/* HACK: accomodate incompatible DLPI drivers */
1067 	if (addr_length == 8)
1068 		addr_length = 6;
1069 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1070 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1071 	dlur->dl_priority.dl_min = 0;
1072 	dlur->dl_priority.dl_max = 0;
1073 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1074 	    (uchar_t *)&dlur[1]);
1075 	return (mp);
1076 }
1077 
1078 /*
1079  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1080  * Return an error if we already have 1 or more ioctls in progress.
1081  * This is used only for non-exclusive ioctls. Currently this is used
1082  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1083  * and thus need to use ipsq_pending_mp_add.
1084  */
1085 boolean_t
1086 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1087 {
1088 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1089 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1090 	/*
1091 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1092 	 */
1093 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1094 	    (add_mp->b_datap->db_type == M_IOCTL));
1095 
1096 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1097 	/*
1098 	 * Return error if the conn has started closing. The conn
1099 	 * could have finished cleaning up the pending mp list,
1100 	 * If so we should not add another mp to the list negating
1101 	 * the cleanup.
1102 	 */
1103 	if (connp->conn_state_flags & CONN_CLOSING)
1104 		return (B_FALSE);
1105 	/*
1106 	 * Add the pending mp to the head of the list, chained by b_next.
1107 	 * Note down the conn on which the ioctl request came, in b_prev.
1108 	 * This will be used to later get the conn, when we get a response
1109 	 * on the ill queue, from some other module (typically arp)
1110 	 */
1111 	add_mp->b_next = (void *)ill->ill_pending_mp;
1112 	add_mp->b_queue = CONNP_TO_WQ(connp);
1113 	ill->ill_pending_mp = add_mp;
1114 	if (connp != NULL)
1115 		connp->conn_oper_pending_ill = ill;
1116 	return (B_TRUE);
1117 }
1118 
1119 /*
1120  * Retrieve the ill_pending_mp and return it. We have to walk the list
1121  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1122  */
1123 mblk_t *
1124 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1125 {
1126 	mblk_t	*prev = NULL;
1127 	mblk_t	*curr = NULL;
1128 	uint_t	id;
1129 	conn_t	*connp;
1130 
1131 	/*
1132 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1133 	 * up the pending mp, but it does not know the ioc_id and
1134 	 * passes in a zero for it.
1135 	 */
1136 	mutex_enter(&ill->ill_lock);
1137 	if (ioc_id != 0)
1138 		*connpp = NULL;
1139 
1140 	/* Search the list for the appropriate ioctl based on ioc_id */
1141 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1142 	    prev = curr, curr = curr->b_next) {
1143 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1144 		connp = Q_TO_CONN(curr->b_queue);
1145 		/* Match based on the ioc_id or based on the conn */
1146 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1147 			break;
1148 	}
1149 
1150 	if (curr != NULL) {
1151 		/* Unlink the mblk from the pending mp list */
1152 		if (prev != NULL) {
1153 			prev->b_next = curr->b_next;
1154 		} else {
1155 			ASSERT(ill->ill_pending_mp == curr);
1156 			ill->ill_pending_mp = curr->b_next;
1157 		}
1158 
1159 		/*
1160 		 * conn refcnt must have been bumped up at the start of
1161 		 * the ioctl. So we can safely access the conn.
1162 		 */
1163 		ASSERT(CONN_Q(curr->b_queue));
1164 		*connpp = Q_TO_CONN(curr->b_queue);
1165 		curr->b_next = NULL;
1166 		curr->b_queue = NULL;
1167 	}
1168 
1169 	mutex_exit(&ill->ill_lock);
1170 
1171 	return (curr);
1172 }
1173 
1174 /*
1175  * Add the pending mp to the list. There can be only 1 pending mp
1176  * in the list. Any exclusive ioctl that needs to wait for a response
1177  * from another module or driver needs to use this function to set
1178  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1179  * the other module/driver. This is also used while waiting for the
1180  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1181  */
1182 boolean_t
1183 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1184     int waitfor)
1185 {
1186 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1187 
1188 	ASSERT(IAM_WRITER_IPIF(ipif));
1189 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1190 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1191 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1192 	/*
1193 	 * The caller may be using a different ipif than the one passed into
1194 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1195 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1196 	 * that `ipsq_current_ipif == ipif'.
1197 	 */
1198 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1199 
1200 	/*
1201 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1202 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1203 	 */
1204 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1205 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1206 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1207 
1208 	if (connp != NULL) {
1209 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1210 		/*
1211 		 * Return error if the conn has started closing. The conn
1212 		 * could have finished cleaning up the pending mp list,
1213 		 * If so we should not add another mp to the list negating
1214 		 * the cleanup.
1215 		 */
1216 		if (connp->conn_state_flags & CONN_CLOSING)
1217 			return (B_FALSE);
1218 	}
1219 	mutex_enter(&ipsq->ipsq_lock);
1220 	ipsq->ipsq_pending_ipif = ipif;
1221 	/*
1222 	 * Note down the queue in b_queue. This will be returned by
1223 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1224 	 * the processing
1225 	 */
1226 	add_mp->b_next = NULL;
1227 	add_mp->b_queue = q;
1228 	ipsq->ipsq_pending_mp = add_mp;
1229 	ipsq->ipsq_waitfor = waitfor;
1230 
1231 	if (connp != NULL)
1232 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1233 	mutex_exit(&ipsq->ipsq_lock);
1234 	return (B_TRUE);
1235 }
1236 
1237 /*
1238  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1239  * queued in the list.
1240  */
1241 mblk_t *
1242 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1243 {
1244 	mblk_t	*curr = NULL;
1245 
1246 	mutex_enter(&ipsq->ipsq_lock);
1247 	*connpp = NULL;
1248 	if (ipsq->ipsq_pending_mp == NULL) {
1249 		mutex_exit(&ipsq->ipsq_lock);
1250 		return (NULL);
1251 	}
1252 
1253 	/* There can be only 1 such excl message */
1254 	curr = ipsq->ipsq_pending_mp;
1255 	ASSERT(curr != NULL && curr->b_next == NULL);
1256 	ipsq->ipsq_pending_ipif = NULL;
1257 	ipsq->ipsq_pending_mp = NULL;
1258 	ipsq->ipsq_waitfor = 0;
1259 	mutex_exit(&ipsq->ipsq_lock);
1260 
1261 	if (CONN_Q(curr->b_queue)) {
1262 		/*
1263 		 * This mp did a refhold on the conn, at the start of the ioctl.
1264 		 * So we can safely return a pointer to the conn to the caller.
1265 		 */
1266 		*connpp = Q_TO_CONN(curr->b_queue);
1267 	} else {
1268 		*connpp = NULL;
1269 	}
1270 	curr->b_next = NULL;
1271 	curr->b_prev = NULL;
1272 	return (curr);
1273 }
1274 
1275 /*
1276  * Cleanup the ioctl mp queued in ipsq_pending_mp
1277  * - Called in the ill_delete path
1278  * - Called in the M_ERROR or M_HANGUP path on the ill.
1279  * - Called in the conn close path.
1280  */
1281 boolean_t
1282 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1283 {
1284 	mblk_t	*mp;
1285 	ipsq_t	*ipsq;
1286 	queue_t	*q;
1287 	ipif_t	*ipif;
1288 
1289 	ASSERT(IAM_WRITER_ILL(ill));
1290 	ipsq = ill->ill_phyint->phyint_ipsq;
1291 	mutex_enter(&ipsq->ipsq_lock);
1292 	/*
1293 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1294 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1295 	 * even if it is meant for another ill, since we have to enqueue
1296 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1297 	 * If connp is non-null we are called from the conn close path.
1298 	 */
1299 	mp = ipsq->ipsq_pending_mp;
1300 	if (mp == NULL || (connp != NULL &&
1301 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1302 		mutex_exit(&ipsq->ipsq_lock);
1303 		return (B_FALSE);
1304 	}
1305 	/* Now remove from the ipsq_pending_mp */
1306 	ipsq->ipsq_pending_mp = NULL;
1307 	q = mp->b_queue;
1308 	mp->b_next = NULL;
1309 	mp->b_prev = NULL;
1310 	mp->b_queue = NULL;
1311 
1312 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1313 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1314 	if (ill->ill_move_in_progress) {
1315 		ILL_CLEAR_MOVE(ill);
1316 	} else if (ill->ill_up_ipifs) {
1317 		ill_group_cleanup(ill);
1318 	}
1319 
1320 	ipif = ipsq->ipsq_pending_ipif;
1321 	ipsq->ipsq_pending_ipif = NULL;
1322 	ipsq->ipsq_waitfor = 0;
1323 	ipsq->ipsq_current_ipif = NULL;
1324 	ipsq->ipsq_current_ioctl = 0;
1325 	mutex_exit(&ipsq->ipsq_lock);
1326 
1327 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1328 		if (connp == NULL) {
1329 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1330 		} else {
1331 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1332 			mutex_enter(&ipif->ipif_ill->ill_lock);
1333 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1334 			mutex_exit(&ipif->ipif_ill->ill_lock);
1335 		}
1336 	} else {
1337 		/*
1338 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1339 		 * be just inet_freemsg. we have to restart it
1340 		 * otherwise the thread will be stuck.
1341 		 */
1342 		inet_freemsg(mp);
1343 	}
1344 	return (B_TRUE);
1345 }
1346 
1347 /*
1348  * The ill is closing. Cleanup all the pending mps. Called exclusively
1349  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1350  * knows this ill, and hence nobody can add an mp to this list
1351  */
1352 static void
1353 ill_pending_mp_cleanup(ill_t *ill)
1354 {
1355 	mblk_t	*mp;
1356 	queue_t	*q;
1357 
1358 	ASSERT(IAM_WRITER_ILL(ill));
1359 
1360 	mutex_enter(&ill->ill_lock);
1361 	/*
1362 	 * Every mp on the pending mp list originating from an ioctl
1363 	 * added 1 to the conn refcnt, at the start of the ioctl.
1364 	 * So bump it down now.  See comments in ip_wput_nondata()
1365 	 */
1366 	while (ill->ill_pending_mp != NULL) {
1367 		mp = ill->ill_pending_mp;
1368 		ill->ill_pending_mp = mp->b_next;
1369 		mutex_exit(&ill->ill_lock);
1370 
1371 		q = mp->b_queue;
1372 		ASSERT(CONN_Q(q));
1373 		mp->b_next = NULL;
1374 		mp->b_prev = NULL;
1375 		mp->b_queue = NULL;
1376 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1377 		mutex_enter(&ill->ill_lock);
1378 	}
1379 	ill->ill_pending_ipif = NULL;
1380 
1381 	mutex_exit(&ill->ill_lock);
1382 }
1383 
1384 /*
1385  * Called in the conn close path and ill delete path
1386  */
1387 static void
1388 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1389 {
1390 	ipsq_t	*ipsq;
1391 	mblk_t	*prev;
1392 	mblk_t	*curr;
1393 	mblk_t	*next;
1394 	queue_t	*q;
1395 	mblk_t	*tmp_list = NULL;
1396 
1397 	ASSERT(IAM_WRITER_ILL(ill));
1398 	if (connp != NULL)
1399 		q = CONNP_TO_WQ(connp);
1400 	else
1401 		q = ill->ill_wq;
1402 
1403 	ipsq = ill->ill_phyint->phyint_ipsq;
1404 	/*
1405 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1406 	 * In the case of ioctl from a conn, there can be only 1 mp
1407 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1408 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1409 	 * ioctls meant for this ill form conn's are not flushed. They will
1410 	 * be processed during ipsq_exit and will not find the ill and will
1411 	 * return error.
1412 	 */
1413 	mutex_enter(&ipsq->ipsq_lock);
1414 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1415 	    curr = next) {
1416 		next = curr->b_next;
1417 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1418 			/* Unlink the mblk from the pending mp list */
1419 			if (prev != NULL) {
1420 				prev->b_next = curr->b_next;
1421 			} else {
1422 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1423 				ipsq->ipsq_xopq_mphead = curr->b_next;
1424 			}
1425 			if (ipsq->ipsq_xopq_mptail == curr)
1426 				ipsq->ipsq_xopq_mptail = prev;
1427 			/*
1428 			 * Create a temporary list and release the ipsq lock
1429 			 * New elements are added to the head of the tmp_list
1430 			 */
1431 			curr->b_next = tmp_list;
1432 			tmp_list = curr;
1433 		} else {
1434 			prev = curr;
1435 		}
1436 	}
1437 	mutex_exit(&ipsq->ipsq_lock);
1438 
1439 	while (tmp_list != NULL) {
1440 		curr = tmp_list;
1441 		tmp_list = curr->b_next;
1442 		curr->b_next = NULL;
1443 		curr->b_prev = NULL;
1444 		curr->b_queue = NULL;
1445 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1446 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1447 			    CONN_CLOSE : NO_COPYOUT, NULL);
1448 		} else {
1449 			/*
1450 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1451 			 * this can't be just inet_freemsg. we have to
1452 			 * restart it otherwise the thread will be stuck.
1453 			 */
1454 			inet_freemsg(curr);
1455 		}
1456 	}
1457 }
1458 
1459 /*
1460  * This conn has started closing. Cleanup any pending ioctl from this conn.
1461  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1462  */
1463 void
1464 conn_ioctl_cleanup(conn_t *connp)
1465 {
1466 	mblk_t *curr;
1467 	ipsq_t	*ipsq;
1468 	ill_t	*ill;
1469 	boolean_t refheld;
1470 
1471 	/*
1472 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1473 	 * ioctl has not yet started, the mp is pending in the list headed by
1474 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1475 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1476 	 * is currently executing now the mp is not queued anywhere but
1477 	 * conn_oper_pending_ill is null. The conn close will wait
1478 	 * till the conn_ref drops to zero.
1479 	 */
1480 	mutex_enter(&connp->conn_lock);
1481 	ill = connp->conn_oper_pending_ill;
1482 	if (ill == NULL) {
1483 		mutex_exit(&connp->conn_lock);
1484 		return;
1485 	}
1486 
1487 	curr = ill_pending_mp_get(ill, &connp, 0);
1488 	if (curr != NULL) {
1489 		mutex_exit(&connp->conn_lock);
1490 		CONN_DEC_REF(connp);
1491 		inet_freemsg(curr);
1492 		return;
1493 	}
1494 	/*
1495 	 * We may not be able to refhold the ill if the ill/ipif
1496 	 * is changing. But we need to make sure that the ill will
1497 	 * not vanish. So we just bump up the ill_waiter count.
1498 	 */
1499 	refheld = ill_waiter_inc(ill);
1500 	mutex_exit(&connp->conn_lock);
1501 	if (refheld) {
1502 		if (ipsq_enter(ill, B_TRUE)) {
1503 			ill_waiter_dcr(ill);
1504 			/*
1505 			 * Check whether this ioctl has started and is
1506 			 * pending now in ipsq_pending_mp. If it is not
1507 			 * found there then check whether this ioctl has
1508 			 * not even started and is in the ipsq_xopq list.
1509 			 */
1510 			if (!ipsq_pending_mp_cleanup(ill, connp))
1511 				ipsq_xopq_mp_cleanup(ill, connp);
1512 			ipsq = ill->ill_phyint->phyint_ipsq;
1513 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1514 			return;
1515 		}
1516 	}
1517 
1518 	/*
1519 	 * The ill is also closing and we could not bump up the
1520 	 * ill_waiter_count or we could not enter the ipsq. Leave
1521 	 * the cleanup to ill_delete
1522 	 */
1523 	mutex_enter(&connp->conn_lock);
1524 	while (connp->conn_oper_pending_ill != NULL)
1525 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1526 	mutex_exit(&connp->conn_lock);
1527 	if (refheld)
1528 		ill_waiter_dcr(ill);
1529 }
1530 
1531 /*
1532  * ipcl_walk function for cleaning up conn_*_ill fields.
1533  */
1534 static void
1535 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1536 {
1537 	ill_t	*ill = (ill_t *)arg;
1538 	ire_t	*ire;
1539 
1540 	mutex_enter(&connp->conn_lock);
1541 	if (connp->conn_multicast_ill == ill) {
1542 		/* Revert to late binding */
1543 		connp->conn_multicast_ill = NULL;
1544 		connp->conn_orig_multicast_ifindex = 0;
1545 	}
1546 	if (connp->conn_incoming_ill == ill)
1547 		connp->conn_incoming_ill = NULL;
1548 	if (connp->conn_outgoing_ill == ill)
1549 		connp->conn_outgoing_ill = NULL;
1550 	if (connp->conn_outgoing_pill == ill)
1551 		connp->conn_outgoing_pill = NULL;
1552 	if (connp->conn_nofailover_ill == ill)
1553 		connp->conn_nofailover_ill = NULL;
1554 	if (connp->conn_xmit_if_ill == ill)
1555 		connp->conn_xmit_if_ill = NULL;
1556 	if (connp->conn_ire_cache != NULL) {
1557 		ire = connp->conn_ire_cache;
1558 		/*
1559 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1560 		 * interface X and ipif coming from interface Y, if interface
1561 		 * X and Y are part of the same IPMPgroup. Thus whenever
1562 		 * interface X goes down, remove all references to it by
1563 		 * checking both on ire_ipif and ire_stq.
1564 		 */
1565 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1566 		    (ire->ire_type == IRE_CACHE &&
1567 		    ire->ire_stq == ill->ill_wq)) {
1568 			connp->conn_ire_cache = NULL;
1569 			mutex_exit(&connp->conn_lock);
1570 			ire_refrele_notr(ire);
1571 			return;
1572 		}
1573 	}
1574 	mutex_exit(&connp->conn_lock);
1575 
1576 }
1577 
1578 /* ARGSUSED */
1579 void
1580 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1581 {
1582 	ill_t	*ill = q->q_ptr;
1583 	ipif_t	*ipif;
1584 
1585 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1586 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1587 		ipif_non_duplicate(ipif);
1588 		ipif_down_tail(ipif);
1589 	}
1590 	ill_down_tail(ill);
1591 	freemsg(mp);
1592 	ipsq_current_finish(ipsq);
1593 }
1594 
1595 /*
1596  * ill_down_start is called when we want to down this ill and bring it up again
1597  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1598  * all interfaces, but don't tear down any plumbing.
1599  */
1600 boolean_t
1601 ill_down_start(queue_t *q, mblk_t *mp)
1602 {
1603 	ill_t	*ill = q->q_ptr;
1604 	ipif_t	*ipif;
1605 
1606 	ASSERT(IAM_WRITER_ILL(ill));
1607 
1608 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1609 		(void) ipif_down(ipif, NULL, NULL);
1610 
1611 	ill_down(ill);
1612 
1613 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1614 
1615 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1616 
1617 	/*
1618 	 * Atomically test and add the pending mp if references are active.
1619 	 */
1620 	mutex_enter(&ill->ill_lock);
1621 	if (!ill_is_quiescent(ill)) {
1622 		/* call cannot fail since `conn_t *' argument is NULL */
1623 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1624 		    mp, ILL_DOWN);
1625 		mutex_exit(&ill->ill_lock);
1626 		return (B_FALSE);
1627 	}
1628 	mutex_exit(&ill->ill_lock);
1629 	return (B_TRUE);
1630 }
1631 
1632 static void
1633 ill_down(ill_t *ill)
1634 {
1635 	ip_stack_t	*ipst = ill->ill_ipst;
1636 
1637 	/* Blow off any IREs dependent on this ILL. */
1638 	ire_walk(ill_downi, (char *)ill, ipst);
1639 
1640 	mutex_enter(&ipst->ips_ire_mrtun_lock);
1641 	if (ipst->ips_ire_mrtun_count != 0) {
1642 		mutex_exit(&ipst->ips_ire_mrtun_lock);
1643 		ire_walk_ill_mrtun(0, 0, ill_downi_mrtun_srcif,
1644 		    (char *)ill, NULL, ipst);
1645 	} else {
1646 		mutex_exit(&ipst->ips_ire_mrtun_lock);
1647 	}
1648 
1649 	/*
1650 	 * If any interface based forwarding table exists
1651 	 * Blow off the ires there dependent on this ill
1652 	 */
1653 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
1654 	if (ipst->ips_ire_srcif_table_count > 0) {
1655 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
1656 		ire_walk_srcif_table_v4(ill_downi_mrtun_srcif, (char *)ill,
1657 		    ipst);
1658 	} else {
1659 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
1660 	}
1661 
1662 	/* Remove any conn_*_ill depending on this ill */
1663 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1664 
1665 	if (ill->ill_group != NULL) {
1666 		illgrp_delete(ill);
1667 	}
1668 }
1669 
1670 static void
1671 ill_down_tail(ill_t *ill)
1672 {
1673 	int	i;
1674 
1675 	/* Destroy ill_srcif_table if it exists */
1676 	/* Lock not reqd really because nobody should be able to access */
1677 	mutex_enter(&ill->ill_lock);
1678 	if (ill->ill_srcif_table != NULL) {
1679 		ill->ill_srcif_refcnt = 0;
1680 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
1681 			rw_destroy(&ill->ill_srcif_table[i].irb_lock);
1682 		}
1683 		kmem_free(ill->ill_srcif_table,
1684 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t));
1685 		ill->ill_srcif_table = NULL;
1686 		ill->ill_srcif_refcnt = 0;
1687 		ill->ill_mrtun_refcnt = 0;
1688 	}
1689 	mutex_exit(&ill->ill_lock);
1690 }
1691 
1692 /*
1693  * ire_walk routine used to delete every IRE that depends on queues
1694  * associated with 'ill'.  (Always called as writer.)
1695  */
1696 static void
1697 ill_downi(ire_t *ire, char *ill_arg)
1698 {
1699 	ill_t	*ill = (ill_t *)ill_arg;
1700 
1701 	/*
1702 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1703 	 * interface X and ipif coming from interface Y, if interface
1704 	 * X and Y are part of the same IPMP group. Thus whenever interface
1705 	 * X goes down, remove all references to it by checking both
1706 	 * on ire_ipif and ire_stq.
1707 	 */
1708 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1709 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1710 		ire_delete(ire);
1711 	}
1712 }
1713 
1714 /*
1715  * A seperate routine for deleting revtun and srcif based routes
1716  * are needed because the ires only deleted when the interface
1717  * is unplumbed. Also these ires have ire_in_ill non-null as well.
1718  * we want to keep mobile IP specific code separate.
1719  */
1720 static void
1721 ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg)
1722 {
1723 	ill_t   *ill = (ill_t *)ill_arg;
1724 
1725 	ASSERT(ire->ire_in_ill != NULL);
1726 
1727 	if ((ire->ire_in_ill != NULL && ire->ire_in_ill == ill) ||
1728 	    (ire->ire_stq == ill->ill_wq) || (ire->ire_stq == ill->ill_rq)) {
1729 		ire_delete(ire);
1730 	}
1731 }
1732 
1733 /*
1734  * Remove ire/nce from the fastpath list.
1735  */
1736 void
1737 ill_fastpath_nack(ill_t *ill)
1738 {
1739 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1740 }
1741 
1742 /* Consume an M_IOCACK of the fastpath probe. */
1743 void
1744 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1745 {
1746 	mblk_t	*mp1 = mp;
1747 
1748 	/*
1749 	 * If this was the first attempt turn on the fastpath probing.
1750 	 */
1751 	mutex_enter(&ill->ill_lock);
1752 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1753 		ill->ill_dlpi_fastpath_state = IDS_OK;
1754 	mutex_exit(&ill->ill_lock);
1755 
1756 	/* Free the M_IOCACK mblk, hold on to the data */
1757 	mp = mp->b_cont;
1758 	freeb(mp1);
1759 	if (mp == NULL)
1760 		return;
1761 	if (mp->b_cont != NULL) {
1762 		/*
1763 		 * Update all IRE's or NCE's that are waiting for
1764 		 * fastpath update.
1765 		 */
1766 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1767 		mp1 = mp->b_cont;
1768 		freeb(mp);
1769 		mp = mp1;
1770 	} else {
1771 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1772 	}
1773 
1774 	freeb(mp);
1775 }
1776 
1777 /*
1778  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1779  * The data portion of the request is a dl_unitdata_req_t template for
1780  * what we would send downstream in the absence of a fastpath confirmation.
1781  */
1782 int
1783 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1784 {
1785 	struct iocblk	*ioc;
1786 	mblk_t	*mp;
1787 
1788 	if (dlur_mp == NULL)
1789 		return (EINVAL);
1790 
1791 	mutex_enter(&ill->ill_lock);
1792 	switch (ill->ill_dlpi_fastpath_state) {
1793 	case IDS_FAILED:
1794 		/*
1795 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1796 		 * support it.
1797 		 */
1798 		mutex_exit(&ill->ill_lock);
1799 		return (ENOTSUP);
1800 	case IDS_UNKNOWN:
1801 		/* This is the first probe */
1802 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1803 		break;
1804 	default:
1805 		break;
1806 	}
1807 	mutex_exit(&ill->ill_lock);
1808 
1809 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1810 		return (EAGAIN);
1811 
1812 	mp->b_cont = copyb(dlur_mp);
1813 	if (mp->b_cont == NULL) {
1814 		freeb(mp);
1815 		return (EAGAIN);
1816 	}
1817 
1818 	ioc = (struct iocblk *)mp->b_rptr;
1819 	ioc->ioc_count = msgdsize(mp->b_cont);
1820 
1821 	putnext(ill->ill_wq, mp);
1822 	return (0);
1823 }
1824 
1825 void
1826 ill_capability_probe(ill_t *ill)
1827 {
1828 	/*
1829 	 * Do so only if negotiation is enabled, capabilities are unknown,
1830 	 * and a capability negotiation is not already in progress.
1831 	 */
1832 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN &&
1833 	    ill->ill_dlpi_capab_state != IDS_RENEG)
1834 		return;
1835 
1836 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1837 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1838 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1839 }
1840 
1841 void
1842 ill_capability_reset(ill_t *ill)
1843 {
1844 	mblk_t *sc_mp = NULL;
1845 	mblk_t *tmp;
1846 
1847 	/*
1848 	 * Note here that we reset the state to UNKNOWN, and later send
1849 	 * down the DL_CAPABILITY_REQ without first setting the state to
1850 	 * INPROGRESS.  We do this in order to distinguish the
1851 	 * DL_CAPABILITY_ACK response which may come back in response to
1852 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1853 	 * also handle the case where the driver doesn't send us back
1854 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1855 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1856 	 * features are turned off until the state reaches IDS_OK.
1857 	 */
1858 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1859 
1860 	/*
1861 	 * Disable sub-capabilities and request a list of sub-capability
1862 	 * messages which will be sent down to the driver.  Each handler
1863 	 * allocates the corresponding dl_capability_sub_t inside an
1864 	 * mblk, and links it to the existing sc_mp mblk, or return it
1865 	 * as sc_mp if it's the first sub-capability (the passed in
1866 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1867 	 * sc_mp will be pulled-up, before passing it downstream.
1868 	 */
1869 	ill_capability_mdt_reset(ill, &sc_mp);
1870 	ill_capability_hcksum_reset(ill, &sc_mp);
1871 	ill_capability_zerocopy_reset(ill, &sc_mp);
1872 	ill_capability_ipsec_reset(ill, &sc_mp);
1873 	ill_capability_dls_reset(ill, &sc_mp);
1874 	ill_capability_lso_reset(ill, &sc_mp);
1875 
1876 	/* Nothing to send down in order to disable the capabilities? */
1877 	if (sc_mp == NULL)
1878 		return;
1879 
1880 	tmp = msgpullup(sc_mp, -1);
1881 	freemsg(sc_mp);
1882 	if ((sc_mp = tmp) == NULL) {
1883 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1884 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1885 		return;
1886 	}
1887 
1888 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1889 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1890 }
1891 
1892 /*
1893  * Request or set new-style hardware capabilities supported by DLS provider.
1894  */
1895 static void
1896 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1897 {
1898 	mblk_t *mp;
1899 	dl_capability_req_t *capb;
1900 	size_t size = 0;
1901 	uint8_t *ptr;
1902 
1903 	if (reqp != NULL)
1904 		size = MBLKL(reqp);
1905 
1906 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1907 	if (mp == NULL) {
1908 		freemsg(reqp);
1909 		return;
1910 	}
1911 	ptr = mp->b_rptr;
1912 
1913 	capb = (dl_capability_req_t *)ptr;
1914 	ptr += sizeof (dl_capability_req_t);
1915 
1916 	if (reqp != NULL) {
1917 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1918 		capb->dl_sub_length = size;
1919 		bcopy(reqp->b_rptr, ptr, size);
1920 		ptr += size;
1921 		mp->b_cont = reqp->b_cont;
1922 		freeb(reqp);
1923 	}
1924 	ASSERT(ptr == mp->b_wptr);
1925 
1926 	ill_dlpi_send(ill, mp);
1927 }
1928 
1929 static void
1930 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1931 {
1932 	dl_capab_id_t *id_ic;
1933 	uint_t sub_dl_cap = outers->dl_cap;
1934 	dl_capability_sub_t *inners;
1935 	uint8_t *capend;
1936 
1937 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1938 
1939 	/*
1940 	 * Note: range checks here are not absolutely sufficient to
1941 	 * make us robust against malformed messages sent by drivers;
1942 	 * this is in keeping with the rest of IP's dlpi handling.
1943 	 * (Remember, it's coming from something else in the kernel
1944 	 * address space)
1945 	 */
1946 
1947 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1948 	if (capend > mp->b_wptr) {
1949 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1950 		    "malformed sub-capability too long for mblk");
1951 		return;
1952 	}
1953 
1954 	id_ic = (dl_capab_id_t *)(outers + 1);
1955 
1956 	if (outers->dl_length < sizeof (*id_ic) ||
1957 	    (inners = &id_ic->id_subcap,
1958 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1959 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1960 		    "encapsulated capab type %d too long for mblk",
1961 		    inners->dl_cap);
1962 		return;
1963 	}
1964 
1965 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1966 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1967 		    "isn't as expected; pass-thru module(s) detected, "
1968 		    "discarding capability\n", inners->dl_cap));
1969 		return;
1970 	}
1971 
1972 	/* Process the encapsulated sub-capability */
1973 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1974 }
1975 
1976 /*
1977  * Process Multidata Transmit capability negotiation ack received from a
1978  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1979  * DL_CAPABILITY_ACK message.
1980  */
1981 static void
1982 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1983 {
1984 	mblk_t *nmp = NULL;
1985 	dl_capability_req_t *oc;
1986 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1987 	ill_mdt_capab_t **ill_mdt_capab;
1988 	uint_t sub_dl_cap = isub->dl_cap;
1989 	uint8_t *capend;
1990 
1991 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1992 
1993 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1994 
1995 	/*
1996 	 * Note: range checks here are not absolutely sufficient to
1997 	 * make us robust against malformed messages sent by drivers;
1998 	 * this is in keeping with the rest of IP's dlpi handling.
1999 	 * (Remember, it's coming from something else in the kernel
2000 	 * address space)
2001 	 */
2002 
2003 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2004 	if (capend > mp->b_wptr) {
2005 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2006 		    "malformed sub-capability too long for mblk");
2007 		return;
2008 	}
2009 
2010 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
2011 
2012 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
2013 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
2014 		    "unsupported MDT sub-capability (version %d, expected %d)",
2015 		    mdt_ic->mdt_version, MDT_VERSION_2);
2016 		return;
2017 	}
2018 
2019 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
2020 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
2021 		    "capability isn't as expected; pass-thru module(s) "
2022 		    "detected, discarding capability\n"));
2023 		return;
2024 	}
2025 
2026 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
2027 
2028 		if (*ill_mdt_capab == NULL) {
2029 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
2030 			    KM_NOSLEEP);
2031 
2032 			if (*ill_mdt_capab == NULL) {
2033 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2034 				    "could not enable MDT version %d "
2035 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
2036 				    ill->ill_name);
2037 				return;
2038 			}
2039 		}
2040 
2041 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
2042 		    "MDT version %d (%d bytes leading, %d bytes trailing "
2043 		    "header spaces, %d max pld bufs, %d span limit)\n",
2044 		    ill->ill_name, MDT_VERSION_2,
2045 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
2046 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
2047 
2048 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
2049 		(*ill_mdt_capab)->ill_mdt_on = 1;
2050 		/*
2051 		 * Round the following values to the nearest 32-bit; ULP
2052 		 * may further adjust them to accomodate for additional
2053 		 * protocol headers.  We pass these values to ULP during
2054 		 * bind time.
2055 		 */
2056 		(*ill_mdt_capab)->ill_mdt_hdr_head =
2057 		    roundup(mdt_ic->mdt_hdr_head, 4);
2058 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
2059 		    roundup(mdt_ic->mdt_hdr_tail, 4);
2060 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
2061 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
2062 
2063 		ill->ill_capabilities |= ILL_CAPAB_MDT;
2064 	} else {
2065 		uint_t size;
2066 		uchar_t *rptr;
2067 
2068 		size = sizeof (dl_capability_req_t) +
2069 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
2070 
2071 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2072 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2073 			    "could not enable MDT for %s (ENOMEM)\n",
2074 			    ill->ill_name);
2075 			return;
2076 		}
2077 
2078 		rptr = nmp->b_rptr;
2079 		/* initialize dl_capability_req_t */
2080 		oc = (dl_capability_req_t *)nmp->b_rptr;
2081 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2082 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2083 		    sizeof (dl_capab_mdt_t);
2084 		nmp->b_rptr += sizeof (dl_capability_req_t);
2085 
2086 		/* initialize dl_capability_sub_t */
2087 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2088 		nmp->b_rptr += sizeof (*isub);
2089 
2090 		/* initialize dl_capab_mdt_t */
2091 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2092 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2093 
2094 		nmp->b_rptr = rptr;
2095 
2096 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2097 		    "to enable MDT version %d\n", ill->ill_name,
2098 		    MDT_VERSION_2));
2099 
2100 		/* set ENABLE flag */
2101 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2102 
2103 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2104 		ill_dlpi_send(ill, nmp);
2105 	}
2106 }
2107 
2108 static void
2109 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2110 {
2111 	mblk_t *mp;
2112 	dl_capab_mdt_t *mdt_subcap;
2113 	dl_capability_sub_t *dl_subcap;
2114 	int size;
2115 
2116 	if (!ILL_MDT_CAPABLE(ill))
2117 		return;
2118 
2119 	ASSERT(ill->ill_mdt_capab != NULL);
2120 	/*
2121 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2122 	 * structure since it's possible that another thread is still
2123 	 * referring to it.  The structure only gets deallocated when
2124 	 * we destroy the ill.
2125 	 */
2126 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2127 
2128 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2129 
2130 	mp = allocb(size, BPRI_HI);
2131 	if (mp == NULL) {
2132 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2133 		    "request to disable MDT\n"));
2134 		return;
2135 	}
2136 
2137 	mp->b_wptr = mp->b_rptr + size;
2138 
2139 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2140 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2141 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2142 
2143 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2144 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2145 	mdt_subcap->mdt_flags = 0;
2146 	mdt_subcap->mdt_hdr_head = 0;
2147 	mdt_subcap->mdt_hdr_tail = 0;
2148 
2149 	if (*sc_mp != NULL)
2150 		linkb(*sc_mp, mp);
2151 	else
2152 		*sc_mp = mp;
2153 }
2154 
2155 /*
2156  * Send a DL_NOTIFY_REQ to the specified ill to enable
2157  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2158  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2159  * acceleration.
2160  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2161  */
2162 static boolean_t
2163 ill_enable_promisc_notify(ill_t *ill)
2164 {
2165 	mblk_t *mp;
2166 	dl_notify_req_t *req;
2167 
2168 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2169 
2170 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2171 	if (mp == NULL)
2172 		return (B_FALSE);
2173 
2174 	req = (dl_notify_req_t *)mp->b_rptr;
2175 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2176 	    DL_NOTE_PROMISC_OFF_PHYS;
2177 
2178 	ill_dlpi_send(ill, mp);
2179 
2180 	return (B_TRUE);
2181 }
2182 
2183 
2184 /*
2185  * Allocate an IPsec capability request which will be filled by our
2186  * caller to turn on support for one or more algorithms.
2187  */
2188 static mblk_t *
2189 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2190 {
2191 	mblk_t *nmp;
2192 	dl_capability_req_t	*ocap;
2193 	dl_capab_ipsec_t	*ocip;
2194 	dl_capab_ipsec_t	*icip;
2195 	uint8_t			*ptr;
2196 	icip = (dl_capab_ipsec_t *)(isub + 1);
2197 
2198 	/*
2199 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2200 	 * PROMISC_ON/OFF notification from the provider. We need to
2201 	 * do this before enabling the algorithms to avoid leakage of
2202 	 * cleartext packets.
2203 	 */
2204 
2205 	if (!ill_enable_promisc_notify(ill))
2206 		return (NULL);
2207 
2208 	/*
2209 	 * Allocate new mblk which will contain a new capability
2210 	 * request to enable the capabilities.
2211 	 */
2212 
2213 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2214 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2215 	if (nmp == NULL)
2216 		return (NULL);
2217 
2218 	ptr = nmp->b_rptr;
2219 
2220 	/* initialize dl_capability_req_t */
2221 	ocap = (dl_capability_req_t *)ptr;
2222 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2223 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2224 	ptr += sizeof (dl_capability_req_t);
2225 
2226 	/* initialize dl_capability_sub_t */
2227 	bcopy(isub, ptr, sizeof (*isub));
2228 	ptr += sizeof (*isub);
2229 
2230 	/* initialize dl_capab_ipsec_t */
2231 	ocip = (dl_capab_ipsec_t *)ptr;
2232 	bcopy(icip, ocip, sizeof (*icip));
2233 
2234 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2235 	return (nmp);
2236 }
2237 
2238 /*
2239  * Process an IPsec capability negotiation ack received from a DLS Provider.
2240  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2241  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2242  */
2243 static void
2244 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2245 {
2246 	dl_capab_ipsec_t	*icip;
2247 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2248 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2249 	uint_t cipher, nciphers;
2250 	mblk_t *nmp;
2251 	uint_t alg_len;
2252 	boolean_t need_sadb_dump;
2253 	uint_t sub_dl_cap = isub->dl_cap;
2254 	ill_ipsec_capab_t **ill_capab;
2255 	uint64_t ill_capab_flag;
2256 	uint8_t *capend, *ciphend;
2257 	boolean_t sadb_resync;
2258 
2259 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2260 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2261 
2262 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2263 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2264 		ill_capab_flag = ILL_CAPAB_AH;
2265 	} else {
2266 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2267 		ill_capab_flag = ILL_CAPAB_ESP;
2268 	}
2269 
2270 	/*
2271 	 * If the ill capability structure exists, then this incoming
2272 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2273 	 * If this is so, then we'd need to resynchronize the SADB
2274 	 * after re-enabling the offloaded ciphers.
2275 	 */
2276 	sadb_resync = (*ill_capab != NULL);
2277 
2278 	/*
2279 	 * Note: range checks here are not absolutely sufficient to
2280 	 * make us robust against malformed messages sent by drivers;
2281 	 * this is in keeping with the rest of IP's dlpi handling.
2282 	 * (Remember, it's coming from something else in the kernel
2283 	 * address space)
2284 	 */
2285 
2286 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2287 	if (capend > mp->b_wptr) {
2288 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2289 		    "malformed sub-capability too long for mblk");
2290 		return;
2291 	}
2292 
2293 	/*
2294 	 * There are two types of acks we process here:
2295 	 * 1. acks in reply to a (first form) generic capability req
2296 	 *    (no ENABLE flag set)
2297 	 * 2. acks in reply to a ENABLE capability req.
2298 	 *    (ENABLE flag set)
2299 	 *
2300 	 * We process the subcapability passed as argument as follows:
2301 	 * 1 do initializations
2302 	 *   1.1 initialize nmp = NULL
2303 	 *   1.2 set need_sadb_dump to B_FALSE
2304 	 * 2 for each cipher in subcapability:
2305 	 *   2.1 if ENABLE flag is set:
2306 	 *	2.1.1 update per-ill ipsec capabilities info
2307 	 *	2.1.2 set need_sadb_dump to B_TRUE
2308 	 *   2.2 if ENABLE flag is not set:
2309 	 *	2.2.1 if nmp is NULL:
2310 	 *		2.2.1.1 allocate and initialize nmp
2311 	 *		2.2.1.2 init current pos in nmp
2312 	 *	2.2.2 copy current cipher to current pos in nmp
2313 	 *	2.2.3 set ENABLE flag in nmp
2314 	 *	2.2.4 update current pos
2315 	 * 3 if nmp is not equal to NULL, send enable request
2316 	 *   3.1 send capability request
2317 	 * 4 if need_sadb_dump is B_TRUE
2318 	 *   4.1 enable promiscuous on/off notifications
2319 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2320 	 *	AH or ESP SA's to interface.
2321 	 */
2322 
2323 	nmp = NULL;
2324 	oalg = NULL;
2325 	need_sadb_dump = B_FALSE;
2326 	icip = (dl_capab_ipsec_t *)(isub + 1);
2327 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2328 
2329 	nciphers = icip->cip_nciphers;
2330 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2331 
2332 	if (ciphend > capend) {
2333 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2334 		    "too many ciphers for sub-capability len");
2335 		return;
2336 	}
2337 
2338 	for (cipher = 0; cipher < nciphers; cipher++) {
2339 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2340 
2341 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2342 			/*
2343 			 * TBD: when we provide a way to disable capabilities
2344 			 * from above, need to manage the request-pending state
2345 			 * and fail if we were not expecting this ACK.
2346 			 */
2347 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2348 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2349 
2350 			/*
2351 			 * Update IPsec capabilities for this ill
2352 			 */
2353 
2354 			if (*ill_capab == NULL) {
2355 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2356 				    ("ill_capability_ipsec_ack: "
2357 					"allocating ipsec_capab for ill\n"));
2358 				*ill_capab = ill_ipsec_capab_alloc();
2359 
2360 				if (*ill_capab == NULL) {
2361 					cmn_err(CE_WARN,
2362 					    "ill_capability_ipsec_ack: "
2363 					    "could not enable IPsec Hardware "
2364 					    "acceleration for %s (ENOMEM)\n",
2365 					    ill->ill_name);
2366 					return;
2367 				}
2368 			}
2369 
2370 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2371 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2372 
2373 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2374 				cmn_err(CE_WARN,
2375 				    "ill_capability_ipsec_ack: "
2376 				    "malformed IPsec algorithm id %d",
2377 				    ialg->alg_prim);
2378 				continue;
2379 			}
2380 
2381 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2382 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2383 				    ialg->alg_prim);
2384 			} else {
2385 				ipsec_capab_algparm_t *alp;
2386 
2387 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2388 				    ialg->alg_prim);
2389 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2390 				    ialg->alg_prim)) {
2391 					cmn_err(CE_WARN,
2392 					    "ill_capability_ipsec_ack: "
2393 					    "no space for IPsec alg id %d",
2394 					    ialg->alg_prim);
2395 					continue;
2396 				}
2397 				alp = &((*ill_capab)->encr_algparm[
2398 						ialg->alg_prim]);
2399 				alp->minkeylen = ialg->alg_minbits;
2400 				alp->maxkeylen = ialg->alg_maxbits;
2401 			}
2402 			ill->ill_capabilities |= ill_capab_flag;
2403 			/*
2404 			 * indicate that a capability was enabled, which
2405 			 * will be used below to kick off a SADB dump
2406 			 * to the ill.
2407 			 */
2408 			need_sadb_dump = B_TRUE;
2409 		} else {
2410 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2411 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2412 				ialg->alg_prim));
2413 
2414 			if (nmp == NULL) {
2415 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2416 				if (nmp == NULL) {
2417 					/*
2418 					 * Sending the PROMISC_ON/OFF
2419 					 * notification request failed.
2420 					 * We cannot enable the algorithms
2421 					 * since the Provider will not
2422 					 * notify IP of promiscous mode
2423 					 * changes, which could lead
2424 					 * to leakage of packets.
2425 					 */
2426 					cmn_err(CE_WARN,
2427 					    "ill_capability_ipsec_ack: "
2428 					    "could not enable IPsec Hardware "
2429 					    "acceleration for %s (ENOMEM)\n",
2430 					    ill->ill_name);
2431 					return;
2432 				}
2433 				/* ptr to current output alg specifier */
2434 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2435 			}
2436 
2437 			/*
2438 			 * Copy current alg specifier, set ENABLE
2439 			 * flag, and advance to next output alg.
2440 			 * For now we enable all IPsec capabilities.
2441 			 */
2442 			ASSERT(oalg != NULL);
2443 			bcopy(ialg, oalg, alg_len);
2444 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2445 			nmp->b_wptr += alg_len;
2446 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2447 		}
2448 
2449 		/* move to next input algorithm specifier */
2450 		ialg = (dl_capab_ipsec_alg_t *)
2451 		    ((char *)ialg + alg_len);
2452 	}
2453 
2454 	if (nmp != NULL)
2455 		/*
2456 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2457 		 * IPsec hardware acceleration.
2458 		 */
2459 		ill_dlpi_send(ill, nmp);
2460 
2461 	if (need_sadb_dump)
2462 		/*
2463 		 * An acknowledgement corresponding to a request to
2464 		 * enable acceleration was received, notify SADB.
2465 		 */
2466 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2467 }
2468 
2469 /*
2470  * Given an mblk with enough space in it, create sub-capability entries for
2471  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2472  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2473  * in preparation for the reset the DL_CAPABILITY_REQ message.
2474  */
2475 static void
2476 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2477     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2478 {
2479 	dl_capab_ipsec_t *oipsec;
2480 	dl_capab_ipsec_alg_t *oalg;
2481 	dl_capability_sub_t *dl_subcap;
2482 	int i, k;
2483 
2484 	ASSERT(nciphers > 0);
2485 	ASSERT(ill_cap != NULL);
2486 	ASSERT(mp != NULL);
2487 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2488 
2489 	/* dl_capability_sub_t for "stype" */
2490 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2491 	dl_subcap->dl_cap = stype;
2492 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2493 	mp->b_wptr += sizeof (dl_capability_sub_t);
2494 
2495 	/* dl_capab_ipsec_t for "stype" */
2496 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2497 	oipsec->cip_version = 1;
2498 	oipsec->cip_nciphers = nciphers;
2499 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2500 
2501 	/* create entries for "stype" AUTH ciphers */
2502 	for (i = 0; i < ill_cap->algs_size; i++) {
2503 		for (k = 0; k < BITSPERBYTE; k++) {
2504 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2505 				continue;
2506 
2507 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2508 			bzero((void *)oalg, sizeof (*oalg));
2509 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2510 			oalg->alg_prim = k + (BITSPERBYTE * i);
2511 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2512 		}
2513 	}
2514 	/* create entries for "stype" ENCR ciphers */
2515 	for (i = 0; i < ill_cap->algs_size; i++) {
2516 		for (k = 0; k < BITSPERBYTE; k++) {
2517 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2518 				continue;
2519 
2520 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2521 			bzero((void *)oalg, sizeof (*oalg));
2522 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2523 			oalg->alg_prim = k + (BITSPERBYTE * i);
2524 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2525 		}
2526 	}
2527 }
2528 
2529 /*
2530  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2531  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2532  * POPC instruction, but our macro is more flexible for an arbitrary length
2533  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2534  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2535  * stays that way, we can reduce the number of iterations required.
2536  */
2537 #define	COUNT_1S(val, sum) {					\
2538 	uint8_t x = val & 0xff;					\
2539 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2540 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2541 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2542 }
2543 
2544 /* ARGSUSED */
2545 static void
2546 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2547 {
2548 	mblk_t *mp;
2549 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2550 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2551 	uint64_t ill_capabilities = ill->ill_capabilities;
2552 	int ah_cnt = 0, esp_cnt = 0;
2553 	int ah_len = 0, esp_len = 0;
2554 	int i, size = 0;
2555 
2556 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2557 		return;
2558 
2559 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2560 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2561 
2562 	/* Find out the number of ciphers for AH */
2563 	if (cap_ah != NULL) {
2564 		for (i = 0; i < cap_ah->algs_size; i++) {
2565 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2566 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2567 		}
2568 		if (ah_cnt > 0) {
2569 			size += sizeof (dl_capability_sub_t) +
2570 			    sizeof (dl_capab_ipsec_t);
2571 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2572 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2573 			size += ah_len;
2574 		}
2575 	}
2576 
2577 	/* Find out the number of ciphers for ESP */
2578 	if (cap_esp != NULL) {
2579 		for (i = 0; i < cap_esp->algs_size; i++) {
2580 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2581 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2582 		}
2583 		if (esp_cnt > 0) {
2584 			size += sizeof (dl_capability_sub_t) +
2585 			    sizeof (dl_capab_ipsec_t);
2586 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2587 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2588 			size += esp_len;
2589 		}
2590 	}
2591 
2592 	if (size == 0) {
2593 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2594 		    "there's nothing to reset\n"));
2595 		return;
2596 	}
2597 
2598 	mp = allocb(size, BPRI_HI);
2599 	if (mp == NULL) {
2600 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2601 		    "request to disable IPSEC Hardware Acceleration\n"));
2602 		return;
2603 	}
2604 
2605 	/*
2606 	 * Clear the capability flags for IPSec HA but retain the ill
2607 	 * capability structures since it's possible that another thread
2608 	 * is still referring to them.  The structures only get deallocated
2609 	 * when we destroy the ill.
2610 	 *
2611 	 * Various places check the flags to see if the ill is capable of
2612 	 * hardware acceleration, and by clearing them we ensure that new
2613 	 * outbound IPSec packets are sent down encrypted.
2614 	 */
2615 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2616 
2617 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2618 	if (ah_cnt > 0) {
2619 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2620 		    cap_ah, mp);
2621 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2622 	}
2623 
2624 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2625 	if (esp_cnt > 0) {
2626 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2627 		    cap_esp, mp);
2628 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2629 	}
2630 
2631 	/*
2632 	 * At this point we've composed a bunch of sub-capabilities to be
2633 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2634 	 * by the caller.  Upon receiving this reset message, the driver
2635 	 * must stop inbound decryption (by destroying all inbound SAs)
2636 	 * and let the corresponding packets come in encrypted.
2637 	 */
2638 
2639 	if (*sc_mp != NULL)
2640 		linkb(*sc_mp, mp);
2641 	else
2642 		*sc_mp = mp;
2643 }
2644 
2645 static void
2646 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2647     boolean_t encapsulated)
2648 {
2649 	boolean_t legacy = B_FALSE;
2650 
2651 	/*
2652 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2653 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2654 	 * instructed the driver to disable its advertised capabilities,
2655 	 * so there's no point in accepting any response at this moment.
2656 	 */
2657 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2658 		return;
2659 
2660 	/*
2661 	 * Note that only the following two sub-capabilities may be
2662 	 * considered as "legacy", since their original definitions
2663 	 * do not incorporate the dl_mid_t module ID token, and hence
2664 	 * may require the use of the wrapper sub-capability.
2665 	 */
2666 	switch (subp->dl_cap) {
2667 	case DL_CAPAB_IPSEC_AH:
2668 	case DL_CAPAB_IPSEC_ESP:
2669 		legacy = B_TRUE;
2670 		break;
2671 	}
2672 
2673 	/*
2674 	 * For legacy sub-capabilities which don't incorporate a queue_t
2675 	 * pointer in their structures, discard them if we detect that
2676 	 * there are intermediate modules in between IP and the driver.
2677 	 */
2678 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2679 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2680 		    "%d discarded; %d module(s) present below IP\n",
2681 		    subp->dl_cap, ill->ill_lmod_cnt));
2682 		return;
2683 	}
2684 
2685 	switch (subp->dl_cap) {
2686 	case DL_CAPAB_IPSEC_AH:
2687 	case DL_CAPAB_IPSEC_ESP:
2688 		ill_capability_ipsec_ack(ill, mp, subp);
2689 		break;
2690 	case DL_CAPAB_MDT:
2691 		ill_capability_mdt_ack(ill, mp, subp);
2692 		break;
2693 	case DL_CAPAB_HCKSUM:
2694 		ill_capability_hcksum_ack(ill, mp, subp);
2695 		break;
2696 	case DL_CAPAB_ZEROCOPY:
2697 		ill_capability_zerocopy_ack(ill, mp, subp);
2698 		break;
2699 	case DL_CAPAB_POLL:
2700 		if (!SOFT_RINGS_ENABLED())
2701 			ill_capability_dls_ack(ill, mp, subp);
2702 		break;
2703 	case DL_CAPAB_SOFT_RING:
2704 		if (SOFT_RINGS_ENABLED())
2705 			ill_capability_dls_ack(ill, mp, subp);
2706 		break;
2707 	case DL_CAPAB_LSO:
2708 		ill_capability_lso_ack(ill, mp, subp);
2709 		break;
2710 	default:
2711 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2712 		    subp->dl_cap));
2713 	}
2714 }
2715 
2716 /*
2717  * As part of negotiating polling capability, the driver tells us
2718  * the default (or normal) blanking interval and packet threshold
2719  * (the receive timer fires if blanking interval is reached or
2720  * the packet threshold is reached).
2721  *
2722  * As part of manipulating the polling interval, we always use our
2723  * estimated interval (avg service time * number of packets queued
2724  * on the squeue) but we try to blank for a minimum of
2725  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2726  * packet threshold during this time. When we are not in polling mode
2727  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2728  * rr_min_blank_ratio but up the packet cnt by a ratio of
2729  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2730  * possible although for a shorter interval.
2731  */
2732 #define	RR_MAX_BLANK_RATIO	20
2733 #define	RR_MIN_BLANK_RATIO	10
2734 #define	RR_MAX_PKT_CNT_RATIO	3
2735 #define	RR_MIN_PKT_CNT_RATIO	3
2736 
2737 /*
2738  * These can be tuned via /etc/system.
2739  */
2740 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2741 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2742 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2743 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2744 
2745 static mac_resource_handle_t
2746 ill_ring_add(void *arg, mac_resource_t *mrp)
2747 {
2748 	ill_t			*ill = (ill_t *)arg;
2749 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2750 	ill_rx_ring_t		*rx_ring;
2751 	int			ip_rx_index;
2752 
2753 	ASSERT(mrp != NULL);
2754 	if (mrp->mr_type != MAC_RX_FIFO) {
2755 		return (NULL);
2756 	}
2757 	ASSERT(ill != NULL);
2758 	ASSERT(ill->ill_dls_capab != NULL);
2759 
2760 	mutex_enter(&ill->ill_lock);
2761 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2762 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2763 		ASSERT(rx_ring != NULL);
2764 
2765 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2766 			time_t normal_blank_time =
2767 			    mrfp->mrf_normal_blank_time;
2768 			uint_t normal_pkt_cnt =
2769 			    mrfp->mrf_normal_pkt_count;
2770 
2771 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2772 
2773 	rx_ring->rr_blank = mrfp->mrf_blank;
2774 	rx_ring->rr_handle = mrfp->mrf_arg;
2775 	rx_ring->rr_ill = ill;
2776 	rx_ring->rr_normal_blank_time = normal_blank_time;
2777 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2778 
2779 			rx_ring->rr_max_blank_time =
2780 			    normal_blank_time * rr_max_blank_ratio;
2781 			rx_ring->rr_min_blank_time =
2782 			    normal_blank_time * rr_min_blank_ratio;
2783 			rx_ring->rr_max_pkt_cnt =
2784 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2785 			rx_ring->rr_min_pkt_cnt =
2786 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2787 
2788 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2789 			mutex_exit(&ill->ill_lock);
2790 
2791 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2792 			    (int), ip_rx_index);
2793 			return ((mac_resource_handle_t)rx_ring);
2794 		}
2795 	}
2796 
2797 	/*
2798 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2799 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2800 	 * should be made configurable. Meanwhile it cause no panic because
2801 	 * driver will pass ip_input a NULL handle which will make
2802 	 * IP allocate the default squeue and Polling mode will not
2803 	 * be used for this ring.
2804 	 */
2805 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2806 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2807 
2808 	mutex_exit(&ill->ill_lock);
2809 	return (NULL);
2810 }
2811 
2812 static boolean_t
2813 ill_capability_dls_init(ill_t *ill)
2814 {
2815 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2816 	conn_t 			*connp;
2817 	size_t			sz;
2818 	ip_stack_t *ipst = ill->ill_ipst;
2819 
2820 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2821 		if (ill_dls == NULL) {
2822 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2823 			    "soft_ring enabled for ill=%s (%p) but data "
2824 			    "structs uninitialized\n", ill->ill_name,
2825 			    (void *)ill);
2826 		}
2827 		return (B_TRUE);
2828 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2829 		if (ill_dls == NULL) {
2830 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2831 			    "polling enabled for ill=%s (%p) but data "
2832 			    "structs uninitialized\n", ill->ill_name,
2833 			(void *)ill);
2834 		}
2835 		return (B_TRUE);
2836 	}
2837 
2838 	if (ill_dls != NULL) {
2839 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2840 		/* Soft_Ring or polling is being re-enabled */
2841 
2842 		connp = ill_dls->ill_unbind_conn;
2843 		ASSERT(rx_ring != NULL);
2844 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2845 		bzero((void *)rx_ring,
2846 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2847 		ill_dls->ill_ring_tbl = rx_ring;
2848 		ill_dls->ill_unbind_conn = connp;
2849 		return (B_TRUE);
2850 	}
2851 
2852 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2853 	    ipst->ips_netstack)) == NULL)
2854 		return (B_FALSE);
2855 
2856 	sz = sizeof (ill_dls_capab_t);
2857 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2858 
2859 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2860 	if (ill_dls == NULL) {
2861 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2862 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2863 		    (void *)ill);
2864 		CONN_DEC_REF(connp);
2865 		return (B_FALSE);
2866 	}
2867 
2868 	/* Allocate space to hold ring table */
2869 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2870 	ill->ill_dls_capab = ill_dls;
2871 	ill_dls->ill_unbind_conn = connp;
2872 	return (B_TRUE);
2873 }
2874 
2875 /*
2876  * ill_capability_dls_disable: disable soft_ring and/or polling
2877  * capability. Since any of the rings might already be in use, need
2878  * to call ipsq_clean_all() which gets behind the squeue to disable
2879  * direct calls if necessary.
2880  */
2881 static void
2882 ill_capability_dls_disable(ill_t *ill)
2883 {
2884 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2885 
2886 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2887 		ipsq_clean_all(ill);
2888 		ill_dls->ill_tx = NULL;
2889 		ill_dls->ill_tx_handle = NULL;
2890 		ill_dls->ill_dls_change_status = NULL;
2891 		ill_dls->ill_dls_bind = NULL;
2892 		ill_dls->ill_dls_unbind = NULL;
2893 	}
2894 
2895 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2896 }
2897 
2898 static void
2899 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2900     dl_capability_sub_t *isub)
2901 {
2902 	uint_t			size;
2903 	uchar_t			*rptr;
2904 	dl_capab_dls_t	dls, *odls;
2905 	ill_dls_capab_t	*ill_dls;
2906 	mblk_t			*nmp = NULL;
2907 	dl_capability_req_t	*ocap;
2908 	uint_t			sub_dl_cap = isub->dl_cap;
2909 
2910 	if (!ill_capability_dls_init(ill))
2911 		return;
2912 	ill_dls = ill->ill_dls_capab;
2913 
2914 	/* Copy locally to get the members aligned */
2915 	bcopy((void *)idls, (void *)&dls,
2916 	    sizeof (dl_capab_dls_t));
2917 
2918 	/* Get the tx function and handle from dld */
2919 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2920 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2921 
2922 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2923 		ill_dls->ill_dls_change_status =
2924 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2925 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2926 		ill_dls->ill_dls_unbind =
2927 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2928 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2929 	}
2930 
2931 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2932 	    isub->dl_length;
2933 
2934 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2935 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2936 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2937 		    ill->ill_name, (void *)ill);
2938 		return;
2939 	}
2940 
2941 	/* initialize dl_capability_req_t */
2942 	rptr = nmp->b_rptr;
2943 	ocap = (dl_capability_req_t *)rptr;
2944 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2945 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2946 	rptr += sizeof (dl_capability_req_t);
2947 
2948 	/* initialize dl_capability_sub_t */
2949 	bcopy(isub, rptr, sizeof (*isub));
2950 	rptr += sizeof (*isub);
2951 
2952 	odls = (dl_capab_dls_t *)rptr;
2953 	rptr += sizeof (dl_capab_dls_t);
2954 
2955 	/* initialize dl_capab_dls_t to be sent down */
2956 	dls.dls_rx_handle = (uintptr_t)ill;
2957 	dls.dls_rx = (uintptr_t)ip_input;
2958 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2959 
2960 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2961 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2962 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2963 		dls.dls_flags = SOFT_RING_ENABLE;
2964 	} else {
2965 		dls.dls_flags = POLL_ENABLE;
2966 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2967 		    "to enable polling\n", ill->ill_name));
2968 	}
2969 	bcopy((void *)&dls, (void *)odls,
2970 	    sizeof (dl_capab_dls_t));
2971 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2972 	/*
2973 	 * nmp points to a DL_CAPABILITY_REQ message to
2974 	 * enable either soft_ring or polling
2975 	 */
2976 	ill_dlpi_send(ill, nmp);
2977 }
2978 
2979 static void
2980 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2981 {
2982 	mblk_t *mp;
2983 	dl_capab_dls_t *idls;
2984 	dl_capability_sub_t *dl_subcap;
2985 	int size;
2986 
2987 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2988 		return;
2989 
2990 	ASSERT(ill->ill_dls_capab != NULL);
2991 
2992 	size = sizeof (*dl_subcap) + sizeof (*idls);
2993 
2994 	mp = allocb(size, BPRI_HI);
2995 	if (mp == NULL) {
2996 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2997 		    "request to disable soft_ring\n"));
2998 		return;
2999 	}
3000 
3001 	mp->b_wptr = mp->b_rptr + size;
3002 
3003 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3004 	dl_subcap->dl_length = sizeof (*idls);
3005 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
3006 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
3007 	else
3008 		dl_subcap->dl_cap = DL_CAPAB_POLL;
3009 
3010 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
3011 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
3012 		idls->dls_flags = SOFT_RING_DISABLE;
3013 	else
3014 		idls->dls_flags = POLL_DISABLE;
3015 
3016 	if (*sc_mp != NULL)
3017 		linkb(*sc_mp, mp);
3018 	else
3019 		*sc_mp = mp;
3020 }
3021 
3022 /*
3023  * Process a soft_ring/poll capability negotiation ack received
3024  * from a DLS Provider.isub must point to the sub-capability
3025  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
3026  */
3027 static void
3028 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3029 {
3030 	dl_capab_dls_t		*idls;
3031 	uint_t			sub_dl_cap = isub->dl_cap;
3032 	uint8_t			*capend;
3033 
3034 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
3035 	    sub_dl_cap == DL_CAPAB_POLL);
3036 
3037 	if (ill->ill_isv6)
3038 		return;
3039 
3040 	/*
3041 	 * Note: range checks here are not absolutely sufficient to
3042 	 * make us robust against malformed messages sent by drivers;
3043 	 * this is in keeping with the rest of IP's dlpi handling.
3044 	 * (Remember, it's coming from something else in the kernel
3045 	 * address space)
3046 	 */
3047 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3048 	if (capend > mp->b_wptr) {
3049 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
3050 		    "malformed sub-capability too long for mblk");
3051 		return;
3052 	}
3053 
3054 	/*
3055 	 * There are two types of acks we process here:
3056 	 * 1. acks in reply to a (first form) generic capability req
3057 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
3058 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
3059 	 *    capability req.
3060 	 */
3061 	idls = (dl_capab_dls_t *)(isub + 1);
3062 
3063 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
3064 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
3065 		    "capability isn't as expected; pass-thru "
3066 		    "module(s) detected, discarding capability\n"));
3067 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
3068 			/*
3069 			 * This is a capability renegotitation case.
3070 			 * The interface better be unusable at this
3071 			 * point other wise bad things will happen
3072 			 * if we disable direct calls on a running
3073 			 * and up interface.
3074 			 */
3075 			ill_capability_dls_disable(ill);
3076 		}
3077 		return;
3078 	}
3079 
3080 	switch (idls->dls_flags) {
3081 	default:
3082 		/* Disable if unknown flag */
3083 	case SOFT_RING_DISABLE:
3084 	case POLL_DISABLE:
3085 		ill_capability_dls_disable(ill);
3086 		break;
3087 	case SOFT_RING_CAPABLE:
3088 	case POLL_CAPABLE:
3089 		/*
3090 		 * If the capability was already enabled, its safe
3091 		 * to disable it first to get rid of stale information
3092 		 * and then start enabling it again.
3093 		 */
3094 		ill_capability_dls_disable(ill);
3095 		ill_capability_dls_capable(ill, idls, isub);
3096 		break;
3097 	case SOFT_RING_ENABLE:
3098 	case POLL_ENABLE:
3099 		mutex_enter(&ill->ill_lock);
3100 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3101 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3102 			ASSERT(ill->ill_dls_capab != NULL);
3103 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3104 		}
3105 		if (sub_dl_cap == DL_CAPAB_POLL &&
3106 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3107 			ASSERT(ill->ill_dls_capab != NULL);
3108 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3109 			ip1dbg(("ill_capability_dls_ack: interface %s "
3110 			    "has enabled polling\n", ill->ill_name));
3111 		}
3112 		mutex_exit(&ill->ill_lock);
3113 		break;
3114 	}
3115 }
3116 
3117 /*
3118  * Process a hardware checksum offload capability negotiation ack received
3119  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3120  * of a DL_CAPABILITY_ACK message.
3121  */
3122 static void
3123 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3124 {
3125 	dl_capability_req_t	*ocap;
3126 	dl_capab_hcksum_t	*ihck, *ohck;
3127 	ill_hcksum_capab_t	**ill_hcksum;
3128 	mblk_t			*nmp = NULL;
3129 	uint_t			sub_dl_cap = isub->dl_cap;
3130 	uint8_t			*capend;
3131 
3132 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3133 
3134 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3135 
3136 	/*
3137 	 * Note: range checks here are not absolutely sufficient to
3138 	 * make us robust against malformed messages sent by drivers;
3139 	 * this is in keeping with the rest of IP's dlpi handling.
3140 	 * (Remember, it's coming from something else in the kernel
3141 	 * address space)
3142 	 */
3143 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3144 	if (capend > mp->b_wptr) {
3145 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3146 		    "malformed sub-capability too long for mblk");
3147 		return;
3148 	}
3149 
3150 	/*
3151 	 * There are two types of acks we process here:
3152 	 * 1. acks in reply to a (first form) generic capability req
3153 	 *    (no ENABLE flag set)
3154 	 * 2. acks in reply to a ENABLE capability req.
3155 	 *    (ENABLE flag set)
3156 	 */
3157 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3158 
3159 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3160 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3161 		    "unsupported hardware checksum "
3162 		    "sub-capability (version %d, expected %d)",
3163 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3164 		return;
3165 	}
3166 
3167 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3168 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3169 		    "checksum capability isn't as expected; pass-thru "
3170 		    "module(s) detected, discarding capability\n"));
3171 		return;
3172 	}
3173 
3174 #define	CURR_HCKSUM_CAPAB				\
3175 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3176 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3177 
3178 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3179 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3180 		/* do ENABLE processing */
3181 		if (*ill_hcksum == NULL) {
3182 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3183 			    KM_NOSLEEP);
3184 
3185 			if (*ill_hcksum == NULL) {
3186 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3187 				    "could not enable hcksum version %d "
3188 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3189 				    ill->ill_name);
3190 				return;
3191 			}
3192 		}
3193 
3194 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3195 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3196 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3197 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3198 		    "has enabled hardware checksumming\n ",
3199 		    ill->ill_name));
3200 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3201 		/*
3202 		 * Enabling hardware checksum offload
3203 		 * Currently IP supports {TCP,UDP}/IPv4
3204 		 * partial and full cksum offload and
3205 		 * IPv4 header checksum offload.
3206 		 * Allocate new mblk which will
3207 		 * contain a new capability request
3208 		 * to enable hardware checksum offload.
3209 		 */
3210 		uint_t	size;
3211 		uchar_t	*rptr;
3212 
3213 		size = sizeof (dl_capability_req_t) +
3214 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3215 
3216 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3217 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3218 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3219 			    ill->ill_name);
3220 			return;
3221 		}
3222 
3223 		rptr = nmp->b_rptr;
3224 		/* initialize dl_capability_req_t */
3225 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3226 		ocap->dl_sub_offset =
3227 		    sizeof (dl_capability_req_t);
3228 		ocap->dl_sub_length =
3229 		    sizeof (dl_capability_sub_t) +
3230 		    isub->dl_length;
3231 		nmp->b_rptr += sizeof (dl_capability_req_t);
3232 
3233 		/* initialize dl_capability_sub_t */
3234 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3235 		nmp->b_rptr += sizeof (*isub);
3236 
3237 		/* initialize dl_capab_hcksum_t */
3238 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3239 		bcopy(ihck, ohck, sizeof (*ihck));
3240 
3241 		nmp->b_rptr = rptr;
3242 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3243 
3244 		/* Set ENABLE flag */
3245 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3246 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3247 
3248 		/*
3249 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3250 		 * hardware checksum acceleration.
3251 		 */
3252 		ill_dlpi_send(ill, nmp);
3253 	} else {
3254 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3255 		    "advertised %x hardware checksum capability flags\n",
3256 		    ill->ill_name, ihck->hcksum_txflags));
3257 	}
3258 }
3259 
3260 static void
3261 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3262 {
3263 	mblk_t *mp;
3264 	dl_capab_hcksum_t *hck_subcap;
3265 	dl_capability_sub_t *dl_subcap;
3266 	int size;
3267 
3268 	if (!ILL_HCKSUM_CAPABLE(ill))
3269 		return;
3270 
3271 	ASSERT(ill->ill_hcksum_capab != NULL);
3272 	/*
3273 	 * Clear the capability flag for hardware checksum offload but
3274 	 * retain the ill_hcksum_capab structure since it's possible that
3275 	 * another thread is still referring to it.  The structure only
3276 	 * gets deallocated when we destroy the ill.
3277 	 */
3278 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3279 
3280 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3281 
3282 	mp = allocb(size, BPRI_HI);
3283 	if (mp == NULL) {
3284 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3285 		    "request to disable hardware checksum offload\n"));
3286 		return;
3287 	}
3288 
3289 	mp->b_wptr = mp->b_rptr + size;
3290 
3291 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3292 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3293 	dl_subcap->dl_length = sizeof (*hck_subcap);
3294 
3295 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3296 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3297 	hck_subcap->hcksum_txflags = 0;
3298 
3299 	if (*sc_mp != NULL)
3300 		linkb(*sc_mp, mp);
3301 	else
3302 		*sc_mp = mp;
3303 }
3304 
3305 static void
3306 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3307 {
3308 	mblk_t *nmp = NULL;
3309 	dl_capability_req_t *oc;
3310 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3311 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3312 	uint_t sub_dl_cap = isub->dl_cap;
3313 	uint8_t *capend;
3314 
3315 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3316 
3317 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3318 
3319 	/*
3320 	 * Note: range checks here are not absolutely sufficient to
3321 	 * make us robust against malformed messages sent by drivers;
3322 	 * this is in keeping with the rest of IP's dlpi handling.
3323 	 * (Remember, it's coming from something else in the kernel
3324 	 * address space)
3325 	 */
3326 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3327 	if (capend > mp->b_wptr) {
3328 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3329 		    "malformed sub-capability too long for mblk");
3330 		return;
3331 	}
3332 
3333 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3334 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3335 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3336 		    "unsupported ZEROCOPY sub-capability (version %d, "
3337 		    "expected %d)", zc_ic->zerocopy_version,
3338 		    ZEROCOPY_VERSION_1);
3339 		return;
3340 	}
3341 
3342 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3343 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3344 		    "capability isn't as expected; pass-thru module(s) "
3345 		    "detected, discarding capability\n"));
3346 		return;
3347 	}
3348 
3349 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3350 		if (*ill_zerocopy_capab == NULL) {
3351 			*ill_zerocopy_capab =
3352 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3353 			    KM_NOSLEEP);
3354 
3355 			if (*ill_zerocopy_capab == NULL) {
3356 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3357 				    "could not enable Zero-copy version %d "
3358 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3359 				    ill->ill_name);
3360 				return;
3361 			}
3362 		}
3363 
3364 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3365 		    "supports Zero-copy version %d\n", ill->ill_name,
3366 		    ZEROCOPY_VERSION_1));
3367 
3368 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3369 		    zc_ic->zerocopy_version;
3370 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3371 		    zc_ic->zerocopy_flags;
3372 
3373 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3374 	} else {
3375 		uint_t size;
3376 		uchar_t *rptr;
3377 
3378 		size = sizeof (dl_capability_req_t) +
3379 		    sizeof (dl_capability_sub_t) +
3380 		    sizeof (dl_capab_zerocopy_t);
3381 
3382 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3383 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3384 			    "could not enable zerocopy for %s (ENOMEM)\n",
3385 			    ill->ill_name);
3386 			return;
3387 		}
3388 
3389 		rptr = nmp->b_rptr;
3390 		/* initialize dl_capability_req_t */
3391 		oc = (dl_capability_req_t *)rptr;
3392 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3393 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3394 		    sizeof (dl_capab_zerocopy_t);
3395 		rptr += sizeof (dl_capability_req_t);
3396 
3397 		/* initialize dl_capability_sub_t */
3398 		bcopy(isub, rptr, sizeof (*isub));
3399 		rptr += sizeof (*isub);
3400 
3401 		/* initialize dl_capab_zerocopy_t */
3402 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3403 		*zc_oc = *zc_ic;
3404 
3405 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3406 		    "to enable zero-copy version %d\n", ill->ill_name,
3407 		    ZEROCOPY_VERSION_1));
3408 
3409 		/* set VMSAFE_MEM flag */
3410 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3411 
3412 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3413 		ill_dlpi_send(ill, nmp);
3414 	}
3415 }
3416 
3417 static void
3418 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3419 {
3420 	mblk_t *mp;
3421 	dl_capab_zerocopy_t *zerocopy_subcap;
3422 	dl_capability_sub_t *dl_subcap;
3423 	int size;
3424 
3425 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3426 		return;
3427 
3428 	ASSERT(ill->ill_zerocopy_capab != NULL);
3429 	/*
3430 	 * Clear the capability flag for Zero-copy but retain the
3431 	 * ill_zerocopy_capab structure since it's possible that another
3432 	 * thread is still referring to it.  The structure only gets
3433 	 * deallocated when we destroy the ill.
3434 	 */
3435 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3436 
3437 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3438 
3439 	mp = allocb(size, BPRI_HI);
3440 	if (mp == NULL) {
3441 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3442 		    "request to disable Zero-copy\n"));
3443 		return;
3444 	}
3445 
3446 	mp->b_wptr = mp->b_rptr + size;
3447 
3448 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3449 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3450 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3451 
3452 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3453 	zerocopy_subcap->zerocopy_version =
3454 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3455 	zerocopy_subcap->zerocopy_flags = 0;
3456 
3457 	if (*sc_mp != NULL)
3458 		linkb(*sc_mp, mp);
3459 	else
3460 		*sc_mp = mp;
3461 }
3462 
3463 /*
3464  * Process Large Segment Offload capability negotiation ack received from a
3465  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3466  * DL_CAPABILITY_ACK message.
3467  */
3468 static void
3469 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3470 {
3471 	mblk_t *nmp = NULL;
3472 	dl_capability_req_t *oc;
3473 	dl_capab_lso_t *lso_ic, *lso_oc;
3474 	ill_lso_capab_t **ill_lso_capab;
3475 	uint_t sub_dl_cap = isub->dl_cap;
3476 	uint8_t *capend;
3477 
3478 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3479 
3480 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3481 
3482 	/*
3483 	 * Note: range checks here are not absolutely sufficient to
3484 	 * make us robust against malformed messages sent by drivers;
3485 	 * this is in keeping with the rest of IP's dlpi handling.
3486 	 * (Remember, it's coming from something else in the kernel
3487 	 * address space)
3488 	 */
3489 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3490 	if (capend > mp->b_wptr) {
3491 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3492 		    "malformed sub-capability too long for mblk");
3493 		return;
3494 	}
3495 
3496 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3497 
3498 	if (lso_ic->lso_version != LSO_VERSION_1) {
3499 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3500 		    "unsupported LSO sub-capability (version %d, expected %d)",
3501 		    lso_ic->lso_version, LSO_VERSION_1);
3502 		return;
3503 	}
3504 
3505 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3506 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3507 		    "capability isn't as expected; pass-thru module(s) "
3508 		    "detected, discarding capability\n"));
3509 		return;
3510 	}
3511 
3512 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3513 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3514 		if (*ill_lso_capab == NULL) {
3515 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3516 			    KM_NOSLEEP);
3517 
3518 			if (*ill_lso_capab == NULL) {
3519 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3520 				    "could not enable LSO version %d "
3521 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3522 				    ill->ill_name);
3523 				return;
3524 			}
3525 		}
3526 
3527 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3528 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3529 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3530 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3531 
3532 		ip1dbg(("ill_capability_lso_ack: interface %s "
3533 		    "has enabled LSO\n ", ill->ill_name));
3534 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3535 		uint_t size;
3536 		uchar_t *rptr;
3537 
3538 		size = sizeof (dl_capability_req_t) +
3539 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3540 
3541 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3542 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3543 			    "could not enable LSO for %s (ENOMEM)\n",
3544 			    ill->ill_name);
3545 			return;
3546 		}
3547 
3548 		rptr = nmp->b_rptr;
3549 		/* initialize dl_capability_req_t */
3550 		oc = (dl_capability_req_t *)nmp->b_rptr;
3551 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3552 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3553 		    sizeof (dl_capab_lso_t);
3554 		nmp->b_rptr += sizeof (dl_capability_req_t);
3555 
3556 		/* initialize dl_capability_sub_t */
3557 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3558 		nmp->b_rptr += sizeof (*isub);
3559 
3560 		/* initialize dl_capab_lso_t */
3561 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3562 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3563 
3564 		nmp->b_rptr = rptr;
3565 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3566 
3567 		/* set ENABLE flag */
3568 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3569 
3570 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3571 		ill_dlpi_send(ill, nmp);
3572 	} else {
3573 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3574 		    "advertised %x LSO capability flags\n",
3575 		    ill->ill_name, lso_ic->lso_flags));
3576 	}
3577 }
3578 
3579 
3580 static void
3581 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3582 {
3583 	mblk_t *mp;
3584 	dl_capab_lso_t *lso_subcap;
3585 	dl_capability_sub_t *dl_subcap;
3586 	int size;
3587 
3588 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3589 		return;
3590 
3591 	ASSERT(ill->ill_lso_capab != NULL);
3592 	/*
3593 	 * Clear the capability flag for LSO but retain the
3594 	 * ill_lso_capab structure since it's possible that another
3595 	 * thread is still referring to it.  The structure only gets
3596 	 * deallocated when we destroy the ill.
3597 	 */
3598 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3599 
3600 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3601 
3602 	mp = allocb(size, BPRI_HI);
3603 	if (mp == NULL) {
3604 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3605 		    "request to disable LSO\n"));
3606 		return;
3607 	}
3608 
3609 	mp->b_wptr = mp->b_rptr + size;
3610 
3611 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3612 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3613 	dl_subcap->dl_length = sizeof (*lso_subcap);
3614 
3615 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3616 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3617 	lso_subcap->lso_flags = 0;
3618 
3619 	if (*sc_mp != NULL)
3620 		linkb(*sc_mp, mp);
3621 	else
3622 		*sc_mp = mp;
3623 }
3624 
3625 /*
3626  * Consume a new-style hardware capabilities negotiation ack.
3627  * Called from ip_rput_dlpi_writer().
3628  */
3629 void
3630 ill_capability_ack(ill_t *ill, mblk_t *mp)
3631 {
3632 	dl_capability_ack_t *capp;
3633 	dl_capability_sub_t *subp, *endp;
3634 
3635 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3636 		ill->ill_dlpi_capab_state = IDS_OK;
3637 
3638 	capp = (dl_capability_ack_t *)mp->b_rptr;
3639 
3640 	if (capp->dl_sub_length == 0)
3641 		/* no new-style capabilities */
3642 		return;
3643 
3644 	/* make sure the driver supplied correct dl_sub_length */
3645 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3646 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3647 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3648 		return;
3649 	}
3650 
3651 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3652 	/*
3653 	 * There are sub-capabilities. Process the ones we know about.
3654 	 * Loop until we don't have room for another sub-cap header..
3655 	 */
3656 	for (subp = SC(capp, capp->dl_sub_offset),
3657 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3658 	    subp <= endp;
3659 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3660 
3661 		switch (subp->dl_cap) {
3662 		case DL_CAPAB_ID_WRAPPER:
3663 			ill_capability_id_ack(ill, mp, subp);
3664 			break;
3665 		default:
3666 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3667 			break;
3668 		}
3669 	}
3670 #undef SC
3671 }
3672 
3673 /*
3674  * This routine is called to scan the fragmentation reassembly table for
3675  * the specified ILL for any packets that are starting to smell.
3676  * dead_interval is the maximum time in seconds that will be tolerated.  It
3677  * will either be the value specified in ip_g_frag_timeout, or zero if the
3678  * ILL is shutting down and it is time to blow everything off.
3679  *
3680  * It returns the number of seconds (as a time_t) that the next frag timer
3681  * should be scheduled for, 0 meaning that the timer doesn't need to be
3682  * re-started.  Note that the method of calculating next_timeout isn't
3683  * entirely accurate since time will flow between the time we grab
3684  * current_time and the time we schedule the next timeout.  This isn't a
3685  * big problem since this is the timer for sending an ICMP reassembly time
3686  * exceeded messages, and it doesn't have to be exactly accurate.
3687  *
3688  * This function is
3689  * sometimes called as writer, although this is not required.
3690  */
3691 time_t
3692 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3693 {
3694 	ipfb_t	*ipfb;
3695 	ipfb_t	*endp;
3696 	ipf_t	*ipf;
3697 	ipf_t	*ipfnext;
3698 	mblk_t	*mp;
3699 	time_t	current_time = gethrestime_sec();
3700 	time_t	next_timeout = 0;
3701 	uint32_t	hdr_length;
3702 	mblk_t	*send_icmp_head;
3703 	mblk_t	*send_icmp_head_v6;
3704 	zoneid_t zoneid;
3705 	ip_stack_t *ipst = ill->ill_ipst;
3706 
3707 	ipfb = ill->ill_frag_hash_tbl;
3708 	if (ipfb == NULL)
3709 		return (B_FALSE);
3710 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3711 	/* Walk the frag hash table. */
3712 	for (; ipfb < endp; ipfb++) {
3713 		send_icmp_head = NULL;
3714 		send_icmp_head_v6 = NULL;
3715 		mutex_enter(&ipfb->ipfb_lock);
3716 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3717 			time_t frag_time = current_time - ipf->ipf_timestamp;
3718 			time_t frag_timeout;
3719 
3720 			if (frag_time < dead_interval) {
3721 				/*
3722 				 * There are some outstanding fragments
3723 				 * that will timeout later.  Make note of
3724 				 * the time so that we can reschedule the
3725 				 * next timeout appropriately.
3726 				 */
3727 				frag_timeout = dead_interval - frag_time;
3728 				if (next_timeout == 0 ||
3729 				    frag_timeout < next_timeout) {
3730 					next_timeout = frag_timeout;
3731 				}
3732 				break;
3733 			}
3734 			/* Time's up.  Get it out of here. */
3735 			hdr_length = ipf->ipf_nf_hdr_len;
3736 			ipfnext = ipf->ipf_hash_next;
3737 			if (ipfnext)
3738 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3739 			*ipf->ipf_ptphn = ipfnext;
3740 			mp = ipf->ipf_mp->b_cont;
3741 			for (; mp; mp = mp->b_cont) {
3742 				/* Extra points for neatness. */
3743 				IP_REASS_SET_START(mp, 0);
3744 				IP_REASS_SET_END(mp, 0);
3745 			}
3746 			mp = ipf->ipf_mp->b_cont;
3747 			ill->ill_frag_count -= ipf->ipf_count;
3748 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3749 			ipfb->ipfb_count -= ipf->ipf_count;
3750 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3751 			ipfb->ipfb_frag_pkts--;
3752 			/*
3753 			 * We do not send any icmp message from here because
3754 			 * we currently are holding the ipfb_lock for this
3755 			 * hash chain. If we try and send any icmp messages
3756 			 * from here we may end up via a put back into ip
3757 			 * trying to get the same lock, causing a recursive
3758 			 * mutex panic. Instead we build a list and send all
3759 			 * the icmp messages after we have dropped the lock.
3760 			 */
3761 			if (ill->ill_isv6) {
3762 				if (hdr_length != 0) {
3763 					mp->b_next = send_icmp_head_v6;
3764 					send_icmp_head_v6 = mp;
3765 				} else {
3766 					freemsg(mp);
3767 				}
3768 			} else {
3769 				if (hdr_length != 0) {
3770 					mp->b_next = send_icmp_head;
3771 					send_icmp_head = mp;
3772 				} else {
3773 					freemsg(mp);
3774 				}
3775 			}
3776 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3777 			freeb(ipf->ipf_mp);
3778 		}
3779 		mutex_exit(&ipfb->ipfb_lock);
3780 		/*
3781 		 * Now need to send any icmp messages that we delayed from
3782 		 * above.
3783 		 */
3784 		while (send_icmp_head_v6 != NULL) {
3785 			ip6_t *ip6h;
3786 
3787 			mp = send_icmp_head_v6;
3788 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3789 			mp->b_next = NULL;
3790 			if (mp->b_datap->db_type == M_CTL)
3791 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3792 			else
3793 				ip6h = (ip6_t *)mp->b_rptr;
3794 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3795 			    ill, ipst);
3796 			if (zoneid == ALL_ZONES) {
3797 				freemsg(mp);
3798 			} else {
3799 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3800 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3801 				    B_FALSE, zoneid, ipst);
3802 			}
3803 		}
3804 		while (send_icmp_head != NULL) {
3805 			ipaddr_t dst;
3806 
3807 			mp = send_icmp_head;
3808 			send_icmp_head = send_icmp_head->b_next;
3809 			mp->b_next = NULL;
3810 
3811 			if (mp->b_datap->db_type == M_CTL)
3812 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3813 			else
3814 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3815 
3816 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3817 			if (zoneid == ALL_ZONES) {
3818 				freemsg(mp);
3819 			} else {
3820 				icmp_time_exceeded(ill->ill_wq, mp,
3821 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3822 				    ipst);
3823 			}
3824 		}
3825 	}
3826 	/*
3827 	 * A non-dying ILL will use the return value to decide whether to
3828 	 * restart the frag timer, and for how long.
3829 	 */
3830 	return (next_timeout);
3831 }
3832 
3833 /*
3834  * This routine is called when the approximate count of mblk memory used
3835  * for the specified ILL has exceeded max_count.
3836  */
3837 void
3838 ill_frag_prune(ill_t *ill, uint_t max_count)
3839 {
3840 	ipfb_t	*ipfb;
3841 	ipf_t	*ipf;
3842 	size_t	count;
3843 
3844 	/*
3845 	 * If we are here within ip_min_frag_prune_time msecs remove
3846 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3847 	 * ill_frag_free_num_pkts.
3848 	 */
3849 	mutex_enter(&ill->ill_lock);
3850 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3851 	    (ip_min_frag_prune_time != 0 ?
3852 	    ip_min_frag_prune_time : msec_per_tick)) {
3853 
3854 		ill->ill_frag_free_num_pkts++;
3855 
3856 	} else {
3857 		ill->ill_frag_free_num_pkts = 0;
3858 	}
3859 	ill->ill_last_frag_clean_time = lbolt;
3860 	mutex_exit(&ill->ill_lock);
3861 
3862 	/*
3863 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3864 	 */
3865 	if (ill->ill_frag_free_num_pkts != 0) {
3866 		int ix;
3867 
3868 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3869 			ipfb = &ill->ill_frag_hash_tbl[ix];
3870 			mutex_enter(&ipfb->ipfb_lock);
3871 			if (ipfb->ipfb_ipf != NULL) {
3872 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3873 				    ill->ill_frag_free_num_pkts);
3874 			}
3875 			mutex_exit(&ipfb->ipfb_lock);
3876 		}
3877 	}
3878 	/*
3879 	 * While the reassembly list for this ILL is too big, prune a fragment
3880 	 * queue by age, oldest first.  Note that the per ILL count is
3881 	 * approximate, while the per frag hash bucket counts are accurate.
3882 	 */
3883 	while (ill->ill_frag_count > max_count) {
3884 		int	ix;
3885 		ipfb_t	*oipfb = NULL;
3886 		uint_t	oldest = UINT_MAX;
3887 
3888 		count = 0;
3889 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3890 			ipfb = &ill->ill_frag_hash_tbl[ix];
3891 			mutex_enter(&ipfb->ipfb_lock);
3892 			ipf = ipfb->ipfb_ipf;
3893 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3894 				oldest = ipf->ipf_gen;
3895 				oipfb = ipfb;
3896 			}
3897 			count += ipfb->ipfb_count;
3898 			mutex_exit(&ipfb->ipfb_lock);
3899 		}
3900 		/* Refresh the per ILL count */
3901 		ill->ill_frag_count = count;
3902 		if (oipfb == NULL) {
3903 			ill->ill_frag_count = 0;
3904 			break;
3905 		}
3906 		if (count <= max_count)
3907 			return;	/* Somebody beat us to it, nothing to do */
3908 		mutex_enter(&oipfb->ipfb_lock);
3909 		ipf = oipfb->ipfb_ipf;
3910 		if (ipf != NULL) {
3911 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3912 		}
3913 		mutex_exit(&oipfb->ipfb_lock);
3914 	}
3915 }
3916 
3917 /*
3918  * free 'free_cnt' fragmented packets starting at ipf.
3919  */
3920 void
3921 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3922 {
3923 	size_t	count;
3924 	mblk_t	*mp;
3925 	mblk_t	*tmp;
3926 	ipf_t **ipfp = ipf->ipf_ptphn;
3927 
3928 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3929 	ASSERT(ipfp != NULL);
3930 	ASSERT(ipf != NULL);
3931 
3932 	while (ipf != NULL && free_cnt-- > 0) {
3933 		count = ipf->ipf_count;
3934 		mp = ipf->ipf_mp;
3935 		ipf = ipf->ipf_hash_next;
3936 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3937 			IP_REASS_SET_START(tmp, 0);
3938 			IP_REASS_SET_END(tmp, 0);
3939 		}
3940 		ill->ill_frag_count -= count;
3941 		ASSERT(ipfb->ipfb_count >= count);
3942 		ipfb->ipfb_count -= count;
3943 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3944 		ipfb->ipfb_frag_pkts--;
3945 		freemsg(mp);
3946 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3947 	}
3948 
3949 	if (ipf)
3950 		ipf->ipf_ptphn = ipfp;
3951 	ipfp[0] = ipf;
3952 }
3953 
3954 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3955 	"obsolete and may be removed in a future release of Solaris.  Use " \
3956 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3957 
3958 /*
3959  * For obsolete per-interface forwarding configuration;
3960  * called in response to ND_GET.
3961  */
3962 /* ARGSUSED */
3963 static int
3964 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3965 {
3966 	ill_t *ill = (ill_t *)cp;
3967 
3968 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3969 
3970 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3971 	return (0);
3972 }
3973 
3974 /*
3975  * For obsolete per-interface forwarding configuration;
3976  * called in response to ND_SET.
3977  */
3978 /* ARGSUSED */
3979 static int
3980 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3981     cred_t *ioc_cr)
3982 {
3983 	long value;
3984 	int retval;
3985 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3986 
3987 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3988 
3989 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3990 	    value < 0 || value > 1) {
3991 		return (EINVAL);
3992 	}
3993 
3994 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3995 	retval = ill_forward_set(q, mp, (value != 0), cp);
3996 	rw_exit(&ipst->ips_ill_g_lock);
3997 	return (retval);
3998 }
3999 
4000 /*
4001  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
4002  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
4003  * up RTS_IFINFO routing socket messages for each interface whose flags we
4004  * change.
4005  */
4006 /* ARGSUSED */
4007 int
4008 ill_forward_set(queue_t *q, mblk_t *mp, boolean_t enable, caddr_t cp)
4009 {
4010 	ill_t *ill = (ill_t *)cp;
4011 	ill_group_t *illgrp;
4012 	ip_stack_t	*ipst = ill->ill_ipst;
4013 
4014 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
4015 
4016 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
4017 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)) ||
4018 	    (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK))
4019 		return (EINVAL);
4020 
4021 	/*
4022 	 * If the ill is in an IPMP group, set the forwarding policy on all
4023 	 * members of the group to the same value.
4024 	 */
4025 	illgrp = ill->ill_group;
4026 	if (illgrp != NULL) {
4027 		ill_t *tmp_ill;
4028 
4029 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
4030 		    tmp_ill = tmp_ill->ill_group_next) {
4031 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
4032 			    (enable ? "Enabling" : "Disabling"),
4033 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
4034 			    tmp_ill->ill_name));
4035 			mutex_enter(&tmp_ill->ill_lock);
4036 			if (enable)
4037 				tmp_ill->ill_flags |= ILLF_ROUTER;
4038 			else
4039 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
4040 			mutex_exit(&tmp_ill->ill_lock);
4041 			if (tmp_ill->ill_isv6)
4042 				ill_set_nce_router_flags(tmp_ill, enable);
4043 			/* Notify routing socket listeners of this change. */
4044 			ip_rts_ifmsg(tmp_ill->ill_ipif);
4045 		}
4046 	} else {
4047 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
4048 		    (enable ? "Enabling" : "Disabling"),
4049 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
4050 		mutex_enter(&ill->ill_lock);
4051 		if (enable)
4052 			ill->ill_flags |= ILLF_ROUTER;
4053 		else
4054 			ill->ill_flags &= ~ILLF_ROUTER;
4055 		mutex_exit(&ill->ill_lock);
4056 		if (ill->ill_isv6)
4057 			ill_set_nce_router_flags(ill, enable);
4058 		/* Notify routing socket listeners of this change. */
4059 		ip_rts_ifmsg(ill->ill_ipif);
4060 	}
4061 
4062 	return (0);
4063 }
4064 
4065 /*
4066  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
4067  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
4068  * set or clear.
4069  */
4070 static void
4071 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
4072 {
4073 	ipif_t *ipif;
4074 	nce_t *nce;
4075 
4076 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4077 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
4078 		if (nce != NULL) {
4079 			mutex_enter(&nce->nce_lock);
4080 			if (enable)
4081 				nce->nce_flags |= NCE_F_ISROUTER;
4082 			else
4083 				nce->nce_flags &= ~NCE_F_ISROUTER;
4084 			mutex_exit(&nce->nce_lock);
4085 			NCE_REFRELE(nce);
4086 		}
4087 	}
4088 }
4089 
4090 /*
4091  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
4092  * for this ill.  Make sure the v6/v4 question has been answered about this
4093  * ill.  The creation of this ndd variable is only for backwards compatibility.
4094  * The preferred way to control per-interface IP forwarding is through the
4095  * ILLF_ROUTER interface flag.
4096  */
4097 static int
4098 ill_set_ndd_name(ill_t *ill)
4099 {
4100 	char *suffix;
4101 	ip_stack_t	*ipst = ill->ill_ipst;
4102 
4103 	ASSERT(IAM_WRITER_ILL(ill));
4104 
4105 	if (ill->ill_isv6)
4106 		suffix = ipv6_forward_suffix;
4107 	else
4108 		suffix = ipv4_forward_suffix;
4109 
4110 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4111 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4112 	/*
4113 	 * Copies over the '\0'.
4114 	 * Note that strlen(suffix) is always bounded.
4115 	 */
4116 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4117 	    strlen(suffix) + 1);
4118 
4119 	/*
4120 	 * Use of the nd table requires holding the reader lock.
4121 	 * Modifying the nd table thru nd_load/nd_unload requires
4122 	 * the writer lock.
4123 	 */
4124 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4125 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4126 	    nd_ill_forward_set, (caddr_t)ill)) {
4127 		/*
4128 		 * If the nd_load failed, it only meant that it could not
4129 		 * allocate a new bunch of room for further NDD expansion.
4130 		 * Because of that, the ill_ndd_name will be set to 0, and
4131 		 * this interface is at the mercy of the global ip_forwarding
4132 		 * variable.
4133 		 */
4134 		rw_exit(&ipst->ips_ip_g_nd_lock);
4135 		ill->ill_ndd_name = NULL;
4136 		return (ENOMEM);
4137 	}
4138 	rw_exit(&ipst->ips_ip_g_nd_lock);
4139 	return (0);
4140 }
4141 
4142 /*
4143  * Intializes the context structure and returns the first ill in the list
4144  * cuurently start_list and end_list can have values:
4145  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4146  * IP_V4_G_HEAD		Traverse IPV4 list only.
4147  * IP_V6_G_HEAD		Traverse IPV6 list only.
4148  */
4149 
4150 /*
4151  * We don't check for CONDEMNED ills here. Caller must do that if
4152  * necessary under the ill lock.
4153  */
4154 ill_t *
4155 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4156     ip_stack_t *ipst)
4157 {
4158 	ill_if_t *ifp;
4159 	ill_t *ill;
4160 	avl_tree_t *avl_tree;
4161 
4162 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4163 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4164 
4165 	/*
4166 	 * setup the lists to search
4167 	 */
4168 	if (end_list != MAX_G_HEADS) {
4169 		ctx->ctx_current_list = start_list;
4170 		ctx->ctx_last_list = end_list;
4171 	} else {
4172 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4173 		ctx->ctx_current_list = 0;
4174 	}
4175 
4176 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4177 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4178 		if (ifp != (ill_if_t *)
4179 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4180 			avl_tree = &ifp->illif_avl_by_ppa;
4181 			ill = avl_first(avl_tree);
4182 			/*
4183 			 * ill is guaranteed to be non NULL or ifp should have
4184 			 * not existed.
4185 			 */
4186 			ASSERT(ill != NULL);
4187 			return (ill);
4188 		}
4189 		ctx->ctx_current_list++;
4190 	}
4191 
4192 	return (NULL);
4193 }
4194 
4195 /*
4196  * returns the next ill in the list. ill_first() must have been called
4197  * before calling ill_next() or bad things will happen.
4198  */
4199 
4200 /*
4201  * We don't check for CONDEMNED ills here. Caller must do that if
4202  * necessary under the ill lock.
4203  */
4204 ill_t *
4205 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4206 {
4207 	ill_if_t *ifp;
4208 	ill_t *ill;
4209 	ip_stack_t	*ipst = lastill->ill_ipst;
4210 
4211 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4212 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4213 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4214 	    AVL_AFTER)) != NULL) {
4215 		return (ill);
4216 	}
4217 
4218 	/* goto next ill_ifp in the list. */
4219 	ifp = lastill->ill_ifptr->illif_next;
4220 
4221 	/* make sure not at end of circular list */
4222 	while (ifp ==
4223 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4224 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4225 			return (NULL);
4226 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4227 	}
4228 
4229 	return (avl_first(&ifp->illif_avl_by_ppa));
4230 }
4231 
4232 /*
4233  * Check interface name for correct format which is name+ppa.
4234  * name can contain characters and digits, the right most digits
4235  * make up the ppa number. use of octal is not allowed, name must contain
4236  * a ppa, return pointer to the start of ppa.
4237  * In case of error return NULL.
4238  */
4239 static char *
4240 ill_get_ppa_ptr(char *name)
4241 {
4242 	int namelen = mi_strlen(name);
4243 
4244 	int len = namelen;
4245 
4246 	name += len;
4247 	while (len > 0) {
4248 		name--;
4249 		if (*name < '0' || *name > '9')
4250 			break;
4251 		len--;
4252 	}
4253 
4254 	/* empty string, all digits, or no trailing digits */
4255 	if (len == 0 || len == (int)namelen)
4256 		return (NULL);
4257 
4258 	name++;
4259 	/* check for attempted use of octal */
4260 	if (*name == '0' && len != (int)namelen - 1)
4261 		return (NULL);
4262 	return (name);
4263 }
4264 
4265 /*
4266  * use avl tree to locate the ill.
4267  */
4268 static ill_t *
4269 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4270     ipsq_func_t func, int *error, ip_stack_t *ipst)
4271 {
4272 	char *ppa_ptr = NULL;
4273 	int len;
4274 	uint_t ppa;
4275 	ill_t *ill = NULL;
4276 	ill_if_t *ifp;
4277 	int list;
4278 	ipsq_t *ipsq;
4279 
4280 	if (error != NULL)
4281 		*error = 0;
4282 
4283 	/*
4284 	 * get ppa ptr
4285 	 */
4286 	if (isv6)
4287 		list = IP_V6_G_HEAD;
4288 	else
4289 		list = IP_V4_G_HEAD;
4290 
4291 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4292 		if (error != NULL)
4293 			*error = ENXIO;
4294 		return (NULL);
4295 	}
4296 
4297 	len = ppa_ptr - name + 1;
4298 
4299 	ppa = stoi(&ppa_ptr);
4300 
4301 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4302 
4303 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4304 		/*
4305 		 * match is done on len - 1 as the name is not null
4306 		 * terminated it contains ppa in addition to the interface
4307 		 * name.
4308 		 */
4309 		if ((ifp->illif_name_len == len) &&
4310 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4311 			break;
4312 		} else {
4313 			ifp = ifp->illif_next;
4314 		}
4315 	}
4316 
4317 
4318 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4319 		/*
4320 		 * Even the interface type does not exist.
4321 		 */
4322 		if (error != NULL)
4323 			*error = ENXIO;
4324 		return (NULL);
4325 	}
4326 
4327 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4328 	if (ill != NULL) {
4329 		/*
4330 		 * The block comment at the start of ipif_down
4331 		 * explains the use of the macros used below
4332 		 */
4333 		GRAB_CONN_LOCK(q);
4334 		mutex_enter(&ill->ill_lock);
4335 		if (ILL_CAN_LOOKUP(ill)) {
4336 			ill_refhold_locked(ill);
4337 			mutex_exit(&ill->ill_lock);
4338 			RELEASE_CONN_LOCK(q);
4339 			return (ill);
4340 		} else if (ILL_CAN_WAIT(ill, q)) {
4341 			ipsq = ill->ill_phyint->phyint_ipsq;
4342 			mutex_enter(&ipsq->ipsq_lock);
4343 			mutex_exit(&ill->ill_lock);
4344 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4345 			mutex_exit(&ipsq->ipsq_lock);
4346 			RELEASE_CONN_LOCK(q);
4347 			*error = EINPROGRESS;
4348 			return (NULL);
4349 		}
4350 		mutex_exit(&ill->ill_lock);
4351 		RELEASE_CONN_LOCK(q);
4352 	}
4353 	if (error != NULL)
4354 		*error = ENXIO;
4355 	return (NULL);
4356 }
4357 
4358 /*
4359  * comparison function for use with avl.
4360  */
4361 static int
4362 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4363 {
4364 	uint_t ppa;
4365 	uint_t ill_ppa;
4366 
4367 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4368 
4369 	ppa = *((uint_t *)ppa_ptr);
4370 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4371 	/*
4372 	 * We want the ill with the lowest ppa to be on the
4373 	 * top.
4374 	 */
4375 	if (ill_ppa < ppa)
4376 		return (1);
4377 	if (ill_ppa > ppa)
4378 		return (-1);
4379 	return (0);
4380 }
4381 
4382 /*
4383  * remove an interface type from the global list.
4384  */
4385 static void
4386 ill_delete_interface_type(ill_if_t *interface)
4387 {
4388 	ASSERT(interface != NULL);
4389 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4390 
4391 	avl_destroy(&interface->illif_avl_by_ppa);
4392 	if (interface->illif_ppa_arena != NULL)
4393 		vmem_destroy(interface->illif_ppa_arena);
4394 
4395 	remque(interface);
4396 
4397 	mi_free(interface);
4398 }
4399 
4400 /* Defined in ip_netinfo.c */
4401 extern ddi_taskq_t	*eventq_queue_nic;
4402 
4403 /*
4404  * remove ill from the global list.
4405  */
4406 static void
4407 ill_glist_delete(ill_t *ill)
4408 {
4409 	char *nicname;
4410 	size_t nicnamelen;
4411 	hook_nic_event_t *info;
4412 	ip_stack_t	*ipst;
4413 
4414 	if (ill == NULL)
4415 		return;
4416 	ipst = ill->ill_ipst;
4417 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4418 
4419 	if (ill->ill_name != NULL) {
4420 		nicname = kmem_alloc(ill->ill_name_length, KM_NOSLEEP);
4421 		if (nicname != NULL) {
4422 			bcopy(ill->ill_name, nicname, ill->ill_name_length);
4423 			nicnamelen = ill->ill_name_length;
4424 		}
4425 	} else {
4426 		nicname = NULL;
4427 		nicnamelen = 0;
4428 	}
4429 
4430 	/*
4431 	 * If the ill was never inserted into the AVL tree
4432 	 * we skip the if branch.
4433 	 */
4434 	if (ill->ill_ifptr != NULL) {
4435 		/*
4436 		 * remove from AVL tree and free ppa number
4437 		 */
4438 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4439 
4440 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4441 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4442 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4443 		}
4444 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4445 			ill_delete_interface_type(ill->ill_ifptr);
4446 		}
4447 
4448 		/*
4449 		 * Indicate ill is no longer in the list.
4450 		 */
4451 		ill->ill_ifptr = NULL;
4452 		ill->ill_name_length = 0;
4453 		ill->ill_name[0] = '\0';
4454 		ill->ill_ppa = UINT_MAX;
4455 	}
4456 
4457 	/*
4458 	 * Run the unplumb hook after the NIC has disappeared from being
4459 	 * visible so that attempts to revalidate its existance will fail.
4460 	 *
4461 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4462 	 * that the ordering of delivered events to listeners matches the
4463 	 * order of them in the kernel.
4464 	 */
4465 	if ((info = ill->ill_nic_event_info) != NULL) {
4466 		if (info->hne_event != NE_DOWN) {
4467 			ip2dbg(("ill_glist_delete: unexpected nic event %d "
4468 			    "attached for %s\n", info->hne_event,
4469 			    ill->ill_name));
4470 			if (info->hne_data != NULL)
4471 				kmem_free(info->hne_data, info->hne_datalen);
4472 			kmem_free(info, sizeof (hook_nic_event_t));
4473 		} else {
4474 			if (ddi_taskq_dispatch(eventq_queue_nic,
4475 			    ip_ne_queue_func, (void *)info, DDI_SLEEP)
4476 			    == DDI_FAILURE) {
4477 				ip2dbg(("ill_glist_delete: ddi_taskq_dispatch "
4478 				    "failed\n"));
4479 				if (info->hne_data != NULL)
4480 					kmem_free(info->hne_data,
4481 					    info->hne_datalen);
4482 				kmem_free(info, sizeof (hook_nic_event_t));
4483 			}
4484 		}
4485 	}
4486 
4487 	/* Generate NE_UNPLUMB event for ill_name. */
4488 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
4489 	if (info != NULL) {
4490 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
4491 		info->hne_lif = 0;
4492 		info->hne_event = NE_UNPLUMB;
4493 		info->hne_data = nicname;
4494 		info->hne_datalen = nicnamelen;
4495 		info->hne_family = ill->ill_isv6 ?
4496 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
4497 	} else {
4498 		ip2dbg(("ill_glist_delete: could not attach UNPLUMB nic event "
4499 		    "information for %s (ENOMEM)\n", ill->ill_name));
4500 		if (nicname != NULL)
4501 			kmem_free(nicname, nicnamelen);
4502 	}
4503 
4504 	ill->ill_nic_event_info = info;
4505 
4506 	ill_phyint_free(ill);
4507 	rw_exit(&ipst->ips_ill_g_lock);
4508 }
4509 
4510 /*
4511  * allocate a ppa, if the number of plumbed interfaces of this type are
4512  * less than ill_no_arena do a linear search to find a unused ppa.
4513  * When the number goes beyond ill_no_arena switch to using an arena.
4514  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4515  * is the return value for an error condition, so allocation starts at one
4516  * and is decremented by one.
4517  */
4518 static int
4519 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4520 {
4521 	ill_t *tmp_ill;
4522 	uint_t start, end;
4523 	int ppa;
4524 
4525 	if (ifp->illif_ppa_arena == NULL &&
4526 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4527 		/*
4528 		 * Create an arena.
4529 		 */
4530 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4531 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4532 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4533 			/* allocate what has already been assigned */
4534 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4535 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4536 		    tmp_ill, AVL_AFTER)) {
4537 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4538 			    1,		/* size */
4539 			    1,		/* align/quantum */
4540 			    0,		/* phase */
4541 			    0,		/* nocross */
4542 		(void *)((uintptr_t)tmp_ill->ill_ppa + 1), /* minaddr */
4543 		(void *)((uintptr_t)tmp_ill->ill_ppa + 2), /* maxaddr */
4544 			    VM_NOSLEEP|VM_FIRSTFIT);
4545 			if (ppa == 0) {
4546 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4547 				    " failed while switching"));
4548 				vmem_destroy(ifp->illif_ppa_arena);
4549 				ifp->illif_ppa_arena = NULL;
4550 				break;
4551 			}
4552 		}
4553 	}
4554 
4555 	if (ifp->illif_ppa_arena != NULL) {
4556 		if (ill->ill_ppa == UINT_MAX) {
4557 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4558 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4559 			if (ppa == 0)
4560 				return (EAGAIN);
4561 			ill->ill_ppa = --ppa;
4562 		} else {
4563 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4564 			    1, 		/* size */
4565 			    1, 		/* align/quantum */
4566 			    0, 		/* phase */
4567 			    0, 		/* nocross */
4568 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4569 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4570 			    VM_NOSLEEP|VM_FIRSTFIT);
4571 			/*
4572 			 * Most likely the allocation failed because
4573 			 * the requested ppa was in use.
4574 			 */
4575 			if (ppa == 0)
4576 				return (EEXIST);
4577 		}
4578 		return (0);
4579 	}
4580 
4581 	/*
4582 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4583 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4584 	 */
4585 	if (ill->ill_ppa == UINT_MAX) {
4586 		end = UINT_MAX - 1;
4587 		start = 0;
4588 	} else {
4589 		end = start = ill->ill_ppa;
4590 	}
4591 
4592 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4593 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4594 		if (start++ >= end) {
4595 			if (ill->ill_ppa == UINT_MAX)
4596 				return (EAGAIN);
4597 			else
4598 				return (EEXIST);
4599 		}
4600 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4601 	}
4602 	ill->ill_ppa = start;
4603 	return (0);
4604 }
4605 
4606 /*
4607  * Insert ill into the list of configured ill's. Once this function completes,
4608  * the ill is globally visible and is available through lookups. More precisely
4609  * this happens after the caller drops the ill_g_lock.
4610  */
4611 static int
4612 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4613 {
4614 	ill_if_t *ill_interface;
4615 	avl_index_t where = 0;
4616 	int error;
4617 	int name_length;
4618 	int index;
4619 	boolean_t check_length = B_FALSE;
4620 	ip_stack_t	*ipst = ill->ill_ipst;
4621 
4622 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4623 
4624 	name_length = mi_strlen(name) + 1;
4625 
4626 	if (isv6)
4627 		index = IP_V6_G_HEAD;
4628 	else
4629 		index = IP_V4_G_HEAD;
4630 
4631 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4632 	/*
4633 	 * Search for interface type based on name
4634 	 */
4635 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4636 		if ((ill_interface->illif_name_len == name_length) &&
4637 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4638 			break;
4639 		}
4640 		ill_interface = ill_interface->illif_next;
4641 	}
4642 
4643 	/*
4644 	 * Interface type not found, create one.
4645 	 */
4646 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4647 
4648 		ill_g_head_t ghead;
4649 
4650 		/*
4651 		 * allocate ill_if_t structure
4652 		 */
4653 
4654 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4655 		if (ill_interface == NULL) {
4656 			return (ENOMEM);
4657 		}
4658 
4659 
4660 
4661 		(void) strcpy(ill_interface->illif_name, name);
4662 		ill_interface->illif_name_len = name_length;
4663 
4664 		avl_create(&ill_interface->illif_avl_by_ppa,
4665 		    ill_compare_ppa, sizeof (ill_t),
4666 		    offsetof(struct ill_s, ill_avl_byppa));
4667 
4668 		/*
4669 		 * link the structure in the back to maintain order
4670 		 * of configuration for ifconfig output.
4671 		 */
4672 		ghead = ipst->ips_ill_g_heads[index];
4673 		insque(ill_interface, ghead.ill_g_list_tail);
4674 
4675 	}
4676 
4677 	if (ill->ill_ppa == UINT_MAX)
4678 		check_length = B_TRUE;
4679 
4680 	error = ill_alloc_ppa(ill_interface, ill);
4681 	if (error != 0) {
4682 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4683 			ill_delete_interface_type(ill->ill_ifptr);
4684 		return (error);
4685 	}
4686 
4687 	/*
4688 	 * When the ppa is choosen by the system, check that there is
4689 	 * enough space to insert ppa. if a specific ppa was passed in this
4690 	 * check is not required as the interface name passed in will have
4691 	 * the right ppa in it.
4692 	 */
4693 	if (check_length) {
4694 		/*
4695 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4696 		 */
4697 		char buf[sizeof (uint_t) * 3];
4698 
4699 		/*
4700 		 * convert ppa to string to calculate the amount of space
4701 		 * required for it in the name.
4702 		 */
4703 		numtos(ill->ill_ppa, buf);
4704 
4705 		/* Do we have enough space to insert ppa ? */
4706 
4707 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4708 			/* Free ppa and interface type struct */
4709 			if (ill_interface->illif_ppa_arena != NULL) {
4710 				vmem_free(ill_interface->illif_ppa_arena,
4711 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4712 			}
4713 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4714 			    0) {
4715 				ill_delete_interface_type(ill->ill_ifptr);
4716 			}
4717 
4718 			return (EINVAL);
4719 		}
4720 	}
4721 
4722 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4723 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4724 
4725 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4726 	    &where);
4727 	ill->ill_ifptr = ill_interface;
4728 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4729 
4730 	ill_phyint_reinit(ill);
4731 	return (0);
4732 }
4733 
4734 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4735 static boolean_t
4736 ipsq_init(ill_t *ill)
4737 {
4738 	ipsq_t  *ipsq;
4739 
4740 	/* Init the ipsq and impicitly enter as writer */
4741 	ill->ill_phyint->phyint_ipsq =
4742 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4743 	if (ill->ill_phyint->phyint_ipsq == NULL)
4744 		return (B_FALSE);
4745 	ipsq = ill->ill_phyint->phyint_ipsq;
4746 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4747 	ill->ill_phyint->phyint_ipsq_next = NULL;
4748 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4749 	ipsq->ipsq_refs = 1;
4750 	ipsq->ipsq_writer = curthread;
4751 	ipsq->ipsq_reentry_cnt = 1;
4752 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4753 #ifdef ILL_DEBUG
4754 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4755 #endif
4756 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4757 	return (B_TRUE);
4758 }
4759 
4760 /*
4761  * ill_init is called by ip_open when a device control stream is opened.
4762  * It does a few initializations, and shoots a DL_INFO_REQ message down
4763  * to the driver.  The response is later picked up in ip_rput_dlpi and
4764  * used to set up default mechanisms for talking to the driver.  (Always
4765  * called as writer.)
4766  *
4767  * If this function returns error, ip_open will call ip_close which in
4768  * turn will call ill_delete to clean up any memory allocated here that
4769  * is not yet freed.
4770  */
4771 int
4772 ill_init(queue_t *q, ill_t *ill)
4773 {
4774 	int	count;
4775 	dl_info_req_t	*dlir;
4776 	mblk_t	*info_mp;
4777 	uchar_t *frag_ptr;
4778 
4779 	/*
4780 	 * The ill is initialized to zero by mi_alloc*(). In addition
4781 	 * some fields already contain valid values, initialized in
4782 	 * ip_open(), before we reach here.
4783 	 */
4784 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4785 
4786 	ill->ill_rq = q;
4787 	ill->ill_wq = WR(q);
4788 
4789 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4790 	    BPRI_HI);
4791 	if (info_mp == NULL)
4792 		return (ENOMEM);
4793 
4794 	/*
4795 	 * Allocate sufficient space to contain our fragment hash table and
4796 	 * the device name.
4797 	 */
4798 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4799 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4800 	if (frag_ptr == NULL) {
4801 		freemsg(info_mp);
4802 		return (ENOMEM);
4803 	}
4804 	ill->ill_frag_ptr = frag_ptr;
4805 	ill->ill_frag_free_num_pkts = 0;
4806 	ill->ill_last_frag_clean_time = 0;
4807 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4808 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4809 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4810 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4811 		    NULL, MUTEX_DEFAULT, NULL);
4812 	}
4813 
4814 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4815 	if (ill->ill_phyint == NULL) {
4816 		freemsg(info_mp);
4817 		mi_free(frag_ptr);
4818 		return (ENOMEM);
4819 	}
4820 
4821 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4822 	/*
4823 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4824 	 * at this point because of the following reason. If we can't
4825 	 * enter the ipsq at some point and cv_wait, the writer that
4826 	 * wakes us up tries to locate us using the list of all phyints
4827 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4828 	 * If we don't set it now, we risk a missed wakeup.
4829 	 */
4830 	ill->ill_phyint->phyint_illv4 = ill;
4831 	ill->ill_ppa = UINT_MAX;
4832 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4833 
4834 	if (!ipsq_init(ill)) {
4835 		freemsg(info_mp);
4836 		mi_free(frag_ptr);
4837 		mi_free(ill->ill_phyint);
4838 		return (ENOMEM);
4839 	}
4840 
4841 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4842 
4843 
4844 	/* Frag queue limit stuff */
4845 	ill->ill_frag_count = 0;
4846 	ill->ill_ipf_gen = 0;
4847 
4848 	ill->ill_global_timer = INFINITY;
4849 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4850 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4851 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4852 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4853 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4854 
4855 	/*
4856 	 * Initialize IPv6 configuration variables.  The IP module is always
4857 	 * opened as an IPv4 module.  Instead tracking down the cases where
4858 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4859 	 * here for convenience, this has no effect until the ill is set to do
4860 	 * IPv6.
4861 	 */
4862 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4863 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4864 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4865 	ill->ill_max_buf = ND_MAX_Q;
4866 	ill->ill_refcnt = 0;
4867 
4868 	/* Send down the Info Request to the driver. */
4869 	info_mp->b_datap->db_type = M_PCPROTO;
4870 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4871 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4872 	dlir->dl_primitive = DL_INFO_REQ;
4873 
4874 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4875 
4876 	qprocson(q);
4877 	ill_dlpi_send(ill, info_mp);
4878 
4879 	return (0);
4880 }
4881 
4882 /*
4883  * ill_dls_info
4884  * creates datalink socket info from the device.
4885  */
4886 int
4887 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4888 {
4889 	size_t	len;
4890 	ill_t	*ill = ipif->ipif_ill;
4891 
4892 	sdl->sdl_family = AF_LINK;
4893 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4894 	sdl->sdl_type = ill->ill_type;
4895 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4896 	len = strlen(sdl->sdl_data);
4897 	ASSERT(len < 256);
4898 	sdl->sdl_nlen = (uchar_t)len;
4899 	sdl->sdl_alen = ill->ill_phys_addr_length;
4900 	sdl->sdl_slen = 0;
4901 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4902 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4903 
4904 	return (sizeof (struct sockaddr_dl));
4905 }
4906 
4907 /*
4908  * ill_xarp_info
4909  * creates xarp info from the device.
4910  */
4911 static int
4912 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4913 {
4914 	sdl->sdl_family = AF_LINK;
4915 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4916 	sdl->sdl_type = ill->ill_type;
4917 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4918 	    sizeof (sdl->sdl_data));
4919 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4920 	sdl->sdl_alen = ill->ill_phys_addr_length;
4921 	sdl->sdl_slen = 0;
4922 	return (sdl->sdl_nlen);
4923 }
4924 
4925 static int
4926 loopback_kstat_update(kstat_t *ksp, int rw)
4927 {
4928 	kstat_named_t *kn;
4929 	netstackid_t	stackid;
4930 	netstack_t	*ns;
4931 	ip_stack_t	*ipst;
4932 
4933 	if (ksp == NULL || ksp->ks_data == NULL)
4934 		return (EIO);
4935 
4936 	if (rw == KSTAT_WRITE)
4937 		return (EACCES);
4938 
4939 	kn = KSTAT_NAMED_PTR(ksp);
4940 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4941 
4942 	ns = netstack_find_by_stackid(stackid);
4943 	if (ns == NULL)
4944 		return (-1);
4945 
4946 	ipst = ns->netstack_ip;
4947 	if (ipst == NULL) {
4948 		netstack_rele(ns);
4949 		return (-1);
4950 	}
4951 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4952 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4953 	netstack_rele(ns);
4954 	return (0);
4955 }
4956 
4957 
4958 /*
4959  * Has ifindex been plumbed already.
4960  * Compares both phyint_ifindex and phyint_group_ifindex.
4961  */
4962 static boolean_t
4963 phyint_exists(uint_t index, ip_stack_t *ipst)
4964 {
4965 	phyint_t *phyi;
4966 
4967 	ASSERT(index != 0);
4968 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4969 	/*
4970 	 * Indexes are stored in the phyint - a common structure
4971 	 * to both IPv4 and IPv6.
4972 	 */
4973 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4974 	for (; phyi != NULL;
4975 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4976 	    phyi, AVL_AFTER)) {
4977 		if (phyi->phyint_ifindex == index ||
4978 		    phyi->phyint_group_ifindex == index)
4979 			return (B_TRUE);
4980 	}
4981 	return (B_FALSE);
4982 }
4983 
4984 /* Pick a unique ifindex */
4985 boolean_t
4986 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4987 {
4988 	uint_t starting_index;
4989 
4990 	if (!ipst->ips_ill_index_wrap) {
4991 		*indexp = ipst->ips_ill_index++;
4992 		if (ipst->ips_ill_index == 0) {
4993 			/* Reached the uint_t limit Next time wrap  */
4994 			ipst->ips_ill_index_wrap = B_TRUE;
4995 		}
4996 		return (B_TRUE);
4997 	}
4998 
4999 	/*
5000 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
5001 	 * at this point and don't want to call any function that attempts
5002 	 * to get the lock again.
5003 	 */
5004 	starting_index = ipst->ips_ill_index++;
5005 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
5006 		if (ipst->ips_ill_index != 0 &&
5007 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
5008 			/* found unused index - use it */
5009 			*indexp = ipst->ips_ill_index;
5010 			return (B_TRUE);
5011 		}
5012 	}
5013 
5014 	/*
5015 	 * all interface indicies are inuse.
5016 	 */
5017 	return (B_FALSE);
5018 }
5019 
5020 /*
5021  * Assign a unique interface index for the phyint.
5022  */
5023 static boolean_t
5024 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
5025 {
5026 	ASSERT(phyi->phyint_ifindex == 0);
5027 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
5028 }
5029 
5030 /*
5031  * Return a pointer to the ill which matches the supplied name.  Note that
5032  * the ill name length includes the null termination character.  (May be
5033  * called as writer.)
5034  * If do_alloc and the interface is "lo0" it will be automatically created.
5035  * Cannot bump up reference on condemned ills. So dup detect can't be done
5036  * using this func.
5037  */
5038 ill_t *
5039 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
5040     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
5041     ip_stack_t *ipst)
5042 {
5043 	ill_t	*ill;
5044 	ipif_t	*ipif;
5045 	kstat_named_t	*kn;
5046 	boolean_t isloopback;
5047 	ipsq_t *old_ipsq;
5048 	in6_addr_t ov6addr;
5049 
5050 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
5051 
5052 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5053 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
5054 	rw_exit(&ipst->ips_ill_g_lock);
5055 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
5056 		return (ill);
5057 
5058 	/*
5059 	 * Couldn't find it.  Does this happen to be a lookup for the
5060 	 * loopback device and are we allowed to allocate it?
5061 	 */
5062 	if (!isloopback || !do_alloc)
5063 		return (NULL);
5064 
5065 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
5066 
5067 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
5068 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
5069 		rw_exit(&ipst->ips_ill_g_lock);
5070 		return (ill);
5071 	}
5072 
5073 	/* Create the loopback device on demand */
5074 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
5075 	    sizeof (ipif_loopback_name), BPRI_MED));
5076 	if (ill == NULL)
5077 		goto done;
5078 
5079 	*ill = ill_null;
5080 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
5081 	ill->ill_ipst = ipst;
5082 	netstack_hold(ipst->ips_netstack);
5083 	/*
5084 	 * For exclusive stacks we set the zoneid to zero
5085 	 * to make IP operate as if in the global zone.
5086 	 */
5087 	ill->ill_zoneid = GLOBAL_ZONEID;
5088 
5089 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
5090 	if (ill->ill_phyint == NULL)
5091 		goto done;
5092 
5093 	if (isv6)
5094 		ill->ill_phyint->phyint_illv6 = ill;
5095 	else
5096 		ill->ill_phyint->phyint_illv4 = ill;
5097 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
5098 	ill->ill_max_frag = IP_LOOPBACK_MTU;
5099 	/* Add room for tcp+ip headers */
5100 	if (isv6) {
5101 		ill->ill_isv6 = B_TRUE;
5102 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
5103 	} else {
5104 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
5105 	}
5106 	if (!ill_allocate_mibs(ill))
5107 		goto done;
5108 	ill->ill_max_mtu = ill->ill_max_frag;
5109 	/*
5110 	 * ipif_loopback_name can't be pointed at directly because its used
5111 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
5112 	 * from the glist, ill_glist_delete() sets the first character of
5113 	 * ill_name to '\0'.
5114 	 */
5115 	ill->ill_name = (char *)ill + sizeof (*ill);
5116 	(void) strcpy(ill->ill_name, ipif_loopback_name);
5117 	ill->ill_name_length = sizeof (ipif_loopback_name);
5118 	/* Set ill_name_set for ill_phyint_reinit to work properly */
5119 
5120 	ill->ill_global_timer = INFINITY;
5121 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
5122 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
5123 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
5124 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
5125 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
5126 
5127 	/* No resolver here. */
5128 	ill->ill_net_type = IRE_LOOPBACK;
5129 
5130 	/* Initialize the ipsq */
5131 	if (!ipsq_init(ill))
5132 		goto done;
5133 
5134 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5135 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5136 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5137 #ifdef ILL_DEBUG
5138 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5139 #endif
5140 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5141 	if (ipif == NULL)
5142 		goto done;
5143 
5144 	ill->ill_flags = ILLF_MULTICAST;
5145 
5146 	ov6addr = ipif->ipif_v6lcl_addr;
5147 	/* Set up default loopback address and mask. */
5148 	if (!isv6) {
5149 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5150 
5151 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5152 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5153 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5154 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5155 		    ipif->ipif_v6subnet);
5156 		ill->ill_flags |= ILLF_IPV4;
5157 	} else {
5158 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5159 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5160 		ipif->ipif_v6net_mask = ipv6_all_ones;
5161 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5162 		    ipif->ipif_v6subnet);
5163 		ill->ill_flags |= ILLF_IPV6;
5164 	}
5165 
5166 	/*
5167 	 * Chain us in at the end of the ill list. hold the ill
5168 	 * before we make it globally visible. 1 for the lookup.
5169 	 */
5170 	ill->ill_refcnt = 0;
5171 	ill_refhold(ill);
5172 
5173 	ill->ill_frag_count = 0;
5174 	ill->ill_frag_free_num_pkts = 0;
5175 	ill->ill_last_frag_clean_time = 0;
5176 
5177 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5178 
5179 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5180 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5181 
5182 	/* Let SCTP know so that it can add this to its list */
5183 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5184 
5185 	/*
5186 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5187 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5188 	 * requires to be after ill_glist_insert() since we need the
5189 	 * ill_index set. Pass on ipv6_loopback as the old address.
5190 	 */
5191 	sctp_update_ipif_addr(ipif, ov6addr);
5192 
5193 	/*
5194 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5195 	 */
5196 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5197 		/* Loopback ills aren't in any IPMP group */
5198 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5199 		ipsq_delete(old_ipsq);
5200 	}
5201 
5202 	/*
5203 	 * Delay this till the ipif is allocated as ipif_allocate
5204 	 * de-references ill_phyint for getting the ifindex. We
5205 	 * can't do this before ipif_allocate because ill_phyint_reinit
5206 	 * -> phyint_assign_ifindex expects ipif to be present.
5207 	 */
5208 	mutex_enter(&ill->ill_phyint->phyint_lock);
5209 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5210 	mutex_exit(&ill->ill_phyint->phyint_lock);
5211 
5212 	if (ipst->ips_loopback_ksp == NULL) {
5213 		/* Export loopback interface statistics */
5214 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5215 		    ipif_loopback_name, "net",
5216 		    KSTAT_TYPE_NAMED, 2, 0,
5217 		    ipst->ips_netstack->netstack_stackid);
5218 		if (ipst->ips_loopback_ksp != NULL) {
5219 			ipst->ips_loopback_ksp->ks_update =
5220 			    loopback_kstat_update;
5221 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5222 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5223 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5224 			ipst->ips_loopback_ksp->ks_private =
5225 			    (void *)(uintptr_t)ipst->ips_netstack->
5226 			    netstack_stackid;
5227 			kstat_install(ipst->ips_loopback_ksp);
5228 		}
5229 	}
5230 
5231 	if (error != NULL)
5232 		*error = 0;
5233 	*did_alloc = B_TRUE;
5234 	rw_exit(&ipst->ips_ill_g_lock);
5235 	return (ill);
5236 done:
5237 	if (ill != NULL) {
5238 		if (ill->ill_phyint != NULL) {
5239 			ipsq_t	*ipsq;
5240 
5241 			ipsq = ill->ill_phyint->phyint_ipsq;
5242 			if (ipsq != NULL) {
5243 				ipsq->ipsq_ipst = NULL;
5244 				kmem_free(ipsq, sizeof (ipsq_t));
5245 			}
5246 			mi_free(ill->ill_phyint);
5247 		}
5248 		ill_free_mib(ill);
5249 		if (ill->ill_ipst != NULL)
5250 			netstack_rele(ill->ill_ipst->ips_netstack);
5251 		mi_free(ill);
5252 	}
5253 	rw_exit(&ipst->ips_ill_g_lock);
5254 	if (error != NULL)
5255 		*error = ENOMEM;
5256 	return (NULL);
5257 }
5258 
5259 /*
5260  * For IPP calls - use the ip_stack_t for global stack.
5261  */
5262 ill_t *
5263 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5264     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5265 {
5266 	ip_stack_t	*ipst;
5267 	ill_t		*ill;
5268 
5269 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5270 	if (ipst == NULL) {
5271 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5272 		return (NULL);
5273 	}
5274 
5275 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5276 	netstack_rele(ipst->ips_netstack);
5277 	return (ill);
5278 }
5279 
5280 /*
5281  * Return a pointer to the ill which matches the index and IP version type.
5282  */
5283 ill_t *
5284 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5285     ipsq_func_t func, int *err, ip_stack_t *ipst)
5286 {
5287 	ill_t	*ill;
5288 	ipsq_t  *ipsq;
5289 	phyint_t *phyi;
5290 
5291 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5292 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5293 
5294 	if (err != NULL)
5295 		*err = 0;
5296 
5297 	/*
5298 	 * Indexes are stored in the phyint - a common structure
5299 	 * to both IPv4 and IPv6.
5300 	 */
5301 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5302 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5303 	    (void *) &index, NULL);
5304 	if (phyi != NULL) {
5305 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5306 		if (ill != NULL) {
5307 			/*
5308 			 * The block comment at the start of ipif_down
5309 			 * explains the use of the macros used below
5310 			 */
5311 			GRAB_CONN_LOCK(q);
5312 			mutex_enter(&ill->ill_lock);
5313 			if (ILL_CAN_LOOKUP(ill)) {
5314 				ill_refhold_locked(ill);
5315 				mutex_exit(&ill->ill_lock);
5316 				RELEASE_CONN_LOCK(q);
5317 				rw_exit(&ipst->ips_ill_g_lock);
5318 				return (ill);
5319 			} else if (ILL_CAN_WAIT(ill, q)) {
5320 				ipsq = ill->ill_phyint->phyint_ipsq;
5321 				mutex_enter(&ipsq->ipsq_lock);
5322 				rw_exit(&ipst->ips_ill_g_lock);
5323 				mutex_exit(&ill->ill_lock);
5324 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5325 				mutex_exit(&ipsq->ipsq_lock);
5326 				RELEASE_CONN_LOCK(q);
5327 				*err = EINPROGRESS;
5328 				return (NULL);
5329 			}
5330 			RELEASE_CONN_LOCK(q);
5331 			mutex_exit(&ill->ill_lock);
5332 		}
5333 	}
5334 	rw_exit(&ipst->ips_ill_g_lock);
5335 	if (err != NULL)
5336 		*err = ENXIO;
5337 	return (NULL);
5338 }
5339 
5340 /*
5341  * Return the ifindex next in sequence after the passed in ifindex.
5342  * If there is no next ifindex for the given protocol, return 0.
5343  */
5344 uint_t
5345 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5346 {
5347 	phyint_t *phyi;
5348 	phyint_t *phyi_initial;
5349 	uint_t   ifindex;
5350 
5351 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5352 
5353 	if (index == 0) {
5354 		phyi = avl_first(
5355 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5356 	} else {
5357 		phyi = phyi_initial = avl_find(
5358 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5359 		    (void *) &index, NULL);
5360 	}
5361 
5362 	for (; phyi != NULL;
5363 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5364 	    phyi, AVL_AFTER)) {
5365 		/*
5366 		 * If we're not returning the first interface in the tree
5367 		 * and we still haven't moved past the phyint_t that
5368 		 * corresponds to index, avl_walk needs to be called again
5369 		 */
5370 		if (!((index != 0) && (phyi == phyi_initial))) {
5371 			if (isv6) {
5372 				if ((phyi->phyint_illv6) &&
5373 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5374 				    (phyi->phyint_illv6->ill_isv6 == 1))
5375 					break;
5376 			} else {
5377 				if ((phyi->phyint_illv4) &&
5378 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5379 				    (phyi->phyint_illv4->ill_isv6 == 0))
5380 					break;
5381 			}
5382 		}
5383 	}
5384 
5385 	rw_exit(&ipst->ips_ill_g_lock);
5386 
5387 	if (phyi != NULL)
5388 		ifindex = phyi->phyint_ifindex;
5389 	else
5390 		ifindex = 0;
5391 
5392 	return (ifindex);
5393 }
5394 
5395 
5396 /*
5397  * Return the ifindex for the named interface.
5398  * If there is no next ifindex for the interface, return 0.
5399  */
5400 uint_t
5401 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5402 {
5403 	phyint_t	*phyi;
5404 	avl_index_t	where = 0;
5405 	uint_t		ifindex;
5406 
5407 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5408 
5409 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5410 	    name, &where)) == NULL) {
5411 		rw_exit(&ipst->ips_ill_g_lock);
5412 		return (0);
5413 	}
5414 
5415 	ifindex = phyi->phyint_ifindex;
5416 
5417 	rw_exit(&ipst->ips_ill_g_lock);
5418 
5419 	return (ifindex);
5420 }
5421 
5422 
5423 /*
5424  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5425  * that gives a running thread a reference to the ill. This reference must be
5426  * released by the thread when it is done accessing the ill and related
5427  * objects. ill_refcnt can not be used to account for static references
5428  * such as other structures pointing to an ill. Callers must generally
5429  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5430  * or be sure that the ill is not being deleted or changing state before
5431  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5432  * ill won't change any of its critical state such as address, netmask etc.
5433  */
5434 void
5435 ill_refhold(ill_t *ill)
5436 {
5437 	mutex_enter(&ill->ill_lock);
5438 	ill->ill_refcnt++;
5439 	ILL_TRACE_REF(ill);
5440 	mutex_exit(&ill->ill_lock);
5441 }
5442 
5443 void
5444 ill_refhold_locked(ill_t *ill)
5445 {
5446 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5447 	ill->ill_refcnt++;
5448 	ILL_TRACE_REF(ill);
5449 }
5450 
5451 int
5452 ill_check_and_refhold(ill_t *ill)
5453 {
5454 	mutex_enter(&ill->ill_lock);
5455 	if (ILL_CAN_LOOKUP(ill)) {
5456 		ill_refhold_locked(ill);
5457 		mutex_exit(&ill->ill_lock);
5458 		return (0);
5459 	}
5460 	mutex_exit(&ill->ill_lock);
5461 	return (ILL_LOOKUP_FAILED);
5462 }
5463 
5464 /*
5465  * Must not be called while holding any locks. Otherwise if this is
5466  * the last reference to be released, there is a chance of recursive mutex
5467  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5468  * to restart an ioctl.
5469  */
5470 void
5471 ill_refrele(ill_t *ill)
5472 {
5473 	mutex_enter(&ill->ill_lock);
5474 	ASSERT(ill->ill_refcnt != 0);
5475 	ill->ill_refcnt--;
5476 	ILL_UNTRACE_REF(ill);
5477 	if (ill->ill_refcnt != 0) {
5478 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5479 		mutex_exit(&ill->ill_lock);
5480 		return;
5481 	}
5482 
5483 	/* Drops the ill_lock */
5484 	ipif_ill_refrele_tail(ill);
5485 }
5486 
5487 /*
5488  * Obtain a weak reference count on the ill. This reference ensures the
5489  * ill won't be freed, but the ill may change any of its critical state
5490  * such as netmask, address etc. Returns an error if the ill has started
5491  * closing.
5492  */
5493 boolean_t
5494 ill_waiter_inc(ill_t *ill)
5495 {
5496 	mutex_enter(&ill->ill_lock);
5497 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5498 		mutex_exit(&ill->ill_lock);
5499 		return (B_FALSE);
5500 	}
5501 	ill->ill_waiters++;
5502 	mutex_exit(&ill->ill_lock);
5503 	return (B_TRUE);
5504 }
5505 
5506 void
5507 ill_waiter_dcr(ill_t *ill)
5508 {
5509 	mutex_enter(&ill->ill_lock);
5510 	ill->ill_waiters--;
5511 	if (ill->ill_waiters == 0)
5512 		cv_broadcast(&ill->ill_cv);
5513 	mutex_exit(&ill->ill_lock);
5514 }
5515 
5516 /*
5517  * Named Dispatch routine to produce a formatted report on all ILLs.
5518  * This report is accessed by using the ndd utility to "get" ND variable
5519  * "ip_ill_status".
5520  */
5521 /* ARGSUSED */
5522 int
5523 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5524 {
5525 	ill_t		*ill;
5526 	ill_walk_context_t ctx;
5527 	ip_stack_t	*ipst;
5528 
5529 	ipst = CONNQ_TO_IPST(q);
5530 
5531 	(void) mi_mpprintf(mp,
5532 	    "ILL      " MI_COL_HDRPAD_STR
5533 	/*   01234567[89ABCDEF] */
5534 	    "rq       " MI_COL_HDRPAD_STR
5535 	/*   01234567[89ABCDEF] */
5536 	    "wq       " MI_COL_HDRPAD_STR
5537 	/*   01234567[89ABCDEF] */
5538 	    "upcnt mxfrg err name");
5539 	/*   12345 12345 123 xxxxxxxx  */
5540 
5541 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5542 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5543 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5544 		(void) mi_mpprintf(mp,
5545 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5546 		    "%05u %05u %03d %s",
5547 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5548 		    ill->ill_ipif_up_count,
5549 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5550 	}
5551 	rw_exit(&ipst->ips_ill_g_lock);
5552 
5553 	return (0);
5554 }
5555 
5556 /*
5557  * Named Dispatch routine to produce a formatted report on all IPIFs.
5558  * This report is accessed by using the ndd utility to "get" ND variable
5559  * "ip_ipif_status".
5560  */
5561 /* ARGSUSED */
5562 int
5563 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5564 {
5565 	char	buf1[INET6_ADDRSTRLEN];
5566 	char	buf2[INET6_ADDRSTRLEN];
5567 	char	buf3[INET6_ADDRSTRLEN];
5568 	char	buf4[INET6_ADDRSTRLEN];
5569 	char	buf5[INET6_ADDRSTRLEN];
5570 	char	buf6[INET6_ADDRSTRLEN];
5571 	char	buf[LIFNAMSIZ];
5572 	ill_t	*ill;
5573 	ipif_t	*ipif;
5574 	nv_t	*nvp;
5575 	uint64_t flags;
5576 	zoneid_t zoneid;
5577 	ill_walk_context_t ctx;
5578 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5579 
5580 	(void) mi_mpprintf(mp,
5581 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5582 	    "\tlocal address\n"
5583 	    "\tsrc address\n"
5584 	    "\tsubnet\n"
5585 	    "\tmask\n"
5586 	    "\tbroadcast\n"
5587 	    "\tp-p-dst");
5588 
5589 	ASSERT(q->q_next == NULL);
5590 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5591 
5592 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5593 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5594 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5595 		for (ipif = ill->ill_ipif; ipif != NULL;
5596 		    ipif = ipif->ipif_next) {
5597 			if (zoneid != GLOBAL_ZONEID &&
5598 			    zoneid != ipif->ipif_zoneid &&
5599 			    ipif->ipif_zoneid != ALL_ZONES)
5600 				continue;
5601 			(void) mi_mpprintf(mp,
5602 			    MI_COL_PTRFMT_STR
5603 			    "%04u %05u %u/%u/%u %s %d",
5604 			    (void *)ipif,
5605 			    ipif->ipif_metric, ipif->ipif_mtu,
5606 			    ipif->ipif_ib_pkt_count,
5607 			    ipif->ipif_ob_pkt_count,
5608 			    ipif->ipif_fo_pkt_count,
5609 			    ipif_get_name(ipif, buf, sizeof (buf)),
5610 			    ipif->ipif_zoneid);
5611 
5612 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5613 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5614 
5615 		/* Tack on text strings for any flags. */
5616 		nvp = ipif_nv_tbl;
5617 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5618 			if (nvp->nv_value & flags)
5619 				(void) mi_mpprintf_nr(mp, " %s",
5620 				    nvp->nv_name);
5621 		}
5622 		(void) mi_mpprintf(mp,
5623 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5624 		    inet_ntop(AF_INET6,
5625 			&ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5626 		    inet_ntop(AF_INET6,
5627 			&ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5628 		    inet_ntop(AF_INET6,
5629 			&ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5630 		    inet_ntop(AF_INET6,
5631 			&ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5632 		    inet_ntop(AF_INET6,
5633 			&ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5634 		    inet_ntop(AF_INET6,
5635 			&ipif->ipif_v6pp_dst_addr,
5636 			buf6, sizeof (buf6)));
5637 		}
5638 	}
5639 	rw_exit(&ipst->ips_ill_g_lock);
5640 	return (0);
5641 }
5642 
5643 /*
5644  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5645  * driver.  We construct best guess defaults for lower level information that
5646  * we need.  If an interface is brought up without injection of any overriding
5647  * information from outside, we have to be ready to go with these defaults.
5648  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5649  * we primarely want the dl_provider_style.
5650  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5651  * at which point we assume the other part of the information is valid.
5652  */
5653 void
5654 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5655 {
5656 	uchar_t		*brdcst_addr;
5657 	uint_t		brdcst_addr_length, phys_addr_length;
5658 	t_scalar_t	sap_length;
5659 	dl_info_ack_t	*dlia;
5660 	ip_m_t		*ipm;
5661 	dl_qos_cl_sel1_t *sel1;
5662 
5663 	ASSERT(IAM_WRITER_ILL(ill));
5664 
5665 	/*
5666 	 * Till the ill is fully up ILL_CHANGING will be set and
5667 	 * the ill is not globally visible. So no need for a lock.
5668 	 */
5669 	dlia = (dl_info_ack_t *)mp->b_rptr;
5670 	ill->ill_mactype = dlia->dl_mac_type;
5671 
5672 	ipm = ip_m_lookup(dlia->dl_mac_type);
5673 	if (ipm == NULL) {
5674 		ipm = ip_m_lookup(DL_OTHER);
5675 		ASSERT(ipm != NULL);
5676 	}
5677 	ill->ill_media = ipm;
5678 
5679 	/*
5680 	 * When the new DLPI stuff is ready we'll pull lengths
5681 	 * from dlia.
5682 	 */
5683 	if (dlia->dl_version == DL_VERSION_2) {
5684 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5685 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5686 		    brdcst_addr_length);
5687 		if (brdcst_addr == NULL) {
5688 			brdcst_addr_length = 0;
5689 		}
5690 		sap_length = dlia->dl_sap_length;
5691 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5692 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5693 		    brdcst_addr_length, sap_length, phys_addr_length));
5694 	} else {
5695 		brdcst_addr_length = 6;
5696 		brdcst_addr = ip_six_byte_all_ones;
5697 		sap_length = -2;
5698 		phys_addr_length = brdcst_addr_length;
5699 	}
5700 
5701 	ill->ill_bcast_addr_length = brdcst_addr_length;
5702 	ill->ill_phys_addr_length = phys_addr_length;
5703 	ill->ill_sap_length = sap_length;
5704 	ill->ill_max_frag = dlia->dl_max_sdu;
5705 	ill->ill_max_mtu = ill->ill_max_frag;
5706 
5707 	ill->ill_type = ipm->ip_m_type;
5708 
5709 	if (!ill->ill_dlpi_style_set) {
5710 		if (dlia->dl_provider_style == DL_STYLE2)
5711 			ill->ill_needs_attach = 1;
5712 
5713 		/*
5714 		 * Allocate the first ipif on this ill. We don't delay it
5715 		 * further as ioctl handling assumes atleast one ipif to
5716 		 * be present.
5717 		 *
5718 		 * At this point we don't know whether the ill is v4 or v6.
5719 		 * We will know this whan the SIOCSLIFNAME happens and
5720 		 * the correct value for ill_isv6 will be assigned in
5721 		 * ipif_set_values(). We need to hold the ill lock and
5722 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5723 		 * the wakeup.
5724 		 */
5725 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5726 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5727 		mutex_enter(&ill->ill_lock);
5728 		ASSERT(ill->ill_dlpi_style_set == 0);
5729 		ill->ill_dlpi_style_set = 1;
5730 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5731 		cv_broadcast(&ill->ill_cv);
5732 		mutex_exit(&ill->ill_lock);
5733 		freemsg(mp);
5734 		return;
5735 	}
5736 	ASSERT(ill->ill_ipif != NULL);
5737 	/*
5738 	 * We know whether it is IPv4 or IPv6 now, as this is the
5739 	 * second DL_INFO_ACK we are recieving in response to the
5740 	 * DL_INFO_REQ sent in ipif_set_values.
5741 	 */
5742 	if (ill->ill_isv6)
5743 		ill->ill_sap = IP6_DL_SAP;
5744 	else
5745 		ill->ill_sap = IP_DL_SAP;
5746 	/*
5747 	 * Set ipif_mtu which is used to set the IRE's
5748 	 * ire_max_frag value. The driver could have sent
5749 	 * a different mtu from what it sent last time. No
5750 	 * need to call ipif_mtu_change because IREs have
5751 	 * not yet been created.
5752 	 */
5753 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5754 	/*
5755 	 * Clear all the flags that were set based on ill_bcast_addr_length
5756 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5757 	 * changed now and we need to re-evaluate.
5758 	 */
5759 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5760 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5761 
5762 	/*
5763 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5764 	 * changed now.
5765 	 */
5766 	if (ill->ill_bcast_addr_length == 0) {
5767 		if (ill->ill_resolver_mp != NULL)
5768 			freemsg(ill->ill_resolver_mp);
5769 		if (ill->ill_bcast_mp != NULL)
5770 			freemsg(ill->ill_bcast_mp);
5771 		if (ill->ill_flags & ILLF_XRESOLV)
5772 			ill->ill_net_type = IRE_IF_RESOLVER;
5773 		else
5774 			ill->ill_net_type = IRE_IF_NORESOLVER;
5775 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5776 		    ill->ill_phys_addr_length,
5777 		    ill->ill_sap,
5778 		    ill->ill_sap_length);
5779 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5780 
5781 		if (ill->ill_isv6)
5782 			/*
5783 			 * Note: xresolv interfaces will eventually need NOARP
5784 			 * set here as well, but that will require those
5785 			 * external resolvers to have some knowledge of
5786 			 * that flag and act appropriately. Not to be changed
5787 			 * at present.
5788 			 */
5789 			ill->ill_flags |= ILLF_NONUD;
5790 		else
5791 			ill->ill_flags |= ILLF_NOARP;
5792 
5793 		if (ill->ill_phys_addr_length == 0) {
5794 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5795 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5796 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5797 			} else {
5798 				/* pt-pt supports multicast. */
5799 				ill->ill_flags |= ILLF_MULTICAST;
5800 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5801 			}
5802 		}
5803 	} else {
5804 		ill->ill_net_type = IRE_IF_RESOLVER;
5805 		if (ill->ill_bcast_mp != NULL)
5806 			freemsg(ill->ill_bcast_mp);
5807 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5808 		    ill->ill_bcast_addr_length, ill->ill_sap,
5809 		    ill->ill_sap_length);
5810 		/*
5811 		 * Later detect lack of DLPI driver multicast
5812 		 * capability by catching DL_ENABMULTI errors in
5813 		 * ip_rput_dlpi.
5814 		 */
5815 		ill->ill_flags |= ILLF_MULTICAST;
5816 		if (!ill->ill_isv6)
5817 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5818 	}
5819 	/* By default an interface does not support any CoS marking */
5820 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5821 
5822 	/*
5823 	 * If we get QoS information in DL_INFO_ACK, the device supports
5824 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5825 	 */
5826 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5827 	    dlia->dl_qos_length);
5828 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5829 		ill->ill_flags |= ILLF_COS_ENABLED;
5830 	}
5831 
5832 	/* Clear any previous error indication. */
5833 	ill->ill_error = 0;
5834 	freemsg(mp);
5835 }
5836 
5837 /*
5838  * Perform various checks to verify that an address would make sense as a
5839  * local, remote, or subnet interface address.
5840  */
5841 static boolean_t
5842 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5843 {
5844 	ipaddr_t	net_mask;
5845 
5846 	/*
5847 	 * Don't allow all zeroes, all ones or experimental address, but allow
5848 	 * all ones netmask.
5849 	 */
5850 	if ((net_mask = ip_net_mask(addr)) == 0)
5851 		return (B_FALSE);
5852 	/* A given netmask overrides the "guess" netmask */
5853 	if (subnet_mask != 0)
5854 		net_mask = subnet_mask;
5855 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5856 	    (addr == (addr | ~net_mask)))) {
5857 		return (B_FALSE);
5858 	}
5859 	if (CLASSD(addr))
5860 		return (B_FALSE);
5861 
5862 	return (B_TRUE);
5863 }
5864 
5865 /*
5866  * ipif_lookup_group
5867  * Returns held ipif
5868  */
5869 ipif_t *
5870 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5871 {
5872 	ire_t	*ire;
5873 	ipif_t	*ipif;
5874 
5875 	ire = ire_lookup_multi(group, zoneid, ipst);
5876 	if (ire == NULL)
5877 		return (NULL);
5878 	ipif = ire->ire_ipif;
5879 	ipif_refhold(ipif);
5880 	ire_refrele(ire);
5881 	return (ipif);
5882 }
5883 
5884 /*
5885  * Look for an ipif with the specified interface address and destination.
5886  * The destination address is used only for matching point-to-point interfaces.
5887  */
5888 ipif_t *
5889 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5890     ipsq_func_t func, int *error, ip_stack_t *ipst)
5891 {
5892 	ipif_t	*ipif;
5893 	ill_t	*ill;
5894 	ill_walk_context_t ctx;
5895 	ipsq_t	*ipsq;
5896 
5897 	if (error != NULL)
5898 		*error = 0;
5899 
5900 	/*
5901 	 * First match all the point-to-point interfaces
5902 	 * before looking at non-point-to-point interfaces.
5903 	 * This is done to avoid returning non-point-to-point
5904 	 * ipif instead of unnumbered point-to-point ipif.
5905 	 */
5906 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5907 	ill = ILL_START_WALK_V4(&ctx, ipst);
5908 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5909 		GRAB_CONN_LOCK(q);
5910 		mutex_enter(&ill->ill_lock);
5911 		for (ipif = ill->ill_ipif; ipif != NULL;
5912 		    ipif = ipif->ipif_next) {
5913 			/* Allow the ipif to be down */
5914 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5915 			    (ipif->ipif_lcl_addr == if_addr) &&
5916 			    (ipif->ipif_pp_dst_addr == dst)) {
5917 				/*
5918 				 * The block comment at the start of ipif_down
5919 				 * explains the use of the macros used below
5920 				 */
5921 				if (IPIF_CAN_LOOKUP(ipif)) {
5922 					ipif_refhold_locked(ipif);
5923 					mutex_exit(&ill->ill_lock);
5924 					RELEASE_CONN_LOCK(q);
5925 					rw_exit(&ipst->ips_ill_g_lock);
5926 					return (ipif);
5927 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5928 					ipsq = ill->ill_phyint->phyint_ipsq;
5929 					mutex_enter(&ipsq->ipsq_lock);
5930 					mutex_exit(&ill->ill_lock);
5931 					rw_exit(&ipst->ips_ill_g_lock);
5932 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5933 						ill);
5934 					mutex_exit(&ipsq->ipsq_lock);
5935 					RELEASE_CONN_LOCK(q);
5936 					*error = EINPROGRESS;
5937 					return (NULL);
5938 				}
5939 			}
5940 		}
5941 		mutex_exit(&ill->ill_lock);
5942 		RELEASE_CONN_LOCK(q);
5943 	}
5944 	rw_exit(&ipst->ips_ill_g_lock);
5945 
5946 	/* lookup the ipif based on interface address */
5947 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5948 	    ipst);
5949 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5950 	return (ipif);
5951 }
5952 
5953 /*
5954  * Look for an ipif with the specified address. For point-point links
5955  * we look for matches on either the destination address and the local
5956  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5957  * is set.
5958  * Matches on a specific ill if match_ill is set.
5959  */
5960 ipif_t *
5961 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5962     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5963 {
5964 	ipif_t  *ipif;
5965 	ill_t   *ill;
5966 	boolean_t ptp = B_FALSE;
5967 	ipsq_t	*ipsq;
5968 	ill_walk_context_t	ctx;
5969 
5970 	if (error != NULL)
5971 		*error = 0;
5972 
5973 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5974 	/*
5975 	 * Repeat twice, first based on local addresses and
5976 	 * next time for pointopoint.
5977 	 */
5978 repeat:
5979 	ill = ILL_START_WALK_V4(&ctx, ipst);
5980 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5981 		if (match_ill != NULL && ill != match_ill) {
5982 			continue;
5983 		}
5984 		GRAB_CONN_LOCK(q);
5985 		mutex_enter(&ill->ill_lock);
5986 		for (ipif = ill->ill_ipif; ipif != NULL;
5987 		    ipif = ipif->ipif_next) {
5988 			if (zoneid != ALL_ZONES &&
5989 			    zoneid != ipif->ipif_zoneid &&
5990 			    ipif->ipif_zoneid != ALL_ZONES)
5991 				continue;
5992 			/* Allow the ipif to be down */
5993 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5994 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5995 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5996 			    (ipif->ipif_pp_dst_addr == addr))) {
5997 				/*
5998 				 * The block comment at the start of ipif_down
5999 				 * explains the use of the macros used below
6000 				 */
6001 				if (IPIF_CAN_LOOKUP(ipif)) {
6002 					ipif_refhold_locked(ipif);
6003 					mutex_exit(&ill->ill_lock);
6004 					RELEASE_CONN_LOCK(q);
6005 					rw_exit(&ipst->ips_ill_g_lock);
6006 					return (ipif);
6007 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6008 					ipsq = ill->ill_phyint->phyint_ipsq;
6009 					mutex_enter(&ipsq->ipsq_lock);
6010 					mutex_exit(&ill->ill_lock);
6011 					rw_exit(&ipst->ips_ill_g_lock);
6012 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6013 						ill);
6014 					mutex_exit(&ipsq->ipsq_lock);
6015 					RELEASE_CONN_LOCK(q);
6016 					*error = EINPROGRESS;
6017 					return (NULL);
6018 				}
6019 			}
6020 		}
6021 		mutex_exit(&ill->ill_lock);
6022 		RELEASE_CONN_LOCK(q);
6023 	}
6024 
6025 	/* If we already did the ptp case, then we are done */
6026 	if (ptp) {
6027 		rw_exit(&ipst->ips_ill_g_lock);
6028 		if (error != NULL)
6029 			*error = ENXIO;
6030 		return (NULL);
6031 	}
6032 	ptp = B_TRUE;
6033 	goto repeat;
6034 }
6035 
6036 /*
6037  * Look for an ipif with the specified address. For point-point links
6038  * we look for matches on either the destination address and the local
6039  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6040  * is set.
6041  * Matches on a specific ill if match_ill is set.
6042  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6043  */
6044 zoneid_t
6045 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6046 {
6047 	zoneid_t zoneid;
6048 	ipif_t  *ipif;
6049 	ill_t   *ill;
6050 	boolean_t ptp = B_FALSE;
6051 	ill_walk_context_t	ctx;
6052 
6053 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6054 	/*
6055 	 * Repeat twice, first based on local addresses and
6056 	 * next time for pointopoint.
6057 	 */
6058 repeat:
6059 	ill = ILL_START_WALK_V4(&ctx, ipst);
6060 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6061 		if (match_ill != NULL && ill != match_ill) {
6062 			continue;
6063 		}
6064 		mutex_enter(&ill->ill_lock);
6065 		for (ipif = ill->ill_ipif; ipif != NULL;
6066 		    ipif = ipif->ipif_next) {
6067 			/* Allow the ipif to be down */
6068 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6069 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6070 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6071 			    (ipif->ipif_pp_dst_addr == addr)) &&
6072 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6073 				zoneid = ipif->ipif_zoneid;
6074 				mutex_exit(&ill->ill_lock);
6075 				rw_exit(&ipst->ips_ill_g_lock);
6076 				/*
6077 				 * If ipif_zoneid was ALL_ZONES then we have
6078 				 * a trusted extensions shared IP address.
6079 				 * In that case GLOBAL_ZONEID works to send.
6080 				 */
6081 				if (zoneid == ALL_ZONES)
6082 					zoneid = GLOBAL_ZONEID;
6083 				return (zoneid);
6084 			}
6085 		}
6086 		mutex_exit(&ill->ill_lock);
6087 	}
6088 
6089 	/* If we already did the ptp case, then we are done */
6090 	if (ptp) {
6091 		rw_exit(&ipst->ips_ill_g_lock);
6092 		return (ALL_ZONES);
6093 	}
6094 	ptp = B_TRUE;
6095 	goto repeat;
6096 }
6097 
6098 /*
6099  * Look for an ipif that matches the specified remote address i.e. the
6100  * ipif that would receive the specified packet.
6101  * First look for directly connected interfaces and then do a recursive
6102  * IRE lookup and pick the first ipif corresponding to the source address in the
6103  * ire.
6104  * Returns: held ipif
6105  */
6106 ipif_t *
6107 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6108 {
6109 	ipif_t	*ipif;
6110 	ire_t	*ire;
6111 	ip_stack_t	*ipst = ill->ill_ipst;
6112 
6113 	ASSERT(!ill->ill_isv6);
6114 
6115 	/*
6116 	 * Someone could be changing this ipif currently or change it
6117 	 * after we return this. Thus  a few packets could use the old
6118 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6119 	 * will atomically be updated or cleaned up with the new value
6120 	 * Thus we don't need a lock to check the flags or other attrs below.
6121 	 */
6122 	mutex_enter(&ill->ill_lock);
6123 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6124 		if (!IPIF_CAN_LOOKUP(ipif))
6125 			continue;
6126 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6127 		    ipif->ipif_zoneid != ALL_ZONES)
6128 			continue;
6129 		/* Allow the ipif to be down */
6130 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6131 			if ((ipif->ipif_pp_dst_addr == addr) ||
6132 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6133 			    ipif->ipif_lcl_addr == addr)) {
6134 				ipif_refhold_locked(ipif);
6135 				mutex_exit(&ill->ill_lock);
6136 				return (ipif);
6137 			}
6138 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6139 			ipif_refhold_locked(ipif);
6140 			mutex_exit(&ill->ill_lock);
6141 			return (ipif);
6142 		}
6143 	}
6144 	mutex_exit(&ill->ill_lock);
6145 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6146 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6147 	if (ire != NULL) {
6148 		/*
6149 		 * The callers of this function wants to know the
6150 		 * interface on which they have to send the replies
6151 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6152 		 * derived from different ills, we really don't care
6153 		 * what we return here.
6154 		 */
6155 		ipif = ire->ire_ipif;
6156 		if (ipif != NULL) {
6157 			ipif_refhold(ipif);
6158 			ire_refrele(ire);
6159 			return (ipif);
6160 		}
6161 		ire_refrele(ire);
6162 	}
6163 	/* Pick the first interface */
6164 	ipif = ipif_get_next_ipif(NULL, ill);
6165 	return (ipif);
6166 }
6167 
6168 /*
6169  * This func does not prevent refcnt from increasing. But if
6170  * the caller has taken steps to that effect, then this func
6171  * can be used to determine whether the ill has become quiescent
6172  */
6173 boolean_t
6174 ill_is_quiescent(ill_t *ill)
6175 {
6176 	ipif_t	*ipif;
6177 
6178 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6179 
6180 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6181 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6182 			return (B_FALSE);
6183 		}
6184 	}
6185 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6186 	    ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 ||
6187 	    ill->ill_mrtun_refcnt != 0) {
6188 		return (B_FALSE);
6189 	}
6190 	return (B_TRUE);
6191 }
6192 
6193 /*
6194  * This func does not prevent refcnt from increasing. But if
6195  * the caller has taken steps to that effect, then this func
6196  * can be used to determine whether the ipif has become quiescent
6197  */
6198 static boolean_t
6199 ipif_is_quiescent(ipif_t *ipif)
6200 {
6201 	ill_t *ill;
6202 
6203 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6204 
6205 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6206 		return (B_FALSE);
6207 	}
6208 
6209 	ill = ipif->ipif_ill;
6210 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6211 	    ill->ill_logical_down) {
6212 		return (B_TRUE);
6213 	}
6214 
6215 	/* This is the last ipif going down or being deleted on this ill */
6216 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6217 		return (B_FALSE);
6218 	}
6219 
6220 	return (B_TRUE);
6221 }
6222 
6223 /*
6224  * This func does not prevent refcnt from increasing. But if
6225  * the caller has taken steps to that effect, then this func
6226  * can be used to determine whether the ipifs marked with IPIF_MOVING
6227  * have become quiescent and can be moved in a failover/failback.
6228  */
6229 static ipif_t *
6230 ill_quiescent_to_move(ill_t *ill)
6231 {
6232 	ipif_t  *ipif;
6233 
6234 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6235 
6236 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6237 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6238 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6239 				return (ipif);
6240 			}
6241 		}
6242 	}
6243 	return (NULL);
6244 }
6245 
6246 /*
6247  * The ipif/ill/ire has been refreled. Do the tail processing.
6248  * Determine if the ipif or ill in question has become quiescent and if so
6249  * wakeup close and/or restart any queued pending ioctl that is waiting
6250  * for the ipif_down (or ill_down)
6251  */
6252 void
6253 ipif_ill_refrele_tail(ill_t *ill)
6254 {
6255 	mblk_t	*mp;
6256 	conn_t	*connp;
6257 	ipsq_t	*ipsq;
6258 	ipif_t	*ipif;
6259 	dl_notify_ind_t *dlindp;
6260 
6261 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6262 
6263 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6264 	    ill_is_quiescent(ill)) {
6265 		/* ill_close may be waiting */
6266 		cv_broadcast(&ill->ill_cv);
6267 	}
6268 
6269 	/* ipsq can't change because ill_lock  is held */
6270 	ipsq = ill->ill_phyint->phyint_ipsq;
6271 	if (ipsq->ipsq_waitfor == 0) {
6272 		/* Not waiting for anything, just return. */
6273 		mutex_exit(&ill->ill_lock);
6274 		return;
6275 	}
6276 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6277 		ipsq->ipsq_pending_ipif != NULL);
6278 	/*
6279 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6280 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6281 	 * be zero for restarting an ioctl that ends up downing the ill.
6282 	 */
6283 	ipif = ipsq->ipsq_pending_ipif;
6284 	if (ipif->ipif_ill != ill) {
6285 		/* The ioctl is pending on some other ill. */
6286 		mutex_exit(&ill->ill_lock);
6287 		return;
6288 	}
6289 
6290 	switch (ipsq->ipsq_waitfor) {
6291 	case IPIF_DOWN:
6292 	case IPIF_FREE:
6293 		if (!ipif_is_quiescent(ipif)) {
6294 			mutex_exit(&ill->ill_lock);
6295 			return;
6296 		}
6297 		break;
6298 
6299 	case ILL_DOWN:
6300 	case ILL_FREE:
6301 		/*
6302 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6303 		 * waits synchronously in ip_close, and no message is queued in
6304 		 * ipsq_pending_mp at all in this case
6305 		 */
6306 		if (!ill_is_quiescent(ill)) {
6307 			mutex_exit(&ill->ill_lock);
6308 			return;
6309 		}
6310 
6311 		break;
6312 
6313 	case ILL_MOVE_OK:
6314 		if (ill_quiescent_to_move(ill) != NULL) {
6315 			mutex_exit(&ill->ill_lock);
6316 			return;
6317 		}
6318 
6319 		break;
6320 	default:
6321 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6322 		    (void *)ipsq, ipsq->ipsq_waitfor);
6323 	}
6324 
6325 	/*
6326 	 * Incr refcnt for the qwriter_ip call below which
6327 	 * does a refrele
6328 	 */
6329 	ill_refhold_locked(ill);
6330 	mutex_exit(&ill->ill_lock);
6331 
6332 	mp = ipsq_pending_mp_get(ipsq, &connp);
6333 	ASSERT(mp != NULL);
6334 
6335 	switch (mp->b_datap->db_type) {
6336 	case M_PCPROTO:
6337 	case M_PROTO:
6338 		/*
6339 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6340 		 */
6341 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6342 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6343 
6344 		switch (dlindp->dl_notification) {
6345 		case DL_NOTE_PHYS_ADDR:
6346 			qwriter_ip(NULL, ill, ill->ill_rq, mp,
6347 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6348 			return;
6349 		default:
6350 			ASSERT(0);
6351 		}
6352 		break;
6353 
6354 	case M_ERROR:
6355 	case M_HANGUP:
6356 		qwriter_ip(NULL, ill, ill->ill_rq, mp, ipif_all_down_tail,
6357 		    CUR_OP, B_TRUE);
6358 		return;
6359 
6360 	case M_IOCTL:
6361 	case M_IOCDATA:
6362 		qwriter_ip(NULL, ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6363 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6364 		return;
6365 
6366 	default:
6367 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6368 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6369 	}
6370 }
6371 
6372 #ifdef ILL_DEBUG
6373 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6374 void
6375 th_trace_rrecord(th_trace_t *th_trace)
6376 {
6377 	tr_buf_t *tr_buf;
6378 	uint_t lastref;
6379 
6380 	lastref = th_trace->th_trace_lastref;
6381 	lastref++;
6382 	if (lastref == TR_BUF_MAX)
6383 		lastref = 0;
6384 	th_trace->th_trace_lastref = lastref;
6385 	tr_buf = &th_trace->th_trbuf[lastref];
6386 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
6387 }
6388 
6389 th_trace_t *
6390 th_trace_ipif_lookup(ipif_t *ipif)
6391 {
6392 	int bucket_id;
6393 	th_trace_t *th_trace;
6394 
6395 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6396 
6397 	bucket_id = IP_TR_HASH(curthread);
6398 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6399 
6400 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
6401 	    th_trace = th_trace->th_next) {
6402 		if (th_trace->th_id == curthread)
6403 			return (th_trace);
6404 	}
6405 	return (NULL);
6406 }
6407 
6408 void
6409 ipif_trace_ref(ipif_t *ipif)
6410 {
6411 	int bucket_id;
6412 	th_trace_t *th_trace;
6413 
6414 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6415 
6416 	if (ipif->ipif_trace_disable)
6417 		return;
6418 
6419 	/*
6420 	 * Attempt to locate the trace buffer for the curthread.
6421 	 * If it does not exist, then allocate a new trace buffer
6422 	 * and link it in list of trace bufs for this ipif, at the head
6423 	 */
6424 	th_trace = th_trace_ipif_lookup(ipif);
6425 	if (th_trace == NULL) {
6426 		bucket_id = IP_TR_HASH(curthread);
6427 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6428 		    KM_NOSLEEP);
6429 		if (th_trace == NULL) {
6430 			ipif->ipif_trace_disable = B_TRUE;
6431 			ipif_trace_cleanup(ipif);
6432 			return;
6433 		}
6434 		th_trace->th_id = curthread;
6435 		th_trace->th_next = ipif->ipif_trace[bucket_id];
6436 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
6437 		if (th_trace->th_next != NULL)
6438 			th_trace->th_next->th_prev = &th_trace->th_next;
6439 		ipif->ipif_trace[bucket_id] = th_trace;
6440 	}
6441 	ASSERT(th_trace->th_refcnt >= 0 &&
6442 		th_trace->th_refcnt < TR_BUF_MAX -1);
6443 	th_trace->th_refcnt++;
6444 	th_trace_rrecord(th_trace);
6445 }
6446 
6447 void
6448 ipif_untrace_ref(ipif_t *ipif)
6449 {
6450 	th_trace_t *th_trace;
6451 
6452 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6453 
6454 	if (ipif->ipif_trace_disable)
6455 		return;
6456 	th_trace = th_trace_ipif_lookup(ipif);
6457 	ASSERT(th_trace != NULL);
6458 	ASSERT(th_trace->th_refcnt > 0);
6459 
6460 	th_trace->th_refcnt--;
6461 	th_trace_rrecord(th_trace);
6462 }
6463 
6464 th_trace_t *
6465 th_trace_ill_lookup(ill_t *ill)
6466 {
6467 	th_trace_t *th_trace;
6468 	int bucket_id;
6469 
6470 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6471 
6472 	bucket_id = IP_TR_HASH(curthread);
6473 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6474 
6475 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
6476 	    th_trace = th_trace->th_next) {
6477 		if (th_trace->th_id == curthread)
6478 			return (th_trace);
6479 	}
6480 	return (NULL);
6481 }
6482 
6483 void
6484 ill_trace_ref(ill_t *ill)
6485 {
6486 	int bucket_id;
6487 	th_trace_t *th_trace;
6488 
6489 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6490 	if (ill->ill_trace_disable)
6491 		return;
6492 	/*
6493 	 * Attempt to locate the trace buffer for the curthread.
6494 	 * If it does not exist, then allocate a new trace buffer
6495 	 * and link it in list of trace bufs for this ill, at the head
6496 	 */
6497 	th_trace = th_trace_ill_lookup(ill);
6498 	if (th_trace == NULL) {
6499 		bucket_id = IP_TR_HASH(curthread);
6500 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6501 		    KM_NOSLEEP);
6502 		if (th_trace == NULL) {
6503 			ill->ill_trace_disable = B_TRUE;
6504 			ill_trace_cleanup(ill);
6505 			return;
6506 		}
6507 		th_trace->th_id = curthread;
6508 		th_trace->th_next = ill->ill_trace[bucket_id];
6509 		th_trace->th_prev = &ill->ill_trace[bucket_id];
6510 		if (th_trace->th_next != NULL)
6511 			th_trace->th_next->th_prev = &th_trace->th_next;
6512 		ill->ill_trace[bucket_id] = th_trace;
6513 	}
6514 	ASSERT(th_trace->th_refcnt >= 0 &&
6515 		th_trace->th_refcnt < TR_BUF_MAX - 1);
6516 
6517 	th_trace->th_refcnt++;
6518 	th_trace_rrecord(th_trace);
6519 }
6520 
6521 void
6522 ill_untrace_ref(ill_t *ill)
6523 {
6524 	th_trace_t *th_trace;
6525 
6526 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6527 
6528 	if (ill->ill_trace_disable)
6529 		return;
6530 	th_trace = th_trace_ill_lookup(ill);
6531 	ASSERT(th_trace != NULL);
6532 	ASSERT(th_trace->th_refcnt > 0);
6533 
6534 	th_trace->th_refcnt--;
6535 	th_trace_rrecord(th_trace);
6536 }
6537 
6538 /*
6539  * Verify that this thread has no refs to the ipif and free
6540  * the trace buffers
6541  */
6542 /* ARGSUSED */
6543 void
6544 ipif_thread_exit(ipif_t *ipif, void *dummy)
6545 {
6546 	th_trace_t *th_trace;
6547 
6548 	mutex_enter(&ipif->ipif_ill->ill_lock);
6549 
6550 	th_trace = th_trace_ipif_lookup(ipif);
6551 	if (th_trace == NULL) {
6552 		mutex_exit(&ipif->ipif_ill->ill_lock);
6553 		return;
6554 	}
6555 	ASSERT(th_trace->th_refcnt == 0);
6556 	/* unlink th_trace and free it */
6557 	*th_trace->th_prev = th_trace->th_next;
6558 	if (th_trace->th_next != NULL)
6559 		th_trace->th_next->th_prev = th_trace->th_prev;
6560 	th_trace->th_next = NULL;
6561 	th_trace->th_prev = NULL;
6562 	kmem_free(th_trace, sizeof (th_trace_t));
6563 
6564 	mutex_exit(&ipif->ipif_ill->ill_lock);
6565 }
6566 
6567 /*
6568  * Verify that this thread has no refs to the ill and free
6569  * the trace buffers
6570  */
6571 /* ARGSUSED */
6572 void
6573 ill_thread_exit(ill_t *ill, void *dummy)
6574 {
6575 	th_trace_t *th_trace;
6576 
6577 	mutex_enter(&ill->ill_lock);
6578 
6579 	th_trace = th_trace_ill_lookup(ill);
6580 	if (th_trace == NULL) {
6581 		mutex_exit(&ill->ill_lock);
6582 		return;
6583 	}
6584 	ASSERT(th_trace->th_refcnt == 0);
6585 	/* unlink th_trace and free it */
6586 	*th_trace->th_prev = th_trace->th_next;
6587 	if (th_trace->th_next != NULL)
6588 		th_trace->th_next->th_prev = th_trace->th_prev;
6589 	th_trace->th_next = NULL;
6590 	th_trace->th_prev = NULL;
6591 	kmem_free(th_trace, sizeof (th_trace_t));
6592 
6593 	mutex_exit(&ill->ill_lock);
6594 }
6595 #endif
6596 
6597 #ifdef ILL_DEBUG
6598 void
6599 ip_thread_exit_stack(ip_stack_t *ipst)
6600 {
6601 	ill_t	*ill;
6602 	ipif_t	*ipif;
6603 	ill_walk_context_t	ctx;
6604 
6605 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6606 	ill = ILL_START_WALK_ALL(&ctx, ipst);
6607 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6608 		for (ipif = ill->ill_ipif; ipif != NULL;
6609 		    ipif = ipif->ipif_next) {
6610 			ipif_thread_exit(ipif, NULL);
6611 		}
6612 		ill_thread_exit(ill, NULL);
6613 	}
6614 	rw_exit(&ipst->ips_ill_g_lock);
6615 
6616 	ire_walk(ire_thread_exit, NULL, ipst);
6617 	ndp_walk_common(ipst->ips_ndp4, NULL, nce_thread_exit, NULL, B_FALSE);
6618 	ndp_walk_common(ipst->ips_ndp6, NULL, nce_thread_exit, NULL, B_FALSE);
6619 }
6620 
6621 /*
6622  * This is a function which is called from thread_exit
6623  * that can be used to debug reference count issues in IP. See comment in
6624  * <inet/ip.h> on how it is used.
6625  */
6626 void
6627 ip_thread_exit(void)
6628 {
6629 	netstack_t *ns;
6630 
6631 	ns = netstack_get_current();
6632 	if (ns != NULL) {
6633 		ip_thread_exit_stack(ns->netstack_ip);
6634 		netstack_rele(ns);
6635 	}
6636 }
6637 
6638 /*
6639  * Called when ipif is unplumbed or when memory alloc fails
6640  */
6641 void
6642 ipif_trace_cleanup(ipif_t *ipif)
6643 {
6644 	int	i;
6645 	th_trace_t	*th_trace;
6646 	th_trace_t	*th_trace_next;
6647 
6648 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6649 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
6650 		    th_trace = th_trace_next) {
6651 			th_trace_next = th_trace->th_next;
6652 			kmem_free(th_trace, sizeof (th_trace_t));
6653 		}
6654 		ipif->ipif_trace[i] = NULL;
6655 	}
6656 }
6657 
6658 /*
6659  * Called when ill is unplumbed or when memory alloc fails
6660  */
6661 void
6662 ill_trace_cleanup(ill_t *ill)
6663 {
6664 	int	i;
6665 	th_trace_t	*th_trace;
6666 	th_trace_t	*th_trace_next;
6667 
6668 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6669 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6670 		    th_trace = th_trace_next) {
6671 			th_trace_next = th_trace->th_next;
6672 			kmem_free(th_trace, sizeof (th_trace_t));
6673 		}
6674 		ill->ill_trace[i] = NULL;
6675 	}
6676 }
6677 
6678 #else
6679 void ip_thread_exit(void) {}
6680 #endif
6681 
6682 void
6683 ipif_refhold_locked(ipif_t *ipif)
6684 {
6685 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6686 	ipif->ipif_refcnt++;
6687 	IPIF_TRACE_REF(ipif);
6688 }
6689 
6690 void
6691 ipif_refhold(ipif_t *ipif)
6692 {
6693 	ill_t	*ill;
6694 
6695 	ill = ipif->ipif_ill;
6696 	mutex_enter(&ill->ill_lock);
6697 	ipif->ipif_refcnt++;
6698 	IPIF_TRACE_REF(ipif);
6699 	mutex_exit(&ill->ill_lock);
6700 }
6701 
6702 /*
6703  * Must not be called while holding any locks. Otherwise if this is
6704  * the last reference to be released there is a chance of recursive mutex
6705  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6706  * to restart an ioctl.
6707  */
6708 void
6709 ipif_refrele(ipif_t *ipif)
6710 {
6711 	ill_t	*ill;
6712 
6713 	ill = ipif->ipif_ill;
6714 
6715 	mutex_enter(&ill->ill_lock);
6716 	ASSERT(ipif->ipif_refcnt != 0);
6717 	ipif->ipif_refcnt--;
6718 	IPIF_UNTRACE_REF(ipif);
6719 	if (ipif->ipif_refcnt != 0) {
6720 		mutex_exit(&ill->ill_lock);
6721 		return;
6722 	}
6723 
6724 	/* Drops the ill_lock */
6725 	ipif_ill_refrele_tail(ill);
6726 }
6727 
6728 ipif_t *
6729 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6730 {
6731 	ipif_t	*ipif;
6732 
6733 	mutex_enter(&ill->ill_lock);
6734 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6735 	    ipif != NULL; ipif = ipif->ipif_next) {
6736 		if (!IPIF_CAN_LOOKUP(ipif))
6737 			continue;
6738 		ipif_refhold_locked(ipif);
6739 		mutex_exit(&ill->ill_lock);
6740 		return (ipif);
6741 	}
6742 	mutex_exit(&ill->ill_lock);
6743 	return (NULL);
6744 }
6745 
6746 /*
6747  * TODO: make this table extendible at run time
6748  * Return a pointer to the mac type info for 'mac_type'
6749  */
6750 static ip_m_t *
6751 ip_m_lookup(t_uscalar_t mac_type)
6752 {
6753 	ip_m_t	*ipm;
6754 
6755 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6756 		if (ipm->ip_m_mac_type == mac_type)
6757 			return (ipm);
6758 	return (NULL);
6759 }
6760 
6761 /*
6762  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6763  * ipif_arg is passed in to associate it with the correct interface.
6764  * We may need to restart this operation if the ipif cannot be looked up
6765  * due to an exclusive operation that is currently in progress. The restart
6766  * entry point is specified by 'func'
6767  */
6768 int
6769 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6770     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6771     ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp,
6772     ipsq_func_t func, struct rtsa_s *sp, ip_stack_t *ipst)
6773 {
6774 	ire_t	*ire;
6775 	ire_t	*gw_ire = NULL;
6776 	ipif_t	*ipif = NULL;
6777 	boolean_t ipif_refheld = B_FALSE;
6778 	uint_t	type;
6779 	int	match_flags = MATCH_IRE_TYPE;
6780 	int	error;
6781 	tsol_gc_t *gc = NULL;
6782 	tsol_gcgrp_t *gcgrp = NULL;
6783 	boolean_t gcgrp_xtraref = B_FALSE;
6784 
6785 	ip1dbg(("ip_rt_add:"));
6786 
6787 	if (ire_arg != NULL)
6788 		*ire_arg = NULL;
6789 
6790 	/*
6791 	 * If this is the case of RTF_HOST being set, then we set the netmask
6792 	 * to all ones (regardless if one was supplied).
6793 	 */
6794 	if (flags & RTF_HOST)
6795 		mask = IP_HOST_MASK;
6796 
6797 	/*
6798 	 * Prevent routes with a zero gateway from being created (since
6799 	 * interfaces can currently be plumbed and brought up no assigned
6800 	 * address).
6801 	 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0.
6802 	 */
6803 	if (gw_addr == 0 && src_ipif == NULL)
6804 		return (ENETUNREACH);
6805 	/*
6806 	 * Get the ipif, if any, corresponding to the gw_addr
6807 	 */
6808 	if (gw_addr != 0) {
6809 		ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func,
6810 		    &error, ipst);
6811 		if (ipif != NULL) {
6812 			if (IS_VNI(ipif->ipif_ill)) {
6813 				ipif_refrele(ipif);
6814 				return (EINVAL);
6815 			}
6816 			ipif_refheld = B_TRUE;
6817 		} else if (error == EINPROGRESS) {
6818 			ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6819 			return (EINPROGRESS);
6820 		} else {
6821 			error = 0;
6822 		}
6823 	}
6824 
6825 	if (ipif != NULL) {
6826 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6827 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6828 	} else {
6829 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6830 	}
6831 
6832 	/*
6833 	 * GateD will attempt to create routes with a loopback interface
6834 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6835 	 * these routes to be added, but create them as interface routes
6836 	 * since the gateway is an interface address.
6837 	 */
6838 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6839 		flags &= ~RTF_GATEWAY;
6840 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6841 		    mask == IP_HOST_MASK) {
6842 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6843 			    ALL_ZONES, NULL, match_flags, ipst);
6844 			if (ire != NULL) {
6845 				ire_refrele(ire);
6846 				if (ipif_refheld)
6847 					ipif_refrele(ipif);
6848 				return (EEXIST);
6849 			}
6850 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6851 			    "for 0x%x\n", (void *)ipif,
6852 			    ipif->ipif_ire_type,
6853 			    ntohl(ipif->ipif_lcl_addr)));
6854 			ire = ire_create(
6855 			    (uchar_t *)&dst_addr,	/* dest address */
6856 			    (uchar_t *)&mask,		/* mask */
6857 			    (uchar_t *)&ipif->ipif_src_addr,
6858 			    NULL,			/* no gateway */
6859 			    NULL,
6860 			    &ipif->ipif_mtu,
6861 			    NULL,
6862 			    ipif->ipif_rq,		/* recv-from queue */
6863 			    NULL,			/* no send-to queue */
6864 			    ipif->ipif_ire_type,	/* LOOPBACK */
6865 			    NULL,
6866 			    ipif,
6867 			    NULL,
6868 			    0,
6869 			    0,
6870 			    0,
6871 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6872 			    RTF_PRIVATE : 0,
6873 			    &ire_uinfo_null,
6874 			    NULL,
6875 			    NULL,
6876 			    ipst);
6877 
6878 			if (ire == NULL) {
6879 				if (ipif_refheld)
6880 					ipif_refrele(ipif);
6881 				return (ENOMEM);
6882 			}
6883 			error = ire_add(&ire, q, mp, func, B_FALSE);
6884 			if (error == 0)
6885 				goto save_ire;
6886 			if (ipif_refheld)
6887 				ipif_refrele(ipif);
6888 			return (error);
6889 
6890 		}
6891 	}
6892 
6893 	/*
6894 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6895 	 * and the gateway address provided is one of the system's interface
6896 	 * addresses.  By using the routing socket interface and supplying an
6897 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6898 	 * specifying an interface route to be created is available which uses
6899 	 * the interface index that specifies the outgoing interface rather than
6900 	 * the address of an outgoing interface (which may not be able to
6901 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6902 	 * flag, routes can be specified which not only specify the next-hop to
6903 	 * be used when routing to a certain prefix, but also which outgoing
6904 	 * interface should be used.
6905 	 *
6906 	 * Previously, interfaces would have unique addresses assigned to them
6907 	 * and so the address assigned to a particular interface could be used
6908 	 * to identify a particular interface.  One exception to this was the
6909 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6910 	 *
6911 	 * With the advent of IPv6 and its link-local addresses, this
6912 	 * restriction was relaxed and interfaces could share addresses between
6913 	 * themselves.  In fact, typically all of the link-local interfaces on
6914 	 * an IPv6 node or router will have the same link-local address.  In
6915 	 * order to differentiate between these interfaces, the use of an
6916 	 * interface index is necessary and this index can be carried inside a
6917 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6918 	 * of using the interface index, however, is that all of the ipif's that
6919 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6920 	 * cannot be used to differentiate between ipif's (or logical
6921 	 * interfaces) that belong to the same ill (physical interface).
6922 	 *
6923 	 * For example, in the following case involving IPv4 interfaces and
6924 	 * logical interfaces
6925 	 *
6926 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6927 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6928 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6929 	 *
6930 	 * the ipif's corresponding to each of these interface routes can be
6931 	 * uniquely identified by the "gateway" (actually interface address).
6932 	 *
6933 	 * In this case involving multiple IPv6 default routes to a particular
6934 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6935 	 * default route is of interest:
6936 	 *
6937 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6938 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6939 	 */
6940 
6941 	/* RTF_GATEWAY not set */
6942 	if (!(flags & RTF_GATEWAY)) {
6943 		queue_t	*stq;
6944 		queue_t	*rfq = NULL;
6945 		ill_t	*in_ill = NULL;
6946 
6947 		if (sp != NULL) {
6948 			ip2dbg(("ip_rt_add: gateway security attributes "
6949 			    "cannot be set with interface route\n"));
6950 			if (ipif_refheld)
6951 				ipif_refrele(ipif);
6952 			return (EINVAL);
6953 		}
6954 
6955 		/*
6956 		 * As the interface index specified with the RTA_IFP sockaddr is
6957 		 * the same for all ipif's off of an ill, the matching logic
6958 		 * below uses MATCH_IRE_ILL if such an index was specified.
6959 		 * This means that routes sharing the same prefix when added
6960 		 * using a RTA_IFP sockaddr must have distinct interface
6961 		 * indices (namely, they must be on distinct ill's).
6962 		 *
6963 		 * On the other hand, since the gateway address will usually be
6964 		 * different for each ipif on the system, the matching logic
6965 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6966 		 * route.  This means that interface routes for the same prefix
6967 		 * can be created if they belong to distinct ipif's and if a
6968 		 * RTA_IFP sockaddr is not present.
6969 		 */
6970 		if (ipif_arg != NULL) {
6971 			if (ipif_refheld)  {
6972 				ipif_refrele(ipif);
6973 				ipif_refheld = B_FALSE;
6974 			}
6975 			ipif = ipif_arg;
6976 			match_flags |= MATCH_IRE_ILL;
6977 		} else {
6978 			/*
6979 			 * Check the ipif corresponding to the gw_addr
6980 			 */
6981 			if (ipif == NULL)
6982 				return (ENETUNREACH);
6983 			match_flags |= MATCH_IRE_IPIF;
6984 		}
6985 		ASSERT(ipif != NULL);
6986 		/*
6987 		 * If src_ipif is not NULL, we have to create
6988 		 * an ire with non-null ire_in_ill value
6989 		 */
6990 		if (src_ipif != NULL) {
6991 			in_ill = src_ipif->ipif_ill;
6992 		}
6993 
6994 		/*
6995 		 * We check for an existing entry at this point.
6996 		 *
6997 		 * Since a netmask isn't passed in via the ioctl interface
6998 		 * (SIOCADDRT), we don't check for a matching netmask in that
6999 		 * case.
7000 		 */
7001 		if (!ioctl_msg)
7002 			match_flags |= MATCH_IRE_MASK;
7003 		if (src_ipif != NULL) {
7004 			/* Look up in the special table */
7005 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
7006 			    ipif, src_ipif->ipif_ill, match_flags);
7007 		} else {
7008 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7009 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
7010 			    NULL, match_flags, ipst);
7011 		}
7012 		if (ire != NULL) {
7013 			ire_refrele(ire);
7014 			if (ipif_refheld)
7015 				ipif_refrele(ipif);
7016 			return (EEXIST);
7017 		}
7018 
7019 		if (src_ipif != NULL) {
7020 			/*
7021 			 * Create the special ire for the IRE table
7022 			 * which hangs out of ire_in_ill. This ire
7023 			 * is in-between IRE_CACHE and IRE_INTERFACE.
7024 			 * Thus rfq is non-NULL.
7025 			 */
7026 			rfq = ipif->ipif_rq;
7027 		}
7028 		/* Create the usual interface ires */
7029 
7030 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7031 		    ? ipif->ipif_rq : ipif->ipif_wq;
7032 
7033 		/*
7034 		 * Create a copy of the IRE_LOOPBACK,
7035 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7036 		 * the modified address and netmask.
7037 		 */
7038 		ire = ire_create(
7039 		    (uchar_t *)&dst_addr,
7040 		    (uint8_t *)&mask,
7041 		    (uint8_t *)&ipif->ipif_src_addr,
7042 		    NULL,
7043 		    NULL,
7044 		    &ipif->ipif_mtu,
7045 		    NULL,
7046 		    rfq,
7047 		    stq,
7048 		    ipif->ipif_net_type,
7049 		    ipif->ipif_resolver_mp,
7050 		    ipif,
7051 		    in_ill,
7052 		    0,
7053 		    0,
7054 		    0,
7055 		    flags,
7056 		    &ire_uinfo_null,
7057 		    NULL,
7058 		    NULL,
7059 		    ipst);
7060 		if (ire == NULL) {
7061 			if (ipif_refheld)
7062 				ipif_refrele(ipif);
7063 			return (ENOMEM);
7064 		}
7065 
7066 		/*
7067 		 * Some software (for example, GateD and Sun Cluster) attempts
7068 		 * to create (what amount to) IRE_PREFIX routes with the
7069 		 * loopback address as the gateway.  This is primarily done to
7070 		 * set up prefixes with the RTF_REJECT flag set (for example,
7071 		 * when generating aggregate routes.)
7072 		 *
7073 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7074 		 * IRE_LOOPBACK, then we map the request into a
7075 		 * IRE_IF_NORESOLVER.
7076 		 *
7077 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7078 		 * routine, but rather using ire_create() directly.
7079 		 *
7080 		 */
7081 		if (ipif->ipif_net_type == IRE_LOOPBACK)
7082 			ire->ire_type = IRE_IF_NORESOLVER;
7083 
7084 		error = ire_add(&ire, q, mp, func, B_FALSE);
7085 		if (error == 0)
7086 			goto save_ire;
7087 
7088 		/*
7089 		 * In the result of failure, ire_add() will have already
7090 		 * deleted the ire in question, so there is no need to
7091 		 * do that here.
7092 		 */
7093 		if (ipif_refheld)
7094 			ipif_refrele(ipif);
7095 		return (error);
7096 	}
7097 	if (ipif_refheld) {
7098 		ipif_refrele(ipif);
7099 		ipif_refheld = B_FALSE;
7100 	}
7101 
7102 	if (src_ipif != NULL) {
7103 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
7104 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
7105 		return (EINVAL);
7106 	}
7107 	/*
7108 	 * Get an interface IRE for the specified gateway.
7109 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7110 	 * gateway, it is currently unreachable and we fail the request
7111 	 * accordingly.
7112 	 */
7113 	ipif = ipif_arg;
7114 	if (ipif_arg != NULL)
7115 		match_flags |= MATCH_IRE_ILL;
7116 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7117 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7118 	if (gw_ire == NULL)
7119 		return (ENETUNREACH);
7120 
7121 	/*
7122 	 * We create one of three types of IREs as a result of this request
7123 	 * based on the netmask.  A netmask of all ones (which is automatically
7124 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7125 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7126 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7127 	 * destination prefix.
7128 	 */
7129 	if (mask == IP_HOST_MASK)
7130 		type = IRE_HOST;
7131 	else if (mask == 0)
7132 		type = IRE_DEFAULT;
7133 	else
7134 		type = IRE_PREFIX;
7135 
7136 	/* check for a duplicate entry */
7137 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7138 	    NULL, ALL_ZONES, 0, NULL,
7139 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7140 	if (ire != NULL) {
7141 		ire_refrele(gw_ire);
7142 		ire_refrele(ire);
7143 		return (EEXIST);
7144 	}
7145 
7146 	/* Security attribute exists */
7147 	if (sp != NULL) {
7148 		tsol_gcgrp_addr_t ga;
7149 
7150 		/* find or create the gateway credentials group */
7151 		ga.ga_af = AF_INET;
7152 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7153 
7154 		/* we hold reference to it upon success */
7155 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7156 		if (gcgrp == NULL) {
7157 			ire_refrele(gw_ire);
7158 			return (ENOMEM);
7159 		}
7160 
7161 		/*
7162 		 * Create and add the security attribute to the group; a
7163 		 * reference to the group is made upon allocating a new
7164 		 * entry successfully.  If it finds an already-existing
7165 		 * entry for the security attribute in the group, it simply
7166 		 * returns it and no new reference is made to the group.
7167 		 */
7168 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7169 		if (gc == NULL) {
7170 			/* release reference held by gcgrp_lookup */
7171 			GCGRP_REFRELE(gcgrp);
7172 			ire_refrele(gw_ire);
7173 			return (ENOMEM);
7174 		}
7175 	}
7176 
7177 	/* Create the IRE. */
7178 	ire = ire_create(
7179 	    (uchar_t *)&dst_addr,		/* dest address */
7180 	    (uchar_t *)&mask,			/* mask */
7181 	    /* src address assigned by the caller? */
7182 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7183 		(flags & RTF_SETSRC)) ?  &src_addr : NULL),
7184 	    (uchar_t *)&gw_addr,		/* gateway address */
7185 	    NULL,				/* no in-srcaddress */
7186 	    &gw_ire->ire_max_frag,
7187 	    NULL,				/* no Fast Path header */
7188 	    NULL,				/* no recv-from queue */
7189 	    NULL,				/* no send-to queue */
7190 	    (ushort_t)type,			/* IRE type */
7191 	    NULL,
7192 	    ipif_arg,
7193 	    NULL,
7194 	    0,
7195 	    0,
7196 	    0,
7197 	    flags,
7198 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7199 	    gc,					/* security attribute */
7200 	    NULL,
7201 	    ipst);
7202 
7203 	/*
7204 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7205 	 * reference to the 'gcgrp'. We can now release the extra reference
7206 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7207 	 */
7208 	if (gcgrp_xtraref)
7209 		GCGRP_REFRELE(gcgrp);
7210 	if (ire == NULL) {
7211 		if (gc != NULL)
7212 			GC_REFRELE(gc);
7213 		ire_refrele(gw_ire);
7214 		return (ENOMEM);
7215 	}
7216 
7217 	/*
7218 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7219 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7220 	 */
7221 
7222 	/* Add the new IRE. */
7223 	error = ire_add(&ire, q, mp, func, B_FALSE);
7224 	if (error != 0) {
7225 		/*
7226 		 * In the result of failure, ire_add() will have already
7227 		 * deleted the ire in question, so there is no need to
7228 		 * do that here.
7229 		 */
7230 		ire_refrele(gw_ire);
7231 		return (error);
7232 	}
7233 
7234 	if (flags & RTF_MULTIRT) {
7235 		/*
7236 		 * Invoke the CGTP (multirouting) filtering module
7237 		 * to add the dst address in the filtering database.
7238 		 * Replicated inbound packets coming from that address
7239 		 * will be filtered to discard the duplicates.
7240 		 * It is not necessary to call the CGTP filter hook
7241 		 * when the dst address is a broadcast or multicast,
7242 		 * because an IP source address cannot be a broadcast
7243 		 * or a multicast.
7244 		 */
7245 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7246 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7247 		if (ire_dst != NULL) {
7248 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7249 			ire_refrele(ire_dst);
7250 			goto save_ire;
7251 		}
7252 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr) &&
7253 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
7254 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
7255 			    ire->ire_addr,
7256 			    ire->ire_gateway_addr,
7257 			    ire->ire_src_addr,
7258 			    gw_ire->ire_src_addr);
7259 			if (res != 0) {
7260 				ire_refrele(gw_ire);
7261 				ire_delete(ire);
7262 				return (res);
7263 			}
7264 		}
7265 	}
7266 
7267 	/*
7268 	 * Now that the prefix IRE entry has been created, delete any
7269 	 * existing gateway IRE cache entries as well as any IRE caches
7270 	 * using the gateway, and force them to be created through
7271 	 * ip_newroute.
7272 	 */
7273 	if (gc != NULL) {
7274 		ASSERT(gcgrp != NULL);
7275 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7276 	}
7277 
7278 save_ire:
7279 	if (gw_ire != NULL) {
7280 		ire_refrele(gw_ire);
7281 	}
7282 	/*
7283 	 * We do not do save_ire for the routes added with RTA_SRCIFP
7284 	 * flag. This route is only added and deleted by mipagent.
7285 	 * So, for simplicity of design, we refrain from saving
7286 	 * ires that are created with srcif value. This may change
7287 	 * in future if we find more usage of srcifp feature.
7288 	 */
7289 	if (ipif != NULL && src_ipif == NULL) {
7290 		/*
7291 		 * Save enough information so that we can recreate the IRE if
7292 		 * the interface goes down and then up.  The metrics associated
7293 		 * with the route will be saved as well when rts_setmetrics() is
7294 		 * called after the IRE has been created.  In the case where
7295 		 * memory cannot be allocated, none of this information will be
7296 		 * saved.
7297 		 */
7298 		ipif_save_ire(ipif, ire);
7299 	}
7300 	if (ioctl_msg)
7301 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7302 	if (ire_arg != NULL) {
7303 		/*
7304 		 * Store the ire that was successfully added into where ire_arg
7305 		 * points to so that callers don't have to look it up
7306 		 * themselves (but they are responsible for ire_refrele()ing
7307 		 * the ire when they are finished with it).
7308 		 */
7309 		*ire_arg = ire;
7310 	} else {
7311 		ire_refrele(ire);		/* Held in ire_add */
7312 	}
7313 	if (ipif_refheld)
7314 		ipif_refrele(ipif);
7315 	return (0);
7316 }
7317 
7318 /*
7319  * ip_rt_delete is called to delete an IPv4 route.
7320  * ipif_arg is passed in to associate it with the correct interface.
7321  * src_ipif is passed to associate the incoming interface of the packet.
7322  * We may need to restart this operation if the ipif cannot be looked up
7323  * due to an exclusive operation that is currently in progress. The restart
7324  * entry point is specified by 'func'
7325  */
7326 /* ARGSUSED4 */
7327 int
7328 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7329     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
7330     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
7331     ip_stack_t *ipst)
7332 {
7333 	ire_t	*ire = NULL;
7334 	ipif_t	*ipif;
7335 	boolean_t ipif_refheld = B_FALSE;
7336 	uint_t	type;
7337 	uint_t	match_flags = MATCH_IRE_TYPE;
7338 	int	err = 0;
7339 
7340 	ip1dbg(("ip_rt_delete:"));
7341 	/*
7342 	 * If this is the case of RTF_HOST being set, then we set the netmask
7343 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7344 	 */
7345 	if (flags & RTF_HOST) {
7346 		mask = IP_HOST_MASK;
7347 		match_flags |= MATCH_IRE_MASK;
7348 	} else if (rtm_addrs & RTA_NETMASK) {
7349 		match_flags |= MATCH_IRE_MASK;
7350 	}
7351 
7352 	/*
7353 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7354 	 * we check if the gateway address is one of our interfaces first,
7355 	 * and fall back on RTF_GATEWAY routes.
7356 	 *
7357 	 * This makes it possible to delete an original
7358 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7359 	 *
7360 	 * As the interface index specified with the RTA_IFP sockaddr is the
7361 	 * same for all ipif's off of an ill, the matching logic below uses
7362 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7363 	 * sharing the same prefix and interface index as the the route
7364 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7365 	 * is specified in the request.
7366 	 *
7367 	 * On the other hand, since the gateway address will usually be
7368 	 * different for each ipif on the system, the matching logic
7369 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7370 	 * route.  This means that interface routes for the same prefix can be
7371 	 * uniquely identified if they belong to distinct ipif's and if a
7372 	 * RTA_IFP sockaddr is not present.
7373 	 *
7374 	 * For more detail on specifying routes by gateway address and by
7375 	 * interface index, see the comments in ip_rt_add().
7376 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
7377 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
7378 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
7379 	 * succeed.
7380 	 */
7381 	if (src_ipif != NULL) {
7382 		if (ipif_arg == NULL && gw_addr != 0) {
7383 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
7384 			    q, mp, func, &err, ipst);
7385 			if (ipif_arg != NULL)
7386 				ipif_refheld = B_TRUE;
7387 		}
7388 		if (ipif_arg == NULL) {
7389 			err = (err == EINPROGRESS) ? err : ESRCH;
7390 			return (err);
7391 		}
7392 		ipif = ipif_arg;
7393 	} else {
7394 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
7395 			    q, mp, func, &err, ipst);
7396 		if (ipif != NULL)
7397 			ipif_refheld = B_TRUE;
7398 		else if (err == EINPROGRESS)
7399 			return (err);
7400 		else
7401 			err = 0;
7402 	}
7403 	if (ipif != NULL) {
7404 		if (ipif_arg != NULL) {
7405 			if (ipif_refheld) {
7406 				ipif_refrele(ipif);
7407 				ipif_refheld = B_FALSE;
7408 			}
7409 			ipif = ipif_arg;
7410 			match_flags |= MATCH_IRE_ILL;
7411 		} else {
7412 			match_flags |= MATCH_IRE_IPIF;
7413 		}
7414 		if (src_ipif != NULL) {
7415 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
7416 			    ipif, src_ipif->ipif_ill, match_flags);
7417 		} else {
7418 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7419 				ire = ire_ctable_lookup(dst_addr, 0,
7420 				    IRE_LOOPBACK, ipif, ALL_ZONES, NULL,
7421 				    match_flags, ipst);
7422 			}
7423 			if (ire == NULL) {
7424 				ire = ire_ftable_lookup(dst_addr, mask, 0,
7425 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
7426 				    NULL, match_flags, ipst);
7427 			}
7428 		}
7429 	}
7430 
7431 	if (ire == NULL) {
7432 		/*
7433 		 * At this point, the gateway address is not one of our own
7434 		 * addresses or a matching interface route was not found.  We
7435 		 * set the IRE type to lookup based on whether
7436 		 * this is a host route, a default route or just a prefix.
7437 		 *
7438 		 * If an ipif_arg was passed in, then the lookup is based on an
7439 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7440 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7441 		 * set as the route being looked up is not a traditional
7442 		 * interface route.
7443 		 * Since we do not add gateway route with srcipif, we don't
7444 		 * expect to find it either.
7445 		 */
7446 		if (src_ipif != NULL) {
7447 			if (ipif_refheld)
7448 				ipif_refrele(ipif);
7449 			return (ESRCH);
7450 		} else {
7451 			match_flags &= ~MATCH_IRE_IPIF;
7452 			match_flags |= MATCH_IRE_GW;
7453 			if (ipif_arg != NULL)
7454 				match_flags |= MATCH_IRE_ILL;
7455 			if (mask == IP_HOST_MASK)
7456 				type = IRE_HOST;
7457 			else if (mask == 0)
7458 				type = IRE_DEFAULT;
7459 			else
7460 				type = IRE_PREFIX;
7461 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
7462 			    ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags,
7463 			    ipst);
7464 		}
7465 	}
7466 
7467 	if (ipif_refheld)
7468 		ipif_refrele(ipif);
7469 
7470 	/* ipif is not refheld anymore */
7471 	if (ire == NULL)
7472 		return (ESRCH);
7473 
7474 	if (ire->ire_flags & RTF_MULTIRT) {
7475 		/*
7476 		 * Invoke the CGTP (multirouting) filtering module
7477 		 * to remove the dst address from the filtering database.
7478 		 * Packets coming from that address will no longer be
7479 		 * filtered to remove duplicates.
7480 		 */
7481 		if (ip_cgtp_filter_ops != NULL &&
7482 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
7483 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(
7484 			    ire->ire_addr, ire->ire_gateway_addr);
7485 		}
7486 		ip_cgtp_bcast_delete(ire, ipst);
7487 	}
7488 
7489 	ipif = ire->ire_ipif;
7490 	/*
7491 	 * Removing from ipif_saved_ire_mp is not necessary
7492 	 * when src_ipif being non-NULL. ip_rt_add does not
7493 	 * save the ires which src_ipif being non-NULL.
7494 	 */
7495 	if (ipif != NULL && src_ipif == NULL) {
7496 		ipif_remove_ire(ipif, ire);
7497 	}
7498 	if (ioctl_msg)
7499 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7500 	ire_delete(ire);
7501 	ire_refrele(ire);
7502 	return (err);
7503 }
7504 
7505 /*
7506  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7507  */
7508 /* ARGSUSED */
7509 int
7510 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7511     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7512 {
7513 	ipaddr_t dst_addr;
7514 	ipaddr_t gw_addr;
7515 	ipaddr_t mask;
7516 	int error = 0;
7517 	mblk_t *mp1;
7518 	struct rtentry *rt;
7519 	ipif_t *ipif = NULL;
7520 	ip_stack_t	*ipst;
7521 
7522 	ASSERT(q->q_next == NULL);
7523 	ipst = CONNQ_TO_IPST(q);
7524 
7525 	ip1dbg(("ip_siocaddrt:"));
7526 	/* Existence of mp1 verified in ip_wput_nondata */
7527 	mp1 = mp->b_cont->b_cont;
7528 	rt = (struct rtentry *)mp1->b_rptr;
7529 
7530 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7531 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7532 
7533 	/*
7534 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7535 	 * to a particular host address.  In this case, we set the netmask to
7536 	 * all ones for the particular destination address.  Otherwise,
7537 	 * determine the netmask to be used based on dst_addr and the interfaces
7538 	 * in use.
7539 	 */
7540 	if (rt->rt_flags & RTF_HOST) {
7541 		mask = IP_HOST_MASK;
7542 	} else {
7543 		/*
7544 		 * Note that ip_subnet_mask returns a zero mask in the case of
7545 		 * default (an all-zeroes address).
7546 		 */
7547 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7548 	}
7549 
7550 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7551 	    NULL, B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7552 	if (ipif != NULL)
7553 		ipif_refrele(ipif);
7554 	return (error);
7555 }
7556 
7557 /*
7558  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7559  */
7560 /* ARGSUSED */
7561 int
7562 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7563     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7564 {
7565 	ipaddr_t dst_addr;
7566 	ipaddr_t gw_addr;
7567 	ipaddr_t mask;
7568 	int error;
7569 	mblk_t *mp1;
7570 	struct rtentry *rt;
7571 	ipif_t *ipif = NULL;
7572 	ip_stack_t	*ipst;
7573 
7574 	ASSERT(q->q_next == NULL);
7575 	ipst = CONNQ_TO_IPST(q);
7576 
7577 	ip1dbg(("ip_siocdelrt:"));
7578 	/* Existence of mp1 verified in ip_wput_nondata */
7579 	mp1 = mp->b_cont->b_cont;
7580 	rt = (struct rtentry *)mp1->b_rptr;
7581 
7582 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7583 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7584 
7585 	/*
7586 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7587 	 * to a particular host address.  In this case, we set the netmask to
7588 	 * all ones for the particular destination address.  Otherwise,
7589 	 * determine the netmask to be used based on dst_addr and the interfaces
7590 	 * in use.
7591 	 */
7592 	if (rt->rt_flags & RTF_HOST) {
7593 		mask = IP_HOST_MASK;
7594 	} else {
7595 		/*
7596 		 * Note that ip_subnet_mask returns a zero mask in the case of
7597 		 * default (an all-zeroes address).
7598 		 */
7599 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7600 	}
7601 
7602 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7603 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
7604 	    B_TRUE, q, mp, ip_process_ioctl, ipst);
7605 	if (ipif != NULL)
7606 		ipif_refrele(ipif);
7607 	return (error);
7608 }
7609 
7610 /*
7611  * Enqueue the mp onto the ipsq, chained by b_next.
7612  * b_prev stores the function to be executed later, and b_queue the queue
7613  * where this mp originated.
7614  */
7615 void
7616 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7617     ill_t *pending_ill)
7618 {
7619 	conn_t	*connp = NULL;
7620 
7621 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7622 	ASSERT(func != NULL);
7623 
7624 	mp->b_queue = q;
7625 	mp->b_prev = (void *)func;
7626 	mp->b_next = NULL;
7627 
7628 	switch (type) {
7629 	case CUR_OP:
7630 		if (ipsq->ipsq_mptail != NULL) {
7631 			ASSERT(ipsq->ipsq_mphead != NULL);
7632 			ipsq->ipsq_mptail->b_next = mp;
7633 		} else {
7634 			ASSERT(ipsq->ipsq_mphead == NULL);
7635 			ipsq->ipsq_mphead = mp;
7636 		}
7637 		ipsq->ipsq_mptail = mp;
7638 		break;
7639 
7640 	case NEW_OP:
7641 		if (ipsq->ipsq_xopq_mptail != NULL) {
7642 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7643 			ipsq->ipsq_xopq_mptail->b_next = mp;
7644 		} else {
7645 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7646 			ipsq->ipsq_xopq_mphead = mp;
7647 		}
7648 		ipsq->ipsq_xopq_mptail = mp;
7649 		break;
7650 	default:
7651 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7652 	}
7653 
7654 	if (CONN_Q(q) && pending_ill != NULL) {
7655 		connp = Q_TO_CONN(q);
7656 
7657 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7658 		connp->conn_oper_pending_ill = pending_ill;
7659 	}
7660 }
7661 
7662 /*
7663  * Return the mp at the head of the ipsq. After emptying the ipsq
7664  * look at the next ioctl, if this ioctl is complete. Otherwise
7665  * return, we will resume when we complete the current ioctl.
7666  * The current ioctl will wait till it gets a response from the
7667  * driver below.
7668  */
7669 static mblk_t *
7670 ipsq_dq(ipsq_t *ipsq)
7671 {
7672 	mblk_t	*mp;
7673 
7674 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7675 
7676 	mp = ipsq->ipsq_mphead;
7677 	if (mp != NULL) {
7678 		ipsq->ipsq_mphead = mp->b_next;
7679 		if (ipsq->ipsq_mphead == NULL)
7680 			ipsq->ipsq_mptail = NULL;
7681 		mp->b_next = NULL;
7682 		return (mp);
7683 	}
7684 	if (ipsq->ipsq_current_ipif != NULL)
7685 		return (NULL);
7686 	mp = ipsq->ipsq_xopq_mphead;
7687 	if (mp != NULL) {
7688 		ipsq->ipsq_xopq_mphead = mp->b_next;
7689 		if (ipsq->ipsq_xopq_mphead == NULL)
7690 			ipsq->ipsq_xopq_mptail = NULL;
7691 		mp->b_next = NULL;
7692 		return (mp);
7693 	}
7694 	return (NULL);
7695 }
7696 
7697 /*
7698  * Enter the ipsq corresponding to ill, by waiting synchronously till
7699  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7700  * will have to drain completely before ipsq_enter returns success.
7701  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7702  * and the ipsq_exit logic will start the next enqueued ioctl after
7703  * completion of the current ioctl. If 'force' is used, we don't wait
7704  * for the enqueued ioctls. This is needed when a conn_close wants to
7705  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7706  * of an ill can also use this option. But we dont' use it currently.
7707  */
7708 #define	ENTER_SQ_WAIT_TICKS 100
7709 boolean_t
7710 ipsq_enter(ill_t *ill, boolean_t force)
7711 {
7712 	ipsq_t	*ipsq;
7713 	boolean_t waited_enough = B_FALSE;
7714 
7715 	/*
7716 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7717 	 * Since the <ill-ipsq> assocs could change while we wait for the
7718 	 * writer, it is easier to wait on a fixed global rather than try to
7719 	 * cv_wait on a changing ipsq.
7720 	 */
7721 	mutex_enter(&ill->ill_lock);
7722 	for (;;) {
7723 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7724 			mutex_exit(&ill->ill_lock);
7725 			return (B_FALSE);
7726 		}
7727 
7728 		ipsq = ill->ill_phyint->phyint_ipsq;
7729 		mutex_enter(&ipsq->ipsq_lock);
7730 		if (ipsq->ipsq_writer == NULL &&
7731 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7732 			break;
7733 		} else if (ipsq->ipsq_writer != NULL) {
7734 			mutex_exit(&ipsq->ipsq_lock);
7735 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7736 		} else {
7737 			mutex_exit(&ipsq->ipsq_lock);
7738 			if (force) {
7739 				(void) cv_timedwait(&ill->ill_cv,
7740 				    &ill->ill_lock,
7741 				    lbolt + ENTER_SQ_WAIT_TICKS);
7742 				waited_enough = B_TRUE;
7743 				continue;
7744 			} else {
7745 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7746 			}
7747 		}
7748 	}
7749 
7750 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7751 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7752 	ipsq->ipsq_writer = curthread;
7753 	ipsq->ipsq_reentry_cnt++;
7754 #ifdef ILL_DEBUG
7755 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7756 #endif
7757 	mutex_exit(&ipsq->ipsq_lock);
7758 	mutex_exit(&ill->ill_lock);
7759 	return (B_TRUE);
7760 }
7761 
7762 /*
7763  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7764  * certain critical operations like plumbing (i.e. most set ioctls),
7765  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7766  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7767  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7768  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7769  * threads executing in the ipsq. Responses from the driver pertain to the
7770  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7771  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7772  *
7773  * If a thread does not want to reenter the ipsq when it is already writer,
7774  * it must make sure that the specified reentry point to be called later
7775  * when the ipsq is empty, nor any code path starting from the specified reentry
7776  * point must never ever try to enter the ipsq again. Otherwise it can lead
7777  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7778  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7779  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7780  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7781  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7782  * ioctl if the current ioctl has completed. If the current ioctl is still
7783  * in progress it simply returns. The current ioctl could be waiting for
7784  * a response from another module (arp_ or the driver or could be waiting for
7785  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7786  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7787  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7788  * ipsq_current_ipif is clear which happens only on ioctl completion.
7789  */
7790 
7791 /*
7792  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7793  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7794  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7795  * completion.
7796  */
7797 ipsq_t *
7798 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7799     ipsq_func_t func, int type, boolean_t reentry_ok)
7800 {
7801 	ipsq_t	*ipsq;
7802 
7803 	/* Only 1 of ipif or ill can be specified */
7804 	ASSERT((ipif != NULL) ^ (ill != NULL));
7805 	if (ipif != NULL)
7806 		ill = ipif->ipif_ill;
7807 
7808 	/*
7809 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7810 	 * ipsq of an ill can't change when ill_lock is held.
7811 	 */
7812 	GRAB_CONN_LOCK(q);
7813 	mutex_enter(&ill->ill_lock);
7814 	ipsq = ill->ill_phyint->phyint_ipsq;
7815 	mutex_enter(&ipsq->ipsq_lock);
7816 
7817 	/*
7818 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7819 	 *    (Note: If the caller does not specify reentry_ok then neither
7820 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7821 	 *    again. Otherwise it can lead to an infinite loop
7822 	 * 2. Enter the ipsq if there is no current writer and this attempted
7823 	 *    entry is part of the current ioctl or operation
7824 	 * 3. Enter the ipsq if there is no current writer and this is a new
7825 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7826 	 *    empty and there is no ioctl (or operation) currently in progress
7827 	 */
7828 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7829 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7830 	    ipsq->ipsq_current_ipif == NULL))) ||
7831 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7832 		/* Success. */
7833 		ipsq->ipsq_reentry_cnt++;
7834 		ipsq->ipsq_writer = curthread;
7835 		mutex_exit(&ipsq->ipsq_lock);
7836 		mutex_exit(&ill->ill_lock);
7837 		RELEASE_CONN_LOCK(q);
7838 #ifdef ILL_DEBUG
7839 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7840 #endif
7841 		return (ipsq);
7842 	}
7843 
7844 	ipsq_enq(ipsq, q, mp, func, type, ill);
7845 
7846 	mutex_exit(&ipsq->ipsq_lock);
7847 	mutex_exit(&ill->ill_lock);
7848 	RELEASE_CONN_LOCK(q);
7849 	return (NULL);
7850 }
7851 
7852 /*
7853  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7854  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7855  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7856  * completion.
7857  *
7858  * This function does a refrele on the ipif/ill.
7859  */
7860 void
7861 qwriter_ip(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7862     ipsq_func_t func, int type, boolean_t reentry_ok)
7863 {
7864 	ipsq_t	*ipsq;
7865 
7866 	ipsq = ipsq_try_enter(ipif, ill, q, mp, func, type, reentry_ok);
7867 	/*
7868 	 * Caller must have done a refhold on the ipif. ipif_refrele
7869 	 * happens on the passed ipif. We can do this since we are
7870 	 * already exclusive, or we won't access ipif henceforth, Both
7871 	 * this func and caller will just return if we ipsq_try_enter
7872 	 * fails above. This is needed because func needs to
7873 	 * see the correct refcount. Eg. removeif can work only then.
7874 	 */
7875 	if (ipif != NULL)
7876 		ipif_refrele(ipif);
7877 	else
7878 		ill_refrele(ill);
7879 	if (ipsq != NULL) {
7880 		(*func)(ipsq, q, mp, NULL);
7881 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7882 	}
7883 }
7884 
7885 /*
7886  * If there are more than ILL_GRP_CNT ills in a group,
7887  * we use kmem alloc'd buffers, else use the stack
7888  */
7889 #define	ILL_GRP_CNT	14
7890 /*
7891  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7892  * Called by a thread that is currently exclusive on this ipsq.
7893  */
7894 void
7895 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7896 {
7897 	queue_t	*q;
7898 	mblk_t	*mp;
7899 	ipsq_func_t	func;
7900 	int	next;
7901 	ill_t	**ill_list = NULL;
7902 	size_t	ill_list_size = 0;
7903 	int	cnt = 0;
7904 	boolean_t need_ipsq_free = B_FALSE;
7905 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7906 
7907 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7908 	mutex_enter(&ipsq->ipsq_lock);
7909 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7910 	if (ipsq->ipsq_reentry_cnt != 1) {
7911 		ipsq->ipsq_reentry_cnt--;
7912 		mutex_exit(&ipsq->ipsq_lock);
7913 		return;
7914 	}
7915 
7916 	mp = ipsq_dq(ipsq);
7917 	while (mp != NULL) {
7918 again:
7919 		mutex_exit(&ipsq->ipsq_lock);
7920 		func = (ipsq_func_t)mp->b_prev;
7921 		q = (queue_t *)mp->b_queue;
7922 		mp->b_prev = NULL;
7923 		mp->b_queue = NULL;
7924 
7925 		/*
7926 		 * If 'q' is an conn queue, it is valid, since we did a
7927 		 * a refhold on the connp, at the start of the ioctl.
7928 		 * If 'q' is an ill queue, it is valid, since close of an
7929 		 * ill will clean up the 'ipsq'.
7930 		 */
7931 		(*func)(ipsq, q, mp, NULL);
7932 
7933 		mutex_enter(&ipsq->ipsq_lock);
7934 		mp = ipsq_dq(ipsq);
7935 	}
7936 
7937 	mutex_exit(&ipsq->ipsq_lock);
7938 
7939 	/*
7940 	 * Need to grab the locks in the right order. Need to
7941 	 * atomically check (under ipsq_lock) that there are no
7942 	 * messages before relinquishing the ipsq. Also need to
7943 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7944 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7945 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7946 	 * to grab ill_g_lock as writer.
7947 	 */
7948 	rw_enter(&ipst->ips_ill_g_lock,
7949 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7950 
7951 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7952 	if (ipsq->ipsq_refs != 0) {
7953 		/* At most 2 ills v4/v6 per phyint */
7954 		cnt = ipsq->ipsq_refs << 1;
7955 		ill_list_size = cnt * sizeof (ill_t *);
7956 		/*
7957 		 * If memory allocation fails, we will do the split
7958 		 * the next time ipsq_exit is called for whatever reason.
7959 		 * As long as the ipsq_split flag is set the need to
7960 		 * split is remembered.
7961 		 */
7962 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7963 		if (ill_list != NULL)
7964 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7965 	}
7966 	mutex_enter(&ipsq->ipsq_lock);
7967 	mp = ipsq_dq(ipsq);
7968 	if (mp != NULL) {
7969 		/* oops, some message has landed up, we can't get out */
7970 		if (ill_list != NULL)
7971 			ill_unlock_ills(ill_list, cnt);
7972 		rw_exit(&ipst->ips_ill_g_lock);
7973 		if (ill_list != NULL)
7974 			kmem_free(ill_list, ill_list_size);
7975 		ill_list = NULL;
7976 		ill_list_size = 0;
7977 		cnt = 0;
7978 		goto again;
7979 	}
7980 
7981 	/*
7982 	 * Split only if no ioctl is pending and if memory alloc succeeded
7983 	 * above.
7984 	 */
7985 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7986 		ill_list != NULL) {
7987 		/*
7988 		 * No new ill can join this ipsq since we are holding the
7989 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7990 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7991 		 * If so we will retry on the next ipsq_exit.
7992 		 */
7993 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7994 	}
7995 
7996 	/*
7997 	 * We are holding the ipsq lock, hence no new messages can
7998 	 * land up on the ipsq, and there are no messages currently.
7999 	 * Now safe to get out. Wake up waiters and relinquish ipsq
8000 	 * atomically while holding ill locks.
8001 	 */
8002 	ipsq->ipsq_writer = NULL;
8003 	ipsq->ipsq_reentry_cnt--;
8004 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
8005 #ifdef ILL_DEBUG
8006 	ipsq->ipsq_depth = 0;
8007 #endif
8008 	mutex_exit(&ipsq->ipsq_lock);
8009 	/*
8010 	 * For IPMP this should wake up all ills in this ipsq.
8011 	 * We need to hold the ill_lock while waking up waiters to
8012 	 * avoid missed wakeups. But there is no need to acquire all
8013 	 * the ill locks and then wakeup. If we have not acquired all
8014 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
8015 	 * wakes up ills one at a time after getting the right ill_lock
8016 	 */
8017 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
8018 	if (ill_list != NULL)
8019 		ill_unlock_ills(ill_list, cnt);
8020 	if (ipsq->ipsq_refs == 0)
8021 		need_ipsq_free = B_TRUE;
8022 	rw_exit(&ipst->ips_ill_g_lock);
8023 	if (ill_list != 0)
8024 		kmem_free(ill_list, ill_list_size);
8025 
8026 	if (need_ipsq_free) {
8027 		/*
8028 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
8029 		 * looked up. ipsq can be looked up only thru ill or phyint
8030 		 * and there are no ills/phyint on this ipsq.
8031 		 */
8032 		ipsq_delete(ipsq);
8033 	}
8034 	/*
8035 	 * Now start any igmp or mld timers that could not be started
8036 	 * while inside the ipsq. The timers can't be started while inside
8037 	 * the ipsq, since igmp_start_timers may need to call untimeout()
8038 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
8039 	 * there could be a deadlock since the timeout handlers
8040 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
8041 	 * wait in ipsq_enter() trying to get the ipsq.
8042 	 *
8043 	 * However there is one exception to the above. If this thread is
8044 	 * itself the igmp/mld timeout handler thread, then we don't want
8045 	 * to start any new timer until the current handler is done. The
8046 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
8047 	 * all others pass B_TRUE.
8048 	 */
8049 	if (start_igmp_timer) {
8050 		mutex_enter(&ipst->ips_igmp_timer_lock);
8051 		next = ipst->ips_igmp_deferred_next;
8052 		ipst->ips_igmp_deferred_next = INFINITY;
8053 		mutex_exit(&ipst->ips_igmp_timer_lock);
8054 
8055 		if (next != INFINITY)
8056 			igmp_start_timers(next, ipst);
8057 	}
8058 
8059 	if (start_mld_timer) {
8060 		mutex_enter(&ipst->ips_mld_timer_lock);
8061 		next = ipst->ips_mld_deferred_next;
8062 		ipst->ips_mld_deferred_next = INFINITY;
8063 		mutex_exit(&ipst->ips_mld_timer_lock);
8064 
8065 		if (next != INFINITY)
8066 			mld_start_timers(next, ipst);
8067 	}
8068 }
8069 
8070 /*
8071  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8072  * and `ioccmd'.
8073  */
8074 void
8075 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8076 {
8077 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8078 
8079 	mutex_enter(&ipsq->ipsq_lock);
8080 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8081 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8082 	ipsq->ipsq_current_ipif = ipif;
8083 	ipsq->ipsq_current_ioctl = ioccmd;
8084 	mutex_exit(&ipsq->ipsq_lock);
8085 }
8086 
8087 /*
8088  * Finish the current exclusive operation on `ipsq'.  Note that other
8089  * operations will not be able to proceed until an ipsq_exit() is done.
8090  */
8091 void
8092 ipsq_current_finish(ipsq_t *ipsq)
8093 {
8094 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8095 
8096 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8097 
8098 	/*
8099 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8100 	 * (but we're careful to never set IPIF_CHANGING in that case).
8101 	 */
8102 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8103 		mutex_enter(&ipif->ipif_ill->ill_lock);
8104 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8105 
8106 		/* Send any queued event */
8107 		ill_nic_info_dispatch(ipif->ipif_ill);
8108 		mutex_exit(&ipif->ipif_ill->ill_lock);
8109 	}
8110 
8111 	mutex_enter(&ipsq->ipsq_lock);
8112 	ASSERT(ipsq->ipsq_current_ipif != NULL);
8113 	ipsq->ipsq_current_ipif = NULL;
8114 	ipsq->ipsq_current_ioctl = 0;
8115 	mutex_exit(&ipsq->ipsq_lock);
8116 }
8117 
8118 /*
8119  * The ill is closing. Flush all messages on the ipsq that originated
8120  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8121  * for this ill since ipsq_enter could not have entered until then.
8122  * New messages can't be queued since the CONDEMNED flag is set.
8123  */
8124 static void
8125 ipsq_flush(ill_t *ill)
8126 {
8127 	queue_t	*q;
8128 	mblk_t	*prev;
8129 	mblk_t	*mp;
8130 	mblk_t	*mp_next;
8131 	ipsq_t	*ipsq;
8132 
8133 	ASSERT(IAM_WRITER_ILL(ill));
8134 	ipsq = ill->ill_phyint->phyint_ipsq;
8135 	/*
8136 	 * Flush any messages sent up by the driver.
8137 	 */
8138 	mutex_enter(&ipsq->ipsq_lock);
8139 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8140 		mp_next = mp->b_next;
8141 		q = mp->b_queue;
8142 		if (q == ill->ill_rq || q == ill->ill_wq) {
8143 			/* Remove the mp from the ipsq */
8144 			if (prev == NULL)
8145 				ipsq->ipsq_mphead = mp->b_next;
8146 			else
8147 				prev->b_next = mp->b_next;
8148 			if (ipsq->ipsq_mptail == mp) {
8149 				ASSERT(mp_next == NULL);
8150 				ipsq->ipsq_mptail = prev;
8151 			}
8152 			inet_freemsg(mp);
8153 		} else {
8154 			prev = mp;
8155 		}
8156 	}
8157 	mutex_exit(&ipsq->ipsq_lock);
8158 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8159 	ipsq_xopq_mp_cleanup(ill, NULL);
8160 	ill_pending_mp_cleanup(ill);
8161 }
8162 
8163 /*
8164  * Clean up one squeue element. ill_inuse_ref is protected by ill_lock.
8165  * The real cleanup happens behind the squeue via ip_squeue_clean function but
8166  * we need to protect ourselfs from 2 threads trying to cleanup at the same
8167  * time (possible with one port going down for aggr and someone tearing down the
8168  * entire aggr simultaneously. So we use ill_inuse_ref protected by ill_lock
8169  * to indicate when the cleanup has started (1 ref) and when the cleanup
8170  * is done (0 ref). When a new ring gets assigned to squeue, we start by
8171  * putting 2 ref on ill_inuse_ref.
8172  */
8173 static void
8174 ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring)
8175 {
8176 	conn_t *connp;
8177 	squeue_t *sqp;
8178 	mblk_t *mp;
8179 
8180 	ASSERT(rx_ring != NULL);
8181 
8182 	/* Just clean one squeue */
8183 	mutex_enter(&ill->ill_lock);
8184 	/*
8185 	 * Reset the ILL_SOFT_RING_ASSIGN bit so that
8186 	 * ip_squeue_soft_ring_affinty() will not go
8187 	 * ahead with assigning rings.
8188 	 */
8189 	ill->ill_state_flags &= ~ILL_SOFT_RING_ASSIGN;
8190 	while (rx_ring->rr_ring_state == ILL_RING_INPROC)
8191 		/* Some operations pending on the ring. Wait */
8192 		cv_wait(&ill->ill_cv, &ill->ill_lock);
8193 
8194 	if (rx_ring->rr_ring_state != ILL_RING_INUSE) {
8195 		/*
8196 		 * Someone already trying to clean
8197 		 * this squeue or its already been cleaned.
8198 		 */
8199 		mutex_exit(&ill->ill_lock);
8200 		return;
8201 	}
8202 	sqp = rx_ring->rr_sqp;
8203 
8204 	if (sqp == NULL) {
8205 		/*
8206 		 * The rx_ring never had a squeue assigned to it.
8207 		 * We are under ill_lock so we can clean it up
8208 		 * here itself since no one can get to it.
8209 		 */
8210 		rx_ring->rr_blank = NULL;
8211 		rx_ring->rr_handle = NULL;
8212 		rx_ring->rr_sqp = NULL;
8213 		rx_ring->rr_ring_state = ILL_RING_FREE;
8214 		mutex_exit(&ill->ill_lock);
8215 		return;
8216 	}
8217 
8218 	/* Set the state that its being cleaned */
8219 	rx_ring->rr_ring_state = ILL_RING_BEING_FREED;
8220 	ASSERT(sqp != NULL);
8221 	mutex_exit(&ill->ill_lock);
8222 
8223 	/*
8224 	 * Use the preallocated ill_unbind_conn for this purpose
8225 	 */
8226 	connp = ill->ill_dls_capab->ill_unbind_conn;
8227 
8228 	if (connp->conn_tcp->tcp_closemp.b_prev == NULL)
8229 		connp->conn_tcp->tcp_closemp_used = B_TRUE;
8230 	else
8231 		cmn_err(CE_PANIC, "ipsq_clean_ring: "
8232 		    "concurrent use of tcp_closemp_used: connp %p tcp %p\n",
8233 		    (void *)connp, (void *)connp->conn_tcp);
8234 
8235 	TCP_DEBUG_GETPCSTACK(connp->conn_tcp->tcmp_stk, 15);
8236 	mp = &connp->conn_tcp->tcp_closemp;
8237 	CONN_INC_REF(connp);
8238 	squeue_enter(sqp, mp, ip_squeue_clean, connp, NULL);
8239 
8240 	mutex_enter(&ill->ill_lock);
8241 	while (rx_ring->rr_ring_state != ILL_RING_FREE)
8242 		cv_wait(&ill->ill_cv, &ill->ill_lock);
8243 
8244 	mutex_exit(&ill->ill_lock);
8245 }
8246 
8247 static void
8248 ipsq_clean_all(ill_t *ill)
8249 {
8250 	int idx;
8251 
8252 	/*
8253 	 * No need to clean if poll_capab isn't set for this ill
8254 	 */
8255 	if (!(ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING)))
8256 		return;
8257 
8258 	for (idx = 0; idx < ILL_MAX_RINGS; idx++) {
8259 		ill_rx_ring_t *ipr = &ill->ill_dls_capab->ill_ring_tbl[idx];
8260 		ipsq_clean_ring(ill, ipr);
8261 	}
8262 
8263 	ill->ill_capabilities &= ~(ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING);
8264 }
8265 
8266 /* ARGSUSED */
8267 int
8268 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8269     ip_ioctl_cmd_t *ipip, void *ifreq)
8270 {
8271 	ill_t	*ill;
8272 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8273 	boolean_t isv6;
8274 	conn_t	*connp;
8275 	ip_stack_t	*ipst;
8276 
8277 	connp = Q_TO_CONN(q);
8278 	ipst = connp->conn_netstack->netstack_ip;
8279 	isv6 = connp->conn_af_isv6;
8280 	/*
8281 	 * Set original index.
8282 	 * Failover and failback move logical interfaces
8283 	 * from one physical interface to another.  The
8284 	 * original index indicates the parent of a logical
8285 	 * interface, in other words, the physical interface
8286 	 * the logical interface will be moved back to on
8287 	 * failback.
8288 	 */
8289 
8290 	/*
8291 	 * Don't allow the original index to be changed
8292 	 * for non-failover addresses, autoconfigured
8293 	 * addresses, or IPv6 link local addresses.
8294 	 */
8295 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8296 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8297 		return (EINVAL);
8298 	}
8299 	/*
8300 	 * The new original index must be in use by some
8301 	 * physical interface.
8302 	 */
8303 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8304 	    NULL, NULL, ipst);
8305 	if (ill == NULL)
8306 		return (ENXIO);
8307 	ill_refrele(ill);
8308 
8309 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8310 	/*
8311 	 * When this ipif gets failed back, don't
8312 	 * preserve the original id, as it is no
8313 	 * longer applicable.
8314 	 */
8315 	ipif->ipif_orig_ipifid = 0;
8316 	/*
8317 	 * For IPv4, change the original index of any
8318 	 * multicast addresses associated with the
8319 	 * ipif to the new value.
8320 	 */
8321 	if (!isv6) {
8322 		ilm_t *ilm;
8323 
8324 		mutex_enter(&ipif->ipif_ill->ill_lock);
8325 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8326 		    ilm = ilm->ilm_next) {
8327 			if (ilm->ilm_ipif == ipif) {
8328 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8329 			}
8330 		}
8331 		mutex_exit(&ipif->ipif_ill->ill_lock);
8332 	}
8333 	return (0);
8334 }
8335 
8336 /* ARGSUSED */
8337 int
8338 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8339     ip_ioctl_cmd_t *ipip, void *ifreq)
8340 {
8341 	struct lifreq *lifr = (struct lifreq *)ifreq;
8342 
8343 	/*
8344 	 * Get the original interface index i.e the one
8345 	 * before FAILOVER if it ever happened.
8346 	 */
8347 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8348 	return (0);
8349 }
8350 
8351 /*
8352  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8353  * refhold and return the associated ipif
8354  */
8355 int
8356 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
8357 {
8358 	boolean_t exists;
8359 	struct iftun_req *ta;
8360 	ipif_t	*ipif;
8361 	ill_t	*ill;
8362 	boolean_t isv6;
8363 	mblk_t	*mp1;
8364 	int	error;
8365 	conn_t	*connp;
8366 	ip_stack_t	*ipst;
8367 
8368 	/* Existence verified in ip_wput_nondata */
8369 	mp1 = mp->b_cont->b_cont;
8370 	ta = (struct iftun_req *)mp1->b_rptr;
8371 	/*
8372 	 * Null terminate the string to protect against buffer
8373 	 * overrun. String was generated by user code and may not
8374 	 * be trusted.
8375 	 */
8376 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8377 
8378 	connp = Q_TO_CONN(q);
8379 	isv6 = connp->conn_af_isv6;
8380 	ipst = connp->conn_netstack->netstack_ip;
8381 
8382 	/* Disallows implicit create */
8383 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8384 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8385 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8386 	if (ipif == NULL)
8387 		return (error);
8388 
8389 	if (ipif->ipif_id != 0) {
8390 		/*
8391 		 * We really don't want to set/get tunnel parameters
8392 		 * on virtual tunnel interfaces.  Only allow the
8393 		 * base tunnel to do these.
8394 		 */
8395 		ipif_refrele(ipif);
8396 		return (EINVAL);
8397 	}
8398 
8399 	/*
8400 	 * Send down to tunnel mod for ioctl processing.
8401 	 * Will finish ioctl in ip_rput_other().
8402 	 */
8403 	ill = ipif->ipif_ill;
8404 	if (ill->ill_net_type == IRE_LOOPBACK) {
8405 		ipif_refrele(ipif);
8406 		return (EOPNOTSUPP);
8407 	}
8408 
8409 	if (ill->ill_wq == NULL) {
8410 		ipif_refrele(ipif);
8411 		return (ENXIO);
8412 	}
8413 	/*
8414 	 * Mark the ioctl as coming from an IPv6 interface for
8415 	 * tun's convenience.
8416 	 */
8417 	if (ill->ill_isv6)
8418 		ta->ifta_flags |= 0x80000000;
8419 	*ipifp = ipif;
8420 	return (0);
8421 }
8422 
8423 /*
8424  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8425  * and return the associated ipif.
8426  * Return value:
8427  *	Non zero: An error has occurred. ci may not be filled out.
8428  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8429  *	a held ipif in ci.ci_ipif.
8430  */
8431 int
8432 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
8433     cmd_info_t *ci, ipsq_func_t func)
8434 {
8435 	sin_t		*sin;
8436 	sin6_t		*sin6;
8437 	char		*name;
8438 	struct ifreq    *ifr;
8439 	struct lifreq    *lifr;
8440 	ipif_t		*ipif = NULL;
8441 	ill_t		*ill;
8442 	conn_t		*connp;
8443 	boolean_t	isv6;
8444 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
8445 	boolean_t	exists;
8446 	int		err;
8447 	mblk_t		*mp1;
8448 	zoneid_t	zoneid;
8449 	ip_stack_t	*ipst;
8450 
8451 	if (q->q_next != NULL) {
8452 		ill = (ill_t *)q->q_ptr;
8453 		isv6 = ill->ill_isv6;
8454 		connp = NULL;
8455 		zoneid = ALL_ZONES;
8456 		ipst = ill->ill_ipst;
8457 	} else {
8458 		ill = NULL;
8459 		connp = Q_TO_CONN(q);
8460 		isv6 = connp->conn_af_isv6;
8461 		zoneid = connp->conn_zoneid;
8462 		if (zoneid == GLOBAL_ZONEID) {
8463 			/* global zone can access ipifs in all zones */
8464 			zoneid = ALL_ZONES;
8465 		}
8466 		ipst = connp->conn_netstack->netstack_ip;
8467 	}
8468 
8469 	/* Has been checked in ip_wput_nondata */
8470 	mp1 = mp->b_cont->b_cont;
8471 
8472 
8473 	if (cmd_type == IF_CMD) {
8474 		/* This a old style SIOC[GS]IF* command */
8475 		ifr = (struct ifreq *)mp1->b_rptr;
8476 		/*
8477 		 * Null terminate the string to protect against buffer
8478 		 * overrun. String was generated by user code and may not
8479 		 * be trusted.
8480 		 */
8481 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8482 		sin = (sin_t *)&ifr->ifr_addr;
8483 		name = ifr->ifr_name;
8484 		ci->ci_sin = sin;
8485 		ci->ci_sin6 = NULL;
8486 		ci->ci_lifr = (struct lifreq *)ifr;
8487 	} else {
8488 		/* This a new style SIOC[GS]LIF* command */
8489 		ASSERT(cmd_type == LIF_CMD);
8490 		lifr = (struct lifreq *)mp1->b_rptr;
8491 		/*
8492 		 * Null terminate the string to protect against buffer
8493 		 * overrun. String was generated by user code and may not
8494 		 * be trusted.
8495 		 */
8496 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8497 		name = lifr->lifr_name;
8498 		sin = (sin_t *)&lifr->lifr_addr;
8499 		sin6 = (sin6_t *)&lifr->lifr_addr;
8500 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
8501 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8502 			    LIFNAMSIZ);
8503 		}
8504 		ci->ci_sin = sin;
8505 		ci->ci_sin6 = sin6;
8506 		ci->ci_lifr = lifr;
8507 	}
8508 
8509 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
8510 		/*
8511 		 * The ioctl will be failed if the ioctl comes down
8512 		 * an conn stream
8513 		 */
8514 		if (ill == NULL) {
8515 			/*
8516 			 * Not an ill queue, return EINVAL same as the
8517 			 * old error code.
8518 			 */
8519 			return (ENXIO);
8520 		}
8521 		ipif = ill->ill_ipif;
8522 		ipif_refhold(ipif);
8523 	} else {
8524 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8525 		    &exists, isv6, zoneid,
8526 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8527 		    ipst);
8528 		if (ipif == NULL) {
8529 			if (err == EINPROGRESS)
8530 				return (err);
8531 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
8532 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
8533 				/*
8534 				 * Need to try both v4 and v6 since this
8535 				 * ioctl can come down either v4 or v6
8536 				 * socket. The lifreq.lifr_family passed
8537 				 * down by this ioctl is AF_UNSPEC.
8538 				 */
8539 				ipif = ipif_lookup_on_name(name,
8540 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8541 				    zoneid, (connp == NULL) ? q :
8542 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8543 				if (err == EINPROGRESS)
8544 					return (err);
8545 			}
8546 			err = 0;	/* Ensure we don't use it below */
8547 		}
8548 	}
8549 
8550 	/*
8551 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8552 	 */
8553 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
8554 		ipif_refrele(ipif);
8555 		return (ENXIO);
8556 	}
8557 
8558 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8559 	    name[0] == '\0') {
8560 		/*
8561 		 * Handle a or a SIOC?IF* with a null name
8562 		 * during plumb (on the ill queue before the I_PLINK).
8563 		 */
8564 		ipif = ill->ill_ipif;
8565 		ipif_refhold(ipif);
8566 	}
8567 
8568 	if (ipif == NULL)
8569 		return (ENXIO);
8570 
8571 	/*
8572 	 * Allow only GET operations if this ipif has been created
8573 	 * temporarily due to a MOVE operation.
8574 	 */
8575 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
8576 		ipif_refrele(ipif);
8577 		return (EINVAL);
8578 	}
8579 
8580 	ci->ci_ipif = ipif;
8581 	return (0);
8582 }
8583 
8584 /*
8585  * Return the total number of ipifs.
8586  */
8587 static uint_t
8588 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8589 {
8590 	uint_t numifs = 0;
8591 	ill_t	*ill;
8592 	ill_walk_context_t	ctx;
8593 	ipif_t	*ipif;
8594 
8595 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8596 	ill = ILL_START_WALK_V4(&ctx, ipst);
8597 
8598 	while (ill != NULL) {
8599 		for (ipif = ill->ill_ipif; ipif != NULL;
8600 		    ipif = ipif->ipif_next) {
8601 			if (ipif->ipif_zoneid == zoneid ||
8602 			    ipif->ipif_zoneid == ALL_ZONES)
8603 				numifs++;
8604 		}
8605 		ill = ill_next(&ctx, ill);
8606 	}
8607 	rw_exit(&ipst->ips_ill_g_lock);
8608 	return (numifs);
8609 }
8610 
8611 /*
8612  * Return the total number of ipifs.
8613  */
8614 static uint_t
8615 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8616 {
8617 	uint_t numifs = 0;
8618 	ill_t	*ill;
8619 	ipif_t	*ipif;
8620 	ill_walk_context_t	ctx;
8621 
8622 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8623 
8624 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8625 	if (family == AF_INET)
8626 		ill = ILL_START_WALK_V4(&ctx, ipst);
8627 	else if (family == AF_INET6)
8628 		ill = ILL_START_WALK_V6(&ctx, ipst);
8629 	else
8630 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8631 
8632 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8633 		for (ipif = ill->ill_ipif; ipif != NULL;
8634 		    ipif = ipif->ipif_next) {
8635 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8636 			    !(lifn_flags & LIFC_NOXMIT))
8637 				continue;
8638 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8639 			    !(lifn_flags & LIFC_TEMPORARY))
8640 				continue;
8641 			if (((ipif->ipif_flags &
8642 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8643 			    IPIF_DEPRECATED)) ||
8644 			    (ill->ill_phyint->phyint_flags &
8645 			    PHYI_LOOPBACK) ||
8646 			    !(ipif->ipif_flags & IPIF_UP)) &&
8647 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8648 				continue;
8649 
8650 			if (zoneid != ipif->ipif_zoneid &&
8651 			    ipif->ipif_zoneid != ALL_ZONES &&
8652 			    (zoneid != GLOBAL_ZONEID ||
8653 			    !(lifn_flags & LIFC_ALLZONES)))
8654 				continue;
8655 
8656 			numifs++;
8657 		}
8658 	}
8659 	rw_exit(&ipst->ips_ill_g_lock);
8660 	return (numifs);
8661 }
8662 
8663 uint_t
8664 ip_get_lifsrcofnum(ill_t *ill)
8665 {
8666 	uint_t numifs = 0;
8667 	ill_t	*ill_head = ill;
8668 	ip_stack_t	*ipst = ill->ill_ipst;
8669 
8670 	/*
8671 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8672 	 * other thread may be trying to relink the ILLs in this usesrc group
8673 	 * and adjusting the ill_usesrc_grp_next pointers
8674 	 */
8675 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8676 	if ((ill->ill_usesrc_ifindex == 0) &&
8677 	    (ill->ill_usesrc_grp_next != NULL)) {
8678 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8679 		    ill = ill->ill_usesrc_grp_next)
8680 			numifs++;
8681 	}
8682 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8683 
8684 	return (numifs);
8685 }
8686 
8687 /* Null values are passed in for ipif, sin, and ifreq */
8688 /* ARGSUSED */
8689 int
8690 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8691     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8692 {
8693 	int *nump;
8694 	conn_t *connp = Q_TO_CONN(q);
8695 
8696 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8697 
8698 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8699 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8700 
8701 	*nump = ip_get_numifs(connp->conn_zoneid,
8702 	    connp->conn_netstack->netstack_ip);
8703 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8704 	return (0);
8705 }
8706 
8707 /* Null values are passed in for ipif, sin, and ifreq */
8708 /* ARGSUSED */
8709 int
8710 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8711     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8712 {
8713 	struct lifnum *lifn;
8714 	mblk_t	*mp1;
8715 	conn_t *connp = Q_TO_CONN(q);
8716 
8717 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8718 
8719 	/* Existence checked in ip_wput_nondata */
8720 	mp1 = mp->b_cont->b_cont;
8721 
8722 	lifn = (struct lifnum *)mp1->b_rptr;
8723 	switch (lifn->lifn_family) {
8724 	case AF_UNSPEC:
8725 	case AF_INET:
8726 	case AF_INET6:
8727 		break;
8728 	default:
8729 		return (EAFNOSUPPORT);
8730 	}
8731 
8732 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8733 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8734 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8735 	return (0);
8736 }
8737 
8738 /* ARGSUSED */
8739 int
8740 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8741     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8742 {
8743 	STRUCT_HANDLE(ifconf, ifc);
8744 	mblk_t *mp1;
8745 	struct iocblk *iocp;
8746 	struct ifreq *ifr;
8747 	ill_walk_context_t	ctx;
8748 	ill_t	*ill;
8749 	ipif_t	*ipif;
8750 	struct sockaddr_in *sin;
8751 	int32_t	ifclen;
8752 	zoneid_t zoneid;
8753 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8754 
8755 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8756 
8757 	ip1dbg(("ip_sioctl_get_ifconf"));
8758 	/* Existence verified in ip_wput_nondata */
8759 	mp1 = mp->b_cont->b_cont;
8760 	iocp = (struct iocblk *)mp->b_rptr;
8761 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8762 
8763 	/*
8764 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8765 	 * the user buffer address and length into which the list of struct
8766 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8767 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8768 	 * the SIOCGIFCONF operation was redefined to simply provide
8769 	 * a large output buffer into which we are supposed to jam the ifreq
8770 	 * array.  The same ioctl command code was used, despite the fact that
8771 	 * both the applications and the kernel code had to change, thus making
8772 	 * it impossible to support both interfaces.
8773 	 *
8774 	 * For reasons not good enough to try to explain, the following
8775 	 * algorithm is used for deciding what to do with one of these:
8776 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8777 	 * form with the output buffer coming down as the continuation message.
8778 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8779 	 * and we have to copy in the ifconf structure to find out how big the
8780 	 * output buffer is and where to copy out to.  Sure no problem...
8781 	 *
8782 	 */
8783 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8784 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8785 		int numifs = 0;
8786 		size_t ifc_bufsize;
8787 
8788 		/*
8789 		 * Must be (better be!) continuation of a TRANSPARENT
8790 		 * IOCTL.  We just copied in the ifconf structure.
8791 		 */
8792 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8793 		    (struct ifconf *)mp1->b_rptr);
8794 
8795 		/*
8796 		 * Allocate a buffer to hold requested information.
8797 		 *
8798 		 * If ifc_len is larger than what is needed, we only
8799 		 * allocate what we will use.
8800 		 *
8801 		 * If ifc_len is smaller than what is needed, return
8802 		 * EINVAL.
8803 		 *
8804 		 * XXX: the ill_t structure can hava 2 counters, for
8805 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8806 		 * number of interfaces for a device, so we don't need
8807 		 * to count them here...
8808 		 */
8809 		numifs = ip_get_numifs(zoneid, ipst);
8810 
8811 		ifclen = STRUCT_FGET(ifc, ifc_len);
8812 		ifc_bufsize = numifs * sizeof (struct ifreq);
8813 		if (ifc_bufsize > ifclen) {
8814 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8815 				/* old behaviour */
8816 				return (EINVAL);
8817 			} else {
8818 				ifc_bufsize = ifclen;
8819 			}
8820 		}
8821 
8822 		mp1 = mi_copyout_alloc(q, mp,
8823 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8824 		if (mp1 == NULL)
8825 			return (ENOMEM);
8826 
8827 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8828 	}
8829 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8830 	/*
8831 	 * the SIOCGIFCONF ioctl only knows about
8832 	 * IPv4 addresses, so don't try to tell
8833 	 * it about interfaces with IPv6-only
8834 	 * addresses. (Last parm 'isv6' is B_FALSE)
8835 	 */
8836 
8837 	ifr = (struct ifreq *)mp1->b_rptr;
8838 
8839 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8840 	ill = ILL_START_WALK_V4(&ctx, ipst);
8841 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8842 		for (ipif = ill->ill_ipif; ipif != NULL;
8843 		    ipif = ipif->ipif_next) {
8844 			if (zoneid != ipif->ipif_zoneid &&
8845 			    ipif->ipif_zoneid != ALL_ZONES)
8846 				continue;
8847 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8848 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8849 					/* old behaviour */
8850 					rw_exit(&ipst->ips_ill_g_lock);
8851 					return (EINVAL);
8852 				} else {
8853 					goto if_copydone;
8854 				}
8855 			}
8856 			(void) ipif_get_name(ipif,
8857 			    ifr->ifr_name,
8858 			    sizeof (ifr->ifr_name));
8859 			sin = (sin_t *)&ifr->ifr_addr;
8860 			*sin = sin_null;
8861 			sin->sin_family = AF_INET;
8862 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8863 			ifr++;
8864 		}
8865 	}
8866 if_copydone:
8867 	rw_exit(&ipst->ips_ill_g_lock);
8868 	mp1->b_wptr = (uchar_t *)ifr;
8869 
8870 	if (STRUCT_BUF(ifc) != NULL) {
8871 		STRUCT_FSET(ifc, ifc_len,
8872 			(int)((uchar_t *)ifr - mp1->b_rptr));
8873 	}
8874 	return (0);
8875 }
8876 
8877 /*
8878  * Get the interfaces using the address hosted on the interface passed in,
8879  * as a source adddress
8880  */
8881 /* ARGSUSED */
8882 int
8883 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8884     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8885 {
8886 	mblk_t *mp1;
8887 	ill_t	*ill, *ill_head;
8888 	ipif_t	*ipif, *orig_ipif;
8889 	int	numlifs = 0;
8890 	size_t	lifs_bufsize, lifsmaxlen;
8891 	struct	lifreq *lifr;
8892 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8893 	uint_t	ifindex;
8894 	zoneid_t zoneid;
8895 	int err = 0;
8896 	boolean_t isv6 = B_FALSE;
8897 	struct	sockaddr_in	*sin;
8898 	struct	sockaddr_in6	*sin6;
8899 	STRUCT_HANDLE(lifsrcof, lifs);
8900 	ip_stack_t		*ipst;
8901 
8902 	ipst = CONNQ_TO_IPST(q);
8903 
8904 	ASSERT(q->q_next == NULL);
8905 
8906 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8907 
8908 	/* Existence verified in ip_wput_nondata */
8909 	mp1 = mp->b_cont->b_cont;
8910 
8911 	/*
8912 	 * Must be (better be!) continuation of a TRANSPARENT
8913 	 * IOCTL.  We just copied in the lifsrcof structure.
8914 	 */
8915 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8916 	    (struct lifsrcof *)mp1->b_rptr);
8917 
8918 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8919 		return (EINVAL);
8920 
8921 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8922 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8923 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8924 	    ip_process_ioctl, &err, ipst);
8925 	if (ipif == NULL) {
8926 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8927 		    ifindex));
8928 		return (err);
8929 	}
8930 
8931 
8932 	/* Allocate a buffer to hold requested information */
8933 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8934 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8935 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8936 	/* The actual size needed is always returned in lifs_len */
8937 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8938 
8939 	/* If the amount we need is more than what is passed in, abort */
8940 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8941 		ipif_refrele(ipif);
8942 		return (0);
8943 	}
8944 
8945 	mp1 = mi_copyout_alloc(q, mp,
8946 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8947 	if (mp1 == NULL) {
8948 		ipif_refrele(ipif);
8949 		return (ENOMEM);
8950 	}
8951 
8952 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8953 	bzero(mp1->b_rptr, lifs_bufsize);
8954 
8955 	lifr = (struct lifreq *)mp1->b_rptr;
8956 
8957 	ill = ill_head = ipif->ipif_ill;
8958 	orig_ipif = ipif;
8959 
8960 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8961 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8962 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8963 
8964 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8965 	for (; (ill != NULL) && (ill != ill_head);
8966 	    ill = ill->ill_usesrc_grp_next) {
8967 
8968 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8969 			break;
8970 
8971 		ipif = ill->ill_ipif;
8972 		(void) ipif_get_name(ipif,
8973 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8974 		if (ipif->ipif_isv6) {
8975 			sin6 = (sin6_t *)&lifr->lifr_addr;
8976 			*sin6 = sin6_null;
8977 			sin6->sin6_family = AF_INET6;
8978 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8979 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8980 			    &ipif->ipif_v6net_mask);
8981 		} else {
8982 			sin = (sin_t *)&lifr->lifr_addr;
8983 			*sin = sin_null;
8984 			sin->sin_family = AF_INET;
8985 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8986 			lifr->lifr_addrlen = ip_mask_to_plen(
8987 			    ipif->ipif_net_mask);
8988 		}
8989 		lifr++;
8990 	}
8991 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8992 	rw_exit(&ipst->ips_ill_g_lock);
8993 	ipif_refrele(orig_ipif);
8994 	mp1->b_wptr = (uchar_t *)lifr;
8995 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8996 
8997 	return (0);
8998 }
8999 
9000 /* ARGSUSED */
9001 int
9002 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
9003     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9004 {
9005 	mblk_t *mp1;
9006 	int	list;
9007 	ill_t	*ill;
9008 	ipif_t	*ipif;
9009 	int	flags;
9010 	int	numlifs = 0;
9011 	size_t	lifc_bufsize;
9012 	struct	lifreq *lifr;
9013 	sa_family_t	family;
9014 	struct	sockaddr_in	*sin;
9015 	struct	sockaddr_in6	*sin6;
9016 	ill_walk_context_t	ctx;
9017 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9018 	int32_t	lifclen;
9019 	zoneid_t zoneid;
9020 	STRUCT_HANDLE(lifconf, lifc);
9021 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9022 
9023 	ip1dbg(("ip_sioctl_get_lifconf"));
9024 
9025 	ASSERT(q->q_next == NULL);
9026 
9027 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9028 
9029 	/* Existence verified in ip_wput_nondata */
9030 	mp1 = mp->b_cont->b_cont;
9031 
9032 	/*
9033 	 * An extended version of SIOCGIFCONF that takes an
9034 	 * additional address family and flags field.
9035 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
9036 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
9037 	 * interfaces are omitted.
9038 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
9039 	 * unless LIFC_TEMPORARY is specified.
9040 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
9041 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
9042 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
9043 	 * has priority over LIFC_NOXMIT.
9044 	 */
9045 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
9046 
9047 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
9048 		return (EINVAL);
9049 
9050 	/*
9051 	 * Must be (better be!) continuation of a TRANSPARENT
9052 	 * IOCTL.  We just copied in the lifconf structure.
9053 	 */
9054 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
9055 
9056 	family = STRUCT_FGET(lifc, lifc_family);
9057 	flags = STRUCT_FGET(lifc, lifc_flags);
9058 
9059 	switch (family) {
9060 	case AF_UNSPEC:
9061 		/*
9062 		 * walk all ILL's.
9063 		 */
9064 		list = MAX_G_HEADS;
9065 		break;
9066 	case AF_INET:
9067 		/*
9068 		 * walk only IPV4 ILL's.
9069 		 */
9070 		list = IP_V4_G_HEAD;
9071 		break;
9072 	case AF_INET6:
9073 		/*
9074 		 * walk only IPV6 ILL's.
9075 		 */
9076 		list = IP_V6_G_HEAD;
9077 		break;
9078 	default:
9079 		return (EAFNOSUPPORT);
9080 	}
9081 
9082 	/*
9083 	 * Allocate a buffer to hold requested information.
9084 	 *
9085 	 * If lifc_len is larger than what is needed, we only
9086 	 * allocate what we will use.
9087 	 *
9088 	 * If lifc_len is smaller than what is needed, return
9089 	 * EINVAL.
9090 	 */
9091 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
9092 	lifc_bufsize = numlifs * sizeof (struct lifreq);
9093 	lifclen = STRUCT_FGET(lifc, lifc_len);
9094 	if (lifc_bufsize > lifclen) {
9095 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
9096 			return (EINVAL);
9097 		else
9098 			lifc_bufsize = lifclen;
9099 	}
9100 
9101 	mp1 = mi_copyout_alloc(q, mp,
9102 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
9103 	if (mp1 == NULL)
9104 		return (ENOMEM);
9105 
9106 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
9107 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
9108 
9109 	lifr = (struct lifreq *)mp1->b_rptr;
9110 
9111 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9112 	ill = ill_first(list, list, &ctx, ipst);
9113 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9114 		for (ipif = ill->ill_ipif; ipif != NULL;
9115 		    ipif = ipif->ipif_next) {
9116 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
9117 			    !(flags & LIFC_NOXMIT))
9118 				continue;
9119 
9120 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
9121 			    !(flags & LIFC_TEMPORARY))
9122 				continue;
9123 
9124 			if (((ipif->ipif_flags &
9125 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
9126 			    IPIF_DEPRECATED)) ||
9127 			    (ill->ill_phyint->phyint_flags &
9128 			    PHYI_LOOPBACK) ||
9129 			    !(ipif->ipif_flags & IPIF_UP)) &&
9130 			    (flags & LIFC_EXTERNAL_SOURCE))
9131 				continue;
9132 
9133 			if (zoneid != ipif->ipif_zoneid &&
9134 			    ipif->ipif_zoneid != ALL_ZONES &&
9135 			    (zoneid != GLOBAL_ZONEID ||
9136 			    !(flags & LIFC_ALLZONES)))
9137 				continue;
9138 
9139 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
9140 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
9141 					rw_exit(&ipst->ips_ill_g_lock);
9142 					return (EINVAL);
9143 				} else {
9144 					goto lif_copydone;
9145 				}
9146 			}
9147 
9148 			(void) ipif_get_name(ipif,
9149 				lifr->lifr_name,
9150 				sizeof (lifr->lifr_name));
9151 			if (ipif->ipif_isv6) {
9152 				sin6 = (sin6_t *)&lifr->lifr_addr;
9153 				*sin6 = sin6_null;
9154 				sin6->sin6_family = AF_INET6;
9155 				sin6->sin6_addr =
9156 				ipif->ipif_v6lcl_addr;
9157 				lifr->lifr_addrlen =
9158 				ip_mask_to_plen_v6(
9159 				    &ipif->ipif_v6net_mask);
9160 			} else {
9161 				sin = (sin_t *)&lifr->lifr_addr;
9162 				*sin = sin_null;
9163 				sin->sin_family = AF_INET;
9164 				sin->sin_addr.s_addr =
9165 				    ipif->ipif_lcl_addr;
9166 				lifr->lifr_addrlen =
9167 				    ip_mask_to_plen(
9168 				    ipif->ipif_net_mask);
9169 			}
9170 			lifr++;
9171 		}
9172 	}
9173 lif_copydone:
9174 	rw_exit(&ipst->ips_ill_g_lock);
9175 
9176 	mp1->b_wptr = (uchar_t *)lifr;
9177 	if (STRUCT_BUF(lifc) != NULL) {
9178 		STRUCT_FSET(lifc, lifc_len,
9179 			(int)((uchar_t *)lifr - mp1->b_rptr));
9180 	}
9181 	return (0);
9182 }
9183 
9184 /* ARGSUSED */
9185 int
9186 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9187     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9188 {
9189 	ip_stack_t	*ipst;
9190 
9191 	if (q->q_next == NULL)
9192 		ipst = CONNQ_TO_IPST(q);
9193 	else
9194 		ipst = ILLQ_TO_IPST(q);
9195 
9196 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9197 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9198 	return (0);
9199 }
9200 
9201 static void
9202 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9203 {
9204 	ip6_asp_t *table;
9205 	size_t table_size;
9206 	mblk_t *data_mp;
9207 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9208 	ip_stack_t	*ipst;
9209 
9210 	if (q->q_next == NULL)
9211 		ipst = CONNQ_TO_IPST(q);
9212 	else
9213 		ipst = ILLQ_TO_IPST(q);
9214 
9215 	/* These two ioctls are I_STR only */
9216 	if (iocp->ioc_count == TRANSPARENT) {
9217 		miocnak(q, mp, 0, EINVAL);
9218 		return;
9219 	}
9220 
9221 	data_mp = mp->b_cont;
9222 	if (data_mp == NULL) {
9223 		/* The user passed us a NULL argument */
9224 		table = NULL;
9225 		table_size = iocp->ioc_count;
9226 	} else {
9227 		/*
9228 		 * The user provided a table.  The stream head
9229 		 * may have copied in the user data in chunks,
9230 		 * so make sure everything is pulled up
9231 		 * properly.
9232 		 */
9233 		if (MBLKL(data_mp) < iocp->ioc_count) {
9234 			mblk_t *new_data_mp;
9235 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9236 			    NULL) {
9237 				miocnak(q, mp, 0, ENOMEM);
9238 				return;
9239 			}
9240 			freemsg(data_mp);
9241 			data_mp = new_data_mp;
9242 			mp->b_cont = data_mp;
9243 		}
9244 		table = (ip6_asp_t *)data_mp->b_rptr;
9245 		table_size = iocp->ioc_count;
9246 	}
9247 
9248 	switch (iocp->ioc_cmd) {
9249 	case SIOCGIP6ADDRPOLICY:
9250 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9251 		if (iocp->ioc_rval == -1)
9252 			iocp->ioc_error = EINVAL;
9253 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9254 		else if (table != NULL &&
9255 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9256 			ip6_asp_t *src = table;
9257 			ip6_asp32_t *dst = (void *)table;
9258 			int count = table_size / sizeof (ip6_asp_t);
9259 			int i;
9260 
9261 			/*
9262 			 * We need to do an in-place shrink of the array
9263 			 * to match the alignment attributes of the
9264 			 * 32-bit ABI looking at it.
9265 			 */
9266 			/* LINTED: logical expression always true: op "||" */
9267 			ASSERT(sizeof (*src) > sizeof (*dst));
9268 			for (i = 1; i < count; i++)
9269 				bcopy(src + i, dst + i, sizeof (*dst));
9270 		}
9271 #endif
9272 		break;
9273 
9274 	case SIOCSIP6ADDRPOLICY:
9275 		ASSERT(mp->b_prev == NULL);
9276 		mp->b_prev = (void *)q;
9277 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9278 		/*
9279 		 * We pass in the datamodel here so that the ip6_asp_replace()
9280 		 * routine can handle converting from 32-bit to native formats
9281 		 * where necessary.
9282 		 *
9283 		 * A better way to handle this might be to convert the inbound
9284 		 * data structure here, and hang it off a new 'mp'; thus the
9285 		 * ip6_asp_replace() logic would always be dealing with native
9286 		 * format data structures..
9287 		 *
9288 		 * (An even simpler way to handle these ioctls is to just
9289 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9290 		 * and just recompile everything that depends on it.)
9291 		 */
9292 #endif
9293 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9294 		    iocp->ioc_flag & IOC_MODELS);
9295 		return;
9296 	}
9297 
9298 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9299 	qreply(q, mp);
9300 }
9301 
9302 static void
9303 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9304 {
9305 	mblk_t 		*data_mp;
9306 	struct dstinforeq	*dir;
9307 	uint8_t		*end, *cur;
9308 	in6_addr_t	*daddr, *saddr;
9309 	ipaddr_t	v4daddr;
9310 	ire_t		*ire;
9311 	char		*slabel, *dlabel;
9312 	boolean_t	isipv4;
9313 	int		match_ire;
9314 	ill_t		*dst_ill;
9315 	ipif_t		*src_ipif, *ire_ipif;
9316 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9317 	zoneid_t	zoneid;
9318 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9319 
9320 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9321 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9322 
9323 	/*
9324 	 * This ioctl is I_STR only, and must have a
9325 	 * data mblk following the M_IOCTL mblk.
9326 	 */
9327 	data_mp = mp->b_cont;
9328 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9329 		miocnak(q, mp, 0, EINVAL);
9330 		return;
9331 	}
9332 
9333 	if (MBLKL(data_mp) < iocp->ioc_count) {
9334 		mblk_t *new_data_mp;
9335 
9336 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9337 			miocnak(q, mp, 0, ENOMEM);
9338 			return;
9339 		}
9340 		freemsg(data_mp);
9341 		data_mp = new_data_mp;
9342 		mp->b_cont = data_mp;
9343 	}
9344 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9345 
9346 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9347 	    end - cur >= sizeof (struct dstinforeq);
9348 	    cur += sizeof (struct dstinforeq)) {
9349 		dir = (struct dstinforeq *)cur;
9350 		daddr = &dir->dir_daddr;
9351 		saddr = &dir->dir_saddr;
9352 
9353 		/*
9354 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9355 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9356 		 * and ipif_select_source[_v6]() do not.
9357 		 */
9358 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9359 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9360 
9361 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9362 		if (isipv4) {
9363 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9364 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9365 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9366 		} else {
9367 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9368 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9369 		}
9370 		if (ire == NULL) {
9371 			dir->dir_dreachable = 0;
9372 
9373 			/* move on to next dst addr */
9374 			continue;
9375 		}
9376 		dir->dir_dreachable = 1;
9377 
9378 		ire_ipif = ire->ire_ipif;
9379 		if (ire_ipif == NULL)
9380 			goto next_dst;
9381 
9382 		/*
9383 		 * We expect to get back an interface ire or a
9384 		 * gateway ire cache entry.  For both types, the
9385 		 * output interface is ire_ipif->ipif_ill.
9386 		 */
9387 		dst_ill = ire_ipif->ipif_ill;
9388 		dir->dir_dmactype = dst_ill->ill_mactype;
9389 
9390 		if (isipv4) {
9391 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9392 		} else {
9393 			src_ipif = ipif_select_source_v6(dst_ill,
9394 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9395 			    zoneid);
9396 		}
9397 		if (src_ipif == NULL)
9398 			goto next_dst;
9399 
9400 		*saddr = src_ipif->ipif_v6lcl_addr;
9401 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9402 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9403 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9404 		dir->dir_sdeprecated =
9405 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9406 		ipif_refrele(src_ipif);
9407 next_dst:
9408 		ire_refrele(ire);
9409 	}
9410 	miocack(q, mp, iocp->ioc_count, 0);
9411 }
9412 
9413 
9414 /*
9415  * Check if this is an address assigned to this machine.
9416  * Skips interfaces that are down by using ire checks.
9417  * Translates mapped addresses to v4 addresses and then
9418  * treats them as such, returning true if the v4 address
9419  * associated with this mapped address is configured.
9420  * Note: Applications will have to be careful what they do
9421  * with the response; use of mapped addresses limits
9422  * what can be done with the socket, especially with
9423  * respect to socket options and ioctls - neither IPv4
9424  * options nor IPv6 sticky options/ancillary data options
9425  * may be used.
9426  */
9427 /* ARGSUSED */
9428 int
9429 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9430     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9431 {
9432 	struct sioc_addrreq *sia;
9433 	sin_t *sin;
9434 	ire_t *ire;
9435 	mblk_t *mp1;
9436 	zoneid_t zoneid;
9437 	ip_stack_t	*ipst;
9438 
9439 	ip1dbg(("ip_sioctl_tmyaddr"));
9440 
9441 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9442 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9443 	ipst = CONNQ_TO_IPST(q);
9444 
9445 	/* Existence verified in ip_wput_nondata */
9446 	mp1 = mp->b_cont->b_cont;
9447 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9448 	sin = (sin_t *)&sia->sa_addr;
9449 	switch (sin->sin_family) {
9450 	case AF_INET6: {
9451 		sin6_t *sin6 = (sin6_t *)sin;
9452 
9453 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9454 			ipaddr_t v4_addr;
9455 
9456 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9457 			    v4_addr);
9458 			ire = ire_ctable_lookup(v4_addr, 0,
9459 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9460 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9461 		} else {
9462 			in6_addr_t v6addr;
9463 
9464 			v6addr = sin6->sin6_addr;
9465 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9466 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9467 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9468 		}
9469 		break;
9470 	}
9471 	case AF_INET: {
9472 		ipaddr_t v4addr;
9473 
9474 		v4addr = sin->sin_addr.s_addr;
9475 		ire = ire_ctable_lookup(v4addr, 0,
9476 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9477 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9478 		break;
9479 	}
9480 	default:
9481 		return (EAFNOSUPPORT);
9482 	}
9483 	if (ire != NULL) {
9484 		sia->sa_res = 1;
9485 		ire_refrele(ire);
9486 	} else {
9487 		sia->sa_res = 0;
9488 	}
9489 	return (0);
9490 }
9491 
9492 /*
9493  * Check if this is an address assigned on-link i.e. neighbor,
9494  * and makes sure it's reachable from the current zone.
9495  * Returns true for my addresses as well.
9496  * Translates mapped addresses to v4 addresses and then
9497  * treats them as such, returning true if the v4 address
9498  * associated with this mapped address is configured.
9499  * Note: Applications will have to be careful what they do
9500  * with the response; use of mapped addresses limits
9501  * what can be done with the socket, especially with
9502  * respect to socket options and ioctls - neither IPv4
9503  * options nor IPv6 sticky options/ancillary data options
9504  * may be used.
9505  */
9506 /* ARGSUSED */
9507 int
9508 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9509     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9510 {
9511 	struct sioc_addrreq *sia;
9512 	sin_t *sin;
9513 	mblk_t	*mp1;
9514 	ire_t *ire = NULL;
9515 	zoneid_t zoneid;
9516 	ip_stack_t	*ipst;
9517 
9518 	ip1dbg(("ip_sioctl_tonlink"));
9519 
9520 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9521 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9522 	ipst = CONNQ_TO_IPST(q);
9523 
9524 	/* Existence verified in ip_wput_nondata */
9525 	mp1 = mp->b_cont->b_cont;
9526 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9527 	sin = (sin_t *)&sia->sa_addr;
9528 
9529 	/*
9530 	 * Match addresses with a zero gateway field to avoid
9531 	 * routes going through a router.
9532 	 * Exclude broadcast and multicast addresses.
9533 	 */
9534 	switch (sin->sin_family) {
9535 	case AF_INET6: {
9536 		sin6_t *sin6 = (sin6_t *)sin;
9537 
9538 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9539 			ipaddr_t v4_addr;
9540 
9541 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9542 			    v4_addr);
9543 			if (!CLASSD(v4_addr)) {
9544 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9545 				    NULL, NULL, zoneid, NULL,
9546 				    MATCH_IRE_GW, ipst);
9547 			}
9548 		} else {
9549 			in6_addr_t v6addr;
9550 			in6_addr_t v6gw;
9551 
9552 			v6addr = sin6->sin6_addr;
9553 			v6gw = ipv6_all_zeros;
9554 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9555 				ire = ire_route_lookup_v6(&v6addr, 0,
9556 				    &v6gw, 0, NULL, NULL, zoneid,
9557 				    NULL, MATCH_IRE_GW, ipst);
9558 			}
9559 		}
9560 		break;
9561 	}
9562 	case AF_INET: {
9563 		ipaddr_t v4addr;
9564 
9565 		v4addr = sin->sin_addr.s_addr;
9566 		if (!CLASSD(v4addr)) {
9567 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9568 			    NULL, NULL, zoneid, NULL,
9569 			    MATCH_IRE_GW, ipst);
9570 		}
9571 		break;
9572 	}
9573 	default:
9574 		return (EAFNOSUPPORT);
9575 	}
9576 	sia->sa_res = 0;
9577 	if (ire != NULL) {
9578 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9579 		    IRE_LOCAL|IRE_LOOPBACK)) {
9580 			sia->sa_res = 1;
9581 		}
9582 		ire_refrele(ire);
9583 	}
9584 	return (0);
9585 }
9586 
9587 /*
9588  * TBD: implement when kernel maintaines a list of site prefixes.
9589  */
9590 /* ARGSUSED */
9591 int
9592 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9593     ip_ioctl_cmd_t *ipip, void *ifreq)
9594 {
9595 	return (ENXIO);
9596 }
9597 
9598 /* ARGSUSED */
9599 int
9600 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9601     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9602 {
9603 	ill_t  		*ill;
9604 	mblk_t		*mp1;
9605 	conn_t		*connp;
9606 	boolean_t	success;
9607 
9608 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9609 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9610 	/* ioctl comes down on an conn */
9611 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9612 	connp = Q_TO_CONN(q);
9613 
9614 	mp->b_datap->db_type = M_IOCTL;
9615 
9616 	/*
9617 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9618 	 * The original mp contains contaminated b_next values due to 'mi',
9619 	 * which is needed to do the mi_copy_done. Unfortunately if we
9620 	 * send down the original mblk itself and if we are popped due to an
9621 	 * an unplumb before the response comes back from tunnel,
9622 	 * the streamhead (which does a freemsg) will see this contaminated
9623 	 * message and the assertion in freemsg about non-null b_next/b_prev
9624 	 * will panic a DEBUG kernel.
9625 	 */
9626 	mp1 = copymsg(mp);
9627 	if (mp1 == NULL)
9628 		return (ENOMEM);
9629 
9630 	ill = ipif->ipif_ill;
9631 	mutex_enter(&connp->conn_lock);
9632 	mutex_enter(&ill->ill_lock);
9633 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9634 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9635 		    mp, 0);
9636 	} else {
9637 		success = ill_pending_mp_add(ill, connp, mp);
9638 	}
9639 	mutex_exit(&ill->ill_lock);
9640 	mutex_exit(&connp->conn_lock);
9641 
9642 	if (success) {
9643 		ip1dbg(("sending down tunparam request "));
9644 		putnext(ill->ill_wq, mp1);
9645 		return (EINPROGRESS);
9646 	} else {
9647 		/* The conn has started closing */
9648 		freemsg(mp1);
9649 		return (EINTR);
9650 	}
9651 }
9652 
9653 static int
9654 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
9655     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
9656 {
9657 	mblk_t *mp1;
9658 	mblk_t *mp2;
9659 	mblk_t *pending_mp;
9660 	ipaddr_t ipaddr;
9661 	area_t *area;
9662 	struct iocblk *iocp;
9663 	conn_t *connp;
9664 	struct arpreq *ar;
9665 	struct xarpreq *xar;
9666 	boolean_t success;
9667 	int flags, alength;
9668 	char *lladdr;
9669 	ip_stack_t	*ipst;
9670 
9671 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9672 	connp = Q_TO_CONN(q);
9673 	ipst = connp->conn_netstack->netstack_ip;
9674 
9675 	iocp = (struct iocblk *)mp->b_rptr;
9676 	/*
9677 	 * ill has already been set depending on whether
9678 	 * bsd style or interface style ioctl.
9679 	 */
9680 	ASSERT(ill != NULL);
9681 
9682 	/*
9683 	 * Is this one of the new SIOC*XARP ioctls?
9684 	 */
9685 	if (x_arp_ioctl) {
9686 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9687 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9688 		ar = NULL;
9689 
9690 		flags = xar->xarp_flags;
9691 		lladdr = LLADDR(&xar->xarp_ha);
9692 		/*
9693 		 * Validate against user's link layer address length
9694 		 * input and name and addr length limits.
9695 		 */
9696 		alength = ill->ill_phys_addr_length;
9697 		if (iocp->ioc_cmd == SIOCSXARP) {
9698 			if (alength != xar->xarp_ha.sdl_alen ||
9699 			    (alength + xar->xarp_ha.sdl_nlen >
9700 			    sizeof (xar->xarp_ha.sdl_data)))
9701 				return (EINVAL);
9702 		}
9703 	} else {
9704 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9705 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9706 		xar = NULL;
9707 
9708 		flags = ar->arp_flags;
9709 		lladdr = ar->arp_ha.sa_data;
9710 		/*
9711 		 * Theoretically, the sa_family could tell us what link
9712 		 * layer type this operation is trying to deal with. By
9713 		 * common usage AF_UNSPEC means ethernet. We'll assume
9714 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9715 		 * for now. Our new SIOC*XARP ioctls can be used more
9716 		 * generally.
9717 		 *
9718 		 * If the underlying media happens to have a non 6 byte
9719 		 * address, arp module will fail set/get, but the del
9720 		 * operation will succeed.
9721 		 */
9722 		alength = 6;
9723 		if ((iocp->ioc_cmd != SIOCDARP) &&
9724 		    (alength != ill->ill_phys_addr_length)) {
9725 			return (EINVAL);
9726 		}
9727 	}
9728 
9729 	/*
9730 	 * We are going to pass up to ARP a packet chain that looks
9731 	 * like:
9732 	 *
9733 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9734 	 *
9735 	 * Get a copy of the original IOCTL mblk to head the chain,
9736 	 * to be sent up (in mp1). Also get another copy to store
9737 	 * in the ill_pending_mp list, for matching the response
9738 	 * when it comes back from ARP.
9739 	 */
9740 	mp1 = copyb(mp);
9741 	pending_mp = copymsg(mp);
9742 	if (mp1 == NULL || pending_mp == NULL) {
9743 		if (mp1 != NULL)
9744 			freeb(mp1);
9745 		if (pending_mp != NULL)
9746 			inet_freemsg(pending_mp);
9747 		return (ENOMEM);
9748 	}
9749 
9750 	ipaddr = sin->sin_addr.s_addr;
9751 
9752 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9753 	    (caddr_t)&ipaddr);
9754 	if (mp2 == NULL) {
9755 		freeb(mp1);
9756 		inet_freemsg(pending_mp);
9757 		return (ENOMEM);
9758 	}
9759 	/* Put together the chain. */
9760 	mp1->b_cont = mp2;
9761 	mp1->b_datap->db_type = M_IOCTL;
9762 	mp2->b_cont = mp;
9763 	mp2->b_datap->db_type = M_DATA;
9764 
9765 	iocp = (struct iocblk *)mp1->b_rptr;
9766 
9767 	/*
9768 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9769 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9770 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9771 	 * ioc_count field; set ioc_count to be correct.
9772 	 */
9773 	iocp->ioc_count = MBLKL(mp1->b_cont);
9774 
9775 	/*
9776 	 * Set the proper command in the ARP message.
9777 	 * Convert the SIOC{G|S|D}ARP calls into our
9778 	 * AR_ENTRY_xxx calls.
9779 	 */
9780 	area = (area_t *)mp2->b_rptr;
9781 	switch (iocp->ioc_cmd) {
9782 	case SIOCDARP:
9783 	case SIOCDXARP:
9784 		/*
9785 		 * We defer deleting the corresponding IRE until
9786 		 * we return from arp.
9787 		 */
9788 		area->area_cmd = AR_ENTRY_DELETE;
9789 		area->area_proto_mask_offset = 0;
9790 		break;
9791 	case SIOCGARP:
9792 	case SIOCGXARP:
9793 		area->area_cmd = AR_ENTRY_SQUERY;
9794 		area->area_proto_mask_offset = 0;
9795 		break;
9796 	case SIOCSARP:
9797 	case SIOCSXARP: {
9798 		/*
9799 		 * Delete the corresponding ire to make sure IP will
9800 		 * pick up any change from arp.
9801 		 */
9802 		if (!if_arp_ioctl) {
9803 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9804 			break;
9805 		} else {
9806 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9807 			if (ipif != NULL) {
9808 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9809 				    ipst);
9810 				ipif_refrele(ipif);
9811 			}
9812 			break;
9813 		}
9814 	}
9815 	}
9816 	iocp->ioc_cmd = area->area_cmd;
9817 
9818 	/*
9819 	 * Before sending 'mp' to ARP, we have to clear the b_next
9820 	 * and b_prev. Otherwise if STREAMS encounters such a message
9821 	 * in freemsg(), (because ARP can close any time) it can cause
9822 	 * a panic. But mi code needs the b_next and b_prev values of
9823 	 * mp->b_cont, to complete the ioctl. So we store it here
9824 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9825 	 * when the response comes down from ARP.
9826 	 */
9827 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9828 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9829 	mp->b_cont->b_next = NULL;
9830 	mp->b_cont->b_prev = NULL;
9831 
9832 	mutex_enter(&connp->conn_lock);
9833 	mutex_enter(&ill->ill_lock);
9834 	/* conn has not yet started closing, hence this can't fail */
9835 	success = ill_pending_mp_add(ill, connp, pending_mp);
9836 	ASSERT(success);
9837 	mutex_exit(&ill->ill_lock);
9838 	mutex_exit(&connp->conn_lock);
9839 
9840 	/*
9841 	 * Fill in the rest of the ARP operation fields.
9842 	 */
9843 	area->area_hw_addr_length = alength;
9844 	bcopy(lladdr,
9845 	    (char *)area + area->area_hw_addr_offset,
9846 	    area->area_hw_addr_length);
9847 	/* Translate the flags. */
9848 	if (flags & ATF_PERM)
9849 		area->area_flags |= ACE_F_PERMANENT;
9850 	if (flags & ATF_PUBL)
9851 		area->area_flags |= ACE_F_PUBLISH;
9852 	if (flags & ATF_AUTHORITY)
9853 		area->area_flags |= ACE_F_AUTHORITY;
9854 
9855 	/*
9856 	 * Up to ARP it goes.  The response will come
9857 	 * back in ip_wput as an M_IOCACK message, and
9858 	 * will be handed to ip_sioctl_iocack for
9859 	 * completion.
9860 	 */
9861 	putnext(ill->ill_rq, mp1);
9862 	return (EINPROGRESS);
9863 }
9864 
9865 /* ARGSUSED */
9866 int
9867 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9868     ip_ioctl_cmd_t *ipip, void *ifreq)
9869 {
9870 	struct xarpreq *xar;
9871 	boolean_t isv6;
9872 	mblk_t	*mp1;
9873 	int	err;
9874 	conn_t	*connp;
9875 	int ifnamelen;
9876 	ire_t	*ire = NULL;
9877 	ill_t	*ill = NULL;
9878 	struct sockaddr_in *sin;
9879 	boolean_t if_arp_ioctl = B_FALSE;
9880 	ip_stack_t	*ipst;
9881 
9882 	/* ioctl comes down on an conn */
9883 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9884 	connp = Q_TO_CONN(q);
9885 	isv6 = connp->conn_af_isv6;
9886 	ipst = connp->conn_netstack->netstack_ip;
9887 
9888 	/* Existance verified in ip_wput_nondata */
9889 	mp1 = mp->b_cont->b_cont;
9890 
9891 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
9892 	xar = (struct xarpreq *)mp1->b_rptr;
9893 	sin = (sin_t *)&xar->xarp_pa;
9894 
9895 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
9896 	    (xar->xarp_pa.ss_family != AF_INET))
9897 		return (ENXIO);
9898 
9899 	ifnamelen = xar->xarp_ha.sdl_nlen;
9900 	if (ifnamelen != 0) {
9901 		char	*cptr, cval;
9902 
9903 		if (ifnamelen >= LIFNAMSIZ)
9904 			return (EINVAL);
9905 
9906 		/*
9907 		 * Instead of bcopying a bunch of bytes,
9908 		 * null-terminate the string in-situ.
9909 		 */
9910 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9911 		cval = *cptr;
9912 		*cptr = '\0';
9913 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9914 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9915 		    &err, NULL, ipst);
9916 		*cptr = cval;
9917 		if (ill == NULL)
9918 			return (err);
9919 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9920 			ill_refrele(ill);
9921 			return (ENXIO);
9922 		}
9923 
9924 		if_arp_ioctl = B_TRUE;
9925 	} else {
9926 		/*
9927 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9928 		 * as an extended BSD ioctl. The kernel uses the IP address
9929 		 * to figure out the network interface.
9930 		 */
9931 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9932 		    ipst);
9933 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9934 		    ((ill = ire_to_ill(ire)) == NULL) ||
9935 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9936 			if (ire != NULL)
9937 				ire_refrele(ire);
9938 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9939 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9940 			    NULL, MATCH_IRE_TYPE, ipst);
9941 			if ((ire == NULL) ||
9942 			    ((ill = ire_to_ill(ire)) == NULL)) {
9943 				if (ire != NULL)
9944 					ire_refrele(ire);
9945 				return (ENXIO);
9946 			}
9947 		}
9948 		ASSERT(ire != NULL && ill != NULL);
9949 	}
9950 
9951 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9952 	if (if_arp_ioctl)
9953 		ill_refrele(ill);
9954 	if (ire != NULL)
9955 		ire_refrele(ire);
9956 
9957 	return (err);
9958 }
9959 
9960 /*
9961  * ARP IOCTLs.
9962  * How does IP get in the business of fronting ARP configuration/queries?
9963  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9964  * are by tradition passed in through a datagram socket.  That lands in IP.
9965  * As it happens, this is just as well since the interface is quite crude in
9966  * that it passes in no information about protocol or hardware types, or
9967  * interface association.  After making the protocol assumption, IP is in
9968  * the position to look up the name of the ILL, which ARP will need, and
9969  * format a request that can be handled by ARP.	 The request is passed up
9970  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9971  * back a response.  ARP supports its own set of more general IOCTLs, in
9972  * case anyone is interested.
9973  */
9974 /* ARGSUSED */
9975 int
9976 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9977     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9978 {
9979 	struct arpreq *ar;
9980 	struct sockaddr_in *sin;
9981 	ire_t	*ire;
9982 	boolean_t isv6;
9983 	mblk_t	*mp1;
9984 	int	err;
9985 	conn_t	*connp;
9986 	ill_t	*ill;
9987 	ip_stack_t	*ipst;
9988 
9989 	/* ioctl comes down on an conn */
9990 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9991 	connp = Q_TO_CONN(q);
9992 	ipst = CONNQ_TO_IPST(q);
9993 	isv6 = connp->conn_af_isv6;
9994 	if (isv6)
9995 		return (ENXIO);
9996 
9997 	/* Existance verified in ip_wput_nondata */
9998 	mp1 = mp->b_cont->b_cont;
9999 
10000 	ar = (struct arpreq *)mp1->b_rptr;
10001 	sin = (sin_t *)&ar->arp_pa;
10002 
10003 	/*
10004 	 * We need to let ARP know on which interface the IP
10005 	 * address has an ARP mapping. In the IPMP case, a
10006 	 * simple forwarding table lookup will return the
10007 	 * IRE_IF_RESOLVER for the first interface in the group,
10008 	 * which might not be the interface on which the
10009 	 * requested IP address was resolved due to the ill
10010 	 * selection algorithm (see ip_newroute_get_dst_ill()).
10011 	 * So we do a cache table lookup first: if the IRE cache
10012 	 * entry for the IP address is still there, it will
10013 	 * contain the ill pointer for the right interface, so
10014 	 * we use that. If the cache entry has been flushed, we
10015 	 * fall back to the forwarding table lookup. This should
10016 	 * be rare enough since IRE cache entries have a longer
10017 	 * life expectancy than ARP cache entries.
10018 	 */
10019 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL, ipst);
10020 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
10021 	    ((ill = ire_to_ill(ire)) == NULL)) {
10022 		if (ire != NULL)
10023 			ire_refrele(ire);
10024 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
10025 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
10026 		    NULL, MATCH_IRE_TYPE, ipst);
10027 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
10028 			if (ire != NULL)
10029 				ire_refrele(ire);
10030 			return (ENXIO);
10031 		}
10032 	}
10033 	ASSERT(ire != NULL && ill != NULL);
10034 
10035 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
10036 	ire_refrele(ire);
10037 	return (err);
10038 }
10039 
10040 /*
10041  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
10042  * atomically set/clear the muxids. Also complete the ioctl by acking or
10043  * naking it.  Note that the code is structured such that the link type,
10044  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
10045  * its clones use the persistent link, while pppd(1M) and perhaps many
10046  * other daemons may use non-persistent link.  When combined with some
10047  * ill_t states, linking and unlinking lower streams may be used as
10048  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
10049  */
10050 /* ARGSUSED */
10051 void
10052 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
10053 {
10054 	mblk_t *mp1;
10055 	mblk_t *mp2;
10056 	struct linkblk *li;
10057 	queue_t	*ipwq;
10058 	char	*name;
10059 	struct qinit *qinfo;
10060 	struct ipmx_s *ipmxp;
10061 	ill_t	*ill = NULL;
10062 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10063 	int	err = 0;
10064 	boolean_t	entered_ipsq = B_FALSE;
10065 	boolean_t islink;
10066 	queue_t *dwq = NULL;
10067 	ip_stack_t	*ipst;
10068 
10069 	if (CONN_Q(q))
10070 		ipst = CONNQ_TO_IPST(q);
10071 	else
10072 		ipst = ILLQ_TO_IPST(q);
10073 
10074 	ASSERT(iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_PUNLINK ||
10075 	    iocp->ioc_cmd == I_LINK || iocp->ioc_cmd == I_UNLINK);
10076 
10077 	islink = (iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_LINK) ?
10078 	    B_TRUE : B_FALSE;
10079 
10080 	mp1 = mp->b_cont;	/* This is the linkblk info */
10081 	li = (struct linkblk *)mp1->b_rptr;
10082 
10083 	/*
10084 	 * ARP has added this special mblk, and the utility is asking us
10085 	 * to perform consistency checks, and also atomically set the
10086 	 * muxid. Ifconfig is an example.  It achieves this by using
10087 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
10088 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
10089 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
10090 	 * and other comments in this routine for more details.
10091 	 */
10092 	mp2 = mp1->b_cont;	/* This is added by ARP */
10093 
10094 	/*
10095 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
10096 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
10097 	 * get the special mblk above.  For backward compatibility, we just
10098 	 * return success.  The utility will use SIOCSLIFMUXID to store
10099 	 * the muxids.  This is not atomic, and can leave the streams
10100 	 * unplumbable if the utility is interrrupted, before it does the
10101 	 * SIOCSLIFMUXID.
10102 	 */
10103 	if (mp2 == NULL) {
10104 		/*
10105 		 * At this point we don't know whether or not this is the
10106 		 * IP module stream or the ARP device stream.  We need to
10107 		 * walk the lower stream in order to find this out, since
10108 		 * the capability negotiation is done only on the IP module
10109 		 * stream.  IP module instance is identified by the module
10110 		 * name IP, non-null q_next, and it's wput not being ip_lwput.
10111 		 * STREAMS ensures that the lower stream (l_qbot) will not
10112 		 * vanish until this ioctl completes. So we can safely walk
10113 		 * the stream or refer to the q_ptr.
10114 		 */
10115 		ipwq = li->l_qbot;
10116 		while (ipwq != NULL) {
10117 			qinfo = ipwq->q_qinfo;
10118 			name = qinfo->qi_minfo->mi_idname;
10119 			if (name != NULL && name[0] != NULL &&
10120 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
10121 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
10122 			    (ipwq->q_next != NULL)) {
10123 				break;
10124 			}
10125 			ipwq = ipwq->q_next;
10126 		}
10127 		/*
10128 		 * This looks like an IP module stream, so trigger
10129 		 * the capability reset or re-negotiation if necessary.
10130 		 */
10131 		if (ipwq != NULL) {
10132 			ill = ipwq->q_ptr;
10133 			ASSERT(ill != NULL);
10134 
10135 			if (ipsq == NULL) {
10136 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
10137 				    ip_sioctl_plink, NEW_OP, B_TRUE);
10138 				if (ipsq == NULL)
10139 					return;
10140 				entered_ipsq = B_TRUE;
10141 			}
10142 			ASSERT(IAM_WRITER_ILL(ill));
10143 			/*
10144 			 * Store the upper read queue of the module
10145 			 * immediately below IP, and count the total
10146 			 * number of lower modules.  Do this only
10147 			 * for I_PLINK or I_LINK event.
10148 			 */
10149 			ill->ill_lmod_rq = NULL;
10150 			ill->ill_lmod_cnt = 0;
10151 			if (islink && (dwq = ipwq->q_next) != NULL) {
10152 				ill->ill_lmod_rq = RD(dwq);
10153 
10154 				while (dwq != NULL) {
10155 					ill->ill_lmod_cnt++;
10156 					dwq = dwq->q_next;
10157 				}
10158 			}
10159 			/*
10160 			 * There's no point in resetting or re-negotiating if
10161 			 * we are not bound to the driver, so only do this if
10162 			 * the DLPI state is idle (up); we assume such state
10163 			 * since ill_ipif_up_count gets incremented in
10164 			 * ipif_up_done(), which is after we are bound to the
10165 			 * driver.  Note that in the case of logical
10166 			 * interfaces, IP won't rebind to the driver unless
10167 			 * the ill_ipif_up_count is 0, meaning that all other
10168 			 * IP interfaces (including the main ipif) are in the
10169 			 * down state.  Because of this, we use such counter
10170 			 * as an indicator, instead of relying on the IPIF_UP
10171 			 * flag, which is per ipif instance.
10172 			 */
10173 			if (ill->ill_ipif_up_count > 0) {
10174 				if (islink)
10175 					ill_capability_probe(ill);
10176 				else
10177 					ill_capability_reset(ill);
10178 			}
10179 		}
10180 		goto done;
10181 	}
10182 
10183 	/*
10184 	 * This is an I_{P}LINK sent down by ifconfig on
10185 	 * /dev/arp. ARP has appended this last (3rd) mblk,
10186 	 * giving more info. STREAMS ensures that the lower
10187 	 * stream (l_qbot) will not vanish until this ioctl
10188 	 * completes. So we can safely walk the stream or refer
10189 	 * to the q_ptr.
10190 	 */
10191 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
10192 	if (ipmxp->ipmx_arpdev_stream) {
10193 		/*
10194 		 * The operation is occuring on the arp-device
10195 		 * stream.
10196 		 */
10197 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
10198 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
10199 		if (ill == NULL) {
10200 			if (err == EINPROGRESS) {
10201 				return;
10202 			} else {
10203 				err = EINVAL;
10204 				goto done;
10205 			}
10206 		}
10207 
10208 		if (ipsq == NULL) {
10209 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10210 			    NEW_OP, B_TRUE);
10211 			if (ipsq == NULL) {
10212 				ill_refrele(ill);
10213 				return;
10214 			}
10215 			entered_ipsq = B_TRUE;
10216 		}
10217 		ASSERT(IAM_WRITER_ILL(ill));
10218 		ill_refrele(ill);
10219 		/*
10220 		 * To ensure consistency between IP and ARP,
10221 		 * the following LIFO scheme is used in
10222 		 * plink/punlink. (IP first, ARP last).
10223 		 * This is because the muxid's are stored
10224 		 * in the IP stream on the ill.
10225 		 *
10226 		 * I_{P}LINK: ifconfig plinks the IP stream before
10227 		 * plinking the ARP stream. On an arp-dev
10228 		 * stream, IP checks that it is not yet
10229 		 * plinked, and it also checks that the
10230 		 * corresponding IP stream is already plinked.
10231 		 *
10232 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream
10233 		 * before punlinking the IP stream. IP does
10234 		 * not allow punlink of the IP stream unless
10235 		 * the arp stream has been punlinked.
10236 		 *
10237 		 */
10238 		if ((islink &&
10239 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
10240 		    (!islink &&
10241 		    ill->ill_arp_muxid != li->l_index)) {
10242 			err = EINVAL;
10243 			goto done;
10244 		}
10245 		if (islink) {
10246 			ill->ill_arp_muxid = li->l_index;
10247 		} else {
10248 			ill->ill_arp_muxid = 0;
10249 		}
10250 	} else {
10251 		/*
10252 		 * This must be the IP module stream with or
10253 		 * without arp. Walk the stream and locate the
10254 		 * IP module. An IP module instance is
10255 		 * identified by the module name IP, non-null
10256 		 * q_next, and it's wput not being ip_lwput.
10257 		 */
10258 		ipwq = li->l_qbot;
10259 		while (ipwq != NULL) {
10260 			qinfo = ipwq->q_qinfo;
10261 			name = qinfo->qi_minfo->mi_idname;
10262 			if (name != NULL && name[0] != NULL &&
10263 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
10264 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
10265 			    (ipwq->q_next != NULL)) {
10266 				break;
10267 			}
10268 			ipwq = ipwq->q_next;
10269 		}
10270 		if (ipwq != NULL) {
10271 			ill = ipwq->q_ptr;
10272 			ASSERT(ill != NULL);
10273 
10274 			if (ipsq == NULL) {
10275 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
10276 				    ip_sioctl_plink, NEW_OP, B_TRUE);
10277 				if (ipsq == NULL)
10278 					return;
10279 				entered_ipsq = B_TRUE;
10280 			}
10281 			ASSERT(IAM_WRITER_ILL(ill));
10282 			/*
10283 			 * Return error if the ip_mux_id is
10284 			 * non-zero and command is I_{P}LINK.
10285 			 * If command is I_{P}UNLINK, return
10286 			 * error if the arp-devstr is not
10287 			 * yet punlinked.
10288 			 */
10289 			if ((islink && ill->ill_ip_muxid != 0) ||
10290 			    (!islink && ill->ill_arp_muxid != 0)) {
10291 				err = EINVAL;
10292 				goto done;
10293 			}
10294 			ill->ill_lmod_rq = NULL;
10295 			ill->ill_lmod_cnt = 0;
10296 			if (islink) {
10297 				/*
10298 				 * Store the upper read queue of the module
10299 				 * immediately below IP, and count the total
10300 				 * number of lower modules.
10301 				 */
10302 				if ((dwq = ipwq->q_next) != NULL) {
10303 					ill->ill_lmod_rq = RD(dwq);
10304 
10305 					while (dwq != NULL) {
10306 						ill->ill_lmod_cnt++;
10307 						dwq = dwq->q_next;
10308 					}
10309 				}
10310 				ill->ill_ip_muxid = li->l_index;
10311 			} else {
10312 				ill->ill_ip_muxid = 0;
10313 			}
10314 
10315 			/*
10316 			 * See comments above about resetting/re-
10317 			 * negotiating driver sub-capabilities.
10318 			 */
10319 			if (ill->ill_ipif_up_count > 0) {
10320 				if (islink)
10321 					ill_capability_probe(ill);
10322 				else
10323 					ill_capability_reset(ill);
10324 			}
10325 		}
10326 	}
10327 done:
10328 	iocp->ioc_count = 0;
10329 	iocp->ioc_error = err;
10330 	if (err == 0)
10331 		mp->b_datap->db_type = M_IOCACK;
10332 	else
10333 		mp->b_datap->db_type = M_IOCNAK;
10334 	qreply(q, mp);
10335 
10336 	/* Conn was refheld in ip_sioctl_copyin_setup */
10337 	if (CONN_Q(q))
10338 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
10339 	if (entered_ipsq)
10340 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10341 }
10342 
10343 /*
10344  * Search the ioctl command in the ioctl tables and return a pointer
10345  * to the ioctl command information. The ioctl command tables are
10346  * static and fully populated at compile time.
10347  */
10348 ip_ioctl_cmd_t *
10349 ip_sioctl_lookup(int ioc_cmd)
10350 {
10351 	int index;
10352 	ip_ioctl_cmd_t *ipip;
10353 	ip_ioctl_cmd_t *ipip_end;
10354 
10355 	if (ioc_cmd == IPI_DONTCARE)
10356 		return (NULL);
10357 
10358 	/*
10359 	 * Do a 2 step search. First search the indexed table
10360 	 * based on the least significant byte of the ioctl cmd.
10361 	 * If we don't find a match, then search the misc table
10362 	 * serially.
10363 	 */
10364 	index = ioc_cmd & 0xFF;
10365 	if (index < ip_ndx_ioctl_count) {
10366 		ipip = &ip_ndx_ioctl_table[index];
10367 		if (ipip->ipi_cmd == ioc_cmd) {
10368 			/* Found a match in the ndx table */
10369 			return (ipip);
10370 		}
10371 	}
10372 
10373 	/* Search the misc table */
10374 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10375 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10376 		if (ipip->ipi_cmd == ioc_cmd)
10377 			/* Found a match in the misc table */
10378 			return (ipip);
10379 	}
10380 
10381 	return (NULL);
10382 }
10383 
10384 /*
10385  * Wrapper function for resuming deferred ioctl processing
10386  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10387  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10388  */
10389 /* ARGSUSED */
10390 void
10391 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10392     void *dummy_arg)
10393 {
10394 	ip_sioctl_copyin_setup(q, mp);
10395 }
10396 
10397 /*
10398  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10399  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10400  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10401  * We establish here the size of the block to be copied in.  mi_copyin
10402  * arranges for this to happen, an processing continues in ip_wput with
10403  * an M_IOCDATA message.
10404  */
10405 void
10406 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10407 {
10408 	int	copyin_size;
10409 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10410 	ip_ioctl_cmd_t *ipip;
10411 	cred_t *cr;
10412 	ip_stack_t	*ipst;
10413 
10414 	if (CONN_Q(q))
10415 		ipst = CONNQ_TO_IPST(q);
10416 	else
10417 		ipst = ILLQ_TO_IPST(q);
10418 
10419 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10420 	if (ipip == NULL) {
10421 		/*
10422 		 * The ioctl is not one we understand or own.
10423 		 * Pass it along to be processed down stream,
10424 		 * if this is a module instance of IP, else nak
10425 		 * the ioctl.
10426 		 */
10427 		if (q->q_next == NULL) {
10428 			goto nak;
10429 		} else {
10430 			putnext(q, mp);
10431 			return;
10432 		}
10433 	}
10434 
10435 	/*
10436 	 * If this is deferred, then we will do all the checks when we
10437 	 * come back.
10438 	 */
10439 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10440 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10441 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10442 		return;
10443 	}
10444 
10445 	/*
10446 	 * Only allow a very small subset of IP ioctls on this stream if
10447 	 * IP is a module and not a driver. Allowing ioctls to be processed
10448 	 * in this case may cause assert failures or data corruption.
10449 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10450 	 * ioctls allowed on an IP module stream, after which this stream
10451 	 * normally becomes a multiplexor (at which time the stream head
10452 	 * will fail all ioctls).
10453 	 */
10454 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10455 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10456 			/*
10457 			 * Pass common Streams ioctls which the IP
10458 			 * module does not own or consume along to
10459 			 * be processed down stream.
10460 			 */
10461 			putnext(q, mp);
10462 			return;
10463 		} else {
10464 			goto nak;
10465 		}
10466 	}
10467 
10468 	/* Make sure we have ioctl data to process. */
10469 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10470 		goto nak;
10471 
10472 	/*
10473 	 * Prefer dblk credential over ioctl credential; some synthesized
10474 	 * ioctls have kcred set because there's no way to crhold()
10475 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10476 	 * the framework; the caller of ioctl needs to hold the reference
10477 	 * for the duration of the call).
10478 	 */
10479 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10480 
10481 	/* Make sure normal users don't send down privileged ioctls */
10482 	if ((ipip->ipi_flags & IPI_PRIV) &&
10483 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10484 		/* We checked the privilege earlier but log it here */
10485 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10486 		return;
10487 	}
10488 
10489 	/*
10490 	 * The ioctl command tables can only encode fixed length
10491 	 * ioctl data. If the length is variable, the table will
10492 	 * encode the length as zero. Such special cases are handled
10493 	 * below in the switch.
10494 	 */
10495 	if (ipip->ipi_copyin_size != 0) {
10496 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10497 		return;
10498 	}
10499 
10500 	switch (iocp->ioc_cmd) {
10501 	case O_SIOCGIFCONF:
10502 	case SIOCGIFCONF:
10503 		/*
10504 		 * This IOCTL is hilarious.  See comments in
10505 		 * ip_sioctl_get_ifconf for the story.
10506 		 */
10507 		if (iocp->ioc_count == TRANSPARENT)
10508 			copyin_size = SIZEOF_STRUCT(ifconf,
10509 			    iocp->ioc_flag);
10510 		else
10511 			copyin_size = iocp->ioc_count;
10512 		mi_copyin(q, mp, NULL, copyin_size);
10513 		return;
10514 
10515 	case O_SIOCGLIFCONF:
10516 	case SIOCGLIFCONF:
10517 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10518 		mi_copyin(q, mp, NULL, copyin_size);
10519 		return;
10520 
10521 	case SIOCGLIFSRCOF:
10522 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10523 		mi_copyin(q, mp, NULL, copyin_size);
10524 		return;
10525 	case SIOCGIP6ADDRPOLICY:
10526 		ip_sioctl_ip6addrpolicy(q, mp);
10527 		ip6_asp_table_refrele(ipst);
10528 		return;
10529 
10530 	case SIOCSIP6ADDRPOLICY:
10531 		ip_sioctl_ip6addrpolicy(q, mp);
10532 		return;
10533 
10534 	case SIOCGDSTINFO:
10535 		ip_sioctl_dstinfo(q, mp);
10536 		ip6_asp_table_refrele(ipst);
10537 		return;
10538 
10539 	case I_PLINK:
10540 	case I_PUNLINK:
10541 	case I_LINK:
10542 	case I_UNLINK:
10543 		/*
10544 		 * We treat non-persistent link similarly as the persistent
10545 		 * link case, in terms of plumbing/unplumbing, as well as
10546 		 * dynamic re-plumbing events indicator.  See comments
10547 		 * in ip_sioctl_plink() for more.
10548 		 *
10549 		 * Request can be enqueued in the 'ipsq' while waiting
10550 		 * to become exclusive. So bump up the conn ref.
10551 		 */
10552 		if (CONN_Q(q))
10553 			CONN_INC_REF(Q_TO_CONN(q));
10554 		ip_sioctl_plink(NULL, q, mp, NULL);
10555 		return;
10556 
10557 	case ND_GET:
10558 	case ND_SET:
10559 		/*
10560 		 * Use of the nd table requires holding the reader lock.
10561 		 * Modifying the nd table thru nd_load/nd_unload requires
10562 		 * the writer lock.
10563 		 */
10564 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10565 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10566 			rw_exit(&ipst->ips_ip_g_nd_lock);
10567 
10568 			if (iocp->ioc_error)
10569 				iocp->ioc_count = 0;
10570 			mp->b_datap->db_type = M_IOCACK;
10571 			qreply(q, mp);
10572 			return;
10573 		}
10574 		rw_exit(&ipst->ips_ip_g_nd_lock);
10575 		/*
10576 		 * We don't understand this subioctl of ND_GET / ND_SET.
10577 		 * Maybe intended for some driver / module below us
10578 		 */
10579 		if (q->q_next) {
10580 			putnext(q, mp);
10581 		} else {
10582 			iocp->ioc_error = ENOENT;
10583 			mp->b_datap->db_type = M_IOCNAK;
10584 			iocp->ioc_count = 0;
10585 			qreply(q, mp);
10586 		}
10587 		return;
10588 
10589 	case IP_IOCTL:
10590 		ip_wput_ioctl(q, mp);
10591 		return;
10592 	default:
10593 		cmn_err(CE_PANIC, "should not happen ");
10594 	}
10595 nak:
10596 	if (mp->b_cont != NULL) {
10597 		freemsg(mp->b_cont);
10598 		mp->b_cont = NULL;
10599 	}
10600 	iocp->ioc_error = EINVAL;
10601 	mp->b_datap->db_type = M_IOCNAK;
10602 	iocp->ioc_count = 0;
10603 	qreply(q, mp);
10604 }
10605 
10606 /* ip_wput hands off ARP IOCTL responses to us */
10607 void
10608 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10609 {
10610 	struct arpreq *ar;
10611 	struct xarpreq *xar;
10612 	area_t	*area;
10613 	mblk_t	*area_mp;
10614 	struct iocblk *iocp;
10615 	mblk_t	*orig_ioc_mp, *tmp;
10616 	struct iocblk	*orig_iocp;
10617 	ill_t *ill;
10618 	conn_t *connp = NULL;
10619 	uint_t ioc_id;
10620 	mblk_t *pending_mp;
10621 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10622 	int *flagsp;
10623 	char *storage = NULL;
10624 	sin_t *sin;
10625 	ipaddr_t addr;
10626 	int err;
10627 	ip_stack_t *ipst;
10628 
10629 	ill = q->q_ptr;
10630 	ASSERT(ill != NULL);
10631 	ipst = ill->ill_ipst;
10632 
10633 	/*
10634 	 * We should get back from ARP a packet chain that looks like:
10635 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10636 	 */
10637 	if (!(area_mp = mp->b_cont) ||
10638 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10639 	    !(orig_ioc_mp = area_mp->b_cont) ||
10640 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10641 		freemsg(mp);
10642 		return;
10643 	}
10644 
10645 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10646 
10647 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10648 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10649 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10650 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10651 		x_arp_ioctl = B_TRUE;
10652 		xar = (struct xarpreq *)tmp->b_rptr;
10653 		sin = (sin_t *)&xar->xarp_pa;
10654 		flagsp = &xar->xarp_flags;
10655 		storage = xar->xarp_ha.sdl_data;
10656 		if (xar->xarp_ha.sdl_nlen != 0)
10657 			ifx_arp_ioctl = B_TRUE;
10658 	} else {
10659 		ar = (struct arpreq *)tmp->b_rptr;
10660 		sin = (sin_t *)&ar->arp_pa;
10661 		flagsp = &ar->arp_flags;
10662 		storage = ar->arp_ha.sa_data;
10663 	}
10664 
10665 	iocp = (struct iocblk *)mp->b_rptr;
10666 
10667 	/*
10668 	 * Pick out the originating queue based on the ioc_id.
10669 	 */
10670 	ioc_id = iocp->ioc_id;
10671 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10672 	if (pending_mp == NULL) {
10673 		ASSERT(connp == NULL);
10674 		inet_freemsg(mp);
10675 		return;
10676 	}
10677 	ASSERT(connp != NULL);
10678 	q = CONNP_TO_WQ(connp);
10679 
10680 	/* Uncouple the internally generated IOCTL from the original one */
10681 	area = (area_t *)area_mp->b_rptr;
10682 	area_mp->b_cont = NULL;
10683 
10684 	/*
10685 	 * Restore the b_next and b_prev used by mi code. This is needed
10686 	 * to complete the ioctl using mi* functions. We stored them in
10687 	 * the pending mp prior to sending the request to ARP.
10688 	 */
10689 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10690 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10691 	inet_freemsg(pending_mp);
10692 
10693 	/*
10694 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10695 	 * Catch the case where there is an IRE_CACHE by no entry in the
10696 	 * arp table.
10697 	 */
10698 	addr = sin->sin_addr.s_addr;
10699 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10700 		ire_t			*ire;
10701 		dl_unitdata_req_t	*dlup;
10702 		mblk_t			*llmp;
10703 		int			addr_len;
10704 		ill_t			*ipsqill = NULL;
10705 
10706 		if (ifx_arp_ioctl) {
10707 			/*
10708 			 * There's no need to lookup the ill, since
10709 			 * we've already done that when we started
10710 			 * processing the ioctl and sent the message
10711 			 * to ARP on that ill.  So use the ill that
10712 			 * is stored in q->q_ptr.
10713 			 */
10714 			ipsqill = ill;
10715 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10716 			    ipsqill->ill_ipif, ALL_ZONES,
10717 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10718 		} else {
10719 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10720 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10721 			if (ire != NULL)
10722 				ipsqill = ire_to_ill(ire);
10723 		}
10724 
10725 		if ((x_arp_ioctl) && (ipsqill != NULL))
10726 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10727 
10728 		if (ire != NULL) {
10729 			/*
10730 			 * Since the ire obtained from cachetable is used for
10731 			 * mac addr copying below, treat an incomplete ire as if
10732 			 * as if we never found it.
10733 			 */
10734 			if (ire->ire_nce != NULL &&
10735 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10736 				ire_refrele(ire);
10737 				ire = NULL;
10738 				ipsqill = NULL;
10739 				goto errack;
10740 			}
10741 			*flagsp = ATF_INUSE;
10742 			llmp = (ire->ire_nce != NULL ?
10743 			    ire->ire_nce->nce_res_mp : NULL);
10744 			if (llmp != NULL && ipsqill != NULL) {
10745 				uchar_t *macaddr;
10746 
10747 				addr_len = ipsqill->ill_phys_addr_length;
10748 				if (x_arp_ioctl && ((addr_len +
10749 				    ipsqill->ill_name_length) >
10750 				    sizeof (xar->xarp_ha.sdl_data))) {
10751 					ire_refrele(ire);
10752 					freemsg(mp);
10753 					ip_ioctl_finish(q, orig_ioc_mp,
10754 					    EINVAL, NO_COPYOUT, NULL);
10755 					return;
10756 				}
10757 				*flagsp |= ATF_COM;
10758 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10759 				if (ipsqill->ill_sap_length < 0)
10760 					macaddr = llmp->b_rptr +
10761 					    dlup->dl_dest_addr_offset;
10762 				else
10763 					macaddr = llmp->b_rptr +
10764 					    dlup->dl_dest_addr_offset +
10765 					    ipsqill->ill_sap_length;
10766 				/*
10767 				 * For SIOCGARP, MAC address length
10768 				 * validation has already been done
10769 				 * before the ioctl was issued to ARP to
10770 				 * allow it to progress only on 6 byte
10771 				 * addressable (ethernet like) media. Thus
10772 				 * the mac address copying can not overwrite
10773 				 * the sa_data area below.
10774 				 */
10775 				bcopy(macaddr, storage, addr_len);
10776 			}
10777 			/* Ditch the internal IOCTL. */
10778 			freemsg(mp);
10779 			ire_refrele(ire);
10780 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10781 			return;
10782 		}
10783 	}
10784 
10785 	/*
10786 	 * Delete the coresponding IRE_CACHE if any.
10787 	 * Reset the error if there was one (in case there was no entry
10788 	 * in arp.)
10789 	 */
10790 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10791 		ipif_t *ipintf = NULL;
10792 
10793 		if (ifx_arp_ioctl) {
10794 			/*
10795 			 * There's no need to lookup the ill, since
10796 			 * we've already done that when we started
10797 			 * processing the ioctl and sent the message
10798 			 * to ARP on that ill.  So use the ill that
10799 			 * is stored in q->q_ptr.
10800 			 */
10801 			ipintf = ill->ill_ipif;
10802 		}
10803 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10804 			/*
10805 			 * The address in "addr" may be an entry for a
10806 			 * router. If that's true, then any off-net
10807 			 * IRE_CACHE entries that go through the router
10808 			 * with address "addr" must be clobbered. Use
10809 			 * ire_walk to achieve this goal.
10810 			 */
10811 			if (ifx_arp_ioctl)
10812 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10813 				    ire_delete_cache_gw, (char *)&addr, ill);
10814 			else
10815 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10816 				    ALL_ZONES, ipst);
10817 			iocp->ioc_error = 0;
10818 		}
10819 	}
10820 errack:
10821 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10822 		err = iocp->ioc_error;
10823 		freemsg(mp);
10824 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10825 		return;
10826 	}
10827 
10828 	/*
10829 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10830 	 * the area_t into the struct {x}arpreq.
10831 	 */
10832 	if (x_arp_ioctl) {
10833 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10834 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10835 		    sizeof (xar->xarp_ha.sdl_data)) {
10836 			freemsg(mp);
10837 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10838 			    NULL);
10839 			return;
10840 		}
10841 	}
10842 	*flagsp = ATF_INUSE;
10843 	if (area->area_flags & ACE_F_PERMANENT)
10844 		*flagsp |= ATF_PERM;
10845 	if (area->area_flags & ACE_F_PUBLISH)
10846 		*flagsp |= ATF_PUBL;
10847 	if (area->area_flags & ACE_F_AUTHORITY)
10848 		*flagsp |= ATF_AUTHORITY;
10849 	if (area->area_hw_addr_length != 0) {
10850 		*flagsp |= ATF_COM;
10851 		/*
10852 		 * For SIOCGARP, MAC address length validation has
10853 		 * already been done before the ioctl was issued to ARP
10854 		 * to allow it to progress only on 6 byte addressable
10855 		 * (ethernet like) media. Thus the mac address copying
10856 		 * can not overwrite the sa_data area below.
10857 		 */
10858 		bcopy((char *)area + area->area_hw_addr_offset,
10859 		    storage, area->area_hw_addr_length);
10860 	}
10861 
10862 	/* Ditch the internal IOCTL. */
10863 	freemsg(mp);
10864 	/* Complete the original. */
10865 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10866 }
10867 
10868 /*
10869  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10870  * interface) create the next available logical interface for this
10871  * physical interface.
10872  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10873  * ipif with the specified name.
10874  *
10875  * If the address family is not AF_UNSPEC then set the address as well.
10876  *
10877  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10878  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10879  *
10880  * Executed as a writer on the ill or ill group.
10881  * So no lock is needed to traverse the ipif chain, or examine the
10882  * phyint flags.
10883  */
10884 /* ARGSUSED */
10885 int
10886 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10887     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10888 {
10889 	mblk_t	*mp1;
10890 	struct lifreq *lifr;
10891 	boolean_t	isv6;
10892 	boolean_t	exists;
10893 	char 	*name;
10894 	char	*endp;
10895 	char	*cp;
10896 	int	namelen;
10897 	ipif_t	*ipif;
10898 	long	id;
10899 	ipsq_t	*ipsq;
10900 	ill_t	*ill;
10901 	sin_t	*sin;
10902 	int	err = 0;
10903 	boolean_t found_sep = B_FALSE;
10904 	conn_t	*connp;
10905 	zoneid_t zoneid;
10906 	int	orig_ifindex = 0;
10907 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10908 
10909 	ASSERT(q->q_next == NULL);
10910 	ip1dbg(("ip_sioctl_addif\n"));
10911 	/* Existence of mp1 has been checked in ip_wput_nondata */
10912 	mp1 = mp->b_cont->b_cont;
10913 	/*
10914 	 * Null terminate the string to protect against buffer
10915 	 * overrun. String was generated by user code and may not
10916 	 * be trusted.
10917 	 */
10918 	lifr = (struct lifreq *)mp1->b_rptr;
10919 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10920 	name = lifr->lifr_name;
10921 	ASSERT(CONN_Q(q));
10922 	connp = Q_TO_CONN(q);
10923 	isv6 = connp->conn_af_isv6;
10924 	zoneid = connp->conn_zoneid;
10925 	namelen = mi_strlen(name);
10926 	if (namelen == 0)
10927 		return (EINVAL);
10928 
10929 	exists = B_FALSE;
10930 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10931 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10932 		/*
10933 		 * Allow creating lo0 using SIOCLIFADDIF.
10934 		 * can't be any other writer thread. So can pass null below
10935 		 * for the last 4 args to ipif_lookup_name.
10936 		 */
10937 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10938 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10939 		/* Prevent any further action */
10940 		if (ipif == NULL) {
10941 			return (ENOBUFS);
10942 		} else if (!exists) {
10943 			/* We created the ipif now and as writer */
10944 			ipif_refrele(ipif);
10945 			return (0);
10946 		} else {
10947 			ill = ipif->ipif_ill;
10948 			ill_refhold(ill);
10949 			ipif_refrele(ipif);
10950 		}
10951 	} else {
10952 		/* Look for a colon in the name. */
10953 		endp = &name[namelen];
10954 		for (cp = endp; --cp > name; ) {
10955 			if (*cp == IPIF_SEPARATOR_CHAR) {
10956 				found_sep = B_TRUE;
10957 				/*
10958 				 * Reject any non-decimal aliases for plumbing
10959 				 * of logical interfaces. Aliases with leading
10960 				 * zeroes are also rejected as they introduce
10961 				 * ambiguity in the naming of the interfaces.
10962 				 * Comparing with "0" takes care of all such
10963 				 * cases.
10964 				 */
10965 				if ((strncmp("0", cp+1, 1)) == 0)
10966 					return (EINVAL);
10967 
10968 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10969 				    id <= 0 || *endp != '\0') {
10970 					return (EINVAL);
10971 				}
10972 				*cp = '\0';
10973 				break;
10974 			}
10975 		}
10976 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10977 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10978 		if (found_sep)
10979 			*cp = IPIF_SEPARATOR_CHAR;
10980 		if (ill == NULL)
10981 			return (err);
10982 	}
10983 
10984 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10985 	    B_TRUE);
10986 
10987 	/*
10988 	 * Release the refhold due to the lookup, now that we are excl
10989 	 * or we are just returning
10990 	 */
10991 	ill_refrele(ill);
10992 
10993 	if (ipsq == NULL)
10994 		return (EINPROGRESS);
10995 
10996 	/*
10997 	 * If the interface is failed, inactive or offlined, look for a working
10998 	 * interface in the ill group and create the ipif there. If we can't
10999 	 * find a good interface, create the ipif anyway so that in.mpathd can
11000 	 * move it to the first repaired interface.
11001 	 */
11002 	if ((ill->ill_phyint->phyint_flags &
11003 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
11004 	    ill->ill_phyint->phyint_groupname_len != 0) {
11005 		phyint_t *phyi;
11006 		char *groupname = ill->ill_phyint->phyint_groupname;
11007 
11008 		/*
11009 		 * We're looking for a working interface, but it doesn't matter
11010 		 * if it's up or down; so instead of following the group lists,
11011 		 * we look at each physical interface and compare the groupname.
11012 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
11013 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
11014 		 * Otherwise we create the ipif on the failed interface.
11015 		 */
11016 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11017 		phyi = avl_first(&ipst->ips_phyint_g_list->
11018 		    phyint_list_avl_by_index);
11019 		for (; phyi != NULL;
11020 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
11021 		    phyint_list_avl_by_index,
11022 		    phyi, AVL_AFTER)) {
11023 			if (phyi->phyint_groupname_len == 0)
11024 				continue;
11025 			ASSERT(phyi->phyint_groupname != NULL);
11026 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
11027 			    !(phyi->phyint_flags &
11028 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
11029 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
11030 			    (phyi->phyint_illv4 != NULL))) {
11031 				break;
11032 			}
11033 		}
11034 		rw_exit(&ipst->ips_ill_g_lock);
11035 
11036 		if (phyi != NULL) {
11037 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
11038 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
11039 			    phyi->phyint_illv4);
11040 		}
11041 	}
11042 
11043 	/*
11044 	 * We are now exclusive on the ipsq, so an ill move will be serialized
11045 	 * before or after us.
11046 	 */
11047 	ASSERT(IAM_WRITER_ILL(ill));
11048 	ASSERT(ill->ill_move_in_progress == B_FALSE);
11049 
11050 	if (found_sep && orig_ifindex == 0) {
11051 		/* Now see if there is an IPIF with this unit number. */
11052 		for (ipif = ill->ill_ipif; ipif != NULL;
11053 		    ipif = ipif->ipif_next) {
11054 			if (ipif->ipif_id == id) {
11055 				err = EEXIST;
11056 				goto done;
11057 			}
11058 		}
11059 	}
11060 
11061 	/*
11062 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
11063 	 * of lo0. We never come here when we plumb lo0:0. It
11064 	 * happens in ipif_lookup_on_name.
11065 	 * The specified unit number is ignored when we create the ipif on a
11066 	 * different interface. However, we save it in ipif_orig_ipifid below so
11067 	 * that the ipif fails back to the right position.
11068 	 */
11069 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
11070 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
11071 		err = ENOBUFS;
11072 		goto done;
11073 	}
11074 
11075 	/* Return created name with ioctl */
11076 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
11077 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
11078 	ip1dbg(("created %s\n", lifr->lifr_name));
11079 
11080 	/* Set address */
11081 	sin = (sin_t *)&lifr->lifr_addr;
11082 	if (sin->sin_family != AF_UNSPEC) {
11083 		err = ip_sioctl_addr(ipif, sin, q, mp,
11084 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
11085 	}
11086 
11087 	/* Set ifindex and unit number for failback */
11088 	if (err == 0 && orig_ifindex != 0) {
11089 		ipif->ipif_orig_ifindex = orig_ifindex;
11090 		if (found_sep) {
11091 			ipif->ipif_orig_ipifid = id;
11092 		}
11093 	}
11094 
11095 done:
11096 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
11097 	return (err);
11098 }
11099 
11100 /*
11101  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
11102  * interface) delete it based on the IP address (on this physical interface).
11103  * Otherwise delete it based on the ipif_id.
11104  * Also, special handling to allow a removeif of lo0.
11105  */
11106 /* ARGSUSED */
11107 int
11108 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11109     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11110 {
11111 	conn_t		*connp;
11112 	ill_t		*ill = ipif->ipif_ill;
11113 	boolean_t	 success;
11114 	ip_stack_t	*ipst;
11115 
11116 	ipst = CONNQ_TO_IPST(q);
11117 
11118 	ASSERT(q->q_next == NULL);
11119 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
11120 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11121 	ASSERT(IAM_WRITER_IPIF(ipif));
11122 
11123 	connp = Q_TO_CONN(q);
11124 	/*
11125 	 * Special case for unplumbing lo0 (the loopback physical interface).
11126 	 * If unplumbing lo0, the incoming address structure has been
11127 	 * initialized to all zeros. When unplumbing lo0, all its logical
11128 	 * interfaces must be removed too.
11129 	 *
11130 	 * Note that this interface may be called to remove a specific
11131 	 * loopback logical interface (eg, lo0:1). But in that case
11132 	 * ipif->ipif_id != 0 so that the code path for that case is the
11133 	 * same as any other interface (meaning it skips the code directly
11134 	 * below).
11135 	 */
11136 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11137 		if (sin->sin_family == AF_UNSPEC &&
11138 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
11139 			/*
11140 			 * Mark it condemned. No new ref. will be made to ill.
11141 			 */
11142 			mutex_enter(&ill->ill_lock);
11143 			ill->ill_state_flags |= ILL_CONDEMNED;
11144 			for (ipif = ill->ill_ipif; ipif != NULL;
11145 			    ipif = ipif->ipif_next) {
11146 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
11147 			}
11148 			mutex_exit(&ill->ill_lock);
11149 
11150 			ipif = ill->ill_ipif;
11151 			/* unplumb the loopback interface */
11152 			ill_delete(ill);
11153 			mutex_enter(&connp->conn_lock);
11154 			mutex_enter(&ill->ill_lock);
11155 			ASSERT(ill->ill_group == NULL);
11156 
11157 			/* Are any references to this ill active */
11158 			if (ill_is_quiescent(ill)) {
11159 				mutex_exit(&ill->ill_lock);
11160 				mutex_exit(&connp->conn_lock);
11161 				ill_delete_tail(ill);
11162 				mi_free(ill);
11163 				return (0);
11164 			}
11165 			success = ipsq_pending_mp_add(connp, ipif,
11166 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
11167 			mutex_exit(&connp->conn_lock);
11168 			mutex_exit(&ill->ill_lock);
11169 			if (success)
11170 				return (EINPROGRESS);
11171 			else
11172 				return (EINTR);
11173 		}
11174 	}
11175 
11176 	/*
11177 	 * We are exclusive on the ipsq, so an ill move will be serialized
11178 	 * before or after us.
11179 	 */
11180 	ASSERT(ill->ill_move_in_progress == B_FALSE);
11181 
11182 	if (ipif->ipif_id == 0) {
11183 		/* Find based on address */
11184 		if (ipif->ipif_isv6) {
11185 			sin6_t *sin6;
11186 
11187 			if (sin->sin_family != AF_INET6)
11188 				return (EAFNOSUPPORT);
11189 
11190 			sin6 = (sin6_t *)sin;
11191 			/* We are a writer, so we should be able to lookup */
11192 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11193 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
11194 			if (ipif == NULL) {
11195 				/*
11196 				 * Maybe the address in on another interface in
11197 				 * the same IPMP group? We check this below.
11198 				 */
11199 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11200 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
11201 				    ipst);
11202 			}
11203 		} else {
11204 			ipaddr_t addr;
11205 
11206 			if (sin->sin_family != AF_INET)
11207 				return (EAFNOSUPPORT);
11208 
11209 			addr = sin->sin_addr.s_addr;
11210 			/* We are a writer, so we should be able to lookup */
11211 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
11212 			    NULL, NULL, NULL, ipst);
11213 			if (ipif == NULL) {
11214 				/*
11215 				 * Maybe the address in on another interface in
11216 				 * the same IPMP group? We check this below.
11217 				 */
11218 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
11219 				    NULL, NULL, NULL, NULL, ipst);
11220 			}
11221 		}
11222 		if (ipif == NULL) {
11223 			return (EADDRNOTAVAIL);
11224 		}
11225 		/*
11226 		 * When the address to be removed is hosted on a different
11227 		 * interface, we check if the interface is in the same IPMP
11228 		 * group as the specified one; if so we proceed with the
11229 		 * removal.
11230 		 * ill->ill_group is NULL when the ill is down, so we have to
11231 		 * compare the group names instead.
11232 		 */
11233 		if (ipif->ipif_ill != ill &&
11234 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
11235 		    ill->ill_phyint->phyint_groupname_len == 0 ||
11236 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
11237 		    ill->ill_phyint->phyint_groupname) != 0)) {
11238 			ipif_refrele(ipif);
11239 			return (EADDRNOTAVAIL);
11240 		}
11241 
11242 		/* This is a writer */
11243 		ipif_refrele(ipif);
11244 	}
11245 
11246 	/*
11247 	 * Can not delete instance zero since it is tied to the ill.
11248 	 */
11249 	if (ipif->ipif_id == 0)
11250 		return (EBUSY);
11251 
11252 	mutex_enter(&ill->ill_lock);
11253 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
11254 	mutex_exit(&ill->ill_lock);
11255 
11256 	ipif_free(ipif);
11257 
11258 	mutex_enter(&connp->conn_lock);
11259 	mutex_enter(&ill->ill_lock);
11260 
11261 	/* Are any references to this ipif active */
11262 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
11263 		mutex_exit(&ill->ill_lock);
11264 		mutex_exit(&connp->conn_lock);
11265 		ipif_non_duplicate(ipif);
11266 		ipif_down_tail(ipif);
11267 		ipif_free_tail(ipif);
11268 		return (0);
11269 	}
11270 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
11271 	    IPIF_FREE);
11272 	mutex_exit(&ill->ill_lock);
11273 	mutex_exit(&connp->conn_lock);
11274 	if (success)
11275 		return (EINPROGRESS);
11276 	else
11277 		return (EINTR);
11278 }
11279 
11280 /*
11281  * Restart the removeif ioctl. The refcnt has gone down to 0.
11282  * The ipif is already condemned. So can't find it thru lookups.
11283  */
11284 /* ARGSUSED */
11285 int
11286 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11287     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11288 {
11289 	ill_t *ill;
11290 
11291 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11292 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11293 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11294 		ill = ipif->ipif_ill;
11295 		ASSERT(IAM_WRITER_ILL(ill));
11296 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
11297 		    (ill->ill_state_flags & IPIF_CONDEMNED));
11298 		ill_delete_tail(ill);
11299 		mi_free(ill);
11300 		return (0);
11301 	}
11302 
11303 	ill = ipif->ipif_ill;
11304 	ASSERT(IAM_WRITER_IPIF(ipif));
11305 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11306 
11307 	ipif_non_duplicate(ipif);
11308 	ipif_down_tail(ipif);
11309 	ipif_free_tail(ipif);
11310 
11311 	ILL_UNMARK_CHANGING(ill);
11312 	return (0);
11313 }
11314 
11315 /*
11316  * Set the local interface address.
11317  * Allow an address of all zero when the interface is down.
11318  */
11319 /* ARGSUSED */
11320 int
11321 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11322     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11323 {
11324 	int err = 0;
11325 	in6_addr_t v6addr;
11326 	boolean_t need_up = B_FALSE;
11327 
11328 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11329 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11330 
11331 	ASSERT(IAM_WRITER_IPIF(ipif));
11332 
11333 	if (ipif->ipif_isv6) {
11334 		sin6_t *sin6;
11335 		ill_t *ill;
11336 		phyint_t *phyi;
11337 
11338 		if (sin->sin_family != AF_INET6)
11339 			return (EAFNOSUPPORT);
11340 
11341 		sin6 = (sin6_t *)sin;
11342 		v6addr = sin6->sin6_addr;
11343 		ill = ipif->ipif_ill;
11344 		phyi = ill->ill_phyint;
11345 
11346 		/*
11347 		 * Enforce that true multicast interfaces have a link-local
11348 		 * address for logical unit 0.
11349 		 */
11350 		if (ipif->ipif_id == 0 &&
11351 		    (ill->ill_flags & ILLF_MULTICAST) &&
11352 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11353 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11354 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11355 			return (EADDRNOTAVAIL);
11356 		}
11357 
11358 		/*
11359 		 * up interfaces shouldn't have the unspecified address
11360 		 * unless they also have the IPIF_NOLOCAL flags set and
11361 		 * have a subnet assigned.
11362 		 */
11363 		if ((ipif->ipif_flags & IPIF_UP) &&
11364 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11365 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11366 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11367 			return (EADDRNOTAVAIL);
11368 		}
11369 
11370 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11371 			return (EADDRNOTAVAIL);
11372 	} else {
11373 		ipaddr_t addr;
11374 
11375 		if (sin->sin_family != AF_INET)
11376 			return (EAFNOSUPPORT);
11377 
11378 		addr = sin->sin_addr.s_addr;
11379 
11380 		/* Allow 0 as the local address. */
11381 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11382 			return (EADDRNOTAVAIL);
11383 
11384 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11385 	}
11386 
11387 
11388 	/*
11389 	 * Even if there is no change we redo things just to rerun
11390 	 * ipif_set_default.
11391 	 */
11392 	if (ipif->ipif_flags & IPIF_UP) {
11393 		/*
11394 		 * Setting a new local address, make sure
11395 		 * we have net and subnet bcast ire's for
11396 		 * the old address if we need them.
11397 		 */
11398 		if (!ipif->ipif_isv6)
11399 			ipif_check_bcast_ires(ipif);
11400 		/*
11401 		 * If the interface is already marked up,
11402 		 * we call ipif_down which will take care
11403 		 * of ditching any IREs that have been set
11404 		 * up based on the old interface address.
11405 		 */
11406 		err = ipif_logical_down(ipif, q, mp);
11407 		if (err == EINPROGRESS)
11408 			return (err);
11409 		ipif_down_tail(ipif);
11410 		need_up = 1;
11411 	}
11412 
11413 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11414 	return (err);
11415 }
11416 
11417 int
11418 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11419     boolean_t need_up)
11420 {
11421 	in6_addr_t v6addr;
11422 	in6_addr_t ov6addr;
11423 	ipaddr_t addr;
11424 	sin6_t	*sin6;
11425 	int	sinlen;
11426 	int	err = 0;
11427 	ill_t	*ill = ipif->ipif_ill;
11428 	boolean_t need_dl_down;
11429 	boolean_t need_arp_down;
11430 	struct iocblk *iocp;
11431 
11432 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11433 
11434 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11435 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11436 	ASSERT(IAM_WRITER_IPIF(ipif));
11437 
11438 	/* Must cancel any pending timer before taking the ill_lock */
11439 	if (ipif->ipif_recovery_id != 0)
11440 		(void) untimeout(ipif->ipif_recovery_id);
11441 	ipif->ipif_recovery_id = 0;
11442 
11443 	if (ipif->ipif_isv6) {
11444 		sin6 = (sin6_t *)sin;
11445 		v6addr = sin6->sin6_addr;
11446 		sinlen = sizeof (struct sockaddr_in6);
11447 	} else {
11448 		addr = sin->sin_addr.s_addr;
11449 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11450 		sinlen = sizeof (struct sockaddr_in);
11451 	}
11452 	mutex_enter(&ill->ill_lock);
11453 	ov6addr = ipif->ipif_v6lcl_addr;
11454 	ipif->ipif_v6lcl_addr = v6addr;
11455 	sctp_update_ipif_addr(ipif, ov6addr);
11456 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11457 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11458 	} else {
11459 		ipif->ipif_v6src_addr = v6addr;
11460 	}
11461 	ipif->ipif_addr_ready = 0;
11462 
11463 	/*
11464 	 * If the interface was previously marked as a duplicate, then since
11465 	 * we've now got a "new" address, it should no longer be considered a
11466 	 * duplicate -- even if the "new" address is the same as the old one.
11467 	 * Note that if all ipifs are down, we may have a pending ARP down
11468 	 * event to handle.  This is because we want to recover from duplicates
11469 	 * and thus delay tearing down ARP until the duplicates have been
11470 	 * removed or disabled.
11471 	 */
11472 	need_dl_down = need_arp_down = B_FALSE;
11473 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11474 		need_arp_down = !need_up;
11475 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11476 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11477 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11478 			need_dl_down = B_TRUE;
11479 		}
11480 	}
11481 
11482 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11483 	    !ill->ill_is_6to4tun) {
11484 		queue_t *wqp = ill->ill_wq;
11485 
11486 		/*
11487 		 * The local address of this interface is a 6to4 address,
11488 		 * check if this interface is in fact a 6to4 tunnel or just
11489 		 * an interface configured with a 6to4 address.  We are only
11490 		 * interested in the former.
11491 		 */
11492 		if (wqp != NULL) {
11493 			while ((wqp->q_next != NULL) &&
11494 			    (wqp->q_next->q_qinfo != NULL) &&
11495 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11496 
11497 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11498 				    == TUN6TO4_MODID) {
11499 					/* set for use in IP */
11500 					ill->ill_is_6to4tun = 1;
11501 					break;
11502 				}
11503 				wqp = wqp->q_next;
11504 			}
11505 		}
11506 	}
11507 
11508 	ipif_set_default(ipif);
11509 
11510 	/*
11511 	 * When publishing an interface address change event, we only notify
11512 	 * the event listeners of the new address.  It is assumed that if they
11513 	 * actively care about the addresses assigned that they will have
11514 	 * already discovered the previous address assigned (if there was one.)
11515 	 *
11516 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11517 	 */
11518 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11519 		hook_nic_event_t *info;
11520 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
11521 			ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d "
11522 			    "attached for %s\n", info->hne_event,
11523 			    ill->ill_name));
11524 			if (info->hne_data != NULL)
11525 				kmem_free(info->hne_data, info->hne_datalen);
11526 			kmem_free(info, sizeof (hook_nic_event_t));
11527 		}
11528 
11529 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
11530 		if (info != NULL) {
11531 			ip_stack_t	*ipst = ill->ill_ipst;
11532 
11533 			info->hne_nic =
11534 			    ipif->ipif_ill->ill_phyint->phyint_hook_ifindex;
11535 			info->hne_lif = MAP_IPIF_ID(ipif->ipif_id);
11536 			info->hne_event = NE_ADDRESS_CHANGE;
11537 			info->hne_family = ipif->ipif_isv6 ?
11538 			    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
11539 			info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP);
11540 			if (info->hne_data != NULL) {
11541 				info->hne_datalen = sinlen;
11542 				bcopy(sin, info->hne_data, sinlen);
11543 			} else {
11544 				ip2dbg(("ip_sioctl_addr_tail: could not attach "
11545 				    "address information for ADDRESS_CHANGE nic"
11546 				    " event of %s (ENOMEM)\n",
11547 				    ipif->ipif_ill->ill_name));
11548 				kmem_free(info, sizeof (hook_nic_event_t));
11549 			}
11550 		} else
11551 			ip2dbg(("ip_sioctl_addr_tail: could not attach "
11552 			    "ADDRESS_CHANGE nic event information for %s "
11553 			    "(ENOMEM)\n", ipif->ipif_ill->ill_name));
11554 
11555 		ipif->ipif_ill->ill_nic_event_info = info;
11556 	}
11557 
11558 	mutex_exit(&ill->ill_lock);
11559 
11560 	if (need_up) {
11561 		/*
11562 		 * Now bring the interface back up.  If this
11563 		 * is the only IPIF for the ILL, ipif_up
11564 		 * will have to re-bind to the device, so
11565 		 * we may get back EINPROGRESS, in which
11566 		 * case, this IOCTL will get completed in
11567 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11568 		 */
11569 		err = ipif_up(ipif, q, mp);
11570 	}
11571 
11572 	if (need_dl_down)
11573 		ill_dl_down(ill);
11574 	if (need_arp_down)
11575 		ipif_arp_down(ipif);
11576 
11577 	return (err);
11578 }
11579 
11580 
11581 /*
11582  * Restart entry point to restart the address set operation after the
11583  * refcounts have dropped to zero.
11584  */
11585 /* ARGSUSED */
11586 int
11587 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11588     ip_ioctl_cmd_t *ipip, void *ifreq)
11589 {
11590 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11591 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11592 	ASSERT(IAM_WRITER_IPIF(ipif));
11593 	ipif_down_tail(ipif);
11594 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11595 }
11596 
11597 /* ARGSUSED */
11598 int
11599 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11600     ip_ioctl_cmd_t *ipip, void *if_req)
11601 {
11602 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11603 	struct lifreq *lifr = (struct lifreq *)if_req;
11604 
11605 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11606 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11607 	/*
11608 	 * The net mask and address can't change since we have a
11609 	 * reference to the ipif. So no lock is necessary.
11610 	 */
11611 	if (ipif->ipif_isv6) {
11612 		*sin6 = sin6_null;
11613 		sin6->sin6_family = AF_INET6;
11614 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11615 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11616 		lifr->lifr_addrlen =
11617 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11618 	} else {
11619 		*sin = sin_null;
11620 		sin->sin_family = AF_INET;
11621 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11622 		if (ipip->ipi_cmd_type == LIF_CMD) {
11623 			lifr->lifr_addrlen =
11624 			    ip_mask_to_plen(ipif->ipif_net_mask);
11625 		}
11626 	}
11627 	return (0);
11628 }
11629 
11630 /*
11631  * Set the destination address for a pt-pt interface.
11632  */
11633 /* ARGSUSED */
11634 int
11635 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11636     ip_ioctl_cmd_t *ipip, void *if_req)
11637 {
11638 	int err = 0;
11639 	in6_addr_t v6addr;
11640 	boolean_t need_up = B_FALSE;
11641 
11642 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11643 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11644 	ASSERT(IAM_WRITER_IPIF(ipif));
11645 
11646 	if (ipif->ipif_isv6) {
11647 		sin6_t *sin6;
11648 
11649 		if (sin->sin_family != AF_INET6)
11650 			return (EAFNOSUPPORT);
11651 
11652 		sin6 = (sin6_t *)sin;
11653 		v6addr = sin6->sin6_addr;
11654 
11655 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11656 			return (EADDRNOTAVAIL);
11657 	} else {
11658 		ipaddr_t addr;
11659 
11660 		if (sin->sin_family != AF_INET)
11661 			return (EAFNOSUPPORT);
11662 
11663 		addr = sin->sin_addr.s_addr;
11664 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11665 			return (EADDRNOTAVAIL);
11666 
11667 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11668 	}
11669 
11670 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11671 		return (0);	/* No change */
11672 
11673 	if (ipif->ipif_flags & IPIF_UP) {
11674 		/*
11675 		 * If the interface is already marked up,
11676 		 * we call ipif_down which will take care
11677 		 * of ditching any IREs that have been set
11678 		 * up based on the old pp dst address.
11679 		 */
11680 		err = ipif_logical_down(ipif, q, mp);
11681 		if (err == EINPROGRESS)
11682 			return (err);
11683 		ipif_down_tail(ipif);
11684 		need_up = B_TRUE;
11685 	}
11686 	/*
11687 	 * could return EINPROGRESS. If so ioctl will complete in
11688 	 * ip_rput_dlpi_writer
11689 	 */
11690 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11691 	return (err);
11692 }
11693 
11694 static int
11695 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11696     boolean_t need_up)
11697 {
11698 	in6_addr_t v6addr;
11699 	ill_t	*ill = ipif->ipif_ill;
11700 	int	err = 0;
11701 	boolean_t need_dl_down;
11702 	boolean_t need_arp_down;
11703 
11704 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11705 	    ipif->ipif_id, (void *)ipif));
11706 
11707 	/* Must cancel any pending timer before taking the ill_lock */
11708 	if (ipif->ipif_recovery_id != 0)
11709 		(void) untimeout(ipif->ipif_recovery_id);
11710 	ipif->ipif_recovery_id = 0;
11711 
11712 	if (ipif->ipif_isv6) {
11713 		sin6_t *sin6;
11714 
11715 		sin6 = (sin6_t *)sin;
11716 		v6addr = sin6->sin6_addr;
11717 	} else {
11718 		ipaddr_t addr;
11719 
11720 		addr = sin->sin_addr.s_addr;
11721 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11722 	}
11723 	mutex_enter(&ill->ill_lock);
11724 	/* Set point to point destination address. */
11725 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11726 		/*
11727 		 * Allow this as a means of creating logical
11728 		 * pt-pt interfaces on top of e.g. an Ethernet.
11729 		 * XXX Undocumented HACK for testing.
11730 		 * pt-pt interfaces are created with NUD disabled.
11731 		 */
11732 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11733 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11734 		if (ipif->ipif_isv6)
11735 			ill->ill_flags |= ILLF_NONUD;
11736 	}
11737 
11738 	/*
11739 	 * If the interface was previously marked as a duplicate, then since
11740 	 * we've now got a "new" address, it should no longer be considered a
11741 	 * duplicate -- even if the "new" address is the same as the old one.
11742 	 * Note that if all ipifs are down, we may have a pending ARP down
11743 	 * event to handle.
11744 	 */
11745 	need_dl_down = need_arp_down = B_FALSE;
11746 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11747 		need_arp_down = !need_up;
11748 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11749 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11750 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11751 			need_dl_down = B_TRUE;
11752 		}
11753 	}
11754 
11755 	/* Set the new address. */
11756 	ipif->ipif_v6pp_dst_addr = v6addr;
11757 	/* Make sure subnet tracks pp_dst */
11758 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11759 	mutex_exit(&ill->ill_lock);
11760 
11761 	if (need_up) {
11762 		/*
11763 		 * Now bring the interface back up.  If this
11764 		 * is the only IPIF for the ILL, ipif_up
11765 		 * will have to re-bind to the device, so
11766 		 * we may get back EINPROGRESS, in which
11767 		 * case, this IOCTL will get completed in
11768 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11769 		 */
11770 		err = ipif_up(ipif, q, mp);
11771 	}
11772 
11773 	if (need_dl_down)
11774 		ill_dl_down(ill);
11775 
11776 	if (need_arp_down)
11777 		ipif_arp_down(ipif);
11778 	return (err);
11779 }
11780 
11781 /*
11782  * Restart entry point to restart the dstaddress set operation after the
11783  * refcounts have dropped to zero.
11784  */
11785 /* ARGSUSED */
11786 int
11787 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11788     ip_ioctl_cmd_t *ipip, void *ifreq)
11789 {
11790 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11791 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11792 	ipif_down_tail(ipif);
11793 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11794 }
11795 
11796 /* ARGSUSED */
11797 int
11798 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11799     ip_ioctl_cmd_t *ipip, void *if_req)
11800 {
11801 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11802 
11803 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11804 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11805 	/*
11806 	 * Get point to point destination address. The addresses can't
11807 	 * change since we hold a reference to the ipif.
11808 	 */
11809 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11810 		return (EADDRNOTAVAIL);
11811 
11812 	if (ipif->ipif_isv6) {
11813 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11814 		*sin6 = sin6_null;
11815 		sin6->sin6_family = AF_INET6;
11816 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11817 	} else {
11818 		*sin = sin_null;
11819 		sin->sin_family = AF_INET;
11820 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11821 	}
11822 	return (0);
11823 }
11824 
11825 /*
11826  * part of ipmp, make this func return the active/inactive state and
11827  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11828  */
11829 /*
11830  * This function either sets or clears the IFF_INACTIVE flag.
11831  *
11832  * As long as there are some addresses or multicast memberships on the
11833  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11834  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11835  * will be used for outbound packets.
11836  *
11837  * Caller needs to verify the validity of setting IFF_INACTIVE.
11838  */
11839 static void
11840 phyint_inactive(phyint_t *phyi)
11841 {
11842 	ill_t *ill_v4;
11843 	ill_t *ill_v6;
11844 	ipif_t *ipif;
11845 	ilm_t *ilm;
11846 
11847 	ill_v4 = phyi->phyint_illv4;
11848 	ill_v6 = phyi->phyint_illv6;
11849 
11850 	/*
11851 	 * No need for a lock while traversing the list since iam
11852 	 * a writer
11853 	 */
11854 	if (ill_v4 != NULL) {
11855 		ASSERT(IAM_WRITER_ILL(ill_v4));
11856 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11857 		    ipif = ipif->ipif_next) {
11858 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11859 				mutex_enter(&phyi->phyint_lock);
11860 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11861 				mutex_exit(&phyi->phyint_lock);
11862 				return;
11863 			}
11864 		}
11865 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11866 		    ilm = ilm->ilm_next) {
11867 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11868 				mutex_enter(&phyi->phyint_lock);
11869 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11870 				mutex_exit(&phyi->phyint_lock);
11871 				return;
11872 			}
11873 		}
11874 	}
11875 	if (ill_v6 != NULL) {
11876 		ill_v6 = phyi->phyint_illv6;
11877 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11878 		    ipif = ipif->ipif_next) {
11879 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11880 				mutex_enter(&phyi->phyint_lock);
11881 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11882 				mutex_exit(&phyi->phyint_lock);
11883 				return;
11884 			}
11885 		}
11886 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11887 		    ilm = ilm->ilm_next) {
11888 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11889 				mutex_enter(&phyi->phyint_lock);
11890 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11891 				mutex_exit(&phyi->phyint_lock);
11892 				return;
11893 			}
11894 		}
11895 	}
11896 	mutex_enter(&phyi->phyint_lock);
11897 	phyi->phyint_flags |= PHYI_INACTIVE;
11898 	mutex_exit(&phyi->phyint_lock);
11899 }
11900 
11901 /*
11902  * This function is called only when the phyint flags change. Currently
11903  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11904  * that we can select a good ill.
11905  */
11906 static void
11907 ip_redo_nomination(phyint_t *phyi)
11908 {
11909 	ill_t *ill_v4;
11910 
11911 	ill_v4 = phyi->phyint_illv4;
11912 
11913 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11914 		ASSERT(IAM_WRITER_ILL(ill_v4));
11915 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11916 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11917 	}
11918 }
11919 
11920 /*
11921  * Heuristic to check if ill is INACTIVE.
11922  * Checks if ill has an ipif with an usable ip address.
11923  *
11924  * Return values:
11925  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11926  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11927  */
11928 static boolean_t
11929 ill_is_inactive(ill_t *ill)
11930 {
11931 	ipif_t *ipif;
11932 
11933 	/* Check whether it is in an IPMP group */
11934 	if (ill->ill_phyint->phyint_groupname == NULL)
11935 		return (B_FALSE);
11936 
11937 	if (ill->ill_ipif_up_count == 0)
11938 		return (B_TRUE);
11939 
11940 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11941 		uint64_t flags = ipif->ipif_flags;
11942 
11943 		/*
11944 		 * This ipif is usable if it is IPIF_UP and not a
11945 		 * dedicated test address.  A dedicated test address
11946 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11947 		 * (note in particular that V6 test addresses are
11948 		 * link-local data addresses and thus are marked
11949 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11950 		 */
11951 		if ((flags & IPIF_UP) &&
11952 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11953 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11954 			return (B_FALSE);
11955 	}
11956 	return (B_TRUE);
11957 }
11958 
11959 /*
11960  * Set interface flags.
11961  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11962  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11963  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11964  *
11965  * NOTE : We really don't enforce that ipif_id zero should be used
11966  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11967  *	  is because applications generally does SICGLIFFLAGS and
11968  *	  ORs in the new flags (that affects the logical) and does a
11969  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11970  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11971  *	  flags that will be turned on is correct with respect to
11972  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11973  */
11974 /* ARGSUSED */
11975 int
11976 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11977     ip_ioctl_cmd_t *ipip, void *if_req)
11978 {
11979 	uint64_t turn_on;
11980 	uint64_t turn_off;
11981 	int	err;
11982 	boolean_t need_up = B_FALSE;
11983 	phyint_t *phyi;
11984 	ill_t *ill;
11985 	uint64_t intf_flags;
11986 	boolean_t phyint_flags_modified = B_FALSE;
11987 	uint64_t flags;
11988 	struct ifreq *ifr;
11989 	struct lifreq *lifr;
11990 	boolean_t set_linklocal = B_FALSE;
11991 	boolean_t zero_source = B_FALSE;
11992 	ip_stack_t *ipst;
11993 
11994 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11995 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11996 
11997 	ASSERT(IAM_WRITER_IPIF(ipif));
11998 
11999 	ill = ipif->ipif_ill;
12000 	phyi = ill->ill_phyint;
12001 	ipst = ill->ill_ipst;
12002 
12003 	if (ipip->ipi_cmd_type == IF_CMD) {
12004 		ifr = (struct ifreq *)if_req;
12005 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
12006 	} else {
12007 		lifr = (struct lifreq *)if_req;
12008 		flags = lifr->lifr_flags;
12009 	}
12010 
12011 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12012 
12013 	/*
12014 	 * Has the flags been set correctly till now ?
12015 	 */
12016 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12017 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12018 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12019 	/*
12020 	 * Compare the new flags to the old, and partition
12021 	 * into those coming on and those going off.
12022 	 * For the 16 bit command keep the bits above bit 16 unchanged.
12023 	 */
12024 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
12025 		flags |= intf_flags & ~0xFFFF;
12026 
12027 	/*
12028 	 * First check which bits will change and then which will
12029 	 * go on and off
12030 	 */
12031 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
12032 	if (!turn_on)
12033 		return (0);	/* No change */
12034 
12035 	turn_off = intf_flags & turn_on;
12036 	turn_on ^= turn_off;
12037 	err = 0;
12038 
12039 	/*
12040 	 * Don't allow any bits belonging to the logical interface
12041 	 * to be set or cleared on the replacement ipif that was
12042 	 * created temporarily during a MOVE.
12043 	 */
12044 	if (ipif->ipif_replace_zero &&
12045 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
12046 		return (EINVAL);
12047 	}
12048 
12049 	/*
12050 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
12051 	 * IPv6 interfaces.
12052 	 */
12053 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
12054 		return (EINVAL);
12055 
12056 	/*
12057 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
12058 	 * interfaces.  It makes no sense in that context.
12059 	 */
12060 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
12061 		return (EINVAL);
12062 
12063 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
12064 		zero_source = B_TRUE;
12065 
12066 	/*
12067 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
12068 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
12069 	 * If the link local address isn't set, and can be set, it will get
12070 	 * set later on in this function.
12071 	 */
12072 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
12073 	    (flags & IFF_UP) && !zero_source &&
12074 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
12075 		if (ipif_cant_setlinklocal(ipif))
12076 			return (EINVAL);
12077 		set_linklocal = B_TRUE;
12078 	}
12079 
12080 	/*
12081 	 * ILL cannot be part of a usesrc group and and IPMP group at the
12082 	 * same time. No need to grab ill_g_usesrc_lock here, see
12083 	 * synchronization notes in ip.c
12084 	 */
12085 	if (turn_on & PHYI_STANDBY &&
12086 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
12087 		return (EINVAL);
12088 	}
12089 
12090 	/*
12091 	 * If we modify physical interface flags, we'll potentially need to
12092 	 * send up two routing socket messages for the changes (one for the
12093 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
12094 	 */
12095 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
12096 		phyint_flags_modified = B_TRUE;
12097 
12098 	/*
12099 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
12100 	 * we need to flush the IRE_CACHES belonging to this ill.
12101 	 * We handle this case here without doing the DOWN/UP dance
12102 	 * like it is done for other flags. If some other flags are
12103 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
12104 	 * below will handle it by bringing it down and then
12105 	 * bringing it UP.
12106 	 */
12107 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
12108 		ill_t *ill_v4, *ill_v6;
12109 
12110 		ill_v4 = phyi->phyint_illv4;
12111 		ill_v6 = phyi->phyint_illv6;
12112 
12113 		/*
12114 		 * First set the INACTIVE flag if needed. Then delete the ires.
12115 		 * ire_add will atomically prevent creating new IRE_CACHEs
12116 		 * unless hidden flag is set.
12117 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
12118 		 */
12119 		if ((turn_on & PHYI_FAILED) &&
12120 		    ((intf_flags & PHYI_STANDBY) ||
12121 		    !ipst->ips_ipmp_enable_failback)) {
12122 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
12123 			phyi->phyint_flags &= ~PHYI_INACTIVE;
12124 		}
12125 		if ((turn_off & PHYI_FAILED) &&
12126 		    ((intf_flags & PHYI_STANDBY) ||
12127 		    (!ipst->ips_ipmp_enable_failback &&
12128 		    ill_is_inactive(ill)))) {
12129 			phyint_inactive(phyi);
12130 		}
12131 
12132 		if (turn_on & PHYI_STANDBY) {
12133 			/*
12134 			 * We implicitly set INACTIVE only when STANDBY is set.
12135 			 * INACTIVE is also set on non-STANDBY phyint when user
12136 			 * disables FAILBACK using configuration file.
12137 			 * Do not allow STANDBY to be set on such INACTIVE
12138 			 * phyint
12139 			 */
12140 			if (phyi->phyint_flags & PHYI_INACTIVE)
12141 				return (EINVAL);
12142 			if (!(phyi->phyint_flags & PHYI_FAILED))
12143 				phyint_inactive(phyi);
12144 		}
12145 		if (turn_off & PHYI_STANDBY) {
12146 			if (ipst->ips_ipmp_enable_failback) {
12147 				/*
12148 				 * Reset PHYI_INACTIVE.
12149 				 */
12150 				phyi->phyint_flags &= ~PHYI_INACTIVE;
12151 			} else if (ill_is_inactive(ill) &&
12152 			    !(phyi->phyint_flags & PHYI_FAILED)) {
12153 				/*
12154 				 * Need to set INACTIVE, when user sets
12155 				 * STANDBY on a non-STANDBY phyint and
12156 				 * later resets STANDBY
12157 				 */
12158 				phyint_inactive(phyi);
12159 			}
12160 		}
12161 		/*
12162 		 * We should always send up a message so that the
12163 		 * daemons come to know of it. Note that the zeroth
12164 		 * interface can be down and the check below for IPIF_UP
12165 		 * will not make sense as we are actually setting
12166 		 * a phyint flag here. We assume that the ipif used
12167 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
12168 		 * send up any message for non-zero ipifs).
12169 		 */
12170 		phyint_flags_modified = B_TRUE;
12171 
12172 		if (ill_v4 != NULL) {
12173 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
12174 			    IRE_CACHE, ill_stq_cache_delete,
12175 			    (char *)ill_v4, ill_v4);
12176 			illgrp_reset_schednext(ill_v4);
12177 		}
12178 		if (ill_v6 != NULL) {
12179 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
12180 			    IRE_CACHE, ill_stq_cache_delete,
12181 			    (char *)ill_v6, ill_v6);
12182 			illgrp_reset_schednext(ill_v6);
12183 		}
12184 	}
12185 
12186 	/*
12187 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
12188 	 * status of the interface and, if the interface is part of an IPMP
12189 	 * group, all other interfaces that are part of the same IPMP
12190 	 * group.
12191 	 */
12192 	if ((turn_on | turn_off) & ILLF_ROUTER) {
12193 		(void) ill_forward_set(q, mp, ((turn_on & ILLF_ROUTER) != 0),
12194 		    (caddr_t)ill);
12195 	}
12196 
12197 	/*
12198 	 * If the interface is not UP and we are not going to
12199 	 * bring it UP, record the flags and return. When the
12200 	 * interface comes UP later, the right actions will be
12201 	 * taken.
12202 	 */
12203 	if (!(ipif->ipif_flags & IPIF_UP) &&
12204 	    !(turn_on & IPIF_UP)) {
12205 		/* Record new flags in their respective places. */
12206 		mutex_enter(&ill->ill_lock);
12207 		mutex_enter(&ill->ill_phyint->phyint_lock);
12208 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12209 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12210 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12211 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12212 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12213 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12214 		mutex_exit(&ill->ill_lock);
12215 		mutex_exit(&ill->ill_phyint->phyint_lock);
12216 
12217 		/*
12218 		 * We do the broadcast and nomination here rather
12219 		 * than waiting for a FAILOVER/FAILBACK to happen. In
12220 		 * the case of FAILBACK from INACTIVE standby to the
12221 		 * interface that has been repaired, PHYI_FAILED has not
12222 		 * been cleared yet. If there are only two interfaces in
12223 		 * that group, all we have is a FAILED and INACTIVE
12224 		 * interface. If we do the nomination soon after a failback,
12225 		 * the broadcast nomination code would select the
12226 		 * INACTIVE interface for receiving broadcasts as FAILED is
12227 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
12228 		 * receive broadcast packets, we need to redo nomination
12229 		 * when the FAILED is cleared here. Thus, in general we
12230 		 * always do the nomination here for FAILED, STANDBY
12231 		 * and OFFLINE.
12232 		 */
12233 		if (((turn_on | turn_off) &
12234 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
12235 			ip_redo_nomination(phyi);
12236 		}
12237 		if (phyint_flags_modified) {
12238 			if (phyi->phyint_illv4 != NULL) {
12239 				ip_rts_ifmsg(phyi->phyint_illv4->
12240 				    ill_ipif);
12241 			}
12242 			if (phyi->phyint_illv6 != NULL) {
12243 				ip_rts_ifmsg(phyi->phyint_illv6->
12244 				    ill_ipif);
12245 			}
12246 		}
12247 		return (0);
12248 	} else if (set_linklocal || zero_source) {
12249 		mutex_enter(&ill->ill_lock);
12250 		if (set_linklocal)
12251 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
12252 		if (zero_source)
12253 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
12254 		mutex_exit(&ill->ill_lock);
12255 	}
12256 
12257 	/*
12258 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
12259 	 * or point-to-point interfaces with an unspecified destination. We do
12260 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
12261 	 * have a subnet assigned, which is how in.ndpd currently manages its
12262 	 * onlink prefix list when no addresses are configured with those
12263 	 * prefixes.
12264 	 */
12265 	if (ipif->ipif_isv6 &&
12266 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
12267 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
12268 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
12269 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12270 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
12271 		return (EINVAL);
12272 	}
12273 
12274 	/*
12275 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
12276 	 * from being brought up.
12277 	 */
12278 	if (!ipif->ipif_isv6 &&
12279 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12280 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
12281 		return (EINVAL);
12282 	}
12283 
12284 	/*
12285 	 * The only flag changes that we currently take specific action on
12286 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
12287 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
12288 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
12289 	 * the flags and bringing it back up again.
12290 	 */
12291 	if ((turn_on|turn_off) &
12292 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
12293 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
12294 		/*
12295 		 * Taking this ipif down, make sure we have
12296 		 * valid net and subnet bcast ire's for other
12297 		 * logical interfaces, if we need them.
12298 		 */
12299 		if (!ipif->ipif_isv6)
12300 			ipif_check_bcast_ires(ipif);
12301 
12302 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
12303 		    !(turn_off & IPIF_UP)) {
12304 			need_up = B_TRUE;
12305 			if (ipif->ipif_flags & IPIF_UP)
12306 				ill->ill_logical_down = 1;
12307 			turn_on &= ~IPIF_UP;
12308 		}
12309 		err = ipif_down(ipif, q, mp);
12310 		ip1dbg(("ipif_down returns %d err ", err));
12311 		if (err == EINPROGRESS)
12312 			return (err);
12313 		ipif_down_tail(ipif);
12314 	}
12315 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12316 }
12317 
12318 static int
12319 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12320     boolean_t need_up)
12321 {
12322 	ill_t	*ill;
12323 	phyint_t *phyi;
12324 	uint64_t turn_on;
12325 	uint64_t turn_off;
12326 	uint64_t intf_flags;
12327 	boolean_t phyint_flags_modified = B_FALSE;
12328 	int	err = 0;
12329 	boolean_t set_linklocal = B_FALSE;
12330 	boolean_t zero_source = B_FALSE;
12331 
12332 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12333 		ipif->ipif_ill->ill_name, ipif->ipif_id));
12334 
12335 	ASSERT(IAM_WRITER_IPIF(ipif));
12336 
12337 	ill = ipif->ipif_ill;
12338 	phyi = ill->ill_phyint;
12339 
12340 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12341 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12342 
12343 	turn_off = intf_flags & turn_on;
12344 	turn_on ^= turn_off;
12345 
12346 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12347 		phyint_flags_modified = B_TRUE;
12348 
12349 	/*
12350 	 * Now we change the flags. Track current value of
12351 	 * other flags in their respective places.
12352 	 */
12353 	mutex_enter(&ill->ill_lock);
12354 	mutex_enter(&phyi->phyint_lock);
12355 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12356 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12357 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12358 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12359 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12360 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12361 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12362 		set_linklocal = B_TRUE;
12363 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12364 	}
12365 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12366 		zero_source = B_TRUE;
12367 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12368 	}
12369 	mutex_exit(&ill->ill_lock);
12370 	mutex_exit(&phyi->phyint_lock);
12371 
12372 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12373 		ip_redo_nomination(phyi);
12374 
12375 	if (set_linklocal)
12376 		(void) ipif_setlinklocal(ipif);
12377 
12378 	if (zero_source)
12379 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12380 	else
12381 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12382 
12383 	if (need_up) {
12384 		/*
12385 		 * XXX ipif_up really does not know whether a phyint flags
12386 		 * was modified or not. So, it sends up information on
12387 		 * only one routing sockets message. As we don't bring up
12388 		 * the interface and also set STANDBY/FAILED simultaneously
12389 		 * it should be okay.
12390 		 */
12391 		err = ipif_up(ipif, q, mp);
12392 	} else {
12393 		/*
12394 		 * Make sure routing socket sees all changes to the flags.
12395 		 * ipif_up_done* handles this when we use ipif_up.
12396 		 */
12397 		if (phyint_flags_modified) {
12398 			if (phyi->phyint_illv4 != NULL) {
12399 				ip_rts_ifmsg(phyi->phyint_illv4->
12400 				    ill_ipif);
12401 			}
12402 			if (phyi->phyint_illv6 != NULL) {
12403 				ip_rts_ifmsg(phyi->phyint_illv6->
12404 				    ill_ipif);
12405 			}
12406 		} else {
12407 			ip_rts_ifmsg(ipif);
12408 		}
12409 		/*
12410 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12411 		 * this in need_up case.
12412 		 */
12413 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12414 	}
12415 	return (err);
12416 }
12417 
12418 /*
12419  * Restart entry point to restart the flags restart operation after the
12420  * refcounts have dropped to zero.
12421  */
12422 /* ARGSUSED */
12423 int
12424 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12425     ip_ioctl_cmd_t *ipip, void *if_req)
12426 {
12427 	int	err;
12428 	struct ifreq *ifr = (struct ifreq *)if_req;
12429 	struct lifreq *lifr = (struct lifreq *)if_req;
12430 
12431 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12432 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12433 
12434 	ipif_down_tail(ipif);
12435 	if (ipip->ipi_cmd_type == IF_CMD) {
12436 		/*
12437 		 * Since ip_sioctl_flags expects an int and ifr_flags
12438 		 * is a short we need to cast ifr_flags into an int
12439 		 * to avoid having sign extension cause bits to get
12440 		 * set that should not be.
12441 		 */
12442 		err = ip_sioctl_flags_tail(ipif,
12443 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12444 		    q, mp, B_TRUE);
12445 	} else {
12446 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12447 		    q, mp, B_TRUE);
12448 	}
12449 	return (err);
12450 }
12451 
12452 /*
12453  * Can operate on either a module or a driver queue.
12454  */
12455 /* ARGSUSED */
12456 int
12457 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12458     ip_ioctl_cmd_t *ipip, void *if_req)
12459 {
12460 	/*
12461 	 * Has the flags been set correctly till now ?
12462 	 */
12463 	ill_t *ill = ipif->ipif_ill;
12464 	phyint_t *phyi = ill->ill_phyint;
12465 
12466 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12467 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12468 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12469 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12470 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12471 
12472 	/*
12473 	 * Need a lock since some flags can be set even when there are
12474 	 * references to the ipif.
12475 	 */
12476 	mutex_enter(&ill->ill_lock);
12477 	if (ipip->ipi_cmd_type == IF_CMD) {
12478 		struct ifreq *ifr = (struct ifreq *)if_req;
12479 
12480 		/* Get interface flags (low 16 only). */
12481 		ifr->ifr_flags = ((ipif->ipif_flags |
12482 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12483 	} else {
12484 		struct lifreq *lifr = (struct lifreq *)if_req;
12485 
12486 		/* Get interface flags. */
12487 		lifr->lifr_flags = ipif->ipif_flags |
12488 		    ill->ill_flags | phyi->phyint_flags;
12489 	}
12490 	mutex_exit(&ill->ill_lock);
12491 	return (0);
12492 }
12493 
12494 /* ARGSUSED */
12495 int
12496 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12497     ip_ioctl_cmd_t *ipip, void *if_req)
12498 {
12499 	int mtu;
12500 	int ip_min_mtu;
12501 	struct ifreq	*ifr;
12502 	struct lifreq *lifr;
12503 	ire_t	*ire;
12504 	ip_stack_t *ipst;
12505 
12506 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12507 	    ipif->ipif_id, (void *)ipif));
12508 	if (ipip->ipi_cmd_type == IF_CMD) {
12509 		ifr = (struct ifreq *)if_req;
12510 		mtu = ifr->ifr_metric;
12511 	} else {
12512 		lifr = (struct lifreq *)if_req;
12513 		mtu = lifr->lifr_mtu;
12514 	}
12515 
12516 	if (ipif->ipif_isv6)
12517 		ip_min_mtu = IPV6_MIN_MTU;
12518 	else
12519 		ip_min_mtu = IP_MIN_MTU;
12520 
12521 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12522 		return (EINVAL);
12523 
12524 	/*
12525 	 * Change the MTU size in all relevant ire's.
12526 	 * Mtu change Vs. new ire creation - protocol below.
12527 	 * First change ipif_mtu and the ire_max_frag of the
12528 	 * interface ire. Then do an ire walk and change the
12529 	 * ire_max_frag of all affected ires. During ire_add
12530 	 * under the bucket lock, set the ire_max_frag of the
12531 	 * new ire being created from the ipif/ire from which
12532 	 * it is being derived. If an mtu change happens after
12533 	 * the ire is added, the new ire will be cleaned up.
12534 	 * Conversely if the mtu change happens before the ire
12535 	 * is added, ire_add will see the new value of the mtu.
12536 	 */
12537 	ipif->ipif_mtu = mtu;
12538 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12539 
12540 	if (ipif->ipif_isv6)
12541 		ire = ipif_to_ire_v6(ipif);
12542 	else
12543 		ire = ipif_to_ire(ipif);
12544 	if (ire != NULL) {
12545 		ire->ire_max_frag = ipif->ipif_mtu;
12546 		ire_refrele(ire);
12547 	}
12548 	ipst = ipif->ipif_ill->ill_ipst;
12549 	if (ipif->ipif_flags & IPIF_UP) {
12550 		if (ipif->ipif_isv6)
12551 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12552 			    ipst);
12553 		else
12554 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12555 			    ipst);
12556 	}
12557 	/* Update the MTU in SCTP's list */
12558 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12559 	return (0);
12560 }
12561 
12562 /* Get interface MTU. */
12563 /* ARGSUSED */
12564 int
12565 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12566 	ip_ioctl_cmd_t *ipip, void *if_req)
12567 {
12568 	struct ifreq	*ifr;
12569 	struct lifreq	*lifr;
12570 
12571 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12572 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12573 	if (ipip->ipi_cmd_type == IF_CMD) {
12574 		ifr = (struct ifreq *)if_req;
12575 		ifr->ifr_metric = ipif->ipif_mtu;
12576 	} else {
12577 		lifr = (struct lifreq *)if_req;
12578 		lifr->lifr_mtu = ipif->ipif_mtu;
12579 	}
12580 	return (0);
12581 }
12582 
12583 /* Set interface broadcast address. */
12584 /* ARGSUSED2 */
12585 int
12586 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12587 	ip_ioctl_cmd_t *ipip, void *if_req)
12588 {
12589 	ipaddr_t addr;
12590 	ire_t	*ire;
12591 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12592 
12593 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12594 	    ipif->ipif_id));
12595 
12596 	ASSERT(IAM_WRITER_IPIF(ipif));
12597 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12598 		return (EADDRNOTAVAIL);
12599 
12600 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12601 
12602 	if (sin->sin_family != AF_INET)
12603 		return (EAFNOSUPPORT);
12604 
12605 	addr = sin->sin_addr.s_addr;
12606 	if (ipif->ipif_flags & IPIF_UP) {
12607 		/*
12608 		 * If we are already up, make sure the new
12609 		 * broadcast address makes sense.  If it does,
12610 		 * there should be an IRE for it already.
12611 		 * Don't match on ipif, only on the ill
12612 		 * since we are sharing these now. Don't use
12613 		 * MATCH_IRE_ILL_GROUP as we are looking for
12614 		 * the broadcast ire on this ill and each ill
12615 		 * in the group has its own broadcast ire.
12616 		 */
12617 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12618 		    ipif, ALL_ZONES, NULL,
12619 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12620 		if (ire == NULL) {
12621 			return (EINVAL);
12622 		} else {
12623 			ire_refrele(ire);
12624 		}
12625 	}
12626 	/*
12627 	 * Changing the broadcast addr for this ipif.
12628 	 * Make sure we have valid net and subnet bcast
12629 	 * ire's for other logical interfaces, if needed.
12630 	 */
12631 	if (addr != ipif->ipif_brd_addr)
12632 		ipif_check_bcast_ires(ipif);
12633 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12634 	return (0);
12635 }
12636 
12637 /* Get interface broadcast address. */
12638 /* ARGSUSED */
12639 int
12640 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12641     ip_ioctl_cmd_t *ipip, void *if_req)
12642 {
12643 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12644 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12645 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12646 		return (EADDRNOTAVAIL);
12647 
12648 	/* IPIF_BROADCAST not possible with IPv6 */
12649 	ASSERT(!ipif->ipif_isv6);
12650 	*sin = sin_null;
12651 	sin->sin_family = AF_INET;
12652 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12653 	return (0);
12654 }
12655 
12656 /*
12657  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12658  */
12659 /* ARGSUSED */
12660 int
12661 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12662     ip_ioctl_cmd_t *ipip, void *if_req)
12663 {
12664 	int err = 0;
12665 	in6_addr_t v6mask;
12666 
12667 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12668 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12669 
12670 	ASSERT(IAM_WRITER_IPIF(ipif));
12671 
12672 	if (ipif->ipif_isv6) {
12673 		sin6_t *sin6;
12674 
12675 		if (sin->sin_family != AF_INET6)
12676 			return (EAFNOSUPPORT);
12677 
12678 		sin6 = (sin6_t *)sin;
12679 		v6mask = sin6->sin6_addr;
12680 	} else {
12681 		ipaddr_t mask;
12682 
12683 		if (sin->sin_family != AF_INET)
12684 			return (EAFNOSUPPORT);
12685 
12686 		mask = sin->sin_addr.s_addr;
12687 		V4MASK_TO_V6(mask, v6mask);
12688 	}
12689 
12690 	/*
12691 	 * No big deal if the interface isn't already up, or the mask
12692 	 * isn't really changing, or this is pt-pt.
12693 	 */
12694 	if (!(ipif->ipif_flags & IPIF_UP) ||
12695 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12696 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12697 		ipif->ipif_v6net_mask = v6mask;
12698 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12699 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12700 			    ipif->ipif_v6net_mask,
12701 			    ipif->ipif_v6subnet);
12702 		}
12703 		return (0);
12704 	}
12705 	/*
12706 	 * Make sure we have valid net and subnet broadcast ire's
12707 	 * for the old netmask, if needed by other logical interfaces.
12708 	 */
12709 	if (!ipif->ipif_isv6)
12710 		ipif_check_bcast_ires(ipif);
12711 
12712 	err = ipif_logical_down(ipif, q, mp);
12713 	if (err == EINPROGRESS)
12714 		return (err);
12715 	ipif_down_tail(ipif);
12716 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12717 	return (err);
12718 }
12719 
12720 static int
12721 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12722 {
12723 	in6_addr_t v6mask;
12724 	int err = 0;
12725 
12726 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12727 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12728 
12729 	if (ipif->ipif_isv6) {
12730 		sin6_t *sin6;
12731 
12732 		sin6 = (sin6_t *)sin;
12733 		v6mask = sin6->sin6_addr;
12734 	} else {
12735 		ipaddr_t mask;
12736 
12737 		mask = sin->sin_addr.s_addr;
12738 		V4MASK_TO_V6(mask, v6mask);
12739 	}
12740 
12741 	ipif->ipif_v6net_mask = v6mask;
12742 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12743 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12744 		    ipif->ipif_v6subnet);
12745 	}
12746 	err = ipif_up(ipif, q, mp);
12747 
12748 	if (err == 0 || err == EINPROGRESS) {
12749 		/*
12750 		 * The interface must be DL_BOUND if this packet has to
12751 		 * go out on the wire. Since we only go through a logical
12752 		 * down and are bound with the driver during an internal
12753 		 * down/up that is satisfied.
12754 		 */
12755 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12756 			/* Potentially broadcast an address mask reply. */
12757 			ipif_mask_reply(ipif);
12758 		}
12759 	}
12760 	return (err);
12761 }
12762 
12763 /* ARGSUSED */
12764 int
12765 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12766     ip_ioctl_cmd_t *ipip, void *if_req)
12767 {
12768 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12769 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12770 	ipif_down_tail(ipif);
12771 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12772 }
12773 
12774 /* Get interface net mask. */
12775 /* ARGSUSED */
12776 int
12777 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12778     ip_ioctl_cmd_t *ipip, void *if_req)
12779 {
12780 	struct lifreq *lifr = (struct lifreq *)if_req;
12781 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12782 
12783 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12784 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12785 
12786 	/*
12787 	 * net mask can't change since we have a reference to the ipif.
12788 	 */
12789 	if (ipif->ipif_isv6) {
12790 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12791 		*sin6 = sin6_null;
12792 		sin6->sin6_family = AF_INET6;
12793 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12794 		lifr->lifr_addrlen =
12795 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12796 	} else {
12797 		*sin = sin_null;
12798 		sin->sin_family = AF_INET;
12799 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12800 		if (ipip->ipi_cmd_type == LIF_CMD) {
12801 			lifr->lifr_addrlen =
12802 			    ip_mask_to_plen(ipif->ipif_net_mask);
12803 		}
12804 	}
12805 	return (0);
12806 }
12807 
12808 /* ARGSUSED */
12809 int
12810 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12811     ip_ioctl_cmd_t *ipip, void *if_req)
12812 {
12813 
12814 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12815 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12816 	/*
12817 	 * Set interface metric.  We don't use this for
12818 	 * anything but we keep track of it in case it is
12819 	 * important to routing applications or such.
12820 	 */
12821 	if (ipip->ipi_cmd_type == IF_CMD) {
12822 		struct ifreq    *ifr;
12823 
12824 		ifr = (struct ifreq *)if_req;
12825 		ipif->ipif_metric = ifr->ifr_metric;
12826 	} else {
12827 		struct lifreq   *lifr;
12828 
12829 		lifr = (struct lifreq *)if_req;
12830 		ipif->ipif_metric = lifr->lifr_metric;
12831 	}
12832 	return (0);
12833 }
12834 
12835 
12836 /* ARGSUSED */
12837 int
12838 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12839     ip_ioctl_cmd_t *ipip, void *if_req)
12840 {
12841 
12842 	/* Get interface metric. */
12843 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12844 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12845 	if (ipip->ipi_cmd_type == IF_CMD) {
12846 		struct ifreq    *ifr;
12847 
12848 		ifr = (struct ifreq *)if_req;
12849 		ifr->ifr_metric = ipif->ipif_metric;
12850 	} else {
12851 		struct lifreq   *lifr;
12852 
12853 		lifr = (struct lifreq *)if_req;
12854 		lifr->lifr_metric = ipif->ipif_metric;
12855 	}
12856 
12857 	return (0);
12858 }
12859 
12860 /* ARGSUSED */
12861 int
12862 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12863     ip_ioctl_cmd_t *ipip, void *if_req)
12864 {
12865 
12866 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12867 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12868 	/*
12869 	 * Set the muxid returned from I_PLINK.
12870 	 */
12871 	if (ipip->ipi_cmd_type == IF_CMD) {
12872 		struct ifreq *ifr = (struct ifreq *)if_req;
12873 
12874 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12875 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12876 	} else {
12877 		struct lifreq *lifr = (struct lifreq *)if_req;
12878 
12879 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12880 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12881 	}
12882 	return (0);
12883 }
12884 
12885 /* ARGSUSED */
12886 int
12887 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12888     ip_ioctl_cmd_t *ipip, void *if_req)
12889 {
12890 
12891 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12892 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12893 	/*
12894 	 * Get the muxid saved in ill for I_PUNLINK.
12895 	 */
12896 	if (ipip->ipi_cmd_type == IF_CMD) {
12897 		struct ifreq *ifr = (struct ifreq *)if_req;
12898 
12899 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12900 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12901 	} else {
12902 		struct lifreq *lifr = (struct lifreq *)if_req;
12903 
12904 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12905 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12906 	}
12907 	return (0);
12908 }
12909 
12910 /*
12911  * Set the subnet prefix. Does not modify the broadcast address.
12912  */
12913 /* ARGSUSED */
12914 int
12915 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12916     ip_ioctl_cmd_t *ipip, void *if_req)
12917 {
12918 	int err = 0;
12919 	in6_addr_t v6addr;
12920 	in6_addr_t v6mask;
12921 	boolean_t need_up = B_FALSE;
12922 	int addrlen;
12923 
12924 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12925 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12926 
12927 	ASSERT(IAM_WRITER_IPIF(ipif));
12928 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12929 
12930 	if (ipif->ipif_isv6) {
12931 		sin6_t *sin6;
12932 
12933 		if (sin->sin_family != AF_INET6)
12934 			return (EAFNOSUPPORT);
12935 
12936 		sin6 = (sin6_t *)sin;
12937 		v6addr = sin6->sin6_addr;
12938 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12939 			return (EADDRNOTAVAIL);
12940 	} else {
12941 		ipaddr_t addr;
12942 
12943 		if (sin->sin_family != AF_INET)
12944 			return (EAFNOSUPPORT);
12945 
12946 		addr = sin->sin_addr.s_addr;
12947 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12948 			return (EADDRNOTAVAIL);
12949 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12950 		/* Add 96 bits */
12951 		addrlen += IPV6_ABITS - IP_ABITS;
12952 	}
12953 
12954 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12955 		return (EINVAL);
12956 
12957 	/* Check if bits in the address is set past the mask */
12958 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12959 		return (EINVAL);
12960 
12961 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12962 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12963 		return (0);	/* No change */
12964 
12965 	if (ipif->ipif_flags & IPIF_UP) {
12966 		/*
12967 		 * If the interface is already marked up,
12968 		 * we call ipif_down which will take care
12969 		 * of ditching any IREs that have been set
12970 		 * up based on the old interface address.
12971 		 */
12972 		err = ipif_logical_down(ipif, q, mp);
12973 		if (err == EINPROGRESS)
12974 			return (err);
12975 		ipif_down_tail(ipif);
12976 		need_up = B_TRUE;
12977 	}
12978 
12979 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12980 	return (err);
12981 }
12982 
12983 static int
12984 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12985     queue_t *q, mblk_t *mp, boolean_t need_up)
12986 {
12987 	ill_t	*ill = ipif->ipif_ill;
12988 	int	err = 0;
12989 
12990 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12991 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12992 
12993 	/* Set the new address. */
12994 	mutex_enter(&ill->ill_lock);
12995 	ipif->ipif_v6net_mask = v6mask;
12996 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12997 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12998 		    ipif->ipif_v6subnet);
12999 	}
13000 	mutex_exit(&ill->ill_lock);
13001 
13002 	if (need_up) {
13003 		/*
13004 		 * Now bring the interface back up.  If this
13005 		 * is the only IPIF for the ILL, ipif_up
13006 		 * will have to re-bind to the device, so
13007 		 * we may get back EINPROGRESS, in which
13008 		 * case, this IOCTL will get completed in
13009 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
13010 		 */
13011 		err = ipif_up(ipif, q, mp);
13012 		if (err == EINPROGRESS)
13013 			return (err);
13014 	}
13015 	return (err);
13016 }
13017 
13018 /* ARGSUSED */
13019 int
13020 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13021     ip_ioctl_cmd_t *ipip, void *if_req)
13022 {
13023 	int	addrlen;
13024 	in6_addr_t v6addr;
13025 	in6_addr_t v6mask;
13026 	struct lifreq *lifr = (struct lifreq *)if_req;
13027 
13028 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
13029 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13030 	ipif_down_tail(ipif);
13031 
13032 	addrlen = lifr->lifr_addrlen;
13033 	if (ipif->ipif_isv6) {
13034 		sin6_t *sin6;
13035 
13036 		sin6 = (sin6_t *)sin;
13037 		v6addr = sin6->sin6_addr;
13038 	} else {
13039 		ipaddr_t addr;
13040 
13041 		addr = sin->sin_addr.s_addr;
13042 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
13043 		addrlen += IPV6_ABITS - IP_ABITS;
13044 	}
13045 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
13046 
13047 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
13048 }
13049 
13050 /* ARGSUSED */
13051 int
13052 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13053     ip_ioctl_cmd_t *ipip, void *if_req)
13054 {
13055 	struct lifreq *lifr = (struct lifreq *)if_req;
13056 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
13057 
13058 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
13059 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13060 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
13061 
13062 	if (ipif->ipif_isv6) {
13063 		*sin6 = sin6_null;
13064 		sin6->sin6_family = AF_INET6;
13065 		sin6->sin6_addr = ipif->ipif_v6subnet;
13066 		lifr->lifr_addrlen =
13067 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
13068 	} else {
13069 		*sin = sin_null;
13070 		sin->sin_family = AF_INET;
13071 		sin->sin_addr.s_addr = ipif->ipif_subnet;
13072 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
13073 	}
13074 	return (0);
13075 }
13076 
13077 /*
13078  * Set the IPv6 address token.
13079  */
13080 /* ARGSUSED */
13081 int
13082 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13083     ip_ioctl_cmd_t *ipi, void *if_req)
13084 {
13085 	ill_t *ill = ipif->ipif_ill;
13086 	int err;
13087 	in6_addr_t v6addr;
13088 	in6_addr_t v6mask;
13089 	boolean_t need_up = B_FALSE;
13090 	int i;
13091 	sin6_t *sin6 = (sin6_t *)sin;
13092 	struct lifreq *lifr = (struct lifreq *)if_req;
13093 	int addrlen;
13094 
13095 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
13096 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13097 	ASSERT(IAM_WRITER_IPIF(ipif));
13098 
13099 	addrlen = lifr->lifr_addrlen;
13100 	/* Only allow for logical unit zero i.e. not on "le0:17" */
13101 	if (ipif->ipif_id != 0)
13102 		return (EINVAL);
13103 
13104 	if (!ipif->ipif_isv6)
13105 		return (EINVAL);
13106 
13107 	if (addrlen > IPV6_ABITS)
13108 		return (EINVAL);
13109 
13110 	v6addr = sin6->sin6_addr;
13111 
13112 	/*
13113 	 * The length of the token is the length from the end.  To get
13114 	 * the proper mask for this, compute the mask of the bits not
13115 	 * in the token; ie. the prefix, and then xor to get the mask.
13116 	 */
13117 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
13118 		return (EINVAL);
13119 	for (i = 0; i < 4; i++) {
13120 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
13121 	}
13122 
13123 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
13124 	    ill->ill_token_length == addrlen)
13125 		return (0);	/* No change */
13126 
13127 	if (ipif->ipif_flags & IPIF_UP) {
13128 		err = ipif_logical_down(ipif, q, mp);
13129 		if (err == EINPROGRESS)
13130 			return (err);
13131 		ipif_down_tail(ipif);
13132 		need_up = B_TRUE;
13133 	}
13134 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
13135 	return (err);
13136 }
13137 
13138 static int
13139 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
13140     mblk_t *mp, boolean_t need_up)
13141 {
13142 	in6_addr_t v6addr;
13143 	in6_addr_t v6mask;
13144 	ill_t	*ill = ipif->ipif_ill;
13145 	int	i;
13146 	int	err = 0;
13147 
13148 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
13149 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13150 	v6addr = sin6->sin6_addr;
13151 	/*
13152 	 * The length of the token is the length from the end.  To get
13153 	 * the proper mask for this, compute the mask of the bits not
13154 	 * in the token; ie. the prefix, and then xor to get the mask.
13155 	 */
13156 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
13157 	for (i = 0; i < 4; i++)
13158 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
13159 
13160 	mutex_enter(&ill->ill_lock);
13161 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
13162 	ill->ill_token_length = addrlen;
13163 	mutex_exit(&ill->ill_lock);
13164 
13165 	if (need_up) {
13166 		/*
13167 		 * Now bring the interface back up.  If this
13168 		 * is the only IPIF for the ILL, ipif_up
13169 		 * will have to re-bind to the device, so
13170 		 * we may get back EINPROGRESS, in which
13171 		 * case, this IOCTL will get completed in
13172 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
13173 		 */
13174 		err = ipif_up(ipif, q, mp);
13175 		if (err == EINPROGRESS)
13176 			return (err);
13177 	}
13178 	return (err);
13179 }
13180 
13181 /* ARGSUSED */
13182 int
13183 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13184     ip_ioctl_cmd_t *ipi, void *if_req)
13185 {
13186 	ill_t *ill;
13187 	sin6_t *sin6 = (sin6_t *)sin;
13188 	struct lifreq *lifr = (struct lifreq *)if_req;
13189 
13190 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
13191 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13192 	if (ipif->ipif_id != 0)
13193 		return (EINVAL);
13194 
13195 	ill = ipif->ipif_ill;
13196 	if (!ill->ill_isv6)
13197 		return (ENXIO);
13198 
13199 	*sin6 = sin6_null;
13200 	sin6->sin6_family = AF_INET6;
13201 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
13202 	sin6->sin6_addr = ill->ill_token;
13203 	lifr->lifr_addrlen = ill->ill_token_length;
13204 	return (0);
13205 }
13206 
13207 /*
13208  * Set (hardware) link specific information that might override
13209  * what was acquired through the DL_INFO_ACK.
13210  * The logic is as follows.
13211  *
13212  * become exclusive
13213  * set CHANGING flag
13214  * change mtu on affected IREs
13215  * clear CHANGING flag
13216  *
13217  * An ire add that occurs before the CHANGING flag is set will have its mtu
13218  * changed by the ip_sioctl_lnkinfo.
13219  *
13220  * During the time the CHANGING flag is set, no new ires will be added to the
13221  * bucket, and ire add will fail (due the CHANGING flag).
13222  *
13223  * An ire add that occurs after the CHANGING flag is set will have the right mtu
13224  * before it is added to the bucket.
13225  *
13226  * Obviously only 1 thread can set the CHANGING flag and we need to become
13227  * exclusive to set the flag.
13228  */
13229 /* ARGSUSED */
13230 int
13231 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13232     ip_ioctl_cmd_t *ipi, void *if_req)
13233 {
13234 	ill_t		*ill = ipif->ipif_ill;
13235 	ipif_t		*nipif;
13236 	int		ip_min_mtu;
13237 	boolean_t	mtu_walk = B_FALSE;
13238 	struct lifreq	*lifr = (struct lifreq *)if_req;
13239 	lif_ifinfo_req_t *lir;
13240 	ire_t		*ire;
13241 
13242 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
13243 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13244 	lir = &lifr->lifr_ifinfo;
13245 	ASSERT(IAM_WRITER_IPIF(ipif));
13246 
13247 	/* Only allow for logical unit zero i.e. not on "le0:17" */
13248 	if (ipif->ipif_id != 0)
13249 		return (EINVAL);
13250 
13251 	/* Set interface MTU. */
13252 	if (ipif->ipif_isv6)
13253 		ip_min_mtu = IPV6_MIN_MTU;
13254 	else
13255 		ip_min_mtu = IP_MIN_MTU;
13256 
13257 	/*
13258 	 * Verify values before we set anything. Allow zero to
13259 	 * mean unspecified.
13260 	 */
13261 	if (lir->lir_maxmtu != 0 &&
13262 	    (lir->lir_maxmtu > ill->ill_max_frag ||
13263 	    lir->lir_maxmtu < ip_min_mtu))
13264 		return (EINVAL);
13265 	if (lir->lir_reachtime != 0 &&
13266 	    lir->lir_reachtime > ND_MAX_REACHTIME)
13267 		return (EINVAL);
13268 	if (lir->lir_reachretrans != 0 &&
13269 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
13270 		return (EINVAL);
13271 
13272 	mutex_enter(&ill->ill_lock);
13273 	ill->ill_state_flags |= ILL_CHANGING;
13274 	for (nipif = ill->ill_ipif; nipif != NULL;
13275 	    nipif = nipif->ipif_next) {
13276 		nipif->ipif_state_flags |= IPIF_CHANGING;
13277 	}
13278 
13279 	mutex_exit(&ill->ill_lock);
13280 
13281 	if (lir->lir_maxmtu != 0) {
13282 		ill->ill_max_mtu = lir->lir_maxmtu;
13283 		ill->ill_mtu_userspecified = 1;
13284 		mtu_walk = B_TRUE;
13285 	}
13286 
13287 	if (lir->lir_reachtime != 0)
13288 		ill->ill_reachable_time = lir->lir_reachtime;
13289 
13290 	if (lir->lir_reachretrans != 0)
13291 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
13292 
13293 	ill->ill_max_hops = lir->lir_maxhops;
13294 
13295 	ill->ill_max_buf = ND_MAX_Q;
13296 
13297 	if (mtu_walk) {
13298 		/*
13299 		 * Set the MTU on all ipifs associated with this ill except
13300 		 * for those whose MTU was fixed via SIOCSLIFMTU.
13301 		 */
13302 		for (nipif = ill->ill_ipif; nipif != NULL;
13303 		    nipif = nipif->ipif_next) {
13304 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
13305 				continue;
13306 
13307 			nipif->ipif_mtu = ill->ill_max_mtu;
13308 
13309 			if (!(nipif->ipif_flags & IPIF_UP))
13310 				continue;
13311 
13312 			if (nipif->ipif_isv6)
13313 				ire = ipif_to_ire_v6(nipif);
13314 			else
13315 				ire = ipif_to_ire(nipif);
13316 			if (ire != NULL) {
13317 				ire->ire_max_frag = ipif->ipif_mtu;
13318 				ire_refrele(ire);
13319 			}
13320 			if (ill->ill_isv6) {
13321 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13322 				    ipif_mtu_change, (char *)nipif,
13323 				    ill);
13324 			} else {
13325 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13326 				    ipif_mtu_change, (char *)nipif,
13327 				    ill);
13328 			}
13329 		}
13330 	}
13331 
13332 	mutex_enter(&ill->ill_lock);
13333 	for (nipif = ill->ill_ipif; nipif != NULL;
13334 	    nipif = nipif->ipif_next) {
13335 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13336 	}
13337 	ILL_UNMARK_CHANGING(ill);
13338 	mutex_exit(&ill->ill_lock);
13339 
13340 	return (0);
13341 }
13342 
13343 /* ARGSUSED */
13344 int
13345 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13346     ip_ioctl_cmd_t *ipi, void *if_req)
13347 {
13348 	struct lif_ifinfo_req *lir;
13349 	ill_t *ill = ipif->ipif_ill;
13350 
13351 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13352 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13353 	if (ipif->ipif_id != 0)
13354 		return (EINVAL);
13355 
13356 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13357 	lir->lir_maxhops = ill->ill_max_hops;
13358 	lir->lir_reachtime = ill->ill_reachable_time;
13359 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13360 	lir->lir_maxmtu = ill->ill_max_mtu;
13361 
13362 	return (0);
13363 }
13364 
13365 /*
13366  * Return best guess as to the subnet mask for the specified address.
13367  * Based on the subnet masks for all the configured interfaces.
13368  *
13369  * We end up returning a zero mask in the case of default, multicast or
13370  * experimental.
13371  */
13372 static ipaddr_t
13373 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13374 {
13375 	ipaddr_t net_mask;
13376 	ill_t	*ill;
13377 	ipif_t	*ipif;
13378 	ill_walk_context_t ctx;
13379 	ipif_t	*fallback_ipif = NULL;
13380 
13381 	net_mask = ip_net_mask(addr);
13382 	if (net_mask == 0) {
13383 		*ipifp = NULL;
13384 		return (0);
13385 	}
13386 
13387 	/* Let's check to see if this is maybe a local subnet route. */
13388 	/* this function only applies to IPv4 interfaces */
13389 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13390 	ill = ILL_START_WALK_V4(&ctx, ipst);
13391 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13392 		mutex_enter(&ill->ill_lock);
13393 		for (ipif = ill->ill_ipif; ipif != NULL;
13394 		    ipif = ipif->ipif_next) {
13395 			if (!IPIF_CAN_LOOKUP(ipif))
13396 				continue;
13397 			if (!(ipif->ipif_flags & IPIF_UP))
13398 				continue;
13399 			if ((ipif->ipif_subnet & net_mask) ==
13400 			    (addr & net_mask)) {
13401 				/*
13402 				 * Don't trust pt-pt interfaces if there are
13403 				 * other interfaces.
13404 				 */
13405 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13406 					if (fallback_ipif == NULL) {
13407 						ipif_refhold_locked(ipif);
13408 						fallback_ipif = ipif;
13409 					}
13410 					continue;
13411 				}
13412 
13413 				/*
13414 				 * Fine. Just assume the same net mask as the
13415 				 * directly attached subnet interface is using.
13416 				 */
13417 				ipif_refhold_locked(ipif);
13418 				mutex_exit(&ill->ill_lock);
13419 				rw_exit(&ipst->ips_ill_g_lock);
13420 				if (fallback_ipif != NULL)
13421 					ipif_refrele(fallback_ipif);
13422 				*ipifp = ipif;
13423 				return (ipif->ipif_net_mask);
13424 			}
13425 		}
13426 		mutex_exit(&ill->ill_lock);
13427 	}
13428 	rw_exit(&ipst->ips_ill_g_lock);
13429 
13430 	*ipifp = fallback_ipif;
13431 	return ((fallback_ipif != NULL) ?
13432 	    fallback_ipif->ipif_net_mask : net_mask);
13433 }
13434 
13435 /*
13436  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13437  */
13438 static void
13439 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13440 {
13441 	IOCP	iocp;
13442 	ipft_t	*ipft;
13443 	ipllc_t	*ipllc;
13444 	mblk_t	*mp1;
13445 	cred_t	*cr;
13446 	int	error = 0;
13447 	conn_t	*connp;
13448 
13449 	ip1dbg(("ip_wput_ioctl"));
13450 	iocp = (IOCP)mp->b_rptr;
13451 	mp1 = mp->b_cont;
13452 	if (mp1 == NULL) {
13453 		iocp->ioc_error = EINVAL;
13454 		mp->b_datap->db_type = M_IOCNAK;
13455 		iocp->ioc_count = 0;
13456 		qreply(q, mp);
13457 		return;
13458 	}
13459 
13460 	/*
13461 	 * These IOCTLs provide various control capabilities to
13462 	 * upstream agents such as ULPs and processes.	There
13463 	 * are currently two such IOCTLs implemented.  They
13464 	 * are used by TCP to provide update information for
13465 	 * existing IREs and to forcibly delete an IRE for a
13466 	 * host that is not responding, thereby forcing an
13467 	 * attempt at a new route.
13468 	 */
13469 	iocp->ioc_error = EINVAL;
13470 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13471 		goto done;
13472 
13473 	ipllc = (ipllc_t *)mp1->b_rptr;
13474 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13475 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13476 			break;
13477 	}
13478 	/*
13479 	 * prefer credential from mblk over ioctl;
13480 	 * see ip_sioctl_copyin_setup
13481 	 */
13482 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13483 
13484 	/*
13485 	 * Refhold the conn in case the request gets queued up in some lookup
13486 	 */
13487 	ASSERT(CONN_Q(q));
13488 	connp = Q_TO_CONN(q);
13489 	CONN_INC_REF(connp);
13490 	if (ipft->ipft_pfi &&
13491 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13492 		pullupmsg(mp1, ipft->ipft_min_size))) {
13493 		error = (*ipft->ipft_pfi)(q,
13494 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13495 	}
13496 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13497 		/*
13498 		 * CONN_OPER_PENDING_DONE happens in the function called
13499 		 * through ipft_pfi above.
13500 		 */
13501 		return;
13502 	}
13503 
13504 	CONN_OPER_PENDING_DONE(connp);
13505 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13506 		freemsg(mp);
13507 		return;
13508 	}
13509 	iocp->ioc_error = error;
13510 
13511 done:
13512 	mp->b_datap->db_type = M_IOCACK;
13513 	if (iocp->ioc_error)
13514 		iocp->ioc_count = 0;
13515 	qreply(q, mp);
13516 }
13517 
13518 /*
13519  * Lookup an ipif using the sequence id (ipif_seqid)
13520  */
13521 ipif_t *
13522 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13523 {
13524 	ipif_t *ipif;
13525 
13526 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13527 
13528 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13529 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13530 			return (ipif);
13531 	}
13532 	return (NULL);
13533 }
13534 
13535 /*
13536  * Assign a unique id for the ipif. This is used later when we send
13537  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13538  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13539  * IRE is added, we verify that ipif has not disappeared.
13540  */
13541 
13542 static void
13543 ipif_assign_seqid(ipif_t *ipif)
13544 {
13545 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13546 
13547 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13548 }
13549 
13550 /*
13551  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13552  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13553  * be inserted into the first space available in the list. The value of
13554  * ipif_id will then be set to the appropriate value for its position.
13555  */
13556 static int
13557 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13558 {
13559 	ill_t *ill;
13560 	ipif_t *tipif;
13561 	ipif_t **tipifp;
13562 	int id;
13563 	ip_stack_t	*ipst;
13564 
13565 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13566 	    IAM_WRITER_IPIF(ipif));
13567 
13568 	ill = ipif->ipif_ill;
13569 	ASSERT(ill != NULL);
13570 	ipst = ill->ill_ipst;
13571 
13572 	/*
13573 	 * In the case of lo0:0 we already hold the ill_g_lock.
13574 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13575 	 * ipif_insert. Another such caller is ipif_move.
13576 	 */
13577 	if (acquire_g_lock)
13578 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13579 	if (acquire_ill_lock)
13580 		mutex_enter(&ill->ill_lock);
13581 	id = ipif->ipif_id;
13582 	tipifp = &(ill->ill_ipif);
13583 	if (id == -1) {	/* need to find a real id */
13584 		id = 0;
13585 		while ((tipif = *tipifp) != NULL) {
13586 			ASSERT(tipif->ipif_id >= id);
13587 			if (tipif->ipif_id != id)
13588 				break; /* non-consecutive id */
13589 			id++;
13590 			tipifp = &(tipif->ipif_next);
13591 		}
13592 		/* limit number of logical interfaces */
13593 		if (id >= ipst->ips_ip_addrs_per_if) {
13594 			if (acquire_ill_lock)
13595 				mutex_exit(&ill->ill_lock);
13596 			if (acquire_g_lock)
13597 				rw_exit(&ipst->ips_ill_g_lock);
13598 			return (-1);
13599 		}
13600 		ipif->ipif_id = id; /* assign new id */
13601 	} else if (id < ipst->ips_ip_addrs_per_if) {
13602 		/* we have a real id; insert ipif in the right place */
13603 		while ((tipif = *tipifp) != NULL) {
13604 			ASSERT(tipif->ipif_id != id);
13605 			if (tipif->ipif_id > id)
13606 				break; /* found correct location */
13607 			tipifp = &(tipif->ipif_next);
13608 		}
13609 	} else {
13610 		if (acquire_ill_lock)
13611 			mutex_exit(&ill->ill_lock);
13612 		if (acquire_g_lock)
13613 			rw_exit(&ipst->ips_ill_g_lock);
13614 		return (-1);
13615 	}
13616 
13617 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13618 
13619 	ipif->ipif_next = tipif;
13620 	*tipifp = ipif;
13621 	if (acquire_ill_lock)
13622 		mutex_exit(&ill->ill_lock);
13623 	if (acquire_g_lock)
13624 		rw_exit(&ipst->ips_ill_g_lock);
13625 	return (0);
13626 }
13627 
13628 /*
13629  * Allocate and initialize a new interface control structure.  (Always
13630  * called as writer.)
13631  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13632  * is not part of the global linked list of ills. ipif_seqid is unique
13633  * in the system and to preserve the uniqueness, it is assigned only
13634  * when ill becomes part of the global list. At that point ill will
13635  * have a name. If it doesn't get assigned here, it will get assigned
13636  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13637  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13638  * the interface flags or any other information from the DL_INFO_ACK for
13639  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13640  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13641  * second DL_INFO_ACK comes in from the driver.
13642  */
13643 static ipif_t *
13644 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13645 {
13646 	ipif_t	*ipif;
13647 	phyint_t *phyi;
13648 
13649 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13650 	    ill->ill_name, id, (void *)ill));
13651 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13652 
13653 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13654 		return (NULL);
13655 	*ipif = ipif_zero;	/* start clean */
13656 
13657 	ipif->ipif_ill = ill;
13658 	ipif->ipif_id = id;	/* could be -1 */
13659 	/*
13660 	 * Inherit the zoneid from the ill; for the shared stack instance
13661 	 * this is always the global zone
13662 	 */
13663 	ipif->ipif_zoneid = ill->ill_zoneid;
13664 
13665 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13666 
13667 	ipif->ipif_refcnt = 0;
13668 	ipif->ipif_saved_ire_cnt = 0;
13669 
13670 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13671 		mi_free(ipif);
13672 		return (NULL);
13673 	}
13674 	/* -1 id should have been replaced by real id */
13675 	id = ipif->ipif_id;
13676 	ASSERT(id >= 0);
13677 
13678 	if (ill->ill_name[0] != '\0')
13679 		ipif_assign_seqid(ipif);
13680 
13681 	/*
13682 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13683 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13684 	 * ioctl sets ipif_orig_ipifid to zero.
13685 	 */
13686 	ipif->ipif_orig_ipifid = id;
13687 
13688 	/*
13689 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13690 	 * The ipif is still not up and can't be looked up until the
13691 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13692 	 */
13693 	mutex_enter(&ill->ill_lock);
13694 	mutex_enter(&ill->ill_phyint->phyint_lock);
13695 	/*
13696 	 * Set the running flag when logical interface zero is created.
13697 	 * For subsequent logical interfaces, a DLPI link down
13698 	 * notification message may have cleared the running flag to
13699 	 * indicate the link is down, so we shouldn't just blindly set it.
13700 	 */
13701 	if (id == 0)
13702 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13703 	ipif->ipif_ire_type = ire_type;
13704 	phyi = ill->ill_phyint;
13705 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13706 
13707 	if (ipif->ipif_isv6) {
13708 		ill->ill_flags |= ILLF_IPV6;
13709 	} else {
13710 		ipaddr_t inaddr_any = INADDR_ANY;
13711 
13712 		ill->ill_flags |= ILLF_IPV4;
13713 
13714 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13715 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13716 		    &ipif->ipif_v6lcl_addr);
13717 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13718 		    &ipif->ipif_v6src_addr);
13719 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13720 		    &ipif->ipif_v6subnet);
13721 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13722 		    &ipif->ipif_v6net_mask);
13723 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13724 		    &ipif->ipif_v6brd_addr);
13725 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13726 		    &ipif->ipif_v6pp_dst_addr);
13727 	}
13728 
13729 	/*
13730 	 * Don't set the interface flags etc. now, will do it in
13731 	 * ip_ll_subnet_defaults.
13732 	 */
13733 	if (!initialize) {
13734 		mutex_exit(&ill->ill_lock);
13735 		mutex_exit(&ill->ill_phyint->phyint_lock);
13736 		return (ipif);
13737 	}
13738 	ipif->ipif_mtu = ill->ill_max_mtu;
13739 
13740 	if (ill->ill_bcast_addr_length != 0) {
13741 		/*
13742 		 * Later detect lack of DLPI driver multicast
13743 		 * capability by catching DL_ENABMULTI errors in
13744 		 * ip_rput_dlpi.
13745 		 */
13746 		ill->ill_flags |= ILLF_MULTICAST;
13747 		if (!ipif->ipif_isv6)
13748 			ipif->ipif_flags |= IPIF_BROADCAST;
13749 	} else {
13750 		if (ill->ill_net_type != IRE_LOOPBACK) {
13751 			if (ipif->ipif_isv6)
13752 				/*
13753 				 * Note: xresolv interfaces will eventually need
13754 				 * NOARP set here as well, but that will require
13755 				 * those external resolvers to have some
13756 				 * knowledge of that flag and act appropriately.
13757 				 * Not to be changed at present.
13758 				 */
13759 				ill->ill_flags |= ILLF_NONUD;
13760 			else
13761 				ill->ill_flags |= ILLF_NOARP;
13762 		}
13763 		if (ill->ill_phys_addr_length == 0) {
13764 			if (ill->ill_media &&
13765 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13766 				ipif->ipif_flags |= IPIF_NOXMIT;
13767 				phyi->phyint_flags |= PHYI_VIRTUAL;
13768 			} else {
13769 				/* pt-pt supports multicast. */
13770 				ill->ill_flags |= ILLF_MULTICAST;
13771 				if (ill->ill_net_type == IRE_LOOPBACK) {
13772 					phyi->phyint_flags |=
13773 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13774 				} else {
13775 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13776 				}
13777 			}
13778 		}
13779 	}
13780 	mutex_exit(&ill->ill_lock);
13781 	mutex_exit(&ill->ill_phyint->phyint_lock);
13782 	return (ipif);
13783 }
13784 
13785 /*
13786  * If appropriate, send a message up to the resolver delete the entry
13787  * for the address of this interface which is going out of business.
13788  * (Always called as writer).
13789  *
13790  * NOTE : We need to check for NULL mps as some of the fields are
13791  *	  initialized only for some interface types. See ipif_resolver_up()
13792  *	  for details.
13793  */
13794 void
13795 ipif_arp_down(ipif_t *ipif)
13796 {
13797 	mblk_t	*mp;
13798 	ill_t	*ill = ipif->ipif_ill;
13799 
13800 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13801 	ASSERT(IAM_WRITER_IPIF(ipif));
13802 
13803 	/* Delete the mapping for the local address */
13804 	mp = ipif->ipif_arp_del_mp;
13805 	if (mp != NULL) {
13806 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13807 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13808 		putnext(ill->ill_rq, mp);
13809 		ipif->ipif_arp_del_mp = NULL;
13810 	}
13811 
13812 	/*
13813 	 * If this is the last ipif that is going down and there are no
13814 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13815 	 * clean up ARP completely.
13816 	 */
13817 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13818 
13819 		/* Send up AR_INTERFACE_DOWN message */
13820 		mp = ill->ill_arp_down_mp;
13821 		if (mp != NULL) {
13822 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13823 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13824 			    ipif->ipif_id));
13825 			putnext(ill->ill_rq, mp);
13826 			ill->ill_arp_down_mp = NULL;
13827 		}
13828 
13829 		/* Tell ARP to delete the multicast mappings */
13830 		mp = ill->ill_arp_del_mapping_mp;
13831 		if (mp != NULL) {
13832 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13833 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13834 			    ipif->ipif_id));
13835 			putnext(ill->ill_rq, mp);
13836 			ill->ill_arp_del_mapping_mp = NULL;
13837 		}
13838 	}
13839 }
13840 
13841 /*
13842  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13843  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13844  * that it wants the add_mp allocated in this function to be returned
13845  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13846  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13847  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13848  * as it does a ipif_arp_down after calling this function - which will
13849  * remove what we add here.
13850  *
13851  * Returns -1 on failures and 0 on success.
13852  */
13853 int
13854 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13855 {
13856 	mblk_t	*del_mp = NULL;
13857 	mblk_t *add_mp = NULL;
13858 	mblk_t *mp;
13859 	ill_t	*ill = ipif->ipif_ill;
13860 	phyint_t *phyi = ill->ill_phyint;
13861 	ipaddr_t addr, mask, extract_mask = 0;
13862 	arma_t	*arma;
13863 	uint8_t *maddr, *bphys_addr;
13864 	uint32_t hw_start;
13865 	dl_unitdata_req_t *dlur;
13866 
13867 	ASSERT(IAM_WRITER_IPIF(ipif));
13868 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13869 		return (0);
13870 
13871 	/*
13872 	 * Delete the existing mapping from ARP. Normally ipif_down
13873 	 * -> ipif_arp_down should send this up to ARP. The only
13874 	 * reason we would find this when we are switching from
13875 	 * Multicast to Broadcast where we did not do a down.
13876 	 */
13877 	mp = ill->ill_arp_del_mapping_mp;
13878 	if (mp != NULL) {
13879 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13880 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13881 		putnext(ill->ill_rq, mp);
13882 		ill->ill_arp_del_mapping_mp = NULL;
13883 	}
13884 
13885 	if (arp_add_mapping_mp != NULL)
13886 		*arp_add_mapping_mp = NULL;
13887 
13888 	/*
13889 	 * Check that the address is not to long for the constant
13890 	 * length reserved in the template arma_t.
13891 	 */
13892 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13893 		return (-1);
13894 
13895 	/* Add mapping mblk */
13896 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13897 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13898 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13899 	    (caddr_t)&addr);
13900 	if (add_mp == NULL)
13901 		return (-1);
13902 	arma = (arma_t *)add_mp->b_rptr;
13903 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13904 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13905 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13906 
13907 	/*
13908 	 * Determine the broadcast address.
13909 	 */
13910 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13911 	if (ill->ill_sap_length < 0)
13912 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13913 	else
13914 		bphys_addr = (uchar_t *)dlur +
13915 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13916 	/*
13917 	 * Check PHYI_MULTI_BCAST and length of physical
13918 	 * address to determine if we use the mapping or the
13919 	 * broadcast address.
13920 	 */
13921 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13922 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13923 		    bphys_addr, maddr, &hw_start, &extract_mask))
13924 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13925 
13926 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13927 	    (ill->ill_flags & ILLF_MULTICAST)) {
13928 		/* Make sure this will not match the "exact" entry. */
13929 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13930 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13931 		    (caddr_t)&addr);
13932 		if (del_mp == NULL) {
13933 			freemsg(add_mp);
13934 			return (-1);
13935 		}
13936 		bcopy(&extract_mask, (char *)arma +
13937 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13938 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13939 			/* Use link-layer broadcast address for MULTI_BCAST */
13940 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13941 			ip2dbg(("ipif_arp_setup_multicast: adding"
13942 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13943 		} else {
13944 			arma->arma_hw_mapping_start = hw_start;
13945 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13946 			    " ARP setup for %s\n", ill->ill_name));
13947 		}
13948 	} else {
13949 		freemsg(add_mp);
13950 		ASSERT(del_mp == NULL);
13951 		/* It is neither MULTICAST nor MULTI_BCAST */
13952 		return (0);
13953 	}
13954 	ASSERT(add_mp != NULL && del_mp != NULL);
13955 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13956 	ill->ill_arp_del_mapping_mp = del_mp;
13957 	if (arp_add_mapping_mp != NULL) {
13958 		/* The caller just wants the mblks allocated */
13959 		*arp_add_mapping_mp = add_mp;
13960 	} else {
13961 		/* The caller wants us to send it to arp */
13962 		putnext(ill->ill_rq, add_mp);
13963 	}
13964 	return (0);
13965 }
13966 
13967 /*
13968  * Get the resolver set up for a new interface address.
13969  * (Always called as writer.)
13970  * Called both for IPv4 and IPv6 interfaces,
13971  * though it only sets up the resolver for v6
13972  * if it's an xresolv interface (one using an external resolver).
13973  * Honors ILLF_NOARP.
13974  * The enumerated value res_act is used to tune the behavior.
13975  * If set to Res_act_initial, then we set up all the resolver
13976  * structures for a new interface.  If set to Res_act_move, then
13977  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13978  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13979  * asynchronous hardware address change notification.  If set to
13980  * Res_act_defend, then we tell ARP that it needs to send a single
13981  * gratuitous message in defense of the address.
13982  * Returns error on failure.
13983  */
13984 int
13985 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13986 {
13987 	caddr_t	addr;
13988 	mblk_t	*arp_up_mp = NULL;
13989 	mblk_t	*arp_down_mp = NULL;
13990 	mblk_t	*arp_add_mp = NULL;
13991 	mblk_t	*arp_del_mp = NULL;
13992 	mblk_t	*arp_add_mapping_mp = NULL;
13993 	mblk_t	*arp_del_mapping_mp = NULL;
13994 	ill_t	*ill = ipif->ipif_ill;
13995 	uchar_t	*area_p = NULL;
13996 	uchar_t	*ared_p = NULL;
13997 	int	err = ENOMEM;
13998 	boolean_t was_dup;
13999 
14000 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
14001 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
14002 	ASSERT(IAM_WRITER_IPIF(ipif));
14003 
14004 	was_dup = B_FALSE;
14005 	if (res_act == Res_act_initial) {
14006 		ipif->ipif_addr_ready = 0;
14007 		/*
14008 		 * We're bringing an interface up here.  There's no way that we
14009 		 * should need to shut down ARP now.
14010 		 */
14011 		mutex_enter(&ill->ill_lock);
14012 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
14013 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
14014 			ill->ill_ipif_dup_count--;
14015 			was_dup = B_TRUE;
14016 		}
14017 		mutex_exit(&ill->ill_lock);
14018 	}
14019 	if (ipif->ipif_recovery_id != 0)
14020 		(void) untimeout(ipif->ipif_recovery_id);
14021 	ipif->ipif_recovery_id = 0;
14022 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
14023 		ipif->ipif_addr_ready = 1;
14024 		return (0);
14025 	}
14026 	/* NDP will set the ipif_addr_ready flag when it's ready */
14027 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
14028 		return (0);
14029 
14030 	if (ill->ill_isv6) {
14031 		/*
14032 		 * External resolver for IPv6
14033 		 */
14034 		ASSERT(res_act == Res_act_initial);
14035 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
14036 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
14037 			area_p = (uchar_t *)&ip6_area_template;
14038 			ared_p = (uchar_t *)&ip6_ared_template;
14039 		}
14040 	} else {
14041 		/*
14042 		 * IPv4 arp case. If the ARP stream has already started
14043 		 * closing, fail this request for ARP bringup. Else
14044 		 * record the fact that an ARP bringup is pending.
14045 		 */
14046 		mutex_enter(&ill->ill_lock);
14047 		if (ill->ill_arp_closing) {
14048 			mutex_exit(&ill->ill_lock);
14049 			err = EINVAL;
14050 			goto failed;
14051 		} else {
14052 			if (ill->ill_ipif_up_count == 0 &&
14053 			    ill->ill_ipif_dup_count == 0 && !was_dup)
14054 				ill->ill_arp_bringup_pending = 1;
14055 			mutex_exit(&ill->ill_lock);
14056 		}
14057 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
14058 			addr = (caddr_t)&ipif->ipif_lcl_addr;
14059 			area_p = (uchar_t *)&ip_area_template;
14060 			ared_p = (uchar_t *)&ip_ared_template;
14061 		}
14062 	}
14063 
14064 	/*
14065 	 * Add an entry for the local address in ARP only if it
14066 	 * is not UNNUMBERED and the address is not INADDR_ANY.
14067 	 */
14068 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
14069 		area_t *area;
14070 
14071 		/* Now ask ARP to publish our address. */
14072 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
14073 		if (arp_add_mp == NULL)
14074 			goto failed;
14075 		area = (area_t *)arp_add_mp->b_rptr;
14076 		if (res_act != Res_act_initial) {
14077 			/*
14078 			 * Copy the new hardware address and length into
14079 			 * arp_add_mp to be sent to ARP.
14080 			 */
14081 			area->area_hw_addr_length = ill->ill_phys_addr_length;
14082 			bcopy(ill->ill_phys_addr,
14083 			    ((char *)area + area->area_hw_addr_offset),
14084 			    area->area_hw_addr_length);
14085 		}
14086 
14087 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
14088 		    ACE_F_MYADDR;
14089 
14090 		if (res_act == Res_act_defend) {
14091 			area->area_flags |= ACE_F_DEFEND;
14092 			/*
14093 			 * If we're just defending our address now, then
14094 			 * there's no need to set up ARP multicast mappings.
14095 			 * The publish command is enough.
14096 			 */
14097 			goto done;
14098 		}
14099 
14100 		if (res_act != Res_act_initial)
14101 			goto arp_setup_multicast;
14102 
14103 		/*
14104 		 * Allocate an ARP deletion message so we know we can tell ARP
14105 		 * when the interface goes down.
14106 		 */
14107 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
14108 		if (arp_del_mp == NULL)
14109 			goto failed;
14110 
14111 	} else {
14112 		if (res_act != Res_act_initial)
14113 			goto done;
14114 	}
14115 	/*
14116 	 * Need to bring up ARP or setup multicast mapping only
14117 	 * when the first interface is coming UP.
14118 	 */
14119 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
14120 	    was_dup) {
14121 		goto done;
14122 	}
14123 
14124 	/*
14125 	 * Allocate an ARP down message (to be saved) and an ARP up
14126 	 * message.
14127 	 */
14128 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
14129 	if (arp_down_mp == NULL)
14130 		goto failed;
14131 
14132 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
14133 	if (arp_up_mp == NULL)
14134 		goto failed;
14135 
14136 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
14137 		goto done;
14138 
14139 arp_setup_multicast:
14140 	/*
14141 	 * Setup the multicast mappings. This function initializes
14142 	 * ill_arp_del_mapping_mp also. This does not need to be done for
14143 	 * IPv6.
14144 	 */
14145 	if (!ill->ill_isv6) {
14146 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
14147 		if (err != 0)
14148 			goto failed;
14149 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
14150 		ASSERT(arp_add_mapping_mp != NULL);
14151 	}
14152 
14153 done:
14154 	if (arp_del_mp != NULL) {
14155 		ASSERT(ipif->ipif_arp_del_mp == NULL);
14156 		ipif->ipif_arp_del_mp = arp_del_mp;
14157 	}
14158 	if (arp_down_mp != NULL) {
14159 		ASSERT(ill->ill_arp_down_mp == NULL);
14160 		ill->ill_arp_down_mp = arp_down_mp;
14161 	}
14162 	if (arp_del_mapping_mp != NULL) {
14163 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
14164 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
14165 	}
14166 	if (arp_up_mp != NULL) {
14167 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
14168 		    ill->ill_name, ipif->ipif_id));
14169 		putnext(ill->ill_rq, arp_up_mp);
14170 	}
14171 	if (arp_add_mp != NULL) {
14172 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
14173 		    ill->ill_name, ipif->ipif_id));
14174 		/*
14175 		 * If it's an extended ARP implementation, then we'll wait to
14176 		 * hear that DAD has finished before using the interface.
14177 		 */
14178 		if (!ill->ill_arp_extend)
14179 			ipif->ipif_addr_ready = 1;
14180 		putnext(ill->ill_rq, arp_add_mp);
14181 	} else {
14182 		ipif->ipif_addr_ready = 1;
14183 	}
14184 	if (arp_add_mapping_mp != NULL) {
14185 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
14186 		    ill->ill_name, ipif->ipif_id));
14187 		putnext(ill->ill_rq, arp_add_mapping_mp);
14188 	}
14189 	if (res_act != Res_act_initial)
14190 		return (0);
14191 
14192 	if (ill->ill_flags & ILLF_NOARP)
14193 		err = ill_arp_off(ill);
14194 	else
14195 		err = ill_arp_on(ill);
14196 	if (err != 0) {
14197 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
14198 		freemsg(ipif->ipif_arp_del_mp);
14199 		freemsg(ill->ill_arp_down_mp);
14200 		freemsg(ill->ill_arp_del_mapping_mp);
14201 		ipif->ipif_arp_del_mp = NULL;
14202 		ill->ill_arp_down_mp = NULL;
14203 		ill->ill_arp_del_mapping_mp = NULL;
14204 		return (err);
14205 	}
14206 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
14207 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
14208 
14209 failed:
14210 	ip1dbg(("ipif_resolver_up: FAILED\n"));
14211 	freemsg(arp_add_mp);
14212 	freemsg(arp_del_mp);
14213 	freemsg(arp_add_mapping_mp);
14214 	freemsg(arp_up_mp);
14215 	freemsg(arp_down_mp);
14216 	ill->ill_arp_bringup_pending = 0;
14217 	return (err);
14218 }
14219 
14220 /*
14221  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
14222  * just gone back up.
14223  */
14224 static void
14225 ipif_arp_start_dad(ipif_t *ipif)
14226 {
14227 	ill_t *ill = ipif->ipif_ill;
14228 	mblk_t *arp_add_mp;
14229 	area_t *area;
14230 
14231 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
14232 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14233 	    ipif->ipif_lcl_addr == INADDR_ANY ||
14234 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
14235 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
14236 		/*
14237 		 * If we can't contact ARP for some reason, that's not really a
14238 		 * problem.  Just send out the routing socket notification that
14239 		 * DAD completion would have done, and continue.
14240 		 */
14241 		ipif_mask_reply(ipif);
14242 		ip_rts_ifmsg(ipif);
14243 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14244 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14245 		ipif->ipif_addr_ready = 1;
14246 		return;
14247 	}
14248 
14249 	/* Setting the 'unverified' flag restarts DAD */
14250 	area = (area_t *)arp_add_mp->b_rptr;
14251 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
14252 	    ACE_F_UNVERIFIED;
14253 	putnext(ill->ill_rq, arp_add_mp);
14254 }
14255 
14256 static void
14257 ipif_ndp_start_dad(ipif_t *ipif)
14258 {
14259 	nce_t *nce;
14260 
14261 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
14262 	if (nce == NULL)
14263 		return;
14264 
14265 	if (!ndp_restart_dad(nce)) {
14266 		/*
14267 		 * If we can't restart DAD for some reason, that's not really a
14268 		 * problem.  Just send out the routing socket notification that
14269 		 * DAD completion would have done, and continue.
14270 		 */
14271 		ip_rts_ifmsg(ipif);
14272 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14273 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14274 		ipif->ipif_addr_ready = 1;
14275 	}
14276 	NCE_REFRELE(nce);
14277 }
14278 
14279 /*
14280  * Restart duplicate address detection on all interfaces on the given ill.
14281  *
14282  * This is called when an interface transitions from down to up
14283  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
14284  *
14285  * Note that since the underlying physical link has transitioned, we must cause
14286  * at least one routing socket message to be sent here, either via DAD
14287  * completion or just by default on the first ipif.  (If we don't do this, then
14288  * in.mpathd will see long delays when doing link-based failure recovery.)
14289  */
14290 void
14291 ill_restart_dad(ill_t *ill, boolean_t went_up)
14292 {
14293 	ipif_t *ipif;
14294 
14295 	if (ill == NULL)
14296 		return;
14297 
14298 	/*
14299 	 * If layer two doesn't support duplicate address detection, then just
14300 	 * send the routing socket message now and be done with it.
14301 	 */
14302 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14303 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14304 		ip_rts_ifmsg(ill->ill_ipif);
14305 		return;
14306 	}
14307 
14308 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14309 		if (went_up) {
14310 			if (ipif->ipif_flags & IPIF_UP) {
14311 				if (ill->ill_isv6)
14312 					ipif_ndp_start_dad(ipif);
14313 				else
14314 					ipif_arp_start_dad(ipif);
14315 			} else if (ill->ill_isv6 &&
14316 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14317 				/*
14318 				 * For IPv4, the ARP module itself will
14319 				 * automatically start the DAD process when it
14320 				 * sees DL_NOTE_LINK_UP.  We respond to the
14321 				 * AR_CN_READY at the completion of that task.
14322 				 * For IPv6, we must kick off the bring-up
14323 				 * process now.
14324 				 */
14325 				ndp_do_recovery(ipif);
14326 			} else {
14327 				/*
14328 				 * Unfortunately, the first ipif is "special"
14329 				 * and represents the underlying ill in the
14330 				 * routing socket messages.  Thus, when this
14331 				 * one ipif is down, we must still notify so
14332 				 * that the user knows the IFF_RUNNING status
14333 				 * change.  (If the first ipif is up, then
14334 				 * we'll handle eventual routing socket
14335 				 * notification via DAD completion.)
14336 				 */
14337 				if (ipif == ill->ill_ipif)
14338 					ip_rts_ifmsg(ill->ill_ipif);
14339 			}
14340 		} else {
14341 			/*
14342 			 * After link down, we'll need to send a new routing
14343 			 * message when the link comes back, so clear
14344 			 * ipif_addr_ready.
14345 			 */
14346 			ipif->ipif_addr_ready = 0;
14347 		}
14348 	}
14349 
14350 	/*
14351 	 * If we've torn down links, then notify the user right away.
14352 	 */
14353 	if (!went_up)
14354 		ip_rts_ifmsg(ill->ill_ipif);
14355 }
14356 
14357 /*
14358  * Wakeup all threads waiting to enter the ipsq, and sleeping
14359  * on any of the ills in this ipsq. The ill_lock of the ill
14360  * must be held so that waiters don't miss wakeups
14361  */
14362 static void
14363 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14364 {
14365 	phyint_t *phyint;
14366 
14367 	phyint = ipsq->ipsq_phyint_list;
14368 	while (phyint != NULL) {
14369 		if (phyint->phyint_illv4) {
14370 			if (!caller_holds_lock)
14371 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14372 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14373 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14374 			if (!caller_holds_lock)
14375 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14376 		}
14377 		if (phyint->phyint_illv6) {
14378 			if (!caller_holds_lock)
14379 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14380 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14381 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14382 			if (!caller_holds_lock)
14383 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14384 		}
14385 		phyint = phyint->phyint_ipsq_next;
14386 	}
14387 }
14388 
14389 static ipsq_t *
14390 ipsq_create(char *groupname, ip_stack_t *ipst)
14391 {
14392 	ipsq_t	*ipsq;
14393 
14394 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14395 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14396 	if (ipsq == NULL) {
14397 		return (NULL);
14398 	}
14399 
14400 	if (groupname != NULL)
14401 		(void) strcpy(ipsq->ipsq_name, groupname);
14402 	else
14403 		ipsq->ipsq_name[0] = '\0';
14404 
14405 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14406 	ipsq->ipsq_flags |= IPSQ_GROUP;
14407 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14408 	ipst->ips_ipsq_g_head = ipsq;
14409 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14410 	return (ipsq);
14411 }
14412 
14413 /*
14414  * Return an ipsq correspoding to the groupname. If 'create' is true
14415  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14416  * uniquely with an IPMP group. However during IPMP groupname operations,
14417  * multiple IPMP groups may be associated with a single ipsq. But no
14418  * IPMP group can be associated with more than 1 ipsq at any time.
14419  * For example
14420  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14421  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14422  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14423  *
14424  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14425  * status shown below during the execution of the above command.
14426  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14427  *
14428  * After the completion of the above groupname command we return to the stable
14429  * state shown below.
14430  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14431  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14432  *
14433  * Because of the above, we don't search based on the ipsq_name since that
14434  * would miss the correct ipsq during certain windows as shown above.
14435  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14436  * natural state.
14437  */
14438 static ipsq_t *
14439 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14440     ip_stack_t *ipst)
14441 {
14442 	ipsq_t	*ipsq;
14443 	int	group_len;
14444 	phyint_t *phyint;
14445 
14446 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14447 
14448 	group_len = strlen(groupname);
14449 	ASSERT(group_len != 0);
14450 	group_len++;
14451 
14452 	for (ipsq = ipst->ips_ipsq_g_head;
14453 	    ipsq != NULL;
14454 	    ipsq = ipsq->ipsq_next) {
14455 		/*
14456 		 * When an ipsq is being split, and ill_split_ipsq
14457 		 * calls this function, we exclude it from being considered.
14458 		 */
14459 		if (ipsq == exclude_ipsq)
14460 			continue;
14461 
14462 		/*
14463 		 * Compare against the ipsq_name. The groupname change happens
14464 		 * in 2 phases. The 1st phase merges the from group into
14465 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14466 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14467 		 * ipsq_name. At this point the phyint_groupname has not been
14468 		 * updated.
14469 		 */
14470 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14471 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14472 			/*
14473 			 * Verify that an ipmp groupname is exactly
14474 			 * part of 1 ipsq and is not found in any other
14475 			 * ipsq.
14476 			 */
14477 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14478 			    NULL);
14479 			return (ipsq);
14480 		}
14481 
14482 		/*
14483 		 * Comparison against ipsq_name alone is not sufficient.
14484 		 * In the case when groups are currently being
14485 		 * merged, the ipsq could hold other IPMP groups temporarily.
14486 		 * so we walk the phyint list and compare against the
14487 		 * phyint_groupname as well.
14488 		 */
14489 		phyint = ipsq->ipsq_phyint_list;
14490 		while (phyint != NULL) {
14491 			if ((group_len == phyint->phyint_groupname_len) &&
14492 			    (bcmp(phyint->phyint_groupname, groupname,
14493 			    group_len) == 0)) {
14494 				/*
14495 				 * Verify that an ipmp groupname is exactly
14496 				 * part of 1 ipsq and is not found in any other
14497 				 * ipsq.
14498 				 */
14499 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14500 				    ipst) == NULL);
14501 				return (ipsq);
14502 			}
14503 			phyint = phyint->phyint_ipsq_next;
14504 		}
14505 	}
14506 	if (create)
14507 		ipsq = ipsq_create(groupname, ipst);
14508 	return (ipsq);
14509 }
14510 
14511 static void
14512 ipsq_delete(ipsq_t *ipsq)
14513 {
14514 	ipsq_t *nipsq;
14515 	ipsq_t *pipsq = NULL;
14516 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14517 
14518 	/*
14519 	 * We don't hold the ipsq lock, but we are sure no new
14520 	 * messages can land up, since the ipsq_refs is zero.
14521 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14522 	 * is associated with this ipsq. (Lookups are based on ill_name
14523 	 * or phyint_groupname)
14524 	 */
14525 	ASSERT(ipsq->ipsq_refs == 0);
14526 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14527 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14528 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14529 		/*
14530 		 * This is not the ipsq of an IPMP group.
14531 		 */
14532 		ipsq->ipsq_ipst = NULL;
14533 		kmem_free(ipsq, sizeof (ipsq_t));
14534 		return;
14535 	}
14536 
14537 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14538 
14539 	/*
14540 	 * Locate the ipsq  before we can remove it from
14541 	 * the singly linked list of ipsq's.
14542 	 */
14543 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14544 	    nipsq = nipsq->ipsq_next) {
14545 		if (nipsq == ipsq) {
14546 			break;
14547 		}
14548 		pipsq = nipsq;
14549 	}
14550 
14551 	ASSERT(nipsq == ipsq);
14552 
14553 	/* unlink ipsq from the list */
14554 	if (pipsq != NULL)
14555 		pipsq->ipsq_next = ipsq->ipsq_next;
14556 	else
14557 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14558 	ipsq->ipsq_ipst = NULL;
14559 	kmem_free(ipsq, sizeof (ipsq_t));
14560 	rw_exit(&ipst->ips_ill_g_lock);
14561 }
14562 
14563 static void
14564 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14565     queue_t *q)
14566 {
14567 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14568 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14569 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14570 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14571 	ASSERT(current_mp != NULL);
14572 
14573 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14574 		NEW_OP, NULL);
14575 
14576 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14577 	    new_ipsq->ipsq_xopq_mphead != NULL);
14578 
14579 	/*
14580 	 * move from old ipsq to the new ipsq.
14581 	 */
14582 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14583 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14584 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14585 
14586 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14587 }
14588 
14589 void
14590 ill_group_cleanup(ill_t *ill)
14591 {
14592 	ill_t *ill_v4;
14593 	ill_t *ill_v6;
14594 	ipif_t *ipif;
14595 
14596 	ill_v4 = ill->ill_phyint->phyint_illv4;
14597 	ill_v6 = ill->ill_phyint->phyint_illv6;
14598 
14599 	if (ill_v4 != NULL) {
14600 		mutex_enter(&ill_v4->ill_lock);
14601 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14602 		    ipif = ipif->ipif_next) {
14603 			IPIF_UNMARK_MOVING(ipif);
14604 		}
14605 		ill_v4->ill_up_ipifs = B_FALSE;
14606 		mutex_exit(&ill_v4->ill_lock);
14607 	}
14608 
14609 	if (ill_v6 != NULL) {
14610 		mutex_enter(&ill_v6->ill_lock);
14611 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14612 		    ipif = ipif->ipif_next) {
14613 			IPIF_UNMARK_MOVING(ipif);
14614 		}
14615 		ill_v6->ill_up_ipifs = B_FALSE;
14616 		mutex_exit(&ill_v6->ill_lock);
14617 	}
14618 }
14619 /*
14620  * This function is called when an ill has had a change in its group status
14621  * to bring up all the ipifs that were up before the change.
14622  */
14623 int
14624 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14625 {
14626 	ipif_t *ipif;
14627 	ill_t *ill_v4;
14628 	ill_t *ill_v6;
14629 	ill_t *from_ill;
14630 	int err = 0;
14631 
14632 
14633 	ASSERT(IAM_WRITER_ILL(ill));
14634 
14635 	/*
14636 	 * Except for ipif_state_flags and ill_state_flags the other
14637 	 * fields of the ipif/ill that are modified below are protected
14638 	 * implicitly since we are a writer. We would have tried to down
14639 	 * even an ipif that was already down, in ill_down_ipifs. So we
14640 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14641 	 */
14642 	ill_v4 = ill->ill_phyint->phyint_illv4;
14643 	ill_v6 = ill->ill_phyint->phyint_illv6;
14644 	if (ill_v4 != NULL) {
14645 		ill_v4->ill_up_ipifs = B_TRUE;
14646 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14647 		    ipif = ipif->ipif_next) {
14648 			mutex_enter(&ill_v4->ill_lock);
14649 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14650 			IPIF_UNMARK_MOVING(ipif);
14651 			mutex_exit(&ill_v4->ill_lock);
14652 			if (ipif->ipif_was_up) {
14653 				if (!(ipif->ipif_flags & IPIF_UP))
14654 					err = ipif_up(ipif, q, mp);
14655 				ipif->ipif_was_up = B_FALSE;
14656 				if (err != 0) {
14657 					/*
14658 					 * Can there be any other error ?
14659 					 */
14660 					ASSERT(err == EINPROGRESS);
14661 					return (err);
14662 				}
14663 			}
14664 		}
14665 		mutex_enter(&ill_v4->ill_lock);
14666 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14667 		mutex_exit(&ill_v4->ill_lock);
14668 		ill_v4->ill_up_ipifs = B_FALSE;
14669 		if (ill_v4->ill_move_in_progress) {
14670 			ASSERT(ill_v4->ill_move_peer != NULL);
14671 			ill_v4->ill_move_in_progress = B_FALSE;
14672 			from_ill = ill_v4->ill_move_peer;
14673 			from_ill->ill_move_in_progress = B_FALSE;
14674 			from_ill->ill_move_peer = NULL;
14675 			mutex_enter(&from_ill->ill_lock);
14676 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14677 			mutex_exit(&from_ill->ill_lock);
14678 			if (ill_v6 == NULL) {
14679 				if (from_ill->ill_phyint->phyint_flags &
14680 				    PHYI_STANDBY) {
14681 					phyint_inactive(from_ill->ill_phyint);
14682 				}
14683 				if (ill_v4->ill_phyint->phyint_flags &
14684 				    PHYI_STANDBY) {
14685 					phyint_inactive(ill_v4->ill_phyint);
14686 				}
14687 			}
14688 			ill_v4->ill_move_peer = NULL;
14689 		}
14690 	}
14691 
14692 	if (ill_v6 != NULL) {
14693 		ill_v6->ill_up_ipifs = B_TRUE;
14694 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14695 		    ipif = ipif->ipif_next) {
14696 			mutex_enter(&ill_v6->ill_lock);
14697 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14698 			IPIF_UNMARK_MOVING(ipif);
14699 			mutex_exit(&ill_v6->ill_lock);
14700 			if (ipif->ipif_was_up) {
14701 				if (!(ipif->ipif_flags & IPIF_UP))
14702 					err = ipif_up(ipif, q, mp);
14703 				ipif->ipif_was_up = B_FALSE;
14704 				if (err != 0) {
14705 					/*
14706 					 * Can there be any other error ?
14707 					 */
14708 					ASSERT(err == EINPROGRESS);
14709 					return (err);
14710 				}
14711 			}
14712 		}
14713 		mutex_enter(&ill_v6->ill_lock);
14714 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14715 		mutex_exit(&ill_v6->ill_lock);
14716 		ill_v6->ill_up_ipifs = B_FALSE;
14717 		if (ill_v6->ill_move_in_progress) {
14718 			ASSERT(ill_v6->ill_move_peer != NULL);
14719 			ill_v6->ill_move_in_progress = B_FALSE;
14720 			from_ill = ill_v6->ill_move_peer;
14721 			from_ill->ill_move_in_progress = B_FALSE;
14722 			from_ill->ill_move_peer = NULL;
14723 			mutex_enter(&from_ill->ill_lock);
14724 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14725 			mutex_exit(&from_ill->ill_lock);
14726 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14727 				phyint_inactive(from_ill->ill_phyint);
14728 			}
14729 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14730 				phyint_inactive(ill_v6->ill_phyint);
14731 			}
14732 			ill_v6->ill_move_peer = NULL;
14733 		}
14734 	}
14735 	return (0);
14736 }
14737 
14738 /*
14739  * bring down all the approriate ipifs.
14740  */
14741 /* ARGSUSED */
14742 static void
14743 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14744 {
14745 	ipif_t *ipif;
14746 
14747 	ASSERT(IAM_WRITER_ILL(ill));
14748 
14749 	/*
14750 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14751 	 * are modified below are protected implicitly since we are a writer
14752 	 */
14753 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14754 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14755 			continue;
14756 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14757 			/*
14758 			 * We go through the ipif_down logic even if the ipif
14759 			 * is already down, since routes can be added based
14760 			 * on down ipifs. Going through ipif_down once again
14761 			 * will delete any IREs created based on these routes.
14762 			 */
14763 			if (ipif->ipif_flags & IPIF_UP)
14764 				ipif->ipif_was_up = B_TRUE;
14765 			/*
14766 			 * If called with chk_nofailover true ipif is moving.
14767 			 */
14768 			mutex_enter(&ill->ill_lock);
14769 			if (chk_nofailover) {
14770 				ipif->ipif_state_flags |=
14771 					IPIF_MOVING | IPIF_CHANGING;
14772 			} else {
14773 				ipif->ipif_state_flags |= IPIF_CHANGING;
14774 			}
14775 			mutex_exit(&ill->ill_lock);
14776 			/*
14777 			 * Need to re-create net/subnet bcast ires if
14778 			 * they are dependent on ipif.
14779 			 */
14780 			if (!ipif->ipif_isv6)
14781 				ipif_check_bcast_ires(ipif);
14782 			(void) ipif_logical_down(ipif, NULL, NULL);
14783 			ipif_non_duplicate(ipif);
14784 			ipif_down_tail(ipif);
14785 			/*
14786 			 * We don't do ipif_multicast_down for IPv4 in
14787 			 * ipif_down. We need to set this so that
14788 			 * ipif_multicast_up will join the
14789 			 * ALLHOSTS_GROUP on to_ill.
14790 			 */
14791 			ipif->ipif_multicast_up = B_FALSE;
14792 		}
14793 	}
14794 }
14795 
14796 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14797 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14798 	(ipsq)->ipsq_refs++;				\
14799 }
14800 
14801 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14802 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14803 	(ipsq)->ipsq_refs--;				\
14804 	if ((ipsq)->ipsq_refs == 0)				\
14805 		(ipsq)->ipsq_name[0] = '\0'; 		\
14806 }
14807 
14808 /*
14809  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14810  * new_ipsq.
14811  */
14812 static void
14813 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14814 {
14815 	phyint_t *phyint;
14816 	phyint_t *next_phyint;
14817 
14818 	/*
14819 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14820 	 * writer and the ill_lock of the ill in question. Also the dest
14821 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14822 	 */
14823 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14824 
14825 	phyint = cur_ipsq->ipsq_phyint_list;
14826 	cur_ipsq->ipsq_phyint_list = NULL;
14827 	while (phyint != NULL) {
14828 		next_phyint = phyint->phyint_ipsq_next;
14829 		IPSQ_DEC_REF(cur_ipsq, ipst);
14830 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14831 		new_ipsq->ipsq_phyint_list = phyint;
14832 		IPSQ_INC_REF(new_ipsq, ipst);
14833 		phyint->phyint_ipsq = new_ipsq;
14834 		phyint = next_phyint;
14835 	}
14836 }
14837 
14838 #define	SPLIT_SUCCESS		0
14839 #define	SPLIT_NOT_NEEDED	1
14840 #define	SPLIT_FAILED		2
14841 
14842 int
14843 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14844     ip_stack_t *ipst)
14845 {
14846 	ipsq_t *newipsq = NULL;
14847 
14848 	/*
14849 	 * Assertions denote pre-requisites for changing the ipsq of
14850 	 * a phyint
14851 	 */
14852 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14853 	/*
14854 	 * <ill-phyint> assocs can't change while ill_g_lock
14855 	 * is held as writer. See ill_phyint_reinit()
14856 	 */
14857 	ASSERT(phyint->phyint_illv4 == NULL ||
14858 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14859 	ASSERT(phyint->phyint_illv6 == NULL ||
14860 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14861 
14862 	if ((phyint->phyint_groupname_len !=
14863 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14864 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14865 	    phyint->phyint_groupname_len) != 0)) {
14866 		/*
14867 		 * Once we fail in creating a new ipsq due to memory shortage,
14868 		 * don't attempt to create new ipsq again, based on another
14869 		 * phyint, since we want all phyints belonging to an IPMP group
14870 		 * to be in the same ipsq even in the event of mem alloc fails.
14871 		 */
14872 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14873 		    cur_ipsq, ipst);
14874 		if (newipsq == NULL) {
14875 			/* Memory allocation failure */
14876 			return (SPLIT_FAILED);
14877 		} else {
14878 			/* ipsq_refs protected by ill_g_lock (writer) */
14879 			IPSQ_DEC_REF(cur_ipsq, ipst);
14880 			phyint->phyint_ipsq = newipsq;
14881 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14882 			newipsq->ipsq_phyint_list = phyint;
14883 			IPSQ_INC_REF(newipsq, ipst);
14884 			return (SPLIT_SUCCESS);
14885 		}
14886 	}
14887 	return (SPLIT_NOT_NEEDED);
14888 }
14889 
14890 /*
14891  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14892  * to do this split
14893  */
14894 static int
14895 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14896 {
14897 	ipsq_t *newipsq;
14898 
14899 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14900 	/*
14901 	 * <ill-phyint> assocs can't change while ill_g_lock
14902 	 * is held as writer. See ill_phyint_reinit()
14903 	 */
14904 
14905 	ASSERT(phyint->phyint_illv4 == NULL ||
14906 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14907 	ASSERT(phyint->phyint_illv6 == NULL ||
14908 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14909 
14910 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14911 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14912 		/*
14913 		 * ipsq_init failed due to no memory
14914 		 * caller will use the same ipsq
14915 		 */
14916 		return (SPLIT_FAILED);
14917 	}
14918 
14919 	/* ipsq_ref is protected by ill_g_lock (writer) */
14920 	IPSQ_DEC_REF(cur_ipsq, ipst);
14921 
14922 	/*
14923 	 * This is a new ipsq that is unknown to the world.
14924 	 * So we don't need to hold ipsq_lock,
14925 	 */
14926 	newipsq = phyint->phyint_ipsq;
14927 	newipsq->ipsq_writer = NULL;
14928 	newipsq->ipsq_reentry_cnt--;
14929 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14930 #ifdef ILL_DEBUG
14931 	newipsq->ipsq_depth = 0;
14932 #endif
14933 
14934 	return (SPLIT_SUCCESS);
14935 }
14936 
14937 /*
14938  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14939  * ipsq's representing their individual groups or themselves. Return
14940  * whether split needs to be retried again later.
14941  */
14942 static boolean_t
14943 ill_split_ipsq(ipsq_t *cur_ipsq)
14944 {
14945 	phyint_t *phyint;
14946 	phyint_t *next_phyint;
14947 	int	error;
14948 	boolean_t need_retry = B_FALSE;
14949 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14950 
14951 	phyint = cur_ipsq->ipsq_phyint_list;
14952 	cur_ipsq->ipsq_phyint_list = NULL;
14953 	while (phyint != NULL) {
14954 		next_phyint = phyint->phyint_ipsq_next;
14955 		/*
14956 		 * 'created' will tell us whether the callee actually
14957 		 * created an ipsq. Lack of memory may force the callee
14958 		 * to return without creating an ipsq.
14959 		 */
14960 		if (phyint->phyint_groupname == NULL) {
14961 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14962 		} else {
14963 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14964 					need_retry, ipst);
14965 		}
14966 
14967 		switch (error) {
14968 		case SPLIT_FAILED:
14969 			need_retry = B_TRUE;
14970 			/* FALLTHRU */
14971 		case SPLIT_NOT_NEEDED:
14972 			/*
14973 			 * Keep it on the list.
14974 			 */
14975 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14976 			cur_ipsq->ipsq_phyint_list = phyint;
14977 			break;
14978 		case SPLIT_SUCCESS:
14979 			break;
14980 		default:
14981 			ASSERT(0);
14982 		}
14983 
14984 		phyint = next_phyint;
14985 	}
14986 	return (need_retry);
14987 }
14988 
14989 /*
14990  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14991  * and return the ills in the list. This list will be
14992  * needed to unlock all the ills later on by the caller.
14993  * The <ill-ipsq> associations could change between the
14994  * lock and unlock. Hence the unlock can't traverse the
14995  * ipsq to get the list of ills.
14996  */
14997 static int
14998 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14999 {
15000 	int	cnt = 0;
15001 	phyint_t	*phyint;
15002 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
15003 
15004 	/*
15005 	 * The caller holds ill_g_lock to ensure that the ill memberships
15006 	 * of the ipsq don't change
15007 	 */
15008 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
15009 
15010 	phyint = ipsq->ipsq_phyint_list;
15011 	while (phyint != NULL) {
15012 		if (phyint->phyint_illv4 != NULL) {
15013 			ASSERT(cnt < list_max);
15014 			list[cnt++] = phyint->phyint_illv4;
15015 		}
15016 		if (phyint->phyint_illv6 != NULL) {
15017 			ASSERT(cnt < list_max);
15018 			list[cnt++] = phyint->phyint_illv6;
15019 		}
15020 		phyint = phyint->phyint_ipsq_next;
15021 	}
15022 	ill_lock_ills(list, cnt);
15023 	return (cnt);
15024 }
15025 
15026 void
15027 ill_lock_ills(ill_t **list, int cnt)
15028 {
15029 	int	i;
15030 
15031 	if (cnt > 1) {
15032 		boolean_t try_again;
15033 		do {
15034 			try_again = B_FALSE;
15035 			for (i = 0; i < cnt - 1; i++) {
15036 				if (list[i] < list[i + 1]) {
15037 					ill_t	*tmp;
15038 
15039 					/* swap the elements */
15040 					tmp = list[i];
15041 					list[i] = list[i + 1];
15042 					list[i + 1] = tmp;
15043 					try_again = B_TRUE;
15044 				}
15045 			}
15046 		} while (try_again);
15047 	}
15048 
15049 	for (i = 0; i < cnt; i++) {
15050 		if (i == 0) {
15051 			if (list[i] != NULL)
15052 				mutex_enter(&list[i]->ill_lock);
15053 			else
15054 				return;
15055 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
15056 			mutex_enter(&list[i]->ill_lock);
15057 		}
15058 	}
15059 }
15060 
15061 void
15062 ill_unlock_ills(ill_t **list, int cnt)
15063 {
15064 	int	i;
15065 
15066 	for (i = 0; i < cnt; i++) {
15067 		if ((i == 0) && (list[i] != NULL)) {
15068 			mutex_exit(&list[i]->ill_lock);
15069 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
15070 			mutex_exit(&list[i]->ill_lock);
15071 		}
15072 	}
15073 }
15074 
15075 /*
15076  * Merge all the ills from 1 ipsq group into another ipsq group.
15077  * The source ipsq group is specified by the ipsq associated with
15078  * 'from_ill'. The destination ipsq group is specified by the ipsq
15079  * associated with 'to_ill' or 'groupname' respectively.
15080  * Note that ipsq itself does not have a reference count mechanism
15081  * and functions don't look up an ipsq and pass it around. Instead
15082  * functions pass around an ill or groupname, and the ipsq is looked
15083  * up from the ill or groupname and the required operation performed
15084  * atomically with the lookup on the ipsq.
15085  */
15086 static int
15087 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
15088     queue_t *q)
15089 {
15090 	ipsq_t *old_ipsq;
15091 	ipsq_t *new_ipsq;
15092 	ill_t	**ill_list;
15093 	int	cnt;
15094 	size_t	ill_list_size;
15095 	boolean_t became_writer_on_new_sq = B_FALSE;
15096 	ip_stack_t	*ipst = from_ill->ill_ipst;
15097 
15098 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
15099 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
15100 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
15101 
15102 	/*
15103 	 * Need to hold ill_g_lock as writer and also the ill_lock to
15104 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
15105 	 * ipsq_lock to prevent new messages from landing on an ipsq.
15106 	 */
15107 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15108 
15109 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
15110 	if (groupname != NULL)
15111 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
15112 	else {
15113 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
15114 	}
15115 
15116 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
15117 
15118 	/*
15119 	 * both groups are on the same ipsq.
15120 	 */
15121 	if (old_ipsq == new_ipsq) {
15122 		rw_exit(&ipst->ips_ill_g_lock);
15123 		return (0);
15124 	}
15125 
15126 	cnt = old_ipsq->ipsq_refs << 1;
15127 	ill_list_size = cnt * sizeof (ill_t *);
15128 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
15129 	if (ill_list == NULL) {
15130 		rw_exit(&ipst->ips_ill_g_lock);
15131 		return (ENOMEM);
15132 	}
15133 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
15134 
15135 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
15136 	mutex_enter(&new_ipsq->ipsq_lock);
15137 	if ((new_ipsq->ipsq_writer == NULL &&
15138 		new_ipsq->ipsq_current_ipif == NULL) ||
15139 	    (new_ipsq->ipsq_writer == curthread)) {
15140 		new_ipsq->ipsq_writer = curthread;
15141 		new_ipsq->ipsq_reentry_cnt++;
15142 		became_writer_on_new_sq = B_TRUE;
15143 	}
15144 
15145 	/*
15146 	 * We are holding ill_g_lock as writer and all the ill locks of
15147 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
15148 	 * message can land up on the old ipsq even though we don't hold the
15149 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
15150 	 */
15151 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
15152 
15153 	/*
15154 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
15155 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
15156 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
15157 	 */
15158 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
15159 
15160 	/*
15161 	 * Mark the new ipsq as needing a split since it is currently
15162 	 * being shared by more than 1 IPMP group. The split will
15163 	 * occur at the end of ipsq_exit
15164 	 */
15165 	new_ipsq->ipsq_split = B_TRUE;
15166 
15167 	/* Now release all the locks */
15168 	mutex_exit(&new_ipsq->ipsq_lock);
15169 	ill_unlock_ills(ill_list, cnt);
15170 	rw_exit(&ipst->ips_ill_g_lock);
15171 
15172 	kmem_free(ill_list, ill_list_size);
15173 
15174 	/*
15175 	 * If we succeeded in becoming writer on the new ipsq, then
15176 	 * drain the new ipsq and start processing  all enqueued messages
15177 	 * including the current ioctl we are processing which is either
15178 	 * a set groupname or failover/failback.
15179 	 */
15180 	if (became_writer_on_new_sq)
15181 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
15182 
15183 	/*
15184 	 * syncq has been changed and all the messages have been moved.
15185 	 */
15186 	mutex_enter(&old_ipsq->ipsq_lock);
15187 	old_ipsq->ipsq_current_ipif = NULL;
15188 	old_ipsq->ipsq_current_ioctl = 0;
15189 	mutex_exit(&old_ipsq->ipsq_lock);
15190 	return (EINPROGRESS);
15191 }
15192 
15193 /*
15194  * Delete and add the loopback copy and non-loopback copy of
15195  * the BROADCAST ire corresponding to ill and addr. Used to
15196  * group broadcast ires together when ill becomes part of
15197  * a group.
15198  *
15199  * This function is also called when ill is leaving the group
15200  * so that the ires belonging to the group gets re-grouped.
15201  */
15202 static void
15203 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
15204 {
15205 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
15206 	ire_t **ire_ptpn = &ire_head;
15207 	ip_stack_t	*ipst = ill->ill_ipst;
15208 
15209 	/*
15210 	 * The loopback and non-loopback IREs are inserted in the order in which
15211 	 * they're found, on the basis that they are correctly ordered (loopback
15212 	 * first).
15213 	 */
15214 	for (;;) {
15215 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15216 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15217 		if (ire == NULL)
15218 			break;
15219 
15220 		/*
15221 		 * we are passing in KM_SLEEP because it is not easy to
15222 		 * go back to a sane state in case of memory failure.
15223 		 */
15224 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
15225 		ASSERT(nire != NULL);
15226 		bzero(nire, sizeof (ire_t));
15227 		/*
15228 		 * Don't use ire_max_frag directly since we don't
15229 		 * hold on to 'ire' until we add the new ire 'nire' and
15230 		 * we don't want the new ire to have a dangling reference
15231 		 * to 'ire'. The ire_max_frag of a broadcast ire must
15232 		 * be in sync with the ipif_mtu of the associate ipif.
15233 		 * For eg. this happens as a result of SIOCSLIFNAME,
15234 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
15235 		 * the driver. A change in ire_max_frag triggered as
15236 		 * as a result of path mtu discovery, or due to an
15237 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
15238 		 * route change -mtu command does not apply to broadcast ires.
15239 		 *
15240 		 * XXX We need a recovery strategy here if ire_init fails
15241 		 */
15242 		if (ire_init(nire,
15243 		    (uchar_t *)&ire->ire_addr,
15244 		    (uchar_t *)&ire->ire_mask,
15245 		    (uchar_t *)&ire->ire_src_addr,
15246 		    (uchar_t *)&ire->ire_gateway_addr,
15247 		    (uchar_t *)&ire->ire_in_src_addr,
15248 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
15249 			&ire->ire_ipif->ipif_mtu,
15250 		    (ire->ire_nce != NULL ? ire->ire_nce->nce_fp_mp : NULL),
15251 		    ire->ire_rfq,
15252 		    ire->ire_stq,
15253 		    ire->ire_type,
15254 		    (ire->ire_nce != NULL? ire->ire_nce->nce_res_mp : NULL),
15255 		    ire->ire_ipif,
15256 		    ire->ire_in_ill,
15257 		    ire->ire_cmask,
15258 		    ire->ire_phandle,
15259 		    ire->ire_ihandle,
15260 		    ire->ire_flags,
15261 		    &ire->ire_uinfo,
15262 		    NULL,
15263 		    NULL,
15264 		    ipst) == NULL) {
15265 			cmn_err(CE_PANIC, "ire_init() failed");
15266 		}
15267 		ire_delete(ire);
15268 		ire_refrele(ire);
15269 
15270 		/*
15271 		 * The newly created IREs are inserted at the tail of the list
15272 		 * starting with ire_head. As we've just allocated them no one
15273 		 * knows about them so it's safe.
15274 		 */
15275 		*ire_ptpn = nire;
15276 		ire_ptpn = &nire->ire_next;
15277 	}
15278 
15279 	for (nire = ire_head; nire != NULL; nire = nire_next) {
15280 		int error;
15281 		ire_t *oire;
15282 		/* unlink the IRE from our list before calling ire_add() */
15283 		nire_next = nire->ire_next;
15284 		nire->ire_next = NULL;
15285 
15286 		/* ire_add adds the ire at the right place in the list */
15287 		oire = nire;
15288 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
15289 		ASSERT(error == 0);
15290 		ASSERT(oire == nire);
15291 		ire_refrele(nire);	/* Held in ire_add */
15292 	}
15293 }
15294 
15295 /*
15296  * This function is usually called when an ill is inserted in
15297  * a group and all the ipifs are already UP. As all the ipifs
15298  * are already UP, the broadcast ires have already been created
15299  * and been inserted. But, ire_add_v4 would not have grouped properly.
15300  * We need to re-group for the benefit of ip_wput_ire which
15301  * expects BROADCAST ires to be grouped properly to avoid sending
15302  * more than one copy of the broadcast packet per group.
15303  *
15304  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
15305  *	  because when ipif_up_done ends up calling this, ires have
15306  *        already been added before illgrp_insert i.e before ill_group
15307  *	  has been initialized.
15308  */
15309 static void
15310 ill_group_bcast_for_xmit(ill_t *ill)
15311 {
15312 	ill_group_t *illgrp;
15313 	ipif_t *ipif;
15314 	ipaddr_t addr;
15315 	ipaddr_t net_mask;
15316 	ipaddr_t subnet_netmask;
15317 
15318 	illgrp = ill->ill_group;
15319 
15320 	/*
15321 	 * This function is called even when an ill is deleted from
15322 	 * the group. Hence, illgrp could be null.
15323 	 */
15324 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15325 		return;
15326 
15327 	/*
15328 	 * Delete all the BROADCAST ires matching this ill and add
15329 	 * them back. This time, ire_add_v4 should take care of
15330 	 * grouping them with others because ill is part of the
15331 	 * group.
15332 	 */
15333 	ill_bcast_delete_and_add(ill, 0);
15334 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15335 
15336 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15337 
15338 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15339 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15340 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15341 		} else {
15342 			net_mask = htonl(IN_CLASSA_NET);
15343 		}
15344 		addr = net_mask & ipif->ipif_subnet;
15345 		ill_bcast_delete_and_add(ill, addr);
15346 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15347 
15348 		subnet_netmask = ipif->ipif_net_mask;
15349 		addr = ipif->ipif_subnet;
15350 		ill_bcast_delete_and_add(ill, addr);
15351 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15352 	}
15353 }
15354 
15355 /*
15356  * This function is called from illgrp_delete when ill is being deleted
15357  * from the group.
15358  *
15359  * As ill is not there in the group anymore, any address belonging
15360  * to this ill should be cleared of IRE_MARK_NORECV.
15361  */
15362 static void
15363 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15364 {
15365 	ire_t *ire;
15366 	irb_t *irb;
15367 	ip_stack_t	*ipst = ill->ill_ipst;
15368 
15369 	ASSERT(ill->ill_group == NULL);
15370 
15371 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15372 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15373 
15374 	if (ire != NULL) {
15375 		/*
15376 		 * IPMP and plumbing operations are serialized on the ipsq, so
15377 		 * no one will insert or delete a broadcast ire under our feet.
15378 		 */
15379 		irb = ire->ire_bucket;
15380 		rw_enter(&irb->irb_lock, RW_READER);
15381 		ire_refrele(ire);
15382 
15383 		for (; ire != NULL; ire = ire->ire_next) {
15384 			if (ire->ire_addr != addr)
15385 				break;
15386 			if (ire_to_ill(ire) != ill)
15387 				continue;
15388 
15389 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15390 			ire->ire_marks &= ~IRE_MARK_NORECV;
15391 		}
15392 		rw_exit(&irb->irb_lock);
15393 	}
15394 }
15395 
15396 /*
15397  * This function must be called only after the broadcast ires
15398  * have been grouped together. For a given address addr, nominate
15399  * only one of the ires whose interface is not FAILED or OFFLINE.
15400  *
15401  * This is also called when an ipif goes down, so that we can nominate
15402  * a different ire with the same address for receiving.
15403  */
15404 static void
15405 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15406 {
15407 	irb_t *irb;
15408 	ire_t *ire;
15409 	ire_t *ire1;
15410 	ire_t *save_ire;
15411 	ire_t **irep = NULL;
15412 	boolean_t first = B_TRUE;
15413 	ire_t *clear_ire = NULL;
15414 	ire_t *start_ire = NULL;
15415 	ire_t	*new_lb_ire;
15416 	ire_t	*new_nlb_ire;
15417 	boolean_t new_lb_ire_used = B_FALSE;
15418 	boolean_t new_nlb_ire_used = B_FALSE;
15419 	uint64_t match_flags;
15420 	uint64_t phyi_flags;
15421 	boolean_t fallback = B_FALSE;
15422 	uint_t	max_frag;
15423 
15424 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15425 	    NULL, MATCH_IRE_TYPE, ipst);
15426 	/*
15427 	 * We may not be able to find some ires if a previous
15428 	 * ire_create failed. This happens when an ipif goes
15429 	 * down and we are unable to create BROADCAST ires due
15430 	 * to memory failure. Thus, we have to check for NULL
15431 	 * below. This should handle the case for LOOPBACK,
15432 	 * POINTOPOINT and interfaces with some POINTOPOINT
15433 	 * logicals for which there are no BROADCAST ires.
15434 	 */
15435 	if (ire == NULL)
15436 		return;
15437 	/*
15438 	 * Currently IRE_BROADCASTS are deleted when an ipif
15439 	 * goes down which runs exclusively. Thus, setting
15440 	 * IRE_MARK_RCVD should not race with ire_delete marking
15441 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15442 	 * be consistent with other parts of the code that walks
15443 	 * a given bucket.
15444 	 */
15445 	save_ire = ire;
15446 	irb = ire->ire_bucket;
15447 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15448 	if (new_lb_ire == NULL) {
15449 		ire_refrele(ire);
15450 		return;
15451 	}
15452 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15453 	if (new_nlb_ire == NULL) {
15454 		ire_refrele(ire);
15455 		kmem_cache_free(ire_cache, new_lb_ire);
15456 		return;
15457 	}
15458 	IRB_REFHOLD(irb);
15459 	rw_enter(&irb->irb_lock, RW_WRITER);
15460 	/*
15461 	 * Get to the first ire matching the address and the
15462 	 * group. If the address does not match we are done
15463 	 * as we could not find the IRE. If the address matches
15464 	 * we should get to the first one matching the group.
15465 	 */
15466 	while (ire != NULL) {
15467 		if (ire->ire_addr != addr ||
15468 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15469 			break;
15470 		}
15471 		ire = ire->ire_next;
15472 	}
15473 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15474 	start_ire = ire;
15475 redo:
15476 	while (ire != NULL && ire->ire_addr == addr &&
15477 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15478 		/*
15479 		 * The first ire for any address within a group
15480 		 * should always be the one with IRE_MARK_NORECV cleared
15481 		 * so that ip_wput_ire can avoid searching for one.
15482 		 * Note down the insertion point which will be used
15483 		 * later.
15484 		 */
15485 		if (first && (irep == NULL))
15486 			irep = ire->ire_ptpn;
15487 		/*
15488 		 * PHYI_FAILED is set when the interface fails.
15489 		 * This interface might have become good, but the
15490 		 * daemon has not yet detected. We should still
15491 		 * not receive on this. PHYI_OFFLINE should never
15492 		 * be picked as this has been offlined and soon
15493 		 * be removed.
15494 		 */
15495 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15496 		if (phyi_flags & PHYI_OFFLINE) {
15497 			ire->ire_marks |= IRE_MARK_NORECV;
15498 			ire = ire->ire_next;
15499 			continue;
15500 		}
15501 		if (phyi_flags & match_flags) {
15502 			ire->ire_marks |= IRE_MARK_NORECV;
15503 			ire = ire->ire_next;
15504 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15505 			    PHYI_INACTIVE) {
15506 				fallback = B_TRUE;
15507 			}
15508 			continue;
15509 		}
15510 		if (first) {
15511 			/*
15512 			 * We will move this to the front of the list later
15513 			 * on.
15514 			 */
15515 			clear_ire = ire;
15516 			ire->ire_marks &= ~IRE_MARK_NORECV;
15517 		} else {
15518 			ire->ire_marks |= IRE_MARK_NORECV;
15519 		}
15520 		first = B_FALSE;
15521 		ire = ire->ire_next;
15522 	}
15523 	/*
15524 	 * If we never nominated anybody, try nominating at least
15525 	 * an INACTIVE, if we found one. Do it only once though.
15526 	 */
15527 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15528 	    fallback) {
15529 		match_flags = PHYI_FAILED;
15530 		ire = start_ire;
15531 		irep = NULL;
15532 		goto redo;
15533 	}
15534 	ire_refrele(save_ire);
15535 
15536 	/*
15537 	 * irep non-NULL indicates that we entered the while loop
15538 	 * above. If clear_ire is at the insertion point, we don't
15539 	 * have to do anything. clear_ire will be NULL if all the
15540 	 * interfaces are failed.
15541 	 *
15542 	 * We cannot unlink and reinsert the ire at the right place
15543 	 * in the list since there can be other walkers of this bucket.
15544 	 * Instead we delete and recreate the ire
15545 	 */
15546 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15547 		ire_t *clear_ire_stq = NULL;
15548 		mblk_t *fp_mp = NULL, *res_mp = NULL;
15549 
15550 		bzero(new_lb_ire, sizeof (ire_t));
15551 		if (clear_ire->ire_nce != NULL) {
15552 			fp_mp = clear_ire->ire_nce->nce_fp_mp;
15553 			res_mp = clear_ire->ire_nce->nce_res_mp;
15554 		}
15555 		/* XXX We need a recovery strategy here. */
15556 		if (ire_init(new_lb_ire,
15557 		    (uchar_t *)&clear_ire->ire_addr,
15558 		    (uchar_t *)&clear_ire->ire_mask,
15559 		    (uchar_t *)&clear_ire->ire_src_addr,
15560 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15561 		    (uchar_t *)&clear_ire->ire_in_src_addr,
15562 		    &clear_ire->ire_max_frag,
15563 		    fp_mp,
15564 		    clear_ire->ire_rfq,
15565 		    clear_ire->ire_stq,
15566 		    clear_ire->ire_type,
15567 		    res_mp,
15568 		    clear_ire->ire_ipif,
15569 		    clear_ire->ire_in_ill,
15570 		    clear_ire->ire_cmask,
15571 		    clear_ire->ire_phandle,
15572 		    clear_ire->ire_ihandle,
15573 		    clear_ire->ire_flags,
15574 		    &clear_ire->ire_uinfo,
15575 		    NULL,
15576 		    NULL,
15577 		    ipst) == NULL)
15578 			cmn_err(CE_PANIC, "ire_init() failed");
15579 		if (clear_ire->ire_stq == NULL) {
15580 			ire_t *ire_next = clear_ire->ire_next;
15581 			if (ire_next != NULL &&
15582 			    ire_next->ire_stq != NULL &&
15583 			    ire_next->ire_addr == clear_ire->ire_addr &&
15584 			    ire_next->ire_ipif->ipif_ill ==
15585 			    clear_ire->ire_ipif->ipif_ill) {
15586 				clear_ire_stq = ire_next;
15587 
15588 				bzero(new_nlb_ire, sizeof (ire_t));
15589 				if (clear_ire_stq->ire_nce != NULL) {
15590 					fp_mp =
15591 					    clear_ire_stq->ire_nce->nce_fp_mp;
15592 					res_mp =
15593 					    clear_ire_stq->ire_nce->nce_res_mp;
15594 				} else {
15595 					fp_mp = res_mp = NULL;
15596 				}
15597 				/* XXX We need a recovery strategy here. */
15598 				if (ire_init(new_nlb_ire,
15599 				    (uchar_t *)&clear_ire_stq->ire_addr,
15600 				    (uchar_t *)&clear_ire_stq->ire_mask,
15601 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15602 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15603 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
15604 				    &clear_ire_stq->ire_max_frag,
15605 				    fp_mp,
15606 				    clear_ire_stq->ire_rfq,
15607 				    clear_ire_stq->ire_stq,
15608 				    clear_ire_stq->ire_type,
15609 				    res_mp,
15610 				    clear_ire_stq->ire_ipif,
15611 				    clear_ire_stq->ire_in_ill,
15612 				    clear_ire_stq->ire_cmask,
15613 				    clear_ire_stq->ire_phandle,
15614 				    clear_ire_stq->ire_ihandle,
15615 				    clear_ire_stq->ire_flags,
15616 				    &clear_ire_stq->ire_uinfo,
15617 				    NULL,
15618 				    NULL,
15619 				    ipst) == NULL)
15620 					cmn_err(CE_PANIC, "ire_init() failed");
15621 			}
15622 		}
15623 
15624 		/*
15625 		 * Delete the ire. We can't call ire_delete() since
15626 		 * we are holding the bucket lock. We can't release the
15627 		 * bucket lock since we can't allow irep to change. So just
15628 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15629 		 * ire from the list and do the refrele.
15630 		 */
15631 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15632 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15633 
15634 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15635 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15636 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15637 		}
15638 
15639 		/*
15640 		 * Also take care of otherfields like ib/ob pkt count
15641 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15642 		 */
15643 
15644 		/* Set the max_frag before adding the ire */
15645 		max_frag = *new_lb_ire->ire_max_fragp;
15646 		new_lb_ire->ire_max_fragp = NULL;
15647 		new_lb_ire->ire_max_frag = max_frag;
15648 
15649 		/* Add the new ire's. Insert at *irep */
15650 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15651 		ire1 = *irep;
15652 		if (ire1 != NULL)
15653 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15654 		new_lb_ire->ire_next = ire1;
15655 		/* Link the new one in. */
15656 		new_lb_ire->ire_ptpn = irep;
15657 		membar_producer();
15658 		*irep = new_lb_ire;
15659 		new_lb_ire_used = B_TRUE;
15660 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15661 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15662 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15663 
15664 		if (clear_ire_stq != NULL) {
15665 			/* Set the max_frag before adding the ire */
15666 			max_frag = *new_nlb_ire->ire_max_fragp;
15667 			new_nlb_ire->ire_max_fragp = NULL;
15668 			new_nlb_ire->ire_max_frag = max_frag;
15669 
15670 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15671 			irep = &new_lb_ire->ire_next;
15672 			/* Add the new ire. Insert at *irep */
15673 			ire1 = *irep;
15674 			if (ire1 != NULL)
15675 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15676 			new_nlb_ire->ire_next = ire1;
15677 			/* Link the new one in. */
15678 			new_nlb_ire->ire_ptpn = irep;
15679 			membar_producer();
15680 			*irep = new_nlb_ire;
15681 			new_nlb_ire_used = B_TRUE;
15682 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15683 			    ire_stats_inserted);
15684 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15685 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15686 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15687 		}
15688 	}
15689 	rw_exit(&irb->irb_lock);
15690 	if (!new_lb_ire_used)
15691 		kmem_cache_free(ire_cache, new_lb_ire);
15692 	if (!new_nlb_ire_used)
15693 		kmem_cache_free(ire_cache, new_nlb_ire);
15694 	IRB_REFRELE(irb);
15695 }
15696 
15697 /*
15698  * Whenever an ipif goes down we have to renominate a different
15699  * broadcast ire to receive. Whenever an ipif comes up, we need
15700  * to make sure that we have only one nominated to receive.
15701  */
15702 static void
15703 ipif_renominate_bcast(ipif_t *ipif)
15704 {
15705 	ill_t *ill = ipif->ipif_ill;
15706 	ipaddr_t subnet_addr;
15707 	ipaddr_t net_addr;
15708 	ipaddr_t net_mask = 0;
15709 	ipaddr_t subnet_netmask;
15710 	ipaddr_t addr;
15711 	ill_group_t *illgrp;
15712 	ip_stack_t	*ipst = ill->ill_ipst;
15713 
15714 	illgrp = ill->ill_group;
15715 	/*
15716 	 * If this is the last ipif going down, it might take
15717 	 * the ill out of the group. In that case ipif_down ->
15718 	 * illgrp_delete takes care of doing the nomination.
15719 	 * ipif_down does not call for this case.
15720 	 */
15721 	ASSERT(illgrp != NULL);
15722 
15723 	/* There could not have been any ires associated with this */
15724 	if (ipif->ipif_subnet == 0)
15725 		return;
15726 
15727 	ill_mark_bcast(illgrp, 0, ipst);
15728 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15729 
15730 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15731 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15732 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15733 	} else {
15734 		net_mask = htonl(IN_CLASSA_NET);
15735 	}
15736 	addr = net_mask & ipif->ipif_subnet;
15737 	ill_mark_bcast(illgrp, addr, ipst);
15738 
15739 	net_addr = ~net_mask | addr;
15740 	ill_mark_bcast(illgrp, net_addr, ipst);
15741 
15742 	subnet_netmask = ipif->ipif_net_mask;
15743 	addr = ipif->ipif_subnet;
15744 	ill_mark_bcast(illgrp, addr, ipst);
15745 
15746 	subnet_addr = ~subnet_netmask | addr;
15747 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15748 }
15749 
15750 /*
15751  * Whenever we form or delete ill groups, we need to nominate one set of
15752  * BROADCAST ires for receiving in the group.
15753  *
15754  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15755  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15756  *    for ill_ipif_up_count to be non-zero. This is the only case where
15757  *    ill_ipif_up_count is zero and we would still find the ires.
15758  *
15759  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15760  *    ipif is UP and we just have to do the nomination.
15761  *
15762  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15763  *    from the group. So, we have to do the nomination.
15764  *
15765  * Because of (3), there could be just one ill in the group. But we have
15766  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15767  * Thus, this function does not optimize when there is only one ill as
15768  * it is not correct for (3).
15769  */
15770 static void
15771 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15772 {
15773 	ill_t *ill;
15774 	ipif_t *ipif;
15775 	ipaddr_t subnet_addr;
15776 	ipaddr_t prev_subnet_addr = 0;
15777 	ipaddr_t net_addr;
15778 	ipaddr_t prev_net_addr = 0;
15779 	ipaddr_t net_mask = 0;
15780 	ipaddr_t subnet_netmask;
15781 	ipaddr_t addr;
15782 	ip_stack_t	*ipst;
15783 
15784 	/*
15785 	 * When the last memeber is leaving, there is nothing to
15786 	 * nominate.
15787 	 */
15788 	if (illgrp->illgrp_ill_count == 0) {
15789 		ASSERT(illgrp->illgrp_ill == NULL);
15790 		return;
15791 	}
15792 
15793 	ill = illgrp->illgrp_ill;
15794 	ASSERT(!ill->ill_isv6);
15795 	ipst = ill->ill_ipst;
15796 	/*
15797 	 * We assume that ires with same address and belonging to the
15798 	 * same group, has been grouped together. Nominating a *single*
15799 	 * ill in the group for sending and receiving broadcast is done
15800 	 * by making sure that the first BROADCAST ire (which will be
15801 	 * the one returned by ire_ctable_lookup for ip_rput and the
15802 	 * one that will be used in ip_wput_ire) will be the one that
15803 	 * will not have IRE_MARK_NORECV set.
15804 	 *
15805 	 * 1) ip_rput checks and discards packets received on ires marked
15806 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15807 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15808 	 *    first ire in the group for every broadcast address in the group.
15809 	 *    ip_rput will accept packets only on the first ire i.e only
15810 	 *    one copy of the ill.
15811 	 *
15812 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15813 	 *    packet for the whole group. It needs to send out on the ill
15814 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15815 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15816 	 *    the copy echoed back on other port where the ire is not marked
15817 	 *    with IRE_MARK_NORECV.
15818 	 *
15819 	 * Note that we just need to have the first IRE either loopback or
15820 	 * non-loopback (either of them may not exist if ire_create failed
15821 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15822 	 * always hit the first one and hence will always accept one copy.
15823 	 *
15824 	 * We have a broadcast ire per ill for all the unique prefixes
15825 	 * hosted on that ill. As we don't have a way of knowing the
15826 	 * unique prefixes on a given ill and hence in the whole group,
15827 	 * we just call ill_mark_bcast on all the prefixes that exist
15828 	 * in the group. For the common case of one prefix, the code
15829 	 * below optimizes by remebering the last address used for
15830 	 * markng. In the case of multiple prefixes, this will still
15831 	 * optimize depending the order of prefixes.
15832 	 *
15833 	 * The only unique address across the whole group is 0.0.0.0 and
15834 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15835 	 * the first ire in the bucket for receiving and disables the
15836 	 * others.
15837 	 */
15838 	ill_mark_bcast(illgrp, 0, ipst);
15839 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15840 	for (; ill != NULL; ill = ill->ill_group_next) {
15841 
15842 		for (ipif = ill->ill_ipif; ipif != NULL;
15843 		    ipif = ipif->ipif_next) {
15844 
15845 			if (!(ipif->ipif_flags & IPIF_UP) ||
15846 			    ipif->ipif_subnet == 0) {
15847 				continue;
15848 			}
15849 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15850 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15851 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15852 			} else {
15853 				net_mask = htonl(IN_CLASSA_NET);
15854 			}
15855 			addr = net_mask & ipif->ipif_subnet;
15856 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15857 				ill_mark_bcast(illgrp, addr, ipst);
15858 				net_addr = ~net_mask | addr;
15859 				ill_mark_bcast(illgrp, net_addr, ipst);
15860 			}
15861 			prev_net_addr = addr;
15862 
15863 			subnet_netmask = ipif->ipif_net_mask;
15864 			addr = ipif->ipif_subnet;
15865 			if (prev_subnet_addr == 0 ||
15866 			    prev_subnet_addr != addr) {
15867 				ill_mark_bcast(illgrp, addr, ipst);
15868 				subnet_addr = ~subnet_netmask | addr;
15869 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15870 			}
15871 			prev_subnet_addr = addr;
15872 		}
15873 	}
15874 }
15875 
15876 /*
15877  * This function is called while forming ill groups.
15878  *
15879  * Currently, we handle only allmulti groups. We want to join
15880  * allmulti on only one of the ills in the groups. In future,
15881  * when we have link aggregation, we may have to join normal
15882  * multicast groups on multiple ills as switch does inbound load
15883  * balancing. Following are the functions that calls this
15884  * function :
15885  *
15886  * 1) ill_recover_multicast : Interface is coming back UP.
15887  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15888  *    will call ill_recover_multicast to recover all the multicast
15889  *    groups. We need to make sure that only one member is joined
15890  *    in the ill group.
15891  *
15892  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15893  *    Somebody is joining allmulti. We need to make sure that only one
15894  *    member is joined in the group.
15895  *
15896  * 3) illgrp_insert : If allmulti has already joined, we need to make
15897  *    sure that only one member is joined in the group.
15898  *
15899  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15900  *    allmulti who we have nominated. We need to pick someother ill.
15901  *
15902  * 5) illgrp_delete : The ill we nominated is leaving the group,
15903  *    we need to pick a new ill to join the group.
15904  *
15905  * For (1), (2), (5) - we just have to check whether there is
15906  * a good ill joined in the group. If we could not find any ills
15907  * joined the group, we should join.
15908  *
15909  * For (4), the one that was nominated to receive, left the group.
15910  * There could be nobody joined in the group when this function is
15911  * called.
15912  *
15913  * For (3) - we need to explicitly check whether there are multiple
15914  * ills joined in the group.
15915  *
15916  * For simplicity, we don't differentiate any of the above cases. We
15917  * just leave the group if it is joined on any of them and join on
15918  * the first good ill.
15919  */
15920 int
15921 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15922 {
15923 	ilm_t *ilm;
15924 	ill_t *ill;
15925 	ill_t *fallback_inactive_ill = NULL;
15926 	ill_t *fallback_failed_ill = NULL;
15927 	int ret = 0;
15928 
15929 	/*
15930 	 * Leave the allmulti on all the ills and start fresh.
15931 	 */
15932 	for (ill = illgrp->illgrp_ill; ill != NULL;
15933 	    ill = ill->ill_group_next) {
15934 		if (ill->ill_join_allmulti)
15935 			(void) ip_leave_allmulti(ill->ill_ipif);
15936 	}
15937 
15938 	/*
15939 	 * Choose a good ill. Fallback to inactive or failed if
15940 	 * none available. We need to fallback to FAILED in the
15941 	 * case where we have 2 interfaces in a group - where
15942 	 * one of them is failed and another is a good one and
15943 	 * the good one (not marked inactive) is leaving the group.
15944 	 */
15945 	ret = 0;
15946 	for (ill = illgrp->illgrp_ill; ill != NULL;
15947 	    ill = ill->ill_group_next) {
15948 		/* Never pick an offline interface */
15949 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15950 			continue;
15951 
15952 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15953 			fallback_failed_ill = ill;
15954 			continue;
15955 		}
15956 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15957 			fallback_inactive_ill = ill;
15958 			continue;
15959 		}
15960 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15961 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15962 				ret = ip_join_allmulti(ill->ill_ipif);
15963 				/*
15964 				 * ip_join_allmulti can fail because of memory
15965 				 * failures. So, make sure we join at least
15966 				 * on one ill.
15967 				 */
15968 				if (ill->ill_join_allmulti)
15969 					return (0);
15970 			}
15971 		}
15972 	}
15973 	if (ret != 0) {
15974 		/*
15975 		 * If we tried nominating above and failed to do so,
15976 		 * return error. We might have tried multiple times.
15977 		 * But, return the latest error.
15978 		 */
15979 		return (ret);
15980 	}
15981 	if ((ill = fallback_inactive_ill) != NULL) {
15982 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15983 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15984 				ret = ip_join_allmulti(ill->ill_ipif);
15985 				return (ret);
15986 			}
15987 		}
15988 	} else if ((ill = fallback_failed_ill) != NULL) {
15989 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15990 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15991 				ret = ip_join_allmulti(ill->ill_ipif);
15992 				return (ret);
15993 			}
15994 		}
15995 	}
15996 	return (0);
15997 }
15998 
15999 /*
16000  * This function is called from illgrp_delete after it is
16001  * deleted from the group to reschedule responsibilities
16002  * to a different ill.
16003  */
16004 static void
16005 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
16006 {
16007 	ilm_t	*ilm;
16008 	ipif_t	*ipif;
16009 	ipaddr_t subnet_addr;
16010 	ipaddr_t net_addr;
16011 	ipaddr_t net_mask = 0;
16012 	ipaddr_t subnet_netmask;
16013 	ipaddr_t addr;
16014 	ip_stack_t *ipst = ill->ill_ipst;
16015 
16016 	ASSERT(ill->ill_group == NULL);
16017 	/*
16018 	 * Broadcast Responsibility:
16019 	 *
16020 	 * 1. If this ill has been nominated for receiving broadcast
16021 	 * packets, we need to find a new one. Before we find a new
16022 	 * one, we need to re-group the ires that are part of this new
16023 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
16024 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
16025 	 * thing for us.
16026 	 *
16027 	 * 2. If this ill was not nominated for receiving broadcast
16028 	 * packets, we need to clear the IRE_MARK_NORECV flag
16029 	 * so that we continue to send up broadcast packets.
16030 	 */
16031 	if (!ill->ill_isv6) {
16032 		/*
16033 		 * Case 1 above : No optimization here. Just redo the
16034 		 * nomination.
16035 		 */
16036 		ill_group_bcast_for_xmit(ill);
16037 		ill_nominate_bcast_rcv(illgrp);
16038 
16039 		/*
16040 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
16041 		 */
16042 		ill_clear_bcast_mark(ill, 0);
16043 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
16044 
16045 		for (ipif = ill->ill_ipif; ipif != NULL;
16046 		    ipif = ipif->ipif_next) {
16047 
16048 			if (!(ipif->ipif_flags & IPIF_UP) ||
16049 			    ipif->ipif_subnet == 0) {
16050 				continue;
16051 			}
16052 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16053 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16054 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
16055 			} else {
16056 				net_mask = htonl(IN_CLASSA_NET);
16057 			}
16058 			addr = net_mask & ipif->ipif_subnet;
16059 			ill_clear_bcast_mark(ill, addr);
16060 
16061 			net_addr = ~net_mask | addr;
16062 			ill_clear_bcast_mark(ill, net_addr);
16063 
16064 			subnet_netmask = ipif->ipif_net_mask;
16065 			addr = ipif->ipif_subnet;
16066 			ill_clear_bcast_mark(ill, addr);
16067 
16068 			subnet_addr = ~subnet_netmask | addr;
16069 			ill_clear_bcast_mark(ill, subnet_addr);
16070 		}
16071 	}
16072 
16073 	/*
16074 	 * Multicast Responsibility.
16075 	 *
16076 	 * If we have joined allmulti on this one, find a new member
16077 	 * in the group to join allmulti. As this ill is already part
16078 	 * of allmulti, we don't have to join on this one.
16079 	 *
16080 	 * If we have not joined allmulti on this one, there is no
16081 	 * responsibility to handoff. But we need to take new
16082 	 * responsibility i.e, join allmulti on this one if we need
16083 	 * to.
16084 	 */
16085 	if (ill->ill_join_allmulti) {
16086 		(void) ill_nominate_mcast_rcv(illgrp);
16087 	} else {
16088 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16089 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16090 				(void) ip_join_allmulti(ill->ill_ipif);
16091 				break;
16092 			}
16093 		}
16094 	}
16095 
16096 	/*
16097 	 * We intentionally do the flushing of IRE_CACHES only matching
16098 	 * on the ill and not on groups. Note that we are already deleted
16099 	 * from the group.
16100 	 *
16101 	 * This will make sure that all IRE_CACHES whose stq is pointing
16102 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
16103 	 * deleted and IRE_CACHES that are not pointing at this ill will
16104 	 * be left alone.
16105 	 */
16106 	if (ill->ill_isv6) {
16107 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16108 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
16109 	} else {
16110 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16111 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
16112 	}
16113 
16114 	/*
16115 	 * Some conn may have cached one of the IREs deleted above. By removing
16116 	 * the ire reference, we clean up the extra reference to the ill held in
16117 	 * ire->ire_stq.
16118 	 */
16119 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
16120 
16121 	/*
16122 	 * Re-do source address selection for all the members in the
16123 	 * group, if they borrowed source address from one of the ipifs
16124 	 * in this ill.
16125 	 */
16126 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
16127 		if (ill->ill_isv6) {
16128 			ipif_update_other_ipifs_v6(ipif, illgrp);
16129 		} else {
16130 			ipif_update_other_ipifs(ipif, illgrp);
16131 		}
16132 	}
16133 }
16134 
16135 /*
16136  * Delete the ill from the group. The caller makes sure that it is
16137  * in a group and it okay to delete from the group. So, we always
16138  * delete here.
16139  */
16140 static void
16141 illgrp_delete(ill_t *ill)
16142 {
16143 	ill_group_t *illgrp;
16144 	ill_group_t *tmpg;
16145 	ill_t *tmp_ill;
16146 	ip_stack_t	*ipst = ill->ill_ipst;
16147 
16148 	/*
16149 	 * Reset illgrp_ill_schednext if it was pointing at us.
16150 	 * We need to do this before we set ill_group to NULL.
16151 	 */
16152 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16153 	mutex_enter(&ill->ill_lock);
16154 
16155 	illgrp_reset_schednext(ill);
16156 
16157 	illgrp = ill->ill_group;
16158 
16159 	/* Delete the ill from illgrp. */
16160 	if (illgrp->illgrp_ill == ill) {
16161 		illgrp->illgrp_ill = ill->ill_group_next;
16162 	} else {
16163 		tmp_ill = illgrp->illgrp_ill;
16164 		while (tmp_ill->ill_group_next != ill) {
16165 			tmp_ill = tmp_ill->ill_group_next;
16166 			ASSERT(tmp_ill != NULL);
16167 		}
16168 		tmp_ill->ill_group_next = ill->ill_group_next;
16169 	}
16170 	ill->ill_group = NULL;
16171 	ill->ill_group_next = NULL;
16172 
16173 	illgrp->illgrp_ill_count--;
16174 	mutex_exit(&ill->ill_lock);
16175 	rw_exit(&ipst->ips_ill_g_lock);
16176 
16177 	/*
16178 	 * As this ill is leaving the group, we need to hand off
16179 	 * the responsibilities to the other ills in the group, if
16180 	 * this ill had some responsibilities.
16181 	 */
16182 
16183 	ill_handoff_responsibility(ill, illgrp);
16184 
16185 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16186 
16187 	if (illgrp->illgrp_ill_count == 0) {
16188 
16189 		ASSERT(illgrp->illgrp_ill == NULL);
16190 		if (ill->ill_isv6) {
16191 			if (illgrp == ipst->ips_illgrp_head_v6) {
16192 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
16193 			} else {
16194 				tmpg = ipst->ips_illgrp_head_v6;
16195 				while (tmpg->illgrp_next != illgrp) {
16196 					tmpg = tmpg->illgrp_next;
16197 					ASSERT(tmpg != NULL);
16198 				}
16199 				tmpg->illgrp_next = illgrp->illgrp_next;
16200 			}
16201 		} else {
16202 			if (illgrp == ipst->ips_illgrp_head_v4) {
16203 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
16204 			} else {
16205 				tmpg = ipst->ips_illgrp_head_v4;
16206 				while (tmpg->illgrp_next != illgrp) {
16207 					tmpg = tmpg->illgrp_next;
16208 					ASSERT(tmpg != NULL);
16209 				}
16210 				tmpg->illgrp_next = illgrp->illgrp_next;
16211 			}
16212 		}
16213 		mutex_destroy(&illgrp->illgrp_lock);
16214 		mi_free(illgrp);
16215 	}
16216 	rw_exit(&ipst->ips_ill_g_lock);
16217 
16218 	/*
16219 	 * Even though the ill is out of the group its not necessary
16220 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
16221 	 * We will split the ipsq when phyint_groupname is set to NULL.
16222 	 */
16223 
16224 	/*
16225 	 * Send a routing sockets message if we are deleting from
16226 	 * groups with names.
16227 	 */
16228 	if (ill->ill_phyint->phyint_groupname_len != 0)
16229 		ip_rts_ifmsg(ill->ill_ipif);
16230 }
16231 
16232 /*
16233  * Re-do source address selection. This is normally called when
16234  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
16235  * ipif comes up.
16236  */
16237 void
16238 ill_update_source_selection(ill_t *ill)
16239 {
16240 	ipif_t *ipif;
16241 
16242 	ASSERT(IAM_WRITER_ILL(ill));
16243 
16244 	if (ill->ill_group != NULL)
16245 		ill = ill->ill_group->illgrp_ill;
16246 
16247 	for (; ill != NULL; ill = ill->ill_group_next) {
16248 		for (ipif = ill->ill_ipif; ipif != NULL;
16249 		    ipif = ipif->ipif_next) {
16250 			if (ill->ill_isv6)
16251 				ipif_recreate_interface_routes_v6(NULL, ipif);
16252 			else
16253 				ipif_recreate_interface_routes(NULL, ipif);
16254 		}
16255 	}
16256 }
16257 
16258 /*
16259  * Insert ill in a group headed by illgrp_head. The caller can either
16260  * pass a groupname in which case we search for a group with the
16261  * same name to insert in or pass a group to insert in. This function
16262  * would only search groups with names.
16263  *
16264  * NOTE : The caller should make sure that there is at least one ipif
16265  *	  UP on this ill so that illgrp_scheduler can pick this ill
16266  *	  for outbound packets. If ill_ipif_up_count is zero, we have
16267  *	  already sent a DL_UNBIND to the driver and we don't want to
16268  *	  send anymore packets. We don't assert for ipif_up_count
16269  *	  to be greater than zero, because ipif_up_done wants to call
16270  *	  this function before bumping up the ipif_up_count. See
16271  *	  ipif_up_done() for details.
16272  */
16273 int
16274 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
16275     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
16276 {
16277 	ill_group_t *illgrp;
16278 	ill_t *prev_ill;
16279 	phyint_t *phyi;
16280 	ip_stack_t	*ipst = ill->ill_ipst;
16281 
16282 	ASSERT(ill->ill_group == NULL);
16283 
16284 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16285 	mutex_enter(&ill->ill_lock);
16286 
16287 	if (groupname != NULL) {
16288 		/*
16289 		 * Look for a group with a matching groupname to insert.
16290 		 */
16291 		for (illgrp = *illgrp_head; illgrp != NULL;
16292 		    illgrp = illgrp->illgrp_next) {
16293 
16294 			ill_t *tmp_ill;
16295 
16296 			/*
16297 			 * If we have an ill_group_t in the list which has
16298 			 * no ill_t assigned then we must be in the process of
16299 			 * removing this group. We skip this as illgrp_delete()
16300 			 * will remove it from the list.
16301 			 */
16302 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
16303 				ASSERT(illgrp->illgrp_ill_count == 0);
16304 				continue;
16305 			}
16306 
16307 			ASSERT(tmp_ill->ill_phyint != NULL);
16308 			phyi = tmp_ill->ill_phyint;
16309 			/*
16310 			 * Look at groups which has names only.
16311 			 */
16312 			if (phyi->phyint_groupname_len == 0)
16313 				continue;
16314 			/*
16315 			 * Names are stored in the phyint common to both
16316 			 * IPv4 and IPv6.
16317 			 */
16318 			if (mi_strcmp(phyi->phyint_groupname,
16319 			    groupname) == 0) {
16320 				break;
16321 			}
16322 		}
16323 	} else {
16324 		/*
16325 		 * If the caller passes in a NULL "grp_to_insert", we
16326 		 * allocate one below and insert this singleton.
16327 		 */
16328 		illgrp = grp_to_insert;
16329 	}
16330 
16331 	ill->ill_group_next = NULL;
16332 
16333 	if (illgrp == NULL) {
16334 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16335 		if (illgrp == NULL) {
16336 			return (ENOMEM);
16337 		}
16338 		illgrp->illgrp_next = *illgrp_head;
16339 		*illgrp_head = illgrp;
16340 		illgrp->illgrp_ill = ill;
16341 		illgrp->illgrp_ill_count = 1;
16342 		ill->ill_group = illgrp;
16343 		/*
16344 		 * Used in illgrp_scheduler to protect multiple threads
16345 		 * from traversing the list.
16346 		 */
16347 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16348 	} else {
16349 		ASSERT(ill->ill_net_type ==
16350 		    illgrp->illgrp_ill->ill_net_type);
16351 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16352 
16353 		/* Insert ill at tail of this group */
16354 		prev_ill = illgrp->illgrp_ill;
16355 		while (prev_ill->ill_group_next != NULL)
16356 			prev_ill = prev_ill->ill_group_next;
16357 		prev_ill->ill_group_next = ill;
16358 		ill->ill_group = illgrp;
16359 		illgrp->illgrp_ill_count++;
16360 		/*
16361 		 * Inherit group properties. Currently only forwarding
16362 		 * is the property we try to keep the same with all the
16363 		 * ills. When there are more, we will abstract this into
16364 		 * a function.
16365 		 */
16366 		ill->ill_flags &= ~ILLF_ROUTER;
16367 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16368 	}
16369 	mutex_exit(&ill->ill_lock);
16370 	rw_exit(&ipst->ips_ill_g_lock);
16371 
16372 	/*
16373 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16374 	 *    may be zero as it has not yet been bumped. But the ires
16375 	 *    have already been added. So, we do the nomination here
16376 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16377 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16378 	 *    ill_ipif_up_count here while nominating broadcast ires for
16379 	 *    receive.
16380 	 *
16381 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16382 	 *    to group them properly as ire_add() has already happened
16383 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16384 	 *    case, we need to do it here anyway.
16385 	 */
16386 	if (!ill->ill_isv6) {
16387 		ill_group_bcast_for_xmit(ill);
16388 		ill_nominate_bcast_rcv(illgrp);
16389 	}
16390 
16391 	if (!ipif_is_coming_up) {
16392 		/*
16393 		 * When ipif_up_done() calls this function, the multicast
16394 		 * groups have not been joined yet. So, there is no point in
16395 		 * nomination. ip_join_allmulti will handle groups when
16396 		 * ill_recover_multicast is called from ipif_up_done() later.
16397 		 */
16398 		(void) ill_nominate_mcast_rcv(illgrp);
16399 		/*
16400 		 * ipif_up_done calls ill_update_source_selection
16401 		 * anyway. Moreover, we don't want to re-create
16402 		 * interface routes while ipif_up_done() still has reference
16403 		 * to them. Refer to ipif_up_done() for more details.
16404 		 */
16405 		ill_update_source_selection(ill);
16406 	}
16407 
16408 	/*
16409 	 * Send a routing sockets message if we are inserting into
16410 	 * groups with names.
16411 	 */
16412 	if (groupname != NULL)
16413 		ip_rts_ifmsg(ill->ill_ipif);
16414 	return (0);
16415 }
16416 
16417 /*
16418  * Return the first phyint matching the groupname. There could
16419  * be more than one when there are ill groups.
16420  *
16421  * If 'usable' is set, then we exclude ones that are marked with any of
16422  * (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE|PHYI_INACTIVE).
16423  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16424  * emulation of ipmp.
16425  */
16426 phyint_t *
16427 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16428 {
16429 	phyint_t *phyi;
16430 
16431 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16432 	/*
16433 	 * Group names are stored in the phyint - a common structure
16434 	 * to both IPv4 and IPv6.
16435 	 */
16436 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16437 	for (; phyi != NULL;
16438 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16439 	    phyi, AVL_AFTER)) {
16440 		if (phyi->phyint_groupname_len == 0)
16441 			continue;
16442 		/*
16443 		 * Skip the ones that should not be used since the callers
16444 		 * sometime use this for sending packets.
16445 		 */
16446 		if (usable && (phyi->phyint_flags &
16447 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE|PHYI_INACTIVE)))
16448 			continue;
16449 
16450 		ASSERT(phyi->phyint_groupname != NULL);
16451 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16452 			return (phyi);
16453 	}
16454 	return (NULL);
16455 }
16456 
16457 
16458 /*
16459  * Return the first usable phyint matching the group index. By 'usable'
16460  * we exclude ones that are marked ununsable with any of
16461  * (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE|PHYI_INACTIVE).
16462  *
16463  * Used only for the ipmp/netinfo emulation of ipmp.
16464  */
16465 phyint_t *
16466 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16467 {
16468 	phyint_t *phyi;
16469 
16470 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16471 
16472 	if (!ipst->ips_ipmp_hook_emulation)
16473 		return (NULL);
16474 
16475 	/*
16476 	 * Group indicies are stored in the phyint - a common structure
16477 	 * to both IPv4 and IPv6.
16478 	 */
16479 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16480 	for (; phyi != NULL;
16481 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16482 	    phyi, AVL_AFTER)) {
16483 		/* Ignore the ones that do not have a group */
16484 		if (phyi->phyint_groupname_len == 0)
16485 			continue;
16486 
16487 		ASSERT(phyi->phyint_group_ifindex != 0);
16488 		/*
16489 		 * Skip the ones that should not be used since the callers
16490 		 * sometime use this for sending packets.
16491 		 */
16492 		if (phyi->phyint_flags &
16493 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE|PHYI_INACTIVE))
16494 			continue;
16495 		if (phyi->phyint_group_ifindex == group_ifindex)
16496 			return (phyi);
16497 	}
16498 	return (NULL);
16499 }
16500 
16501 
16502 /*
16503  * MT notes on creation and deletion of IPMP groups
16504  *
16505  * Creation and deletion of IPMP groups introduce the need to merge or
16506  * split the associated serialization objects i.e the ipsq's. Normally all
16507  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16508  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16509  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16510  * is a need to change the <ill-ipsq> association and we have to operate on both
16511  * the source and destination IPMP groups. For eg. attempting to set the
16512  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16513  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16514  * source or destination IPMP group are mapped to a single ipsq for executing
16515  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16516  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16517  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16518  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16519  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16520  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16521  *
16522  * In the above example the ioctl handling code locates the current ipsq of hme0
16523  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16524  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16525  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16526  * the destination ipsq. If the destination ipsq is not busy, it also enters
16527  * the destination ipsq exclusively. Now the actual groupname setting operation
16528  * can proceed. If the destination ipsq is busy, the operation is enqueued
16529  * on the destination (merged) ipsq and will be handled in the unwind from
16530  * ipsq_exit.
16531  *
16532  * To prevent other threads accessing the ill while the group name change is
16533  * in progres, we bring down the ipifs which also removes the ill from the
16534  * group. The group is changed in phyint and when the first ipif on the ill
16535  * is brought up, the ill is inserted into the right IPMP group by
16536  * illgrp_insert.
16537  */
16538 /* ARGSUSED */
16539 int
16540 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16541     ip_ioctl_cmd_t *ipip, void *ifreq)
16542 {
16543 	int i;
16544 	char *tmp;
16545 	int namelen;
16546 	ill_t *ill = ipif->ipif_ill;
16547 	ill_t *ill_v4, *ill_v6;
16548 	int err = 0;
16549 	phyint_t *phyi;
16550 	phyint_t *phyi_tmp;
16551 	struct lifreq *lifr;
16552 	mblk_t	*mp1;
16553 	char *groupname;
16554 	ipsq_t *ipsq;
16555 	ip_stack_t	*ipst = ill->ill_ipst;
16556 
16557 	ASSERT(IAM_WRITER_IPIF(ipif));
16558 
16559 	/* Existance verified in ip_wput_nondata */
16560 	mp1 = mp->b_cont->b_cont;
16561 	lifr = (struct lifreq *)mp1->b_rptr;
16562 	groupname = lifr->lifr_groupname;
16563 
16564 	if (ipif->ipif_id != 0)
16565 		return (EINVAL);
16566 
16567 	phyi = ill->ill_phyint;
16568 	ASSERT(phyi != NULL);
16569 
16570 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16571 		return (EINVAL);
16572 
16573 	tmp = groupname;
16574 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16575 		;
16576 
16577 	if (i == LIFNAMSIZ) {
16578 		/* no null termination */
16579 		return (EINVAL);
16580 	}
16581 
16582 	/*
16583 	 * Calculate the namelen exclusive of the null
16584 	 * termination character.
16585 	 */
16586 	namelen = tmp - groupname;
16587 
16588 	ill_v4 = phyi->phyint_illv4;
16589 	ill_v6 = phyi->phyint_illv6;
16590 
16591 	/*
16592 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16593 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16594 	 * synchronization notes in ip.c
16595 	 */
16596 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16597 		return (EINVAL);
16598 	}
16599 
16600 	/*
16601 	 * mark the ill as changing.
16602 	 * this should queue all new requests on the syncq.
16603 	 */
16604 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16605 
16606 	if (ill_v4 != NULL)
16607 		ill_v4->ill_state_flags |= ILL_CHANGING;
16608 	if (ill_v6 != NULL)
16609 		ill_v6->ill_state_flags |= ILL_CHANGING;
16610 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16611 
16612 	if (namelen == 0) {
16613 		/*
16614 		 * Null string means remove this interface from the
16615 		 * existing group.
16616 		 */
16617 		if (phyi->phyint_groupname_len == 0) {
16618 			/*
16619 			 * Never was in a group.
16620 			 */
16621 			err = 0;
16622 			goto done;
16623 		}
16624 
16625 		/*
16626 		 * IPv4 or IPv6 may be temporarily out of the group when all
16627 		 * the ipifs are down. Thus, we need to check for ill_group to
16628 		 * be non-NULL.
16629 		 */
16630 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16631 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16632 			mutex_enter(&ill_v4->ill_lock);
16633 			if (!ill_is_quiescent(ill_v4)) {
16634 				/*
16635 				 * ipsq_pending_mp_add will not fail since
16636 				 * connp is NULL
16637 				 */
16638 				(void) ipsq_pending_mp_add(NULL,
16639 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16640 				mutex_exit(&ill_v4->ill_lock);
16641 				err = EINPROGRESS;
16642 				goto done;
16643 			}
16644 			mutex_exit(&ill_v4->ill_lock);
16645 		}
16646 
16647 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16648 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16649 			mutex_enter(&ill_v6->ill_lock);
16650 			if (!ill_is_quiescent(ill_v6)) {
16651 				(void) ipsq_pending_mp_add(NULL,
16652 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16653 				mutex_exit(&ill_v6->ill_lock);
16654 				err = EINPROGRESS;
16655 				goto done;
16656 			}
16657 			mutex_exit(&ill_v6->ill_lock);
16658 		}
16659 
16660 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16661 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16662 		mutex_enter(&phyi->phyint_lock);
16663 		ASSERT(phyi->phyint_groupname != NULL);
16664 		mi_free(phyi->phyint_groupname);
16665 		phyi->phyint_groupname = NULL;
16666 		phyi->phyint_groupname_len = 0;
16667 
16668 		/* Restore the ifindex used to be the per interface one */
16669 		phyi->phyint_group_ifindex = 0;
16670 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16671 		mutex_exit(&phyi->phyint_lock);
16672 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16673 		rw_exit(&ipst->ips_ill_g_lock);
16674 		err = ill_up_ipifs(ill, q, mp);
16675 
16676 		/*
16677 		 * set the split flag so that the ipsq can be split
16678 		 */
16679 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16680 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16681 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16682 
16683 	} else {
16684 		if (phyi->phyint_groupname_len != 0) {
16685 			ASSERT(phyi->phyint_groupname != NULL);
16686 			/* Are we inserting in the same group ? */
16687 			if (mi_strcmp(groupname,
16688 			    phyi->phyint_groupname) == 0) {
16689 				err = 0;
16690 				goto done;
16691 			}
16692 		}
16693 
16694 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16695 		/*
16696 		 * Merge ipsq for the group's.
16697 		 * This check is here as multiple groups/ills might be
16698 		 * sharing the same ipsq.
16699 		 * If we have to merege than the operation is restarted
16700 		 * on the new ipsq.
16701 		 */
16702 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16703 		if (phyi->phyint_ipsq != ipsq) {
16704 			rw_exit(&ipst->ips_ill_g_lock);
16705 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16706 			goto done;
16707 		}
16708 		/*
16709 		 * Running exclusive on new ipsq.
16710 		 */
16711 
16712 		ASSERT(ipsq != NULL);
16713 		ASSERT(ipsq->ipsq_writer == curthread);
16714 
16715 		/*
16716 		 * Check whether the ill_type and ill_net_type matches before
16717 		 * we allocate any memory so that the cleanup is easier.
16718 		 *
16719 		 * We can't group dissimilar ones as we can't load spread
16720 		 * packets across the group because of potential link-level
16721 		 * header differences.
16722 		 */
16723 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16724 		if (phyi_tmp != NULL) {
16725 			if ((ill_v4 != NULL &&
16726 			    phyi_tmp->phyint_illv4 != NULL) &&
16727 			    ((ill_v4->ill_net_type !=
16728 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16729 			    (ill_v4->ill_type !=
16730 			    phyi_tmp->phyint_illv4->ill_type))) {
16731 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16732 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16733 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16734 				rw_exit(&ipst->ips_ill_g_lock);
16735 				return (EINVAL);
16736 			}
16737 			if ((ill_v6 != NULL &&
16738 			    phyi_tmp->phyint_illv6 != NULL) &&
16739 			    ((ill_v6->ill_net_type !=
16740 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16741 			    (ill_v6->ill_type !=
16742 			    phyi_tmp->phyint_illv6->ill_type))) {
16743 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16744 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16745 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16746 				rw_exit(&ipst->ips_ill_g_lock);
16747 				return (EINVAL);
16748 			}
16749 		}
16750 
16751 		rw_exit(&ipst->ips_ill_g_lock);
16752 
16753 		/*
16754 		 * bring down all v4 ipifs.
16755 		 */
16756 		if (ill_v4 != NULL) {
16757 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16758 		}
16759 
16760 		/*
16761 		 * bring down all v6 ipifs.
16762 		 */
16763 		if (ill_v6 != NULL) {
16764 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16765 		}
16766 
16767 		/*
16768 		 * make sure all ipifs are down and there are no active
16769 		 * references. Call to ipsq_pending_mp_add will not fail
16770 		 * since connp is NULL.
16771 		 */
16772 		if (ill_v4 != NULL) {
16773 			mutex_enter(&ill_v4->ill_lock);
16774 			if (!ill_is_quiescent(ill_v4)) {
16775 				(void) ipsq_pending_mp_add(NULL,
16776 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16777 				mutex_exit(&ill_v4->ill_lock);
16778 				err = EINPROGRESS;
16779 				goto done;
16780 			}
16781 			mutex_exit(&ill_v4->ill_lock);
16782 		}
16783 
16784 		if (ill_v6 != NULL) {
16785 			mutex_enter(&ill_v6->ill_lock);
16786 			if (!ill_is_quiescent(ill_v6)) {
16787 				(void) ipsq_pending_mp_add(NULL,
16788 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16789 				mutex_exit(&ill_v6->ill_lock);
16790 				err = EINPROGRESS;
16791 				goto done;
16792 			}
16793 			mutex_exit(&ill_v6->ill_lock);
16794 		}
16795 
16796 		/*
16797 		 * allocate including space for null terminator
16798 		 * before we insert.
16799 		 */
16800 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16801 		if (tmp == NULL)
16802 			return (ENOMEM);
16803 
16804 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16805 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16806 		mutex_enter(&phyi->phyint_lock);
16807 		if (phyi->phyint_groupname_len != 0) {
16808 			ASSERT(phyi->phyint_groupname != NULL);
16809 			mi_free(phyi->phyint_groupname);
16810 		}
16811 
16812 		/*
16813 		 * setup the new group name.
16814 		 */
16815 		phyi->phyint_groupname = tmp;
16816 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16817 		phyi->phyint_groupname_len = namelen + 1;
16818 
16819 		if (ipst->ips_ipmp_hook_emulation) {
16820 			/*
16821 			 * If the group already exists we use the existing
16822 			 * group_ifindex, otherwise we pick a new index here.
16823 			 */
16824 			if (phyi_tmp != NULL) {
16825 				phyi->phyint_group_ifindex =
16826 				    phyi_tmp->phyint_group_ifindex;
16827 			} else {
16828 				/* XXX We need a recovery strategy here. */
16829 				if (!ip_assign_ifindex(
16830 				    &phyi->phyint_group_ifindex, ipst))
16831 					cmn_err(CE_PANIC,
16832 					    "ip_assign_ifindex() failed");
16833 			}
16834 		}
16835 		/*
16836 		 * Select whether the netinfo and hook use the per-interface
16837 		 * or per-group ifindex.
16838 		 */
16839 		if (ipst->ips_ipmp_hook_emulation)
16840 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16841 		else
16842 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16843 
16844 		if (ipst->ips_ipmp_hook_emulation &&
16845 		    phyi_tmp != NULL) {
16846 			/* First phyint in group - group PLUMB event */
16847 			ill_nic_info_plumb(ill, B_TRUE);
16848 		}
16849 		mutex_exit(&phyi->phyint_lock);
16850 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16851 		rw_exit(&ipst->ips_ill_g_lock);
16852 
16853 		err = ill_up_ipifs(ill, q, mp);
16854 	}
16855 
16856 done:
16857 	/*
16858 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16859 	 */
16860 	if (err != EINPROGRESS) {
16861 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16862 		if (ill_v4 != NULL)
16863 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16864 		if (ill_v6 != NULL)
16865 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16866 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16867 	}
16868 	return (err);
16869 }
16870 
16871 /* ARGSUSED */
16872 int
16873 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16874     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16875 {
16876 	ill_t *ill;
16877 	phyint_t *phyi;
16878 	struct lifreq *lifr;
16879 	mblk_t	*mp1;
16880 
16881 	/* Existence verified in ip_wput_nondata */
16882 	mp1 = mp->b_cont->b_cont;
16883 	lifr = (struct lifreq *)mp1->b_rptr;
16884 	ill = ipif->ipif_ill;
16885 	phyi = ill->ill_phyint;
16886 
16887 	lifr->lifr_groupname[0] = '\0';
16888 	/*
16889 	 * ill_group may be null if all the interfaces
16890 	 * are down. But still, the phyint should always
16891 	 * hold the name.
16892 	 */
16893 	if (phyi->phyint_groupname_len != 0) {
16894 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16895 		    phyi->phyint_groupname_len);
16896 	}
16897 
16898 	return (0);
16899 }
16900 
16901 
16902 typedef struct conn_move_s {
16903 	ill_t	*cm_from_ill;
16904 	ill_t	*cm_to_ill;
16905 	int	cm_ifindex;
16906 } conn_move_t;
16907 
16908 /*
16909  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16910  */
16911 static void
16912 conn_move(conn_t *connp, caddr_t arg)
16913 {
16914 	conn_move_t *connm;
16915 	int ifindex;
16916 	int i;
16917 	ill_t *from_ill;
16918 	ill_t *to_ill;
16919 	ilg_t *ilg;
16920 	ilm_t *ret_ilm;
16921 
16922 	connm = (conn_move_t *)arg;
16923 	ifindex = connm->cm_ifindex;
16924 	from_ill = connm->cm_from_ill;
16925 	to_ill = connm->cm_to_ill;
16926 
16927 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16928 
16929 	/* All multicast fields protected by conn_lock */
16930 	mutex_enter(&connp->conn_lock);
16931 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16932 	if ((connp->conn_outgoing_ill == from_ill) &&
16933 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16934 		connp->conn_outgoing_ill = to_ill;
16935 		connp->conn_incoming_ill = to_ill;
16936 	}
16937 
16938 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16939 
16940 	if ((connp->conn_multicast_ill == from_ill) &&
16941 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16942 		connp->conn_multicast_ill = connm->cm_to_ill;
16943 	}
16944 
16945 	/* Change IP_XMIT_IF associations */
16946 	if ((connp->conn_xmit_if_ill == from_ill) &&
16947 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
16948 		connp->conn_xmit_if_ill = to_ill;
16949 	}
16950 	/*
16951 	 * Change the ilg_ill to point to the new one. This assumes
16952 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16953 	 * has been told to receive packets on this interface.
16954 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16955 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16956 	 * some ilms may not have moved. We check to see whether
16957 	 * the ilms have moved to to_ill. We can't check on from_ill
16958 	 * as in the process of moving, we could have split an ilm
16959 	 * in to two - which has the same orig_ifindex and v6group.
16960 	 *
16961 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16962 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16963 	 */
16964 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16965 		ilg = &connp->conn_ilg[i];
16966 		if ((ilg->ilg_ill == from_ill) &&
16967 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16968 			/* ifindex != 0 indicates failback */
16969 			if (ifindex != 0) {
16970 				connp->conn_ilg[i].ilg_ill = to_ill;
16971 				continue;
16972 			}
16973 
16974 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16975 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16976 			    connp->conn_zoneid);
16977 
16978 			if (ret_ilm != NULL)
16979 				connp->conn_ilg[i].ilg_ill = to_ill;
16980 		}
16981 	}
16982 	mutex_exit(&connp->conn_lock);
16983 }
16984 
16985 static void
16986 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16987 {
16988 	conn_move_t connm;
16989 	ip_stack_t	*ipst = from_ill->ill_ipst;
16990 
16991 	connm.cm_from_ill = from_ill;
16992 	connm.cm_to_ill = to_ill;
16993 	connm.cm_ifindex = ifindex;
16994 
16995 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16996 }
16997 
16998 /*
16999  * ilm has been moved from from_ill to to_ill.
17000  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
17001  * appropriately.
17002  *
17003  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
17004  *	  the code there de-references ipif_ill to get the ill to
17005  *	  send multicast requests. It does not work as ipif is on its
17006  *	  move and already moved when this function is called.
17007  *	  Thus, we need to use from_ill and to_ill send down multicast
17008  *	  requests.
17009  */
17010 static void
17011 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
17012 {
17013 	ipif_t *ipif;
17014 	ilm_t *ilm;
17015 
17016 	/*
17017 	 * See whether we need to send down DL_ENABMULTI_REQ on
17018 	 * to_ill as ilm has just been added.
17019 	 */
17020 	ASSERT(IAM_WRITER_ILL(to_ill));
17021 	ASSERT(IAM_WRITER_ILL(from_ill));
17022 
17023 	ILM_WALKER_HOLD(to_ill);
17024 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
17025 
17026 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
17027 			continue;
17028 		/*
17029 		 * no locks held, ill/ipif cannot dissappear as long
17030 		 * as we are writer.
17031 		 */
17032 		ipif = to_ill->ill_ipif;
17033 		/*
17034 		 * No need to hold any lock as we are the writer and this
17035 		 * can only be changed by a writer.
17036 		 */
17037 		ilm->ilm_is_new = B_FALSE;
17038 
17039 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
17040 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
17041 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
17042 			    "resolver\n"));
17043 			continue;		/* Must be IRE_IF_NORESOLVER */
17044 		}
17045 
17046 
17047 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
17048 			ip1dbg(("ilm_send_multicast_reqs: "
17049 			    "to_ill MULTI_BCAST\n"));
17050 			goto from;
17051 		}
17052 
17053 		if (to_ill->ill_isv6)
17054 			mld_joingroup(ilm);
17055 		else
17056 			igmp_joingroup(ilm);
17057 
17058 		if (to_ill->ill_ipif_up_count == 0) {
17059 			/*
17060 			 * Nobody there. All multicast addresses will be
17061 			 * re-joined when we get the DL_BIND_ACK bringing the
17062 			 * interface up.
17063 			 */
17064 			ilm->ilm_notify_driver = B_FALSE;
17065 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
17066 			goto from;
17067 		}
17068 
17069 		/*
17070 		 * For allmulti address, we want to join on only one interface.
17071 		 * Checking for ilm_numentries_v6 is not correct as you may
17072 		 * find an ilm with zero address on to_ill, but we may not
17073 		 * have nominated to_ill for receiving. Thus, if we have
17074 		 * nominated from_ill (ill_join_allmulti is set), nominate
17075 		 * only if to_ill is not already nominated (to_ill normally
17076 		 * should not have been nominated if "from_ill" has already
17077 		 * been nominated. As we don't prevent failovers from happening
17078 		 * across groups, we don't assert).
17079 		 */
17080 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
17081 			/*
17082 			 * There is no need to hold ill locks as we are
17083 			 * writer on both ills and when ill_join_allmulti
17084 			 * is changed the thread is always a writer.
17085 			 */
17086 			if (from_ill->ill_join_allmulti &&
17087 			    !to_ill->ill_join_allmulti) {
17088 				(void) ip_join_allmulti(to_ill->ill_ipif);
17089 			}
17090 		} else if (ilm->ilm_notify_driver) {
17091 
17092 			/*
17093 			 * This is a newly moved ilm so we need to tell the
17094 			 * driver about the new group. There can be more than
17095 			 * one ilm's for the same group in the list each with a
17096 			 * different orig_ifindex. We have to inform the driver
17097 			 * once. In ilm_move_v[4,6] we only set the flag
17098 			 * ilm_notify_driver for the first ilm.
17099 			 */
17100 
17101 			(void) ip_ll_send_enabmulti_req(to_ill,
17102 			    &ilm->ilm_v6addr);
17103 		}
17104 
17105 		ilm->ilm_notify_driver = B_FALSE;
17106 
17107 		/*
17108 		 * See whether we need to send down DL_DISABMULTI_REQ on
17109 		 * from_ill as ilm has just been removed.
17110 		 */
17111 from:
17112 		ipif = from_ill->ill_ipif;
17113 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
17114 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
17115 			ip1dbg(("ilm_send_multicast_reqs: "
17116 			    "from_ill not resolver\n"));
17117 			continue;		/* Must be IRE_IF_NORESOLVER */
17118 		}
17119 
17120 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
17121 			ip1dbg(("ilm_send_multicast_reqs: "
17122 			    "from_ill MULTI_BCAST\n"));
17123 			continue;
17124 		}
17125 
17126 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
17127 			if (from_ill->ill_join_allmulti)
17128 			    (void) ip_leave_allmulti(from_ill->ill_ipif);
17129 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
17130 			(void) ip_ll_send_disabmulti_req(from_ill,
17131 		    &ilm->ilm_v6addr);
17132 		}
17133 	}
17134 	ILM_WALKER_RELE(to_ill);
17135 }
17136 
17137 /*
17138  * This function is called when all multicast memberships needs
17139  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
17140  * called only once unlike the IPv4 counterpart where it is called after
17141  * every logical interface is moved. The reason is due to multicast
17142  * memberships are joined using an interface address in IPv4 while in
17143  * IPv6, interface index is used.
17144  */
17145 static void
17146 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
17147 {
17148 	ilm_t	*ilm;
17149 	ilm_t	*ilm_next;
17150 	ilm_t	*new_ilm;
17151 	ilm_t	**ilmp;
17152 	int	count;
17153 	char buf[INET6_ADDRSTRLEN];
17154 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
17155 	ip_stack_t	*ipst = from_ill->ill_ipst;
17156 
17157 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17158 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17159 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17160 
17161 	if (ifindex == 0) {
17162 		/*
17163 		 * Form the solicited node mcast address which is used later.
17164 		 */
17165 		ipif_t *ipif;
17166 
17167 		ipif = from_ill->ill_ipif;
17168 		ASSERT(ipif->ipif_id == 0);
17169 
17170 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
17171 	}
17172 
17173 	ilmp = &from_ill->ill_ilm;
17174 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17175 		ilm_next = ilm->ilm_next;
17176 
17177 		if (ilm->ilm_flags & ILM_DELETED) {
17178 			ilmp = &ilm->ilm_next;
17179 			continue;
17180 		}
17181 
17182 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
17183 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
17184 		ASSERT(ilm->ilm_orig_ifindex != 0);
17185 		if (ilm->ilm_orig_ifindex == ifindex) {
17186 			/*
17187 			 * We are failing back multicast memberships.
17188 			 * If the same ilm exists in to_ill, it means somebody
17189 			 * has joined the same group there e.g. ff02::1
17190 			 * is joined within the kernel when the interfaces
17191 			 * came UP.
17192 			 */
17193 			ASSERT(ilm->ilm_ipif == NULL);
17194 			if (new_ilm != NULL) {
17195 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17196 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17197 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17198 					new_ilm->ilm_is_new = B_TRUE;
17199 				}
17200 			} else {
17201 				/*
17202 				 * check if we can just move the ilm
17203 				 */
17204 				if (from_ill->ill_ilm_walker_cnt != 0) {
17205 					/*
17206 					 * We have walkers we cannot move
17207 					 * the ilm, so allocate a new ilm,
17208 					 * this (old) ilm will be marked
17209 					 * ILM_DELETED at the end of the loop
17210 					 * and will be freed when the
17211 					 * last walker exits.
17212 					 */
17213 					new_ilm = (ilm_t *)mi_zalloc
17214 					    (sizeof (ilm_t));
17215 					if (new_ilm == NULL) {
17216 						ip0dbg(("ilm_move_v6: "
17217 						    "FAILBACK of IPv6"
17218 						    " multicast address %s : "
17219 						    "from %s to"
17220 						    " %s failed : ENOMEM \n",
17221 						    inet_ntop(AF_INET6,
17222 						    &ilm->ilm_v6addr, buf,
17223 						    sizeof (buf)),
17224 						    from_ill->ill_name,
17225 						    to_ill->ill_name));
17226 
17227 							ilmp = &ilm->ilm_next;
17228 							continue;
17229 					}
17230 					*new_ilm = *ilm;
17231 					/*
17232 					 * we don't want new_ilm linked to
17233 					 * ilm's filter list.
17234 					 */
17235 					new_ilm->ilm_filter = NULL;
17236 				} else {
17237 					/*
17238 					 * No walkers we can move the ilm.
17239 					 * lets take it out of the list.
17240 					 */
17241 					*ilmp = ilm->ilm_next;
17242 					ilm->ilm_next = NULL;
17243 					new_ilm = ilm;
17244 				}
17245 
17246 				/*
17247 				 * if this is the first ilm for the group
17248 				 * set ilm_notify_driver so that we notify the
17249 				 * driver in ilm_send_multicast_reqs.
17250 				 */
17251 				if (ilm_lookup_ill_v6(to_ill,
17252 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17253 					new_ilm->ilm_notify_driver = B_TRUE;
17254 
17255 				new_ilm->ilm_ill = to_ill;
17256 				/* Add to the to_ill's list */
17257 				new_ilm->ilm_next = to_ill->ill_ilm;
17258 				to_ill->ill_ilm = new_ilm;
17259 				/*
17260 				 * set the flag so that mld_joingroup is
17261 				 * called in ilm_send_multicast_reqs().
17262 				 */
17263 				new_ilm->ilm_is_new = B_TRUE;
17264 			}
17265 			goto bottom;
17266 		} else if (ifindex != 0) {
17267 			/*
17268 			 * If this is FAILBACK (ifindex != 0) and the ifindex
17269 			 * has not matched above, look at the next ilm.
17270 			 */
17271 			ilmp = &ilm->ilm_next;
17272 			continue;
17273 		}
17274 		/*
17275 		 * If we are here, it means ifindex is 0. Failover
17276 		 * everything.
17277 		 *
17278 		 * We need to handle solicited node mcast address
17279 		 * and all_nodes mcast address differently as they
17280 		 * are joined witin the kenrel (ipif_multicast_up)
17281 		 * and potentially from the userland. We are called
17282 		 * after the ipifs of from_ill has been moved.
17283 		 * If we still find ilms on ill with solicited node
17284 		 * mcast address or all_nodes mcast address, it must
17285 		 * belong to the UP interface that has not moved e.g.
17286 		 * ipif_id 0 with the link local prefix does not move.
17287 		 * We join this on the new ill accounting for all the
17288 		 * userland memberships so that applications don't
17289 		 * see any failure.
17290 		 *
17291 		 * We need to make sure that we account only for the
17292 		 * solicited node and all node multicast addresses
17293 		 * that was brought UP on these. In the case of
17294 		 * a failover from A to B, we might have ilms belonging
17295 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
17296 		 * for the membership from the userland. If we are failing
17297 		 * over from B to C now, we will find the ones belonging
17298 		 * to A on B. These don't account for the ill_ipif_up_count.
17299 		 * They just move from B to C. The check below on
17300 		 * ilm_orig_ifindex ensures that.
17301 		 */
17302 		if ((ilm->ilm_orig_ifindex ==
17303 		    from_ill->ill_phyint->phyint_ifindex) &&
17304 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
17305 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
17306 		    &ilm->ilm_v6addr))) {
17307 			ASSERT(ilm->ilm_refcnt > 0);
17308 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
17309 			/*
17310 			 * For indentation reasons, we are not using a
17311 			 * "else" here.
17312 			 */
17313 			if (count == 0) {
17314 				ilmp = &ilm->ilm_next;
17315 				continue;
17316 			}
17317 			ilm->ilm_refcnt -= count;
17318 			if (new_ilm != NULL) {
17319 				/*
17320 				 * Can find one with the same
17321 				 * ilm_orig_ifindex, if we are failing
17322 				 * over to a STANDBY. This happens
17323 				 * when somebody wants to join a group
17324 				 * on a STANDBY interface and we
17325 				 * internally join on a different one.
17326 				 * If we had joined on from_ill then, a
17327 				 * failover now will find a new ilm
17328 				 * with this index.
17329 				 */
17330 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17331 				    " new ilm on %s, group address %s\n",
17332 				    to_ill->ill_name,
17333 				    inet_ntop(AF_INET6,
17334 				    &ilm->ilm_v6addr, buf,
17335 				    sizeof (buf))));
17336 				new_ilm->ilm_refcnt += count;
17337 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17338 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17339 					new_ilm->ilm_is_new = B_TRUE;
17340 				}
17341 			} else {
17342 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17343 				if (new_ilm == NULL) {
17344 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17345 					    " multicast address %s : from %s to"
17346 					    " %s failed : ENOMEM \n",
17347 					    inet_ntop(AF_INET6,
17348 					    &ilm->ilm_v6addr, buf,
17349 					    sizeof (buf)), from_ill->ill_name,
17350 					    to_ill->ill_name));
17351 					ilmp = &ilm->ilm_next;
17352 					continue;
17353 				}
17354 				*new_ilm = *ilm;
17355 				new_ilm->ilm_filter = NULL;
17356 				new_ilm->ilm_refcnt = count;
17357 				new_ilm->ilm_timer = INFINITY;
17358 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17359 				new_ilm->ilm_is_new = B_TRUE;
17360 				/*
17361 				 * If the to_ill has not joined this
17362 				 * group we need to tell the driver in
17363 				 * ill_send_multicast_reqs.
17364 				 */
17365 				if (ilm_lookup_ill_v6(to_ill,
17366 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17367 					new_ilm->ilm_notify_driver = B_TRUE;
17368 
17369 				new_ilm->ilm_ill = to_ill;
17370 				/* Add to the to_ill's list */
17371 				new_ilm->ilm_next = to_ill->ill_ilm;
17372 				to_ill->ill_ilm = new_ilm;
17373 				ASSERT(new_ilm->ilm_ipif == NULL);
17374 			}
17375 			if (ilm->ilm_refcnt == 0) {
17376 				goto bottom;
17377 			} else {
17378 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17379 				CLEAR_SLIST(new_ilm->ilm_filter);
17380 				ilmp = &ilm->ilm_next;
17381 			}
17382 			continue;
17383 		} else {
17384 			/*
17385 			 * ifindex = 0 means, move everything pointing at
17386 			 * from_ill. We are doing this becuase ill has
17387 			 * either FAILED or became INACTIVE.
17388 			 *
17389 			 * As we would like to move things later back to
17390 			 * from_ill, we want to retain the identity of this
17391 			 * ilm. Thus, we don't blindly increment the reference
17392 			 * count on the ilms matching the address alone. We
17393 			 * need to match on the ilm_orig_index also. new_ilm
17394 			 * was obtained by matching ilm_orig_index also.
17395 			 */
17396 			if (new_ilm != NULL) {
17397 				/*
17398 				 * This is possible only if a previous restore
17399 				 * was incomplete i.e restore to
17400 				 * ilm_orig_ifindex left some ilms because
17401 				 * of some failures. Thus when we are failing
17402 				 * again, we might find our old friends there.
17403 				 */
17404 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17405 				    " on %s, group address %s\n",
17406 				    to_ill->ill_name,
17407 				    inet_ntop(AF_INET6,
17408 				    &ilm->ilm_v6addr, buf,
17409 				    sizeof (buf))));
17410 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17411 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17412 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17413 					new_ilm->ilm_is_new = B_TRUE;
17414 				}
17415 			} else {
17416 				if (from_ill->ill_ilm_walker_cnt != 0) {
17417 					new_ilm = (ilm_t *)
17418 					    mi_zalloc(sizeof (ilm_t));
17419 					if (new_ilm == NULL) {
17420 						ip0dbg(("ilm_move_v6: "
17421 						    "FAILOVER of IPv6"
17422 						    " multicast address %s : "
17423 						    "from %s to"
17424 						    " %s failed : ENOMEM \n",
17425 						    inet_ntop(AF_INET6,
17426 						    &ilm->ilm_v6addr, buf,
17427 						    sizeof (buf)),
17428 						    from_ill->ill_name,
17429 						    to_ill->ill_name));
17430 
17431 							ilmp = &ilm->ilm_next;
17432 							continue;
17433 					}
17434 					*new_ilm = *ilm;
17435 					new_ilm->ilm_filter = NULL;
17436 				} else {
17437 					*ilmp = ilm->ilm_next;
17438 					new_ilm = ilm;
17439 				}
17440 				/*
17441 				 * If the to_ill has not joined this
17442 				 * group we need to tell the driver in
17443 				 * ill_send_multicast_reqs.
17444 				 */
17445 				if (ilm_lookup_ill_v6(to_ill,
17446 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17447 					new_ilm->ilm_notify_driver = B_TRUE;
17448 
17449 				/* Add to the to_ill's list */
17450 				new_ilm->ilm_next = to_ill->ill_ilm;
17451 				to_ill->ill_ilm = new_ilm;
17452 				ASSERT(ilm->ilm_ipif == NULL);
17453 				new_ilm->ilm_ill = to_ill;
17454 				new_ilm->ilm_is_new = B_TRUE;
17455 			}
17456 
17457 		}
17458 
17459 bottom:
17460 		/*
17461 		 * Revert multicast filter state to (EXCLUDE, NULL).
17462 		 * new_ilm->ilm_is_new should already be set if needed.
17463 		 */
17464 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17465 		CLEAR_SLIST(new_ilm->ilm_filter);
17466 		/*
17467 		 * We allocated/got a new ilm, free the old one.
17468 		 */
17469 		if (new_ilm != ilm) {
17470 			if (from_ill->ill_ilm_walker_cnt == 0) {
17471 				*ilmp = ilm->ilm_next;
17472 				ilm->ilm_next = NULL;
17473 				FREE_SLIST(ilm->ilm_filter);
17474 				FREE_SLIST(ilm->ilm_pendsrcs);
17475 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17476 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17477 				mi_free((char *)ilm);
17478 			} else {
17479 				ilm->ilm_flags |= ILM_DELETED;
17480 				from_ill->ill_ilm_cleanup_reqd = 1;
17481 				ilmp = &ilm->ilm_next;
17482 			}
17483 		}
17484 	}
17485 }
17486 
17487 /*
17488  * Move all the multicast memberships to to_ill. Called when
17489  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17490  * different from IPv6 counterpart as multicast memberships are associated
17491  * with ills in IPv6. This function is called after every ipif is moved
17492  * unlike IPv6, where it is moved only once.
17493  */
17494 static void
17495 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17496 {
17497 	ilm_t	*ilm;
17498 	ilm_t	*ilm_next;
17499 	ilm_t	*new_ilm;
17500 	ilm_t	**ilmp;
17501 	ip_stack_t	*ipst = from_ill->ill_ipst;
17502 
17503 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17504 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17505 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17506 
17507 	ilmp = &from_ill->ill_ilm;
17508 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17509 		ilm_next = ilm->ilm_next;
17510 
17511 		if (ilm->ilm_flags & ILM_DELETED) {
17512 			ilmp = &ilm->ilm_next;
17513 			continue;
17514 		}
17515 
17516 		ASSERT(ilm->ilm_ipif != NULL);
17517 
17518 		if (ilm->ilm_ipif != ipif) {
17519 			ilmp = &ilm->ilm_next;
17520 			continue;
17521 		}
17522 
17523 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17524 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17525 			/*
17526 			 * We joined this in ipif_multicast_up
17527 			 * and we never did an ipif_multicast_down
17528 			 * for IPv4. If nobody else from the userland
17529 			 * has reference, we free the ilm, and later
17530 			 * when this ipif comes up on the new ill,
17531 			 * we will join this again.
17532 			 */
17533 			if (--ilm->ilm_refcnt == 0)
17534 				goto delete_ilm;
17535 
17536 			new_ilm = ilm_lookup_ipif(ipif,
17537 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17538 			if (new_ilm != NULL) {
17539 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17540 				/*
17541 				 * We still need to deal with the from_ill.
17542 				 */
17543 				new_ilm->ilm_is_new = B_TRUE;
17544 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17545 				CLEAR_SLIST(new_ilm->ilm_filter);
17546 				goto delete_ilm;
17547 			}
17548 			/*
17549 			 * If we could not find one e.g. ipif is
17550 			 * still down on to_ill, we add this ilm
17551 			 * on ill_new to preserve the reference
17552 			 * count.
17553 			 */
17554 		}
17555 		/*
17556 		 * When ipifs move, ilms always move with it
17557 		 * to the NEW ill. Thus we should never be
17558 		 * able to find ilm till we really move it here.
17559 		 */
17560 		ASSERT(ilm_lookup_ipif(ipif,
17561 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17562 
17563 		if (from_ill->ill_ilm_walker_cnt != 0) {
17564 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17565 			if (new_ilm == NULL) {
17566 				char buf[INET6_ADDRSTRLEN];
17567 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17568 				    " multicast address %s : "
17569 				    "from %s to"
17570 				    " %s failed : ENOMEM \n",
17571 				    inet_ntop(AF_INET,
17572 				    &ilm->ilm_v6addr, buf,
17573 				    sizeof (buf)),
17574 				    from_ill->ill_name,
17575 				    to_ill->ill_name));
17576 
17577 				ilmp = &ilm->ilm_next;
17578 				continue;
17579 			}
17580 			*new_ilm = *ilm;
17581 			/* We don't want new_ilm linked to ilm's filter list */
17582 			new_ilm->ilm_filter = NULL;
17583 		} else {
17584 			/* Remove from the list */
17585 			*ilmp = ilm->ilm_next;
17586 			new_ilm = ilm;
17587 		}
17588 
17589 		/*
17590 		 * If we have never joined this group on the to_ill
17591 		 * make sure we tell the driver.
17592 		 */
17593 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17594 		    ALL_ZONES) == NULL)
17595 			new_ilm->ilm_notify_driver = B_TRUE;
17596 
17597 		/* Add to the to_ill's list */
17598 		new_ilm->ilm_next = to_ill->ill_ilm;
17599 		to_ill->ill_ilm = new_ilm;
17600 		new_ilm->ilm_is_new = B_TRUE;
17601 
17602 		/*
17603 		 * Revert multicast filter state to (EXCLUDE, NULL)
17604 		 */
17605 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17606 		CLEAR_SLIST(new_ilm->ilm_filter);
17607 
17608 		/*
17609 		 * Delete only if we have allocated a new ilm.
17610 		 */
17611 		if (new_ilm != ilm) {
17612 delete_ilm:
17613 			if (from_ill->ill_ilm_walker_cnt == 0) {
17614 				/* Remove from the list */
17615 				*ilmp = ilm->ilm_next;
17616 				ilm->ilm_next = NULL;
17617 				FREE_SLIST(ilm->ilm_filter);
17618 				FREE_SLIST(ilm->ilm_pendsrcs);
17619 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17620 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17621 				mi_free((char *)ilm);
17622 			} else {
17623 				ilm->ilm_flags |= ILM_DELETED;
17624 				from_ill->ill_ilm_cleanup_reqd = 1;
17625 				ilmp = &ilm->ilm_next;
17626 			}
17627 		}
17628 	}
17629 }
17630 
17631 static uint_t
17632 ipif_get_id(ill_t *ill, uint_t id)
17633 {
17634 	uint_t	unit;
17635 	ipif_t	*tipif;
17636 	boolean_t found = B_FALSE;
17637 	ip_stack_t	*ipst = ill->ill_ipst;
17638 
17639 	/*
17640 	 * During failback, we want to go back to the same id
17641 	 * instead of the smallest id so that the original
17642 	 * configuration is maintained. id is non-zero in that
17643 	 * case.
17644 	 */
17645 	if (id != 0) {
17646 		/*
17647 		 * While failing back, if we still have an ipif with
17648 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17649 		 * as soon as we return from this function. It was
17650 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17651 		 * we can choose the smallest id. Thus we return zero
17652 		 * in that case ignoring the hint.
17653 		 */
17654 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17655 			return (0);
17656 		for (tipif = ill->ill_ipif; tipif != NULL;
17657 		    tipif = tipif->ipif_next) {
17658 			if (tipif->ipif_id == id) {
17659 				found = B_TRUE;
17660 				break;
17661 			}
17662 		}
17663 		/*
17664 		 * If somebody already plumbed another logical
17665 		 * with the same id, we won't be able to find it.
17666 		 */
17667 		if (!found)
17668 			return (id);
17669 	}
17670 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17671 		found = B_FALSE;
17672 		for (tipif = ill->ill_ipif; tipif != NULL;
17673 		    tipif = tipif->ipif_next) {
17674 			if (tipif->ipif_id == unit) {
17675 				found = B_TRUE;
17676 				break;
17677 			}
17678 		}
17679 		if (!found)
17680 			break;
17681 	}
17682 	return (unit);
17683 }
17684 
17685 /* ARGSUSED */
17686 static int
17687 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17688     ipif_t **rep_ipif_ptr)
17689 {
17690 	ill_t	*from_ill;
17691 	ipif_t	*rep_ipif;
17692 	ipif_t	**ipifp;
17693 	uint_t	unit;
17694 	int err = 0;
17695 	ipif_t	*to_ipif;
17696 	struct iocblk	*iocp;
17697 	boolean_t failback_cmd;
17698 	boolean_t remove_ipif;
17699 	int	rc;
17700 	ip_stack_t	*ipst;
17701 
17702 	ASSERT(IAM_WRITER_ILL(to_ill));
17703 	ASSERT(IAM_WRITER_IPIF(ipif));
17704 
17705 	iocp = (struct iocblk *)mp->b_rptr;
17706 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17707 	remove_ipif = B_FALSE;
17708 
17709 	from_ill = ipif->ipif_ill;
17710 	ipst = from_ill->ill_ipst;
17711 
17712 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17713 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17714 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17715 
17716 	/*
17717 	 * Don't move LINK LOCAL addresses as they are tied to
17718 	 * physical interface.
17719 	 */
17720 	if (from_ill->ill_isv6 &&
17721 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17722 		ipif->ipif_was_up = B_FALSE;
17723 		IPIF_UNMARK_MOVING(ipif);
17724 		return (0);
17725 	}
17726 
17727 	/*
17728 	 * We set the ipif_id to maximum so that the search for
17729 	 * ipif_id will pick the lowest number i.e 0 in the
17730 	 * following 2 cases :
17731 	 *
17732 	 * 1) We have a replacement ipif at the head of to_ill.
17733 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17734 	 *    on to_ill and hence the MOVE might fail. We want to
17735 	 *    remove it only if we could move the ipif. Thus, by
17736 	 *    setting it to the MAX value, we make the search in
17737 	 *    ipif_get_id return the zeroth id.
17738 	 *
17739 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17740 	 *    we might just have a zero address plumbed on the ipif
17741 	 *    with zero id in the case of IPv4. We remove that while
17742 	 *    doing the failback. We want to remove it only if we
17743 	 *    could move the ipif. Thus, by setting it to the MAX
17744 	 *    value, we make the search in ipif_get_id return the
17745 	 *    zeroth id.
17746 	 *
17747 	 * Both (1) and (2) are done only when when we are moving
17748 	 * an ipif (either due to failover/failback) which originally
17749 	 * belonged to this interface i.e the ipif_orig_ifindex is
17750 	 * the same as to_ill's ifindex. This is needed so that
17751 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17752 	 * from B -> A (B is being removed from the group) and
17753 	 * FAILBACK from A -> B restores the original configuration.
17754 	 * Without the check for orig_ifindex, the second FAILOVER
17755 	 * could make the ipif belonging to B replace the A's zeroth
17756 	 * ipif and the subsequent failback re-creating the replacement
17757 	 * ipif again.
17758 	 *
17759 	 * NOTE : We created the replacement ipif when we did a
17760 	 * FAILOVER (See below). We could check for FAILBACK and
17761 	 * then look for replacement ipif to be removed. But we don't
17762 	 * want to do that because we wan't to allow the possibility
17763 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17764 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17765 	 * from B -> A.
17766 	 */
17767 	to_ipif = to_ill->ill_ipif;
17768 	if ((to_ill->ill_phyint->phyint_ifindex ==
17769 	    ipif->ipif_orig_ifindex) &&
17770 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17771 		ASSERT(to_ipif->ipif_id == 0);
17772 		remove_ipif = B_TRUE;
17773 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17774 	}
17775 	/*
17776 	 * Find the lowest logical unit number on the to_ill.
17777 	 * If we are failing back, try to get the original id
17778 	 * rather than the lowest one so that the original
17779 	 * configuration is maintained.
17780 	 *
17781 	 * XXX need a better scheme for this.
17782 	 */
17783 	if (failback_cmd) {
17784 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17785 	} else {
17786 		unit = ipif_get_id(to_ill, 0);
17787 	}
17788 
17789 	/* Reset back to zero in case we fail below */
17790 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17791 		to_ipif->ipif_id = 0;
17792 
17793 	if (unit == ipst->ips_ip_addrs_per_if) {
17794 		ipif->ipif_was_up = B_FALSE;
17795 		IPIF_UNMARK_MOVING(ipif);
17796 		return (EINVAL);
17797 	}
17798 
17799 	/*
17800 	 * ipif is ready to move from "from_ill" to "to_ill".
17801 	 *
17802 	 * 1) If we are moving ipif with id zero, create a
17803 	 *    replacement ipif for this ipif on from_ill. If this fails
17804 	 *    fail the MOVE operation.
17805 	 *
17806 	 * 2) Remove the replacement ipif on to_ill if any.
17807 	 *    We could remove the replacement ipif when we are moving
17808 	 *    the ipif with id zero. But what if somebody already
17809 	 *    unplumbed it ? Thus we always remove it if it is present.
17810 	 *    We want to do it only if we are sure we are going to
17811 	 *    move the ipif to to_ill which is why there are no
17812 	 *    returns due to error till ipif is linked to to_ill.
17813 	 *    Note that the first ipif that we failback will always
17814 	 *    be zero if it is present.
17815 	 */
17816 	if (ipif->ipif_id == 0) {
17817 		ipaddr_t inaddr_any = INADDR_ANY;
17818 
17819 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17820 		if (rep_ipif == NULL) {
17821 			ipif->ipif_was_up = B_FALSE;
17822 			IPIF_UNMARK_MOVING(ipif);
17823 			return (ENOMEM);
17824 		}
17825 		*rep_ipif = ipif_zero;
17826 		/*
17827 		 * Before we put the ipif on the list, store the addresses
17828 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17829 		 * assumes so. This logic is not any different from what
17830 		 * ipif_allocate does.
17831 		 */
17832 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17833 		    &rep_ipif->ipif_v6lcl_addr);
17834 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17835 		    &rep_ipif->ipif_v6src_addr);
17836 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17837 		    &rep_ipif->ipif_v6subnet);
17838 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17839 		    &rep_ipif->ipif_v6net_mask);
17840 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17841 		    &rep_ipif->ipif_v6brd_addr);
17842 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17843 		    &rep_ipif->ipif_v6pp_dst_addr);
17844 		/*
17845 		 * We mark IPIF_NOFAILOVER so that this can never
17846 		 * move.
17847 		 */
17848 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17849 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17850 		rep_ipif->ipif_replace_zero = B_TRUE;
17851 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17852 		    MUTEX_DEFAULT, NULL);
17853 		rep_ipif->ipif_id = 0;
17854 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17855 		rep_ipif->ipif_ill = from_ill;
17856 		rep_ipif->ipif_orig_ifindex =
17857 		    from_ill->ill_phyint->phyint_ifindex;
17858 		/* Insert at head */
17859 		rep_ipif->ipif_next = from_ill->ill_ipif;
17860 		from_ill->ill_ipif = rep_ipif;
17861 		/*
17862 		 * We don't really care to let apps know about
17863 		 * this interface.
17864 		 */
17865 	}
17866 
17867 	if (remove_ipif) {
17868 		/*
17869 		 * We set to a max value above for this case to get
17870 		 * id zero. ASSERT that we did get one.
17871 		 */
17872 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17873 		rep_ipif = to_ipif;
17874 		to_ill->ill_ipif = rep_ipif->ipif_next;
17875 		rep_ipif->ipif_next = NULL;
17876 		/*
17877 		 * If some apps scanned and find this interface,
17878 		 * it is time to let them know, so that they can
17879 		 * delete it.
17880 		 */
17881 
17882 		*rep_ipif_ptr = rep_ipif;
17883 	}
17884 
17885 	/* Get it out of the ILL interface list. */
17886 	ipifp = &ipif->ipif_ill->ill_ipif;
17887 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
17888 		if (*ipifp == ipif) {
17889 			*ipifp = ipif->ipif_next;
17890 			break;
17891 		}
17892 	}
17893 
17894 	/* Assign the new ill */
17895 	ipif->ipif_ill = to_ill;
17896 	ipif->ipif_id = unit;
17897 	/* id has already been checked */
17898 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17899 	ASSERT(rc == 0);
17900 	/* Let SCTP update its list */
17901 	sctp_move_ipif(ipif, from_ill, to_ill);
17902 	/*
17903 	 * Handle the failover and failback of ipif_t between
17904 	 * ill_t that have differing maximum mtu values.
17905 	 */
17906 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17907 		if (ipif->ipif_saved_mtu == 0) {
17908 			/*
17909 			 * As this ipif_t is moving to an ill_t
17910 			 * that has a lower ill_max_mtu, its
17911 			 * ipif_mtu needs to be saved so it can
17912 			 * be restored during failback or during
17913 			 * failover to an ill_t which has a
17914 			 * higher ill_max_mtu.
17915 			 */
17916 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17917 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17918 		} else {
17919 			/*
17920 			 * The ipif_t is, once again, moving to
17921 			 * an ill_t that has a lower maximum mtu
17922 			 * value.
17923 			 */
17924 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17925 		}
17926 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17927 	    ipif->ipif_saved_mtu != 0) {
17928 		/*
17929 		 * The mtu of this ipif_t had to be reduced
17930 		 * during an earlier failover; this is an
17931 		 * opportunity for it to be increased (either as
17932 		 * part of another failover or a failback).
17933 		 */
17934 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17935 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17936 			ipif->ipif_saved_mtu = 0;
17937 		} else {
17938 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17939 		}
17940 	}
17941 
17942 	/*
17943 	 * We preserve all the other fields of the ipif including
17944 	 * ipif_saved_ire_mp. The routes that are saved here will
17945 	 * be recreated on the new interface and back on the old
17946 	 * interface when we move back.
17947 	 */
17948 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17949 
17950 	return (err);
17951 }
17952 
17953 static int
17954 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17955     int ifindex, ipif_t **rep_ipif_ptr)
17956 {
17957 	ipif_t *mipif;
17958 	ipif_t *ipif_next;
17959 	int err;
17960 
17961 	/*
17962 	 * We don't really try to MOVE back things if some of the
17963 	 * operations fail. The daemon will take care of moving again
17964 	 * later on.
17965 	 */
17966 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17967 		ipif_next = mipif->ipif_next;
17968 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17969 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17970 
17971 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17972 
17973 			/*
17974 			 * When the MOVE fails, it is the job of the
17975 			 * application to take care of this properly
17976 			 * i.e try again if it is ENOMEM.
17977 			 */
17978 			if (mipif->ipif_ill != from_ill) {
17979 				/*
17980 				 * ipif has moved.
17981 				 *
17982 				 * Move the multicast memberships associated
17983 				 * with this ipif to the new ill. For IPv6, we
17984 				 * do it once after all the ipifs are moved
17985 				 * (in ill_move) as they are not associated
17986 				 * with ipifs.
17987 				 *
17988 				 * We need to move the ilms as the ipif has
17989 				 * already been moved to a new ill even
17990 				 * in the case of errors. Neither
17991 				 * ilm_free(ipif) will find the ilm
17992 				 * when somebody unplumbs this ipif nor
17993 				 * ilm_delete(ilm) will be able to find the
17994 				 * ilm, if we don't move now.
17995 				 */
17996 				if (!from_ill->ill_isv6)
17997 					ilm_move_v4(from_ill, to_ill, mipif);
17998 			}
17999 
18000 			if (err != 0)
18001 				return (err);
18002 		}
18003 	}
18004 	return (0);
18005 }
18006 
18007 static int
18008 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
18009 {
18010 	int ifindex;
18011 	int err;
18012 	struct iocblk	*iocp;
18013 	ipif_t	*ipif;
18014 	ipif_t *rep_ipif_ptr = NULL;
18015 	ipif_t	*from_ipif = NULL;
18016 	boolean_t check_rep_if = B_FALSE;
18017 	ip_stack_t	*ipst = from_ill->ill_ipst;
18018 
18019 	iocp = (struct iocblk *)mp->b_rptr;
18020 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
18021 		/*
18022 		 * Move everything pointing at from_ill to to_ill.
18023 		 * We acheive this by passing in 0 as ifindex.
18024 		 */
18025 		ifindex = 0;
18026 	} else {
18027 		/*
18028 		 * Move everything pointing at from_ill whose original
18029 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
18030 		 * We acheive this by passing in ifindex rather than 0.
18031 		 * Multicast vifs, ilgs move implicitly because ipifs move.
18032 		 */
18033 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
18034 		ifindex = to_ill->ill_phyint->phyint_ifindex;
18035 	}
18036 
18037 	/*
18038 	 * Determine if there is at least one ipif that would move from
18039 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
18040 	 * ipif (if it exists) on the to_ill would be consumed as a result of
18041 	 * the move, in which case we need to quiesce the replacement ipif also.
18042 	 */
18043 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
18044 	    from_ipif = from_ipif->ipif_next) {
18045 		if (((ifindex == 0) ||
18046 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
18047 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
18048 			check_rep_if = B_TRUE;
18049 			break;
18050 		}
18051 	}
18052 
18053 
18054 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
18055 
18056 	GRAB_ILL_LOCKS(from_ill, to_ill);
18057 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
18058 		(void) ipsq_pending_mp_add(NULL, ipif, q,
18059 		    mp, ILL_MOVE_OK);
18060 		RELEASE_ILL_LOCKS(from_ill, to_ill);
18061 		return (EINPROGRESS);
18062 	}
18063 
18064 	/* Check if the replacement ipif is quiescent to delete */
18065 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
18066 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
18067 		to_ill->ill_ipif->ipif_state_flags |=
18068 		    IPIF_MOVING | IPIF_CHANGING;
18069 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
18070 			(void) ipsq_pending_mp_add(NULL, ipif, q,
18071 			    mp, ILL_MOVE_OK);
18072 			RELEASE_ILL_LOCKS(from_ill, to_ill);
18073 			return (EINPROGRESS);
18074 		}
18075 	}
18076 	RELEASE_ILL_LOCKS(from_ill, to_ill);
18077 
18078 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
18079 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
18080 	GRAB_ILL_LOCKS(from_ill, to_ill);
18081 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
18082 
18083 	/* ilm_move is done inside ipif_move for IPv4 */
18084 	if (err == 0 && from_ill->ill_isv6)
18085 		ilm_move_v6(from_ill, to_ill, ifindex);
18086 
18087 	RELEASE_ILL_LOCKS(from_ill, to_ill);
18088 	rw_exit(&ipst->ips_ill_g_lock);
18089 
18090 	/*
18091 	 * send rts messages and multicast messages.
18092 	 */
18093 	if (rep_ipif_ptr != NULL) {
18094 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
18095 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
18096 			rep_ipif_ptr->ipif_recovery_id = 0;
18097 		}
18098 		ip_rts_ifmsg(rep_ipif_ptr);
18099 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
18100 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
18101 		mi_free(rep_ipif_ptr);
18102 	}
18103 
18104 	conn_move_ill(from_ill, to_ill, ifindex);
18105 
18106 	return (err);
18107 }
18108 
18109 /*
18110  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
18111  * Also checks for the validity of the arguments.
18112  * Note: We are already exclusive inside the from group.
18113  * It is upto the caller to release refcnt on the to_ill's.
18114  */
18115 static int
18116 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
18117     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
18118 {
18119 	int dst_index;
18120 	ipif_t *ipif_v4, *ipif_v6;
18121 	struct lifreq *lifr;
18122 	mblk_t *mp1;
18123 	boolean_t exists;
18124 	sin_t	*sin;
18125 	int	err = 0;
18126 	ip_stack_t	*ipst;
18127 
18128 	if (CONN_Q(q))
18129 		ipst = CONNQ_TO_IPST(q);
18130 	else
18131 		ipst = ILLQ_TO_IPST(q);
18132 
18133 
18134 	if ((mp1 = mp->b_cont) == NULL)
18135 		return (EPROTO);
18136 
18137 	if ((mp1 = mp1->b_cont) == NULL)
18138 		return (EPROTO);
18139 
18140 	lifr = (struct lifreq *)mp1->b_rptr;
18141 	sin = (sin_t *)&lifr->lifr_addr;
18142 
18143 	/*
18144 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
18145 	 * specific operations.
18146 	 */
18147 	if (sin->sin_family != AF_UNSPEC)
18148 		return (EINVAL);
18149 
18150 	/*
18151 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
18152 	 * NULLs for the last 4 args and we know the lookup won't fail
18153 	 * with EINPROGRESS.
18154 	 */
18155 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
18156 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
18157 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
18158 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
18159 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
18160 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
18161 
18162 	if (ipif_v4 == NULL && ipif_v6 == NULL)
18163 		return (ENXIO);
18164 
18165 	if (ipif_v4 != NULL) {
18166 		ASSERT(ipif_v4->ipif_refcnt != 0);
18167 		if (ipif_v4->ipif_id != 0) {
18168 			err = EINVAL;
18169 			goto done;
18170 		}
18171 
18172 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
18173 		*ill_from_v4 = ipif_v4->ipif_ill;
18174 	}
18175 
18176 	if (ipif_v6 != NULL) {
18177 		ASSERT(ipif_v6->ipif_refcnt != 0);
18178 		if (ipif_v6->ipif_id != 0) {
18179 			err = EINVAL;
18180 			goto done;
18181 		}
18182 
18183 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
18184 		*ill_from_v6 = ipif_v6->ipif_ill;
18185 	}
18186 
18187 	err = 0;
18188 	dst_index = lifr->lifr_movetoindex;
18189 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
18190 	    q, mp, ip_process_ioctl, &err, ipst);
18191 	if (err != 0) {
18192 		/*
18193 		 * There could be only v6.
18194 		 */
18195 		if (err != ENXIO)
18196 			goto done;
18197 		err = 0;
18198 	}
18199 
18200 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
18201 	    q, mp, ip_process_ioctl, &err, ipst);
18202 	if (err != 0) {
18203 		if (err != ENXIO)
18204 			goto done;
18205 		if (*ill_to_v4 == NULL) {
18206 			err = ENXIO;
18207 			goto done;
18208 		}
18209 		err = 0;
18210 	}
18211 
18212 	/*
18213 	 * If we have something to MOVE i.e "from" not NULL,
18214 	 * "to" should be non-NULL.
18215 	 */
18216 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
18217 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
18218 		err = EINVAL;
18219 	}
18220 
18221 done:
18222 	if (ipif_v4 != NULL)
18223 		ipif_refrele(ipif_v4);
18224 	if (ipif_v6 != NULL)
18225 		ipif_refrele(ipif_v6);
18226 	return (err);
18227 }
18228 
18229 /*
18230  * FAILOVER and FAILBACK are modelled as MOVE operations.
18231  *
18232  * We don't check whether the MOVE is within the same group or
18233  * not, because this ioctl can be used as a generic mechanism
18234  * to failover from interface A to B, though things will function
18235  * only if they are really part of the same group. Moreover,
18236  * all ipifs may be down and hence temporarily out of the group.
18237  *
18238  * ipif's that need to be moved are first brought down; V4 ipifs are brought
18239  * down first and then V6.  For each we wait for the ipif's to become quiescent.
18240  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
18241  * have been deleted and there are no active references. Once quiescent the
18242  * ipif's are moved and brought up on the new ill.
18243  *
18244  * Normally the source ill and destination ill belong to the same IPMP group
18245  * and hence the same ipsq_t. In the event they don't belong to the same
18246  * same group the two ipsq's are first merged into one ipsq - that of the
18247  * to_ill. The multicast memberships on the source and destination ill cannot
18248  * change during the move operation since multicast joins/leaves also have to
18249  * execute on the same ipsq and are hence serialized.
18250  */
18251 /* ARGSUSED */
18252 int
18253 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18254     ip_ioctl_cmd_t *ipip, void *ifreq)
18255 {
18256 	ill_t *ill_to_v4 = NULL;
18257 	ill_t *ill_to_v6 = NULL;
18258 	ill_t *ill_from_v4 = NULL;
18259 	ill_t *ill_from_v6 = NULL;
18260 	int err = 0;
18261 
18262 	/*
18263 	 * setup from and to ill's, we can get EINPROGRESS only for
18264 	 * to_ill's.
18265 	 */
18266 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
18267 	    &ill_to_v4, &ill_to_v6);
18268 
18269 	if (err != 0) {
18270 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
18271 		goto done;
18272 	}
18273 
18274 	/*
18275 	 * nothing to do.
18276 	 */
18277 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
18278 		goto done;
18279 	}
18280 
18281 	/*
18282 	 * nothing to do.
18283 	 */
18284 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
18285 		goto done;
18286 	}
18287 
18288 	/*
18289 	 * Mark the ill as changing.
18290 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
18291 	 * in ill_up_ipifs in case of error they are cleared below.
18292 	 */
18293 
18294 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18295 	if (ill_from_v4 != NULL)
18296 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
18297 	if (ill_from_v6 != NULL)
18298 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
18299 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18300 
18301 	/*
18302 	 * Make sure that both src and dst are
18303 	 * in the same syncq group. If not make it happen.
18304 	 * We are not holding any locks because we are the writer
18305 	 * on the from_ipsq and we will hold locks in ill_merge_groups
18306 	 * to protect to_ipsq against changing.
18307 	 */
18308 	if (ill_from_v4 != NULL) {
18309 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
18310 		    ill_to_v4->ill_phyint->phyint_ipsq) {
18311 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
18312 			    NULL, mp, q);
18313 			goto err_ret;
18314 
18315 		}
18316 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18317 	} else {
18318 
18319 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18320 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18321 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18322 			    NULL, mp, q);
18323 			goto err_ret;
18324 
18325 		}
18326 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18327 	}
18328 
18329 	/*
18330 	 * Now that the ipsq's have been merged and we are the writer
18331 	 * lets mark to_ill as changing as well.
18332 	 */
18333 
18334 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18335 	if (ill_to_v4 != NULL)
18336 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18337 	if (ill_to_v6 != NULL)
18338 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18339 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18340 
18341 	/*
18342 	 * Its ok for us to proceed with the move even if
18343 	 * ill_pending_mp is non null on one of the from ill's as the reply
18344 	 * should not be looking at the ipif, it should only care about the
18345 	 * ill itself.
18346 	 */
18347 
18348 	/*
18349 	 * lets move ipv4 first.
18350 	 */
18351 	if (ill_from_v4 != NULL) {
18352 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18353 		ill_from_v4->ill_move_in_progress = B_TRUE;
18354 		ill_to_v4->ill_move_in_progress = B_TRUE;
18355 		ill_to_v4->ill_move_peer = ill_from_v4;
18356 		ill_from_v4->ill_move_peer = ill_to_v4;
18357 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18358 	}
18359 
18360 	/*
18361 	 * Now lets move ipv6.
18362 	 */
18363 	if (err == 0 && ill_from_v6 != NULL) {
18364 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18365 		ill_from_v6->ill_move_in_progress = B_TRUE;
18366 		ill_to_v6->ill_move_in_progress = B_TRUE;
18367 		ill_to_v6->ill_move_peer = ill_from_v6;
18368 		ill_from_v6->ill_move_peer = ill_to_v6;
18369 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18370 	}
18371 
18372 err_ret:
18373 	/*
18374 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18375 	 * moved to become quiescent.
18376 	 */
18377 	if (err == EINPROGRESS) {
18378 		goto done;
18379 	}
18380 
18381 	/*
18382 	 * if err is set ill_up_ipifs will not be called
18383 	 * lets clear the flags.
18384 	 */
18385 
18386 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18387 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18388 	/*
18389 	 * Some of the clearing may be redundant. But it is simple
18390 	 * not making any extra checks.
18391 	 */
18392 	if (ill_from_v6 != NULL) {
18393 		ill_from_v6->ill_move_in_progress = B_FALSE;
18394 		ill_from_v6->ill_move_peer = NULL;
18395 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18396 	}
18397 	if (ill_from_v4 != NULL) {
18398 		ill_from_v4->ill_move_in_progress = B_FALSE;
18399 		ill_from_v4->ill_move_peer = NULL;
18400 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18401 	}
18402 	if (ill_to_v6 != NULL) {
18403 		ill_to_v6->ill_move_in_progress = B_FALSE;
18404 		ill_to_v6->ill_move_peer = NULL;
18405 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18406 	}
18407 	if (ill_to_v4 != NULL) {
18408 		ill_to_v4->ill_move_in_progress = B_FALSE;
18409 		ill_to_v4->ill_move_peer = NULL;
18410 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18411 	}
18412 
18413 	/*
18414 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18415 	 * Do this always to maintain proper state i.e even in case of errors.
18416 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18417 	 * we need not call on both v4 and v6 interfaces.
18418 	 */
18419 	if (ill_from_v4 != NULL) {
18420 		if ((ill_from_v4->ill_phyint->phyint_flags &
18421 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18422 			phyint_inactive(ill_from_v4->ill_phyint);
18423 		}
18424 	} else if (ill_from_v6 != NULL) {
18425 		if ((ill_from_v6->ill_phyint->phyint_flags &
18426 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18427 			phyint_inactive(ill_from_v6->ill_phyint);
18428 		}
18429 	}
18430 
18431 	if (ill_to_v4 != NULL) {
18432 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18433 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18434 		}
18435 	} else if (ill_to_v6 != NULL) {
18436 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18437 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18438 		}
18439 	}
18440 
18441 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18442 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18443 
18444 no_err:
18445 	/*
18446 	 * lets bring the interfaces up on the to_ill.
18447 	 */
18448 	if (err == 0) {
18449 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18450 		    q, mp);
18451 	}
18452 
18453 	if (err == 0) {
18454 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18455 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18456 
18457 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18458 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18459 	}
18460 done:
18461 
18462 	if (ill_to_v4 != NULL) {
18463 		ill_refrele(ill_to_v4);
18464 	}
18465 	if (ill_to_v6 != NULL) {
18466 		ill_refrele(ill_to_v6);
18467 	}
18468 
18469 	return (err);
18470 }
18471 
18472 static void
18473 ill_dl_down(ill_t *ill)
18474 {
18475 	/*
18476 	 * The ill is down; unbind but stay attached since we're still
18477 	 * associated with a PPA. If we have negotiated DLPI capabilites
18478 	 * with the data link service provider (IDS_OK) then reset them.
18479 	 * The interval between unbinding and rebinding is potentially
18480 	 * unbounded hence we cannot assume things will be the same.
18481 	 * The DLPI capabilities will be probed again when the data link
18482 	 * is brought up.
18483 	 */
18484 	mblk_t	*mp = ill->ill_unbind_mp;
18485 	hook_nic_event_t *info;
18486 
18487 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18488 
18489 	ill->ill_unbind_mp = NULL;
18490 	if (mp != NULL) {
18491 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18492 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18493 		    ill->ill_name));
18494 		mutex_enter(&ill->ill_lock);
18495 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18496 		mutex_exit(&ill->ill_lock);
18497 		if (ill->ill_dlpi_capab_state == IDS_OK)
18498 			ill_capability_reset(ill);
18499 		ill_dlpi_send(ill, mp);
18500 	}
18501 
18502 	/*
18503 	 * Toss all of our multicast memberships.  We could keep them, but
18504 	 * then we'd have to do bookkeeping of any joins and leaves performed
18505 	 * by the application while the the interface is down (we can't just
18506 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18507 	 * on a downed interface).
18508 	 */
18509 	ill_leave_multicast(ill);
18510 
18511 	mutex_enter(&ill->ill_lock);
18512 
18513 	ill->ill_dl_up = 0;
18514 
18515 	if ((info = ill->ill_nic_event_info) != NULL) {
18516 		ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n",
18517 		    info->hne_event, ill->ill_name));
18518 		if (info->hne_data != NULL)
18519 			kmem_free(info->hne_data, info->hne_datalen);
18520 		kmem_free(info, sizeof (hook_nic_event_t));
18521 	}
18522 
18523 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
18524 	if (info != NULL) {
18525 		ip_stack_t	*ipst = ill->ill_ipst;
18526 
18527 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
18528 		info->hne_lif = 0;
18529 		info->hne_event = NE_DOWN;
18530 		info->hne_data = NULL;
18531 		info->hne_datalen = 0;
18532 		info->hne_family = ill->ill_isv6 ?
18533 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18534 	} else
18535 		ip2dbg(("ill_dl_down: could not attach DOWN nic event "
18536 		    "information for %s (ENOMEM)\n", ill->ill_name));
18537 
18538 	ill->ill_nic_event_info = info;
18539 
18540 	mutex_exit(&ill->ill_lock);
18541 }
18542 
18543 void
18544 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18545 {
18546 	union DL_primitives *dlp;
18547 	t_uscalar_t prim;
18548 
18549 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18550 
18551 	dlp = (union DL_primitives *)mp->b_rptr;
18552 	prim = dlp->dl_primitive;
18553 
18554 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18555 		dlpi_prim_str(prim), prim, ill->ill_name));
18556 
18557 	switch (prim) {
18558 	case DL_PHYS_ADDR_REQ:
18559 	{
18560 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18561 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18562 		break;
18563 	}
18564 	case DL_BIND_REQ:
18565 		mutex_enter(&ill->ill_lock);
18566 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18567 		mutex_exit(&ill->ill_lock);
18568 		break;
18569 	}
18570 
18571 	/*
18572 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18573 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18574 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18575 	 */
18576 	mutex_enter(&ill->ill_lock);
18577 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18578 	    (prim == DL_UNBIND_REQ)) {
18579 		ill->ill_dlpi_pending = prim;
18580 	}
18581 	mutex_exit(&ill->ill_lock);
18582 
18583 	/*
18584 	 * Some drivers send M_FLUSH up to IP as part of unbind
18585 	 * request.  When this M_FLUSH is sent back to the driver,
18586 	 * this can go after we send the detach request if the
18587 	 * M_FLUSH ends up in IP's syncq. To avoid that, we reply
18588 	 * to the M_FLUSH in ip_rput and locally generate another
18589 	 * M_FLUSH for the correctness.  This will get freed in
18590 	 * ip_wput_nondata.
18591 	 */
18592 	if (prim == DL_UNBIND_REQ)
18593 		(void) putnextctl1(ill->ill_rq, M_FLUSH, FLUSHRW);
18594 
18595 	putnext(ill->ill_wq, mp);
18596 }
18597 
18598 /*
18599  * Send a DLPI control message to the driver but make sure there
18600  * is only one outstanding message. Uses ill_dlpi_pending to tell
18601  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18602  * when an ACK or a NAK is received to process the next queued message.
18603  *
18604  * We don't protect ill_dlpi_pending with any lock. This is okay as
18605  * every place where its accessed, ip is exclusive while accessing
18606  * ill_dlpi_pending except when this function is called from ill_init()
18607  */
18608 void
18609 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18610 {
18611 	mblk_t **mpp;
18612 
18613 	ASSERT(IAM_WRITER_ILL(ill));
18614 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18615 
18616 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18617 		/* Must queue message. Tail insertion */
18618 		mpp = &ill->ill_dlpi_deferred;
18619 		while (*mpp != NULL)
18620 			mpp = &((*mpp)->b_next);
18621 
18622 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18623 		    ill->ill_name));
18624 
18625 		*mpp = mp;
18626 		return;
18627 	}
18628 
18629 	ill_dlpi_dispatch(ill, mp);
18630 }
18631 
18632 /*
18633  * Called when an DLPI control message has been acked or nacked to
18634  * send down the next queued message (if any).
18635  */
18636 void
18637 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18638 {
18639 	mblk_t *mp;
18640 
18641 	ASSERT(IAM_WRITER_ILL(ill));
18642 
18643 	ASSERT(prim != DL_PRIM_INVAL);
18644 	if (ill->ill_dlpi_pending != prim) {
18645 		if (ill->ill_dlpi_pending == DL_PRIM_INVAL) {
18646 			(void) mi_strlog(ill->ill_rq, 1,
18647 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
18648 			    "ill_dlpi_done: unsolicited ack for %s from %s\n",
18649 			    dlpi_prim_str(prim), ill->ill_name);
18650 		} else {
18651 			(void) mi_strlog(ill->ill_rq, 1,
18652 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
18653 			    "ill_dlpi_done: unexpected ack for %s from %s "
18654 			    "(expecting ack for %s)\n",
18655 			    dlpi_prim_str(prim), ill->ill_name,
18656 			    dlpi_prim_str(ill->ill_dlpi_pending));
18657 		}
18658 		return;
18659 	}
18660 
18661 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18662 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18663 
18664 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18665 		mutex_enter(&ill->ill_lock);
18666 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18667 		cv_signal(&ill->ill_cv);
18668 		mutex_exit(&ill->ill_lock);
18669 		return;
18670 	}
18671 
18672 	ill->ill_dlpi_deferred = mp->b_next;
18673 	mp->b_next = NULL;
18674 
18675 	ill_dlpi_dispatch(ill, mp);
18676 }
18677 
18678 void
18679 conn_delete_ire(conn_t *connp, caddr_t arg)
18680 {
18681 	ipif_t	*ipif = (ipif_t *)arg;
18682 	ire_t	*ire;
18683 
18684 	/*
18685 	 * Look at the cached ires on conns which has pointers to ipifs.
18686 	 * We just call ire_refrele which clears up the reference
18687 	 * to ire. Called when a conn closes. Also called from ipif_free
18688 	 * to cleanup indirect references to the stale ipif via the cached ire.
18689 	 */
18690 	mutex_enter(&connp->conn_lock);
18691 	ire = connp->conn_ire_cache;
18692 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18693 		connp->conn_ire_cache = NULL;
18694 		mutex_exit(&connp->conn_lock);
18695 		IRE_REFRELE_NOTR(ire);
18696 		return;
18697 	}
18698 	mutex_exit(&connp->conn_lock);
18699 
18700 }
18701 
18702 /*
18703  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18704  * of IREs. Those IREs may have been previously cached in the conn structure.
18705  * This ipcl_walk() walker function releases all references to such IREs based
18706  * on the condemned flag.
18707  */
18708 /* ARGSUSED */
18709 void
18710 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18711 {
18712 	ire_t	*ire;
18713 
18714 	mutex_enter(&connp->conn_lock);
18715 	ire = connp->conn_ire_cache;
18716 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18717 		connp->conn_ire_cache = NULL;
18718 		mutex_exit(&connp->conn_lock);
18719 		IRE_REFRELE_NOTR(ire);
18720 		return;
18721 	}
18722 	mutex_exit(&connp->conn_lock);
18723 }
18724 
18725 /*
18726  * Take down a specific interface, but don't lose any information about it.
18727  * Also delete interface from its interface group (ifgrp).
18728  * (Always called as writer.)
18729  * This function goes through the down sequence even if the interface is
18730  * already down. There are 2 reasons.
18731  * a. Currently we permit interface routes that depend on down interfaces
18732  *    to be added. This behaviour itself is questionable. However it appears
18733  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18734  *    time. We go thru the cleanup in order to remove these routes.
18735  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18736  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18737  *    down, but we need to cleanup i.e. do ill_dl_down and
18738  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18739  *
18740  * IP-MT notes:
18741  *
18742  * Model of reference to interfaces.
18743  *
18744  * The following members in ipif_t track references to the ipif.
18745  *	int     ipif_refcnt;    Active reference count
18746  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18747  * The following members in ill_t track references to the ill.
18748  *	int             ill_refcnt;     active refcnt
18749  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18750  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18751  *
18752  * Reference to an ipif or ill can be obtained in any of the following ways.
18753  *
18754  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18755  * Pointers to ipif / ill from other data structures viz ire and conn.
18756  * Implicit reference to the ipif / ill by holding a reference to the ire.
18757  *
18758  * The ipif/ill lookup functions return a reference held ipif / ill.
18759  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18760  * This is a purely dynamic reference count associated with threads holding
18761  * references to the ipif / ill. Pointers from other structures do not
18762  * count towards this reference count.
18763  *
18764  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18765  * ipif/ill. This is incremented whenever a new ire is created referencing the
18766  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18767  * actually added to the ire hash table. The count is decremented in
18768  * ire_inactive where the ire is destroyed.
18769  *
18770  * nce's reference ill's thru nce_ill and the count of nce's associated with
18771  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18772  * ndp_add() where the nce is actually added to the table. Similarly it is
18773  * decremented in ndp_inactive where the nce 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 v6 memberships we established in ipif_multicast_up(); the
18860 	 * v4 ones are left alone (as is the ipif_multicast_up flag, so we
18861 	 * know not to rejoin when the interface is brought back up).
18862 	 */
18863 	if (ipif->ipif_isv6)
18864 		ipif_multicast_down(ipif);
18865 	/*
18866 	 * Remove from the mapping for __sin6_src_id. We insert only
18867 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18868 	 * stored as mapped addresses, we need to check for mapped
18869 	 * INADDR_ANY also.
18870 	 */
18871 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18872 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18873 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18874 		int err;
18875 
18876 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18877 		    ipif->ipif_zoneid, ipst);
18878 		if (err != 0) {
18879 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18880 		}
18881 	}
18882 
18883 	/*
18884 	 * Before we delete the ill from the group (if any), we need
18885 	 * to make sure that we delete all the routes dependent on
18886 	 * this and also any ipifs dependent on this ipif for
18887 	 * source address. We need to do before we delete from
18888 	 * the group because
18889 	 *
18890 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18891 	 *
18892 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18893 	 *    for re-doing source address selection. Note that
18894 	 *    ipif_select_source[_v6] called from
18895 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18896 	 *    because we have already marked down here i.e cleared
18897 	 *    IPIF_UP.
18898 	 */
18899 	if (ipif->ipif_isv6) {
18900 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18901 		    ipst);
18902 	} else {
18903 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18904 		    ipst);
18905 	}
18906 
18907 	/*
18908 	 * Need to add these also to be saved and restored when the
18909 	 * ipif is brought down and up
18910 	 */
18911 	mutex_enter(&ipst->ips_ire_mrtun_lock);
18912 	if (ipst->ips_ire_mrtun_count != 0) {
18913 		mutex_exit(&ipst->ips_ire_mrtun_lock);
18914 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
18915 		    (char *)ipif, NULL, ipst);
18916 	} else {
18917 		mutex_exit(&ipst->ips_ire_mrtun_lock);
18918 	}
18919 
18920 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
18921 	if (ipst->ips_ire_srcif_table_count > 0) {
18922 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
18923 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif,
18924 		    ipst);
18925 	} else {
18926 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
18927 	}
18928 
18929 	/*
18930 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18931 	 * ires have been deleted above. Otherwise a thread could end up
18932 	 * caching an ire in a conn after we have finished the cleanup of the
18933 	 * conn. The caching is done after making sure that the ire is not yet
18934 	 * condemned. Also documented in the block comment above ip_output
18935 	 */
18936 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18937 	/* Also, delete the ires cached in SCTP */
18938 	sctp_ire_cache_flush(ipif);
18939 
18940 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
18941 	nattymod_clean_ipif(ipif);
18942 
18943 	/*
18944 	 * Update any other ipifs which have used "our" local address as
18945 	 * a source address. This entails removing and recreating IRE_INTERFACE
18946 	 * entries for such ipifs.
18947 	 */
18948 	if (ipif->ipif_isv6)
18949 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18950 	else
18951 		ipif_update_other_ipifs(ipif, ill->ill_group);
18952 
18953 	if (ipif_was_up) {
18954 		/*
18955 		 * Check whether it is last ipif to leave this group.
18956 		 * If this is the last ipif to leave, we should remove
18957 		 * this ill from the group as ipif_select_source will not
18958 		 * be able to find any useful ipifs if this ill is selected
18959 		 * for load balancing.
18960 		 *
18961 		 * For nameless groups, we should call ifgrp_delete if this
18962 		 * belongs to some group. As this ipif is going down, we may
18963 		 * need to reconstruct groups.
18964 		 */
18965 		phyi = ill->ill_phyint;
18966 		/*
18967 		 * If the phyint_groupname_len is 0, it may or may not
18968 		 * be in the nameless group. If the phyint_groupname_len is
18969 		 * not 0, then this ill should be part of some group.
18970 		 * As we always insert this ill in the group if
18971 		 * phyint_groupname_len is not zero when the first ipif
18972 		 * comes up (in ipif_up_done), it should be in a group
18973 		 * when the namelen is not 0.
18974 		 *
18975 		 * NOTE : When we delete the ill from the group,it will
18976 		 * blow away all the IRE_CACHES pointing either at this ipif or
18977 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18978 		 * should be pointing at this ill.
18979 		 */
18980 		ASSERT(phyi->phyint_groupname_len == 0 ||
18981 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18982 
18983 		if (phyi->phyint_groupname_len != 0) {
18984 			if (ill->ill_ipif_up_count == 0)
18985 				illgrp_delete(ill);
18986 		}
18987 
18988 		/*
18989 		 * If we have deleted some of the broadcast ires associated
18990 		 * with this ipif, we need to re-nominate somebody else if
18991 		 * the ires that we deleted were the nominated ones.
18992 		 */
18993 		if (ill->ill_group != NULL && !ill->ill_isv6)
18994 			ipif_renominate_bcast(ipif);
18995 	}
18996 
18997 	/*
18998 	 * neighbor-discovery or arp entries for this interface.
18999 	 */
19000 	ipif_ndp_down(ipif);
19001 
19002 	/*
19003 	 * If mp is NULL the caller will wait for the appropriate refcnt.
19004 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
19005 	 * and ill_delete -> ipif_free -> ipif_down
19006 	 */
19007 	if (mp == NULL) {
19008 		ASSERT(q == NULL);
19009 		return (0);
19010 	}
19011 
19012 	if (CONN_Q(q)) {
19013 		connp = Q_TO_CONN(q);
19014 		mutex_enter(&connp->conn_lock);
19015 	} else {
19016 		connp = NULL;
19017 	}
19018 	mutex_enter(&ill->ill_lock);
19019 	/*
19020 	 * Are there any ire's pointing to this ipif that are still active ?
19021 	 * If this is the last ipif going down, are there any ire's pointing
19022 	 * to this ill that are still active ?
19023 	 */
19024 	if (ipif_is_quiescent(ipif)) {
19025 		mutex_exit(&ill->ill_lock);
19026 		if (connp != NULL)
19027 			mutex_exit(&connp->conn_lock);
19028 		return (0);
19029 	}
19030 
19031 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
19032 	    ill->ill_name, (void *)ill));
19033 	/*
19034 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
19035 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
19036 	 * which in turn is called by the last refrele on the ipif/ill/ire.
19037 	 */
19038 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
19039 	if (!success) {
19040 		/* The conn is closing. So just return */
19041 		ASSERT(connp != NULL);
19042 		mutex_exit(&ill->ill_lock);
19043 		mutex_exit(&connp->conn_lock);
19044 		return (EINTR);
19045 	}
19046 
19047 	mutex_exit(&ill->ill_lock);
19048 	if (connp != NULL)
19049 		mutex_exit(&connp->conn_lock);
19050 	return (EINPROGRESS);
19051 }
19052 
19053 void
19054 ipif_down_tail(ipif_t *ipif)
19055 {
19056 	ill_t	*ill = ipif->ipif_ill;
19057 
19058 	/*
19059 	 * Skip any loopback interface (null wq).
19060 	 * If this is the last logical interface on the ill
19061 	 * have ill_dl_down tell the driver we are gone (unbind)
19062 	 * Note that lun 0 can ipif_down even though
19063 	 * there are other logical units that are up.
19064 	 * This occurs e.g. when we change a "significant" IFF_ flag.
19065 	 */
19066 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
19067 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
19068 	    ill->ill_dl_up) {
19069 		ill_dl_down(ill);
19070 	}
19071 	ill->ill_logical_down = 0;
19072 
19073 	/*
19074 	 * Have to be after removing the routes in ipif_down_delete_ire.
19075 	 */
19076 	if (ipif->ipif_isv6) {
19077 		if (ill->ill_flags & ILLF_XRESOLV)
19078 			ipif_arp_down(ipif);
19079 	} else {
19080 		ipif_arp_down(ipif);
19081 	}
19082 
19083 	ip_rts_ifmsg(ipif);
19084 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
19085 }
19086 
19087 /*
19088  * Bring interface logically down without bringing the physical interface
19089  * down e.g. when the netmask is changed. This avoids long lasting link
19090  * negotiations between an ethernet interface and a certain switches.
19091  */
19092 static int
19093 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
19094 {
19095 	/*
19096 	 * The ill_logical_down flag is a transient flag. It is set here
19097 	 * and is cleared once the down has completed in ipif_down_tail.
19098 	 * This flag does not indicate whether the ill stream is in the
19099 	 * DL_BOUND state with the driver. Instead this flag is used by
19100 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
19101 	 * the driver. The state of the ill stream i.e. whether it is
19102 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
19103 	 */
19104 	ipif->ipif_ill->ill_logical_down = 1;
19105 	return (ipif_down(ipif, q, mp));
19106 }
19107 
19108 /*
19109  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
19110  * If the usesrc client ILL is already part of a usesrc group or not,
19111  * in either case a ire_stq with the matching usesrc client ILL will
19112  * locate the IRE's that need to be deleted. We want IREs to be created
19113  * with the new source address.
19114  */
19115 static void
19116 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
19117 {
19118 	ill_t	*ucill = (ill_t *)ill_arg;
19119 
19120 	ASSERT(IAM_WRITER_ILL(ucill));
19121 
19122 	if (ire->ire_stq == NULL)
19123 		return;
19124 
19125 	if ((ire->ire_type == IRE_CACHE) &&
19126 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
19127 		ire_delete(ire);
19128 }
19129 
19130 /*
19131  * ire_walk routine to delete every IRE dependent on the interface
19132  * address that is going down.	(Always called as writer.)
19133  * Works for both v4 and v6.
19134  * In addition for checking for ire_ipif matches it also checks for
19135  * IRE_CACHE entries which have the same source address as the
19136  * disappearing ipif since ipif_select_source might have picked
19137  * that source. Note that ipif_down/ipif_update_other_ipifs takes
19138  * care of any IRE_INTERFACE with the disappearing source address.
19139  */
19140 static void
19141 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
19142 {
19143 	ipif_t	*ipif = (ipif_t *)ipif_arg;
19144 	ill_t *ire_ill;
19145 	ill_t *ipif_ill;
19146 
19147 	ASSERT(IAM_WRITER_IPIF(ipif));
19148 	if (ire->ire_ipif == NULL)
19149 		return;
19150 
19151 	/*
19152 	 * For IPv4, we derive source addresses for an IRE from ipif's
19153 	 * belonging to the same IPMP group as the IRE's outgoing
19154 	 * interface.  If an IRE's outgoing interface isn't in the
19155 	 * same IPMP group as a particular ipif, then that ipif
19156 	 * couldn't have been used as a source address for this IRE.
19157 	 *
19158 	 * For IPv6, source addresses are only restricted to the IPMP group
19159 	 * if the IRE is for a link-local address or a multicast address.
19160 	 * Otherwise, source addresses for an IRE can be chosen from
19161 	 * interfaces other than the the outgoing interface for that IRE.
19162 	 *
19163 	 * For source address selection details, see ipif_select_source()
19164 	 * and ipif_select_source_v6().
19165 	 */
19166 	if (ire->ire_ipversion == IPV4_VERSION ||
19167 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
19168 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
19169 		ire_ill = ire->ire_ipif->ipif_ill;
19170 		ipif_ill = ipif->ipif_ill;
19171 
19172 		if (ire_ill->ill_group != ipif_ill->ill_group) {
19173 			return;
19174 		}
19175 	}
19176 
19177 
19178 	if (ire->ire_ipif != ipif) {
19179 		/*
19180 		 * Look for a matching source address.
19181 		 */
19182 		if (ire->ire_type != IRE_CACHE)
19183 			return;
19184 		if (ipif->ipif_flags & IPIF_NOLOCAL)
19185 			return;
19186 
19187 		if (ire->ire_ipversion == IPV4_VERSION) {
19188 			if (ire->ire_src_addr != ipif->ipif_src_addr)
19189 				return;
19190 		} else {
19191 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
19192 			    &ipif->ipif_v6lcl_addr))
19193 				return;
19194 		}
19195 		ire_delete(ire);
19196 		return;
19197 	}
19198 	/*
19199 	 * ire_delete() will do an ire_flush_cache which will delete
19200 	 * all ire_ipif matches
19201 	 */
19202 	ire_delete(ire);
19203 }
19204 
19205 /*
19206  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
19207  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
19208  * 2) when an interface is brought up or down (on that ill).
19209  * This ensures that the IRE_CACHE entries don't retain stale source
19210  * address selection results.
19211  */
19212 void
19213 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
19214 {
19215 	ill_t	*ill = (ill_t *)ill_arg;
19216 	ill_t	*ipif_ill;
19217 
19218 	ASSERT(IAM_WRITER_ILL(ill));
19219 	/*
19220 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19221 	 * Hence this should be IRE_CACHE.
19222 	 */
19223 	ASSERT(ire->ire_type == IRE_CACHE);
19224 
19225 	/*
19226 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
19227 	 * We are only interested in IRE_CACHES that has borrowed
19228 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
19229 	 * for which we need to look at ire_ipif->ipif_ill match
19230 	 * with ill.
19231 	 */
19232 	ASSERT(ire->ire_ipif != NULL);
19233 	ipif_ill = ire->ire_ipif->ipif_ill;
19234 	if (ipif_ill == ill || (ill->ill_group != NULL &&
19235 	    ipif_ill->ill_group == ill->ill_group)) {
19236 		ire_delete(ire);
19237 	}
19238 }
19239 
19240 /*
19241  * Delete all the ire whose stq references ill_arg.
19242  */
19243 static void
19244 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
19245 {
19246 	ill_t	*ill = (ill_t *)ill_arg;
19247 	ill_t	*ire_ill;
19248 
19249 	ASSERT(IAM_WRITER_ILL(ill));
19250 	/*
19251 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19252 	 * Hence this should be IRE_CACHE.
19253 	 */
19254 	ASSERT(ire->ire_type == IRE_CACHE);
19255 
19256 	/*
19257 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19258 	 * matches ill. We are only interested in IRE_CACHES that
19259 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19260 	 * filtering here.
19261 	 */
19262 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19263 
19264 	if (ire_ill == ill)
19265 		ire_delete(ire);
19266 }
19267 
19268 /*
19269  * This is called when an ill leaves the group. We want to delete
19270  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19271  * pointing at ill.
19272  */
19273 static void
19274 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19275 {
19276 	ill_t	*ill = (ill_t *)ill_arg;
19277 
19278 	ASSERT(IAM_WRITER_ILL(ill));
19279 	ASSERT(ill->ill_group == NULL);
19280 	/*
19281 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19282 	 * Hence this should be IRE_CACHE.
19283 	 */
19284 	ASSERT(ire->ire_type == IRE_CACHE);
19285 	/*
19286 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19287 	 * matches ill. We are interested in both.
19288 	 */
19289 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19290 	    (ire->ire_ipif->ipif_ill == ill));
19291 
19292 	ire_delete(ire);
19293 }
19294 
19295 /*
19296  * Initiate deallocate of an IPIF. Always called as writer. Called by
19297  * ill_delete or ip_sioctl_removeif.
19298  */
19299 static void
19300 ipif_free(ipif_t *ipif)
19301 {
19302 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19303 
19304 	ASSERT(IAM_WRITER_IPIF(ipif));
19305 
19306 	if (ipif->ipif_recovery_id != 0)
19307 		(void) untimeout(ipif->ipif_recovery_id);
19308 	ipif->ipif_recovery_id = 0;
19309 
19310 	/* Remove conn references */
19311 	reset_conn_ipif(ipif);
19312 
19313 	/*
19314 	 * Make sure we have valid net and subnet broadcast ire's for the
19315 	 * other ipif's which share them with this ipif.
19316 	 */
19317 	if (!ipif->ipif_isv6)
19318 		ipif_check_bcast_ires(ipif);
19319 
19320 	/*
19321 	 * Take down the interface. We can be called either from ill_delete
19322 	 * or from ip_sioctl_removeif.
19323 	 */
19324 	(void) ipif_down(ipif, NULL, NULL);
19325 
19326 	/*
19327 	 * Now that the interface is down, there's no chance it can still
19328 	 * become a duplicate.  Cancel any timer that may have been set while
19329 	 * tearing down.
19330 	 */
19331 	if (ipif->ipif_recovery_id != 0)
19332 		(void) untimeout(ipif->ipif_recovery_id);
19333 	ipif->ipif_recovery_id = 0;
19334 
19335 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19336 	/* Remove pointers to this ill in the multicast routing tables */
19337 	reset_mrt_vif_ipif(ipif);
19338 	rw_exit(&ipst->ips_ill_g_lock);
19339 }
19340 
19341 /*
19342  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19343  * also ill_move().
19344  */
19345 static void
19346 ipif_free_tail(ipif_t *ipif)
19347 {
19348 	mblk_t	*mp;
19349 	ipif_t	**ipifp;
19350 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19351 
19352 	/*
19353 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19354 	 */
19355 	mutex_enter(&ipif->ipif_saved_ire_lock);
19356 	mp = ipif->ipif_saved_ire_mp;
19357 	ipif->ipif_saved_ire_mp = NULL;
19358 	mutex_exit(&ipif->ipif_saved_ire_lock);
19359 	freemsg(mp);
19360 
19361 	/*
19362 	 * Need to hold both ill_g_lock and ill_lock while
19363 	 * inserting or removing an ipif from the linked list
19364 	 * of ipifs hanging off the ill.
19365 	 */
19366 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19367 	/*
19368 	 * Remove all multicast memberships on the interface now.
19369 	 * This removes IPv4 multicast memberships joined within
19370 	 * the kernel as ipif_down does not do ipif_multicast_down
19371 	 * for IPv4. IPv6 is not handled here as the multicast memberships
19372 	 * are based on ill and not on ipif.
19373 	 */
19374 	ilm_free(ipif);
19375 
19376 	/*
19377 	 * Since we held the ill_g_lock while doing the ilm_free above,
19378 	 * we can assert the ilms were really deleted and not just marked
19379 	 * ILM_DELETED.
19380 	 */
19381 	ASSERT(ilm_walk_ipif(ipif) == 0);
19382 
19383 
19384 	IPIF_TRACE_CLEANUP(ipif);
19385 
19386 	/* Ask SCTP to take it out of it list */
19387 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19388 
19389 	mutex_enter(&ipif->ipif_ill->ill_lock);
19390 	/* Get it out of the ILL interface list. */
19391 	ipifp = &ipif->ipif_ill->ill_ipif;
19392 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
19393 		if (*ipifp == ipif) {
19394 			*ipifp = ipif->ipif_next;
19395 			break;
19396 		}
19397 	}
19398 
19399 	mutex_exit(&ipif->ipif_ill->ill_lock);
19400 	rw_exit(&ipst->ips_ill_g_lock);
19401 
19402 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19403 
19404 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19405 	ASSERT(ipif->ipif_recovery_id == 0);
19406 
19407 	/* Free the memory. */
19408 	mi_free((char *)ipif);
19409 }
19410 
19411 /*
19412  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
19413  * "ill_name" otherwise.
19414  */
19415 char *
19416 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19417 {
19418 	char	lbuf[32];
19419 	char	*name;
19420 	size_t	name_len;
19421 
19422 	buf[0] = '\0';
19423 	if (!ipif)
19424 		return (buf);
19425 	name = ipif->ipif_ill->ill_name;
19426 	name_len = ipif->ipif_ill->ill_name_length;
19427 	if (ipif->ipif_id != 0) {
19428 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19429 		    ipif->ipif_id);
19430 		name = lbuf;
19431 		name_len = mi_strlen(name) + 1;
19432 	}
19433 	len -= 1;
19434 	buf[len] = '\0';
19435 	len = MIN(len, name_len);
19436 	bcopy(name, buf, len);
19437 	return (buf);
19438 }
19439 
19440 /*
19441  * Find an IPIF based on the name passed in.  Names can be of the
19442  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19443  * The <phys> string can have forms like <dev><#> (e.g., le0),
19444  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19445  * When there is no colon, the implied unit id is zero. <phys> must
19446  * correspond to the name of an ILL.  (May be called as writer.)
19447  */
19448 static ipif_t *
19449 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19450     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19451     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19452 {
19453 	char	*cp;
19454 	char	*endp;
19455 	long	id;
19456 	ill_t	*ill;
19457 	ipif_t	*ipif;
19458 	uint_t	ire_type;
19459 	boolean_t did_alloc = B_FALSE;
19460 	ipsq_t	*ipsq;
19461 
19462 	if (error != NULL)
19463 		*error = 0;
19464 
19465 	/*
19466 	 * If the caller wants to us to create the ipif, make sure we have a
19467 	 * valid zoneid
19468 	 */
19469 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19470 
19471 	if (namelen == 0) {
19472 		if (error != NULL)
19473 			*error = ENXIO;
19474 		return (NULL);
19475 	}
19476 
19477 	*exists = B_FALSE;
19478 	/* Look for a colon in the name. */
19479 	endp = &name[namelen];
19480 	for (cp = endp; --cp > name; ) {
19481 		if (*cp == IPIF_SEPARATOR_CHAR)
19482 			break;
19483 	}
19484 
19485 	if (*cp == IPIF_SEPARATOR_CHAR) {
19486 		/*
19487 		 * Reject any non-decimal aliases for logical
19488 		 * interfaces. Aliases with leading zeroes
19489 		 * are also rejected as they introduce ambiguity
19490 		 * in the naming of the interfaces.
19491 		 * In order to confirm with existing semantics,
19492 		 * and to not break any programs/script relying
19493 		 * on that behaviour, if<0>:0 is considered to be
19494 		 * a valid interface.
19495 		 *
19496 		 * If alias has two or more digits and the first
19497 		 * is zero, fail.
19498 		 */
19499 		if (&cp[2] < endp && cp[1] == '0')
19500 			return (NULL);
19501 	}
19502 
19503 	if (cp <= name) {
19504 		cp = endp;
19505 	} else {
19506 		*cp = '\0';
19507 	}
19508 
19509 	/*
19510 	 * Look up the ILL, based on the portion of the name
19511 	 * before the slash. ill_lookup_on_name returns a held ill.
19512 	 * Temporary to check whether ill exists already. If so
19513 	 * ill_lookup_on_name will clear it.
19514 	 */
19515 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19516 	    q, mp, func, error, &did_alloc, ipst);
19517 	if (cp != endp)
19518 		*cp = IPIF_SEPARATOR_CHAR;
19519 	if (ill == NULL)
19520 		return (NULL);
19521 
19522 	/* Establish the unit number in the name. */
19523 	id = 0;
19524 	if (cp < endp && *endp == '\0') {
19525 		/* If there was a colon, the unit number follows. */
19526 		cp++;
19527 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19528 			ill_refrele(ill);
19529 			if (error != NULL)
19530 				*error = ENXIO;
19531 			return (NULL);
19532 		}
19533 	}
19534 
19535 	GRAB_CONN_LOCK(q);
19536 	mutex_enter(&ill->ill_lock);
19537 	/* Now see if there is an IPIF with this unit number. */
19538 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19539 		if (ipif->ipif_id == id) {
19540 			if (zoneid != ALL_ZONES &&
19541 			    zoneid != ipif->ipif_zoneid &&
19542 			    ipif->ipif_zoneid != ALL_ZONES) {
19543 				mutex_exit(&ill->ill_lock);
19544 				RELEASE_CONN_LOCK(q);
19545 				ill_refrele(ill);
19546 				if (error != NULL)
19547 					*error = ENXIO;
19548 				return (NULL);
19549 			}
19550 			/*
19551 			 * The block comment at the start of ipif_down
19552 			 * explains the use of the macros used below
19553 			 */
19554 			if (IPIF_CAN_LOOKUP(ipif)) {
19555 				ipif_refhold_locked(ipif);
19556 				mutex_exit(&ill->ill_lock);
19557 				if (!did_alloc)
19558 					*exists = B_TRUE;
19559 				/*
19560 				 * Drop locks before calling ill_refrele
19561 				 * since it can potentially call into
19562 				 * ipif_ill_refrele_tail which can end up
19563 				 * in trying to acquire any lock.
19564 				 */
19565 				RELEASE_CONN_LOCK(q);
19566 				ill_refrele(ill);
19567 				return (ipif);
19568 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19569 				ipsq = ill->ill_phyint->phyint_ipsq;
19570 				mutex_enter(&ipsq->ipsq_lock);
19571 				mutex_exit(&ill->ill_lock);
19572 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19573 				mutex_exit(&ipsq->ipsq_lock);
19574 				RELEASE_CONN_LOCK(q);
19575 				ill_refrele(ill);
19576 				*error = EINPROGRESS;
19577 				return (NULL);
19578 			}
19579 		}
19580 	}
19581 	RELEASE_CONN_LOCK(q);
19582 
19583 	if (!do_alloc) {
19584 		mutex_exit(&ill->ill_lock);
19585 		ill_refrele(ill);
19586 		if (error != NULL)
19587 			*error = ENXIO;
19588 		return (NULL);
19589 	}
19590 
19591 	/*
19592 	 * If none found, atomically allocate and return a new one.
19593 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19594 	 * to support "receive only" use of lo0:1 etc. as is still done
19595 	 * below as an initial guess.
19596 	 * However, this is now likely to be overriden later in ipif_up_done()
19597 	 * when we know for sure what address has been configured on the
19598 	 * interface, since we might have more than one loopback interface
19599 	 * with a loopback address, e.g. in the case of zones, and all the
19600 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19601 	 */
19602 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19603 		ire_type = IRE_LOOPBACK;
19604 	else
19605 		ire_type = IRE_LOCAL;
19606 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19607 	if (ipif != NULL)
19608 		ipif_refhold_locked(ipif);
19609 	else if (error != NULL)
19610 		*error = ENOMEM;
19611 	mutex_exit(&ill->ill_lock);
19612 	ill_refrele(ill);
19613 	return (ipif);
19614 }
19615 
19616 /*
19617  * This routine is called whenever a new address comes up on an ipif.  If
19618  * we are configured to respond to address mask requests, then we are supposed
19619  * to broadcast an address mask reply at this time.  This routine is also
19620  * called if we are already up, but a netmask change is made.  This is legal
19621  * but might not make the system manager very popular.	(May be called
19622  * as writer.)
19623  */
19624 void
19625 ipif_mask_reply(ipif_t *ipif)
19626 {
19627 	icmph_t	*icmph;
19628 	ipha_t	*ipha;
19629 	mblk_t	*mp;
19630 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19631 
19632 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19633 
19634 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19635 		return;
19636 
19637 	/* ICMP mask reply is IPv4 only */
19638 	ASSERT(!ipif->ipif_isv6);
19639 	/* ICMP mask reply is not for a loopback interface */
19640 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19641 
19642 	mp = allocb(REPLY_LEN, BPRI_HI);
19643 	if (mp == NULL)
19644 		return;
19645 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19646 
19647 	ipha = (ipha_t *)mp->b_rptr;
19648 	bzero(ipha, REPLY_LEN);
19649 	*ipha = icmp_ipha;
19650 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19651 	ipha->ipha_src = ipif->ipif_src_addr;
19652 	ipha->ipha_dst = ipif->ipif_brd_addr;
19653 	ipha->ipha_length = htons(REPLY_LEN);
19654 	ipha->ipha_ident = 0;
19655 
19656 	icmph = (icmph_t *)&ipha[1];
19657 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19658 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19659 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19660 
19661 	put(ipif->ipif_wq, mp);
19662 
19663 #undef	REPLY_LEN
19664 }
19665 
19666 /*
19667  * When the mtu in the ipif 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 static void
19672 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19673 {
19674 	ipif_t *ipif = (ipif_t *)ipif_arg;
19675 
19676 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19677 		return;
19678 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19679 }
19680 
19681 /*
19682  * When the mtu in the ill changes, we call this routine through ire_walk
19683  * to update all the relevant IREs.
19684  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19685  */
19686 void
19687 ill_mtu_change(ire_t *ire, char *ill_arg)
19688 {
19689 	ill_t	*ill = (ill_t *)ill_arg;
19690 
19691 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19692 		return;
19693 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19694 }
19695 
19696 /*
19697  * Join the ipif specific multicast groups.
19698  * Must be called after a mapping has been set up in the resolver.  (Always
19699  * called as writer.)
19700  */
19701 void
19702 ipif_multicast_up(ipif_t *ipif)
19703 {
19704 	int err, index;
19705 	ill_t *ill;
19706 
19707 	ASSERT(IAM_WRITER_IPIF(ipif));
19708 
19709 	ill = ipif->ipif_ill;
19710 	index = ill->ill_phyint->phyint_ifindex;
19711 
19712 	ip1dbg(("ipif_multicast_up\n"));
19713 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19714 		return;
19715 
19716 	if (ipif->ipif_isv6) {
19717 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19718 			return;
19719 
19720 		/* Join the all hosts multicast address */
19721 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19722 		/*
19723 		 * Passing B_TRUE means we have to join the multicast
19724 		 * membership on this interface even though this is
19725 		 * FAILED. If we join on a different one in the group,
19726 		 * we will not be able to delete the membership later
19727 		 * as we currently don't track where we join when we
19728 		 * join within the kernel unlike applications where
19729 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19730 		 * for more on this.
19731 		 */
19732 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19733 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19734 		if (err != 0) {
19735 			ip0dbg(("ipif_multicast_up: "
19736 			    "all_hosts_mcast failed %d\n",
19737 			    err));
19738 			return;
19739 		}
19740 		/*
19741 		 * Enable multicast for the solicited node multicast address
19742 		 */
19743 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19744 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19745 
19746 			ipv6_multi.s6_addr32[3] |=
19747 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19748 
19749 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19750 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19751 			    NULL);
19752 			if (err != 0) {
19753 				ip0dbg(("ipif_multicast_up: solicited MC"
19754 				    " failed %d\n", err));
19755 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19756 				    ill, ill->ill_phyint->phyint_ifindex,
19757 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19758 				return;
19759 			}
19760 		}
19761 	} else {
19762 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19763 			return;
19764 
19765 		/* Join the all hosts multicast address */
19766 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19767 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19768 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19769 		if (err) {
19770 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19771 			return;
19772 		}
19773 	}
19774 	ipif->ipif_multicast_up = 1;
19775 }
19776 
19777 /*
19778  * Blow away any IPv6 multicast groups that we joined in ipif_multicast_up();
19779  * any explicit memberships are blown away in ill_leave_multicast() when the
19780  * ill is brought down.
19781  */
19782 static void
19783 ipif_multicast_down(ipif_t *ipif)
19784 {
19785 	int err;
19786 
19787 	ASSERT(IAM_WRITER_IPIF(ipif));
19788 
19789 	ip1dbg(("ipif_multicast_down\n"));
19790 	if (!ipif->ipif_multicast_up)
19791 		return;
19792 
19793 	ASSERT(ipif->ipif_isv6);
19794 
19795 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19796 
19797 	/*
19798 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19799 	 * we should look for ilms on this ill rather than the ones that have
19800 	 * been failed over here.  They are here temporarily. As
19801 	 * ipif_multicast_up has joined on this ill, we should delete only
19802 	 * from this ill.
19803 	 */
19804 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19805 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19806 	    B_TRUE, B_TRUE);
19807 	if (err != 0) {
19808 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19809 		    err));
19810 	}
19811 	/*
19812 	 * Disable multicast for the solicited node multicast address
19813 	 */
19814 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19815 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19816 
19817 		ipv6_multi.s6_addr32[3] |=
19818 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19819 
19820 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19821 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19822 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19823 
19824 		if (err != 0) {
19825 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19826 			    err));
19827 		}
19828 	}
19829 
19830 	ipif->ipif_multicast_up = 0;
19831 }
19832 
19833 /*
19834  * Used when an interface comes up to recreate any extra routes on this
19835  * interface.
19836  */
19837 static ire_t **
19838 ipif_recover_ire(ipif_t *ipif)
19839 {
19840 	mblk_t	*mp;
19841 	ire_t	**ipif_saved_irep;
19842 	ire_t	**irep;
19843 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19844 
19845 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19846 	    ipif->ipif_id));
19847 
19848 	mutex_enter(&ipif->ipif_saved_ire_lock);
19849 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19850 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19851 	if (ipif_saved_irep == NULL) {
19852 		mutex_exit(&ipif->ipif_saved_ire_lock);
19853 		return (NULL);
19854 	}
19855 
19856 	irep = ipif_saved_irep;
19857 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19858 		ire_t		*ire;
19859 		queue_t		*rfq;
19860 		queue_t		*stq;
19861 		ifrt_t		*ifrt;
19862 		uchar_t		*src_addr;
19863 		uchar_t		*gateway_addr;
19864 		mblk_t		*resolver_mp;
19865 		ushort_t	type;
19866 
19867 		/*
19868 		 * When the ire was initially created and then added in
19869 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19870 		 * in the case of a traditional interface route, or as one of
19871 		 * the IRE_OFFSUBNET types (with the exception of
19872 		 * IRE_HOST types ire which is created by icmp_redirect() and
19873 		 * which we don't need to save or recover).  In the case where
19874 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19875 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19876 		 * to satisfy software like GateD and Sun Cluster which creates
19877 		 * routes using the the loopback interface's address as a
19878 		 * gateway.
19879 		 *
19880 		 * As ifrt->ifrt_type reflects the already updated ire_type and
19881 		 * since ire_create() expects that IRE_IF_NORESOLVER will have
19882 		 * a valid nce_res_mp field (which doesn't make sense for a
19883 		 * IRE_LOOPBACK), ire_create() will be called in the same way
19884 		 * here as in ip_rt_add(), namely using ipif->ipif_net_type when
19885 		 * the route looks like a traditional interface route (where
19886 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19887 		 * the saved ifrt->ifrt_type.  This means that in the case where
19888 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19889 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19890 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19891 		 */
19892 		ifrt = (ifrt_t *)mp->b_rptr;
19893 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19894 			rfq = NULL;
19895 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19896 			    ? ipif->ipif_rq : 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 = NULL;
19901 			resolver_mp = ipif->ipif_resolver_mp;
19902 			type = ipif->ipif_net_type;
19903 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19904 			/* Recover multiroute broadcast IRE. */
19905 			rfq = ipif->ipif_rq;
19906 			stq = ipif->ipif_wq;
19907 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19908 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19909 			    : (uint8_t *)&ipif->ipif_src_addr;
19910 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19911 			resolver_mp = ipif->ipif_bcast_mp;
19912 			type = ifrt->ifrt_type;
19913 		} else {
19914 			rfq = NULL;
19915 			stq = NULL;
19916 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19917 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19918 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19919 			resolver_mp = NULL;
19920 			type = ifrt->ifrt_type;
19921 		}
19922 
19923 		/*
19924 		 * Create a copy of the IRE with the saved address and netmask.
19925 		 */
19926 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19927 		    "0x%x/0x%x\n",
19928 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19929 		    ntohl(ifrt->ifrt_addr),
19930 		    ntohl(ifrt->ifrt_mask)));
19931 		ire = ire_create(
19932 		    (uint8_t *)&ifrt->ifrt_addr,
19933 		    (uint8_t *)&ifrt->ifrt_mask,
19934 		    src_addr,
19935 		    gateway_addr,
19936 		    NULL,
19937 		    &ifrt->ifrt_max_frag,
19938 		    NULL,
19939 		    rfq,
19940 		    stq,
19941 		    type,
19942 		    resolver_mp,
19943 		    ipif,
19944 		    NULL,
19945 		    0,
19946 		    0,
19947 		    0,
19948 		    ifrt->ifrt_flags,
19949 		    &ifrt->ifrt_iulp_info,
19950 		    NULL,
19951 		    NULL,
19952 		    ipst);
19953 
19954 		if (ire == NULL) {
19955 			mutex_exit(&ipif->ipif_saved_ire_lock);
19956 			kmem_free(ipif_saved_irep,
19957 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19958 			return (NULL);
19959 		}
19960 
19961 		/*
19962 		 * Some software (for example, GateD and Sun Cluster) attempts
19963 		 * to create (what amount to) IRE_PREFIX routes with the
19964 		 * loopback address as the gateway.  This is primarily done to
19965 		 * set up prefixes with the RTF_REJECT flag set (for example,
19966 		 * when generating aggregate routes.)
19967 		 *
19968 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19969 		 * IRE_LOOPBACK, then we map the request into a
19970 		 * IRE_IF_NORESOLVER.
19971 		 */
19972 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19973 			ire->ire_type = IRE_IF_NORESOLVER;
19974 		/*
19975 		 * ire held by ire_add, will be refreled' towards the
19976 		 * the end of ipif_up_done
19977 		 */
19978 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19979 		*irep = ire;
19980 		irep++;
19981 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19982 	}
19983 	mutex_exit(&ipif->ipif_saved_ire_lock);
19984 	return (ipif_saved_irep);
19985 }
19986 
19987 /*
19988  * Used to set the netmask and broadcast address to default values when the
19989  * interface is brought up.  (Always called as writer.)
19990  */
19991 static void
19992 ipif_set_default(ipif_t *ipif)
19993 {
19994 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19995 
19996 	if (!ipif->ipif_isv6) {
19997 		/*
19998 		 * Interface holds an IPv4 address. Default
19999 		 * mask is the natural netmask.
20000 		 */
20001 		if (!ipif->ipif_net_mask) {
20002 			ipaddr_t	v4mask;
20003 
20004 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
20005 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
20006 		}
20007 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20008 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20009 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
20010 		} else {
20011 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
20012 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
20013 		}
20014 		/*
20015 		 * NOTE: SunOS 4.X does this even if the broadcast address
20016 		 * has been already set thus we do the same here.
20017 		 */
20018 		if (ipif->ipif_flags & IPIF_BROADCAST) {
20019 			ipaddr_t	v4addr;
20020 
20021 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
20022 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
20023 		}
20024 	} else {
20025 		/*
20026 		 * Interface holds an IPv6-only address.  Default
20027 		 * mask is all-ones.
20028 		 */
20029 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
20030 			ipif->ipif_v6net_mask = ipv6_all_ones;
20031 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20032 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20033 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
20034 		} else {
20035 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
20036 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
20037 		}
20038 	}
20039 }
20040 
20041 /*
20042  * Return 0 if this address can be used as local address without causing
20043  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
20044  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
20045  * Special checks are needed to allow the same IPv6 link-local address
20046  * on different ills.
20047  * TODO: allowing the same site-local address on different ill's.
20048  */
20049 int
20050 ip_addr_availability_check(ipif_t *new_ipif)
20051 {
20052 	in6_addr_t our_v6addr;
20053 	ill_t *ill;
20054 	ipif_t *ipif;
20055 	ill_walk_context_t ctx;
20056 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
20057 
20058 	ASSERT(IAM_WRITER_IPIF(new_ipif));
20059 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
20060 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
20061 
20062 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
20063 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
20064 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
20065 		return (0);
20066 
20067 	our_v6addr = new_ipif->ipif_v6lcl_addr;
20068 
20069 	if (new_ipif->ipif_isv6)
20070 		ill = ILL_START_WALK_V6(&ctx, ipst);
20071 	else
20072 		ill = ILL_START_WALK_V4(&ctx, ipst);
20073 
20074 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
20075 		for (ipif = ill->ill_ipif; ipif != NULL;
20076 		    ipif = ipif->ipif_next) {
20077 			if ((ipif == new_ipif) ||
20078 			    !(ipif->ipif_flags & IPIF_UP) ||
20079 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
20080 				continue;
20081 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
20082 			    &our_v6addr)) {
20083 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
20084 				    new_ipif->ipif_flags |= IPIF_UNNUMBERED;
20085 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
20086 				    ipif->ipif_flags |= IPIF_UNNUMBERED;
20087 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
20088 				    new_ipif->ipif_ill != ill)
20089 					continue;
20090 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
20091 				    new_ipif->ipif_ill != ill)
20092 					continue;
20093 				else if (new_ipif->ipif_zoneid !=
20094 				    ipif->ipif_zoneid &&
20095 				    ipif->ipif_zoneid != ALL_ZONES &&
20096 				    (ill->ill_phyint->phyint_flags &
20097 				    PHYI_LOOPBACK))
20098 					continue;
20099 				else if (new_ipif->ipif_ill == ill)
20100 					return (EADDRINUSE);
20101 				else
20102 					return (EADDRNOTAVAIL);
20103 			}
20104 		}
20105 	}
20106 
20107 	return (0);
20108 }
20109 
20110 /*
20111  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
20112  * IREs for the ipif.
20113  * When the routine returns EINPROGRESS then mp has been consumed and
20114  * the ioctl will be acked from ip_rput_dlpi.
20115  */
20116 static int
20117 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
20118 {
20119 	ill_t	*ill = ipif->ipif_ill;
20120 	boolean_t isv6 = ipif->ipif_isv6;
20121 	int	err = 0;
20122 	boolean_t success;
20123 
20124 	ASSERT(IAM_WRITER_IPIF(ipif));
20125 
20126 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
20127 
20128 	/* Shouldn't get here if it is already up. */
20129 	if (ipif->ipif_flags & IPIF_UP)
20130 		return (EALREADY);
20131 
20132 	/* Skip arp/ndp for any loopback interface. */
20133 	if (ill->ill_wq != NULL) {
20134 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
20135 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
20136 
20137 		if (!ill->ill_dl_up) {
20138 			/*
20139 			 * ill_dl_up is not yet set. i.e. we are yet to
20140 			 * DL_BIND with the driver and this is the first
20141 			 * logical interface on the ill to become "up".
20142 			 * Tell the driver to get going (via DL_BIND_REQ).
20143 			 * Note that changing "significant" IFF_ flags
20144 			 * address/netmask etc cause a down/up dance, but
20145 			 * does not cause an unbind (DL_UNBIND) with the driver
20146 			 */
20147 			return (ill_dl_up(ill, ipif, mp, q));
20148 		}
20149 
20150 		/*
20151 		 * ipif_resolver_up may end up sending an
20152 		 * AR_INTERFACE_UP message to ARP, which would, in
20153 		 * turn send a DLPI message to the driver. ioctls are
20154 		 * serialized and so we cannot send more than one
20155 		 * interface up message at a time. If ipif_resolver_up
20156 		 * does send an interface up message to ARP, we get
20157 		 * EINPROGRESS and we will complete in ip_arp_done.
20158 		 */
20159 
20160 		ASSERT(connp != NULL || !CONN_Q(q));
20161 		ASSERT(ipsq->ipsq_pending_mp == NULL);
20162 		if (connp != NULL)
20163 			mutex_enter(&connp->conn_lock);
20164 		mutex_enter(&ill->ill_lock);
20165 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20166 		mutex_exit(&ill->ill_lock);
20167 		if (connp != NULL)
20168 			mutex_exit(&connp->conn_lock);
20169 		if (!success)
20170 			return (EINTR);
20171 
20172 		/*
20173 		 * Crank up IPv6 neighbor discovery
20174 		 * Unlike ARP, this should complete when
20175 		 * ipif_ndp_up returns. However, for
20176 		 * ILLF_XRESOLV interfaces we also send a
20177 		 * AR_INTERFACE_UP to the external resolver.
20178 		 * That ioctl will complete in ip_rput.
20179 		 */
20180 		if (isv6) {
20181 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr);
20182 			if (err != 0) {
20183 				if (err != EINPROGRESS)
20184 					mp = ipsq_pending_mp_get(ipsq, &connp);
20185 				return (err);
20186 			}
20187 		}
20188 		/* Now, ARP */
20189 		err = ipif_resolver_up(ipif, Res_act_initial);
20190 		if (err == EINPROGRESS) {
20191 			/* We will complete it in ip_arp_done */
20192 			return (err);
20193 		}
20194 		mp = ipsq_pending_mp_get(ipsq, &connp);
20195 		ASSERT(mp != NULL);
20196 		if (err != 0)
20197 			return (err);
20198 	} else {
20199 		/*
20200 		 * Interfaces without underlying hardware don't do duplicate
20201 		 * address detection.
20202 		 */
20203 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
20204 		ipif->ipif_addr_ready = 1;
20205 	}
20206 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
20207 }
20208 
20209 /*
20210  * Perform a bind for the physical device.
20211  * When the routine returns EINPROGRESS then mp has been consumed and
20212  * the ioctl will be acked from ip_rput_dlpi.
20213  * Allocate an unbind message and save it until ipif_down.
20214  */
20215 static int
20216 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
20217 {
20218 	mblk_t	*areq_mp = NULL;
20219 	mblk_t	*bind_mp = NULL;
20220 	mblk_t	*unbind_mp = NULL;
20221 	conn_t	*connp;
20222 	boolean_t success;
20223 
20224 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
20225 	ASSERT(IAM_WRITER_ILL(ill));
20226 
20227 	ASSERT(mp != NULL);
20228 
20229 	/* Create a resolver cookie for ARP */
20230 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
20231 		areq_t		*areq;
20232 		uint16_t	sap_addr;
20233 
20234 		areq_mp = ill_arp_alloc(ill,
20235 			(uchar_t *)&ip_areq_template, 0);
20236 		if (areq_mp == NULL) {
20237 			return (ENOMEM);
20238 		}
20239 		freemsg(ill->ill_resolver_mp);
20240 		ill->ill_resolver_mp = areq_mp;
20241 		areq = (areq_t *)areq_mp->b_rptr;
20242 		sap_addr = ill->ill_sap;
20243 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
20244 		/*
20245 		 * Wait till we call ill_pending_mp_add to determine
20246 		 * the success before we free the ill_resolver_mp and
20247 		 * attach areq_mp in it's place.
20248 		 */
20249 	}
20250 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
20251 	    DL_BIND_REQ);
20252 	if (bind_mp == NULL)
20253 		goto bad;
20254 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
20255 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
20256 
20257 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
20258 	if (unbind_mp == NULL)
20259 		goto bad;
20260 
20261 	/*
20262 	 * Record state needed to complete this operation when the
20263 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
20264 	 */
20265 	ASSERT(WR(q)->q_next == NULL);
20266 	connp = Q_TO_CONN(q);
20267 
20268 	mutex_enter(&connp->conn_lock);
20269 	mutex_enter(&ipif->ipif_ill->ill_lock);
20270 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20271 	mutex_exit(&ipif->ipif_ill->ill_lock);
20272 	mutex_exit(&connp->conn_lock);
20273 	if (!success)
20274 		goto bad;
20275 
20276 	/*
20277 	 * Save the unbind message for ill_dl_down(); it will be consumed when
20278 	 * the interface goes down.
20279 	 */
20280 	ASSERT(ill->ill_unbind_mp == NULL);
20281 	ill->ill_unbind_mp = unbind_mp;
20282 
20283 	ill_dlpi_send(ill, bind_mp);
20284 	/* Send down link-layer capabilities probe if not already done. */
20285 	ill_capability_probe(ill);
20286 
20287 	/*
20288 	 * Sysid used to rely on the fact that netboots set domainname
20289 	 * and the like. Now that miniroot boots aren't strictly netboots
20290 	 * and miniroot network configuration is driven from userland
20291 	 * these things still need to be set. This situation can be detected
20292 	 * by comparing the interface being configured here to the one
20293 	 * dhcack was set to reference by the boot loader. Once sysid is
20294 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20295 	 */
20296 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
20297 	    (strcmp(ill->ill_name, dhcack) == 0) &&
20298 	    (strlen(srpc_domain) == 0)) {
20299 		if (dhcpinit() != 0)
20300 			cmn_err(CE_WARN, "no cached dhcp response");
20301 	}
20302 
20303 	/*
20304 	 * This operation will complete in ip_rput_dlpi with either
20305 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20306 	 */
20307 	return (EINPROGRESS);
20308 bad:
20309 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20310 	/*
20311 	 * We don't have to check for possible removal from illgrp
20312 	 * as we have not yet inserted in illgrp. For groups
20313 	 * without names, this ipif is still not UP and hence
20314 	 * this could not have possibly had any influence in forming
20315 	 * groups.
20316 	 */
20317 
20318 	freemsg(bind_mp);
20319 	freemsg(unbind_mp);
20320 	return (ENOMEM);
20321 }
20322 
20323 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20324 
20325 /*
20326  * DLPI and ARP is up.
20327  * Create all the IREs associated with an interface bring up multicast.
20328  * Set the interface flag and finish other initialization
20329  * that potentially had to be differed to after DL_BIND_ACK.
20330  */
20331 int
20332 ipif_up_done(ipif_t *ipif)
20333 {
20334 	ire_t	*ire_array[20];
20335 	ire_t	**irep = ire_array;
20336 	ire_t	**irep1;
20337 	ipaddr_t net_mask = 0;
20338 	ipaddr_t subnet_mask, route_mask;
20339 	ill_t	*ill = ipif->ipif_ill;
20340 	queue_t	*stq;
20341 	ipif_t	 *src_ipif;
20342 	ipif_t   *tmp_ipif;
20343 	boolean_t	flush_ire_cache = B_TRUE;
20344 	int	err = 0;
20345 	phyint_t *phyi;
20346 	ire_t	**ipif_saved_irep = NULL;
20347 	int ipif_saved_ire_cnt;
20348 	int	cnt;
20349 	boolean_t	src_ipif_held = B_FALSE;
20350 	boolean_t	ire_added = B_FALSE;
20351 	boolean_t	loopback = B_FALSE;
20352 	ip_stack_t	*ipst = ill->ill_ipst;
20353 
20354 	ip1dbg(("ipif_up_done(%s:%u)\n",
20355 		ipif->ipif_ill->ill_name, ipif->ipif_id));
20356 	/* Check if this is a loopback interface */
20357 	if (ipif->ipif_ill->ill_wq == NULL)
20358 		loopback = B_TRUE;
20359 
20360 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20361 	/*
20362 	 * If all other interfaces for this ill are down or DEPRECATED,
20363 	 * or otherwise unsuitable for source address selection, remove
20364 	 * any IRE_CACHE entries for this ill to make sure source
20365 	 * address selection gets to take this new ipif into account.
20366 	 * No need to hold ill_lock while traversing the ipif list since
20367 	 * we are writer
20368 	 */
20369 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20370 		tmp_ipif = tmp_ipif->ipif_next) {
20371 		if (((tmp_ipif->ipif_flags &
20372 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20373 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20374 		    (tmp_ipif == ipif))
20375 			continue;
20376 		/* first useable pre-existing interface */
20377 		flush_ire_cache = B_FALSE;
20378 		break;
20379 	}
20380 	if (flush_ire_cache)
20381 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20382 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20383 
20384 	/*
20385 	 * Figure out which way the send-to queue should go.  Only
20386 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20387 	 * should show up here.
20388 	 */
20389 	switch (ill->ill_net_type) {
20390 	case IRE_IF_RESOLVER:
20391 		stq = ill->ill_rq;
20392 		break;
20393 	case IRE_IF_NORESOLVER:
20394 	case IRE_LOOPBACK:
20395 		stq = ill->ill_wq;
20396 		break;
20397 	default:
20398 		return (EINVAL);
20399 	}
20400 
20401 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
20402 		/*
20403 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20404 		 * ipif_lookup_on_name(), but in the case of zones we can have
20405 		 * several loopback addresses on lo0. So all the interfaces with
20406 		 * loopback addresses need to be marked IRE_LOOPBACK.
20407 		 */
20408 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20409 		    htonl(INADDR_LOOPBACK))
20410 			ipif->ipif_ire_type = IRE_LOOPBACK;
20411 		else
20412 			ipif->ipif_ire_type = IRE_LOCAL;
20413 	}
20414 
20415 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20416 		/*
20417 		 * Can't use our source address. Select a different
20418 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20419 		 */
20420 		src_ipif = ipif_select_source(ipif->ipif_ill,
20421 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20422 		if (src_ipif == NULL)
20423 			src_ipif = ipif;	/* Last resort */
20424 		else
20425 			src_ipif_held = B_TRUE;
20426 	} else {
20427 		src_ipif = ipif;
20428 	}
20429 
20430 	/* Create all the IREs associated with this interface */
20431 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20432 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20433 
20434 		/*
20435 		 * If we're on a labeled system then make sure that zone-
20436 		 * private addresses have proper remote host database entries.
20437 		 */
20438 		if (is_system_labeled() &&
20439 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20440 		    !tsol_check_interface_address(ipif))
20441 			return (EINVAL);
20442 
20443 		/* Register the source address for __sin6_src_id */
20444 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20445 		    ipif->ipif_zoneid, ipst);
20446 		if (err != 0) {
20447 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20448 			return (err);
20449 		}
20450 
20451 		/* If the interface address is set, create the local IRE. */
20452 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20453 			(void *)ipif,
20454 			ipif->ipif_ire_type,
20455 			ntohl(ipif->ipif_lcl_addr)));
20456 		*irep++ = ire_create(
20457 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20458 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20459 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20460 		    NULL,				/* no gateway */
20461 		    NULL,
20462 		    &ip_loopback_mtuplus,		/* max frag size */
20463 		    NULL,
20464 		    ipif->ipif_rq,			/* recv-from queue */
20465 		    NULL,				/* no send-to queue */
20466 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20467 		    NULL,
20468 		    ipif,
20469 		    NULL,
20470 		    0,
20471 		    0,
20472 		    0,
20473 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20474 		    RTF_PRIVATE : 0,
20475 		    &ire_uinfo_null,
20476 		    NULL,
20477 		    NULL,
20478 		    ipst);
20479 	} else {
20480 		ip1dbg((
20481 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20482 		    ipif->ipif_ire_type,
20483 		    ntohl(ipif->ipif_lcl_addr),
20484 		    (uint_t)ipif->ipif_flags));
20485 	}
20486 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20487 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20488 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20489 	} else {
20490 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20491 	}
20492 
20493 	subnet_mask = ipif->ipif_net_mask;
20494 
20495 	/*
20496 	 * If mask was not specified, use natural netmask of
20497 	 * interface address. Also, store this mask back into the
20498 	 * ipif struct.
20499 	 */
20500 	if (subnet_mask == 0) {
20501 		subnet_mask = net_mask;
20502 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20503 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20504 		    ipif->ipif_v6subnet);
20505 	}
20506 
20507 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20508 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20509 	    ipif->ipif_subnet != INADDR_ANY) {
20510 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20511 
20512 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20513 			route_mask = IP_HOST_MASK;
20514 		} else {
20515 			route_mask = subnet_mask;
20516 		}
20517 
20518 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20519 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20520 			(void *)ipif, (void *)ill,
20521 			ill->ill_net_type,
20522 			ntohl(ipif->ipif_subnet)));
20523 		*irep++ = ire_create(
20524 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20525 		    (uchar_t *)&route_mask,		/* mask */
20526 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20527 		    NULL,				/* no gateway */
20528 		    NULL,
20529 		    &ipif->ipif_mtu,			/* max frag */
20530 		    NULL,
20531 		    NULL,				/* no recv queue */
20532 		    stq,				/* send-to queue */
20533 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20534 		    ill->ill_resolver_mp,		/* xmit header */
20535 		    ipif,
20536 		    NULL,
20537 		    0,
20538 		    0,
20539 		    0,
20540 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20541 		    &ire_uinfo_null,
20542 		    NULL,
20543 		    NULL,
20544 		    ipst);
20545 	}
20546 
20547 	/*
20548 	 * If the interface address is set, create the broadcast IREs.
20549 	 *
20550 	 * ire_create_bcast checks if the proposed new IRE matches
20551 	 * any existing IRE's with the same physical interface (ILL).
20552 	 * This should get rid of duplicates.
20553 	 * ire_create_bcast also check IPIF_NOXMIT and does not create
20554 	 * any broadcast ires.
20555 	 */
20556 	if ((ipif->ipif_subnet != INADDR_ANY) &&
20557 	    (ipif->ipif_flags & IPIF_BROADCAST)) {
20558 		ipaddr_t addr;
20559 
20560 		ip1dbg(("ipif_up_done: creating broadcast IRE\n"));
20561 		irep = ire_check_and_create_bcast(ipif, 0, irep,
20562 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20563 		irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep,
20564 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20565 
20566 		/*
20567 		 * For backward compatibility, we need to create net
20568 		 * broadcast ire's based on the old "IP address class
20569 		 * system."  The reason is that some old machines only
20570 		 * respond to these class derived net broadcast.
20571 		 *
20572 		 * But we should not create these net broadcast ire's if
20573 		 * the subnet_mask is shorter than the IP address class based
20574 		 * derived netmask.  Otherwise, we may create a net
20575 		 * broadcast address which is the same as an IP address
20576 		 * on the subnet.  Then TCP will refuse to talk to that
20577 		 * address.
20578 		 *
20579 		 * Nor do we need IRE_BROADCAST ire's for the interface
20580 		 * with the netmask as 0xFFFFFFFF, as IRE_LOCAL for that
20581 		 * interface is already created.  Creating these broadcast
20582 		 * ire's will only create confusion as the "addr" is going
20583 		 * to be same as that of the IP address of the interface.
20584 		 */
20585 		if (net_mask < subnet_mask) {
20586 			addr = net_mask & ipif->ipif_subnet;
20587 			irep = ire_check_and_create_bcast(ipif, addr, irep,
20588 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20589 			irep = ire_check_and_create_bcast(ipif,
20590 			    ~net_mask | addr, irep,
20591 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20592 		}
20593 
20594 		if (subnet_mask != 0xFFFFFFFF) {
20595 			addr = ipif->ipif_subnet;
20596 			irep = ire_check_and_create_bcast(ipif, addr, irep,
20597 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20598 			irep = ire_check_and_create_bcast(ipif,
20599 			    ~subnet_mask|addr, irep,
20600 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20601 		}
20602 	}
20603 
20604 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20605 
20606 	/* If an earlier ire_create failed, get out now */
20607 	for (irep1 = irep; irep1 > ire_array; ) {
20608 		irep1--;
20609 		if (*irep1 == NULL) {
20610 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20611 			err = ENOMEM;
20612 			goto bad;
20613 		}
20614 	}
20615 
20616 	/*
20617 	 * Need to atomically check for ip_addr_availablity_check
20618 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20619 	 * from group also.The ill_g_lock is grabbed as reader
20620 	 * just to make sure no new ills or new ipifs are being added
20621 	 * to the system while we are checking the uniqueness of addresses.
20622 	 */
20623 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20624 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20625 	/* Mark it up, and increment counters. */
20626 	ipif->ipif_flags |= IPIF_UP;
20627 	ill->ill_ipif_up_count++;
20628 	err = ip_addr_availability_check(ipif);
20629 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20630 	rw_exit(&ipst->ips_ill_g_lock);
20631 
20632 	if (err != 0) {
20633 		/*
20634 		 * Our address may already be up on the same ill. In this case,
20635 		 * the ARP entry for our ipif replaced the one for the other
20636 		 * ipif. So we don't want to delete it (otherwise the other ipif
20637 		 * would be unable to send packets).
20638 		 * ip_addr_availability_check() identifies this case for us and
20639 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20640 		 * which is the expected error code.
20641 		 */
20642 		if (err == EADDRINUSE) {
20643 			freemsg(ipif->ipif_arp_del_mp);
20644 			ipif->ipif_arp_del_mp = NULL;
20645 			err = EADDRNOTAVAIL;
20646 		}
20647 		ill->ill_ipif_up_count--;
20648 		ipif->ipif_flags &= ~IPIF_UP;
20649 		goto bad;
20650 	}
20651 
20652 	/*
20653 	 * Add in all newly created IREs.  ire_create_bcast() has
20654 	 * already checked for duplicates of the IRE_BROADCAST type.
20655 	 * We want to add before we call ifgrp_insert which wants
20656 	 * to know whether IRE_IF_RESOLVER exists or not.
20657 	 *
20658 	 * NOTE : We refrele the ire though we may branch to "bad"
20659 	 *	  later on where we do ire_delete. This is okay
20660 	 *	  because nobody can delete it as we are running
20661 	 *	  exclusively.
20662 	 */
20663 	for (irep1 = irep; irep1 > ire_array; ) {
20664 		irep1--;
20665 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20666 		/*
20667 		 * refheld by ire_add. refele towards the end of the func
20668 		 */
20669 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20670 	}
20671 	ire_added = B_TRUE;
20672 	/*
20673 	 * Form groups if possible.
20674 	 *
20675 	 * If we are supposed to be in a ill_group with a name, insert it
20676 	 * now as we know that at least one ipif is UP. Otherwise form
20677 	 * nameless groups.
20678 	 *
20679 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20680 	 * this ipif into the appropriate interface group, or create a
20681 	 * new one. If this is already in a nameless group, we try to form
20682 	 * a bigger group looking at other ills potentially sharing this
20683 	 * ipif's prefix.
20684 	 */
20685 	phyi = ill->ill_phyint;
20686 	if (phyi->phyint_groupname_len != 0) {
20687 		ASSERT(phyi->phyint_groupname != NULL);
20688 		if (ill->ill_ipif_up_count == 1) {
20689 			ASSERT(ill->ill_group == NULL);
20690 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20691 			    phyi->phyint_groupname, NULL, B_TRUE);
20692 			if (err != 0) {
20693 				ip1dbg(("ipif_up_done: illgrp allocation "
20694 				    "failed, error %d\n", err));
20695 				goto bad;
20696 			}
20697 		}
20698 		ASSERT(ill->ill_group != NULL);
20699 	}
20700 
20701 	/*
20702 	 * When this is part of group, we need to make sure that
20703 	 * any broadcast ires created because of this ipif coming
20704 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20705 	 * so that we don't receive duplicate broadcast packets.
20706 	 */
20707 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20708 		ipif_renominate_bcast(ipif);
20709 
20710 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20711 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20712 	ipif_saved_irep = ipif_recover_ire(ipif);
20713 
20714 	if (!loopback) {
20715 		/*
20716 		 * If the broadcast address has been set, make sure it makes
20717 		 * sense based on the interface address.
20718 		 * Only match on ill since we are sharing broadcast addresses.
20719 		 */
20720 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20721 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20722 			ire_t	*ire;
20723 
20724 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20725 			    IRE_BROADCAST, ipif, ALL_ZONES,
20726 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20727 
20728 			if (ire == NULL) {
20729 				/*
20730 				 * If there isn't a matching broadcast IRE,
20731 				 * revert to the default for this netmask.
20732 				 */
20733 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20734 				mutex_enter(&ipif->ipif_ill->ill_lock);
20735 				ipif_set_default(ipif);
20736 				mutex_exit(&ipif->ipif_ill->ill_lock);
20737 			} else {
20738 				ire_refrele(ire);
20739 			}
20740 		}
20741 
20742 	}
20743 
20744 	/* This is the first interface on this ill */
20745 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20746 		/*
20747 		 * Need to recover all multicast memberships in the driver.
20748 		 * This had to be deferred until we had attached.
20749 		 */
20750 		ill_recover_multicast(ill);
20751 	}
20752 	/* Join the allhosts multicast address */
20753 	ipif_multicast_up(ipif);
20754 
20755 	if (!loopback) {
20756 		/*
20757 		 * See whether anybody else would benefit from the
20758 		 * new ipif that we added. We call this always rather
20759 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20760 		 * ipif is for the benefit of illgrp_insert (done above)
20761 		 * which does not do source address selection as it does
20762 		 * not want to re-create interface routes that we are
20763 		 * having reference to it here.
20764 		 */
20765 		ill_update_source_selection(ill);
20766 	}
20767 
20768 	for (irep1 = irep; irep1 > ire_array; ) {
20769 		irep1--;
20770 		if (*irep1 != NULL) {
20771 			/* was held in ire_add */
20772 			ire_refrele(*irep1);
20773 		}
20774 	}
20775 
20776 	cnt = ipif_saved_ire_cnt;
20777 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20778 		if (*irep1 != NULL) {
20779 			/* was held in ire_add */
20780 			ire_refrele(*irep1);
20781 		}
20782 	}
20783 
20784 	if (!loopback && ipif->ipif_addr_ready) {
20785 		/* Broadcast an address mask reply. */
20786 		ipif_mask_reply(ipif);
20787 	}
20788 	if (ipif_saved_irep != NULL) {
20789 		kmem_free(ipif_saved_irep,
20790 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20791 	}
20792 	if (src_ipif_held)
20793 		ipif_refrele(src_ipif);
20794 
20795 	/*
20796 	 * This had to be deferred until we had bound.  Tell routing sockets and
20797 	 * others that this interface is up if it looks like the address has
20798 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20799 	 * duplicate address detection to do its thing.
20800 	 */
20801 	if (ipif->ipif_addr_ready) {
20802 		ip_rts_ifmsg(ipif);
20803 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20804 		/* Let SCTP update the status for this ipif */
20805 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20806 	}
20807 	return (0);
20808 
20809 bad:
20810 	ip1dbg(("ipif_up_done: FAILED \n"));
20811 	/*
20812 	 * We don't have to bother removing from ill groups because
20813 	 *
20814 	 * 1) For groups with names, we insert only when the first ipif
20815 	 *    comes up. In that case if it fails, it will not be in any
20816 	 *    group. So, we need not try to remove for that case.
20817 	 *
20818 	 * 2) For groups without names, either we tried to insert ipif_ill
20819 	 *    in a group as singleton or found some other group to become
20820 	 *    a bigger group. For the former, if it fails we don't have
20821 	 *    anything to do as ipif_ill is not in the group and for the
20822 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20823 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20824 	 */
20825 	while (irep > ire_array) {
20826 		irep--;
20827 		if (*irep != NULL) {
20828 			ire_delete(*irep);
20829 			if (ire_added)
20830 				ire_refrele(*irep);
20831 		}
20832 	}
20833 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20834 
20835 	if (ipif_saved_irep != NULL) {
20836 		kmem_free(ipif_saved_irep,
20837 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20838 	}
20839 	if (src_ipif_held)
20840 		ipif_refrele(src_ipif);
20841 
20842 	ipif_arp_down(ipif);
20843 	return (err);
20844 }
20845 
20846 /*
20847  * Turn off the ARP with the ILLF_NOARP flag.
20848  */
20849 static int
20850 ill_arp_off(ill_t *ill)
20851 {
20852 	mblk_t	*arp_off_mp = NULL;
20853 	mblk_t	*arp_on_mp = NULL;
20854 
20855 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20856 
20857 	ASSERT(IAM_WRITER_ILL(ill));
20858 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20859 
20860 	/*
20861 	 * If the on message is still around we've already done
20862 	 * an arp_off without doing an arp_on thus there is no
20863 	 * work needed.
20864 	 */
20865 	if (ill->ill_arp_on_mp != NULL)
20866 		return (0);
20867 
20868 	/*
20869 	 * Allocate an ARP on message (to be saved) and an ARP off message
20870 	 */
20871 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20872 	if (!arp_off_mp)
20873 		return (ENOMEM);
20874 
20875 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20876 	if (!arp_on_mp)
20877 		goto failed;
20878 
20879 	ASSERT(ill->ill_arp_on_mp == NULL);
20880 	ill->ill_arp_on_mp = arp_on_mp;
20881 
20882 	/* Send an AR_INTERFACE_OFF request */
20883 	putnext(ill->ill_rq, arp_off_mp);
20884 	return (0);
20885 failed:
20886 
20887 	if (arp_off_mp)
20888 		freemsg(arp_off_mp);
20889 	return (ENOMEM);
20890 }
20891 
20892 /*
20893  * Turn on ARP by turning off the ILLF_NOARP flag.
20894  */
20895 static int
20896 ill_arp_on(ill_t *ill)
20897 {
20898 	mblk_t	*mp;
20899 
20900 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20901 
20902 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20903 
20904 	ASSERT(IAM_WRITER_ILL(ill));
20905 	/*
20906 	 * Send an AR_INTERFACE_ON request if we have already done
20907 	 * an arp_off (which allocated the message).
20908 	 */
20909 	if (ill->ill_arp_on_mp != NULL) {
20910 		mp = ill->ill_arp_on_mp;
20911 		ill->ill_arp_on_mp = NULL;
20912 		putnext(ill->ill_rq, mp);
20913 	}
20914 	return (0);
20915 }
20916 
20917 /*
20918  * Called after either deleting ill from the group or when setting
20919  * FAILED or STANDBY on the interface.
20920  */
20921 static void
20922 illgrp_reset_schednext(ill_t *ill)
20923 {
20924 	ill_group_t *illgrp;
20925 	ill_t *save_ill;
20926 
20927 	ASSERT(IAM_WRITER_ILL(ill));
20928 	/*
20929 	 * When called from illgrp_delete, ill_group will be non-NULL.
20930 	 * But when called from ip_sioctl_flags, it could be NULL if
20931 	 * somebody is setting FAILED/INACTIVE on some interface which
20932 	 * is not part of a group.
20933 	 */
20934 	illgrp = ill->ill_group;
20935 	if (illgrp == NULL)
20936 		return;
20937 	if (illgrp->illgrp_ill_schednext != ill)
20938 		return;
20939 
20940 	illgrp->illgrp_ill_schednext = NULL;
20941 	save_ill = ill;
20942 	/*
20943 	 * Choose a good ill to be the next one for
20944 	 * outbound traffic. As the flags FAILED/STANDBY is
20945 	 * not yet marked when called from ip_sioctl_flags,
20946 	 * we check for ill separately.
20947 	 */
20948 	for (ill = illgrp->illgrp_ill; ill != NULL;
20949 	    ill = ill->ill_group_next) {
20950 		if ((ill != save_ill) &&
20951 		    !(ill->ill_phyint->phyint_flags &
20952 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20953 			illgrp->illgrp_ill_schednext = ill;
20954 			return;
20955 		}
20956 	}
20957 }
20958 
20959 /*
20960  * Given an ill, find the next ill in the group to be scheduled.
20961  * (This should be called by ip_newroute() before ire_create().)
20962  * The passed in ill may be pulled out of the group, after we have picked
20963  * up a different outgoing ill from the same group. However ire add will
20964  * atomically check this.
20965  */
20966 ill_t *
20967 illgrp_scheduler(ill_t *ill)
20968 {
20969 	ill_t *retill;
20970 	ill_group_t *illgrp;
20971 	int illcnt;
20972 	int i;
20973 	uint64_t flags;
20974 	ip_stack_t	*ipst = ill->ill_ipst;
20975 
20976 	/*
20977 	 * We don't use a lock to check for the ill_group. If this ill
20978 	 * is currently being inserted we may end up just returning this
20979 	 * ill itself. That is ok.
20980 	 */
20981 	if (ill->ill_group == NULL) {
20982 		ill_refhold(ill);
20983 		return (ill);
20984 	}
20985 
20986 	/*
20987 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20988 	 * a set of stable ills. No ill can be added or deleted or change
20989 	 * group while we hold the reader lock.
20990 	 */
20991 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20992 	if ((illgrp = ill->ill_group) == NULL) {
20993 		rw_exit(&ipst->ips_ill_g_lock);
20994 		ill_refhold(ill);
20995 		return (ill);
20996 	}
20997 
20998 	illcnt = illgrp->illgrp_ill_count;
20999 	mutex_enter(&illgrp->illgrp_lock);
21000 	retill = illgrp->illgrp_ill_schednext;
21001 
21002 	if (retill == NULL)
21003 		retill = illgrp->illgrp_ill;
21004 
21005 	/*
21006 	 * We do a circular search beginning at illgrp_ill_schednext
21007 	 * or illgrp_ill. We don't check the flags against the ill lock
21008 	 * since it can change anytime. The ire creation will be atomic
21009 	 * and will fail if the ill is FAILED or OFFLINE.
21010 	 */
21011 	for (i = 0; i < illcnt; i++) {
21012 		flags = retill->ill_phyint->phyint_flags;
21013 
21014 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
21015 		    ILL_CAN_LOOKUP(retill)) {
21016 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
21017 			ill_refhold(retill);
21018 			break;
21019 		}
21020 		retill = retill->ill_group_next;
21021 		if (retill == NULL)
21022 			retill = illgrp->illgrp_ill;
21023 	}
21024 	mutex_exit(&illgrp->illgrp_lock);
21025 	rw_exit(&ipst->ips_ill_g_lock);
21026 
21027 	return (i == illcnt ? NULL : retill);
21028 }
21029 
21030 /*
21031  * Checks for availbility of a usable source address (if there is one) when the
21032  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
21033  * this selection is done regardless of the destination.
21034  */
21035 boolean_t
21036 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
21037 {
21038 	uint_t	ifindex;
21039 	ipif_t	*ipif = NULL;
21040 	ill_t	*uill;
21041 	boolean_t isv6;
21042 	ip_stack_t	*ipst = ill->ill_ipst;
21043 
21044 	ASSERT(ill != NULL);
21045 
21046 	isv6 = ill->ill_isv6;
21047 	ifindex = ill->ill_usesrc_ifindex;
21048 	if (ifindex != 0) {
21049 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
21050 		    NULL, ipst);
21051 		if (uill == NULL)
21052 			return (NULL);
21053 		mutex_enter(&uill->ill_lock);
21054 		for (ipif = uill->ill_ipif; ipif != NULL;
21055 		    ipif = ipif->ipif_next) {
21056 			if (!IPIF_CAN_LOOKUP(ipif))
21057 				continue;
21058 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
21059 				continue;
21060 			if (!(ipif->ipif_flags & IPIF_UP))
21061 				continue;
21062 			if (ipif->ipif_zoneid != zoneid)
21063 				continue;
21064 			if ((isv6 &&
21065 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
21066 			    (ipif->ipif_lcl_addr == INADDR_ANY))
21067 				continue;
21068 			mutex_exit(&uill->ill_lock);
21069 			ill_refrele(uill);
21070 			return (B_TRUE);
21071 		}
21072 		mutex_exit(&uill->ill_lock);
21073 		ill_refrele(uill);
21074 	}
21075 	return (B_FALSE);
21076 }
21077 
21078 /*
21079  * Determine the best source address given a destination address and an ill.
21080  * Prefers non-deprecated over deprecated but will return a deprecated
21081  * address if there is no other choice. If there is a usable source address
21082  * on the interface pointed to by ill_usesrc_ifindex then that is given
21083  * first preference.
21084  *
21085  * Returns NULL if there is no suitable source address for the ill.
21086  * This only occurs when there is no valid source address for the ill.
21087  */
21088 ipif_t *
21089 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
21090 {
21091 	ipif_t *ipif;
21092 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
21093 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
21094 	int index = 0;
21095 	boolean_t wrapped = B_FALSE;
21096 	boolean_t same_subnet_only = B_FALSE;
21097 	boolean_t ipif_same_found, ipif_other_found;
21098 	boolean_t specific_found;
21099 	ill_t	*till, *usill = NULL;
21100 	tsol_tpc_t *src_rhtp, *dst_rhtp;
21101 	ip_stack_t	*ipst = ill->ill_ipst;
21102 
21103 	if (ill->ill_usesrc_ifindex != 0) {
21104 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
21105 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
21106 		if (usill != NULL)
21107 			ill = usill;	/* Select source from usesrc ILL */
21108 		else
21109 			return (NULL);
21110 	}
21111 
21112 	/*
21113 	 * If we're dealing with an unlabeled destination on a labeled system,
21114 	 * make sure that we ignore source addresses that are incompatible with
21115 	 * the destination's default label.  That destination's default label
21116 	 * must dominate the minimum label on the source address.
21117 	 */
21118 	dst_rhtp = NULL;
21119 	if (is_system_labeled()) {
21120 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
21121 		if (dst_rhtp == NULL)
21122 			return (NULL);
21123 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
21124 			TPC_RELE(dst_rhtp);
21125 			dst_rhtp = NULL;
21126 		}
21127 	}
21128 
21129 	/*
21130 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
21131 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
21132 	 * After selecting the right ipif, under ill_lock make sure ipif is
21133 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
21134 	 * we retry. Inside the loop we still need to check for CONDEMNED,
21135 	 * but not under a lock.
21136 	 */
21137 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21138 
21139 retry:
21140 	till = ill;
21141 	ipif_arr[0] = NULL;
21142 
21143 	if (till->ill_group != NULL)
21144 		till = till->ill_group->illgrp_ill;
21145 
21146 	/*
21147 	 * Choose one good source address from each ill across the group.
21148 	 * If possible choose a source address in the same subnet as
21149 	 * the destination address.
21150 	 *
21151 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
21152 	 * This is okay because of the following.
21153 	 *
21154 	 *    If PHYI_FAILED is set and we still have non-deprecated
21155 	 *    addresses, it means the addresses have not yet been
21156 	 *    failed over to a different interface. We potentially
21157 	 *    select them to create IRE_CACHES, which will be later
21158 	 *    flushed when the addresses move over.
21159 	 *
21160 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
21161 	 *    addresses, it means either the user has configured them
21162 	 *    or PHYI_INACTIVE has not been cleared after the addresses
21163 	 *    been moved over. For the former, in.mpathd does a failover
21164 	 *    when the interface becomes INACTIVE and hence we should
21165 	 *    not find them. Once INACTIVE is set, we don't allow them
21166 	 *    to create logical interfaces anymore. For the latter, a
21167 	 *    flush will happen when INACTIVE is cleared which will
21168 	 *    flush the IRE_CACHES.
21169 	 *
21170 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
21171 	 *    over soon. We potentially select them to create IRE_CACHEs,
21172 	 *    which will be later flushed when the addresses move over.
21173 	 *
21174 	 * NOTE : As ipif_select_source is called to borrow source address
21175 	 * for an ipif that is part of a group, source address selection
21176 	 * will be re-done whenever the group changes i.e either an
21177 	 * insertion/deletion in the group.
21178 	 *
21179 	 * Fill ipif_arr[] with source addresses, using these rules:
21180 	 *
21181 	 *	1. At most one source address from a given ill ends up
21182 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
21183 	 *	   associated with a given ill ends up in ipif_arr[].
21184 	 *
21185 	 *	2. If there is at least one non-deprecated ipif in the
21186 	 *	   IPMP group with a source address on the same subnet as
21187 	 *	   our destination, then fill ipif_arr[] only with
21188 	 *	   source addresses on the same subnet as our destination.
21189 	 *	   Note that because of (1), only the first
21190 	 *	   non-deprecated ipif found with a source address
21191 	 *	   matching the destination ends up in ipif_arr[].
21192 	 *
21193 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
21194 	 *	   addresses not in the same subnet as our destination.
21195 	 *	   Again, because of (1), only the first off-subnet source
21196 	 *	   address will be chosen.
21197 	 *
21198 	 *	4. If there are no non-deprecated ipifs, then just use
21199 	 *	   the source address associated with the last deprecated
21200 	 *	   one we find that happens to be on the same subnet,
21201 	 *	   otherwise the first one not in the same subnet.
21202 	 */
21203 	specific_found = B_FALSE;
21204 	for (; till != NULL; till = till->ill_group_next) {
21205 		ipif_same_found = B_FALSE;
21206 		ipif_other_found = B_FALSE;
21207 		for (ipif = till->ill_ipif; ipif != NULL;
21208 		    ipif = ipif->ipif_next) {
21209 			if (!IPIF_CAN_LOOKUP(ipif))
21210 				continue;
21211 			/* Always skip NOLOCAL and ANYCAST interfaces */
21212 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
21213 				continue;
21214 			if (!(ipif->ipif_flags & IPIF_UP) ||
21215 			    !ipif->ipif_addr_ready)
21216 				continue;
21217 			if (ipif->ipif_zoneid != zoneid &&
21218 			    ipif->ipif_zoneid != ALL_ZONES)
21219 				continue;
21220 			/*
21221 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
21222 			 * but are not valid as source addresses.
21223 			 */
21224 			if (ipif->ipif_lcl_addr == INADDR_ANY)
21225 				continue;
21226 
21227 			/*
21228 			 * Check compatibility of local address for
21229 			 * destination's default label if we're on a labeled
21230 			 * system.  Incompatible addresses can't be used at
21231 			 * all.
21232 			 */
21233 			if (dst_rhtp != NULL) {
21234 				boolean_t incompat;
21235 
21236 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
21237 				    IPV4_VERSION, B_FALSE);
21238 				if (src_rhtp == NULL)
21239 					continue;
21240 				incompat =
21241 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
21242 				    src_rhtp->tpc_tp.tp_doi !=
21243 				    dst_rhtp->tpc_tp.tp_doi ||
21244 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
21245 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
21246 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
21247 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
21248 				TPC_RELE(src_rhtp);
21249 				if (incompat)
21250 					continue;
21251 			}
21252 
21253 			/*
21254 			 * We prefer not to use all all-zones addresses, if we
21255 			 * can avoid it, as they pose problems with unlabeled
21256 			 * destinations.
21257 			 */
21258 			if (ipif->ipif_zoneid != ALL_ZONES) {
21259 				if (!specific_found &&
21260 				    (!same_subnet_only ||
21261 				    (ipif->ipif_net_mask & dst) ==
21262 				    ipif->ipif_subnet)) {
21263 					index = 0;
21264 					specific_found = B_TRUE;
21265 					ipif_other_found = B_FALSE;
21266 				}
21267 			} else {
21268 				if (specific_found)
21269 					continue;
21270 			}
21271 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
21272 				if (ipif_dep == NULL ||
21273 				    (ipif->ipif_net_mask & dst) ==
21274 				    ipif->ipif_subnet)
21275 					ipif_dep = ipif;
21276 				continue;
21277 			}
21278 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
21279 				/* found a source address in the same subnet */
21280 				if (!same_subnet_only) {
21281 					same_subnet_only = B_TRUE;
21282 					index = 0;
21283 				}
21284 				ipif_same_found = B_TRUE;
21285 			} else {
21286 				if (same_subnet_only || ipif_other_found)
21287 					continue;
21288 				ipif_other_found = B_TRUE;
21289 			}
21290 			ipif_arr[index++] = ipif;
21291 			if (index == MAX_IPIF_SELECT_SOURCE) {
21292 				wrapped = B_TRUE;
21293 				index = 0;
21294 			}
21295 			if (ipif_same_found)
21296 				break;
21297 		}
21298 	}
21299 
21300 	if (ipif_arr[0] == NULL) {
21301 		ipif = ipif_dep;
21302 	} else {
21303 		if (wrapped)
21304 			index = MAX_IPIF_SELECT_SOURCE;
21305 		ipif = ipif_arr[ipif_rand(ipst) % index];
21306 		ASSERT(ipif != NULL);
21307 	}
21308 
21309 	if (ipif != NULL) {
21310 		mutex_enter(&ipif->ipif_ill->ill_lock);
21311 		if (!IPIF_CAN_LOOKUP(ipif)) {
21312 			mutex_exit(&ipif->ipif_ill->ill_lock);
21313 			goto retry;
21314 		}
21315 		ipif_refhold_locked(ipif);
21316 		mutex_exit(&ipif->ipif_ill->ill_lock);
21317 	}
21318 
21319 	rw_exit(&ipst->ips_ill_g_lock);
21320 	if (usill != NULL)
21321 		ill_refrele(usill);
21322 	if (dst_rhtp != NULL)
21323 		TPC_RELE(dst_rhtp);
21324 
21325 #ifdef DEBUG
21326 	if (ipif == NULL) {
21327 		char buf1[INET6_ADDRSTRLEN];
21328 
21329 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
21330 		    ill->ill_name,
21331 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
21332 	} else {
21333 		char buf1[INET6_ADDRSTRLEN];
21334 		char buf2[INET6_ADDRSTRLEN];
21335 
21336 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
21337 		    ipif->ipif_ill->ill_name,
21338 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21339 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21340 		    buf2, sizeof (buf2))));
21341 	}
21342 #endif /* DEBUG */
21343 	return (ipif);
21344 }
21345 
21346 
21347 /*
21348  * If old_ipif is not NULL, see if ipif was derived from old
21349  * ipif and if so, recreate the interface route by re-doing
21350  * source address selection. This happens when ipif_down ->
21351  * ipif_update_other_ipifs calls us.
21352  *
21353  * If old_ipif is NULL, just redo the source address selection
21354  * if needed. This happens when illgrp_insert or ipif_up_done
21355  * calls us.
21356  */
21357 static void
21358 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21359 {
21360 	ire_t *ire;
21361 	ire_t *ipif_ire;
21362 	queue_t *stq;
21363 	ipif_t *nipif;
21364 	ill_t *ill;
21365 	boolean_t need_rele = B_FALSE;
21366 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21367 
21368 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21369 	ASSERT(IAM_WRITER_IPIF(ipif));
21370 
21371 	ill = ipif->ipif_ill;
21372 	if (!(ipif->ipif_flags &
21373 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21374 		/*
21375 		 * Can't possibly have borrowed the source
21376 		 * from old_ipif.
21377 		 */
21378 		return;
21379 	}
21380 
21381 	/*
21382 	 * Is there any work to be done? No work if the address
21383 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21384 	 * ipif_select_source() does not borrow addresses from
21385 	 * NOLOCAL and ANYCAST interfaces).
21386 	 */
21387 	if ((old_ipif != NULL) &&
21388 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21389 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21390 	    (old_ipif->ipif_flags &
21391 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21392 		return;
21393 	}
21394 
21395 	/*
21396 	 * Perform the same checks as when creating the
21397 	 * IRE_INTERFACE in ipif_up_done.
21398 	 */
21399 	if (!(ipif->ipif_flags & IPIF_UP))
21400 		return;
21401 
21402 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21403 	    (ipif->ipif_subnet == INADDR_ANY))
21404 		return;
21405 
21406 	ipif_ire = ipif_to_ire(ipif);
21407 	if (ipif_ire == NULL)
21408 		return;
21409 
21410 	/*
21411 	 * We know that ipif uses some other source for its
21412 	 * IRE_INTERFACE. Is it using the source of this
21413 	 * old_ipif?
21414 	 */
21415 	if (old_ipif != NULL &&
21416 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21417 		ire_refrele(ipif_ire);
21418 		return;
21419 	}
21420 	if (ip_debug > 2) {
21421 		/* ip1dbg */
21422 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21423 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21424 	}
21425 
21426 	stq = ipif_ire->ire_stq;
21427 
21428 	/*
21429 	 * Can't use our source address. Select a different
21430 	 * source address for the IRE_INTERFACE.
21431 	 */
21432 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21433 	if (nipif == NULL) {
21434 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21435 		nipif = ipif;
21436 	} else {
21437 		need_rele = B_TRUE;
21438 	}
21439 
21440 	ire = ire_create(
21441 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21442 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21443 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21444 	    NULL,				/* no gateway */
21445 	    NULL,
21446 	    &ipif->ipif_mtu,			/* max frag */
21447 	    NULL,				/* fast path header */
21448 	    NULL,				/* no recv from queue */
21449 	    stq,				/* send-to queue */
21450 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21451 	    ill->ill_resolver_mp,		/* xmit header */
21452 	    ipif,
21453 	    NULL,
21454 	    0,
21455 	    0,
21456 	    0,
21457 	    0,
21458 	    &ire_uinfo_null,
21459 	    NULL,
21460 	    NULL,
21461 	    ipst);
21462 
21463 	if (ire != NULL) {
21464 		ire_t *ret_ire;
21465 		int error;
21466 
21467 		/*
21468 		 * We don't need ipif_ire anymore. We need to delete
21469 		 * before we add so that ire_add does not detect
21470 		 * duplicates.
21471 		 */
21472 		ire_delete(ipif_ire);
21473 		ret_ire = ire;
21474 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21475 		ASSERT(error == 0);
21476 		ASSERT(ire == ret_ire);
21477 		/* Held in ire_add */
21478 		ire_refrele(ret_ire);
21479 	}
21480 	/*
21481 	 * Either we are falling through from above or could not
21482 	 * allocate a replacement.
21483 	 */
21484 	ire_refrele(ipif_ire);
21485 	if (need_rele)
21486 		ipif_refrele(nipif);
21487 }
21488 
21489 /*
21490  * This old_ipif is going away.
21491  *
21492  * Determine if any other ipif's is using our address as
21493  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21494  * IPIF_DEPRECATED).
21495  * Find the IRE_INTERFACE for such ipifs and recreate them
21496  * to use an different source address following the rules in
21497  * ipif_up_done.
21498  *
21499  * This function takes an illgrp as an argument so that illgrp_delete
21500  * can call this to update source address even after deleting the
21501  * old_ipif->ipif_ill from the ill group.
21502  */
21503 static void
21504 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21505 {
21506 	ipif_t *ipif;
21507 	ill_t *ill;
21508 	char	buf[INET6_ADDRSTRLEN];
21509 
21510 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21511 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21512 
21513 	ill = old_ipif->ipif_ill;
21514 
21515 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21516 	    ill->ill_name,
21517 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21518 	    buf, sizeof (buf))));
21519 	/*
21520 	 * If this part of a group, look at all ills as ipif_select_source
21521 	 * borrows source address across all the ills in the group.
21522 	 */
21523 	if (illgrp != NULL)
21524 		ill = illgrp->illgrp_ill;
21525 
21526 	for (; ill != NULL; ill = ill->ill_group_next) {
21527 		for (ipif = ill->ill_ipif; ipif != NULL;
21528 		    ipif = ipif->ipif_next) {
21529 
21530 			if (ipif == old_ipif)
21531 				continue;
21532 
21533 			ipif_recreate_interface_routes(old_ipif, ipif);
21534 		}
21535 	}
21536 }
21537 
21538 /* ARGSUSED */
21539 int
21540 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21541 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21542 {
21543 	/*
21544 	 * ill_phyint_reinit merged the v4 and v6 into a single
21545 	 * ipsq. Could also have become part of a ipmp group in the
21546 	 * process, and we might not have been able to complete the
21547 	 * operation in ipif_set_values, if we could not become
21548 	 * exclusive.  If so restart it here.
21549 	 */
21550 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21551 }
21552 
21553 
21554 /*
21555  * Can operate on either a module or a driver queue.
21556  * Returns an error if not a module queue.
21557  */
21558 /* ARGSUSED */
21559 int
21560 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21561     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21562 {
21563 	queue_t		*q1 = q;
21564 	char 		*cp;
21565 	char		interf_name[LIFNAMSIZ];
21566 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21567 
21568 	if (q->q_next == NULL) {
21569 		ip1dbg((
21570 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21571 		return (EINVAL);
21572 	}
21573 
21574 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21575 		return (EALREADY);
21576 
21577 	do {
21578 		q1 = q1->q_next;
21579 	} while (q1->q_next);
21580 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21581 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21582 
21583 	/*
21584 	 * Here we are not going to delay the ioack until after
21585 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21586 	 * original ioctl message before sending the requests.
21587 	 */
21588 	return (ipif_set_values(q, mp, interf_name, &ppa));
21589 }
21590 
21591 /* ARGSUSED */
21592 int
21593 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21594     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21595 {
21596 	return (ENXIO);
21597 }
21598 
21599 /*
21600  * Net and subnet broadcast ire's are now specific to the particular
21601  * physical interface (ill) and not to any one locigal interface (ipif).
21602  * However, if a particular logical interface is being taken down, it's
21603  * associated ire's will be taken down as well.  Hence, when we go to
21604  * take down or change the local address, broadcast address or netmask
21605  * of a specific logical interface, we must check to make sure that we
21606  * have valid net and subnet broadcast ire's for the other logical
21607  * interfaces which may have been shared with the logical interface
21608  * being brought down or changed.
21609  *
21610  * There is one set of 0.0.0.0 and 255.255.255.255 per ill. Usually it
21611  * is tied to the first interface coming UP. If that ipif is going down,
21612  * we need to recreate them on the next valid ipif.
21613  *
21614  * Note: assume that the ipif passed in is still up so that it's IRE
21615  * entries are still valid.
21616  */
21617 static void
21618 ipif_check_bcast_ires(ipif_t *test_ipif)
21619 {
21620 	ipif_t	*ipif;
21621 	ire_t	*test_subnet_ire, *test_net_ire;
21622 	ire_t	*test_allzero_ire, *test_allone_ire;
21623 	ire_t	*ire_array[12];
21624 	ire_t	**irep = &ire_array[0];
21625 	ire_t	**irep1;
21626 	ipaddr_t net_addr, subnet_addr, net_mask, subnet_mask;
21627 	ipaddr_t test_net_addr, test_subnet_addr;
21628 	ipaddr_t test_net_mask, test_subnet_mask;
21629 	boolean_t need_net_bcast_ire = B_FALSE;
21630 	boolean_t need_subnet_bcast_ire = B_FALSE;
21631 	boolean_t allzero_bcast_ire_created = B_FALSE;
21632 	boolean_t allone_bcast_ire_created = B_FALSE;
21633 	boolean_t net_bcast_ire_created = B_FALSE;
21634 	boolean_t subnet_bcast_ire_created = B_FALSE;
21635 
21636 	ipif_t  *backup_ipif_net = (ipif_t *)NULL;
21637 	ipif_t  *backup_ipif_subnet = (ipif_t *)NULL;
21638 	ipif_t  *backup_ipif_allzeros = (ipif_t *)NULL;
21639 	ipif_t  *backup_ipif_allones = (ipif_t *)NULL;
21640 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
21641 	ip_stack_t	*ipst = test_ipif->ipif_ill->ill_ipst;
21642 
21643 	ASSERT(!test_ipif->ipif_isv6);
21644 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21645 
21646 	/*
21647 	 * No broadcast IREs for the LOOPBACK interface
21648 	 * or others such as point to point and IPIF_NOXMIT.
21649 	 */
21650 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21651 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21652 		return;
21653 
21654 	test_allzero_ire = ire_ctable_lookup(0, 0, IRE_BROADCAST,
21655 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21656 	    ipst);
21657 
21658 	test_allone_ire = ire_ctable_lookup(INADDR_BROADCAST, 0, IRE_BROADCAST,
21659 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21660 	    ipst);
21661 
21662 	test_net_mask = ip_net_mask(test_ipif->ipif_subnet);
21663 	test_subnet_mask = test_ipif->ipif_net_mask;
21664 
21665 	/*
21666 	 * If no net mask set, assume the default based on net class.
21667 	 */
21668 	if (test_subnet_mask == 0)
21669 		test_subnet_mask = test_net_mask;
21670 
21671 	/*
21672 	 * Check if there is a network broadcast ire associated with this ipif
21673 	 */
21674 	test_net_addr = test_net_mask  & test_ipif->ipif_subnet;
21675 	test_net_ire = ire_ctable_lookup(test_net_addr, 0, IRE_BROADCAST,
21676 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21677 	    ipst);
21678 
21679 	/*
21680 	 * Check if there is a subnet broadcast IRE associated with this ipif
21681 	 */
21682 	test_subnet_addr = test_subnet_mask  & test_ipif->ipif_subnet;
21683 	test_subnet_ire = ire_ctable_lookup(test_subnet_addr, 0, IRE_BROADCAST,
21684 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21685 	    ipst);
21686 
21687 	/*
21688 	 * No broadcast ire's associated with this ipif.
21689 	 */
21690 	if ((test_subnet_ire == NULL) && (test_net_ire == NULL) &&
21691 	    (test_allzero_ire == NULL) && (test_allone_ire == NULL)) {
21692 		return;
21693 	}
21694 
21695 	/*
21696 	 * We have established which bcast ires have to be replaced.
21697 	 * Next we try to locate ipifs that match there ires.
21698 	 * The rules are simple: If we find an ipif that matches on the subnet
21699 	 * address it will also match on the net address, the allzeros and
21700 	 * allones address. Any ipif that matches only on the net address will
21701 	 * also match the allzeros and allones addresses.
21702 	 * The other criterion is the ipif_flags. We look for non-deprecated
21703 	 * (and non-anycast and non-nolocal) ipifs as the best choice.
21704 	 * ipifs with check_flags matching (deprecated, etc) are used only
21705 	 * if good ipifs are not available. While looping, we save existing
21706 	 * deprecated ipifs as backup_ipif.
21707 	 * We loop through all the ipifs for this ill looking for ipifs
21708 	 * whose broadcast addr match the ipif passed in, but do not have
21709 	 * their own broadcast ires. For creating 0.0.0.0 and
21710 	 * 255.255.255.255 we just need an ipif on this ill to create.
21711 	 */
21712 	for (ipif = test_ipif->ipif_ill->ill_ipif; ipif != NULL;
21713 	    ipif = ipif->ipif_next) {
21714 
21715 		ASSERT(!ipif->ipif_isv6);
21716 		/*
21717 		 * Already checked the ipif passed in.
21718 		 */
21719 		if (ipif == test_ipif) {
21720 			continue;
21721 		}
21722 
21723 		/*
21724 		 * We only need to recreate broadcast ires if another ipif in
21725 		 * the same zone uses them. The new ires must be created in the
21726 		 * same zone.
21727 		 */
21728 		if (ipif->ipif_zoneid != test_ipif->ipif_zoneid) {
21729 			continue;
21730 		}
21731 
21732 		/*
21733 		 * Only interested in logical interfaces with valid local
21734 		 * addresses or with the ability to broadcast.
21735 		 */
21736 		if ((ipif->ipif_subnet == 0) ||
21737 		    !(ipif->ipif_flags & IPIF_BROADCAST) ||
21738 		    (ipif->ipif_flags & IPIF_NOXMIT) ||
21739 		    !(ipif->ipif_flags & IPIF_UP)) {
21740 			continue;
21741 		}
21742 		/*
21743 		 * Check if there is a net broadcast ire for this
21744 		 * net address.  If it turns out that the ipif we are
21745 		 * about to take down owns this ire, we must make a
21746 		 * new one because it is potentially going away.
21747 		 */
21748 		if (test_net_ire && (!net_bcast_ire_created)) {
21749 			net_mask = ip_net_mask(ipif->ipif_subnet);
21750 			net_addr = net_mask & ipif->ipif_subnet;
21751 			if (net_addr == test_net_addr) {
21752 				need_net_bcast_ire = B_TRUE;
21753 				/*
21754 				 * Use DEPRECATED ipif only if no good
21755 				 * ires are available. subnet_addr is
21756 				 * a better match than net_addr.
21757 				 */
21758 				if ((ipif->ipif_flags & check_flags) &&
21759 				    (backup_ipif_net == NULL)) {
21760 					backup_ipif_net = ipif;
21761 				}
21762 			}
21763 		}
21764 		/*
21765 		 * Check if there is a subnet broadcast ire for this
21766 		 * net address.  If it turns out that the ipif we are
21767 		 * about to take down owns this ire, we must make a
21768 		 * new one because it is potentially going away.
21769 		 */
21770 		if (test_subnet_ire && (!subnet_bcast_ire_created)) {
21771 			subnet_mask = ipif->ipif_net_mask;
21772 			subnet_addr = ipif->ipif_subnet;
21773 			if (subnet_addr == test_subnet_addr) {
21774 				need_subnet_bcast_ire = B_TRUE;
21775 				if ((ipif->ipif_flags & check_flags) &&
21776 				    (backup_ipif_subnet == NULL)) {
21777 					backup_ipif_subnet = ipif;
21778 				}
21779 			}
21780 		}
21781 
21782 
21783 		/* Short circuit here if this ipif is deprecated */
21784 		if (ipif->ipif_flags & check_flags) {
21785 			if ((test_allzero_ire != NULL) &&
21786 			    (!allzero_bcast_ire_created) &&
21787 			    (backup_ipif_allzeros == NULL)) {
21788 				backup_ipif_allzeros = ipif;
21789 			}
21790 			if ((test_allone_ire != NULL) &&
21791 			    (!allone_bcast_ire_created) &&
21792 			    (backup_ipif_allones == NULL)) {
21793 				backup_ipif_allones = ipif;
21794 			}
21795 			continue;
21796 		}
21797 
21798 		/*
21799 		 * Found an ipif which has the same broadcast ire as the
21800 		 * ipif passed in and the ipif passed in "owns" the ire.
21801 		 * Create new broadcast ire's for this broadcast addr.
21802 		 */
21803 		if (need_net_bcast_ire && !net_bcast_ire_created) {
21804 			irep = ire_create_bcast(ipif, net_addr, irep);
21805 			irep = ire_create_bcast(ipif,
21806 			    ~net_mask | net_addr, irep);
21807 			net_bcast_ire_created = B_TRUE;
21808 		}
21809 		if (need_subnet_bcast_ire && !subnet_bcast_ire_created) {
21810 			irep = ire_create_bcast(ipif, subnet_addr, irep);
21811 			irep = ire_create_bcast(ipif,
21812 			    ~subnet_mask | subnet_addr, irep);
21813 			subnet_bcast_ire_created = B_TRUE;
21814 		}
21815 		if (test_allzero_ire != NULL && !allzero_bcast_ire_created) {
21816 			irep = ire_create_bcast(ipif, 0, irep);
21817 			allzero_bcast_ire_created = B_TRUE;
21818 		}
21819 		if (test_allone_ire != NULL && !allone_bcast_ire_created) {
21820 			irep = ire_create_bcast(ipif, INADDR_BROADCAST, irep);
21821 			allone_bcast_ire_created = B_TRUE;
21822 		}
21823 		/*
21824 		 * Once we have created all the appropriate ires, we
21825 		 * just break out of this loop to add what we have created.
21826 		 * This has been indented similar to ire_match_args for
21827 		 * readability.
21828 		 */
21829 		if (((test_net_ire == NULL) ||
21830 			(net_bcast_ire_created)) &&
21831 		    ((test_subnet_ire == NULL) ||
21832 			(subnet_bcast_ire_created)) &&
21833 		    ((test_allzero_ire == NULL) ||
21834 			(allzero_bcast_ire_created)) &&
21835 		    ((test_allone_ire == NULL) ||
21836 			(allone_bcast_ire_created))) {
21837 			break;
21838 		}
21839 	}
21840 
21841 	/*
21842 	 * Create bcast ires on deprecated ipifs if no non-deprecated ipifs
21843 	 * exist. 6 pairs of bcast ires are needed.
21844 	 * Note - the old ires are deleted in ipif_down.
21845 	 */
21846 	if (need_net_bcast_ire && !net_bcast_ire_created && backup_ipif_net) {
21847 		ipif = backup_ipif_net;
21848 		irep = ire_create_bcast(ipif, net_addr, irep);
21849 		irep = ire_create_bcast(ipif, ~net_mask | net_addr, irep);
21850 		net_bcast_ire_created = B_TRUE;
21851 	}
21852 	if (need_subnet_bcast_ire && !subnet_bcast_ire_created &&
21853 	    backup_ipif_subnet) {
21854 		ipif = backup_ipif_subnet;
21855 		irep = ire_create_bcast(ipif, subnet_addr, irep);
21856 		irep = ire_create_bcast(ipif,
21857 		    ~subnet_mask | subnet_addr, irep);
21858 		subnet_bcast_ire_created = B_TRUE;
21859 	}
21860 	if (test_allzero_ire != NULL && !allzero_bcast_ire_created &&
21861 	    backup_ipif_allzeros) {
21862 		irep = ire_create_bcast(backup_ipif_allzeros, 0, irep);
21863 		allzero_bcast_ire_created = B_TRUE;
21864 	}
21865 	if (test_allone_ire != NULL && !allone_bcast_ire_created &&
21866 	    backup_ipif_allones) {
21867 		irep = ire_create_bcast(backup_ipif_allones,
21868 		    INADDR_BROADCAST, irep);
21869 		allone_bcast_ire_created = B_TRUE;
21870 	}
21871 
21872 	/*
21873 	 * If we can't create all of them, don't add any of them.
21874 	 * Code in ip_wput_ire and ire_to_ill assumes that we
21875 	 * always have a non-loopback copy and loopback copy
21876 	 * for a given address.
21877 	 */
21878 	for (irep1 = irep; irep1 > ire_array; ) {
21879 		irep1--;
21880 		if (*irep1 == NULL) {
21881 			ip0dbg(("ipif_check_bcast_ires: can't create "
21882 			    "IRE_BROADCAST, memory allocation failure\n"));
21883 			while (irep > ire_array) {
21884 				irep--;
21885 				if (*irep != NULL)
21886 					ire_delete(*irep);
21887 			}
21888 			goto bad;
21889 		}
21890 	}
21891 	for (irep1 = irep; irep1 > ire_array; ) {
21892 		int error;
21893 
21894 		irep1--;
21895 		error = ire_add(irep1, NULL, NULL, NULL, B_FALSE);
21896 		if (error == 0) {
21897 			ire_refrele(*irep1);		/* Held in ire_add */
21898 		}
21899 	}
21900 bad:
21901 	if (test_allzero_ire != NULL)
21902 		ire_refrele(test_allzero_ire);
21903 	if (test_allone_ire != NULL)
21904 		ire_refrele(test_allone_ire);
21905 	if (test_net_ire != NULL)
21906 		ire_refrele(test_net_ire);
21907 	if (test_subnet_ire != NULL)
21908 		ire_refrele(test_subnet_ire);
21909 }
21910 
21911 /*
21912  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21913  * from lifr_flags and the name from lifr_name.
21914  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21915  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21916  * Returns EINPROGRESS when mp has been consumed by queueing it on
21917  * ill_pending_mp and the ioctl will complete in ip_rput.
21918  *
21919  * Can operate on either a module or a driver queue.
21920  * Returns an error if not a module queue.
21921  */
21922 /* ARGSUSED */
21923 int
21924 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21925     ip_ioctl_cmd_t *ipip, void *if_req)
21926 {
21927 	int	err;
21928 	ill_t	*ill;
21929 	struct lifreq *lifr = (struct lifreq *)if_req;
21930 
21931 	ASSERT(ipif != NULL);
21932 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21933 
21934 	if (q->q_next == NULL) {
21935 		ip1dbg((
21936 		    "if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21937 		return (EINVAL);
21938 	}
21939 
21940 	ill = (ill_t *)q->q_ptr;
21941 	/*
21942 	 * If we are not writer on 'q' then this interface exists already
21943 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
21944 	 * So return EALREADY
21945 	 */
21946 	if (ill != ipif->ipif_ill)
21947 		return (EALREADY);
21948 
21949 	if (ill->ill_name[0] != '\0')
21950 		return (EALREADY);
21951 
21952 	/*
21953 	 * Set all the flags. Allows all kinds of override. Provide some
21954 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21955 	 * unless there is either multicast/broadcast support in the driver
21956 	 * or it is a pt-pt link.
21957 	 */
21958 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21959 		/* Meaningless to IP thus don't allow them to be set. */
21960 		ip1dbg(("ip_setname: EINVAL 1\n"));
21961 		return (EINVAL);
21962 	}
21963 	/*
21964 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21965 	 * ill_bcast_addr_length info.
21966 	 */
21967 	if (!ill->ill_needs_attach &&
21968 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21969 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21970 	    ill->ill_bcast_addr_length == 0)) {
21971 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21972 		ip1dbg(("ip_setname: EINVAL 2\n"));
21973 		return (EINVAL);
21974 	}
21975 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21976 	    ((lifr->lifr_flags & IFF_IPV6) ||
21977 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21978 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21979 		ip1dbg(("ip_setname: EINVAL 3\n"));
21980 		return (EINVAL);
21981 	}
21982 	if (lifr->lifr_flags & IFF_UP) {
21983 		/* Can only be set with SIOCSLIFFLAGS */
21984 		ip1dbg(("ip_setname: EINVAL 4\n"));
21985 		return (EINVAL);
21986 	}
21987 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21988 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21989 		ip1dbg(("ip_setname: EINVAL 5\n"));
21990 		return (EINVAL);
21991 	}
21992 	/*
21993 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21994 	 */
21995 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21996 	    !(lifr->lifr_flags & IFF_IPV6) &&
21997 	    !(ipif->ipif_isv6)) {
21998 		ip1dbg(("ip_setname: EINVAL 6\n"));
21999 		return (EINVAL);
22000 	}
22001 
22002 	/*
22003 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
22004 	 * we have all the flags here. So, we assign rather than we OR.
22005 	 * We can't OR the flags here because we don't want to set
22006 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
22007 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
22008 	 * on lifr_flags value here.
22009 	 */
22010 	/*
22011 	 * This ill has not been inserted into the global list.
22012 	 * So we are still single threaded and don't need any lock
22013 	 */
22014 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
22015 	    ~IFF_DUPLICATE;
22016 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
22017 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
22018 
22019 	/* We started off as V4. */
22020 	if (ill->ill_flags & ILLF_IPV6) {
22021 		ill->ill_phyint->phyint_illv6 = ill;
22022 		ill->ill_phyint->phyint_illv4 = NULL;
22023 	}
22024 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
22025 	return (err);
22026 }
22027 
22028 /* ARGSUSED */
22029 int
22030 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22031     ip_ioctl_cmd_t *ipip, void *if_req)
22032 {
22033 	/*
22034 	 * ill_phyint_reinit merged the v4 and v6 into a single
22035 	 * ipsq. Could also have become part of a ipmp group in the
22036 	 * process, and we might not have been able to complete the
22037 	 * slifname in ipif_set_values, if we could not become
22038 	 * exclusive.  If so restart it here
22039 	 */
22040 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
22041 }
22042 
22043 /*
22044  * Return a pointer to the ipif which matches the index, IP version type and
22045  * zoneid.
22046  */
22047 ipif_t *
22048 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
22049     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
22050 {
22051 	ill_t	*ill;
22052 	ipsq_t  *ipsq;
22053 	phyint_t *phyi;
22054 	ipif_t	*ipif;
22055 
22056 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
22057 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
22058 
22059 	if (err != NULL)
22060 		*err = 0;
22061 
22062 	/*
22063 	 * Indexes are stored in the phyint - a common structure
22064 	 * to both IPv4 and IPv6.
22065 	 */
22066 
22067 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
22068 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22069 	    (void *) &index, NULL);
22070 	if (phyi != NULL) {
22071 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
22072 		if (ill == NULL) {
22073 			rw_exit(&ipst->ips_ill_g_lock);
22074 			if (err != NULL)
22075 				*err = ENXIO;
22076 			return (NULL);
22077 		}
22078 		GRAB_CONN_LOCK(q);
22079 		mutex_enter(&ill->ill_lock);
22080 		if (ILL_CAN_LOOKUP(ill)) {
22081 			for (ipif = ill->ill_ipif; ipif != NULL;
22082 			    ipif = ipif->ipif_next) {
22083 				if (IPIF_CAN_LOOKUP(ipif) &&
22084 				    (zoneid == ALL_ZONES ||
22085 				    zoneid == ipif->ipif_zoneid ||
22086 				    ipif->ipif_zoneid == ALL_ZONES)) {
22087 					ipif_refhold_locked(ipif);
22088 					mutex_exit(&ill->ill_lock);
22089 					RELEASE_CONN_LOCK(q);
22090 					rw_exit(&ipst->ips_ill_g_lock);
22091 					return (ipif);
22092 				}
22093 			}
22094 		} else if (ILL_CAN_WAIT(ill, q)) {
22095 			ipsq = ill->ill_phyint->phyint_ipsq;
22096 			mutex_enter(&ipsq->ipsq_lock);
22097 			rw_exit(&ipst->ips_ill_g_lock);
22098 			mutex_exit(&ill->ill_lock);
22099 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
22100 			mutex_exit(&ipsq->ipsq_lock);
22101 			RELEASE_CONN_LOCK(q);
22102 			*err = EINPROGRESS;
22103 			return (NULL);
22104 		}
22105 		mutex_exit(&ill->ill_lock);
22106 		RELEASE_CONN_LOCK(q);
22107 	}
22108 	rw_exit(&ipst->ips_ill_g_lock);
22109 	if (err != NULL)
22110 		*err = ENXIO;
22111 	return (NULL);
22112 }
22113 
22114 typedef struct conn_change_s {
22115 	uint_t cc_old_ifindex;
22116 	uint_t cc_new_ifindex;
22117 } conn_change_t;
22118 
22119 /*
22120  * ipcl_walk function for changing interface index.
22121  */
22122 static void
22123 conn_change_ifindex(conn_t *connp, caddr_t arg)
22124 {
22125 	conn_change_t *connc;
22126 	uint_t old_ifindex;
22127 	uint_t new_ifindex;
22128 	int i;
22129 	ilg_t *ilg;
22130 
22131 	connc = (conn_change_t *)arg;
22132 	old_ifindex = connc->cc_old_ifindex;
22133 	new_ifindex = connc->cc_new_ifindex;
22134 
22135 	if (connp->conn_orig_bound_ifindex == old_ifindex)
22136 		connp->conn_orig_bound_ifindex = new_ifindex;
22137 
22138 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
22139 		connp->conn_orig_multicast_ifindex = new_ifindex;
22140 
22141 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
22142 		connp->conn_orig_xmit_ifindex = new_ifindex;
22143 
22144 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
22145 		ilg = &connp->conn_ilg[i];
22146 		if (ilg->ilg_orig_ifindex == old_ifindex)
22147 			ilg->ilg_orig_ifindex = new_ifindex;
22148 	}
22149 }
22150 
22151 /*
22152  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
22153  * to new_index if it matches the old_index.
22154  *
22155  * Failovers typically happen within a group of ills. But somebody
22156  * can remove an ill from the group after a failover happened. If
22157  * we are setting the ifindex after this, we potentially need to
22158  * look at all the ills rather than just the ones in the group.
22159  * We cut down the work by looking at matching ill_net_types
22160  * and ill_types as we could not possibly grouped them together.
22161  */
22162 static void
22163 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
22164 {
22165 	ill_t *ill;
22166 	ipif_t *ipif;
22167 	uint_t old_ifindex;
22168 	uint_t new_ifindex;
22169 	ilm_t *ilm;
22170 	ill_walk_context_t ctx;
22171 	ip_stack_t	*ipst = ill_orig->ill_ipst;
22172 
22173 	old_ifindex = connc->cc_old_ifindex;
22174 	new_ifindex = connc->cc_new_ifindex;
22175 
22176 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
22177 	ill = ILL_START_WALK_ALL(&ctx, ipst);
22178 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
22179 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
22180 			(ill_orig->ill_type != ill->ill_type)) {
22181 			continue;
22182 		}
22183 		for (ipif = ill->ill_ipif; ipif != NULL;
22184 				ipif = ipif->ipif_next) {
22185 			if (ipif->ipif_orig_ifindex == old_ifindex)
22186 				ipif->ipif_orig_ifindex = new_ifindex;
22187 		}
22188 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
22189 			if (ilm->ilm_orig_ifindex == old_ifindex)
22190 				ilm->ilm_orig_ifindex = new_ifindex;
22191 		}
22192 	}
22193 	rw_exit(&ipst->ips_ill_g_lock);
22194 }
22195 
22196 /*
22197  * We first need to ensure that the new index is unique, and
22198  * then carry the change across both v4 and v6 ill representation
22199  * of the physical interface.
22200  */
22201 /* ARGSUSED */
22202 int
22203 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22204     ip_ioctl_cmd_t *ipip, void *ifreq)
22205 {
22206 	ill_t		*ill;
22207 	ill_t		*ill_other;
22208 	phyint_t	*phyi;
22209 	int		old_index;
22210 	conn_change_t	connc;
22211 	struct ifreq	*ifr = (struct ifreq *)ifreq;
22212 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22213 	uint_t	index;
22214 	ill_t	*ill_v4;
22215 	ill_t	*ill_v6;
22216 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22217 
22218 	if (ipip->ipi_cmd_type == IF_CMD)
22219 		index = ifr->ifr_index;
22220 	else
22221 		index = lifr->lifr_index;
22222 
22223 	/*
22224 	 * Only allow on physical interface. Also, index zero is illegal.
22225 	 *
22226 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
22227 	 *
22228 	 * 1) If PHYI_FAILED is set, a failover could have happened which
22229 	 *    implies a possible failback might have to happen. As failback
22230 	 *    depends on the old index, we should fail setting the index.
22231 	 *
22232 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
22233 	 *    any addresses or multicast memberships are failed over to
22234 	 *    a non-STANDBY interface. As failback depends on the old
22235 	 *    index, we should fail setting the index for this case also.
22236 	 *
22237 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
22238 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
22239 	 */
22240 	ill = ipif->ipif_ill;
22241 	phyi = ill->ill_phyint;
22242 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
22243 	    ipif->ipif_id != 0 || index == 0) {
22244 		return (EINVAL);
22245 	}
22246 	old_index = phyi->phyint_ifindex;
22247 
22248 	/* If the index is not changing, no work to do */
22249 	if (old_index == index)
22250 		return (0);
22251 
22252 	/*
22253 	 * Use ill_lookup_on_ifindex to determine if the
22254 	 * new index is unused and if so allow the change.
22255 	 */
22256 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
22257 	    ipst);
22258 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
22259 	    ipst);
22260 	if (ill_v6 != NULL || ill_v4 != NULL) {
22261 		if (ill_v4 != NULL)
22262 			ill_refrele(ill_v4);
22263 		if (ill_v6 != NULL)
22264 			ill_refrele(ill_v6);
22265 		return (EBUSY);
22266 	}
22267 
22268 	/*
22269 	 * The new index is unused. Set it in the phyint.
22270 	 * Locate the other ill so that we can send a routing
22271 	 * sockets message.
22272 	 */
22273 	if (ill->ill_isv6) {
22274 		ill_other = phyi->phyint_illv4;
22275 	} else {
22276 		ill_other = phyi->phyint_illv6;
22277 	}
22278 
22279 	phyi->phyint_ifindex = index;
22280 
22281 	connc.cc_old_ifindex = old_index;
22282 	connc.cc_new_ifindex = index;
22283 	ip_change_ifindex(ill, &connc);
22284 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
22285 
22286 	/* Send the routing sockets message */
22287 	ip_rts_ifmsg(ipif);
22288 	if (ill_other != NULL)
22289 		ip_rts_ifmsg(ill_other->ill_ipif);
22290 
22291 	return (0);
22292 }
22293 
22294 /* ARGSUSED */
22295 int
22296 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22297     ip_ioctl_cmd_t *ipip, void *ifreq)
22298 {
22299 	struct ifreq	*ifr = (struct ifreq *)ifreq;
22300 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22301 
22302 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
22303 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22304 	/* Get the interface index */
22305 	if (ipip->ipi_cmd_type == IF_CMD) {
22306 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22307 	} else {
22308 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22309 	}
22310 	return (0);
22311 }
22312 
22313 /* ARGSUSED */
22314 int
22315 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22316     ip_ioctl_cmd_t *ipip, void *ifreq)
22317 {
22318 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22319 
22320 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
22321 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22322 	/* Get the interface zone */
22323 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22324 	lifr->lifr_zoneid = ipif->ipif_zoneid;
22325 	return (0);
22326 }
22327 
22328 /*
22329  * Set the zoneid of an interface.
22330  */
22331 /* ARGSUSED */
22332 int
22333 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22334     ip_ioctl_cmd_t *ipip, void *ifreq)
22335 {
22336 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22337 	int err = 0;
22338 	boolean_t need_up = B_FALSE;
22339 	zone_t *zptr;
22340 	zone_status_t status;
22341 	zoneid_t zoneid;
22342 
22343 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22344 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
22345 		if (!is_system_labeled())
22346 			return (ENOTSUP);
22347 		zoneid = GLOBAL_ZONEID;
22348 	}
22349 
22350 	/* cannot assign instance zero to a non-global zone */
22351 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
22352 		return (ENOTSUP);
22353 
22354 	/*
22355 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
22356 	 * the event of a race with the zone shutdown processing, since IP
22357 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22358 	 * interface will be cleaned up even if the zone is shut down
22359 	 * immediately after the status check. If the interface can't be brought
22360 	 * down right away, and the zone is shut down before the restart
22361 	 * function is called, we resolve the possible races by rechecking the
22362 	 * zone status in the restart function.
22363 	 */
22364 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22365 		return (EINVAL);
22366 	status = zone_status_get(zptr);
22367 	zone_rele(zptr);
22368 
22369 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22370 		return (EINVAL);
22371 
22372 	if (ipif->ipif_flags & IPIF_UP) {
22373 		/*
22374 		 * If the interface is already marked up,
22375 		 * we call ipif_down which will take care
22376 		 * of ditching any IREs that have been set
22377 		 * up based on the old interface address.
22378 		 */
22379 		err = ipif_logical_down(ipif, q, mp);
22380 		if (err == EINPROGRESS)
22381 			return (err);
22382 		ipif_down_tail(ipif);
22383 		need_up = B_TRUE;
22384 	}
22385 
22386 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22387 	return (err);
22388 }
22389 
22390 static int
22391 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22392     queue_t *q, mblk_t *mp, boolean_t need_up)
22393 {
22394 	int	err = 0;
22395 	ip_stack_t	*ipst;
22396 
22397 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22398 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22399 
22400 	if (CONN_Q(q))
22401 		ipst = CONNQ_TO_IPST(q);
22402 	else
22403 		ipst = ILLQ_TO_IPST(q);
22404 
22405 	/*
22406 	 * For exclusive stacks we don't allow a different zoneid than
22407 	 * global.
22408 	 */
22409 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22410 	    zoneid != GLOBAL_ZONEID)
22411 		return (EINVAL);
22412 
22413 	/* Set the new zone id. */
22414 	ipif->ipif_zoneid = zoneid;
22415 
22416 	/* Update sctp list */
22417 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22418 
22419 	if (need_up) {
22420 		/*
22421 		 * Now bring the interface back up.  If this
22422 		 * is the only IPIF for the ILL, ipif_up
22423 		 * will have to re-bind to the device, so
22424 		 * we may get back EINPROGRESS, in which
22425 		 * case, this IOCTL will get completed in
22426 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22427 		 */
22428 		err = ipif_up(ipif, q, mp);
22429 	}
22430 	return (err);
22431 }
22432 
22433 /* ARGSUSED */
22434 int
22435 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22436     ip_ioctl_cmd_t *ipip, void *if_req)
22437 {
22438 	struct lifreq *lifr = (struct lifreq *)if_req;
22439 	zoneid_t zoneid;
22440 	zone_t *zptr;
22441 	zone_status_t status;
22442 
22443 	ASSERT(ipif->ipif_id != 0);
22444 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22445 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22446 		zoneid = GLOBAL_ZONEID;
22447 
22448 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22449 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22450 
22451 	/*
22452 	 * We recheck the zone status to resolve the following race condition:
22453 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22454 	 * 2) hme0:1 is up and can't be brought down right away;
22455 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22456 	 * 3) zone "myzone" is halted; the zone status switches to
22457 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22458 	 * the interfaces to remove - hme0:1 is not returned because it's not
22459 	 * yet in "myzone", so it won't be removed;
22460 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22461 	 * status check here, we would have hme0:1 in "myzone" after it's been
22462 	 * destroyed.
22463 	 * Note that if the status check fails, we need to bring the interface
22464 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22465 	 * ipif_up_done[_v6]().
22466 	 */
22467 	status = ZONE_IS_UNINITIALIZED;
22468 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22469 		status = zone_status_get(zptr);
22470 		zone_rele(zptr);
22471 	}
22472 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22473 		if (ipif->ipif_isv6) {
22474 			(void) ipif_up_done_v6(ipif);
22475 		} else {
22476 			(void) ipif_up_done(ipif);
22477 		}
22478 		return (EINVAL);
22479 	}
22480 
22481 	ipif_down_tail(ipif);
22482 
22483 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22484 	    B_TRUE));
22485 }
22486 
22487 /* ARGSUSED */
22488 int
22489 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22490 	ip_ioctl_cmd_t *ipip, void *ifreq)
22491 {
22492 	struct lifreq	*lifr = ifreq;
22493 
22494 	ASSERT(q->q_next == NULL);
22495 	ASSERT(CONN_Q(q));
22496 
22497 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22498 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22499 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22500 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22501 
22502 	return (0);
22503 }
22504 
22505 
22506 /* Find the previous ILL in this usesrc group */
22507 static ill_t *
22508 ill_prev_usesrc(ill_t *uill)
22509 {
22510 	ill_t *ill;
22511 
22512 	for (ill = uill->ill_usesrc_grp_next;
22513 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22514 	    ill = ill->ill_usesrc_grp_next)
22515 		/* do nothing */;
22516 	return (ill);
22517 }
22518 
22519 /*
22520  * Release all members of the usesrc group. This routine is called
22521  * from ill_delete when the interface being unplumbed is the
22522  * group head.
22523  */
22524 static void
22525 ill_disband_usesrc_group(ill_t *uill)
22526 {
22527 	ill_t *next_ill, *tmp_ill;
22528 	ip_stack_t	*ipst = uill->ill_ipst;
22529 
22530 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22531 	next_ill = uill->ill_usesrc_grp_next;
22532 
22533 	do {
22534 		ASSERT(next_ill != NULL);
22535 		tmp_ill = next_ill->ill_usesrc_grp_next;
22536 		ASSERT(tmp_ill != NULL);
22537 		next_ill->ill_usesrc_grp_next = NULL;
22538 		next_ill->ill_usesrc_ifindex = 0;
22539 		next_ill = tmp_ill;
22540 	} while (next_ill->ill_usesrc_ifindex != 0);
22541 	uill->ill_usesrc_grp_next = NULL;
22542 }
22543 
22544 /*
22545  * Remove the client usesrc ILL from the list and relink to a new list
22546  */
22547 int
22548 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22549 {
22550 	ill_t *ill, *tmp_ill;
22551 	ip_stack_t	*ipst = ucill->ill_ipst;
22552 
22553 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22554 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22555 
22556 	/*
22557 	 * Check if the usesrc client ILL passed in is not already
22558 	 * in use as a usesrc ILL i.e one whose source address is
22559 	 * in use OR a usesrc ILL is not already in use as a usesrc
22560 	 * client ILL
22561 	 */
22562 	if ((ucill->ill_usesrc_ifindex == 0) ||
22563 	    (uill->ill_usesrc_ifindex != 0)) {
22564 		return (-1);
22565 	}
22566 
22567 	ill = ill_prev_usesrc(ucill);
22568 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22569 
22570 	/* Remove from the current list */
22571 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22572 		/* Only two elements in the list */
22573 		ASSERT(ill->ill_usesrc_ifindex == 0);
22574 		ill->ill_usesrc_grp_next = NULL;
22575 	} else {
22576 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22577 	}
22578 
22579 	if (ifindex == 0) {
22580 		ucill->ill_usesrc_ifindex = 0;
22581 		ucill->ill_usesrc_grp_next = NULL;
22582 		return (0);
22583 	}
22584 
22585 	ucill->ill_usesrc_ifindex = ifindex;
22586 	tmp_ill = uill->ill_usesrc_grp_next;
22587 	uill->ill_usesrc_grp_next = ucill;
22588 	ucill->ill_usesrc_grp_next =
22589 	    (tmp_ill != NULL) ? tmp_ill : uill;
22590 	return (0);
22591 }
22592 
22593 /*
22594  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22595  * ip.c for locking details.
22596  */
22597 /* ARGSUSED */
22598 int
22599 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22600     ip_ioctl_cmd_t *ipip, void *ifreq)
22601 {
22602 	struct lifreq *lifr = (struct lifreq *)ifreq;
22603 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22604 	    ill_flag_changed = B_FALSE;
22605 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22606 	int err = 0, ret;
22607 	uint_t ifindex;
22608 	phyint_t *us_phyint, *us_cli_phyint;
22609 	ipsq_t *ipsq = NULL;
22610 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22611 
22612 	ASSERT(IAM_WRITER_IPIF(ipif));
22613 	ASSERT(q->q_next == NULL);
22614 	ASSERT(CONN_Q(q));
22615 
22616 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22617 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22618 
22619 	ASSERT(us_cli_phyint != NULL);
22620 
22621 	/*
22622 	 * If the client ILL is being used for IPMP, abort.
22623 	 * Note, this can be done before ipsq_try_enter since we are already
22624 	 * exclusive on this ILL
22625 	 */
22626 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22627 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22628 		return (EINVAL);
22629 	}
22630 
22631 	ifindex = lifr->lifr_index;
22632 	if (ifindex == 0) {
22633 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22634 			/* non usesrc group interface, nothing to reset */
22635 			return (0);
22636 		}
22637 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22638 		/* valid reset request */
22639 		reset_flg = B_TRUE;
22640 	}
22641 
22642 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22643 	    ip_process_ioctl, &err, ipst);
22644 
22645 	if (usesrc_ill == NULL) {
22646 		return (err);
22647 	}
22648 
22649 	/*
22650 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22651 	 * group nor can either of the interfaces be used for standy. So
22652 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22653 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22654 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22655 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22656 	 * the usesrc_cli_ill
22657 	 */
22658 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22659 	    NEW_OP, B_TRUE);
22660 	if (ipsq == NULL) {
22661 		err = EINPROGRESS;
22662 		/* Operation enqueued on the ipsq of the usesrc ILL */
22663 		goto done;
22664 	}
22665 
22666 	/* Check if the usesrc_ill is used for IPMP */
22667 	us_phyint = usesrc_ill->ill_phyint;
22668 	if ((us_phyint->phyint_groupname != NULL) ||
22669 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22670 		err = EINVAL;
22671 		goto done;
22672 	}
22673 
22674 	/*
22675 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22676 	 * already a client then return EINVAL
22677 	 */
22678 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22679 		err = EINVAL;
22680 		goto done;
22681 	}
22682 
22683 	/*
22684 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22685 	 * be then this is a duplicate operation.
22686 	 */
22687 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22688 		err = 0;
22689 		goto done;
22690 	}
22691 
22692 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22693 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22694 	    usesrc_ill->ill_isv6));
22695 
22696 	/*
22697 	 * The next step ensures that no new ires will be created referencing
22698 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22699 	 * we go through an ire walk deleting all ire caches that reference
22700 	 * the client ill. New ires referencing the client ill that are added
22701 	 * to the ire table before the ILL_CHANGING flag is set, will be
22702 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22703 	 * the client ill while the ILL_CHANGING flag is set will be failed
22704 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22705 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22706 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22707 	 * belong to the same usesrc group.
22708 	 */
22709 	mutex_enter(&usesrc_cli_ill->ill_lock);
22710 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22711 	mutex_exit(&usesrc_cli_ill->ill_lock);
22712 	ill_flag_changed = B_TRUE;
22713 
22714 	if (ipif->ipif_isv6)
22715 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22716 		    ALL_ZONES, ipst);
22717 	else
22718 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22719 		    ALL_ZONES, ipst);
22720 
22721 	/*
22722 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22723 	 * and the ill_usesrc_ifindex fields
22724 	 */
22725 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22726 
22727 	if (reset_flg) {
22728 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22729 		if (ret != 0) {
22730 			err = EINVAL;
22731 		}
22732 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22733 		goto done;
22734 	}
22735 
22736 	/*
22737 	 * Four possibilities to consider:
22738 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22739 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22740 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22741 	 * 4. Both are part of their respective usesrc groups
22742 	 */
22743 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22744 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22745 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22746 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22747 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22748 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22749 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22750 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22751 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22752 		/* Insert at head of list */
22753 		usesrc_cli_ill->ill_usesrc_grp_next =
22754 		    usesrc_ill->ill_usesrc_grp_next;
22755 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22756 	} else {
22757 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22758 		    ifindex);
22759 		if (ret != 0)
22760 			err = EINVAL;
22761 	}
22762 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22763 
22764 done:
22765 	if (ill_flag_changed) {
22766 		mutex_enter(&usesrc_cli_ill->ill_lock);
22767 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22768 		mutex_exit(&usesrc_cli_ill->ill_lock);
22769 	}
22770 	if (ipsq != NULL)
22771 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22772 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22773 	ill_refrele(usesrc_ill);
22774 	return (err);
22775 }
22776 
22777 /*
22778  * comparison function used by avl.
22779  */
22780 static int
22781 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22782 {
22783 
22784 	uint_t index;
22785 
22786 	ASSERT(phyip != NULL && index_ptr != NULL);
22787 
22788 	index = *((uint_t *)index_ptr);
22789 	/*
22790 	 * let the phyint with the lowest index be on top.
22791 	 */
22792 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22793 		return (1);
22794 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22795 		return (-1);
22796 	return (0);
22797 }
22798 
22799 /*
22800  * comparison function used by avl.
22801  */
22802 static int
22803 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22804 {
22805 	ill_t *ill;
22806 	int res = 0;
22807 
22808 	ASSERT(phyip != NULL && name_ptr != NULL);
22809 
22810 	if (((phyint_t *)phyip)->phyint_illv4)
22811 		ill = ((phyint_t *)phyip)->phyint_illv4;
22812 	else
22813 		ill = ((phyint_t *)phyip)->phyint_illv6;
22814 	ASSERT(ill != NULL);
22815 
22816 	res = strcmp(ill->ill_name, (char *)name_ptr);
22817 	if (res > 0)
22818 		return (1);
22819 	else if (res < 0)
22820 		return (-1);
22821 	return (0);
22822 }
22823 /*
22824  * This function is called from ill_delete when the ill is being
22825  * unplumbed. We remove the reference from the phyint and we also
22826  * free the phyint when there are no more references to it.
22827  */
22828 static void
22829 ill_phyint_free(ill_t *ill)
22830 {
22831 	phyint_t *phyi;
22832 	phyint_t *next_phyint;
22833 	ipsq_t *cur_ipsq;
22834 	ip_stack_t	*ipst = ill->ill_ipst;
22835 
22836 	ASSERT(ill->ill_phyint != NULL);
22837 
22838 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22839 	phyi = ill->ill_phyint;
22840 	ill->ill_phyint = NULL;
22841 	/*
22842 	 * ill_init allocates a phyint always to store the copy
22843 	 * of flags relevant to phyint. At that point in time, we could
22844 	 * not assign the name and hence phyint_illv4/v6 could not be
22845 	 * initialized. Later in ipif_set_values, we assign the name to
22846 	 * the ill, at which point in time we assign phyint_illv4/v6.
22847 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22848 	 */
22849 	if (ill->ill_flags & ILLF_IPV6) {
22850 		phyi->phyint_illv6 = NULL;
22851 	} else {
22852 		phyi->phyint_illv4 = NULL;
22853 	}
22854 	/*
22855 	 * ipif_down removes it from the group when the last ipif goes
22856 	 * down.
22857 	 */
22858 	ASSERT(ill->ill_group == NULL);
22859 
22860 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22861 		return;
22862 
22863 	/*
22864 	 * Make sure this phyint was put in the list.
22865 	 */
22866 	if (phyi->phyint_ifindex > 0) {
22867 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22868 		    phyi);
22869 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22870 		    phyi);
22871 	}
22872 	/*
22873 	 * remove phyint from the ipsq list.
22874 	 */
22875 	cur_ipsq = phyi->phyint_ipsq;
22876 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22877 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22878 	} else {
22879 		next_phyint = cur_ipsq->ipsq_phyint_list;
22880 		while (next_phyint != NULL) {
22881 			if (next_phyint->phyint_ipsq_next == phyi) {
22882 				next_phyint->phyint_ipsq_next =
22883 					phyi->phyint_ipsq_next;
22884 				break;
22885 			}
22886 			next_phyint = next_phyint->phyint_ipsq_next;
22887 		}
22888 		ASSERT(next_phyint != NULL);
22889 	}
22890 	IPSQ_DEC_REF(cur_ipsq, ipst);
22891 
22892 	if (phyi->phyint_groupname_len != 0) {
22893 		ASSERT(phyi->phyint_groupname != NULL);
22894 		mi_free(phyi->phyint_groupname);
22895 	}
22896 	mi_free(phyi);
22897 }
22898 
22899 /*
22900  * Attach the ill to the phyint structure which can be shared by both
22901  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22902  * function is called from ipif_set_values and ill_lookup_on_name (for
22903  * loopback) where we know the name of the ill. We lookup the ill and if
22904  * there is one present already with the name use that phyint. Otherwise
22905  * reuse the one allocated by ill_init.
22906  */
22907 static void
22908 ill_phyint_reinit(ill_t *ill)
22909 {
22910 	boolean_t isv6 = ill->ill_isv6;
22911 	phyint_t *phyi_old;
22912 	phyint_t *phyi;
22913 	avl_index_t where = 0;
22914 	ill_t	*ill_other = NULL;
22915 	ipsq_t	*ipsq;
22916 	ip_stack_t	*ipst = ill->ill_ipst;
22917 
22918 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22919 
22920 	phyi_old = ill->ill_phyint;
22921 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22922 	    phyi_old->phyint_illv6 == NULL));
22923 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22924 	    phyi_old->phyint_illv4 == NULL));
22925 	ASSERT(phyi_old->phyint_ifindex == 0);
22926 
22927 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22928 	    ill->ill_name, &where);
22929 
22930 	/*
22931 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22932 	 *    the global list of ills. So no other thread could have located
22933 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22934 	 * 2. Now locate the other protocol instance of this ill.
22935 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22936 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22937 	 *    of neither ill can change.
22938 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22939 	 *    other ill.
22940 	 * 5. Release all locks.
22941 	 */
22942 
22943 	/*
22944 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22945 	 * we are initializing IPv4.
22946 	 */
22947 	if (phyi != NULL) {
22948 		ill_other = (isv6) ? phyi->phyint_illv4 :
22949 		    phyi->phyint_illv6;
22950 		ASSERT(ill_other->ill_phyint != NULL);
22951 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22952 		    (!isv6 && ill_other->ill_isv6));
22953 		GRAB_ILL_LOCKS(ill, ill_other);
22954 		/*
22955 		 * We are potentially throwing away phyint_flags which
22956 		 * could be different from the one that we obtain from
22957 		 * ill_other->ill_phyint. But it is okay as we are assuming
22958 		 * that the state maintained within IP is correct.
22959 		 */
22960 		mutex_enter(&phyi->phyint_lock);
22961 		if (isv6) {
22962 			ASSERT(phyi->phyint_illv6 == NULL);
22963 			phyi->phyint_illv6 = ill;
22964 		} else {
22965 			ASSERT(phyi->phyint_illv4 == NULL);
22966 			phyi->phyint_illv4 = ill;
22967 		}
22968 		/*
22969 		 * This is a new ill, currently undergoing SLIFNAME
22970 		 * So we could not have joined an IPMP group until now.
22971 		 */
22972 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22973 		    phyi_old->phyint_groupname == NULL);
22974 
22975 		/*
22976 		 * This phyi_old is going away. Decref ipsq_refs and
22977 		 * assert it is zero. The ipsq itself will be freed in
22978 		 * ipsq_exit
22979 		 */
22980 		ipsq = phyi_old->phyint_ipsq;
22981 		IPSQ_DEC_REF(ipsq, ipst);
22982 		ASSERT(ipsq->ipsq_refs == 0);
22983 		/* Get the singleton phyint out of the ipsq list */
22984 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22985 		ipsq->ipsq_phyint_list = NULL;
22986 		phyi_old->phyint_illv4 = NULL;
22987 		phyi_old->phyint_illv6 = NULL;
22988 		mi_free(phyi_old);
22989 	} else {
22990 		mutex_enter(&ill->ill_lock);
22991 		/*
22992 		 * We don't need to acquire any lock, since
22993 		 * the ill is not yet visible globally  and we
22994 		 * have not yet released the ill_g_lock.
22995 		 */
22996 		phyi = phyi_old;
22997 		mutex_enter(&phyi->phyint_lock);
22998 		/* XXX We need a recovery strategy here. */
22999 		if (!phyint_assign_ifindex(phyi, ipst))
23000 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
23001 
23002 		/* No IPMP group yet, thus the hook uses the ifindex */
23003 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
23004 
23005 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23006 		    (void *)phyi, where);
23007 
23008 		(void) avl_find(&ipst->ips_phyint_g_list->
23009 		    phyint_list_avl_by_index,
23010 		    &phyi->phyint_ifindex, &where);
23011 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23012 		    (void *)phyi, where);
23013 	}
23014 
23015 	/*
23016 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
23017 	 * pending mp is not affected because that is per ill basis.
23018 	 */
23019 	ill->ill_phyint = phyi;
23020 
23021 	/*
23022 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
23023 	 * We do this here as when the first ipif was allocated,
23024 	 * ipif_allocate does not know the right interface index.
23025 	 */
23026 
23027 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
23028 	/*
23029 	 * Now that the phyint's ifindex has been assigned, complete the
23030 	 * remaining
23031 	 */
23032 
23033 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
23034 	if (ill->ill_isv6) {
23035 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
23036 		    ill->ill_phyint->phyint_ifindex;
23037 	}
23038 
23039 	/*
23040 	 * Generate an event within the hooks framework to indicate that
23041 	 * a new interface has just been added to IP.  For this event to
23042 	 * be generated, the network interface must, at least, have an
23043 	 * ifindex assigned to it.
23044 	 *
23045 	 * This needs to be run inside the ill_g_lock perimeter to ensure
23046 	 * that the ordering of delivered events to listeners matches the
23047 	 * order of them in the kernel.
23048 	 *
23049 	 * This function could be called from ill_lookup_on_name. In that case
23050 	 * the interface is loopback "lo", which will not generate a NIC event.
23051 	 */
23052 	if (ill->ill_name_length <= 2 ||
23053 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
23054 		/*
23055 		 * Generate nic plumb event for ill_name even if
23056 		 * ipmp_hook_emulation is set. That avoids generating events
23057 		 * for the ill_names should ipmp_hook_emulation be turned on
23058 		 * later.
23059 		 */
23060 		ill_nic_info_plumb(ill, B_FALSE);
23061 	}
23062 	RELEASE_ILL_LOCKS(ill, ill_other);
23063 	mutex_exit(&phyi->phyint_lock);
23064 }
23065 
23066 /*
23067  * Allocate a NE_PLUMB nic info event and store in the ill.
23068  * If 'group' is set we do it for the group name, otherwise the ill name.
23069  * It will be sent when we leave the ipsq.
23070  */
23071 void
23072 ill_nic_info_plumb(ill_t *ill, boolean_t group)
23073 {
23074 	phyint_t	*phyi = ill->ill_phyint;
23075 	ip_stack_t	*ipst = ill->ill_ipst;
23076 	hook_nic_event_t *info;
23077 	char		*name;
23078 	int		namelen;
23079 
23080 	ASSERT(MUTEX_HELD(&ill->ill_lock));
23081 
23082 	if ((info = ill->ill_nic_event_info) != NULL) {
23083 		ip2dbg(("ill_nic_info_plumb: unexpected nic event %d "
23084 		    "attached for %s\n", info->hne_event,
23085 		    ill->ill_name));
23086 		if (info->hne_data != NULL)
23087 			kmem_free(info->hne_data, info->hne_datalen);
23088 		kmem_free(info, sizeof (hook_nic_event_t));
23089 		ill->ill_nic_event_info = NULL;
23090 	}
23091 
23092 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
23093 	if (info == NULL) {
23094 		ip2dbg(("ill_nic_info_plumb: could not attach PLUMB nic "
23095 		    "event information for %s (ENOMEM)\n",
23096 		    ill->ill_name));
23097 		return;
23098 	}
23099 
23100 	if (group) {
23101 		ASSERT(phyi->phyint_groupname_len != 0);
23102 		namelen = phyi->phyint_groupname_len;
23103 		name = phyi->phyint_groupname;
23104 	} else {
23105 		namelen = ill->ill_name_length;
23106 		name = ill->ill_name;
23107 	}
23108 
23109 	info->hne_nic = phyi->phyint_hook_ifindex;
23110 	info->hne_lif = 0;
23111 	info->hne_event = NE_PLUMB;
23112 	info->hne_family = ill->ill_isv6 ?
23113 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
23114 
23115 	info->hne_data = kmem_alloc(namelen, KM_NOSLEEP);
23116 	if (info->hne_data != NULL) {
23117 		info->hne_datalen = namelen;
23118 		bcopy(name, info->hne_data, info->hne_datalen);
23119 	} else {
23120 		ip2dbg(("ill_nic_info_plumb: could not attach "
23121 		    "name information for PLUMB nic event "
23122 		    "of %s (ENOMEM)\n", name));
23123 		kmem_free(info, sizeof (hook_nic_event_t));
23124 		info = NULL;
23125 	}
23126 	ill->ill_nic_event_info = info;
23127 }
23128 
23129 /*
23130  * Unhook the nic event message from the ill and enqueue it
23131  * into the nic event taskq.
23132  */
23133 void
23134 ill_nic_info_dispatch(ill_t *ill)
23135 {
23136 	hook_nic_event_t *info;
23137 
23138 	ASSERT(MUTEX_HELD(&ill->ill_lock));
23139 
23140 	if ((info = ill->ill_nic_event_info) != NULL) {
23141 		if (ddi_taskq_dispatch(eventq_queue_nic,
23142 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
23143 			ip2dbg(("ill_nic_info_dispatch: "
23144 			    "ddi_taskq_dispatch failed\n"));
23145 			if (info->hne_data != NULL)
23146 				kmem_free(info->hne_data, info->hne_datalen);
23147 			kmem_free(info, sizeof (hook_nic_event_t));
23148 		}
23149 		ill->ill_nic_event_info = NULL;
23150 	}
23151 }
23152 
23153 /*
23154  * Notify any downstream modules of the name of this interface.
23155  * An M_IOCTL is used even though we don't expect a successful reply.
23156  * Any reply message from the driver (presumably an M_IOCNAK) will
23157  * eventually get discarded somewhere upstream.  The message format is
23158  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
23159  * to IP.
23160  */
23161 static void
23162 ip_ifname_notify(ill_t *ill, queue_t *q)
23163 {
23164 	mblk_t *mp1, *mp2;
23165 	struct iocblk *iocp;
23166 	struct lifreq *lifr;
23167 
23168 	mp1 = mkiocb(SIOCSLIFNAME);
23169 	if (mp1 == NULL)
23170 		return;
23171 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
23172 	if (mp2 == NULL) {
23173 		freeb(mp1);
23174 		return;
23175 	}
23176 
23177 	mp1->b_cont = mp2;
23178 	iocp = (struct iocblk *)mp1->b_rptr;
23179 	iocp->ioc_count = sizeof (struct lifreq);
23180 
23181 	lifr = (struct lifreq *)mp2->b_rptr;
23182 	mp2->b_wptr += sizeof (struct lifreq);
23183 	bzero(lifr, sizeof (struct lifreq));
23184 
23185 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
23186 	lifr->lifr_ppa = ill->ill_ppa;
23187 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
23188 
23189 	putnext(q, mp1);
23190 }
23191 
23192 static int
23193 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
23194 {
23195 	int err;
23196 	ip_stack_t	*ipst = ill->ill_ipst;
23197 
23198 	/* Set the obsolete NDD per-interface forwarding name. */
23199 	err = ill_set_ndd_name(ill);
23200 	if (err != 0) {
23201 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
23202 		    err);
23203 	}
23204 
23205 	/* Tell downstream modules where they are. */
23206 	ip_ifname_notify(ill, q);
23207 
23208 	/*
23209 	 * ill_dl_phys returns EINPROGRESS in the usual case.
23210 	 * Error cases are ENOMEM ...
23211 	 */
23212 	err = ill_dl_phys(ill, ipif, mp, q);
23213 
23214 	/*
23215 	 * If there is no IRE expiration timer running, get one started.
23216 	 * igmp and mld timers will be triggered by the first multicast
23217 	 */
23218 	if (ipst->ips_ip_ire_expire_id == 0) {
23219 		/*
23220 		 * acquire the lock and check again.
23221 		 */
23222 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
23223 		if (ipst->ips_ip_ire_expire_id == 0) {
23224 			ipst->ips_ip_ire_expire_id = timeout(
23225 			    ip_trash_timer_expire, ipst,
23226 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
23227 		}
23228 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
23229 	}
23230 
23231 	if (ill->ill_isv6) {
23232 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
23233 		if (ipst->ips_mld_slowtimeout_id == 0) {
23234 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
23235 			    (void *)ipst,
23236 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
23237 		}
23238 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
23239 	} else {
23240 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
23241 		if (ipst->ips_igmp_slowtimeout_id == 0) {
23242 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
23243 				(void *)ipst,
23244 				MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
23245 		}
23246 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
23247 	}
23248 
23249 	return (err);
23250 }
23251 
23252 /*
23253  * Common routine for ppa and ifname setting. Should be called exclusive.
23254  *
23255  * Returns EINPROGRESS when mp has been consumed by queueing it on
23256  * ill_pending_mp and the ioctl will complete in ip_rput.
23257  *
23258  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
23259  * the new name and new ppa in lifr_name and lifr_ppa respectively.
23260  * For SLIFNAME, we pass these values back to the userland.
23261  */
23262 static int
23263 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
23264 {
23265 	ill_t	*ill;
23266 	ipif_t	*ipif;
23267 	ipsq_t	*ipsq;
23268 	char	*ppa_ptr;
23269 	char	*old_ptr;
23270 	char	old_char;
23271 	int	error;
23272 	ip_stack_t	*ipst;
23273 
23274 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
23275 	ASSERT(q->q_next != NULL);
23276 	ASSERT(interf_name != NULL);
23277 
23278 	ill = (ill_t *)q->q_ptr;
23279 	ipst = ill->ill_ipst;
23280 
23281 	ASSERT(ill->ill_ipst != NULL);
23282 	ASSERT(ill->ill_name[0] == '\0');
23283 	ASSERT(IAM_WRITER_ILL(ill));
23284 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
23285 	ASSERT(ill->ill_ppa == UINT_MAX);
23286 
23287 	/* The ppa is sent down by ifconfig or is chosen */
23288 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
23289 		return (EINVAL);
23290 	}
23291 
23292 	/*
23293 	 * make sure ppa passed in is same as ppa in the name.
23294 	 * This check is not made when ppa == UINT_MAX in that case ppa
23295 	 * in the name could be anything. System will choose a ppa and
23296 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
23297 	 */
23298 	if (*new_ppa_ptr != UINT_MAX) {
23299 		/* stoi changes the pointer */
23300 		old_ptr = ppa_ptr;
23301 		/*
23302 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
23303 		 * (they don't have an externally visible ppa).  We assign one
23304 		 * here so that we can manage the interface.  Note that in
23305 		 * the past this value was always 0 for DLPI 1 drivers.
23306 		 */
23307 		if (*new_ppa_ptr == 0)
23308 			*new_ppa_ptr = stoi(&old_ptr);
23309 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
23310 			return (EINVAL);
23311 	}
23312 	/*
23313 	 * terminate string before ppa
23314 	 * save char at that location.
23315 	 */
23316 	old_char = ppa_ptr[0];
23317 	ppa_ptr[0] = '\0';
23318 
23319 	ill->ill_ppa = *new_ppa_ptr;
23320 	/*
23321 	 * Finish as much work now as possible before calling ill_glist_insert
23322 	 * which makes the ill globally visible and also merges it with the
23323 	 * other protocol instance of this phyint. The remaining work is
23324 	 * done after entering the ipsq which may happen sometime later.
23325 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
23326 	 */
23327 	ipif = ill->ill_ipif;
23328 
23329 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
23330 	ipif_assign_seqid(ipif);
23331 
23332 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
23333 		ill->ill_flags |= ILLF_IPV4;
23334 
23335 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
23336 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
23337 
23338 	if (ill->ill_flags & ILLF_IPV6) {
23339 
23340 		ill->ill_isv6 = B_TRUE;
23341 		if (ill->ill_rq != NULL) {
23342 			ill->ill_rq->q_qinfo = &rinit_ipv6;
23343 			ill->ill_wq->q_qinfo = &winit_ipv6;
23344 		}
23345 
23346 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
23347 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
23348 		ipif->ipif_v6src_addr = ipv6_all_zeros;
23349 		ipif->ipif_v6subnet = ipv6_all_zeros;
23350 		ipif->ipif_v6net_mask = ipv6_all_zeros;
23351 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
23352 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
23353 		/*
23354 		 * point-to-point or Non-mulicast capable
23355 		 * interfaces won't do NUD unless explicitly
23356 		 * configured to do so.
23357 		 */
23358 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
23359 		    !(ill->ill_flags & ILLF_MULTICAST)) {
23360 			ill->ill_flags |= ILLF_NONUD;
23361 		}
23362 		/* Make sure IPv4 specific flag is not set on IPv6 if */
23363 		if (ill->ill_flags & ILLF_NOARP) {
23364 			/*
23365 			 * Note: xresolv interfaces will eventually need
23366 			 * NOARP set here as well, but that will require
23367 			 * those external resolvers to have some
23368 			 * knowledge of that flag and act appropriately.
23369 			 * Not to be changed at present.
23370 			 */
23371 			ill->ill_flags &= ~ILLF_NOARP;
23372 		}
23373 		/*
23374 		 * Set the ILLF_ROUTER flag according to the global
23375 		 * IPv6 forwarding policy.
23376 		 */
23377 		if (ipst->ips_ipv6_forward != 0)
23378 			ill->ill_flags |= ILLF_ROUTER;
23379 	} else if (ill->ill_flags & ILLF_IPV4) {
23380 		ill->ill_isv6 = B_FALSE;
23381 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
23382 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
23383 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
23384 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
23385 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
23386 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
23387 		/*
23388 		 * Set the ILLF_ROUTER flag according to the global
23389 		 * IPv4 forwarding policy.
23390 		 */
23391 		if (ipst->ips_ip_g_forward != 0)
23392 			ill->ill_flags |= ILLF_ROUTER;
23393 	}
23394 
23395 	ASSERT(ill->ill_phyint != NULL);
23396 
23397 	/*
23398 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23399 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23400 	 */
23401 	if (!ill_allocate_mibs(ill))
23402 		return (ENOMEM);
23403 
23404 	/*
23405 	 * Pick a default sap until we get the DL_INFO_ACK back from
23406 	 * the driver.
23407 	 */
23408 	if (ill->ill_sap == 0) {
23409 		if (ill->ill_isv6)
23410 			ill->ill_sap  = IP6_DL_SAP;
23411 		else
23412 			ill->ill_sap  = IP_DL_SAP;
23413 	}
23414 
23415 	ill->ill_ifname_pending = 1;
23416 	ill->ill_ifname_pending_err = 0;
23417 
23418 	ill_refhold(ill);
23419 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23420 	if ((error = ill_glist_insert(ill, interf_name,
23421 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23422 		ill->ill_ppa = UINT_MAX;
23423 		ill->ill_name[0] = '\0';
23424 		/*
23425 		 * undo null termination done above.
23426 		 */
23427 		ppa_ptr[0] = old_char;
23428 		rw_exit(&ipst->ips_ill_g_lock);
23429 		ill_refrele(ill);
23430 		return (error);
23431 	}
23432 
23433 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23434 
23435 	/*
23436 	 * When we return the buffer pointed to by interf_name should contain
23437 	 * the same name as in ill_name.
23438 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23439 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23440 	 * so copy full name and update the ppa ptr.
23441 	 * When ppa passed in != UINT_MAX all values are correct just undo
23442 	 * null termination, this saves a bcopy.
23443 	 */
23444 	if (*new_ppa_ptr == UINT_MAX) {
23445 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23446 		*new_ppa_ptr = ill->ill_ppa;
23447 	} else {
23448 		/*
23449 		 * undo null termination done above.
23450 		 */
23451 		ppa_ptr[0] = old_char;
23452 	}
23453 
23454 	/* Let SCTP know about this ILL */
23455 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23456 
23457 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23458 	    B_TRUE);
23459 
23460 	rw_exit(&ipst->ips_ill_g_lock);
23461 	ill_refrele(ill);
23462 	if (ipsq == NULL)
23463 		return (EINPROGRESS);
23464 
23465 	/*
23466 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23467 	 */
23468 	if (ipsq->ipsq_current_ipif == NULL)
23469 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23470 	else
23471 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23472 
23473 	error = ipif_set_values_tail(ill, ipif, mp, q);
23474 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23475 	if (error != 0 && error != EINPROGRESS) {
23476 		/*
23477 		 * restore previous values
23478 		 */
23479 		ill->ill_isv6 = B_FALSE;
23480 	}
23481 	return (error);
23482 }
23483 
23484 
23485 void
23486 ipif_init(ip_stack_t *ipst)
23487 {
23488 	hrtime_t hrt;
23489 	int i;
23490 
23491 	/*
23492 	 * Can't call drv_getparm here as it is too early in the boot.
23493 	 * As we use ipif_src_random just for picking a different
23494 	 * source address everytime, this need not be really random.
23495 	 */
23496 	hrt = gethrtime();
23497 	ipst->ips_ipif_src_random =
23498 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23499 
23500 	for (i = 0; i < MAX_G_HEADS; i++) {
23501 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23502 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23503 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23504 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23505 	}
23506 
23507 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23508 	    ill_phyint_compare_index,
23509 	    sizeof (phyint_t),
23510 	    offsetof(struct phyint, phyint_avl_by_index));
23511 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23512 	    ill_phyint_compare_name,
23513 	    sizeof (phyint_t),
23514 	    offsetof(struct phyint, phyint_avl_by_name));
23515 }
23516 
23517 /*
23518  * This is called by ip_rt_add when src_addr value is other than zero.
23519  * src_addr signifies the source address of the incoming packet. For
23520  * reverse tunnel route we need to create a source addr based routing
23521  * table. This routine creates ip_mrtun_table if it's empty and then
23522  * it adds the route entry hashed by source address. It verifies that
23523  * the outgoing interface is always a non-resolver interface (tunnel).
23524  */
23525 int
23526 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
23527     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func,
23528     ip_stack_t *ipst)
23529 {
23530 	ire_t   *ire;
23531 	ire_t	*save_ire;
23532 	ipif_t  *ipif;
23533 	ill_t   *in_ill = NULL;
23534 	ill_t	*out_ill;
23535 	queue_t	*stq;
23536 	mblk_t	*dlureq_mp;
23537 	int	error;
23538 
23539 	if (ire_arg != NULL)
23540 		*ire_arg = NULL;
23541 	ASSERT(in_src_addr != INADDR_ANY);
23542 
23543 	ipif = ipif_arg;
23544 	if (ipif != NULL) {
23545 		out_ill = ipif->ipif_ill;
23546 	} else {
23547 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
23548 		return (EINVAL);
23549 	}
23550 
23551 	if (src_ipif == NULL) {
23552 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
23553 		return (EINVAL);
23554 	}
23555 	in_ill = src_ipif->ipif_ill;
23556 
23557 	/*
23558 	 * Check for duplicates. We don't need to
23559 	 * match out_ill, because the uniqueness of
23560 	 * a route is only dependent on src_addr and
23561 	 * in_ill.
23562 	 */
23563 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
23564 	if (ire != NULL) {
23565 		ire_refrele(ire);
23566 		return (EEXIST);
23567 	}
23568 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
23569 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
23570 		    ipif->ipif_net_type));
23571 		return (EINVAL);
23572 	}
23573 
23574 	stq = ipif->ipif_wq;
23575 	ASSERT(stq != NULL);
23576 
23577 	/*
23578 	 * The outgoing interface must be non-resolver
23579 	 * interface.
23580 	 */
23581 	dlureq_mp = ill_dlur_gen(NULL,
23582 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
23583 	    out_ill->ill_sap_length);
23584 
23585 	if (dlureq_mp == NULL) {
23586 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
23587 		return (ENOMEM);
23588 	}
23589 
23590 	/* Create the IRE. */
23591 
23592 	ire = ire_create(
23593 	    NULL,				/* Zero dst addr */
23594 	    NULL,				/* Zero mask */
23595 	    NULL,				/* Zero gateway addr */
23596 	    NULL,				/* Zero ipif_src addr */
23597 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
23598 	    &ipif->ipif_mtu,
23599 	    NULL,
23600 	    NULL,				/* rfq */
23601 	    stq,
23602 	    IRE_MIPRTUN,
23603 	    dlureq_mp,
23604 	    ipif,
23605 	    in_ill,
23606 	    0,
23607 	    0,
23608 	    0,
23609 	    flags,
23610 	    &ire_uinfo_null,
23611 	    NULL,
23612 	    NULL,
23613 	    ipst);
23614 
23615 	if (ire == NULL) {
23616 		freeb(dlureq_mp);
23617 		return (ENOMEM);
23618 	}
23619 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
23620 	    ire->ire_type));
23621 	save_ire = ire;
23622 	ASSERT(save_ire != NULL);
23623 	error = ire_add_mrtun(&ire, q, mp, func);
23624 	/*
23625 	 * If ire_add_mrtun() failed, the ire passed in was freed
23626 	 * so there is no need to do so here.
23627 	 */
23628 	if (error != 0) {
23629 		return (error);
23630 	}
23631 
23632 	/* Duplicate check */
23633 	if (ire != save_ire) {
23634 		/* route already exists by now */
23635 		ire_refrele(ire);
23636 		return (EEXIST);
23637 	}
23638 
23639 	if (ire_arg != NULL) {
23640 		/*
23641 		 * Store the ire that was just added. the caller
23642 		 * ip_rts_request responsible for doing ire_refrele()
23643 		 * on it.
23644 		 */
23645 		*ire_arg = ire;
23646 	} else {
23647 		ire_refrele(ire);	/* held in ire_add_mrtun */
23648 	}
23649 
23650 	return (0);
23651 }
23652 
23653 /*
23654  * It is called by ip_rt_delete() only when mipagent requests to delete
23655  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
23656  */
23657 
23658 int
23659 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
23660 {
23661 	ire_t   *ire = NULL;
23662 
23663 	if (in_src_addr == INADDR_ANY)
23664 		return (EINVAL);
23665 	if (src_ipif == NULL)
23666 		return (EINVAL);
23667 
23668 	/* search if this route exists in the ip_mrtun_table */
23669 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
23670 	if (ire == NULL) {
23671 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
23672 		return (ESRCH);
23673 	}
23674 	ire_delete(ire);
23675 	ire_refrele(ire);
23676 	return (0);
23677 }
23678 
23679 /*
23680  * Lookup the ipif corresponding to the onlink destination address. For
23681  * point-to-point interfaces, it matches with remote endpoint destination
23682  * address. For point-to-multipoint interfaces it only tries to match the
23683  * destination with the interface's subnet address. The longest, most specific
23684  * match is found to take care of such rare network configurations like -
23685  * le0: 129.146.1.1/16
23686  * le1: 129.146.2.2/24
23687  * It is used only by SO_DONTROUTE at the moment.
23688  */
23689 ipif_t *
23690 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23691 {
23692 	ipif_t	*ipif, *best_ipif;
23693 	ill_t	*ill;
23694 	ill_walk_context_t ctx;
23695 
23696 	ASSERT(zoneid != ALL_ZONES);
23697 	best_ipif = NULL;
23698 
23699 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23700 	ill = ILL_START_WALK_V4(&ctx, ipst);
23701 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23702 		mutex_enter(&ill->ill_lock);
23703 		for (ipif = ill->ill_ipif; ipif != NULL;
23704 		    ipif = ipif->ipif_next) {
23705 			if (!IPIF_CAN_LOOKUP(ipif))
23706 				continue;
23707 			if (ipif->ipif_zoneid != zoneid &&
23708 			    ipif->ipif_zoneid != ALL_ZONES)
23709 				continue;
23710 			/*
23711 			 * Point-to-point case. Look for exact match with
23712 			 * destination address.
23713 			 */
23714 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23715 				if (ipif->ipif_pp_dst_addr == addr) {
23716 					ipif_refhold_locked(ipif);
23717 					mutex_exit(&ill->ill_lock);
23718 					rw_exit(&ipst->ips_ill_g_lock);
23719 					if (best_ipif != NULL)
23720 						ipif_refrele(best_ipif);
23721 					return (ipif);
23722 				}
23723 			} else if (ipif->ipif_subnet == (addr &
23724 			    ipif->ipif_net_mask)) {
23725 				/*
23726 				 * Point-to-multipoint case. Looping through to
23727 				 * find the most specific match. If there are
23728 				 * multiple best match ipif's then prefer ipif's
23729 				 * that are UP. If there is only one best match
23730 				 * ipif and it is DOWN we must still return it.
23731 				 */
23732 				if ((best_ipif == NULL) ||
23733 				    (ipif->ipif_net_mask >
23734 				    best_ipif->ipif_net_mask) ||
23735 				    ((ipif->ipif_net_mask ==
23736 				    best_ipif->ipif_net_mask) &&
23737 				    ((ipif->ipif_flags & IPIF_UP) &&
23738 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23739 					ipif_refhold_locked(ipif);
23740 					mutex_exit(&ill->ill_lock);
23741 					rw_exit(&ipst->ips_ill_g_lock);
23742 					if (best_ipif != NULL)
23743 						ipif_refrele(best_ipif);
23744 					best_ipif = ipif;
23745 					rw_enter(&ipst->ips_ill_g_lock,
23746 					    RW_READER);
23747 					mutex_enter(&ill->ill_lock);
23748 				}
23749 			}
23750 		}
23751 		mutex_exit(&ill->ill_lock);
23752 	}
23753 	rw_exit(&ipst->ips_ill_g_lock);
23754 	return (best_ipif);
23755 }
23756 
23757 
23758 /*
23759  * Save enough information so that we can recreate the IRE if
23760  * the interface goes down and then up.
23761  */
23762 static void
23763 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23764 {
23765 	mblk_t	*save_mp;
23766 
23767 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23768 	if (save_mp != NULL) {
23769 		ifrt_t	*ifrt;
23770 
23771 		save_mp->b_wptr += sizeof (ifrt_t);
23772 		ifrt = (ifrt_t *)save_mp->b_rptr;
23773 		bzero(ifrt, sizeof (ifrt_t));
23774 		ifrt->ifrt_type = ire->ire_type;
23775 		ifrt->ifrt_addr = ire->ire_addr;
23776 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23777 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23778 		ifrt->ifrt_mask = ire->ire_mask;
23779 		ifrt->ifrt_flags = ire->ire_flags;
23780 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23781 		mutex_enter(&ipif->ipif_saved_ire_lock);
23782 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23783 		ipif->ipif_saved_ire_mp = save_mp;
23784 		ipif->ipif_saved_ire_cnt++;
23785 		mutex_exit(&ipif->ipif_saved_ire_lock);
23786 	}
23787 }
23788 
23789 
23790 static void
23791 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23792 {
23793 	mblk_t	**mpp;
23794 	mblk_t	*mp;
23795 	ifrt_t	*ifrt;
23796 
23797 	/* Remove from ipif_saved_ire_mp list if it is there */
23798 	mutex_enter(&ipif->ipif_saved_ire_lock);
23799 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23800 	    mpp = &(*mpp)->b_cont) {
23801 		/*
23802 		 * On a given ipif, the triple of address, gateway and
23803 		 * mask is unique for each saved IRE (in the case of
23804 		 * ordinary interface routes, the gateway address is
23805 		 * all-zeroes).
23806 		 */
23807 		mp = *mpp;
23808 		ifrt = (ifrt_t *)mp->b_rptr;
23809 		if (ifrt->ifrt_addr == ire->ire_addr &&
23810 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23811 		    ifrt->ifrt_mask == ire->ire_mask) {
23812 			*mpp = mp->b_cont;
23813 			ipif->ipif_saved_ire_cnt--;
23814 			freeb(mp);
23815 			break;
23816 		}
23817 	}
23818 	mutex_exit(&ipif->ipif_saved_ire_lock);
23819 }
23820 
23821 
23822 /*
23823  * IP multirouting broadcast routes handling
23824  * Append CGTP broadcast IREs to regular ones created
23825  * at ifconfig time.
23826  */
23827 static void
23828 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23829 {
23830 	ire_t *ire_prim;
23831 
23832 	ASSERT(ire != NULL);
23833 	ASSERT(ire_dst != NULL);
23834 
23835 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23836 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23837 	if (ire_prim != NULL) {
23838 		/*
23839 		 * We are in the special case of broadcasts for
23840 		 * CGTP. We add an IRE_BROADCAST that holds
23841 		 * the RTF_MULTIRT flag, the destination
23842 		 * address of ire_dst and the low level
23843 		 * info of ire_prim. In other words, CGTP
23844 		 * broadcast is added to the redundant ipif.
23845 		 */
23846 		ipif_t *ipif_prim;
23847 		ire_t  *bcast_ire;
23848 
23849 		ipif_prim = ire_prim->ire_ipif;
23850 
23851 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23852 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23853 		    (void *)ire_dst, (void *)ire_prim,
23854 		    (void *)ipif_prim));
23855 
23856 		bcast_ire = ire_create(
23857 		    (uchar_t *)&ire->ire_addr,
23858 		    (uchar_t *)&ip_g_all_ones,
23859 		    (uchar_t *)&ire_dst->ire_src_addr,
23860 		    (uchar_t *)&ire->ire_gateway_addr,
23861 		    NULL,
23862 		    &ipif_prim->ipif_mtu,
23863 		    NULL,
23864 		    ipif_prim->ipif_rq,
23865 		    ipif_prim->ipif_wq,
23866 		    IRE_BROADCAST,
23867 		    ipif_prim->ipif_bcast_mp,
23868 		    ipif_prim,
23869 		    NULL,
23870 		    0,
23871 		    0,
23872 		    0,
23873 		    ire->ire_flags,
23874 		    &ire_uinfo_null,
23875 		    NULL,
23876 		    NULL,
23877 		    ipst);
23878 
23879 		if (bcast_ire != NULL) {
23880 
23881 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23882 			    B_FALSE) == 0) {
23883 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23884 				    "added bcast_ire %p\n",
23885 				    (void *)bcast_ire));
23886 
23887 				ipif_save_ire(bcast_ire->ire_ipif,
23888 				    bcast_ire);
23889 				ire_refrele(bcast_ire);
23890 			}
23891 		}
23892 		ire_refrele(ire_prim);
23893 	}
23894 }
23895 
23896 
23897 /*
23898  * IP multirouting broadcast routes handling
23899  * Remove the broadcast ire
23900  */
23901 static void
23902 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23903 {
23904 	ire_t *ire_dst;
23905 
23906 	ASSERT(ire != NULL);
23907 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23908 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23909 	if (ire_dst != NULL) {
23910 		ire_t *ire_prim;
23911 
23912 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23913 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23914 		if (ire_prim != NULL) {
23915 			ipif_t *ipif_prim;
23916 			ire_t  *bcast_ire;
23917 
23918 			ipif_prim = ire_prim->ire_ipif;
23919 
23920 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23921 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23922 			    (void *)ire_dst, (void *)ire_prim,
23923 			    (void *)ipif_prim));
23924 
23925 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23926 			    ire->ire_gateway_addr,
23927 			    IRE_BROADCAST,
23928 			    ipif_prim, ALL_ZONES,
23929 			    NULL,
23930 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23931 			    MATCH_IRE_MASK, ipst);
23932 
23933 			if (bcast_ire != NULL) {
23934 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23935 				    "looked up bcast_ire %p\n",
23936 				    (void *)bcast_ire));
23937 				ipif_remove_ire(bcast_ire->ire_ipif,
23938 					bcast_ire);
23939 				ire_delete(bcast_ire);
23940 			}
23941 			ire_refrele(ire_prim);
23942 		}
23943 		ire_refrele(ire_dst);
23944 	}
23945 }
23946 
23947 /*
23948  * IPsec hardware acceleration capabilities related functions.
23949  */
23950 
23951 /*
23952  * Free a per-ill IPsec capabilities structure.
23953  */
23954 static void
23955 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23956 {
23957 	if (capab->auth_hw_algs != NULL)
23958 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23959 	if (capab->encr_hw_algs != NULL)
23960 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23961 	if (capab->encr_algparm != NULL)
23962 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23963 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23964 }
23965 
23966 /*
23967  * Allocate a new per-ill IPsec capabilities structure. This structure
23968  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23969  * an array which specifies, for each algorithm, whether this algorithm
23970  * is supported by the ill or not.
23971  */
23972 static ill_ipsec_capab_t *
23973 ill_ipsec_capab_alloc(void)
23974 {
23975 	ill_ipsec_capab_t *capab;
23976 	uint_t nelems;
23977 
23978 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23979 	if (capab == NULL)
23980 		return (NULL);
23981 
23982 	/* we need one bit per algorithm */
23983 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23984 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23985 
23986 	/* allocate memory to store algorithm flags */
23987 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23988 	if (capab->encr_hw_algs == NULL)
23989 		goto nomem;
23990 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23991 	if (capab->auth_hw_algs == NULL)
23992 		goto nomem;
23993 	/*
23994 	 * Leave encr_algparm NULL for now since we won't need it half
23995 	 * the time
23996 	 */
23997 	return (capab);
23998 
23999 nomem:
24000 	ill_ipsec_capab_free(capab);
24001 	return (NULL);
24002 }
24003 
24004 /*
24005  * Resize capability array.  Since we're exclusive, this is OK.
24006  */
24007 static boolean_t
24008 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
24009 {
24010 	ipsec_capab_algparm_t *nalp, *oalp;
24011 	uint32_t olen, nlen;
24012 
24013 	oalp = capab->encr_algparm;
24014 	olen = capab->encr_algparm_size;
24015 
24016 	if (oalp != NULL) {
24017 		if (algid < capab->encr_algparm_end)
24018 			return (B_TRUE);
24019 	}
24020 
24021 	nlen = (algid + 1) * sizeof (*nalp);
24022 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
24023 	if (nalp == NULL)
24024 		return (B_FALSE);
24025 
24026 	if (oalp != NULL) {
24027 		bcopy(oalp, nalp, olen);
24028 		kmem_free(oalp, olen);
24029 	}
24030 	capab->encr_algparm = nalp;
24031 	capab->encr_algparm_size = nlen;
24032 	capab->encr_algparm_end = algid + 1;
24033 
24034 	return (B_TRUE);
24035 }
24036 
24037 /*
24038  * Compare the capabilities of the specified ill with the protocol
24039  * and algorithms specified by the SA passed as argument.
24040  * If they match, returns B_TRUE, B_FALSE if they do not match.
24041  *
24042  * The ill can be passed as a pointer to it, or by specifying its index
24043  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
24044  *
24045  * Called by ipsec_out_is_accelerated() do decide whether an outbound
24046  * packet is eligible for hardware acceleration, and by
24047  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
24048  * to a particular ill.
24049  */
24050 boolean_t
24051 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
24052     ipsa_t *sa, netstack_t *ns)
24053 {
24054 	boolean_t sa_isv6;
24055 	uint_t algid;
24056 	struct ill_ipsec_capab_s *cpp;
24057 	boolean_t need_refrele = B_FALSE;
24058 	ip_stack_t	*ipst = ns->netstack_ip;
24059 
24060 	if (ill == NULL) {
24061 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
24062 		    NULL, NULL, NULL, ipst);
24063 		if (ill == NULL) {
24064 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
24065 			return (B_FALSE);
24066 		}
24067 		need_refrele = B_TRUE;
24068 	}
24069 
24070 	/*
24071 	 * Use the address length specified by the SA to determine
24072 	 * if it corresponds to a IPv6 address, and fail the matching
24073 	 * if the isv6 flag passed as argument does not match.
24074 	 * Note: this check is used for SADB capability checking before
24075 	 * sending SA information to an ill.
24076 	 */
24077 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
24078 	if (sa_isv6 != ill_isv6)
24079 		/* protocol mismatch */
24080 		goto done;
24081 
24082 	/*
24083 	 * Check if the ill supports the protocol, algorithm(s) and
24084 	 * key size(s) specified by the SA, and get the pointers to
24085 	 * the algorithms supported by the ill.
24086 	 */
24087 	switch (sa->ipsa_type) {
24088 
24089 	case SADB_SATYPE_ESP:
24090 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
24091 			/* ill does not support ESP acceleration */
24092 			goto done;
24093 		cpp = ill->ill_ipsec_capab_esp;
24094 		algid = sa->ipsa_auth_alg;
24095 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
24096 			goto done;
24097 		algid = sa->ipsa_encr_alg;
24098 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
24099 			goto done;
24100 		if (algid < cpp->encr_algparm_end) {
24101 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
24102 			if (sa->ipsa_encrkeybits < alp->minkeylen)
24103 				goto done;
24104 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
24105 				goto done;
24106 		}
24107 		break;
24108 
24109 	case SADB_SATYPE_AH:
24110 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
24111 			/* ill does not support AH acceleration */
24112 			goto done;
24113 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
24114 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
24115 			goto done;
24116 		break;
24117 	}
24118 
24119 	if (need_refrele)
24120 		ill_refrele(ill);
24121 	return (B_TRUE);
24122 done:
24123 	if (need_refrele)
24124 		ill_refrele(ill);
24125 	return (B_FALSE);
24126 }
24127 
24128 
24129 /*
24130  * Add a new ill to the list of IPsec capable ills.
24131  * Called from ill_capability_ipsec_ack() when an ACK was received
24132  * indicating that IPsec hardware processing was enabled for an ill.
24133  *
24134  * ill must point to the ill for which acceleration was enabled.
24135  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
24136  */
24137 static void
24138 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
24139 {
24140 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
24141 	uint_t sa_type;
24142 	uint_t ipproto;
24143 	ip_stack_t	*ipst = ill->ill_ipst;
24144 
24145 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
24146 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
24147 
24148 	switch (dl_cap) {
24149 	case DL_CAPAB_IPSEC_AH:
24150 		sa_type = SADB_SATYPE_AH;
24151 		ills = &ipst->ips_ipsec_capab_ills_ah;
24152 		ipproto = IPPROTO_AH;
24153 		break;
24154 	case DL_CAPAB_IPSEC_ESP:
24155 		sa_type = SADB_SATYPE_ESP;
24156 		ills = &ipst->ips_ipsec_capab_ills_esp;
24157 		ipproto = IPPROTO_ESP;
24158 		break;
24159 	}
24160 
24161 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
24162 
24163 	/*
24164 	 * Add ill index to list of hardware accelerators. If
24165 	 * already in list, do nothing.
24166 	 */
24167 	for (cur_ill = *ills; cur_ill != NULL &&
24168 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
24169 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
24170 		;
24171 
24172 	if (cur_ill == NULL) {
24173 		/* if this is a new entry for this ill */
24174 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
24175 		if (new_ill == NULL) {
24176 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24177 			return;
24178 		}
24179 
24180 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
24181 		new_ill->ill_isv6 = ill->ill_isv6;
24182 		new_ill->next = *ills;
24183 		*ills = new_ill;
24184 	} else if (!sadb_resync) {
24185 		/* not resync'ing SADB and an entry exists for this ill */
24186 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24187 		return;
24188 	}
24189 
24190 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24191 
24192 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
24193 		/*
24194 		 * IPsec module for protocol loaded, initiate dump
24195 		 * of the SADB to this ill.
24196 		 */
24197 		sadb_ill_download(ill, sa_type);
24198 }
24199 
24200 /*
24201  * Remove an ill from the list of IPsec capable ills.
24202  */
24203 static void
24204 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
24205 {
24206 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
24207 	ip_stack_t	*ipst = ill->ill_ipst;
24208 
24209 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
24210 	    dl_cap == DL_CAPAB_IPSEC_ESP);
24211 
24212 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
24213 	    &ipst->ips_ipsec_capab_ills_esp;
24214 
24215 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
24216 
24217 	prev_ill = NULL;
24218 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
24219 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
24220 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
24221 		;
24222 	if (cur_ill == NULL) {
24223 		/* entry not found */
24224 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24225 		return;
24226 	}
24227 	if (prev_ill == NULL) {
24228 		/* entry at front of list */
24229 		*ills = NULL;
24230 	} else {
24231 		prev_ill->next = cur_ill->next;
24232 	}
24233 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
24234 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24235 }
24236 
24237 
24238 /*
24239  * Handling of DL_CONTROL_REQ messages that must be sent down to
24240  * an ill while having exclusive access.
24241  */
24242 /* ARGSUSED */
24243 static void
24244 ill_ipsec_capab_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
24245 {
24246 	ill_t *ill = (ill_t *)q->q_ptr;
24247 
24248 	ill_dlpi_send(ill, mp);
24249 }
24250 
24251 
24252 /*
24253  * Called by SADB to send a DL_CONTROL_REQ message to every ill
24254  * supporting the specified IPsec protocol acceleration.
24255  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
24256  * We free the mblk and, if sa is non-null, release the held referece.
24257  */
24258 void
24259 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
24260     netstack_t *ns)
24261 {
24262 	ipsec_capab_ill_t *ici, *cur_ici;
24263 	ill_t *ill;
24264 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
24265 	ip_stack_t	*ipst = ns->netstack_ip;
24266 
24267 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
24268 	    ipst->ips_ipsec_capab_ills_esp;
24269 
24270 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
24271 
24272 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
24273 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
24274 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
24275 
24276 		/*
24277 		 * Handle the case where the ill goes away while the SADB is
24278 		 * attempting to send messages.  If it's going away, it's
24279 		 * nuking its shadow SADB, so we don't care..
24280 		 */
24281 
24282 		if (ill == NULL)
24283 			continue;
24284 
24285 		if (sa != NULL) {
24286 			/*
24287 			 * Make sure capabilities match before
24288 			 * sending SA to ill.
24289 			 */
24290 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
24291 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
24292 				ill_refrele(ill);
24293 				continue;
24294 			}
24295 
24296 			mutex_enter(&sa->ipsa_lock);
24297 			sa->ipsa_flags |= IPSA_F_HW;
24298 			mutex_exit(&sa->ipsa_lock);
24299 		}
24300 
24301 		/*
24302 		 * Copy template message, and add it to the front
24303 		 * of the mblk ship list. We want to avoid holding
24304 		 * the ipsec_capab_ills_lock while sending the
24305 		 * message to the ills.
24306 		 *
24307 		 * The b_next and b_prev are temporarily used
24308 		 * to build a list of mblks to be sent down, and to
24309 		 * save the ill to which they must be sent.
24310 		 */
24311 		nmp = copymsg(mp);
24312 		if (nmp == NULL) {
24313 			ill_refrele(ill);
24314 			continue;
24315 		}
24316 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
24317 		nmp->b_next = mp_ship_list;
24318 		mp_ship_list = nmp;
24319 		nmp->b_prev = (mblk_t *)ill;
24320 	}
24321 
24322 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24323 
24324 	nmp = mp_ship_list;
24325 	while (nmp != NULL) {
24326 		/* restore the mblk to a sane state */
24327 		next_mp = nmp->b_next;
24328 		nmp->b_next = NULL;
24329 		ill = (ill_t *)nmp->b_prev;
24330 		nmp->b_prev = NULL;
24331 
24332 		/*
24333 		 * Ship the mblk to the ill, must be exclusive. Keep the
24334 		 * reference to the ill as qwriter_ip() does a ill_referele().
24335 		 */
24336 		(void) qwriter_ip(NULL, ill, ill->ill_wq, nmp,
24337 		    ill_ipsec_capab_send_writer, NEW_OP, B_TRUE);
24338 
24339 		nmp = next_mp;
24340 	}
24341 
24342 	if (sa != NULL)
24343 		IPSA_REFRELE(sa);
24344 	freemsg(mp);
24345 }
24346 
24347 
24348 /*
24349  * Derive an interface id from the link layer address.
24350  * Knows about IEEE 802 and IEEE EUI-64 mappings.
24351  */
24352 static boolean_t
24353 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24354 {
24355 	char		*addr;
24356 
24357 	if (phys_length != ETHERADDRL)
24358 		return (B_FALSE);
24359 
24360 	/* Form EUI-64 like address */
24361 	addr = (char *)&v6addr->s6_addr32[2];
24362 	bcopy((char *)phys_addr, addr, 3);
24363 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
24364 	addr[3] = (char)0xff;
24365 	addr[4] = (char)0xfe;
24366 	bcopy((char *)phys_addr + 3, addr + 5, 3);
24367 	return (B_TRUE);
24368 }
24369 
24370 /* ARGSUSED */
24371 static boolean_t
24372 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24373 {
24374 	return (B_FALSE);
24375 }
24376 
24377 /* ARGSUSED */
24378 static boolean_t
24379 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
24380     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
24381 {
24382 	/*
24383 	 * Multicast address mappings used over Ethernet/802.X.
24384 	 * This address is used as a base for mappings.
24385 	 */
24386 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
24387 	    0x00, 0x00, 0x00};
24388 
24389 	/*
24390 	 * Extract low order 32 bits from IPv6 multicast address.
24391 	 * Or that into the link layer address, starting from the
24392 	 * second byte.
24393 	 */
24394 	*hw_start = 2;
24395 	v6_extract_mask->s6_addr32[0] = 0;
24396 	v6_extract_mask->s6_addr32[1] = 0;
24397 	v6_extract_mask->s6_addr32[2] = 0;
24398 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24399 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
24400 	return (B_TRUE);
24401 }
24402 
24403 /*
24404  * Indicate by return value whether multicast is supported. If not,
24405  * this code should not touch/change any parameters.
24406  */
24407 /* ARGSUSED */
24408 static boolean_t
24409 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24410     uint32_t *hw_start, ipaddr_t *extract_mask)
24411 {
24412 	/*
24413 	 * Multicast address mappings used over Ethernet/802.X.
24414 	 * This address is used as a base for mappings.
24415 	 */
24416 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
24417 	    0x00, 0x00, 0x00 };
24418 
24419 	if (phys_length != ETHERADDRL)
24420 		return (B_FALSE);
24421 
24422 	*extract_mask = htonl(0x007fffff);
24423 	*hw_start = 2;
24424 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
24425 	return (B_TRUE);
24426 }
24427 
24428 /*
24429  * Derive IPoIB interface id from the link layer address.
24430  */
24431 static boolean_t
24432 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24433 {
24434 	char		*addr;
24435 
24436 	if (phys_length != 20)
24437 		return (B_FALSE);
24438 	addr = (char *)&v6addr->s6_addr32[2];
24439 	bcopy(phys_addr + 12, addr, 8);
24440 	/*
24441 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
24442 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
24443 	 * rules. In these cases, the IBA considers these GUIDs to be in
24444 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
24445 	 * required; vendors are required not to assign global EUI-64's
24446 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
24447 	 * of the interface identifier. Whether the GUID is in modified
24448 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
24449 	 * bit set to 1.
24450 	 */
24451 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
24452 	return (B_TRUE);
24453 }
24454 
24455 /*
24456  * Note on mapping from multicast IP addresses to IPoIB multicast link
24457  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
24458  * The format of an IPoIB multicast address is:
24459  *
24460  *  4 byte QPN      Scope Sign.  Pkey
24461  * +--------------------------------------------+
24462  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
24463  * +--------------------------------------------+
24464  *
24465  * The Scope and Pkey components are properties of the IBA port and
24466  * network interface. They can be ascertained from the broadcast address.
24467  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
24468  */
24469 
24470 static boolean_t
24471 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
24472     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
24473 {
24474 	/*
24475 	 * Base IPoIB IPv6 multicast address used for mappings.
24476 	 * Does not contain the IBA scope/Pkey values.
24477 	 */
24478 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24479 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
24480 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24481 
24482 	/*
24483 	 * Extract low order 80 bits from IPv6 multicast address.
24484 	 * Or that into the link layer address, starting from the
24485 	 * sixth byte.
24486 	 */
24487 	*hw_start = 6;
24488 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
24489 
24490 	/*
24491 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24492 	 */
24493 	*(maddr + 5) = *(bphys_addr + 5);
24494 	*(maddr + 8) = *(bphys_addr + 8);
24495 	*(maddr + 9) = *(bphys_addr + 9);
24496 
24497 	v6_extract_mask->s6_addr32[0] = 0;
24498 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
24499 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
24500 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24501 	return (B_TRUE);
24502 }
24503 
24504 static boolean_t
24505 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24506     uint32_t *hw_start, ipaddr_t *extract_mask)
24507 {
24508 	/*
24509 	 * Base IPoIB IPv4 multicast address used for mappings.
24510 	 * Does not contain the IBA scope/Pkey values.
24511 	 */
24512 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24513 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
24514 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24515 
24516 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
24517 		return (B_FALSE);
24518 
24519 	/*
24520 	 * Extract low order 28 bits from IPv4 multicast address.
24521 	 * Or that into the link layer address, starting from the
24522 	 * sixteenth byte.
24523 	 */
24524 	*extract_mask = htonl(0x0fffffff);
24525 	*hw_start = 16;
24526 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
24527 
24528 	/*
24529 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24530 	 */
24531 	*(maddr + 5) = *(bphys_addr + 5);
24532 	*(maddr + 8) = *(bphys_addr + 8);
24533 	*(maddr + 9) = *(bphys_addr + 9);
24534 	return (B_TRUE);
24535 }
24536 
24537 /*
24538  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
24539  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
24540  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
24541  * the link-local address is preferred.
24542  */
24543 boolean_t
24544 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24545 {
24546 	ipif_t	*ipif;
24547 	ipif_t	*maybe_ipif = NULL;
24548 
24549 	mutex_enter(&ill->ill_lock);
24550 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24551 		mutex_exit(&ill->ill_lock);
24552 		if (ipifp != NULL)
24553 			*ipifp = NULL;
24554 		return (B_FALSE);
24555 	}
24556 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24557 		if (!IPIF_CAN_LOOKUP(ipif))
24558 			continue;
24559 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
24560 		    ipif->ipif_zoneid != ALL_ZONES)
24561 			continue;
24562 		if ((ipif->ipif_flags & flags) != flags)
24563 			continue;
24564 
24565 		if (ipifp == NULL) {
24566 			mutex_exit(&ill->ill_lock);
24567 			ASSERT(maybe_ipif == NULL);
24568 			return (B_TRUE);
24569 		}
24570 		if (!ill->ill_isv6 ||
24571 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
24572 			ipif_refhold_locked(ipif);
24573 			mutex_exit(&ill->ill_lock);
24574 			*ipifp = ipif;
24575 			return (B_TRUE);
24576 		}
24577 		if (maybe_ipif == NULL)
24578 			maybe_ipif = ipif;
24579 	}
24580 	if (ipifp != NULL) {
24581 		if (maybe_ipif != NULL)
24582 			ipif_refhold_locked(maybe_ipif);
24583 		*ipifp = maybe_ipif;
24584 	}
24585 	mutex_exit(&ill->ill_lock);
24586 	return (maybe_ipif != NULL);
24587 }
24588 
24589 /*
24590  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24591  */
24592 boolean_t
24593 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24594 {
24595 	ill_t *illg;
24596 	ip_stack_t	*ipst = ill->ill_ipst;
24597 
24598 	/*
24599 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24600 	 */
24601 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24602 		return (B_TRUE);
24603 	}
24604 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24605 	if (ill->ill_group == NULL) {
24606 		/* ill not in a group */
24607 		rw_exit(&ipst->ips_ill_g_lock);
24608 		return (B_FALSE);
24609 	}
24610 
24611 	/*
24612 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24613 	 * group. We need to look for an ipif in the zone on all the ills in the
24614 	 * group.
24615 	 */
24616 	illg = ill->ill_group->illgrp_ill;
24617 	do {
24618 		/*
24619 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24620 		 * that it's not there.
24621 		 */
24622 		if (illg != ill &&
24623 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24624 			break;
24625 		}
24626 	} while ((illg = illg->ill_group_next) != NULL);
24627 	rw_exit(&ipst->ips_ill_g_lock);
24628 	return (illg != NULL);
24629 }
24630 
24631 /*
24632  * Check if this ill is only being used to send ICMP probes for IPMP
24633  */
24634 boolean_t
24635 ill_is_probeonly(ill_t *ill)
24636 {
24637 	/*
24638 	 * Check if the interface is FAILED, or INACTIVE
24639 	 */
24640 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24641 		return (B_TRUE);
24642 
24643 	return (B_FALSE);
24644 }
24645 
24646 /*
24647  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24648  * If a pointer to an ipif_t is returned then the caller will need to do
24649  * an ill_refrele().
24650  *
24651  * If there is no real interface which matches the ifindex, then it looks
24652  * for a group that has a matching index. In the case of a group match the
24653  * lifidx must be zero. We don't need emulate the logical interfaces
24654  * since IP Filter's use of netinfo doesn't use that.
24655  */
24656 ipif_t *
24657 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24658     ip_stack_t *ipst)
24659 {
24660 	ipif_t *ipif;
24661 	ill_t *ill;
24662 
24663 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24664 	    ipst);
24665 
24666 	if (ill == NULL) {
24667 		/* Fallback to group names only if hook_emulation set */
24668 		if (!ipst->ips_ipmp_hook_emulation)
24669 			return (NULL);
24670 
24671 		if (lifidx != 0)
24672 			return (NULL);
24673 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24674 		if (ill == NULL)
24675 			return (NULL);
24676 	}
24677 
24678 	mutex_enter(&ill->ill_lock);
24679 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24680 		mutex_exit(&ill->ill_lock);
24681 		ill_refrele(ill);
24682 		return (NULL);
24683 	}
24684 
24685 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24686 		if (!IPIF_CAN_LOOKUP(ipif))
24687 			continue;
24688 		if (lifidx == ipif->ipif_id) {
24689 			ipif_refhold_locked(ipif);
24690 			break;
24691 		}
24692 	}
24693 
24694 	mutex_exit(&ill->ill_lock);
24695 	ill_refrele(ill);
24696 	return (ipif);
24697 }
24698 
24699 /*
24700  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24701  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24702  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24703  * for details.
24704  */
24705 void
24706 ill_fastpath_flush(ill_t *ill)
24707 {
24708 	ip_stack_t *ipst = ill->ill_ipst;
24709 
24710 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24711 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24712 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24713 }
24714 
24715 /*
24716  * Set the physical address information for `ill' to the contents of the
24717  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24718  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24719  * EINPROGRESS will be returned.
24720  */
24721 int
24722 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24723 {
24724 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24725 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24726 
24727 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24728 
24729 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24730 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24731 		/* Changing DL_IPV6_TOKEN is not yet supported */
24732 		return (0);
24733 	}
24734 
24735 	/*
24736 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24737 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24738 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24739 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24740 	 */
24741 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24742 		freemsg(mp);
24743 		return (ENOMEM);
24744 	}
24745 
24746 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24747 
24748 	/*
24749 	 * If we can quiesce the ill, then set the address.  If not, then
24750 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24751 	 */
24752 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24753 	mutex_enter(&ill->ill_lock);
24754 	if (!ill_is_quiescent(ill)) {
24755 		/* call cannot fail since `conn_t *' argument is NULL */
24756 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24757 		    mp, ILL_DOWN);
24758 		mutex_exit(&ill->ill_lock);
24759 		return (EINPROGRESS);
24760 	}
24761 	mutex_exit(&ill->ill_lock);
24762 
24763 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24764 	return (0);
24765 }
24766 
24767 /*
24768  * Once the ill associated with `q' has quiesced, set its physical address
24769  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24770  * are passed (linked by b_cont), since we sometimes need to save two distinct
24771  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24772  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24773  * is quiesced, we know any stale IREs with the old address information have
24774  * already been removed, so we don't need to call ill_fastpath_flush().
24775  */
24776 /* ARGSUSED */
24777 static void
24778 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24779 {
24780 	ill_t		*ill = q->q_ptr;
24781 	mblk_t		*addrmp2 = unlinkb(addrmp);
24782 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24783 	uint_t		addrlen, addroff;
24784 
24785 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24786 
24787 	addroff	= dlindp->dl_addr_offset;
24788 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24789 
24790 	switch (dlindp->dl_data) {
24791 	case DL_IPV6_LINK_LAYER_ADDR:
24792 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24793 		freemsg(addrmp2);
24794 		break;
24795 
24796 	case DL_CURR_PHYS_ADDR:
24797 		freemsg(ill->ill_phys_addr_mp);
24798 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24799 		ill->ill_phys_addr_mp = addrmp;
24800 		ill->ill_phys_addr_length = addrlen;
24801 
24802 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24803 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24804 		else
24805 			freemsg(addrmp2);
24806 		break;
24807 	default:
24808 		ASSERT(0);
24809 	}
24810 
24811 	/*
24812 	 * If there are ipifs to bring up, ill_up_ipifs() will return nonzero,
24813 	 * and ipsq_current_finish() will be called by ip_rput_dlpi_writer()
24814 	 * or ip_arp_done() when the last ipif is brought up.
24815 	 */
24816 	if (ill_up_ipifs(ill, q, addrmp) == 0)
24817 		ipsq_current_finish(ipsq);
24818 }
24819 
24820 /*
24821  * Helper routine for setting the ill_nd_lla fields.
24822  */
24823 void
24824 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24825 {
24826 	freemsg(ill->ill_nd_lla_mp);
24827 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24828 	ill->ill_nd_lla_mp = ndmp;
24829 	ill->ill_nd_lla_len = addrlen;
24830 }
24831 
24832 
24833 
24834 major_t IP_MAJ;
24835 #define	IP	"ip"
24836 
24837 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24838 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24839 
24840 /*
24841  * Issue REMOVEIF ioctls to have the loopback interfaces
24842  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24843  * the former going away when the user-level processes in the zone
24844  * are killed  * and the latter are cleaned up by the stream head
24845  * str_stack_shutdown callback that undoes all I_PLINKs.
24846  */
24847 void
24848 ip_loopback_cleanup(ip_stack_t *ipst)
24849 {
24850 	int error;
24851 	ldi_handle_t	lh = NULL;
24852 	ldi_ident_t	li = NULL;
24853 	int		rval;
24854 	cred_t		*cr;
24855 	struct strioctl iocb;
24856 	struct lifreq	lifreq;
24857 
24858 	IP_MAJ = ddi_name_to_major(IP);
24859 
24860 #ifdef NS_DEBUG
24861 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24862 	    ipst->ips_netstack->netstack_stackid);
24863 #endif
24864 
24865 	bzero(&lifreq, sizeof (lifreq));
24866 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24867 
24868 	error = ldi_ident_from_major(IP_MAJ, &li);
24869 	if (error) {
24870 #ifdef DEBUG
24871 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24872 		    error);
24873 #endif
24874 		return;
24875 	}
24876 
24877 	cr = zone_get_kcred(netstackid_to_zoneid(
24878 		ipst->ips_netstack->netstack_stackid));
24879 	ASSERT(cr != NULL);
24880 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24881 	if (error) {
24882 #ifdef DEBUG
24883 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24884 		    error);
24885 #endif
24886 		goto out;
24887 	}
24888 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24889 	iocb.ic_timout = 15;
24890 	iocb.ic_len = sizeof (lifreq);
24891 	iocb.ic_dp = (char *)&lifreq;
24892 
24893 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24894 	/* LINTED - statement has no consequent */
24895 	if (error) {
24896 #ifdef NS_DEBUG
24897 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24898 		    "UDP6 error %d\n", error);
24899 #endif
24900 	}
24901 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24902 	lh = NULL;
24903 
24904 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24905 	if (error) {
24906 #ifdef NS_DEBUG
24907 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24908 		    error);
24909 #endif
24910 		goto out;
24911 	}
24912 
24913 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24914 	iocb.ic_timout = 15;
24915 	iocb.ic_len = sizeof (lifreq);
24916 	iocb.ic_dp = (char *)&lifreq;
24917 
24918 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24919 	/* LINTED - statement has no consequent */
24920 	if (error) {
24921 #ifdef NS_DEBUG
24922 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24923 		    "UDP error %d\n", error);
24924 #endif
24925 	}
24926 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24927 	lh = NULL;
24928 
24929 out:
24930 	/* Close layered handles */
24931 	if (lh)
24932 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24933 	if (li)
24934 		ldi_ident_release(li);
24935 
24936 	crfree(cr);
24937 }
24938