xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision b02637af6dc592eb1f43cb4c74f06268648dbd2d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the interface control functions for IP.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strlog.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/kstat.h>
44 #include <sys/debug.h>
45 #include <sys/zone.h>
46 #include <sys/sunldi.h>
47 #include <sys/file.h>
48 
49 #include <sys/kmem.h>
50 #include <sys/systm.h>
51 #include <sys/param.h>
52 #include <sys/socket.h>
53 #include <sys/isa_defs.h>
54 #include <net/if.h>
55 #include <net/if_arp.h>
56 #include <net/if_types.h>
57 #include <net/if_dl.h>
58 #include <net/route.h>
59 #include <sys/sockio.h>
60 #include <netinet/in.h>
61 #include <netinet/ip6.h>
62 #include <netinet/icmp6.h>
63 #include <netinet/igmp_var.h>
64 #include <sys/strsun.h>
65 #include <sys/policy.h>
66 #include <sys/ethernet.h>
67 
68 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
69 #include <inet/mi.h>
70 #include <inet/nd.h>
71 #include <inet/arp.h>
72 #include <inet/mib2.h>
73 #include <inet/ip.h>
74 #include <inet/ip6.h>
75 #include <inet/ip6_asp.h>
76 #include <inet/tcp.h>
77 #include <inet/ip_multi.h>
78 #include <inet/ip_ire.h>
79 #include <inet/ip_ftable.h>
80 #include <inet/ip_rts.h>
81 #include <inet/ip_ndp.h>
82 #include <inet/ip_if.h>
83 #include <inet/ip_impl.h>
84 #include <inet/tun.h>
85 #include <inet/sctp_ip.h>
86 #include <inet/ip_netinfo.h>
87 #include <inet/mib2.h>
88 
89 #include <net/pfkeyv2.h>
90 #include <inet/ipsec_info.h>
91 #include <inet/sadb.h>
92 #include <inet/ipsec_impl.h>
93 #include <sys/iphada.h>
94 
95 
96 #include <netinet/igmp.h>
97 #include <inet/ip_listutils.h>
98 #include <inet/ipclassifier.h>
99 #include <sys/mac.h>
100 
101 #include <sys/systeminfo.h>
102 #include <sys/bootconf.h>
103 
104 #include <sys/tsol/tndb.h>
105 #include <sys/tsol/tnet.h>
106 
107 /* The character which tells where the ill_name ends */
108 #define	IPIF_SEPARATOR_CHAR	':'
109 
110 /* IP ioctl function table entry */
111 typedef struct ipft_s {
112 	int	ipft_cmd;
113 	pfi_t	ipft_pfi;
114 	int	ipft_min_size;
115 	int	ipft_flags;
116 } ipft_t;
117 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
118 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
119 
120 typedef struct ip_sock_ar_s {
121 	union {
122 		area_t	ip_sock_area;
123 		ared_t	ip_sock_ared;
124 		areq_t	ip_sock_areq;
125 	} ip_sock_ar_u;
126 	queue_t	*ip_sock_ar_q;
127 } ip_sock_ar_t;
128 
129 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
130 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
131 		    char *value, caddr_t cp, cred_t *ioc_cr);
132 
133 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
134 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
135 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
136     mblk_t *mp, boolean_t need_up);
137 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
140     queue_t *q, mblk_t *mp, boolean_t need_up);
141 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
142     mblk_t *mp, boolean_t need_up);
143 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
144     mblk_t *mp);
145 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
146     queue_t *q, mblk_t *mp, boolean_t need_up);
147 static int	ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp,
148     sin_t *sin, boolean_t x_arp_ioctl, boolean_t if_arp_ioctl);
149 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
150     int ioccmd, struct linkblk *li, boolean_t doconsist);
151 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
152 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
153 static void	ipsq_flush(ill_t *ill);
154 
155 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
156     queue_t *q, mblk_t *mp, boolean_t need_up);
157 static void	ipsq_delete(ipsq_t *);
158 
159 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
160 		    boolean_t initialize);
161 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
162 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
163 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
164 		    boolean_t isv6);
165 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
166 static void	ipif_delete_cache_ire(ire_t *, char *);
167 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
168 static void	ipif_free(ipif_t *ipif);
169 static void	ipif_free_tail(ipif_t *ipif);
170 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
171 static void	ipif_multicast_down(ipif_t *ipif);
172 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
173 static void	ipif_set_default(ipif_t *ipif);
174 static int	ipif_set_values(queue_t *q, mblk_t *mp,
175     char *interf_name, uint_t *ppa);
176 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
177     queue_t *q);
178 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
179     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
180     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
181 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
182 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
183 
184 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
185 static int	ill_arp_off(ill_t *ill);
186 static int	ill_arp_on(ill_t *ill);
187 static void	ill_delete_interface_type(ill_if_t *);
188 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
189 static void	ill_dl_down(ill_t *ill);
190 static void	ill_down(ill_t *ill);
191 static void	ill_downi(ire_t *ire, char *ill_arg);
192 static void	ill_free_mib(ill_t *ill);
193 static void	ill_glist_delete(ill_t *);
194 static boolean_t ill_has_usable_ipif(ill_t *);
195 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
196 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
197 static void	ill_phyint_free(ill_t *ill);
198 static void	ill_phyint_reinit(ill_t *ill);
199 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
200 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
201 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
202 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
203 static void	ill_stq_cache_delete(ire_t *, char *);
204 
205 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
206 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
207 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     in6_addr_t *);
209 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
210     ipaddr_t *);
211 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
212 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
213     in6_addr_t *);
214 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
215     ipaddr_t *);
216 
217 static void	ipif_save_ire(ipif_t *, ire_t *);
218 static void	ipif_remove_ire(ipif_t *, ire_t *);
219 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
220 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
221 
222 /*
223  * Per-ill IPsec capabilities management.
224  */
225 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
226 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
227 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
228 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
229 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
230 static void ill_capability_proto(ill_t *, int, mblk_t *);
231 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
232     boolean_t);
233 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
234 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
235 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
236 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
237 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
238 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
239 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
240 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
241     dl_capability_sub_t *);
242 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
243 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
244 static void ill_capability_lso_reset(ill_t *, mblk_t **);
245 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
246 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
247 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
248 static void	ill_capability_dls_disable(ill_t *);
249 
250 static void	illgrp_cache_delete(ire_t *, char *);
251 static void	illgrp_delete(ill_t *ill);
252 static void	illgrp_reset_schednext(ill_t *ill);
253 
254 static ill_t	*ill_prev_usesrc(ill_t *);
255 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
256 static void	ill_disband_usesrc_group(ill_t *);
257 
258 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
259 
260 /*
261  * if we go over the memory footprint limit more than once in this msec
262  * interval, we'll start pruning aggressively.
263  */
264 int ip_min_frag_prune_time = 0;
265 
266 /*
267  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
268  * and the IPsec DOI
269  */
270 #define	MAX_IPSEC_ALGS	256
271 
272 #define	BITSPERBYTE	8
273 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
274 
275 #define	IPSEC_ALG_ENABLE(algs, algid) \
276 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
277 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
278 
279 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
280 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
281 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
282 
283 typedef uint8_t ipsec_capab_elem_t;
284 
285 /*
286  * Per-algorithm parameters.  Note that at present, only encryption
287  * algorithms have variable keysize (IKE does not provide a way to negotiate
288  * auth algorithm keysize).
289  *
290  * All sizes here are in bits.
291  */
292 typedef struct
293 {
294 	uint16_t	minkeylen;
295 	uint16_t	maxkeylen;
296 } ipsec_capab_algparm_t;
297 
298 /*
299  * Per-ill capabilities.
300  */
301 struct ill_ipsec_capab_s {
302 	ipsec_capab_elem_t *encr_hw_algs;
303 	ipsec_capab_elem_t *auth_hw_algs;
304 	uint32_t algs_size;	/* size of _hw_algs in bytes */
305 	/* algorithm key lengths */
306 	ipsec_capab_algparm_t *encr_algparm;
307 	uint32_t encr_algparm_size;
308 	uint32_t encr_algparm_end;
309 };
310 
311 /*
312  * The field values are larger than strictly necessary for simple
313  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
314  */
315 static area_t	ip_area_template = {
316 	AR_ENTRY_ADD,			/* area_cmd */
317 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
318 					/* area_name_offset */
319 	/* area_name_length temporarily holds this structure length */
320 	sizeof (area_t),			/* area_name_length */
321 	IP_ARP_PROTO_TYPE,		/* area_proto */
322 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
323 	IP_ADDR_LEN,			/* area_proto_addr_length */
324 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
325 					/* area_proto_mask_offset */
326 	0,				/* area_flags */
327 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
328 					/* area_hw_addr_offset */
329 	/* Zero length hw_addr_length means 'use your idea of the address' */
330 	0				/* area_hw_addr_length */
331 };
332 
333 /*
334  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
335  * support
336  */
337 static area_t	ip6_area_template = {
338 	AR_ENTRY_ADD,			/* area_cmd */
339 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
340 					/* area_name_offset */
341 	/* area_name_length temporarily holds this structure length */
342 	sizeof (area_t),			/* area_name_length */
343 	IP_ARP_PROTO_TYPE,		/* area_proto */
344 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
345 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
346 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
347 					/* area_proto_mask_offset */
348 	0,				/* area_flags */
349 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
350 					/* area_hw_addr_offset */
351 	/* Zero length hw_addr_length means 'use your idea of the address' */
352 	0				/* area_hw_addr_length */
353 };
354 
355 static ared_t	ip_ared_template = {
356 	AR_ENTRY_DELETE,
357 	sizeof (ared_t) + IP_ADDR_LEN,
358 	sizeof (ared_t),
359 	IP_ARP_PROTO_TYPE,
360 	sizeof (ared_t),
361 	IP_ADDR_LEN
362 };
363 
364 static ared_t	ip6_ared_template = {
365 	AR_ENTRY_DELETE,
366 	sizeof (ared_t) + IPV6_ADDR_LEN,
367 	sizeof (ared_t),
368 	IP_ARP_PROTO_TYPE,
369 	sizeof (ared_t),
370 	IPV6_ADDR_LEN
371 };
372 
373 /*
374  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
375  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
376  * areq is used).
377  */
378 static areq_t	ip_areq_template = {
379 	AR_ENTRY_QUERY,			/* cmd */
380 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
381 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
382 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
383 	sizeof (areq_t),			/* target addr offset */
384 	IP_ADDR_LEN,			/* target addr_length */
385 	0,				/* flags */
386 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
387 	IP_ADDR_LEN,			/* sender addr length */
388 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
389 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
390 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
391 	/* anything else filled in by the code */
392 };
393 
394 static arc_t	ip_aru_template = {
395 	AR_INTERFACE_UP,
396 	sizeof (arc_t),		/* Name offset */
397 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
398 };
399 
400 static arc_t	ip_ard_template = {
401 	AR_INTERFACE_DOWN,
402 	sizeof (arc_t),		/* Name offset */
403 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
404 };
405 
406 static arc_t	ip_aron_template = {
407 	AR_INTERFACE_ON,
408 	sizeof (arc_t),		/* Name offset */
409 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
410 };
411 
412 static arc_t	ip_aroff_template = {
413 	AR_INTERFACE_OFF,
414 	sizeof (arc_t),		/* Name offset */
415 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
416 };
417 
418 
419 static arma_t	ip_arma_multi_template = {
420 	AR_MAPPING_ADD,
421 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
422 				/* Name offset */
423 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
424 	IP_ARP_PROTO_TYPE,
425 	sizeof (arma_t),			/* proto_addr_offset */
426 	IP_ADDR_LEN,				/* proto_addr_length */
427 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
428 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
429 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
430 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
431 	IP_MAX_HW_LEN,				/* hw_addr_length */
432 	0,					/* hw_mapping_start */
433 };
434 
435 static ipft_t	ip_ioctl_ftbl[] = {
436 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
437 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
438 		IPFT_F_NO_REPLY },
439 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
440 		IPFT_F_NO_REPLY },
441 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
442 	{ 0 }
443 };
444 
445 /* Simple ICMP IP Header Template */
446 static ipha_t icmp_ipha = {
447 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
448 };
449 
450 /* Flag descriptors for ip_ipif_report */
451 static nv_t	ipif_nv_tbl[] = {
452 	{ IPIF_UP,		"UP" },
453 	{ IPIF_BROADCAST,	"BROADCAST" },
454 	{ ILLF_DEBUG,		"DEBUG" },
455 	{ PHYI_LOOPBACK,	"LOOPBACK" },
456 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
457 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
458 	{ PHYI_RUNNING,		"RUNNING" },
459 	{ ILLF_NOARP,		"NOARP" },
460 	{ PHYI_PROMISC,		"PROMISC" },
461 	{ PHYI_ALLMULTI,	"ALLMULTI" },
462 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
463 	{ ILLF_MULTICAST,	"MULTICAST" },
464 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
465 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
466 	{ IPIF_DHCPRUNNING,	"DHCP" },
467 	{ IPIF_PRIVATE,		"PRIVATE" },
468 	{ IPIF_NOXMIT,		"NOXMIT" },
469 	{ IPIF_NOLOCAL,		"NOLOCAL" },
470 	{ IPIF_DEPRECATED,	"DEPRECATED" },
471 	{ IPIF_PREFERRED,	"PREFERRED" },
472 	{ IPIF_TEMPORARY,	"TEMPORARY" },
473 	{ IPIF_ADDRCONF,	"ADDRCONF" },
474 	{ PHYI_VIRTUAL,		"VIRTUAL" },
475 	{ ILLF_ROUTER,		"ROUTER" },
476 	{ ILLF_NONUD,		"NONUD" },
477 	{ IPIF_ANYCAST,		"ANYCAST" },
478 	{ ILLF_NORTEXCH,	"NORTEXCH" },
479 	{ ILLF_IPV4,		"IPV4" },
480 	{ ILLF_IPV6,		"IPV6" },
481 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
482 	{ PHYI_FAILED,		"FAILED" },
483 	{ PHYI_STANDBY,		"STANDBY" },
484 	{ PHYI_INACTIVE,	"INACTIVE" },
485 	{ PHYI_OFFLINE,		"OFFLINE" },
486 };
487 
488 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
489 
490 static ip_m_t	ip_m_tbl[] = {
491 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
492 	    ip_ether_v6intfid },
493 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
494 	    ip_nodef_v6intfid },
495 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
496 	    ip_nodef_v6intfid },
497 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_nodef_v6intfid },
499 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_ether_v6intfid },
501 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
502 	    ip_ib_v6intfid },
503 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
504 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
505 	    ip_nodef_v6intfid }
506 };
507 
508 static ill_t	ill_null;		/* Empty ILL for init. */
509 char	ipif_loopback_name[] = "lo0";
510 static char *ipv4_forward_suffix = ":ip_forwarding";
511 static char *ipv6_forward_suffix = ":ip6_forwarding";
512 static	sin6_t	sin6_null;	/* Zero address for quick clears */
513 static	sin_t	sin_null;	/* Zero address for quick clears */
514 
515 /* When set search for unused ipif_seqid */
516 static ipif_t	ipif_zero;
517 
518 /*
519  * ppa arena is created after these many
520  * interfaces have been plumbed.
521  */
522 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
523 
524 /*
525  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
526  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
527  * set through platform specific code (Niagara/Ontario).
528  */
529 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
530 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
531 
532 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
533 
534 static uint_t
535 ipif_rand(ip_stack_t *ipst)
536 {
537 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
538 	    12345;
539 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
540 }
541 
542 /*
543  * Allocate per-interface mibs.
544  * Returns true if ok. False otherwise.
545  *  ipsq  may not yet be allocated (loopback case ).
546  */
547 static boolean_t
548 ill_allocate_mibs(ill_t *ill)
549 {
550 	/* Already allocated? */
551 	if (ill->ill_ip_mib != NULL) {
552 		if (ill->ill_isv6)
553 			ASSERT(ill->ill_icmp6_mib != NULL);
554 		return (B_TRUE);
555 	}
556 
557 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
558 	    KM_NOSLEEP);
559 	if (ill->ill_ip_mib == NULL) {
560 		return (B_FALSE);
561 	}
562 
563 	/* Setup static information */
564 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
565 	    sizeof (mib2_ipIfStatsEntry_t));
566 	if (ill->ill_isv6) {
567 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
568 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
569 		    sizeof (mib2_ipv6AddrEntry_t));
570 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
571 		    sizeof (mib2_ipv6RouteEntry_t));
572 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
573 		    sizeof (mib2_ipv6NetToMediaEntry_t));
574 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
575 		    sizeof (ipv6_member_t));
576 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
577 		    sizeof (ipv6_grpsrc_t));
578 	} else {
579 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
580 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
581 		    sizeof (mib2_ipAddrEntry_t));
582 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
583 		    sizeof (mib2_ipRouteEntry_t));
584 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
585 		    sizeof (mib2_ipNetToMediaEntry_t));
586 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
587 		    sizeof (ip_member_t));
588 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
589 		    sizeof (ip_grpsrc_t));
590 
591 		/*
592 		 * For a v4 ill, we are done at this point, because per ill
593 		 * icmp mibs are only used for v6.
594 		 */
595 		return (B_TRUE);
596 	}
597 
598 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
599 	    KM_NOSLEEP);
600 	if (ill->ill_icmp6_mib == NULL) {
601 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
602 		ill->ill_ip_mib = NULL;
603 		return (B_FALSE);
604 	}
605 	/* static icmp info */
606 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
607 	    sizeof (mib2_ipv6IfIcmpEntry_t);
608 	/*
609 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
610 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
611 	 * -> ill_phyint_reinit
612 	 */
613 	return (B_TRUE);
614 }
615 
616 /*
617  * Common code for preparation of ARP commands.  Two points to remember:
618  * 	1) The ill_name is tacked on at the end of the allocated space so
619  *	   the templates name_offset field must contain the total space
620  *	   to allocate less the name length.
621  *
622  *	2) The templates name_length field should contain the *template*
623  *	   length.  We use it as a parameter to bcopy() and then write
624  *	   the real ill_name_length into the name_length field of the copy.
625  * (Always called as writer.)
626  */
627 mblk_t *
628 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
629 {
630 	arc_t	*arc = (arc_t *)template;
631 	char	*cp;
632 	int	len;
633 	mblk_t	*mp;
634 	uint_t	name_length = ill->ill_name_length;
635 	uint_t	template_len = arc->arc_name_length;
636 
637 	len = arc->arc_name_offset + name_length;
638 	mp = allocb(len, BPRI_HI);
639 	if (mp == NULL)
640 		return (NULL);
641 	cp = (char *)mp->b_rptr;
642 	mp->b_wptr = (uchar_t *)&cp[len];
643 	if (template_len)
644 		bcopy(template, cp, template_len);
645 	if (len > template_len)
646 		bzero(&cp[template_len], len - template_len);
647 	mp->b_datap->db_type = M_PROTO;
648 
649 	arc = (arc_t *)cp;
650 	arc->arc_name_length = name_length;
651 	cp = (char *)arc + arc->arc_name_offset;
652 	bcopy(ill->ill_name, cp, name_length);
653 
654 	if (addr) {
655 		area_t	*area = (area_t *)mp->b_rptr;
656 
657 		cp = (char *)area + area->area_proto_addr_offset;
658 		bcopy(addr, cp, area->area_proto_addr_length);
659 		if (area->area_cmd == AR_ENTRY_ADD) {
660 			cp = (char *)area;
661 			len = area->area_proto_addr_length;
662 			if (area->area_proto_mask_offset)
663 				cp += area->area_proto_mask_offset;
664 			else
665 				cp += area->area_proto_addr_offset + len;
666 			while (len-- > 0)
667 				*cp++ = (char)~0;
668 		}
669 	}
670 	return (mp);
671 }
672 
673 mblk_t *
674 ipif_area_alloc(ipif_t *ipif)
675 {
676 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
677 	    (char *)&ipif->ipif_lcl_addr));
678 }
679 
680 mblk_t *
681 ipif_ared_alloc(ipif_t *ipif)
682 {
683 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
684 	    (char *)&ipif->ipif_lcl_addr));
685 }
686 
687 mblk_t *
688 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
689 {
690 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
691 	    (char *)&addr));
692 }
693 
694 /*
695  * Completely vaporize a lower level tap and all associated interfaces.
696  * ill_delete is called only out of ip_close when the device control
697  * stream is being closed.
698  */
699 void
700 ill_delete(ill_t *ill)
701 {
702 	ipif_t	*ipif;
703 	ill_t	*prev_ill;
704 	ip_stack_t	*ipst = ill->ill_ipst;
705 
706 	/*
707 	 * ill_delete may be forcibly entering the ipsq. The previous
708 	 * ioctl may not have completed and may need to be aborted.
709 	 * ipsq_flush takes care of it. If we don't need to enter the
710 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
711 	 * ill_delete_tail is sufficient.
712 	 */
713 	ipsq_flush(ill);
714 
715 	/*
716 	 * Nuke all interfaces.  ipif_free will take down the interface,
717 	 * remove it from the list, and free the data structure.
718 	 * Walk down the ipif list and remove the logical interfaces
719 	 * first before removing the main ipif. We can't unplumb
720 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
721 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
722 	 * POINTOPOINT.
723 	 *
724 	 * If ill_ipif was not properly initialized (i.e low on memory),
725 	 * then no interfaces to clean up. In this case just clean up the
726 	 * ill.
727 	 */
728 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
729 		ipif_free(ipif);
730 
731 	/*
732 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
733 	 * So nobody can be using this mp now. Free the mp allocated for
734 	 * honoring ILLF_NOARP
735 	 */
736 	freemsg(ill->ill_arp_on_mp);
737 	ill->ill_arp_on_mp = NULL;
738 
739 	/* Clean up msgs on pending upcalls for mrouted */
740 	reset_mrt_ill(ill);
741 
742 	/*
743 	 * ipif_free -> reset_conn_ipif will remove all multicast
744 	 * references for IPv4. For IPv6, we need to do it here as
745 	 * it points only at ills.
746 	 */
747 	reset_conn_ill(ill);
748 
749 	/*
750 	 * ill_down will arrange to blow off any IRE's dependent on this
751 	 * ILL, and shut down fragmentation reassembly.
752 	 */
753 	ill_down(ill);
754 
755 	/* Let SCTP know, so that it can remove this from its list. */
756 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
757 
758 	/*
759 	 * If an address on this ILL is being used as a source address then
760 	 * clear out the pointers in other ILLs that point to this ILL.
761 	 */
762 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
763 	if (ill->ill_usesrc_grp_next != NULL) {
764 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
765 			ill_disband_usesrc_group(ill);
766 		} else {	/* consumer of the usesrc ILL */
767 			prev_ill = ill_prev_usesrc(ill);
768 			prev_ill->ill_usesrc_grp_next =
769 			    ill->ill_usesrc_grp_next;
770 		}
771 	}
772 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
773 }
774 
775 static void
776 ipif_non_duplicate(ipif_t *ipif)
777 {
778 	ill_t *ill = ipif->ipif_ill;
779 	mutex_enter(&ill->ill_lock);
780 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
781 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
782 		ASSERT(ill->ill_ipif_dup_count > 0);
783 		ill->ill_ipif_dup_count--;
784 	}
785 	mutex_exit(&ill->ill_lock);
786 }
787 
788 /*
789  * ill_delete_tail is called from ip_modclose after all references
790  * to the closing ill are gone. The wait is done in ip_modclose
791  */
792 void
793 ill_delete_tail(ill_t *ill)
794 {
795 	mblk_t	**mpp;
796 	ipif_t	*ipif;
797 	ip_stack_t	*ipst = ill->ill_ipst;
798 
799 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
800 		ipif_non_duplicate(ipif);
801 		ipif_down_tail(ipif);
802 	}
803 
804 	ASSERT(ill->ill_ipif_dup_count == 0 &&
805 	    ill->ill_arp_down_mp == NULL &&
806 	    ill->ill_arp_del_mapping_mp == NULL);
807 
808 	/*
809 	 * If polling capability is enabled (which signifies direct
810 	 * upcall into IP and driver has ill saved as a handle),
811 	 * we need to make sure that unbind has completed before we
812 	 * let the ill disappear and driver no longer has any reference
813 	 * to this ill.
814 	 */
815 	mutex_enter(&ill->ill_lock);
816 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
817 		cv_wait(&ill->ill_cv, &ill->ill_lock);
818 	mutex_exit(&ill->ill_lock);
819 
820 	/*
821 	 * Clean up polling and soft ring capabilities
822 	 */
823 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
824 		ill_capability_dls_disable(ill);
825 
826 	if (ill->ill_net_type != IRE_LOOPBACK)
827 		qprocsoff(ill->ill_rq);
828 
829 	/*
830 	 * We do an ipsq_flush once again now. New messages could have
831 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
832 	 * could also have landed up if an ioctl thread had looked up
833 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
834 	 * enqueued the ioctl when we did the ipsq_flush last time.
835 	 */
836 	ipsq_flush(ill);
837 
838 	/*
839 	 * Free capabilities.
840 	 */
841 	if (ill->ill_ipsec_capab_ah != NULL) {
842 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
843 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
844 		ill->ill_ipsec_capab_ah = NULL;
845 	}
846 
847 	if (ill->ill_ipsec_capab_esp != NULL) {
848 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
849 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
850 		ill->ill_ipsec_capab_esp = NULL;
851 	}
852 
853 	if (ill->ill_mdt_capab != NULL) {
854 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
855 		ill->ill_mdt_capab = NULL;
856 	}
857 
858 	if (ill->ill_hcksum_capab != NULL) {
859 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
860 		ill->ill_hcksum_capab = NULL;
861 	}
862 
863 	if (ill->ill_zerocopy_capab != NULL) {
864 		kmem_free(ill->ill_zerocopy_capab,
865 		    sizeof (ill_zerocopy_capab_t));
866 		ill->ill_zerocopy_capab = NULL;
867 	}
868 
869 	if (ill->ill_lso_capab != NULL) {
870 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
871 		ill->ill_lso_capab = NULL;
872 	}
873 
874 	if (ill->ill_dls_capab != NULL) {
875 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
876 		ill->ill_dls_capab->ill_unbind_conn = NULL;
877 		kmem_free(ill->ill_dls_capab,
878 		    sizeof (ill_dls_capab_t) +
879 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
880 		ill->ill_dls_capab = NULL;
881 	}
882 
883 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
884 
885 	while (ill->ill_ipif != NULL)
886 		ipif_free_tail(ill->ill_ipif);
887 
888 	/*
889 	 * We have removed all references to ilm from conn and the ones joined
890 	 * within the kernel.
891 	 *
892 	 * We don't walk conns, mrts and ires because
893 	 *
894 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
895 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
896 	 *    ill references.
897 	 */
898 	ASSERT(ilm_walk_ill(ill) == 0);
899 	/*
900 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
901 	 * could free the phyint. No more reference to the phyint after this
902 	 * point.
903 	 */
904 	(void) ill_glist_delete(ill);
905 
906 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
907 	if (ill->ill_ndd_name != NULL)
908 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
909 	rw_exit(&ipst->ips_ip_g_nd_lock);
910 
911 
912 	if (ill->ill_frag_ptr != NULL) {
913 		uint_t count;
914 
915 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
916 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
917 		}
918 		mi_free(ill->ill_frag_ptr);
919 		ill->ill_frag_ptr = NULL;
920 		ill->ill_frag_hash_tbl = NULL;
921 	}
922 
923 	freemsg(ill->ill_nd_lla_mp);
924 	/* Free all retained control messages. */
925 	mpp = &ill->ill_first_mp_to_free;
926 	do {
927 		while (mpp[0]) {
928 			mblk_t  *mp;
929 			mblk_t  *mp1;
930 
931 			mp = mpp[0];
932 			mpp[0] = mp->b_next;
933 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
934 				mp1->b_next = NULL;
935 				mp1->b_prev = NULL;
936 			}
937 			freemsg(mp);
938 		}
939 	} while (mpp++ != &ill->ill_last_mp_to_free);
940 
941 	ill_free_mib(ill);
942 	/* Drop refcnt here */
943 	netstack_rele(ill->ill_ipst->ips_netstack);
944 	ill->ill_ipst = NULL;
945 
946 	ILL_TRACE_CLEANUP(ill);
947 }
948 
949 static void
950 ill_free_mib(ill_t *ill)
951 {
952 	ip_stack_t *ipst = ill->ill_ipst;
953 
954 	/*
955 	 * MIB statistics must not be lost, so when an interface
956 	 * goes away the counter values will be added to the global
957 	 * MIBs.
958 	 */
959 	if (ill->ill_ip_mib != NULL) {
960 		if (ill->ill_isv6) {
961 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
962 			    ill->ill_ip_mib);
963 		} else {
964 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
965 			    ill->ill_ip_mib);
966 		}
967 
968 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
969 		ill->ill_ip_mib = NULL;
970 	}
971 	if (ill->ill_icmp6_mib != NULL) {
972 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
973 		    ill->ill_icmp6_mib);
974 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
975 		ill->ill_icmp6_mib = NULL;
976 	}
977 }
978 
979 /*
980  * Concatenate together a physical address and a sap.
981  *
982  * Sap_lengths are interpreted as follows:
983  *   sap_length == 0	==>	no sap
984  *   sap_length > 0	==>	sap is at the head of the dlpi address
985  *   sap_length < 0	==>	sap is at the tail of the dlpi address
986  */
987 static void
988 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
989     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
990 {
991 	uint16_t sap_addr = (uint16_t)sap_src;
992 
993 	if (sap_length == 0) {
994 		if (phys_src == NULL)
995 			bzero(dst, phys_length);
996 		else
997 			bcopy(phys_src, dst, phys_length);
998 	} else if (sap_length < 0) {
999 		if (phys_src == NULL)
1000 			bzero(dst, phys_length);
1001 		else
1002 			bcopy(phys_src, dst, phys_length);
1003 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1004 	} else {
1005 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1006 		if (phys_src == NULL)
1007 			bzero((char *)dst + sap_length, phys_length);
1008 		else
1009 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1010 	}
1011 }
1012 
1013 /*
1014  * Generate a dl_unitdata_req mblk for the device and address given.
1015  * addr_length is the length of the physical portion of the address.
1016  * If addr is NULL include an all zero address of the specified length.
1017  * TRUE? In any case, addr_length is taken to be the entire length of the
1018  * dlpi address, including the absolute value of sap_length.
1019  */
1020 mblk_t *
1021 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1022 		t_scalar_t sap_length)
1023 {
1024 	dl_unitdata_req_t *dlur;
1025 	mblk_t	*mp;
1026 	t_scalar_t	abs_sap_length;		/* absolute value */
1027 
1028 	abs_sap_length = ABS(sap_length);
1029 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1030 	    DL_UNITDATA_REQ);
1031 	if (mp == NULL)
1032 		return (NULL);
1033 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1034 	/* HACK: accomodate incompatible DLPI drivers */
1035 	if (addr_length == 8)
1036 		addr_length = 6;
1037 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1038 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1039 	dlur->dl_priority.dl_min = 0;
1040 	dlur->dl_priority.dl_max = 0;
1041 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1042 	    (uchar_t *)&dlur[1]);
1043 	return (mp);
1044 }
1045 
1046 /*
1047  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1048  * Return an error if we already have 1 or more ioctls in progress.
1049  * This is used only for non-exclusive ioctls. Currently this is used
1050  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1051  * and thus need to use ipsq_pending_mp_add.
1052  */
1053 boolean_t
1054 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1055 {
1056 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1057 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1058 	/*
1059 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1060 	 */
1061 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1062 	    (add_mp->b_datap->db_type == M_IOCTL));
1063 
1064 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1065 	/*
1066 	 * Return error if the conn has started closing. The conn
1067 	 * could have finished cleaning up the pending mp list,
1068 	 * If so we should not add another mp to the list negating
1069 	 * the cleanup.
1070 	 */
1071 	if (connp->conn_state_flags & CONN_CLOSING)
1072 		return (B_FALSE);
1073 	/*
1074 	 * Add the pending mp to the head of the list, chained by b_next.
1075 	 * Note down the conn on which the ioctl request came, in b_prev.
1076 	 * This will be used to later get the conn, when we get a response
1077 	 * on the ill queue, from some other module (typically arp)
1078 	 */
1079 	add_mp->b_next = (void *)ill->ill_pending_mp;
1080 	add_mp->b_queue = CONNP_TO_WQ(connp);
1081 	ill->ill_pending_mp = add_mp;
1082 	if (connp != NULL)
1083 		connp->conn_oper_pending_ill = ill;
1084 	return (B_TRUE);
1085 }
1086 
1087 /*
1088  * Retrieve the ill_pending_mp and return it. We have to walk the list
1089  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1090  */
1091 mblk_t *
1092 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1093 {
1094 	mblk_t	*prev = NULL;
1095 	mblk_t	*curr = NULL;
1096 	uint_t	id;
1097 	conn_t	*connp;
1098 
1099 	/*
1100 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1101 	 * up the pending mp, but it does not know the ioc_id and
1102 	 * passes in a zero for it.
1103 	 */
1104 	mutex_enter(&ill->ill_lock);
1105 	if (ioc_id != 0)
1106 		*connpp = NULL;
1107 
1108 	/* Search the list for the appropriate ioctl based on ioc_id */
1109 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1110 	    prev = curr, curr = curr->b_next) {
1111 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1112 		connp = Q_TO_CONN(curr->b_queue);
1113 		/* Match based on the ioc_id or based on the conn */
1114 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1115 			break;
1116 	}
1117 
1118 	if (curr != NULL) {
1119 		/* Unlink the mblk from the pending mp list */
1120 		if (prev != NULL) {
1121 			prev->b_next = curr->b_next;
1122 		} else {
1123 			ASSERT(ill->ill_pending_mp == curr);
1124 			ill->ill_pending_mp = curr->b_next;
1125 		}
1126 
1127 		/*
1128 		 * conn refcnt must have been bumped up at the start of
1129 		 * the ioctl. So we can safely access the conn.
1130 		 */
1131 		ASSERT(CONN_Q(curr->b_queue));
1132 		*connpp = Q_TO_CONN(curr->b_queue);
1133 		curr->b_next = NULL;
1134 		curr->b_queue = NULL;
1135 	}
1136 
1137 	mutex_exit(&ill->ill_lock);
1138 
1139 	return (curr);
1140 }
1141 
1142 /*
1143  * Add the pending mp to the list. There can be only 1 pending mp
1144  * in the list. Any exclusive ioctl that needs to wait for a response
1145  * from another module or driver needs to use this function to set
1146  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1147  * the other module/driver. This is also used while waiting for the
1148  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1149  */
1150 boolean_t
1151 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1152     int waitfor)
1153 {
1154 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1155 
1156 	ASSERT(IAM_WRITER_IPIF(ipif));
1157 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1158 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1159 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1160 	/*
1161 	 * The caller may be using a different ipif than the one passed into
1162 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1163 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1164 	 * that `ipsq_current_ipif == ipif'.
1165 	 */
1166 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1167 
1168 	/*
1169 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1170 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1171 	 */
1172 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1173 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1174 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1175 
1176 	if (connp != NULL) {
1177 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1178 		/*
1179 		 * Return error if the conn has started closing. The conn
1180 		 * could have finished cleaning up the pending mp list,
1181 		 * If so we should not add another mp to the list negating
1182 		 * the cleanup.
1183 		 */
1184 		if (connp->conn_state_flags & CONN_CLOSING)
1185 			return (B_FALSE);
1186 	}
1187 	mutex_enter(&ipsq->ipsq_lock);
1188 	ipsq->ipsq_pending_ipif = ipif;
1189 	/*
1190 	 * Note down the queue in b_queue. This will be returned by
1191 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1192 	 * the processing
1193 	 */
1194 	add_mp->b_next = NULL;
1195 	add_mp->b_queue = q;
1196 	ipsq->ipsq_pending_mp = add_mp;
1197 	ipsq->ipsq_waitfor = waitfor;
1198 
1199 	if (connp != NULL)
1200 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1201 	mutex_exit(&ipsq->ipsq_lock);
1202 	return (B_TRUE);
1203 }
1204 
1205 /*
1206  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1207  * queued in the list.
1208  */
1209 mblk_t *
1210 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1211 {
1212 	mblk_t	*curr = NULL;
1213 
1214 	mutex_enter(&ipsq->ipsq_lock);
1215 	*connpp = NULL;
1216 	if (ipsq->ipsq_pending_mp == NULL) {
1217 		mutex_exit(&ipsq->ipsq_lock);
1218 		return (NULL);
1219 	}
1220 
1221 	/* There can be only 1 such excl message */
1222 	curr = ipsq->ipsq_pending_mp;
1223 	ASSERT(curr != NULL && curr->b_next == NULL);
1224 	ipsq->ipsq_pending_ipif = NULL;
1225 	ipsq->ipsq_pending_mp = NULL;
1226 	ipsq->ipsq_waitfor = 0;
1227 	mutex_exit(&ipsq->ipsq_lock);
1228 
1229 	if (CONN_Q(curr->b_queue)) {
1230 		/*
1231 		 * This mp did a refhold on the conn, at the start of the ioctl.
1232 		 * So we can safely return a pointer to the conn to the caller.
1233 		 */
1234 		*connpp = Q_TO_CONN(curr->b_queue);
1235 	} else {
1236 		*connpp = NULL;
1237 	}
1238 	curr->b_next = NULL;
1239 	curr->b_prev = NULL;
1240 	return (curr);
1241 }
1242 
1243 /*
1244  * Cleanup the ioctl mp queued in ipsq_pending_mp
1245  * - Called in the ill_delete path
1246  * - Called in the M_ERROR or M_HANGUP path on the ill.
1247  * - Called in the conn close path.
1248  */
1249 boolean_t
1250 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1251 {
1252 	mblk_t	*mp;
1253 	ipsq_t	*ipsq;
1254 	queue_t	*q;
1255 	ipif_t	*ipif;
1256 
1257 	ASSERT(IAM_WRITER_ILL(ill));
1258 	ipsq = ill->ill_phyint->phyint_ipsq;
1259 	mutex_enter(&ipsq->ipsq_lock);
1260 	/*
1261 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1262 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1263 	 * even if it is meant for another ill, since we have to enqueue
1264 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1265 	 * If connp is non-null we are called from the conn close path.
1266 	 */
1267 	mp = ipsq->ipsq_pending_mp;
1268 	if (mp == NULL || (connp != NULL &&
1269 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1270 		mutex_exit(&ipsq->ipsq_lock);
1271 		return (B_FALSE);
1272 	}
1273 	/* Now remove from the ipsq_pending_mp */
1274 	ipsq->ipsq_pending_mp = NULL;
1275 	q = mp->b_queue;
1276 	mp->b_next = NULL;
1277 	mp->b_prev = NULL;
1278 	mp->b_queue = NULL;
1279 
1280 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1281 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1282 	if (ill->ill_move_in_progress) {
1283 		ILL_CLEAR_MOVE(ill);
1284 	} else if (ill->ill_up_ipifs) {
1285 		ill_group_cleanup(ill);
1286 	}
1287 
1288 	ipif = ipsq->ipsq_pending_ipif;
1289 	ipsq->ipsq_pending_ipif = NULL;
1290 	ipsq->ipsq_waitfor = 0;
1291 	ipsq->ipsq_current_ipif = NULL;
1292 	ipsq->ipsq_current_ioctl = 0;
1293 	mutex_exit(&ipsq->ipsq_lock);
1294 
1295 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1296 		if (connp == NULL) {
1297 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1298 		} else {
1299 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1300 			mutex_enter(&ipif->ipif_ill->ill_lock);
1301 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1302 			mutex_exit(&ipif->ipif_ill->ill_lock);
1303 		}
1304 	} else {
1305 		/*
1306 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1307 		 * be just inet_freemsg. we have to restart it
1308 		 * otherwise the thread will be stuck.
1309 		 */
1310 		inet_freemsg(mp);
1311 	}
1312 	return (B_TRUE);
1313 }
1314 
1315 /*
1316  * The ill is closing. Cleanup all the pending mps. Called exclusively
1317  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1318  * knows this ill, and hence nobody can add an mp to this list
1319  */
1320 static void
1321 ill_pending_mp_cleanup(ill_t *ill)
1322 {
1323 	mblk_t	*mp;
1324 	queue_t	*q;
1325 
1326 	ASSERT(IAM_WRITER_ILL(ill));
1327 
1328 	mutex_enter(&ill->ill_lock);
1329 	/*
1330 	 * Every mp on the pending mp list originating from an ioctl
1331 	 * added 1 to the conn refcnt, at the start of the ioctl.
1332 	 * So bump it down now.  See comments in ip_wput_nondata()
1333 	 */
1334 	while (ill->ill_pending_mp != NULL) {
1335 		mp = ill->ill_pending_mp;
1336 		ill->ill_pending_mp = mp->b_next;
1337 		mutex_exit(&ill->ill_lock);
1338 
1339 		q = mp->b_queue;
1340 		ASSERT(CONN_Q(q));
1341 		mp->b_next = NULL;
1342 		mp->b_prev = NULL;
1343 		mp->b_queue = NULL;
1344 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1345 		mutex_enter(&ill->ill_lock);
1346 	}
1347 	ill->ill_pending_ipif = NULL;
1348 
1349 	mutex_exit(&ill->ill_lock);
1350 }
1351 
1352 /*
1353  * Called in the conn close path and ill delete path
1354  */
1355 static void
1356 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1357 {
1358 	ipsq_t	*ipsq;
1359 	mblk_t	*prev;
1360 	mblk_t	*curr;
1361 	mblk_t	*next;
1362 	queue_t	*q;
1363 	mblk_t	*tmp_list = NULL;
1364 
1365 	ASSERT(IAM_WRITER_ILL(ill));
1366 	if (connp != NULL)
1367 		q = CONNP_TO_WQ(connp);
1368 	else
1369 		q = ill->ill_wq;
1370 
1371 	ipsq = ill->ill_phyint->phyint_ipsq;
1372 	/*
1373 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1374 	 * In the case of ioctl from a conn, there can be only 1 mp
1375 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1376 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1377 	 * ioctls meant for this ill form conn's are not flushed. They will
1378 	 * be processed during ipsq_exit and will not find the ill and will
1379 	 * return error.
1380 	 */
1381 	mutex_enter(&ipsq->ipsq_lock);
1382 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1383 	    curr = next) {
1384 		next = curr->b_next;
1385 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1386 			/* Unlink the mblk from the pending mp list */
1387 			if (prev != NULL) {
1388 				prev->b_next = curr->b_next;
1389 			} else {
1390 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1391 				ipsq->ipsq_xopq_mphead = curr->b_next;
1392 			}
1393 			if (ipsq->ipsq_xopq_mptail == curr)
1394 				ipsq->ipsq_xopq_mptail = prev;
1395 			/*
1396 			 * Create a temporary list and release the ipsq lock
1397 			 * New elements are added to the head of the tmp_list
1398 			 */
1399 			curr->b_next = tmp_list;
1400 			tmp_list = curr;
1401 		} else {
1402 			prev = curr;
1403 		}
1404 	}
1405 	mutex_exit(&ipsq->ipsq_lock);
1406 
1407 	while (tmp_list != NULL) {
1408 		curr = tmp_list;
1409 		tmp_list = curr->b_next;
1410 		curr->b_next = NULL;
1411 		curr->b_prev = NULL;
1412 		curr->b_queue = NULL;
1413 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1414 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1415 			    CONN_CLOSE : NO_COPYOUT, NULL);
1416 		} else {
1417 			/*
1418 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1419 			 * this can't be just inet_freemsg. we have to
1420 			 * restart it otherwise the thread will be stuck.
1421 			 */
1422 			inet_freemsg(curr);
1423 		}
1424 	}
1425 }
1426 
1427 /*
1428  * This conn has started closing. Cleanup any pending ioctl from this conn.
1429  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1430  */
1431 void
1432 conn_ioctl_cleanup(conn_t *connp)
1433 {
1434 	mblk_t *curr;
1435 	ipsq_t	*ipsq;
1436 	ill_t	*ill;
1437 	boolean_t refheld;
1438 
1439 	/*
1440 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1441 	 * ioctl has not yet started, the mp is pending in the list headed by
1442 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1443 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1444 	 * is currently executing now the mp is not queued anywhere but
1445 	 * conn_oper_pending_ill is null. The conn close will wait
1446 	 * till the conn_ref drops to zero.
1447 	 */
1448 	mutex_enter(&connp->conn_lock);
1449 	ill = connp->conn_oper_pending_ill;
1450 	if (ill == NULL) {
1451 		mutex_exit(&connp->conn_lock);
1452 		return;
1453 	}
1454 
1455 	curr = ill_pending_mp_get(ill, &connp, 0);
1456 	if (curr != NULL) {
1457 		mutex_exit(&connp->conn_lock);
1458 		CONN_DEC_REF(connp);
1459 		inet_freemsg(curr);
1460 		return;
1461 	}
1462 	/*
1463 	 * We may not be able to refhold the ill if the ill/ipif
1464 	 * is changing. But we need to make sure that the ill will
1465 	 * not vanish. So we just bump up the ill_waiter count.
1466 	 */
1467 	refheld = ill_waiter_inc(ill);
1468 	mutex_exit(&connp->conn_lock);
1469 	if (refheld) {
1470 		if (ipsq_enter(ill, B_TRUE)) {
1471 			ill_waiter_dcr(ill);
1472 			/*
1473 			 * Check whether this ioctl has started and is
1474 			 * pending now in ipsq_pending_mp. If it is not
1475 			 * found there then check whether this ioctl has
1476 			 * not even started and is in the ipsq_xopq list.
1477 			 */
1478 			if (!ipsq_pending_mp_cleanup(ill, connp))
1479 				ipsq_xopq_mp_cleanup(ill, connp);
1480 			ipsq = ill->ill_phyint->phyint_ipsq;
1481 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1482 			return;
1483 		}
1484 	}
1485 
1486 	/*
1487 	 * The ill is also closing and we could not bump up the
1488 	 * ill_waiter_count or we could not enter the ipsq. Leave
1489 	 * the cleanup to ill_delete
1490 	 */
1491 	mutex_enter(&connp->conn_lock);
1492 	while (connp->conn_oper_pending_ill != NULL)
1493 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1494 	mutex_exit(&connp->conn_lock);
1495 	if (refheld)
1496 		ill_waiter_dcr(ill);
1497 }
1498 
1499 /*
1500  * ipcl_walk function for cleaning up conn_*_ill fields.
1501  */
1502 static void
1503 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1504 {
1505 	ill_t	*ill = (ill_t *)arg;
1506 	ire_t	*ire;
1507 
1508 	mutex_enter(&connp->conn_lock);
1509 	if (connp->conn_multicast_ill == ill) {
1510 		/* Revert to late binding */
1511 		connp->conn_multicast_ill = NULL;
1512 		connp->conn_orig_multicast_ifindex = 0;
1513 	}
1514 	if (connp->conn_incoming_ill == ill)
1515 		connp->conn_incoming_ill = NULL;
1516 	if (connp->conn_outgoing_ill == ill)
1517 		connp->conn_outgoing_ill = NULL;
1518 	if (connp->conn_outgoing_pill == ill)
1519 		connp->conn_outgoing_pill = NULL;
1520 	if (connp->conn_nofailover_ill == ill)
1521 		connp->conn_nofailover_ill = NULL;
1522 	if (connp->conn_xmit_if_ill == ill)
1523 		connp->conn_xmit_if_ill = NULL;
1524 	if (connp->conn_ire_cache != NULL) {
1525 		ire = connp->conn_ire_cache;
1526 		/*
1527 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1528 		 * interface X and ipif coming from interface Y, if interface
1529 		 * X and Y are part of the same IPMPgroup. Thus whenever
1530 		 * interface X goes down, remove all references to it by
1531 		 * checking both on ire_ipif and ire_stq.
1532 		 */
1533 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1534 		    (ire->ire_type == IRE_CACHE &&
1535 		    ire->ire_stq == ill->ill_wq)) {
1536 			connp->conn_ire_cache = NULL;
1537 			mutex_exit(&connp->conn_lock);
1538 			ire_refrele_notr(ire);
1539 			return;
1540 		}
1541 	}
1542 	mutex_exit(&connp->conn_lock);
1543 
1544 }
1545 
1546 /* ARGSUSED */
1547 void
1548 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1549 {
1550 	ill_t	*ill = q->q_ptr;
1551 	ipif_t	*ipif;
1552 
1553 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1554 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1555 		ipif_non_duplicate(ipif);
1556 		ipif_down_tail(ipif);
1557 	}
1558 	freemsg(mp);
1559 	ipsq_current_finish(ipsq);
1560 }
1561 
1562 /*
1563  * ill_down_start is called when we want to down this ill and bring it up again
1564  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1565  * all interfaces, but don't tear down any plumbing.
1566  */
1567 boolean_t
1568 ill_down_start(queue_t *q, mblk_t *mp)
1569 {
1570 	ill_t	*ill = q->q_ptr;
1571 	ipif_t	*ipif;
1572 
1573 	ASSERT(IAM_WRITER_ILL(ill));
1574 
1575 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1576 		(void) ipif_down(ipif, NULL, NULL);
1577 
1578 	ill_down(ill);
1579 
1580 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1581 
1582 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1583 
1584 	/*
1585 	 * Atomically test and add the pending mp if references are active.
1586 	 */
1587 	mutex_enter(&ill->ill_lock);
1588 	if (!ill_is_quiescent(ill)) {
1589 		/* call cannot fail since `conn_t *' argument is NULL */
1590 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1591 		    mp, ILL_DOWN);
1592 		mutex_exit(&ill->ill_lock);
1593 		return (B_FALSE);
1594 	}
1595 	mutex_exit(&ill->ill_lock);
1596 	return (B_TRUE);
1597 }
1598 
1599 static void
1600 ill_down(ill_t *ill)
1601 {
1602 	ip_stack_t	*ipst = ill->ill_ipst;
1603 
1604 	/* Blow off any IREs dependent on this ILL. */
1605 	ire_walk(ill_downi, (char *)ill, ipst);
1606 
1607 	/* Remove any conn_*_ill depending on this ill */
1608 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1609 
1610 	if (ill->ill_group != NULL) {
1611 		illgrp_delete(ill);
1612 	}
1613 }
1614 
1615 /*
1616  * ire_walk routine used to delete every IRE that depends on queues
1617  * associated with 'ill'.  (Always called as writer.)
1618  */
1619 static void
1620 ill_downi(ire_t *ire, char *ill_arg)
1621 {
1622 	ill_t	*ill = (ill_t *)ill_arg;
1623 
1624 	/*
1625 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1626 	 * interface X and ipif coming from interface Y, if interface
1627 	 * X and Y are part of the same IPMP group. Thus whenever interface
1628 	 * X goes down, remove all references to it by checking both
1629 	 * on ire_ipif and ire_stq.
1630 	 */
1631 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1632 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1633 		ire_delete(ire);
1634 	}
1635 }
1636 
1637 /*
1638  * Remove ire/nce from the fastpath list.
1639  */
1640 void
1641 ill_fastpath_nack(ill_t *ill)
1642 {
1643 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1644 }
1645 
1646 /* Consume an M_IOCACK of the fastpath probe. */
1647 void
1648 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1649 {
1650 	mblk_t	*mp1 = mp;
1651 
1652 	/*
1653 	 * If this was the first attempt turn on the fastpath probing.
1654 	 */
1655 	mutex_enter(&ill->ill_lock);
1656 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1657 		ill->ill_dlpi_fastpath_state = IDS_OK;
1658 	mutex_exit(&ill->ill_lock);
1659 
1660 	/* Free the M_IOCACK mblk, hold on to the data */
1661 	mp = mp->b_cont;
1662 	freeb(mp1);
1663 	if (mp == NULL)
1664 		return;
1665 	if (mp->b_cont != NULL) {
1666 		/*
1667 		 * Update all IRE's or NCE's that are waiting for
1668 		 * fastpath update.
1669 		 */
1670 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1671 		mp1 = mp->b_cont;
1672 		freeb(mp);
1673 		mp = mp1;
1674 	} else {
1675 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1676 	}
1677 
1678 	freeb(mp);
1679 }
1680 
1681 /*
1682  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1683  * The data portion of the request is a dl_unitdata_req_t template for
1684  * what we would send downstream in the absence of a fastpath confirmation.
1685  */
1686 int
1687 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1688 {
1689 	struct iocblk	*ioc;
1690 	mblk_t	*mp;
1691 
1692 	if (dlur_mp == NULL)
1693 		return (EINVAL);
1694 
1695 	mutex_enter(&ill->ill_lock);
1696 	switch (ill->ill_dlpi_fastpath_state) {
1697 	case IDS_FAILED:
1698 		/*
1699 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1700 		 * support it.
1701 		 */
1702 		mutex_exit(&ill->ill_lock);
1703 		return (ENOTSUP);
1704 	case IDS_UNKNOWN:
1705 		/* This is the first probe */
1706 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1707 		break;
1708 	default:
1709 		break;
1710 	}
1711 	mutex_exit(&ill->ill_lock);
1712 
1713 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1714 		return (EAGAIN);
1715 
1716 	mp->b_cont = copyb(dlur_mp);
1717 	if (mp->b_cont == NULL) {
1718 		freeb(mp);
1719 		return (EAGAIN);
1720 	}
1721 
1722 	ioc = (struct iocblk *)mp->b_rptr;
1723 	ioc->ioc_count = msgdsize(mp->b_cont);
1724 
1725 	putnext(ill->ill_wq, mp);
1726 	return (0);
1727 }
1728 
1729 void
1730 ill_capability_probe(ill_t *ill)
1731 {
1732 	/*
1733 	 * Do so only if negotiation is enabled, capabilities are unknown,
1734 	 * and a capability negotiation is not already in progress.
1735 	 */
1736 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN &&
1737 	    ill->ill_dlpi_capab_state != IDS_RENEG)
1738 		return;
1739 
1740 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1741 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1742 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1743 }
1744 
1745 void
1746 ill_capability_reset(ill_t *ill)
1747 {
1748 	mblk_t *sc_mp = NULL;
1749 	mblk_t *tmp;
1750 
1751 	/*
1752 	 * Note here that we reset the state to UNKNOWN, and later send
1753 	 * down the DL_CAPABILITY_REQ without first setting the state to
1754 	 * INPROGRESS.  We do this in order to distinguish the
1755 	 * DL_CAPABILITY_ACK response which may come back in response to
1756 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1757 	 * also handle the case where the driver doesn't send us back
1758 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1759 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1760 	 * features are turned off until the state reaches IDS_OK.
1761 	 */
1762 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1763 
1764 	/*
1765 	 * Disable sub-capabilities and request a list of sub-capability
1766 	 * messages which will be sent down to the driver.  Each handler
1767 	 * allocates the corresponding dl_capability_sub_t inside an
1768 	 * mblk, and links it to the existing sc_mp mblk, or return it
1769 	 * as sc_mp if it's the first sub-capability (the passed in
1770 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1771 	 * sc_mp will be pulled-up, before passing it downstream.
1772 	 */
1773 	ill_capability_mdt_reset(ill, &sc_mp);
1774 	ill_capability_hcksum_reset(ill, &sc_mp);
1775 	ill_capability_zerocopy_reset(ill, &sc_mp);
1776 	ill_capability_ipsec_reset(ill, &sc_mp);
1777 	ill_capability_dls_reset(ill, &sc_mp);
1778 	ill_capability_lso_reset(ill, &sc_mp);
1779 
1780 	/* Nothing to send down in order to disable the capabilities? */
1781 	if (sc_mp == NULL)
1782 		return;
1783 
1784 	tmp = msgpullup(sc_mp, -1);
1785 	freemsg(sc_mp);
1786 	if ((sc_mp = tmp) == NULL) {
1787 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1788 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1789 		return;
1790 	}
1791 
1792 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1793 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1794 }
1795 
1796 /*
1797  * Request or set new-style hardware capabilities supported by DLS provider.
1798  */
1799 static void
1800 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1801 {
1802 	mblk_t *mp;
1803 	dl_capability_req_t *capb;
1804 	size_t size = 0;
1805 	uint8_t *ptr;
1806 
1807 	if (reqp != NULL)
1808 		size = MBLKL(reqp);
1809 
1810 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1811 	if (mp == NULL) {
1812 		freemsg(reqp);
1813 		return;
1814 	}
1815 	ptr = mp->b_rptr;
1816 
1817 	capb = (dl_capability_req_t *)ptr;
1818 	ptr += sizeof (dl_capability_req_t);
1819 
1820 	if (reqp != NULL) {
1821 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1822 		capb->dl_sub_length = size;
1823 		bcopy(reqp->b_rptr, ptr, size);
1824 		ptr += size;
1825 		mp->b_cont = reqp->b_cont;
1826 		freeb(reqp);
1827 	}
1828 	ASSERT(ptr == mp->b_wptr);
1829 
1830 	ill_dlpi_send(ill, mp);
1831 }
1832 
1833 static void
1834 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1835 {
1836 	dl_capab_id_t *id_ic;
1837 	uint_t sub_dl_cap = outers->dl_cap;
1838 	dl_capability_sub_t *inners;
1839 	uint8_t *capend;
1840 
1841 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1842 
1843 	/*
1844 	 * Note: range checks here are not absolutely sufficient to
1845 	 * make us robust against malformed messages sent by drivers;
1846 	 * this is in keeping with the rest of IP's dlpi handling.
1847 	 * (Remember, it's coming from something else in the kernel
1848 	 * address space)
1849 	 */
1850 
1851 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1852 	if (capend > mp->b_wptr) {
1853 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1854 		    "malformed sub-capability too long for mblk");
1855 		return;
1856 	}
1857 
1858 	id_ic = (dl_capab_id_t *)(outers + 1);
1859 
1860 	if (outers->dl_length < sizeof (*id_ic) ||
1861 	    (inners = &id_ic->id_subcap,
1862 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1863 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1864 		    "encapsulated capab type %d too long for mblk",
1865 		    inners->dl_cap);
1866 		return;
1867 	}
1868 
1869 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1870 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1871 		    "isn't as expected; pass-thru module(s) detected, "
1872 		    "discarding capability\n", inners->dl_cap));
1873 		return;
1874 	}
1875 
1876 	/* Process the encapsulated sub-capability */
1877 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1878 }
1879 
1880 /*
1881  * Process Multidata Transmit capability negotiation ack received from a
1882  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1883  * DL_CAPABILITY_ACK message.
1884  */
1885 static void
1886 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1887 {
1888 	mblk_t *nmp = NULL;
1889 	dl_capability_req_t *oc;
1890 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1891 	ill_mdt_capab_t **ill_mdt_capab;
1892 	uint_t sub_dl_cap = isub->dl_cap;
1893 	uint8_t *capend;
1894 
1895 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1896 
1897 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1898 
1899 	/*
1900 	 * Note: range checks here are not absolutely sufficient to
1901 	 * make us robust against malformed messages sent by drivers;
1902 	 * this is in keeping with the rest of IP's dlpi handling.
1903 	 * (Remember, it's coming from something else in the kernel
1904 	 * address space)
1905 	 */
1906 
1907 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1908 	if (capend > mp->b_wptr) {
1909 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1910 		    "malformed sub-capability too long for mblk");
1911 		return;
1912 	}
1913 
1914 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1915 
1916 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1917 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1918 		    "unsupported MDT sub-capability (version %d, expected %d)",
1919 		    mdt_ic->mdt_version, MDT_VERSION_2);
1920 		return;
1921 	}
1922 
1923 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1924 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1925 		    "capability isn't as expected; pass-thru module(s) "
1926 		    "detected, discarding capability\n"));
1927 		return;
1928 	}
1929 
1930 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1931 
1932 		if (*ill_mdt_capab == NULL) {
1933 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1934 			    KM_NOSLEEP);
1935 
1936 			if (*ill_mdt_capab == NULL) {
1937 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1938 				    "could not enable MDT version %d "
1939 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1940 				    ill->ill_name);
1941 				return;
1942 			}
1943 		}
1944 
1945 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1946 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1947 		    "header spaces, %d max pld bufs, %d span limit)\n",
1948 		    ill->ill_name, MDT_VERSION_2,
1949 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1950 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1951 
1952 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1953 		(*ill_mdt_capab)->ill_mdt_on = 1;
1954 		/*
1955 		 * Round the following values to the nearest 32-bit; ULP
1956 		 * may further adjust them to accomodate for additional
1957 		 * protocol headers.  We pass these values to ULP during
1958 		 * bind time.
1959 		 */
1960 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1961 		    roundup(mdt_ic->mdt_hdr_head, 4);
1962 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1963 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1964 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1965 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1966 
1967 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1968 	} else {
1969 		uint_t size;
1970 		uchar_t *rptr;
1971 
1972 		size = sizeof (dl_capability_req_t) +
1973 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1974 
1975 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1976 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1977 			    "could not enable MDT for %s (ENOMEM)\n",
1978 			    ill->ill_name);
1979 			return;
1980 		}
1981 
1982 		rptr = nmp->b_rptr;
1983 		/* initialize dl_capability_req_t */
1984 		oc = (dl_capability_req_t *)nmp->b_rptr;
1985 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1986 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1987 		    sizeof (dl_capab_mdt_t);
1988 		nmp->b_rptr += sizeof (dl_capability_req_t);
1989 
1990 		/* initialize dl_capability_sub_t */
1991 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1992 		nmp->b_rptr += sizeof (*isub);
1993 
1994 		/* initialize dl_capab_mdt_t */
1995 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
1996 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
1997 
1998 		nmp->b_rptr = rptr;
1999 
2000 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2001 		    "to enable MDT version %d\n", ill->ill_name,
2002 		    MDT_VERSION_2));
2003 
2004 		/* set ENABLE flag */
2005 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2006 
2007 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2008 		ill_dlpi_send(ill, nmp);
2009 	}
2010 }
2011 
2012 static void
2013 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2014 {
2015 	mblk_t *mp;
2016 	dl_capab_mdt_t *mdt_subcap;
2017 	dl_capability_sub_t *dl_subcap;
2018 	int size;
2019 
2020 	if (!ILL_MDT_CAPABLE(ill))
2021 		return;
2022 
2023 	ASSERT(ill->ill_mdt_capab != NULL);
2024 	/*
2025 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2026 	 * structure since it's possible that another thread is still
2027 	 * referring to it.  The structure only gets deallocated when
2028 	 * we destroy the ill.
2029 	 */
2030 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2031 
2032 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2033 
2034 	mp = allocb(size, BPRI_HI);
2035 	if (mp == NULL) {
2036 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2037 		    "request to disable MDT\n"));
2038 		return;
2039 	}
2040 
2041 	mp->b_wptr = mp->b_rptr + size;
2042 
2043 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2044 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2045 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2046 
2047 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2048 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2049 	mdt_subcap->mdt_flags = 0;
2050 	mdt_subcap->mdt_hdr_head = 0;
2051 	mdt_subcap->mdt_hdr_tail = 0;
2052 
2053 	if (*sc_mp != NULL)
2054 		linkb(*sc_mp, mp);
2055 	else
2056 		*sc_mp = mp;
2057 }
2058 
2059 /*
2060  * Send a DL_NOTIFY_REQ to the specified ill to enable
2061  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2062  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2063  * acceleration.
2064  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2065  */
2066 static boolean_t
2067 ill_enable_promisc_notify(ill_t *ill)
2068 {
2069 	mblk_t *mp;
2070 	dl_notify_req_t *req;
2071 
2072 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2073 
2074 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2075 	if (mp == NULL)
2076 		return (B_FALSE);
2077 
2078 	req = (dl_notify_req_t *)mp->b_rptr;
2079 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2080 	    DL_NOTE_PROMISC_OFF_PHYS;
2081 
2082 	ill_dlpi_send(ill, mp);
2083 
2084 	return (B_TRUE);
2085 }
2086 
2087 
2088 /*
2089  * Allocate an IPsec capability request which will be filled by our
2090  * caller to turn on support for one or more algorithms.
2091  */
2092 static mblk_t *
2093 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2094 {
2095 	mblk_t *nmp;
2096 	dl_capability_req_t	*ocap;
2097 	dl_capab_ipsec_t	*ocip;
2098 	dl_capab_ipsec_t	*icip;
2099 	uint8_t			*ptr;
2100 	icip = (dl_capab_ipsec_t *)(isub + 1);
2101 
2102 	/*
2103 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2104 	 * PROMISC_ON/OFF notification from the provider. We need to
2105 	 * do this before enabling the algorithms to avoid leakage of
2106 	 * cleartext packets.
2107 	 */
2108 
2109 	if (!ill_enable_promisc_notify(ill))
2110 		return (NULL);
2111 
2112 	/*
2113 	 * Allocate new mblk which will contain a new capability
2114 	 * request to enable the capabilities.
2115 	 */
2116 
2117 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2118 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2119 	if (nmp == NULL)
2120 		return (NULL);
2121 
2122 	ptr = nmp->b_rptr;
2123 
2124 	/* initialize dl_capability_req_t */
2125 	ocap = (dl_capability_req_t *)ptr;
2126 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2127 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2128 	ptr += sizeof (dl_capability_req_t);
2129 
2130 	/* initialize dl_capability_sub_t */
2131 	bcopy(isub, ptr, sizeof (*isub));
2132 	ptr += sizeof (*isub);
2133 
2134 	/* initialize dl_capab_ipsec_t */
2135 	ocip = (dl_capab_ipsec_t *)ptr;
2136 	bcopy(icip, ocip, sizeof (*icip));
2137 
2138 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2139 	return (nmp);
2140 }
2141 
2142 /*
2143  * Process an IPsec capability negotiation ack received from a DLS Provider.
2144  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2145  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2146  */
2147 static void
2148 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2149 {
2150 	dl_capab_ipsec_t	*icip;
2151 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2152 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2153 	uint_t cipher, nciphers;
2154 	mblk_t *nmp;
2155 	uint_t alg_len;
2156 	boolean_t need_sadb_dump;
2157 	uint_t sub_dl_cap = isub->dl_cap;
2158 	ill_ipsec_capab_t **ill_capab;
2159 	uint64_t ill_capab_flag;
2160 	uint8_t *capend, *ciphend;
2161 	boolean_t sadb_resync;
2162 
2163 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2164 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2165 
2166 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2167 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2168 		ill_capab_flag = ILL_CAPAB_AH;
2169 	} else {
2170 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2171 		ill_capab_flag = ILL_CAPAB_ESP;
2172 	}
2173 
2174 	/*
2175 	 * If the ill capability structure exists, then this incoming
2176 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2177 	 * If this is so, then we'd need to resynchronize the SADB
2178 	 * after re-enabling the offloaded ciphers.
2179 	 */
2180 	sadb_resync = (*ill_capab != NULL);
2181 
2182 	/*
2183 	 * Note: range checks here are not absolutely sufficient to
2184 	 * make us robust against malformed messages sent by drivers;
2185 	 * this is in keeping with the rest of IP's dlpi handling.
2186 	 * (Remember, it's coming from something else in the kernel
2187 	 * address space)
2188 	 */
2189 
2190 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2191 	if (capend > mp->b_wptr) {
2192 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2193 		    "malformed sub-capability too long for mblk");
2194 		return;
2195 	}
2196 
2197 	/*
2198 	 * There are two types of acks we process here:
2199 	 * 1. acks in reply to a (first form) generic capability req
2200 	 *    (no ENABLE flag set)
2201 	 * 2. acks in reply to a ENABLE capability req.
2202 	 *    (ENABLE flag set)
2203 	 *
2204 	 * We process the subcapability passed as argument as follows:
2205 	 * 1 do initializations
2206 	 *   1.1 initialize nmp = NULL
2207 	 *   1.2 set need_sadb_dump to B_FALSE
2208 	 * 2 for each cipher in subcapability:
2209 	 *   2.1 if ENABLE flag is set:
2210 	 *	2.1.1 update per-ill ipsec capabilities info
2211 	 *	2.1.2 set need_sadb_dump to B_TRUE
2212 	 *   2.2 if ENABLE flag is not set:
2213 	 *	2.2.1 if nmp is NULL:
2214 	 *		2.2.1.1 allocate and initialize nmp
2215 	 *		2.2.1.2 init current pos in nmp
2216 	 *	2.2.2 copy current cipher to current pos in nmp
2217 	 *	2.2.3 set ENABLE flag in nmp
2218 	 *	2.2.4 update current pos
2219 	 * 3 if nmp is not equal to NULL, send enable request
2220 	 *   3.1 send capability request
2221 	 * 4 if need_sadb_dump is B_TRUE
2222 	 *   4.1 enable promiscuous on/off notifications
2223 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2224 	 *	AH or ESP SA's to interface.
2225 	 */
2226 
2227 	nmp = NULL;
2228 	oalg = NULL;
2229 	need_sadb_dump = B_FALSE;
2230 	icip = (dl_capab_ipsec_t *)(isub + 1);
2231 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2232 
2233 	nciphers = icip->cip_nciphers;
2234 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2235 
2236 	if (ciphend > capend) {
2237 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2238 		    "too many ciphers for sub-capability len");
2239 		return;
2240 	}
2241 
2242 	for (cipher = 0; cipher < nciphers; cipher++) {
2243 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2244 
2245 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2246 			/*
2247 			 * TBD: when we provide a way to disable capabilities
2248 			 * from above, need to manage the request-pending state
2249 			 * and fail if we were not expecting this ACK.
2250 			 */
2251 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2252 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2253 
2254 			/*
2255 			 * Update IPsec capabilities for this ill
2256 			 */
2257 
2258 			if (*ill_capab == NULL) {
2259 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2260 				    ("ill_capability_ipsec_ack: "
2261 				    "allocating ipsec_capab for ill\n"));
2262 				*ill_capab = ill_ipsec_capab_alloc();
2263 
2264 				if (*ill_capab == NULL) {
2265 					cmn_err(CE_WARN,
2266 					    "ill_capability_ipsec_ack: "
2267 					    "could not enable IPsec Hardware "
2268 					    "acceleration for %s (ENOMEM)\n",
2269 					    ill->ill_name);
2270 					return;
2271 				}
2272 			}
2273 
2274 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2275 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2276 
2277 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2278 				cmn_err(CE_WARN,
2279 				    "ill_capability_ipsec_ack: "
2280 				    "malformed IPsec algorithm id %d",
2281 				    ialg->alg_prim);
2282 				continue;
2283 			}
2284 
2285 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2286 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2287 				    ialg->alg_prim);
2288 			} else {
2289 				ipsec_capab_algparm_t *alp;
2290 
2291 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2292 				    ialg->alg_prim);
2293 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2294 				    ialg->alg_prim)) {
2295 					cmn_err(CE_WARN,
2296 					    "ill_capability_ipsec_ack: "
2297 					    "no space for IPsec alg id %d",
2298 					    ialg->alg_prim);
2299 					continue;
2300 				}
2301 				alp = &((*ill_capab)->encr_algparm[
2302 				    ialg->alg_prim]);
2303 				alp->minkeylen = ialg->alg_minbits;
2304 				alp->maxkeylen = ialg->alg_maxbits;
2305 			}
2306 			ill->ill_capabilities |= ill_capab_flag;
2307 			/*
2308 			 * indicate that a capability was enabled, which
2309 			 * will be used below to kick off a SADB dump
2310 			 * to the ill.
2311 			 */
2312 			need_sadb_dump = B_TRUE;
2313 		} else {
2314 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2315 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2316 			    ialg->alg_prim));
2317 
2318 			if (nmp == NULL) {
2319 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2320 				if (nmp == NULL) {
2321 					/*
2322 					 * Sending the PROMISC_ON/OFF
2323 					 * notification request failed.
2324 					 * We cannot enable the algorithms
2325 					 * since the Provider will not
2326 					 * notify IP of promiscous mode
2327 					 * changes, which could lead
2328 					 * to leakage of packets.
2329 					 */
2330 					cmn_err(CE_WARN,
2331 					    "ill_capability_ipsec_ack: "
2332 					    "could not enable IPsec Hardware "
2333 					    "acceleration for %s (ENOMEM)\n",
2334 					    ill->ill_name);
2335 					return;
2336 				}
2337 				/* ptr to current output alg specifier */
2338 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2339 			}
2340 
2341 			/*
2342 			 * Copy current alg specifier, set ENABLE
2343 			 * flag, and advance to next output alg.
2344 			 * For now we enable all IPsec capabilities.
2345 			 */
2346 			ASSERT(oalg != NULL);
2347 			bcopy(ialg, oalg, alg_len);
2348 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2349 			nmp->b_wptr += alg_len;
2350 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2351 		}
2352 
2353 		/* move to next input algorithm specifier */
2354 		ialg = (dl_capab_ipsec_alg_t *)
2355 		    ((char *)ialg + alg_len);
2356 	}
2357 
2358 	if (nmp != NULL)
2359 		/*
2360 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2361 		 * IPsec hardware acceleration.
2362 		 */
2363 		ill_dlpi_send(ill, nmp);
2364 
2365 	if (need_sadb_dump)
2366 		/*
2367 		 * An acknowledgement corresponding to a request to
2368 		 * enable acceleration was received, notify SADB.
2369 		 */
2370 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2371 }
2372 
2373 /*
2374  * Given an mblk with enough space in it, create sub-capability entries for
2375  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2376  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2377  * in preparation for the reset the DL_CAPABILITY_REQ message.
2378  */
2379 static void
2380 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2381     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2382 {
2383 	dl_capab_ipsec_t *oipsec;
2384 	dl_capab_ipsec_alg_t *oalg;
2385 	dl_capability_sub_t *dl_subcap;
2386 	int i, k;
2387 
2388 	ASSERT(nciphers > 0);
2389 	ASSERT(ill_cap != NULL);
2390 	ASSERT(mp != NULL);
2391 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2392 
2393 	/* dl_capability_sub_t for "stype" */
2394 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2395 	dl_subcap->dl_cap = stype;
2396 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2397 	mp->b_wptr += sizeof (dl_capability_sub_t);
2398 
2399 	/* dl_capab_ipsec_t for "stype" */
2400 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2401 	oipsec->cip_version = 1;
2402 	oipsec->cip_nciphers = nciphers;
2403 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2404 
2405 	/* create entries for "stype" AUTH ciphers */
2406 	for (i = 0; i < ill_cap->algs_size; i++) {
2407 		for (k = 0; k < BITSPERBYTE; k++) {
2408 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2409 				continue;
2410 
2411 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2412 			bzero((void *)oalg, sizeof (*oalg));
2413 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2414 			oalg->alg_prim = k + (BITSPERBYTE * i);
2415 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2416 		}
2417 	}
2418 	/* create entries for "stype" ENCR ciphers */
2419 	for (i = 0; i < ill_cap->algs_size; i++) {
2420 		for (k = 0; k < BITSPERBYTE; k++) {
2421 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2422 				continue;
2423 
2424 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2425 			bzero((void *)oalg, sizeof (*oalg));
2426 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2427 			oalg->alg_prim = k + (BITSPERBYTE * i);
2428 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2429 		}
2430 	}
2431 }
2432 
2433 /*
2434  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2435  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2436  * POPC instruction, but our macro is more flexible for an arbitrary length
2437  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2438  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2439  * stays that way, we can reduce the number of iterations required.
2440  */
2441 #define	COUNT_1S(val, sum) {					\
2442 	uint8_t x = val & 0xff;					\
2443 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2444 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2445 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2446 }
2447 
2448 /* ARGSUSED */
2449 static void
2450 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2451 {
2452 	mblk_t *mp;
2453 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2454 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2455 	uint64_t ill_capabilities = ill->ill_capabilities;
2456 	int ah_cnt = 0, esp_cnt = 0;
2457 	int ah_len = 0, esp_len = 0;
2458 	int i, size = 0;
2459 
2460 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2461 		return;
2462 
2463 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2464 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2465 
2466 	/* Find out the number of ciphers for AH */
2467 	if (cap_ah != NULL) {
2468 		for (i = 0; i < cap_ah->algs_size; i++) {
2469 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2470 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2471 		}
2472 		if (ah_cnt > 0) {
2473 			size += sizeof (dl_capability_sub_t) +
2474 			    sizeof (dl_capab_ipsec_t);
2475 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2476 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2477 			size += ah_len;
2478 		}
2479 	}
2480 
2481 	/* Find out the number of ciphers for ESP */
2482 	if (cap_esp != NULL) {
2483 		for (i = 0; i < cap_esp->algs_size; i++) {
2484 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2485 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2486 		}
2487 		if (esp_cnt > 0) {
2488 			size += sizeof (dl_capability_sub_t) +
2489 			    sizeof (dl_capab_ipsec_t);
2490 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2491 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2492 			size += esp_len;
2493 		}
2494 	}
2495 
2496 	if (size == 0) {
2497 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2498 		    "there's nothing to reset\n"));
2499 		return;
2500 	}
2501 
2502 	mp = allocb(size, BPRI_HI);
2503 	if (mp == NULL) {
2504 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2505 		    "request to disable IPSEC Hardware Acceleration\n"));
2506 		return;
2507 	}
2508 
2509 	/*
2510 	 * Clear the capability flags for IPSec HA but retain the ill
2511 	 * capability structures since it's possible that another thread
2512 	 * is still referring to them.  The structures only get deallocated
2513 	 * when we destroy the ill.
2514 	 *
2515 	 * Various places check the flags to see if the ill is capable of
2516 	 * hardware acceleration, and by clearing them we ensure that new
2517 	 * outbound IPSec packets are sent down encrypted.
2518 	 */
2519 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2520 
2521 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2522 	if (ah_cnt > 0) {
2523 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2524 		    cap_ah, mp);
2525 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2526 	}
2527 
2528 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2529 	if (esp_cnt > 0) {
2530 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2531 		    cap_esp, mp);
2532 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2533 	}
2534 
2535 	/*
2536 	 * At this point we've composed a bunch of sub-capabilities to be
2537 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2538 	 * by the caller.  Upon receiving this reset message, the driver
2539 	 * must stop inbound decryption (by destroying all inbound SAs)
2540 	 * and let the corresponding packets come in encrypted.
2541 	 */
2542 
2543 	if (*sc_mp != NULL)
2544 		linkb(*sc_mp, mp);
2545 	else
2546 		*sc_mp = mp;
2547 }
2548 
2549 static void
2550 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2551     boolean_t encapsulated)
2552 {
2553 	boolean_t legacy = B_FALSE;
2554 
2555 	/*
2556 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2557 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2558 	 * instructed the driver to disable its advertised capabilities,
2559 	 * so there's no point in accepting any response at this moment.
2560 	 */
2561 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2562 		return;
2563 
2564 	/*
2565 	 * Note that only the following two sub-capabilities may be
2566 	 * considered as "legacy", since their original definitions
2567 	 * do not incorporate the dl_mid_t module ID token, and hence
2568 	 * may require the use of the wrapper sub-capability.
2569 	 */
2570 	switch (subp->dl_cap) {
2571 	case DL_CAPAB_IPSEC_AH:
2572 	case DL_CAPAB_IPSEC_ESP:
2573 		legacy = B_TRUE;
2574 		break;
2575 	}
2576 
2577 	/*
2578 	 * For legacy sub-capabilities which don't incorporate a queue_t
2579 	 * pointer in their structures, discard them if we detect that
2580 	 * there are intermediate modules in between IP and the driver.
2581 	 */
2582 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2583 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2584 		    "%d discarded; %d module(s) present below IP\n",
2585 		    subp->dl_cap, ill->ill_lmod_cnt));
2586 		return;
2587 	}
2588 
2589 	switch (subp->dl_cap) {
2590 	case DL_CAPAB_IPSEC_AH:
2591 	case DL_CAPAB_IPSEC_ESP:
2592 		ill_capability_ipsec_ack(ill, mp, subp);
2593 		break;
2594 	case DL_CAPAB_MDT:
2595 		ill_capability_mdt_ack(ill, mp, subp);
2596 		break;
2597 	case DL_CAPAB_HCKSUM:
2598 		ill_capability_hcksum_ack(ill, mp, subp);
2599 		break;
2600 	case DL_CAPAB_ZEROCOPY:
2601 		ill_capability_zerocopy_ack(ill, mp, subp);
2602 		break;
2603 	case DL_CAPAB_POLL:
2604 		if (!SOFT_RINGS_ENABLED())
2605 			ill_capability_dls_ack(ill, mp, subp);
2606 		break;
2607 	case DL_CAPAB_SOFT_RING:
2608 		if (SOFT_RINGS_ENABLED())
2609 			ill_capability_dls_ack(ill, mp, subp);
2610 		break;
2611 	case DL_CAPAB_LSO:
2612 		ill_capability_lso_ack(ill, mp, subp);
2613 		break;
2614 	default:
2615 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2616 		    subp->dl_cap));
2617 	}
2618 }
2619 
2620 /*
2621  * As part of negotiating polling capability, the driver tells us
2622  * the default (or normal) blanking interval and packet threshold
2623  * (the receive timer fires if blanking interval is reached or
2624  * the packet threshold is reached).
2625  *
2626  * As part of manipulating the polling interval, we always use our
2627  * estimated interval (avg service time * number of packets queued
2628  * on the squeue) but we try to blank for a minimum of
2629  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2630  * packet threshold during this time. When we are not in polling mode
2631  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2632  * rr_min_blank_ratio but up the packet cnt by a ratio of
2633  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2634  * possible although for a shorter interval.
2635  */
2636 #define	RR_MAX_BLANK_RATIO	20
2637 #define	RR_MIN_BLANK_RATIO	10
2638 #define	RR_MAX_PKT_CNT_RATIO	3
2639 #define	RR_MIN_PKT_CNT_RATIO	3
2640 
2641 /*
2642  * These can be tuned via /etc/system.
2643  */
2644 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2645 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2646 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2647 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2648 
2649 static mac_resource_handle_t
2650 ill_ring_add(void *arg, mac_resource_t *mrp)
2651 {
2652 	ill_t			*ill = (ill_t *)arg;
2653 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2654 	ill_rx_ring_t		*rx_ring;
2655 	int			ip_rx_index;
2656 
2657 	ASSERT(mrp != NULL);
2658 	if (mrp->mr_type != MAC_RX_FIFO) {
2659 		return (NULL);
2660 	}
2661 	ASSERT(ill != NULL);
2662 	ASSERT(ill->ill_dls_capab != NULL);
2663 
2664 	mutex_enter(&ill->ill_lock);
2665 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2666 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2667 		ASSERT(rx_ring != NULL);
2668 
2669 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2670 			time_t normal_blank_time =
2671 			    mrfp->mrf_normal_blank_time;
2672 			uint_t normal_pkt_cnt =
2673 			    mrfp->mrf_normal_pkt_count;
2674 
2675 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2676 
2677 	rx_ring->rr_blank = mrfp->mrf_blank;
2678 	rx_ring->rr_handle = mrfp->mrf_arg;
2679 	rx_ring->rr_ill = ill;
2680 	rx_ring->rr_normal_blank_time = normal_blank_time;
2681 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2682 
2683 			rx_ring->rr_max_blank_time =
2684 			    normal_blank_time * rr_max_blank_ratio;
2685 			rx_ring->rr_min_blank_time =
2686 			    normal_blank_time * rr_min_blank_ratio;
2687 			rx_ring->rr_max_pkt_cnt =
2688 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2689 			rx_ring->rr_min_pkt_cnt =
2690 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2691 
2692 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2693 			mutex_exit(&ill->ill_lock);
2694 
2695 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2696 			    (int), ip_rx_index);
2697 			return ((mac_resource_handle_t)rx_ring);
2698 		}
2699 	}
2700 
2701 	/*
2702 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2703 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2704 	 * should be made configurable. Meanwhile it cause no panic because
2705 	 * driver will pass ip_input a NULL handle which will make
2706 	 * IP allocate the default squeue and Polling mode will not
2707 	 * be used for this ring.
2708 	 */
2709 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2710 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2711 
2712 	mutex_exit(&ill->ill_lock);
2713 	return (NULL);
2714 }
2715 
2716 static boolean_t
2717 ill_capability_dls_init(ill_t *ill)
2718 {
2719 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2720 	conn_t 			*connp;
2721 	size_t			sz;
2722 	ip_stack_t *ipst = ill->ill_ipst;
2723 
2724 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2725 		if (ill_dls == NULL) {
2726 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2727 			    "soft_ring enabled for ill=%s (%p) but data "
2728 			    "structs uninitialized\n", ill->ill_name,
2729 			    (void *)ill);
2730 		}
2731 		return (B_TRUE);
2732 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2733 		if (ill_dls == NULL) {
2734 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2735 			    "polling enabled for ill=%s (%p) but data "
2736 			    "structs uninitialized\n", ill->ill_name,
2737 			    (void *)ill);
2738 		}
2739 		return (B_TRUE);
2740 	}
2741 
2742 	if (ill_dls != NULL) {
2743 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2744 		/* Soft_Ring or polling is being re-enabled */
2745 
2746 		connp = ill_dls->ill_unbind_conn;
2747 		ASSERT(rx_ring != NULL);
2748 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2749 		bzero((void *)rx_ring,
2750 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2751 		ill_dls->ill_ring_tbl = rx_ring;
2752 		ill_dls->ill_unbind_conn = connp;
2753 		return (B_TRUE);
2754 	}
2755 
2756 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2757 	    ipst->ips_netstack)) == NULL)
2758 		return (B_FALSE);
2759 
2760 	sz = sizeof (ill_dls_capab_t);
2761 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2762 
2763 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2764 	if (ill_dls == NULL) {
2765 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2766 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2767 		    (void *)ill);
2768 		CONN_DEC_REF(connp);
2769 		return (B_FALSE);
2770 	}
2771 
2772 	/* Allocate space to hold ring table */
2773 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2774 	ill->ill_dls_capab = ill_dls;
2775 	ill_dls->ill_unbind_conn = connp;
2776 	return (B_TRUE);
2777 }
2778 
2779 /*
2780  * ill_capability_dls_disable: disable soft_ring and/or polling
2781  * capability. Since any of the rings might already be in use, need
2782  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2783  * direct calls if necessary.
2784  */
2785 static void
2786 ill_capability_dls_disable(ill_t *ill)
2787 {
2788 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2789 
2790 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2791 		ip_squeue_clean_all(ill);
2792 		ill_dls->ill_tx = NULL;
2793 		ill_dls->ill_tx_handle = NULL;
2794 		ill_dls->ill_dls_change_status = NULL;
2795 		ill_dls->ill_dls_bind = NULL;
2796 		ill_dls->ill_dls_unbind = NULL;
2797 	}
2798 
2799 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2800 }
2801 
2802 static void
2803 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2804     dl_capability_sub_t *isub)
2805 {
2806 	uint_t			size;
2807 	uchar_t			*rptr;
2808 	dl_capab_dls_t	dls, *odls;
2809 	ill_dls_capab_t	*ill_dls;
2810 	mblk_t			*nmp = NULL;
2811 	dl_capability_req_t	*ocap;
2812 	uint_t			sub_dl_cap = isub->dl_cap;
2813 
2814 	if (!ill_capability_dls_init(ill))
2815 		return;
2816 	ill_dls = ill->ill_dls_capab;
2817 
2818 	/* Copy locally to get the members aligned */
2819 	bcopy((void *)idls, (void *)&dls,
2820 	    sizeof (dl_capab_dls_t));
2821 
2822 	/* Get the tx function and handle from dld */
2823 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2824 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2825 
2826 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2827 		ill_dls->ill_dls_change_status =
2828 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2829 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2830 		ill_dls->ill_dls_unbind =
2831 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2832 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2833 	}
2834 
2835 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2836 	    isub->dl_length;
2837 
2838 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2839 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2840 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2841 		    ill->ill_name, (void *)ill);
2842 		return;
2843 	}
2844 
2845 	/* initialize dl_capability_req_t */
2846 	rptr = nmp->b_rptr;
2847 	ocap = (dl_capability_req_t *)rptr;
2848 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2849 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2850 	rptr += sizeof (dl_capability_req_t);
2851 
2852 	/* initialize dl_capability_sub_t */
2853 	bcopy(isub, rptr, sizeof (*isub));
2854 	rptr += sizeof (*isub);
2855 
2856 	odls = (dl_capab_dls_t *)rptr;
2857 	rptr += sizeof (dl_capab_dls_t);
2858 
2859 	/* initialize dl_capab_dls_t to be sent down */
2860 	dls.dls_rx_handle = (uintptr_t)ill;
2861 	dls.dls_rx = (uintptr_t)ip_input;
2862 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2863 
2864 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2865 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2866 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2867 		dls.dls_flags = SOFT_RING_ENABLE;
2868 	} else {
2869 		dls.dls_flags = POLL_ENABLE;
2870 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2871 		    "to enable polling\n", ill->ill_name));
2872 	}
2873 	bcopy((void *)&dls, (void *)odls,
2874 	    sizeof (dl_capab_dls_t));
2875 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2876 	/*
2877 	 * nmp points to a DL_CAPABILITY_REQ message to
2878 	 * enable either soft_ring or polling
2879 	 */
2880 	ill_dlpi_send(ill, nmp);
2881 }
2882 
2883 static void
2884 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2885 {
2886 	mblk_t *mp;
2887 	dl_capab_dls_t *idls;
2888 	dl_capability_sub_t *dl_subcap;
2889 	int size;
2890 
2891 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2892 		return;
2893 
2894 	ASSERT(ill->ill_dls_capab != NULL);
2895 
2896 	size = sizeof (*dl_subcap) + sizeof (*idls);
2897 
2898 	mp = allocb(size, BPRI_HI);
2899 	if (mp == NULL) {
2900 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2901 		    "request to disable soft_ring\n"));
2902 		return;
2903 	}
2904 
2905 	mp->b_wptr = mp->b_rptr + size;
2906 
2907 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2908 	dl_subcap->dl_length = sizeof (*idls);
2909 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2910 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2911 	else
2912 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2913 
2914 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2915 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2916 		idls->dls_flags = SOFT_RING_DISABLE;
2917 	else
2918 		idls->dls_flags = POLL_DISABLE;
2919 
2920 	if (*sc_mp != NULL)
2921 		linkb(*sc_mp, mp);
2922 	else
2923 		*sc_mp = mp;
2924 }
2925 
2926 /*
2927  * Process a soft_ring/poll capability negotiation ack received
2928  * from a DLS Provider.isub must point to the sub-capability
2929  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2930  */
2931 static void
2932 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2933 {
2934 	dl_capab_dls_t		*idls;
2935 	uint_t			sub_dl_cap = isub->dl_cap;
2936 	uint8_t			*capend;
2937 
2938 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2939 	    sub_dl_cap == DL_CAPAB_POLL);
2940 
2941 	if (ill->ill_isv6)
2942 		return;
2943 
2944 	/*
2945 	 * Note: range checks here are not absolutely sufficient to
2946 	 * make us robust against malformed messages sent by drivers;
2947 	 * this is in keeping with the rest of IP's dlpi handling.
2948 	 * (Remember, it's coming from something else in the kernel
2949 	 * address space)
2950 	 */
2951 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2952 	if (capend > mp->b_wptr) {
2953 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2954 		    "malformed sub-capability too long for mblk");
2955 		return;
2956 	}
2957 
2958 	/*
2959 	 * There are two types of acks we process here:
2960 	 * 1. acks in reply to a (first form) generic capability req
2961 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2962 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2963 	 *    capability req.
2964 	 */
2965 	idls = (dl_capab_dls_t *)(isub + 1);
2966 
2967 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2968 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2969 		    "capability isn't as expected; pass-thru "
2970 		    "module(s) detected, discarding capability\n"));
2971 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2972 			/*
2973 			 * This is a capability renegotitation case.
2974 			 * The interface better be unusable at this
2975 			 * point other wise bad things will happen
2976 			 * if we disable direct calls on a running
2977 			 * and up interface.
2978 			 */
2979 			ill_capability_dls_disable(ill);
2980 		}
2981 		return;
2982 	}
2983 
2984 	switch (idls->dls_flags) {
2985 	default:
2986 		/* Disable if unknown flag */
2987 	case SOFT_RING_DISABLE:
2988 	case POLL_DISABLE:
2989 		ill_capability_dls_disable(ill);
2990 		break;
2991 	case SOFT_RING_CAPABLE:
2992 	case POLL_CAPABLE:
2993 		/*
2994 		 * If the capability was already enabled, its safe
2995 		 * to disable it first to get rid of stale information
2996 		 * and then start enabling it again.
2997 		 */
2998 		ill_capability_dls_disable(ill);
2999 		ill_capability_dls_capable(ill, idls, isub);
3000 		break;
3001 	case SOFT_RING_ENABLE:
3002 	case POLL_ENABLE:
3003 		mutex_enter(&ill->ill_lock);
3004 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3005 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3006 			ASSERT(ill->ill_dls_capab != NULL);
3007 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3008 		}
3009 		if (sub_dl_cap == DL_CAPAB_POLL &&
3010 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3011 			ASSERT(ill->ill_dls_capab != NULL);
3012 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3013 			ip1dbg(("ill_capability_dls_ack: interface %s "
3014 			    "has enabled polling\n", ill->ill_name));
3015 		}
3016 		mutex_exit(&ill->ill_lock);
3017 		break;
3018 	}
3019 }
3020 
3021 /*
3022  * Process a hardware checksum offload capability negotiation ack received
3023  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3024  * of a DL_CAPABILITY_ACK message.
3025  */
3026 static void
3027 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3028 {
3029 	dl_capability_req_t	*ocap;
3030 	dl_capab_hcksum_t	*ihck, *ohck;
3031 	ill_hcksum_capab_t	**ill_hcksum;
3032 	mblk_t			*nmp = NULL;
3033 	uint_t			sub_dl_cap = isub->dl_cap;
3034 	uint8_t			*capend;
3035 
3036 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3037 
3038 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
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_hcksum_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 	 *    (no ENABLE flag set)
3058 	 * 2. acks in reply to a ENABLE capability req.
3059 	 *    (ENABLE flag set)
3060 	 */
3061 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3062 
3063 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3064 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3065 		    "unsupported hardware checksum "
3066 		    "sub-capability (version %d, expected %d)",
3067 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3068 		return;
3069 	}
3070 
3071 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3072 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3073 		    "checksum capability isn't as expected; pass-thru "
3074 		    "module(s) detected, discarding capability\n"));
3075 		return;
3076 	}
3077 
3078 #define	CURR_HCKSUM_CAPAB				\
3079 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3080 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3081 
3082 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3083 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3084 		/* do ENABLE processing */
3085 		if (*ill_hcksum == NULL) {
3086 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3087 			    KM_NOSLEEP);
3088 
3089 			if (*ill_hcksum == NULL) {
3090 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3091 				    "could not enable hcksum version %d "
3092 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3093 				    ill->ill_name);
3094 				return;
3095 			}
3096 		}
3097 
3098 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3099 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3100 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3101 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3102 		    "has enabled hardware checksumming\n ",
3103 		    ill->ill_name));
3104 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3105 		/*
3106 		 * Enabling hardware checksum offload
3107 		 * Currently IP supports {TCP,UDP}/IPv4
3108 		 * partial and full cksum offload and
3109 		 * IPv4 header checksum offload.
3110 		 * Allocate new mblk which will
3111 		 * contain a new capability request
3112 		 * to enable hardware checksum offload.
3113 		 */
3114 		uint_t	size;
3115 		uchar_t	*rptr;
3116 
3117 		size = sizeof (dl_capability_req_t) +
3118 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3119 
3120 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3121 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3122 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3123 			    ill->ill_name);
3124 			return;
3125 		}
3126 
3127 		rptr = nmp->b_rptr;
3128 		/* initialize dl_capability_req_t */
3129 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3130 		ocap->dl_sub_offset =
3131 		    sizeof (dl_capability_req_t);
3132 		ocap->dl_sub_length =
3133 		    sizeof (dl_capability_sub_t) +
3134 		    isub->dl_length;
3135 		nmp->b_rptr += sizeof (dl_capability_req_t);
3136 
3137 		/* initialize dl_capability_sub_t */
3138 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3139 		nmp->b_rptr += sizeof (*isub);
3140 
3141 		/* initialize dl_capab_hcksum_t */
3142 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3143 		bcopy(ihck, ohck, sizeof (*ihck));
3144 
3145 		nmp->b_rptr = rptr;
3146 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3147 
3148 		/* Set ENABLE flag */
3149 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3150 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3151 
3152 		/*
3153 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3154 		 * hardware checksum acceleration.
3155 		 */
3156 		ill_dlpi_send(ill, nmp);
3157 	} else {
3158 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3159 		    "advertised %x hardware checksum capability flags\n",
3160 		    ill->ill_name, ihck->hcksum_txflags));
3161 	}
3162 }
3163 
3164 static void
3165 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3166 {
3167 	mblk_t *mp;
3168 	dl_capab_hcksum_t *hck_subcap;
3169 	dl_capability_sub_t *dl_subcap;
3170 	int size;
3171 
3172 	if (!ILL_HCKSUM_CAPABLE(ill))
3173 		return;
3174 
3175 	ASSERT(ill->ill_hcksum_capab != NULL);
3176 	/*
3177 	 * Clear the capability flag for hardware checksum offload but
3178 	 * retain the ill_hcksum_capab structure since it's possible that
3179 	 * another thread is still referring to it.  The structure only
3180 	 * gets deallocated when we destroy the ill.
3181 	 */
3182 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3183 
3184 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3185 
3186 	mp = allocb(size, BPRI_HI);
3187 	if (mp == NULL) {
3188 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3189 		    "request to disable hardware checksum offload\n"));
3190 		return;
3191 	}
3192 
3193 	mp->b_wptr = mp->b_rptr + size;
3194 
3195 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3196 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3197 	dl_subcap->dl_length = sizeof (*hck_subcap);
3198 
3199 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3200 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3201 	hck_subcap->hcksum_txflags = 0;
3202 
3203 	if (*sc_mp != NULL)
3204 		linkb(*sc_mp, mp);
3205 	else
3206 		*sc_mp = mp;
3207 }
3208 
3209 static void
3210 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3211 {
3212 	mblk_t *nmp = NULL;
3213 	dl_capability_req_t *oc;
3214 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3215 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3216 	uint_t sub_dl_cap = isub->dl_cap;
3217 	uint8_t *capend;
3218 
3219 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3220 
3221 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3222 
3223 	/*
3224 	 * Note: range checks here are not absolutely sufficient to
3225 	 * make us robust against malformed messages sent by drivers;
3226 	 * this is in keeping with the rest of IP's dlpi handling.
3227 	 * (Remember, it's coming from something else in the kernel
3228 	 * address space)
3229 	 */
3230 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3231 	if (capend > mp->b_wptr) {
3232 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3233 		    "malformed sub-capability too long for mblk");
3234 		return;
3235 	}
3236 
3237 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3238 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3239 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3240 		    "unsupported ZEROCOPY sub-capability (version %d, "
3241 		    "expected %d)", zc_ic->zerocopy_version,
3242 		    ZEROCOPY_VERSION_1);
3243 		return;
3244 	}
3245 
3246 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3247 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3248 		    "capability isn't as expected; pass-thru module(s) "
3249 		    "detected, discarding capability\n"));
3250 		return;
3251 	}
3252 
3253 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3254 		if (*ill_zerocopy_capab == NULL) {
3255 			*ill_zerocopy_capab =
3256 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3257 			    KM_NOSLEEP);
3258 
3259 			if (*ill_zerocopy_capab == NULL) {
3260 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3261 				    "could not enable Zero-copy version %d "
3262 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3263 				    ill->ill_name);
3264 				return;
3265 			}
3266 		}
3267 
3268 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3269 		    "supports Zero-copy version %d\n", ill->ill_name,
3270 		    ZEROCOPY_VERSION_1));
3271 
3272 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3273 		    zc_ic->zerocopy_version;
3274 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3275 		    zc_ic->zerocopy_flags;
3276 
3277 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3278 	} else {
3279 		uint_t size;
3280 		uchar_t *rptr;
3281 
3282 		size = sizeof (dl_capability_req_t) +
3283 		    sizeof (dl_capability_sub_t) +
3284 		    sizeof (dl_capab_zerocopy_t);
3285 
3286 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3287 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3288 			    "could not enable zerocopy for %s (ENOMEM)\n",
3289 			    ill->ill_name);
3290 			return;
3291 		}
3292 
3293 		rptr = nmp->b_rptr;
3294 		/* initialize dl_capability_req_t */
3295 		oc = (dl_capability_req_t *)rptr;
3296 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3297 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3298 		    sizeof (dl_capab_zerocopy_t);
3299 		rptr += sizeof (dl_capability_req_t);
3300 
3301 		/* initialize dl_capability_sub_t */
3302 		bcopy(isub, rptr, sizeof (*isub));
3303 		rptr += sizeof (*isub);
3304 
3305 		/* initialize dl_capab_zerocopy_t */
3306 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3307 		*zc_oc = *zc_ic;
3308 
3309 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3310 		    "to enable zero-copy version %d\n", ill->ill_name,
3311 		    ZEROCOPY_VERSION_1));
3312 
3313 		/* set VMSAFE_MEM flag */
3314 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3315 
3316 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3317 		ill_dlpi_send(ill, nmp);
3318 	}
3319 }
3320 
3321 static void
3322 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3323 {
3324 	mblk_t *mp;
3325 	dl_capab_zerocopy_t *zerocopy_subcap;
3326 	dl_capability_sub_t *dl_subcap;
3327 	int size;
3328 
3329 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3330 		return;
3331 
3332 	ASSERT(ill->ill_zerocopy_capab != NULL);
3333 	/*
3334 	 * Clear the capability flag for Zero-copy but retain the
3335 	 * ill_zerocopy_capab structure since it's possible that another
3336 	 * thread is still referring to it.  The structure only gets
3337 	 * deallocated when we destroy the ill.
3338 	 */
3339 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3340 
3341 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3342 
3343 	mp = allocb(size, BPRI_HI);
3344 	if (mp == NULL) {
3345 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3346 		    "request to disable Zero-copy\n"));
3347 		return;
3348 	}
3349 
3350 	mp->b_wptr = mp->b_rptr + size;
3351 
3352 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3353 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3354 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3355 
3356 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3357 	zerocopy_subcap->zerocopy_version =
3358 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3359 	zerocopy_subcap->zerocopy_flags = 0;
3360 
3361 	if (*sc_mp != NULL)
3362 		linkb(*sc_mp, mp);
3363 	else
3364 		*sc_mp = mp;
3365 }
3366 
3367 /*
3368  * Process Large Segment Offload capability negotiation ack received from a
3369  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3370  * DL_CAPABILITY_ACK message.
3371  */
3372 static void
3373 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3374 {
3375 	mblk_t *nmp = NULL;
3376 	dl_capability_req_t *oc;
3377 	dl_capab_lso_t *lso_ic, *lso_oc;
3378 	ill_lso_capab_t **ill_lso_capab;
3379 	uint_t sub_dl_cap = isub->dl_cap;
3380 	uint8_t *capend;
3381 
3382 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3383 
3384 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3385 
3386 	/*
3387 	 * Note: range checks here are not absolutely sufficient to
3388 	 * make us robust against malformed messages sent by drivers;
3389 	 * this is in keeping with the rest of IP's dlpi handling.
3390 	 * (Remember, it's coming from something else in the kernel
3391 	 * address space)
3392 	 */
3393 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3394 	if (capend > mp->b_wptr) {
3395 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3396 		    "malformed sub-capability too long for mblk");
3397 		return;
3398 	}
3399 
3400 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3401 
3402 	if (lso_ic->lso_version != LSO_VERSION_1) {
3403 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3404 		    "unsupported LSO sub-capability (version %d, expected %d)",
3405 		    lso_ic->lso_version, LSO_VERSION_1);
3406 		return;
3407 	}
3408 
3409 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3410 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3411 		    "capability isn't as expected; pass-thru module(s) "
3412 		    "detected, discarding capability\n"));
3413 		return;
3414 	}
3415 
3416 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3417 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3418 		if (*ill_lso_capab == NULL) {
3419 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3420 			    KM_NOSLEEP);
3421 
3422 			if (*ill_lso_capab == NULL) {
3423 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3424 				    "could not enable LSO version %d "
3425 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3426 				    ill->ill_name);
3427 				return;
3428 			}
3429 		}
3430 
3431 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3432 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3433 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3434 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3435 
3436 		ip1dbg(("ill_capability_lso_ack: interface %s "
3437 		    "has enabled LSO\n ", ill->ill_name));
3438 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3439 		uint_t size;
3440 		uchar_t *rptr;
3441 
3442 		size = sizeof (dl_capability_req_t) +
3443 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3444 
3445 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3446 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3447 			    "could not enable LSO for %s (ENOMEM)\n",
3448 			    ill->ill_name);
3449 			return;
3450 		}
3451 
3452 		rptr = nmp->b_rptr;
3453 		/* initialize dl_capability_req_t */
3454 		oc = (dl_capability_req_t *)nmp->b_rptr;
3455 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3456 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3457 		    sizeof (dl_capab_lso_t);
3458 		nmp->b_rptr += sizeof (dl_capability_req_t);
3459 
3460 		/* initialize dl_capability_sub_t */
3461 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3462 		nmp->b_rptr += sizeof (*isub);
3463 
3464 		/* initialize dl_capab_lso_t */
3465 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3466 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3467 
3468 		nmp->b_rptr = rptr;
3469 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3470 
3471 		/* set ENABLE flag */
3472 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3473 
3474 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3475 		ill_dlpi_send(ill, nmp);
3476 	} else {
3477 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3478 		    "advertised %x LSO capability flags\n",
3479 		    ill->ill_name, lso_ic->lso_flags));
3480 	}
3481 }
3482 
3483 
3484 static void
3485 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3486 {
3487 	mblk_t *mp;
3488 	dl_capab_lso_t *lso_subcap;
3489 	dl_capability_sub_t *dl_subcap;
3490 	int size;
3491 
3492 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3493 		return;
3494 
3495 	ASSERT(ill->ill_lso_capab != NULL);
3496 	/*
3497 	 * Clear the capability flag for LSO but retain the
3498 	 * ill_lso_capab structure since it's possible that another
3499 	 * thread is still referring to it.  The structure only gets
3500 	 * deallocated when we destroy the ill.
3501 	 */
3502 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3503 
3504 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3505 
3506 	mp = allocb(size, BPRI_HI);
3507 	if (mp == NULL) {
3508 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3509 		    "request to disable LSO\n"));
3510 		return;
3511 	}
3512 
3513 	mp->b_wptr = mp->b_rptr + size;
3514 
3515 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3516 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3517 	dl_subcap->dl_length = sizeof (*lso_subcap);
3518 
3519 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3520 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3521 	lso_subcap->lso_flags = 0;
3522 
3523 	if (*sc_mp != NULL)
3524 		linkb(*sc_mp, mp);
3525 	else
3526 		*sc_mp = mp;
3527 }
3528 
3529 /*
3530  * Consume a new-style hardware capabilities negotiation ack.
3531  * Called from ip_rput_dlpi_writer().
3532  */
3533 void
3534 ill_capability_ack(ill_t *ill, mblk_t *mp)
3535 {
3536 	dl_capability_ack_t *capp;
3537 	dl_capability_sub_t *subp, *endp;
3538 
3539 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3540 		ill->ill_dlpi_capab_state = IDS_OK;
3541 
3542 	capp = (dl_capability_ack_t *)mp->b_rptr;
3543 
3544 	if (capp->dl_sub_length == 0)
3545 		/* no new-style capabilities */
3546 		return;
3547 
3548 	/* make sure the driver supplied correct dl_sub_length */
3549 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3550 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3551 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3552 		return;
3553 	}
3554 
3555 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3556 	/*
3557 	 * There are sub-capabilities. Process the ones we know about.
3558 	 * Loop until we don't have room for another sub-cap header..
3559 	 */
3560 	for (subp = SC(capp, capp->dl_sub_offset),
3561 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3562 	    subp <= endp;
3563 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3564 
3565 		switch (subp->dl_cap) {
3566 		case DL_CAPAB_ID_WRAPPER:
3567 			ill_capability_id_ack(ill, mp, subp);
3568 			break;
3569 		default:
3570 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3571 			break;
3572 		}
3573 	}
3574 #undef SC
3575 }
3576 
3577 /*
3578  * This routine is called to scan the fragmentation reassembly table for
3579  * the specified ILL for any packets that are starting to smell.
3580  * dead_interval is the maximum time in seconds that will be tolerated.  It
3581  * will either be the value specified in ip_g_frag_timeout, or zero if the
3582  * ILL is shutting down and it is time to blow everything off.
3583  *
3584  * It returns the number of seconds (as a time_t) that the next frag timer
3585  * should be scheduled for, 0 meaning that the timer doesn't need to be
3586  * re-started.  Note that the method of calculating next_timeout isn't
3587  * entirely accurate since time will flow between the time we grab
3588  * current_time and the time we schedule the next timeout.  This isn't a
3589  * big problem since this is the timer for sending an ICMP reassembly time
3590  * exceeded messages, and it doesn't have to be exactly accurate.
3591  *
3592  * This function is
3593  * sometimes called as writer, although this is not required.
3594  */
3595 time_t
3596 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3597 {
3598 	ipfb_t	*ipfb;
3599 	ipfb_t	*endp;
3600 	ipf_t	*ipf;
3601 	ipf_t	*ipfnext;
3602 	mblk_t	*mp;
3603 	time_t	current_time = gethrestime_sec();
3604 	time_t	next_timeout = 0;
3605 	uint32_t	hdr_length;
3606 	mblk_t	*send_icmp_head;
3607 	mblk_t	*send_icmp_head_v6;
3608 	zoneid_t zoneid;
3609 	ip_stack_t *ipst = ill->ill_ipst;
3610 
3611 	ipfb = ill->ill_frag_hash_tbl;
3612 	if (ipfb == NULL)
3613 		return (B_FALSE);
3614 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3615 	/* Walk the frag hash table. */
3616 	for (; ipfb < endp; ipfb++) {
3617 		send_icmp_head = NULL;
3618 		send_icmp_head_v6 = NULL;
3619 		mutex_enter(&ipfb->ipfb_lock);
3620 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3621 			time_t frag_time = current_time - ipf->ipf_timestamp;
3622 			time_t frag_timeout;
3623 
3624 			if (frag_time < dead_interval) {
3625 				/*
3626 				 * There are some outstanding fragments
3627 				 * that will timeout later.  Make note of
3628 				 * the time so that we can reschedule the
3629 				 * next timeout appropriately.
3630 				 */
3631 				frag_timeout = dead_interval - frag_time;
3632 				if (next_timeout == 0 ||
3633 				    frag_timeout < next_timeout) {
3634 					next_timeout = frag_timeout;
3635 				}
3636 				break;
3637 			}
3638 			/* Time's up.  Get it out of here. */
3639 			hdr_length = ipf->ipf_nf_hdr_len;
3640 			ipfnext = ipf->ipf_hash_next;
3641 			if (ipfnext)
3642 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3643 			*ipf->ipf_ptphn = ipfnext;
3644 			mp = ipf->ipf_mp->b_cont;
3645 			for (; mp; mp = mp->b_cont) {
3646 				/* Extra points for neatness. */
3647 				IP_REASS_SET_START(mp, 0);
3648 				IP_REASS_SET_END(mp, 0);
3649 			}
3650 			mp = ipf->ipf_mp->b_cont;
3651 			ill->ill_frag_count -= ipf->ipf_count;
3652 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3653 			ipfb->ipfb_count -= ipf->ipf_count;
3654 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3655 			ipfb->ipfb_frag_pkts--;
3656 			/*
3657 			 * We do not send any icmp message from here because
3658 			 * we currently are holding the ipfb_lock for this
3659 			 * hash chain. If we try and send any icmp messages
3660 			 * from here we may end up via a put back into ip
3661 			 * trying to get the same lock, causing a recursive
3662 			 * mutex panic. Instead we build a list and send all
3663 			 * the icmp messages after we have dropped the lock.
3664 			 */
3665 			if (ill->ill_isv6) {
3666 				if (hdr_length != 0) {
3667 					mp->b_next = send_icmp_head_v6;
3668 					send_icmp_head_v6 = mp;
3669 				} else {
3670 					freemsg(mp);
3671 				}
3672 			} else {
3673 				if (hdr_length != 0) {
3674 					mp->b_next = send_icmp_head;
3675 					send_icmp_head = mp;
3676 				} else {
3677 					freemsg(mp);
3678 				}
3679 			}
3680 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3681 			freeb(ipf->ipf_mp);
3682 		}
3683 		mutex_exit(&ipfb->ipfb_lock);
3684 		/*
3685 		 * Now need to send any icmp messages that we delayed from
3686 		 * above.
3687 		 */
3688 		while (send_icmp_head_v6 != NULL) {
3689 			ip6_t *ip6h;
3690 
3691 			mp = send_icmp_head_v6;
3692 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3693 			mp->b_next = NULL;
3694 			if (mp->b_datap->db_type == M_CTL)
3695 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3696 			else
3697 				ip6h = (ip6_t *)mp->b_rptr;
3698 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3699 			    ill, ipst);
3700 			if (zoneid == ALL_ZONES) {
3701 				freemsg(mp);
3702 			} else {
3703 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3704 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3705 				    B_FALSE, zoneid, ipst);
3706 			}
3707 		}
3708 		while (send_icmp_head != NULL) {
3709 			ipaddr_t dst;
3710 
3711 			mp = send_icmp_head;
3712 			send_icmp_head = send_icmp_head->b_next;
3713 			mp->b_next = NULL;
3714 
3715 			if (mp->b_datap->db_type == M_CTL)
3716 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3717 			else
3718 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3719 
3720 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3721 			if (zoneid == ALL_ZONES) {
3722 				freemsg(mp);
3723 			} else {
3724 				icmp_time_exceeded(ill->ill_wq, mp,
3725 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3726 				    ipst);
3727 			}
3728 		}
3729 	}
3730 	/*
3731 	 * A non-dying ILL will use the return value to decide whether to
3732 	 * restart the frag timer, and for how long.
3733 	 */
3734 	return (next_timeout);
3735 }
3736 
3737 /*
3738  * This routine is called when the approximate count of mblk memory used
3739  * for the specified ILL has exceeded max_count.
3740  */
3741 void
3742 ill_frag_prune(ill_t *ill, uint_t max_count)
3743 {
3744 	ipfb_t	*ipfb;
3745 	ipf_t	*ipf;
3746 	size_t	count;
3747 
3748 	/*
3749 	 * If we are here within ip_min_frag_prune_time msecs remove
3750 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3751 	 * ill_frag_free_num_pkts.
3752 	 */
3753 	mutex_enter(&ill->ill_lock);
3754 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3755 	    (ip_min_frag_prune_time != 0 ?
3756 	    ip_min_frag_prune_time : msec_per_tick)) {
3757 
3758 		ill->ill_frag_free_num_pkts++;
3759 
3760 	} else {
3761 		ill->ill_frag_free_num_pkts = 0;
3762 	}
3763 	ill->ill_last_frag_clean_time = lbolt;
3764 	mutex_exit(&ill->ill_lock);
3765 
3766 	/*
3767 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3768 	 */
3769 	if (ill->ill_frag_free_num_pkts != 0) {
3770 		int ix;
3771 
3772 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3773 			ipfb = &ill->ill_frag_hash_tbl[ix];
3774 			mutex_enter(&ipfb->ipfb_lock);
3775 			if (ipfb->ipfb_ipf != NULL) {
3776 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3777 				    ill->ill_frag_free_num_pkts);
3778 			}
3779 			mutex_exit(&ipfb->ipfb_lock);
3780 		}
3781 	}
3782 	/*
3783 	 * While the reassembly list for this ILL is too big, prune a fragment
3784 	 * queue by age, oldest first.  Note that the per ILL count is
3785 	 * approximate, while the per frag hash bucket counts are accurate.
3786 	 */
3787 	while (ill->ill_frag_count > max_count) {
3788 		int	ix;
3789 		ipfb_t	*oipfb = NULL;
3790 		uint_t	oldest = UINT_MAX;
3791 
3792 		count = 0;
3793 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3794 			ipfb = &ill->ill_frag_hash_tbl[ix];
3795 			mutex_enter(&ipfb->ipfb_lock);
3796 			ipf = ipfb->ipfb_ipf;
3797 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3798 				oldest = ipf->ipf_gen;
3799 				oipfb = ipfb;
3800 			}
3801 			count += ipfb->ipfb_count;
3802 			mutex_exit(&ipfb->ipfb_lock);
3803 		}
3804 		/* Refresh the per ILL count */
3805 		ill->ill_frag_count = count;
3806 		if (oipfb == NULL) {
3807 			ill->ill_frag_count = 0;
3808 			break;
3809 		}
3810 		if (count <= max_count)
3811 			return;	/* Somebody beat us to it, nothing to do */
3812 		mutex_enter(&oipfb->ipfb_lock);
3813 		ipf = oipfb->ipfb_ipf;
3814 		if (ipf != NULL) {
3815 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3816 		}
3817 		mutex_exit(&oipfb->ipfb_lock);
3818 	}
3819 }
3820 
3821 /*
3822  * free 'free_cnt' fragmented packets starting at ipf.
3823  */
3824 void
3825 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3826 {
3827 	size_t	count;
3828 	mblk_t	*mp;
3829 	mblk_t	*tmp;
3830 	ipf_t **ipfp = ipf->ipf_ptphn;
3831 
3832 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3833 	ASSERT(ipfp != NULL);
3834 	ASSERT(ipf != NULL);
3835 
3836 	while (ipf != NULL && free_cnt-- > 0) {
3837 		count = ipf->ipf_count;
3838 		mp = ipf->ipf_mp;
3839 		ipf = ipf->ipf_hash_next;
3840 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3841 			IP_REASS_SET_START(tmp, 0);
3842 			IP_REASS_SET_END(tmp, 0);
3843 		}
3844 		ill->ill_frag_count -= count;
3845 		ASSERT(ipfb->ipfb_count >= count);
3846 		ipfb->ipfb_count -= count;
3847 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3848 		ipfb->ipfb_frag_pkts--;
3849 		freemsg(mp);
3850 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3851 	}
3852 
3853 	if (ipf)
3854 		ipf->ipf_ptphn = ipfp;
3855 	ipfp[0] = ipf;
3856 }
3857 
3858 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3859 	"obsolete and may be removed in a future release of Solaris.  Use " \
3860 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3861 
3862 /*
3863  * For obsolete per-interface forwarding configuration;
3864  * called in response to ND_GET.
3865  */
3866 /* ARGSUSED */
3867 static int
3868 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3869 {
3870 	ill_t *ill = (ill_t *)cp;
3871 
3872 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3873 
3874 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3875 	return (0);
3876 }
3877 
3878 /*
3879  * For obsolete per-interface forwarding configuration;
3880  * called in response to ND_SET.
3881  */
3882 /* ARGSUSED */
3883 static int
3884 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3885     cred_t *ioc_cr)
3886 {
3887 	long value;
3888 	int retval;
3889 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3890 
3891 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3892 
3893 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3894 	    value < 0 || value > 1) {
3895 		return (EINVAL);
3896 	}
3897 
3898 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3899 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3900 	rw_exit(&ipst->ips_ill_g_lock);
3901 	return (retval);
3902 }
3903 
3904 /*
3905  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3906  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3907  * up RTS_IFINFO routing socket messages for each interface whose flags we
3908  * change.
3909  */
3910 int
3911 ill_forward_set(ill_t *ill, boolean_t enable)
3912 {
3913 	ill_group_t *illgrp;
3914 	ip_stack_t	*ipst = ill->ill_ipst;
3915 
3916 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3917 
3918 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3919 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3920 		return (0);
3921 
3922 	if (IS_LOOPBACK(ill))
3923 		return (EINVAL);
3924 
3925 	/*
3926 	 * If the ill is in an IPMP group, set the forwarding policy on all
3927 	 * members of the group to the same value.
3928 	 */
3929 	illgrp = ill->ill_group;
3930 	if (illgrp != NULL) {
3931 		ill_t *tmp_ill;
3932 
3933 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3934 		    tmp_ill = tmp_ill->ill_group_next) {
3935 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3936 			    (enable ? "Enabling" : "Disabling"),
3937 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3938 			    tmp_ill->ill_name));
3939 			mutex_enter(&tmp_ill->ill_lock);
3940 			if (enable)
3941 				tmp_ill->ill_flags |= ILLF_ROUTER;
3942 			else
3943 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3944 			mutex_exit(&tmp_ill->ill_lock);
3945 			if (tmp_ill->ill_isv6)
3946 				ill_set_nce_router_flags(tmp_ill, enable);
3947 			/* Notify routing socket listeners of this change. */
3948 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3949 		}
3950 	} else {
3951 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3952 		    (enable ? "Enabling" : "Disabling"),
3953 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3954 		mutex_enter(&ill->ill_lock);
3955 		if (enable)
3956 			ill->ill_flags |= ILLF_ROUTER;
3957 		else
3958 			ill->ill_flags &= ~ILLF_ROUTER;
3959 		mutex_exit(&ill->ill_lock);
3960 		if (ill->ill_isv6)
3961 			ill_set_nce_router_flags(ill, enable);
3962 		/* Notify routing socket listeners of this change. */
3963 		ip_rts_ifmsg(ill->ill_ipif);
3964 	}
3965 
3966 	return (0);
3967 }
3968 
3969 /*
3970  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3971  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3972  * set or clear.
3973  */
3974 static void
3975 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3976 {
3977 	ipif_t *ipif;
3978 	nce_t *nce;
3979 
3980 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3981 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3982 		if (nce != NULL) {
3983 			mutex_enter(&nce->nce_lock);
3984 			if (enable)
3985 				nce->nce_flags |= NCE_F_ISROUTER;
3986 			else
3987 				nce->nce_flags &= ~NCE_F_ISROUTER;
3988 			mutex_exit(&nce->nce_lock);
3989 			NCE_REFRELE(nce);
3990 		}
3991 	}
3992 }
3993 
3994 /*
3995  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3996  * for this ill.  Make sure the v6/v4 question has been answered about this
3997  * ill.  The creation of this ndd variable is only for backwards compatibility.
3998  * The preferred way to control per-interface IP forwarding is through the
3999  * ILLF_ROUTER interface flag.
4000  */
4001 static int
4002 ill_set_ndd_name(ill_t *ill)
4003 {
4004 	char *suffix;
4005 	ip_stack_t	*ipst = ill->ill_ipst;
4006 
4007 	ASSERT(IAM_WRITER_ILL(ill));
4008 
4009 	if (ill->ill_isv6)
4010 		suffix = ipv6_forward_suffix;
4011 	else
4012 		suffix = ipv4_forward_suffix;
4013 
4014 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4015 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4016 	/*
4017 	 * Copies over the '\0'.
4018 	 * Note that strlen(suffix) is always bounded.
4019 	 */
4020 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4021 	    strlen(suffix) + 1);
4022 
4023 	/*
4024 	 * Use of the nd table requires holding the reader lock.
4025 	 * Modifying the nd table thru nd_load/nd_unload requires
4026 	 * the writer lock.
4027 	 */
4028 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4029 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4030 	    nd_ill_forward_set, (caddr_t)ill)) {
4031 		/*
4032 		 * If the nd_load failed, it only meant that it could not
4033 		 * allocate a new bunch of room for further NDD expansion.
4034 		 * Because of that, the ill_ndd_name will be set to 0, and
4035 		 * this interface is at the mercy of the global ip_forwarding
4036 		 * variable.
4037 		 */
4038 		rw_exit(&ipst->ips_ip_g_nd_lock);
4039 		ill->ill_ndd_name = NULL;
4040 		return (ENOMEM);
4041 	}
4042 	rw_exit(&ipst->ips_ip_g_nd_lock);
4043 	return (0);
4044 }
4045 
4046 /*
4047  * Intializes the context structure and returns the first ill in the list
4048  * cuurently start_list and end_list can have values:
4049  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4050  * IP_V4_G_HEAD		Traverse IPV4 list only.
4051  * IP_V6_G_HEAD		Traverse IPV6 list only.
4052  */
4053 
4054 /*
4055  * We don't check for CONDEMNED ills here. Caller must do that if
4056  * necessary under the ill lock.
4057  */
4058 ill_t *
4059 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4060     ip_stack_t *ipst)
4061 {
4062 	ill_if_t *ifp;
4063 	ill_t *ill;
4064 	avl_tree_t *avl_tree;
4065 
4066 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4067 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4068 
4069 	/*
4070 	 * setup the lists to search
4071 	 */
4072 	if (end_list != MAX_G_HEADS) {
4073 		ctx->ctx_current_list = start_list;
4074 		ctx->ctx_last_list = end_list;
4075 	} else {
4076 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4077 		ctx->ctx_current_list = 0;
4078 	}
4079 
4080 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4081 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4082 		if (ifp != (ill_if_t *)
4083 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4084 			avl_tree = &ifp->illif_avl_by_ppa;
4085 			ill = avl_first(avl_tree);
4086 			/*
4087 			 * ill is guaranteed to be non NULL or ifp should have
4088 			 * not existed.
4089 			 */
4090 			ASSERT(ill != NULL);
4091 			return (ill);
4092 		}
4093 		ctx->ctx_current_list++;
4094 	}
4095 
4096 	return (NULL);
4097 }
4098 
4099 /*
4100  * returns the next ill in the list. ill_first() must have been called
4101  * before calling ill_next() or bad things will happen.
4102  */
4103 
4104 /*
4105  * We don't check for CONDEMNED ills here. Caller must do that if
4106  * necessary under the ill lock.
4107  */
4108 ill_t *
4109 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4110 {
4111 	ill_if_t *ifp;
4112 	ill_t *ill;
4113 	ip_stack_t	*ipst = lastill->ill_ipst;
4114 
4115 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4116 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4117 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4118 	    AVL_AFTER)) != NULL) {
4119 		return (ill);
4120 	}
4121 
4122 	/* goto next ill_ifp in the list. */
4123 	ifp = lastill->ill_ifptr->illif_next;
4124 
4125 	/* make sure not at end of circular list */
4126 	while (ifp ==
4127 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4128 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4129 			return (NULL);
4130 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4131 	}
4132 
4133 	return (avl_first(&ifp->illif_avl_by_ppa));
4134 }
4135 
4136 /*
4137  * Check interface name for correct format which is name+ppa.
4138  * name can contain characters and digits, the right most digits
4139  * make up the ppa number. use of octal is not allowed, name must contain
4140  * a ppa, return pointer to the start of ppa.
4141  * In case of error return NULL.
4142  */
4143 static char *
4144 ill_get_ppa_ptr(char *name)
4145 {
4146 	int namelen = mi_strlen(name);
4147 
4148 	int len = namelen;
4149 
4150 	name += len;
4151 	while (len > 0) {
4152 		name--;
4153 		if (*name < '0' || *name > '9')
4154 			break;
4155 		len--;
4156 	}
4157 
4158 	/* empty string, all digits, or no trailing digits */
4159 	if (len == 0 || len == (int)namelen)
4160 		return (NULL);
4161 
4162 	name++;
4163 	/* check for attempted use of octal */
4164 	if (*name == '0' && len != (int)namelen - 1)
4165 		return (NULL);
4166 	return (name);
4167 }
4168 
4169 /*
4170  * use avl tree to locate the ill.
4171  */
4172 static ill_t *
4173 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4174     ipsq_func_t func, int *error, ip_stack_t *ipst)
4175 {
4176 	char *ppa_ptr = NULL;
4177 	int len;
4178 	uint_t ppa;
4179 	ill_t *ill = NULL;
4180 	ill_if_t *ifp;
4181 	int list;
4182 	ipsq_t *ipsq;
4183 
4184 	if (error != NULL)
4185 		*error = 0;
4186 
4187 	/*
4188 	 * get ppa ptr
4189 	 */
4190 	if (isv6)
4191 		list = IP_V6_G_HEAD;
4192 	else
4193 		list = IP_V4_G_HEAD;
4194 
4195 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4196 		if (error != NULL)
4197 			*error = ENXIO;
4198 		return (NULL);
4199 	}
4200 
4201 	len = ppa_ptr - name + 1;
4202 
4203 	ppa = stoi(&ppa_ptr);
4204 
4205 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4206 
4207 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4208 		/*
4209 		 * match is done on len - 1 as the name is not null
4210 		 * terminated it contains ppa in addition to the interface
4211 		 * name.
4212 		 */
4213 		if ((ifp->illif_name_len == len) &&
4214 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4215 			break;
4216 		} else {
4217 			ifp = ifp->illif_next;
4218 		}
4219 	}
4220 
4221 
4222 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4223 		/*
4224 		 * Even the interface type does not exist.
4225 		 */
4226 		if (error != NULL)
4227 			*error = ENXIO;
4228 		return (NULL);
4229 	}
4230 
4231 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4232 	if (ill != NULL) {
4233 		/*
4234 		 * The block comment at the start of ipif_down
4235 		 * explains the use of the macros used below
4236 		 */
4237 		GRAB_CONN_LOCK(q);
4238 		mutex_enter(&ill->ill_lock);
4239 		if (ILL_CAN_LOOKUP(ill)) {
4240 			ill_refhold_locked(ill);
4241 			mutex_exit(&ill->ill_lock);
4242 			RELEASE_CONN_LOCK(q);
4243 			return (ill);
4244 		} else if (ILL_CAN_WAIT(ill, q)) {
4245 			ipsq = ill->ill_phyint->phyint_ipsq;
4246 			mutex_enter(&ipsq->ipsq_lock);
4247 			mutex_exit(&ill->ill_lock);
4248 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4249 			mutex_exit(&ipsq->ipsq_lock);
4250 			RELEASE_CONN_LOCK(q);
4251 			*error = EINPROGRESS;
4252 			return (NULL);
4253 		}
4254 		mutex_exit(&ill->ill_lock);
4255 		RELEASE_CONN_LOCK(q);
4256 	}
4257 	if (error != NULL)
4258 		*error = ENXIO;
4259 	return (NULL);
4260 }
4261 
4262 /*
4263  * comparison function for use with avl.
4264  */
4265 static int
4266 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4267 {
4268 	uint_t ppa;
4269 	uint_t ill_ppa;
4270 
4271 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4272 
4273 	ppa = *((uint_t *)ppa_ptr);
4274 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4275 	/*
4276 	 * We want the ill with the lowest ppa to be on the
4277 	 * top.
4278 	 */
4279 	if (ill_ppa < ppa)
4280 		return (1);
4281 	if (ill_ppa > ppa)
4282 		return (-1);
4283 	return (0);
4284 }
4285 
4286 /*
4287  * remove an interface type from the global list.
4288  */
4289 static void
4290 ill_delete_interface_type(ill_if_t *interface)
4291 {
4292 	ASSERT(interface != NULL);
4293 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4294 
4295 	avl_destroy(&interface->illif_avl_by_ppa);
4296 	if (interface->illif_ppa_arena != NULL)
4297 		vmem_destroy(interface->illif_ppa_arena);
4298 
4299 	remque(interface);
4300 
4301 	mi_free(interface);
4302 }
4303 
4304 /* Defined in ip_netinfo.c */
4305 extern ddi_taskq_t	*eventq_queue_nic;
4306 
4307 /*
4308  * remove ill from the global list.
4309  */
4310 static void
4311 ill_glist_delete(ill_t *ill)
4312 {
4313 	char *nicname;
4314 	size_t nicnamelen;
4315 	hook_nic_event_t *info;
4316 	ip_stack_t	*ipst;
4317 
4318 	if (ill == NULL)
4319 		return;
4320 	ipst = ill->ill_ipst;
4321 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4322 
4323 	if (ill->ill_name != NULL) {
4324 		nicname = kmem_alloc(ill->ill_name_length, KM_NOSLEEP);
4325 		if (nicname != NULL) {
4326 			bcopy(ill->ill_name, nicname, ill->ill_name_length);
4327 			nicnamelen = ill->ill_name_length;
4328 		}
4329 	} else {
4330 		nicname = NULL;
4331 		nicnamelen = 0;
4332 	}
4333 
4334 	/*
4335 	 * If the ill was never inserted into the AVL tree
4336 	 * we skip the if branch.
4337 	 */
4338 	if (ill->ill_ifptr != NULL) {
4339 		/*
4340 		 * remove from AVL tree and free ppa number
4341 		 */
4342 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4343 
4344 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4345 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4346 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4347 		}
4348 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4349 			ill_delete_interface_type(ill->ill_ifptr);
4350 		}
4351 
4352 		/*
4353 		 * Indicate ill is no longer in the list.
4354 		 */
4355 		ill->ill_ifptr = NULL;
4356 		ill->ill_name_length = 0;
4357 		ill->ill_name[0] = '\0';
4358 		ill->ill_ppa = UINT_MAX;
4359 	}
4360 
4361 	/*
4362 	 * Run the unplumb hook after the NIC has disappeared from being
4363 	 * visible so that attempts to revalidate its existance will fail.
4364 	 *
4365 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4366 	 * that the ordering of delivered events to listeners matches the
4367 	 * order of them in the kernel.
4368 	 */
4369 	if ((info = ill->ill_nic_event_info) != NULL) {
4370 		if (info->hne_event != NE_DOWN) {
4371 			ip2dbg(("ill_glist_delete: unexpected nic event %d "
4372 			    "attached for %s\n", info->hne_event,
4373 			    ill->ill_name));
4374 			if (info->hne_data != NULL)
4375 				kmem_free(info->hne_data, info->hne_datalen);
4376 			kmem_free(info, sizeof (hook_nic_event_t));
4377 		} else {
4378 			if (ddi_taskq_dispatch(eventq_queue_nic,
4379 			    ip_ne_queue_func, (void *)info, DDI_SLEEP)
4380 			    == DDI_FAILURE) {
4381 				ip2dbg(("ill_glist_delete: ddi_taskq_dispatch "
4382 				    "failed\n"));
4383 				if (info->hne_data != NULL)
4384 					kmem_free(info->hne_data,
4385 					    info->hne_datalen);
4386 				kmem_free(info, sizeof (hook_nic_event_t));
4387 			}
4388 		}
4389 	}
4390 
4391 	/* Generate NE_UNPLUMB event for ill_name. */
4392 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
4393 	if (info != NULL) {
4394 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
4395 		info->hne_lif = 0;
4396 		info->hne_event = NE_UNPLUMB;
4397 		info->hne_data = nicname;
4398 		info->hne_datalen = nicnamelen;
4399 		info->hne_family = ill->ill_isv6 ?
4400 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
4401 	} else {
4402 		ip2dbg(("ill_glist_delete: could not attach UNPLUMB nic event "
4403 		    "information for %s (ENOMEM)\n", ill->ill_name));
4404 		if (nicname != NULL)
4405 			kmem_free(nicname, nicnamelen);
4406 	}
4407 
4408 	ill->ill_nic_event_info = info;
4409 
4410 	ill_phyint_free(ill);
4411 	rw_exit(&ipst->ips_ill_g_lock);
4412 }
4413 
4414 /*
4415  * allocate a ppa, if the number of plumbed interfaces of this type are
4416  * less than ill_no_arena do a linear search to find a unused ppa.
4417  * When the number goes beyond ill_no_arena switch to using an arena.
4418  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4419  * is the return value for an error condition, so allocation starts at one
4420  * and is decremented by one.
4421  */
4422 static int
4423 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4424 {
4425 	ill_t *tmp_ill;
4426 	uint_t start, end;
4427 	int ppa;
4428 
4429 	if (ifp->illif_ppa_arena == NULL &&
4430 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4431 		/*
4432 		 * Create an arena.
4433 		 */
4434 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4435 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4436 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4437 			/* allocate what has already been assigned */
4438 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4439 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4440 		    tmp_ill, AVL_AFTER)) {
4441 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4442 			    1,		/* size */
4443 			    1,		/* align/quantum */
4444 			    0,		/* phase */
4445 			    0,		/* nocross */
4446 			    /* minaddr */
4447 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4448 			    /* maxaddr */
4449 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4450 			    VM_NOSLEEP|VM_FIRSTFIT);
4451 			if (ppa == 0) {
4452 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4453 				    " failed while switching"));
4454 				vmem_destroy(ifp->illif_ppa_arena);
4455 				ifp->illif_ppa_arena = NULL;
4456 				break;
4457 			}
4458 		}
4459 	}
4460 
4461 	if (ifp->illif_ppa_arena != NULL) {
4462 		if (ill->ill_ppa == UINT_MAX) {
4463 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4464 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4465 			if (ppa == 0)
4466 				return (EAGAIN);
4467 			ill->ill_ppa = --ppa;
4468 		} else {
4469 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4470 			    1, 		/* size */
4471 			    1, 		/* align/quantum */
4472 			    0, 		/* phase */
4473 			    0, 		/* nocross */
4474 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4475 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4476 			    VM_NOSLEEP|VM_FIRSTFIT);
4477 			/*
4478 			 * Most likely the allocation failed because
4479 			 * the requested ppa was in use.
4480 			 */
4481 			if (ppa == 0)
4482 				return (EEXIST);
4483 		}
4484 		return (0);
4485 	}
4486 
4487 	/*
4488 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4489 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4490 	 */
4491 	if (ill->ill_ppa == UINT_MAX) {
4492 		end = UINT_MAX - 1;
4493 		start = 0;
4494 	} else {
4495 		end = start = ill->ill_ppa;
4496 	}
4497 
4498 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4499 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4500 		if (start++ >= end) {
4501 			if (ill->ill_ppa == UINT_MAX)
4502 				return (EAGAIN);
4503 			else
4504 				return (EEXIST);
4505 		}
4506 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4507 	}
4508 	ill->ill_ppa = start;
4509 	return (0);
4510 }
4511 
4512 /*
4513  * Insert ill into the list of configured ill's. Once this function completes,
4514  * the ill is globally visible and is available through lookups. More precisely
4515  * this happens after the caller drops the ill_g_lock.
4516  */
4517 static int
4518 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4519 {
4520 	ill_if_t *ill_interface;
4521 	avl_index_t where = 0;
4522 	int error;
4523 	int name_length;
4524 	int index;
4525 	boolean_t check_length = B_FALSE;
4526 	ip_stack_t	*ipst = ill->ill_ipst;
4527 
4528 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4529 
4530 	name_length = mi_strlen(name) + 1;
4531 
4532 	if (isv6)
4533 		index = IP_V6_G_HEAD;
4534 	else
4535 		index = IP_V4_G_HEAD;
4536 
4537 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4538 	/*
4539 	 * Search for interface type based on name
4540 	 */
4541 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4542 		if ((ill_interface->illif_name_len == name_length) &&
4543 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4544 			break;
4545 		}
4546 		ill_interface = ill_interface->illif_next;
4547 	}
4548 
4549 	/*
4550 	 * Interface type not found, create one.
4551 	 */
4552 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4553 
4554 		ill_g_head_t ghead;
4555 
4556 		/*
4557 		 * allocate ill_if_t structure
4558 		 */
4559 
4560 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4561 		if (ill_interface == NULL) {
4562 			return (ENOMEM);
4563 		}
4564 
4565 
4566 
4567 		(void) strcpy(ill_interface->illif_name, name);
4568 		ill_interface->illif_name_len = name_length;
4569 
4570 		avl_create(&ill_interface->illif_avl_by_ppa,
4571 		    ill_compare_ppa, sizeof (ill_t),
4572 		    offsetof(struct ill_s, ill_avl_byppa));
4573 
4574 		/*
4575 		 * link the structure in the back to maintain order
4576 		 * of configuration for ifconfig output.
4577 		 */
4578 		ghead = ipst->ips_ill_g_heads[index];
4579 		insque(ill_interface, ghead.ill_g_list_tail);
4580 
4581 	}
4582 
4583 	if (ill->ill_ppa == UINT_MAX)
4584 		check_length = B_TRUE;
4585 
4586 	error = ill_alloc_ppa(ill_interface, ill);
4587 	if (error != 0) {
4588 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4589 			ill_delete_interface_type(ill->ill_ifptr);
4590 		return (error);
4591 	}
4592 
4593 	/*
4594 	 * When the ppa is choosen by the system, check that there is
4595 	 * enough space to insert ppa. if a specific ppa was passed in this
4596 	 * check is not required as the interface name passed in will have
4597 	 * the right ppa in it.
4598 	 */
4599 	if (check_length) {
4600 		/*
4601 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4602 		 */
4603 		char buf[sizeof (uint_t) * 3];
4604 
4605 		/*
4606 		 * convert ppa to string to calculate the amount of space
4607 		 * required for it in the name.
4608 		 */
4609 		numtos(ill->ill_ppa, buf);
4610 
4611 		/* Do we have enough space to insert ppa ? */
4612 
4613 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4614 			/* Free ppa and interface type struct */
4615 			if (ill_interface->illif_ppa_arena != NULL) {
4616 				vmem_free(ill_interface->illif_ppa_arena,
4617 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4618 			}
4619 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4620 			    0) {
4621 				ill_delete_interface_type(ill->ill_ifptr);
4622 			}
4623 
4624 			return (EINVAL);
4625 		}
4626 	}
4627 
4628 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4629 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4630 
4631 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4632 	    &where);
4633 	ill->ill_ifptr = ill_interface;
4634 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4635 
4636 	ill_phyint_reinit(ill);
4637 	return (0);
4638 }
4639 
4640 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4641 static boolean_t
4642 ipsq_init(ill_t *ill)
4643 {
4644 	ipsq_t  *ipsq;
4645 
4646 	/* Init the ipsq and impicitly enter as writer */
4647 	ill->ill_phyint->phyint_ipsq =
4648 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4649 	if (ill->ill_phyint->phyint_ipsq == NULL)
4650 		return (B_FALSE);
4651 	ipsq = ill->ill_phyint->phyint_ipsq;
4652 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4653 	ill->ill_phyint->phyint_ipsq_next = NULL;
4654 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4655 	ipsq->ipsq_refs = 1;
4656 	ipsq->ipsq_writer = curthread;
4657 	ipsq->ipsq_reentry_cnt = 1;
4658 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4659 #ifdef ILL_DEBUG
4660 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4661 #endif
4662 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4663 	return (B_TRUE);
4664 }
4665 
4666 /*
4667  * ill_init is called by ip_open when a device control stream is opened.
4668  * It does a few initializations, and shoots a DL_INFO_REQ message down
4669  * to the driver.  The response is later picked up in ip_rput_dlpi and
4670  * used to set up default mechanisms for talking to the driver.  (Always
4671  * called as writer.)
4672  *
4673  * If this function returns error, ip_open will call ip_close which in
4674  * turn will call ill_delete to clean up any memory allocated here that
4675  * is not yet freed.
4676  */
4677 int
4678 ill_init(queue_t *q, ill_t *ill)
4679 {
4680 	int	count;
4681 	dl_info_req_t	*dlir;
4682 	mblk_t	*info_mp;
4683 	uchar_t *frag_ptr;
4684 
4685 	/*
4686 	 * The ill is initialized to zero by mi_alloc*(). In addition
4687 	 * some fields already contain valid values, initialized in
4688 	 * ip_open(), before we reach here.
4689 	 */
4690 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4691 
4692 	ill->ill_rq = q;
4693 	ill->ill_wq = WR(q);
4694 
4695 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4696 	    BPRI_HI);
4697 	if (info_mp == NULL)
4698 		return (ENOMEM);
4699 
4700 	/*
4701 	 * Allocate sufficient space to contain our fragment hash table and
4702 	 * the device name.
4703 	 */
4704 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4705 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4706 	if (frag_ptr == NULL) {
4707 		freemsg(info_mp);
4708 		return (ENOMEM);
4709 	}
4710 	ill->ill_frag_ptr = frag_ptr;
4711 	ill->ill_frag_free_num_pkts = 0;
4712 	ill->ill_last_frag_clean_time = 0;
4713 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4714 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4715 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4716 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4717 		    NULL, MUTEX_DEFAULT, NULL);
4718 	}
4719 
4720 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4721 	if (ill->ill_phyint == NULL) {
4722 		freemsg(info_mp);
4723 		mi_free(frag_ptr);
4724 		return (ENOMEM);
4725 	}
4726 
4727 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4728 	/*
4729 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4730 	 * at this point because of the following reason. If we can't
4731 	 * enter the ipsq at some point and cv_wait, the writer that
4732 	 * wakes us up tries to locate us using the list of all phyints
4733 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4734 	 * If we don't set it now, we risk a missed wakeup.
4735 	 */
4736 	ill->ill_phyint->phyint_illv4 = ill;
4737 	ill->ill_ppa = UINT_MAX;
4738 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4739 
4740 	if (!ipsq_init(ill)) {
4741 		freemsg(info_mp);
4742 		mi_free(frag_ptr);
4743 		mi_free(ill->ill_phyint);
4744 		return (ENOMEM);
4745 	}
4746 
4747 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4748 
4749 
4750 	/* Frag queue limit stuff */
4751 	ill->ill_frag_count = 0;
4752 	ill->ill_ipf_gen = 0;
4753 
4754 	ill->ill_global_timer = INFINITY;
4755 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4756 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4757 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4758 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4759 
4760 	/*
4761 	 * Initialize IPv6 configuration variables.  The IP module is always
4762 	 * opened as an IPv4 module.  Instead tracking down the cases where
4763 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4764 	 * here for convenience, this has no effect until the ill is set to do
4765 	 * IPv6.
4766 	 */
4767 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4768 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4769 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4770 	ill->ill_max_buf = ND_MAX_Q;
4771 	ill->ill_refcnt = 0;
4772 
4773 	/* Send down the Info Request to the driver. */
4774 	info_mp->b_datap->db_type = M_PCPROTO;
4775 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4776 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4777 	dlir->dl_primitive = DL_INFO_REQ;
4778 
4779 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4780 
4781 	qprocson(q);
4782 	ill_dlpi_send(ill, info_mp);
4783 
4784 	return (0);
4785 }
4786 
4787 /*
4788  * ill_dls_info
4789  * creates datalink socket info from the device.
4790  */
4791 int
4792 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4793 {
4794 	size_t	len;
4795 	ill_t	*ill = ipif->ipif_ill;
4796 
4797 	sdl->sdl_family = AF_LINK;
4798 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4799 	sdl->sdl_type = ill->ill_type;
4800 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4801 	len = strlen(sdl->sdl_data);
4802 	ASSERT(len < 256);
4803 	sdl->sdl_nlen = (uchar_t)len;
4804 	sdl->sdl_alen = ill->ill_phys_addr_length;
4805 	sdl->sdl_slen = 0;
4806 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4807 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4808 
4809 	return (sizeof (struct sockaddr_dl));
4810 }
4811 
4812 /*
4813  * ill_xarp_info
4814  * creates xarp info from the device.
4815  */
4816 static int
4817 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4818 {
4819 	sdl->sdl_family = AF_LINK;
4820 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4821 	sdl->sdl_type = ill->ill_type;
4822 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4823 	    sizeof (sdl->sdl_data));
4824 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4825 	sdl->sdl_alen = ill->ill_phys_addr_length;
4826 	sdl->sdl_slen = 0;
4827 	return (sdl->sdl_nlen);
4828 }
4829 
4830 static int
4831 loopback_kstat_update(kstat_t *ksp, int rw)
4832 {
4833 	kstat_named_t *kn;
4834 	netstackid_t	stackid;
4835 	netstack_t	*ns;
4836 	ip_stack_t	*ipst;
4837 
4838 	if (ksp == NULL || ksp->ks_data == NULL)
4839 		return (EIO);
4840 
4841 	if (rw == KSTAT_WRITE)
4842 		return (EACCES);
4843 
4844 	kn = KSTAT_NAMED_PTR(ksp);
4845 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4846 
4847 	ns = netstack_find_by_stackid(stackid);
4848 	if (ns == NULL)
4849 		return (-1);
4850 
4851 	ipst = ns->netstack_ip;
4852 	if (ipst == NULL) {
4853 		netstack_rele(ns);
4854 		return (-1);
4855 	}
4856 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4857 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4858 	netstack_rele(ns);
4859 	return (0);
4860 }
4861 
4862 
4863 /*
4864  * Has ifindex been plumbed already.
4865  * Compares both phyint_ifindex and phyint_group_ifindex.
4866  */
4867 static boolean_t
4868 phyint_exists(uint_t index, ip_stack_t *ipst)
4869 {
4870 	phyint_t *phyi;
4871 
4872 	ASSERT(index != 0);
4873 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4874 	/*
4875 	 * Indexes are stored in the phyint - a common structure
4876 	 * to both IPv4 and IPv6.
4877 	 */
4878 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4879 	for (; phyi != NULL;
4880 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4881 	    phyi, AVL_AFTER)) {
4882 		if (phyi->phyint_ifindex == index ||
4883 		    phyi->phyint_group_ifindex == index)
4884 			return (B_TRUE);
4885 	}
4886 	return (B_FALSE);
4887 }
4888 
4889 /* Pick a unique ifindex */
4890 boolean_t
4891 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4892 {
4893 	uint_t starting_index;
4894 
4895 	if (!ipst->ips_ill_index_wrap) {
4896 		*indexp = ipst->ips_ill_index++;
4897 		if (ipst->ips_ill_index == 0) {
4898 			/* Reached the uint_t limit Next time wrap  */
4899 			ipst->ips_ill_index_wrap = B_TRUE;
4900 		}
4901 		return (B_TRUE);
4902 	}
4903 
4904 	/*
4905 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4906 	 * at this point and don't want to call any function that attempts
4907 	 * to get the lock again.
4908 	 */
4909 	starting_index = ipst->ips_ill_index++;
4910 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4911 		if (ipst->ips_ill_index != 0 &&
4912 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4913 			/* found unused index - use it */
4914 			*indexp = ipst->ips_ill_index;
4915 			return (B_TRUE);
4916 		}
4917 	}
4918 
4919 	/*
4920 	 * all interface indicies are inuse.
4921 	 */
4922 	return (B_FALSE);
4923 }
4924 
4925 /*
4926  * Assign a unique interface index for the phyint.
4927  */
4928 static boolean_t
4929 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4930 {
4931 	ASSERT(phyi->phyint_ifindex == 0);
4932 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4933 }
4934 
4935 /*
4936  * Return a pointer to the ill which matches the supplied name.  Note that
4937  * the ill name length includes the null termination character.  (May be
4938  * called as writer.)
4939  * If do_alloc and the interface is "lo0" it will be automatically created.
4940  * Cannot bump up reference on condemned ills. So dup detect can't be done
4941  * using this func.
4942  */
4943 ill_t *
4944 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4945     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4946     ip_stack_t *ipst)
4947 {
4948 	ill_t	*ill;
4949 	ipif_t	*ipif;
4950 	kstat_named_t	*kn;
4951 	boolean_t isloopback;
4952 	ipsq_t *old_ipsq;
4953 	in6_addr_t ov6addr;
4954 
4955 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4956 
4957 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4958 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4959 	rw_exit(&ipst->ips_ill_g_lock);
4960 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4961 		return (ill);
4962 
4963 	/*
4964 	 * Couldn't find it.  Does this happen to be a lookup for the
4965 	 * loopback device and are we allowed to allocate it?
4966 	 */
4967 	if (!isloopback || !do_alloc)
4968 		return (NULL);
4969 
4970 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4971 
4972 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4973 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4974 		rw_exit(&ipst->ips_ill_g_lock);
4975 		return (ill);
4976 	}
4977 
4978 	/* Create the loopback device on demand */
4979 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4980 	    sizeof (ipif_loopback_name), BPRI_MED));
4981 	if (ill == NULL)
4982 		goto done;
4983 
4984 	*ill = ill_null;
4985 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4986 	ill->ill_ipst = ipst;
4987 	netstack_hold(ipst->ips_netstack);
4988 	/*
4989 	 * For exclusive stacks we set the zoneid to zero
4990 	 * to make IP operate as if in the global zone.
4991 	 */
4992 	ill->ill_zoneid = GLOBAL_ZONEID;
4993 
4994 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4995 	if (ill->ill_phyint == NULL)
4996 		goto done;
4997 
4998 	if (isv6)
4999 		ill->ill_phyint->phyint_illv6 = ill;
5000 	else
5001 		ill->ill_phyint->phyint_illv4 = ill;
5002 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
5003 	ill->ill_max_frag = IP_LOOPBACK_MTU;
5004 	/* Add room for tcp+ip headers */
5005 	if (isv6) {
5006 		ill->ill_isv6 = B_TRUE;
5007 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
5008 	} else {
5009 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
5010 	}
5011 	if (!ill_allocate_mibs(ill))
5012 		goto done;
5013 	ill->ill_max_mtu = ill->ill_max_frag;
5014 	/*
5015 	 * ipif_loopback_name can't be pointed at directly because its used
5016 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
5017 	 * from the glist, ill_glist_delete() sets the first character of
5018 	 * ill_name to '\0'.
5019 	 */
5020 	ill->ill_name = (char *)ill + sizeof (*ill);
5021 	(void) strcpy(ill->ill_name, ipif_loopback_name);
5022 	ill->ill_name_length = sizeof (ipif_loopback_name);
5023 	/* Set ill_name_set for ill_phyint_reinit to work properly */
5024 
5025 	ill->ill_global_timer = INFINITY;
5026 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
5027 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
5028 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
5029 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
5030 
5031 	/* No resolver here. */
5032 	ill->ill_net_type = IRE_LOOPBACK;
5033 
5034 	/* Initialize the ipsq */
5035 	if (!ipsq_init(ill))
5036 		goto done;
5037 
5038 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5039 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5040 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5041 #ifdef ILL_DEBUG
5042 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5043 #endif
5044 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5045 	if (ipif == NULL)
5046 		goto done;
5047 
5048 	ill->ill_flags = ILLF_MULTICAST;
5049 
5050 	ov6addr = ipif->ipif_v6lcl_addr;
5051 	/* Set up default loopback address and mask. */
5052 	if (!isv6) {
5053 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5054 
5055 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5056 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5057 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5058 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5059 		    ipif->ipif_v6subnet);
5060 		ill->ill_flags |= ILLF_IPV4;
5061 	} else {
5062 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5063 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5064 		ipif->ipif_v6net_mask = ipv6_all_ones;
5065 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5066 		    ipif->ipif_v6subnet);
5067 		ill->ill_flags |= ILLF_IPV6;
5068 	}
5069 
5070 	/*
5071 	 * Chain us in at the end of the ill list. hold the ill
5072 	 * before we make it globally visible. 1 for the lookup.
5073 	 */
5074 	ill->ill_refcnt = 0;
5075 	ill_refhold(ill);
5076 
5077 	ill->ill_frag_count = 0;
5078 	ill->ill_frag_free_num_pkts = 0;
5079 	ill->ill_last_frag_clean_time = 0;
5080 
5081 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5082 
5083 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5084 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5085 
5086 	/* Let SCTP know so that it can add this to its list */
5087 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5088 
5089 	/*
5090 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5091 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5092 	 * requires to be after ill_glist_insert() since we need the
5093 	 * ill_index set. Pass on ipv6_loopback as the old address.
5094 	 */
5095 	sctp_update_ipif_addr(ipif, ov6addr);
5096 
5097 	/*
5098 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5099 	 */
5100 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5101 		/* Loopback ills aren't in any IPMP group */
5102 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5103 		ipsq_delete(old_ipsq);
5104 	}
5105 
5106 	/*
5107 	 * Delay this till the ipif is allocated as ipif_allocate
5108 	 * de-references ill_phyint for getting the ifindex. We
5109 	 * can't do this before ipif_allocate because ill_phyint_reinit
5110 	 * -> phyint_assign_ifindex expects ipif to be present.
5111 	 */
5112 	mutex_enter(&ill->ill_phyint->phyint_lock);
5113 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5114 	mutex_exit(&ill->ill_phyint->phyint_lock);
5115 
5116 	if (ipst->ips_loopback_ksp == NULL) {
5117 		/* Export loopback interface statistics */
5118 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5119 		    ipif_loopback_name, "net",
5120 		    KSTAT_TYPE_NAMED, 2, 0,
5121 		    ipst->ips_netstack->netstack_stackid);
5122 		if (ipst->ips_loopback_ksp != NULL) {
5123 			ipst->ips_loopback_ksp->ks_update =
5124 			    loopback_kstat_update;
5125 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5126 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5127 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5128 			ipst->ips_loopback_ksp->ks_private =
5129 			    (void *)(uintptr_t)ipst->ips_netstack->
5130 			    netstack_stackid;
5131 			kstat_install(ipst->ips_loopback_ksp);
5132 		}
5133 	}
5134 
5135 	if (error != NULL)
5136 		*error = 0;
5137 	*did_alloc = B_TRUE;
5138 	rw_exit(&ipst->ips_ill_g_lock);
5139 	return (ill);
5140 done:
5141 	if (ill != NULL) {
5142 		if (ill->ill_phyint != NULL) {
5143 			ipsq_t	*ipsq;
5144 
5145 			ipsq = ill->ill_phyint->phyint_ipsq;
5146 			if (ipsq != NULL) {
5147 				ipsq->ipsq_ipst = NULL;
5148 				kmem_free(ipsq, sizeof (ipsq_t));
5149 			}
5150 			mi_free(ill->ill_phyint);
5151 		}
5152 		ill_free_mib(ill);
5153 		if (ill->ill_ipst != NULL)
5154 			netstack_rele(ill->ill_ipst->ips_netstack);
5155 		mi_free(ill);
5156 	}
5157 	rw_exit(&ipst->ips_ill_g_lock);
5158 	if (error != NULL)
5159 		*error = ENOMEM;
5160 	return (NULL);
5161 }
5162 
5163 /*
5164  * For IPP calls - use the ip_stack_t for global stack.
5165  */
5166 ill_t *
5167 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5168     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5169 {
5170 	ip_stack_t	*ipst;
5171 	ill_t		*ill;
5172 
5173 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5174 	if (ipst == NULL) {
5175 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5176 		return (NULL);
5177 	}
5178 
5179 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5180 	netstack_rele(ipst->ips_netstack);
5181 	return (ill);
5182 }
5183 
5184 /*
5185  * Return a pointer to the ill which matches the index and IP version type.
5186  */
5187 ill_t *
5188 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5189     ipsq_func_t func, int *err, ip_stack_t *ipst)
5190 {
5191 	ill_t	*ill;
5192 	ipsq_t  *ipsq;
5193 	phyint_t *phyi;
5194 
5195 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5196 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5197 
5198 	if (err != NULL)
5199 		*err = 0;
5200 
5201 	/*
5202 	 * Indexes are stored in the phyint - a common structure
5203 	 * to both IPv4 and IPv6.
5204 	 */
5205 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5206 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5207 	    (void *) &index, NULL);
5208 	if (phyi != NULL) {
5209 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5210 		if (ill != NULL) {
5211 			/*
5212 			 * The block comment at the start of ipif_down
5213 			 * explains the use of the macros used below
5214 			 */
5215 			GRAB_CONN_LOCK(q);
5216 			mutex_enter(&ill->ill_lock);
5217 			if (ILL_CAN_LOOKUP(ill)) {
5218 				ill_refhold_locked(ill);
5219 				mutex_exit(&ill->ill_lock);
5220 				RELEASE_CONN_LOCK(q);
5221 				rw_exit(&ipst->ips_ill_g_lock);
5222 				return (ill);
5223 			} else if (ILL_CAN_WAIT(ill, q)) {
5224 				ipsq = ill->ill_phyint->phyint_ipsq;
5225 				mutex_enter(&ipsq->ipsq_lock);
5226 				rw_exit(&ipst->ips_ill_g_lock);
5227 				mutex_exit(&ill->ill_lock);
5228 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5229 				mutex_exit(&ipsq->ipsq_lock);
5230 				RELEASE_CONN_LOCK(q);
5231 				*err = EINPROGRESS;
5232 				return (NULL);
5233 			}
5234 			RELEASE_CONN_LOCK(q);
5235 			mutex_exit(&ill->ill_lock);
5236 		}
5237 	}
5238 	rw_exit(&ipst->ips_ill_g_lock);
5239 	if (err != NULL)
5240 		*err = ENXIO;
5241 	return (NULL);
5242 }
5243 
5244 /*
5245  * Return the ifindex next in sequence after the passed in ifindex.
5246  * If there is no next ifindex for the given protocol, return 0.
5247  */
5248 uint_t
5249 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5250 {
5251 	phyint_t *phyi;
5252 	phyint_t *phyi_initial;
5253 	uint_t   ifindex;
5254 
5255 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5256 
5257 	if (index == 0) {
5258 		phyi = avl_first(
5259 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5260 	} else {
5261 		phyi = phyi_initial = avl_find(
5262 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5263 		    (void *) &index, NULL);
5264 	}
5265 
5266 	for (; phyi != NULL;
5267 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5268 	    phyi, AVL_AFTER)) {
5269 		/*
5270 		 * If we're not returning the first interface in the tree
5271 		 * and we still haven't moved past the phyint_t that
5272 		 * corresponds to index, avl_walk needs to be called again
5273 		 */
5274 		if (!((index != 0) && (phyi == phyi_initial))) {
5275 			if (isv6) {
5276 				if ((phyi->phyint_illv6) &&
5277 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5278 				    (phyi->phyint_illv6->ill_isv6 == 1))
5279 					break;
5280 			} else {
5281 				if ((phyi->phyint_illv4) &&
5282 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5283 				    (phyi->phyint_illv4->ill_isv6 == 0))
5284 					break;
5285 			}
5286 		}
5287 	}
5288 
5289 	rw_exit(&ipst->ips_ill_g_lock);
5290 
5291 	if (phyi != NULL)
5292 		ifindex = phyi->phyint_ifindex;
5293 	else
5294 		ifindex = 0;
5295 
5296 	return (ifindex);
5297 }
5298 
5299 
5300 /*
5301  * Return the ifindex for the named interface.
5302  * If there is no next ifindex for the interface, return 0.
5303  */
5304 uint_t
5305 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5306 {
5307 	phyint_t	*phyi;
5308 	avl_index_t	where = 0;
5309 	uint_t		ifindex;
5310 
5311 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5312 
5313 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5314 	    name, &where)) == NULL) {
5315 		rw_exit(&ipst->ips_ill_g_lock);
5316 		return (0);
5317 	}
5318 
5319 	ifindex = phyi->phyint_ifindex;
5320 
5321 	rw_exit(&ipst->ips_ill_g_lock);
5322 
5323 	return (ifindex);
5324 }
5325 
5326 
5327 /*
5328  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5329  * that gives a running thread a reference to the ill. This reference must be
5330  * released by the thread when it is done accessing the ill and related
5331  * objects. ill_refcnt can not be used to account for static references
5332  * such as other structures pointing to an ill. Callers must generally
5333  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5334  * or be sure that the ill is not being deleted or changing state before
5335  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5336  * ill won't change any of its critical state such as address, netmask etc.
5337  */
5338 void
5339 ill_refhold(ill_t *ill)
5340 {
5341 	mutex_enter(&ill->ill_lock);
5342 	ill->ill_refcnt++;
5343 	ILL_TRACE_REF(ill);
5344 	mutex_exit(&ill->ill_lock);
5345 }
5346 
5347 void
5348 ill_refhold_locked(ill_t *ill)
5349 {
5350 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5351 	ill->ill_refcnt++;
5352 	ILL_TRACE_REF(ill);
5353 }
5354 
5355 int
5356 ill_check_and_refhold(ill_t *ill)
5357 {
5358 	mutex_enter(&ill->ill_lock);
5359 	if (ILL_CAN_LOOKUP(ill)) {
5360 		ill_refhold_locked(ill);
5361 		mutex_exit(&ill->ill_lock);
5362 		return (0);
5363 	}
5364 	mutex_exit(&ill->ill_lock);
5365 	return (ILL_LOOKUP_FAILED);
5366 }
5367 
5368 /*
5369  * Must not be called while holding any locks. Otherwise if this is
5370  * the last reference to be released, there is a chance of recursive mutex
5371  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5372  * to restart an ioctl.
5373  */
5374 void
5375 ill_refrele(ill_t *ill)
5376 {
5377 	mutex_enter(&ill->ill_lock);
5378 	ASSERT(ill->ill_refcnt != 0);
5379 	ill->ill_refcnt--;
5380 	ILL_UNTRACE_REF(ill);
5381 	if (ill->ill_refcnt != 0) {
5382 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5383 		mutex_exit(&ill->ill_lock);
5384 		return;
5385 	}
5386 
5387 	/* Drops the ill_lock */
5388 	ipif_ill_refrele_tail(ill);
5389 }
5390 
5391 /*
5392  * Obtain a weak reference count on the ill. This reference ensures the
5393  * ill won't be freed, but the ill may change any of its critical state
5394  * such as netmask, address etc. Returns an error if the ill has started
5395  * closing.
5396  */
5397 boolean_t
5398 ill_waiter_inc(ill_t *ill)
5399 {
5400 	mutex_enter(&ill->ill_lock);
5401 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5402 		mutex_exit(&ill->ill_lock);
5403 		return (B_FALSE);
5404 	}
5405 	ill->ill_waiters++;
5406 	mutex_exit(&ill->ill_lock);
5407 	return (B_TRUE);
5408 }
5409 
5410 void
5411 ill_waiter_dcr(ill_t *ill)
5412 {
5413 	mutex_enter(&ill->ill_lock);
5414 	ill->ill_waiters--;
5415 	if (ill->ill_waiters == 0)
5416 		cv_broadcast(&ill->ill_cv);
5417 	mutex_exit(&ill->ill_lock);
5418 }
5419 
5420 /*
5421  * Named Dispatch routine to produce a formatted report on all ILLs.
5422  * This report is accessed by using the ndd utility to "get" ND variable
5423  * "ip_ill_status".
5424  */
5425 /* ARGSUSED */
5426 int
5427 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5428 {
5429 	ill_t		*ill;
5430 	ill_walk_context_t ctx;
5431 	ip_stack_t	*ipst;
5432 
5433 	ipst = CONNQ_TO_IPST(q);
5434 
5435 	(void) mi_mpprintf(mp,
5436 	    "ILL      " MI_COL_HDRPAD_STR
5437 	/*   01234567[89ABCDEF] */
5438 	    "rq       " MI_COL_HDRPAD_STR
5439 	/*   01234567[89ABCDEF] */
5440 	    "wq       " MI_COL_HDRPAD_STR
5441 	/*   01234567[89ABCDEF] */
5442 	    "upcnt mxfrg err name");
5443 	/*   12345 12345 123 xxxxxxxx  */
5444 
5445 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5446 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5447 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5448 		(void) mi_mpprintf(mp,
5449 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5450 		    "%05u %05u %03d %s",
5451 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5452 		    ill->ill_ipif_up_count,
5453 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5454 	}
5455 	rw_exit(&ipst->ips_ill_g_lock);
5456 
5457 	return (0);
5458 }
5459 
5460 /*
5461  * Named Dispatch routine to produce a formatted report on all IPIFs.
5462  * This report is accessed by using the ndd utility to "get" ND variable
5463  * "ip_ipif_status".
5464  */
5465 /* ARGSUSED */
5466 int
5467 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5468 {
5469 	char	buf1[INET6_ADDRSTRLEN];
5470 	char	buf2[INET6_ADDRSTRLEN];
5471 	char	buf3[INET6_ADDRSTRLEN];
5472 	char	buf4[INET6_ADDRSTRLEN];
5473 	char	buf5[INET6_ADDRSTRLEN];
5474 	char	buf6[INET6_ADDRSTRLEN];
5475 	char	buf[LIFNAMSIZ];
5476 	ill_t	*ill;
5477 	ipif_t	*ipif;
5478 	nv_t	*nvp;
5479 	uint64_t flags;
5480 	zoneid_t zoneid;
5481 	ill_walk_context_t ctx;
5482 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5483 
5484 	(void) mi_mpprintf(mp,
5485 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5486 	    "\tlocal address\n"
5487 	    "\tsrc address\n"
5488 	    "\tsubnet\n"
5489 	    "\tmask\n"
5490 	    "\tbroadcast\n"
5491 	    "\tp-p-dst");
5492 
5493 	ASSERT(q->q_next == NULL);
5494 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5495 
5496 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5497 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5498 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5499 		for (ipif = ill->ill_ipif; ipif != NULL;
5500 		    ipif = ipif->ipif_next) {
5501 			if (zoneid != GLOBAL_ZONEID &&
5502 			    zoneid != ipif->ipif_zoneid &&
5503 			    ipif->ipif_zoneid != ALL_ZONES)
5504 				continue;
5505 			(void) mi_mpprintf(mp,
5506 			    MI_COL_PTRFMT_STR
5507 			    "%04u %05u %u/%u/%u %s %d",
5508 			    (void *)ipif,
5509 			    ipif->ipif_metric, ipif->ipif_mtu,
5510 			    ipif->ipif_ib_pkt_count,
5511 			    ipif->ipif_ob_pkt_count,
5512 			    ipif->ipif_fo_pkt_count,
5513 			    ipif_get_name(ipif, buf, sizeof (buf)),
5514 			    ipif->ipif_zoneid);
5515 
5516 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5517 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5518 
5519 		/* Tack on text strings for any flags. */
5520 		nvp = ipif_nv_tbl;
5521 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5522 			if (nvp->nv_value & flags)
5523 				(void) mi_mpprintf_nr(mp, " %s",
5524 				    nvp->nv_name);
5525 		}
5526 		(void) mi_mpprintf(mp,
5527 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5528 		    inet_ntop(AF_INET6,
5529 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5530 		    inet_ntop(AF_INET6,
5531 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5532 		    inet_ntop(AF_INET6,
5533 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5534 		    inet_ntop(AF_INET6,
5535 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5536 		    inet_ntop(AF_INET6,
5537 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5538 		    inet_ntop(AF_INET6,
5539 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5540 		}
5541 	}
5542 	rw_exit(&ipst->ips_ill_g_lock);
5543 	return (0);
5544 }
5545 
5546 /*
5547  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5548  * driver.  We construct best guess defaults for lower level information that
5549  * we need.  If an interface is brought up without injection of any overriding
5550  * information from outside, we have to be ready to go with these defaults.
5551  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5552  * we primarely want the dl_provider_style.
5553  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5554  * at which point we assume the other part of the information is valid.
5555  */
5556 void
5557 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5558 {
5559 	uchar_t		*brdcst_addr;
5560 	uint_t		brdcst_addr_length, phys_addr_length;
5561 	t_scalar_t	sap_length;
5562 	dl_info_ack_t	*dlia;
5563 	ip_m_t		*ipm;
5564 	dl_qos_cl_sel1_t *sel1;
5565 
5566 	ASSERT(IAM_WRITER_ILL(ill));
5567 
5568 	/*
5569 	 * Till the ill is fully up ILL_CHANGING will be set and
5570 	 * the ill is not globally visible. So no need for a lock.
5571 	 */
5572 	dlia = (dl_info_ack_t *)mp->b_rptr;
5573 	ill->ill_mactype = dlia->dl_mac_type;
5574 
5575 	ipm = ip_m_lookup(dlia->dl_mac_type);
5576 	if (ipm == NULL) {
5577 		ipm = ip_m_lookup(DL_OTHER);
5578 		ASSERT(ipm != NULL);
5579 	}
5580 	ill->ill_media = ipm;
5581 
5582 	/*
5583 	 * When the new DLPI stuff is ready we'll pull lengths
5584 	 * from dlia.
5585 	 */
5586 	if (dlia->dl_version == DL_VERSION_2) {
5587 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5588 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5589 		    brdcst_addr_length);
5590 		if (brdcst_addr == NULL) {
5591 			brdcst_addr_length = 0;
5592 		}
5593 		sap_length = dlia->dl_sap_length;
5594 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5595 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5596 		    brdcst_addr_length, sap_length, phys_addr_length));
5597 	} else {
5598 		brdcst_addr_length = 6;
5599 		brdcst_addr = ip_six_byte_all_ones;
5600 		sap_length = -2;
5601 		phys_addr_length = brdcst_addr_length;
5602 	}
5603 
5604 	ill->ill_bcast_addr_length = brdcst_addr_length;
5605 	ill->ill_phys_addr_length = phys_addr_length;
5606 	ill->ill_sap_length = sap_length;
5607 	ill->ill_max_frag = dlia->dl_max_sdu;
5608 	ill->ill_max_mtu = ill->ill_max_frag;
5609 
5610 	ill->ill_type = ipm->ip_m_type;
5611 
5612 	if (!ill->ill_dlpi_style_set) {
5613 		if (dlia->dl_provider_style == DL_STYLE2)
5614 			ill->ill_needs_attach = 1;
5615 
5616 		/*
5617 		 * Allocate the first ipif on this ill. We don't delay it
5618 		 * further as ioctl handling assumes atleast one ipif to
5619 		 * be present.
5620 		 *
5621 		 * At this point we don't know whether the ill is v4 or v6.
5622 		 * We will know this whan the SIOCSLIFNAME happens and
5623 		 * the correct value for ill_isv6 will be assigned in
5624 		 * ipif_set_values(). We need to hold the ill lock and
5625 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5626 		 * the wakeup.
5627 		 */
5628 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5629 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5630 		mutex_enter(&ill->ill_lock);
5631 		ASSERT(ill->ill_dlpi_style_set == 0);
5632 		ill->ill_dlpi_style_set = 1;
5633 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5634 		cv_broadcast(&ill->ill_cv);
5635 		mutex_exit(&ill->ill_lock);
5636 		freemsg(mp);
5637 		return;
5638 	}
5639 	ASSERT(ill->ill_ipif != NULL);
5640 	/*
5641 	 * We know whether it is IPv4 or IPv6 now, as this is the
5642 	 * second DL_INFO_ACK we are recieving in response to the
5643 	 * DL_INFO_REQ sent in ipif_set_values.
5644 	 */
5645 	if (ill->ill_isv6)
5646 		ill->ill_sap = IP6_DL_SAP;
5647 	else
5648 		ill->ill_sap = IP_DL_SAP;
5649 	/*
5650 	 * Set ipif_mtu which is used to set the IRE's
5651 	 * ire_max_frag value. The driver could have sent
5652 	 * a different mtu from what it sent last time. No
5653 	 * need to call ipif_mtu_change because IREs have
5654 	 * not yet been created.
5655 	 */
5656 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5657 	/*
5658 	 * Clear all the flags that were set based on ill_bcast_addr_length
5659 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5660 	 * changed now and we need to re-evaluate.
5661 	 */
5662 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5663 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5664 
5665 	/*
5666 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5667 	 * changed now.
5668 	 */
5669 	if (ill->ill_bcast_addr_length == 0) {
5670 		if (ill->ill_resolver_mp != NULL)
5671 			freemsg(ill->ill_resolver_mp);
5672 		if (ill->ill_bcast_mp != NULL)
5673 			freemsg(ill->ill_bcast_mp);
5674 		if (ill->ill_flags & ILLF_XRESOLV)
5675 			ill->ill_net_type = IRE_IF_RESOLVER;
5676 		else
5677 			ill->ill_net_type = IRE_IF_NORESOLVER;
5678 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5679 		    ill->ill_phys_addr_length,
5680 		    ill->ill_sap,
5681 		    ill->ill_sap_length);
5682 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5683 
5684 		if (ill->ill_isv6)
5685 			/*
5686 			 * Note: xresolv interfaces will eventually need NOARP
5687 			 * set here as well, but that will require those
5688 			 * external resolvers to have some knowledge of
5689 			 * that flag and act appropriately. Not to be changed
5690 			 * at present.
5691 			 */
5692 			ill->ill_flags |= ILLF_NONUD;
5693 		else
5694 			ill->ill_flags |= ILLF_NOARP;
5695 
5696 		if (ill->ill_phys_addr_length == 0) {
5697 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5698 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5699 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5700 			} else {
5701 				/* pt-pt supports multicast. */
5702 				ill->ill_flags |= ILLF_MULTICAST;
5703 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5704 			}
5705 		}
5706 	} else {
5707 		ill->ill_net_type = IRE_IF_RESOLVER;
5708 		if (ill->ill_bcast_mp != NULL)
5709 			freemsg(ill->ill_bcast_mp);
5710 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5711 		    ill->ill_bcast_addr_length, ill->ill_sap,
5712 		    ill->ill_sap_length);
5713 		/*
5714 		 * Later detect lack of DLPI driver multicast
5715 		 * capability by catching DL_ENABMULTI errors in
5716 		 * ip_rput_dlpi.
5717 		 */
5718 		ill->ill_flags |= ILLF_MULTICAST;
5719 		if (!ill->ill_isv6)
5720 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5721 	}
5722 	/* By default an interface does not support any CoS marking */
5723 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5724 
5725 	/*
5726 	 * If we get QoS information in DL_INFO_ACK, the device supports
5727 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5728 	 */
5729 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5730 	    dlia->dl_qos_length);
5731 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5732 		ill->ill_flags |= ILLF_COS_ENABLED;
5733 	}
5734 
5735 	/* Clear any previous error indication. */
5736 	ill->ill_error = 0;
5737 	freemsg(mp);
5738 }
5739 
5740 /*
5741  * Perform various checks to verify that an address would make sense as a
5742  * local, remote, or subnet interface address.
5743  */
5744 static boolean_t
5745 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5746 {
5747 	ipaddr_t	net_mask;
5748 
5749 	/*
5750 	 * Don't allow all zeroes, all ones or experimental address, but allow
5751 	 * all ones netmask.
5752 	 */
5753 	if ((net_mask = ip_net_mask(addr)) == 0)
5754 		return (B_FALSE);
5755 	/* A given netmask overrides the "guess" netmask */
5756 	if (subnet_mask != 0)
5757 		net_mask = subnet_mask;
5758 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5759 	    (addr == (addr | ~net_mask)))) {
5760 		return (B_FALSE);
5761 	}
5762 	if (CLASSD(addr))
5763 		return (B_FALSE);
5764 
5765 	return (B_TRUE);
5766 }
5767 
5768 #define	V6_IPIF_LINKLOCAL(p)	\
5769 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5770 
5771 /*
5772  * Compare two given ipifs and check if the second one is better than
5773  * the first one using the order of preference (not taking deprecated
5774  * into acount) specified in ipif_lookup_multicast().
5775  */
5776 static boolean_t
5777 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5778 {
5779 	/* Check the least preferred first. */
5780 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5781 		/* If both ipifs are the same, use the first one. */
5782 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5783 			return (B_FALSE);
5784 		else
5785 			return (B_TRUE);
5786 	}
5787 
5788 	/* For IPv6, check for link local address. */
5789 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5790 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5791 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5792 			/* The second one is equal or less preferred. */
5793 			return (B_FALSE);
5794 		} else {
5795 			return (B_TRUE);
5796 		}
5797 	}
5798 
5799 	/* Then check for point to point interface. */
5800 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5801 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5802 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5803 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5804 			return (B_FALSE);
5805 		} else {
5806 			return (B_TRUE);
5807 		}
5808 	}
5809 
5810 	/* old_ipif is a normal interface, so no need to use the new one. */
5811 	return (B_FALSE);
5812 }
5813 
5814 /*
5815  * Find any non-virtual, not condemned, and up multicast capable interface
5816  * given an IP instance and zoneid.  Order of preference is:
5817  *
5818  * 1. normal
5819  * 1.1 normal, but deprecated
5820  * 2. point to point
5821  * 2.1 point to point, but deprecated
5822  * 3. link local
5823  * 3.1 link local, but deprecated
5824  * 4. loopback.
5825  */
5826 ipif_t *
5827 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5828 {
5829 	ill_t			*ill;
5830 	ill_walk_context_t	ctx;
5831 	ipif_t			*ipif;
5832 	ipif_t			*saved_ipif = NULL;
5833 	ipif_t			*dep_ipif = NULL;
5834 
5835 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5836 	if (isv6)
5837 		ill = ILL_START_WALK_V6(&ctx, ipst);
5838 	else
5839 		ill = ILL_START_WALK_V4(&ctx, ipst);
5840 
5841 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5842 		mutex_enter(&ill->ill_lock);
5843 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5844 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5845 			mutex_exit(&ill->ill_lock);
5846 			continue;
5847 		}
5848 		for (ipif = ill->ill_ipif; ipif != NULL;
5849 		    ipif = ipif->ipif_next) {
5850 			if (zoneid != ipif->ipif_zoneid &&
5851 			    zoneid != ALL_ZONES &&
5852 			    ipif->ipif_zoneid != ALL_ZONES) {
5853 				continue;
5854 			}
5855 			if (!(ipif->ipif_flags & IPIF_UP) ||
5856 			    !IPIF_CAN_LOOKUP(ipif)) {
5857 				continue;
5858 			}
5859 
5860 			/*
5861 			 * Found one candidate.  If it is deprecated,
5862 			 * remember it in dep_ipif.  If it is not deprecated,
5863 			 * remember it in saved_ipif.
5864 			 */
5865 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5866 				if (dep_ipif == NULL) {
5867 					dep_ipif = ipif;
5868 				} else if (ipif_comp_multi(dep_ipif, ipif,
5869 				    isv6)) {
5870 					/*
5871 					 * If the previous dep_ipif does not
5872 					 * belong to the same ill, we've done
5873 					 * a ipif_refhold() on it.  So we need
5874 					 * to release it.
5875 					 */
5876 					if (dep_ipif->ipif_ill != ill)
5877 						ipif_refrele(dep_ipif);
5878 					dep_ipif = ipif;
5879 				}
5880 				continue;
5881 			}
5882 			if (saved_ipif == NULL) {
5883 				saved_ipif = ipif;
5884 			} else {
5885 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5886 					if (saved_ipif->ipif_ill != ill)
5887 						ipif_refrele(saved_ipif);
5888 					saved_ipif = ipif;
5889 				}
5890 			}
5891 		}
5892 		/*
5893 		 * Before going to the next ill, do a ipif_refhold() on the
5894 		 * saved ones.
5895 		 */
5896 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5897 			ipif_refhold_locked(saved_ipif);
5898 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5899 			ipif_refhold_locked(dep_ipif);
5900 		mutex_exit(&ill->ill_lock);
5901 	}
5902 	rw_exit(&ipst->ips_ill_g_lock);
5903 
5904 	/*
5905 	 * If we have only the saved_ipif, return it.  But if we have both
5906 	 * saved_ipif and dep_ipif, check to see which one is better.
5907 	 */
5908 	if (saved_ipif != NULL) {
5909 		if (dep_ipif != NULL) {
5910 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5911 				ipif_refrele(saved_ipif);
5912 				return (dep_ipif);
5913 			} else {
5914 				ipif_refrele(dep_ipif);
5915 				return (saved_ipif);
5916 			}
5917 		}
5918 		return (saved_ipif);
5919 	} else {
5920 		return (dep_ipif);
5921 	}
5922 }
5923 
5924 /*
5925  * This function is called when an application does not specify an interface
5926  * to be used for multicast traffic (joining a group/sending data).  It
5927  * calls ire_lookup_multi() to look for an interface route for the
5928  * specified multicast group.  Doing this allows the administrator to add
5929  * prefix routes for multicast to indicate which interface to be used for
5930  * multicast traffic in the above scenario.  The route could be for all
5931  * multicast (224.0/4), for a single multicast group (a /32 route) or
5932  * anything in between.  If there is no such multicast route, we just find
5933  * any multicast capable interface and return it.  The returned ipif
5934  * is refhold'ed.
5935  */
5936 ipif_t *
5937 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5938 {
5939 	ire_t			*ire;
5940 	ipif_t			*ipif;
5941 
5942 	ire = ire_lookup_multi(group, zoneid, ipst);
5943 	if (ire != NULL) {
5944 		ipif = ire->ire_ipif;
5945 		ipif_refhold(ipif);
5946 		ire_refrele(ire);
5947 		return (ipif);
5948 	}
5949 
5950 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5951 }
5952 
5953 /*
5954  * Look for an ipif with the specified interface address and destination.
5955  * The destination address is used only for matching point-to-point interfaces.
5956  */
5957 ipif_t *
5958 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5959     ipsq_func_t func, int *error, ip_stack_t *ipst)
5960 {
5961 	ipif_t	*ipif;
5962 	ill_t	*ill;
5963 	ill_walk_context_t ctx;
5964 	ipsq_t	*ipsq;
5965 
5966 	if (error != NULL)
5967 		*error = 0;
5968 
5969 	/*
5970 	 * First match all the point-to-point interfaces
5971 	 * before looking at non-point-to-point interfaces.
5972 	 * This is done to avoid returning non-point-to-point
5973 	 * ipif instead of unnumbered point-to-point ipif.
5974 	 */
5975 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5976 	ill = ILL_START_WALK_V4(&ctx, ipst);
5977 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5978 		GRAB_CONN_LOCK(q);
5979 		mutex_enter(&ill->ill_lock);
5980 		for (ipif = ill->ill_ipif; ipif != NULL;
5981 		    ipif = ipif->ipif_next) {
5982 			/* Allow the ipif to be down */
5983 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5984 			    (ipif->ipif_lcl_addr == if_addr) &&
5985 			    (ipif->ipif_pp_dst_addr == dst)) {
5986 				/*
5987 				 * The block comment at the start of ipif_down
5988 				 * explains the use of the macros used below
5989 				 */
5990 				if (IPIF_CAN_LOOKUP(ipif)) {
5991 					ipif_refhold_locked(ipif);
5992 					mutex_exit(&ill->ill_lock);
5993 					RELEASE_CONN_LOCK(q);
5994 					rw_exit(&ipst->ips_ill_g_lock);
5995 					return (ipif);
5996 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5997 					ipsq = ill->ill_phyint->phyint_ipsq;
5998 					mutex_enter(&ipsq->ipsq_lock);
5999 					mutex_exit(&ill->ill_lock);
6000 					rw_exit(&ipst->ips_ill_g_lock);
6001 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6002 					    ill);
6003 					mutex_exit(&ipsq->ipsq_lock);
6004 					RELEASE_CONN_LOCK(q);
6005 					*error = EINPROGRESS;
6006 					return (NULL);
6007 				}
6008 			}
6009 		}
6010 		mutex_exit(&ill->ill_lock);
6011 		RELEASE_CONN_LOCK(q);
6012 	}
6013 	rw_exit(&ipst->ips_ill_g_lock);
6014 
6015 	/* lookup the ipif based on interface address */
6016 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
6017 	    ipst);
6018 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
6019 	return (ipif);
6020 }
6021 
6022 /*
6023  * Look for an ipif with the specified address. For point-point links
6024  * we look for matches on either the destination address and the local
6025  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6026  * is set.
6027  * Matches on a specific ill if match_ill is set.
6028  */
6029 ipif_t *
6030 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
6031     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
6032 {
6033 	ipif_t  *ipif;
6034 	ill_t   *ill;
6035 	boolean_t ptp = B_FALSE;
6036 	ipsq_t	*ipsq;
6037 	ill_walk_context_t	ctx;
6038 
6039 	if (error != NULL)
6040 		*error = 0;
6041 
6042 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6043 	/*
6044 	 * Repeat twice, first based on local addresses and
6045 	 * next time for pointopoint.
6046 	 */
6047 repeat:
6048 	ill = ILL_START_WALK_V4(&ctx, ipst);
6049 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6050 		if (match_ill != NULL && ill != match_ill) {
6051 			continue;
6052 		}
6053 		GRAB_CONN_LOCK(q);
6054 		mutex_enter(&ill->ill_lock);
6055 		for (ipif = ill->ill_ipif; ipif != NULL;
6056 		    ipif = ipif->ipif_next) {
6057 			if (zoneid != ALL_ZONES &&
6058 			    zoneid != ipif->ipif_zoneid &&
6059 			    ipif->ipif_zoneid != ALL_ZONES)
6060 				continue;
6061 			/* Allow the ipif to be down */
6062 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6063 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6064 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6065 			    (ipif->ipif_pp_dst_addr == addr))) {
6066 				/*
6067 				 * The block comment at the start of ipif_down
6068 				 * explains the use of the macros used below
6069 				 */
6070 				if (IPIF_CAN_LOOKUP(ipif)) {
6071 					ipif_refhold_locked(ipif);
6072 					mutex_exit(&ill->ill_lock);
6073 					RELEASE_CONN_LOCK(q);
6074 					rw_exit(&ipst->ips_ill_g_lock);
6075 					return (ipif);
6076 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6077 					ipsq = ill->ill_phyint->phyint_ipsq;
6078 					mutex_enter(&ipsq->ipsq_lock);
6079 					mutex_exit(&ill->ill_lock);
6080 					rw_exit(&ipst->ips_ill_g_lock);
6081 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6082 					    ill);
6083 					mutex_exit(&ipsq->ipsq_lock);
6084 					RELEASE_CONN_LOCK(q);
6085 					*error = EINPROGRESS;
6086 					return (NULL);
6087 				}
6088 			}
6089 		}
6090 		mutex_exit(&ill->ill_lock);
6091 		RELEASE_CONN_LOCK(q);
6092 	}
6093 
6094 	/* If we already did the ptp case, then we are done */
6095 	if (ptp) {
6096 		rw_exit(&ipst->ips_ill_g_lock);
6097 		if (error != NULL)
6098 			*error = ENXIO;
6099 		return (NULL);
6100 	}
6101 	ptp = B_TRUE;
6102 	goto repeat;
6103 }
6104 
6105 /*
6106  * Look for an ipif with the specified address. For point-point links
6107  * we look for matches on either the destination address and the local
6108  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6109  * is set.
6110  * Matches on a specific ill if match_ill is set.
6111  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6112  */
6113 zoneid_t
6114 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6115 {
6116 	zoneid_t zoneid;
6117 	ipif_t  *ipif;
6118 	ill_t   *ill;
6119 	boolean_t ptp = B_FALSE;
6120 	ill_walk_context_t	ctx;
6121 
6122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6123 	/*
6124 	 * Repeat twice, first based on local addresses and
6125 	 * next time for pointopoint.
6126 	 */
6127 repeat:
6128 	ill = ILL_START_WALK_V4(&ctx, ipst);
6129 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6130 		if (match_ill != NULL && ill != match_ill) {
6131 			continue;
6132 		}
6133 		mutex_enter(&ill->ill_lock);
6134 		for (ipif = ill->ill_ipif; ipif != NULL;
6135 		    ipif = ipif->ipif_next) {
6136 			/* Allow the ipif to be down */
6137 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6138 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6139 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6140 			    (ipif->ipif_pp_dst_addr == addr)) &&
6141 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6142 				zoneid = ipif->ipif_zoneid;
6143 				mutex_exit(&ill->ill_lock);
6144 				rw_exit(&ipst->ips_ill_g_lock);
6145 				/*
6146 				 * If ipif_zoneid was ALL_ZONES then we have
6147 				 * a trusted extensions shared IP address.
6148 				 * In that case GLOBAL_ZONEID works to send.
6149 				 */
6150 				if (zoneid == ALL_ZONES)
6151 					zoneid = GLOBAL_ZONEID;
6152 				return (zoneid);
6153 			}
6154 		}
6155 		mutex_exit(&ill->ill_lock);
6156 	}
6157 
6158 	/* If we already did the ptp case, then we are done */
6159 	if (ptp) {
6160 		rw_exit(&ipst->ips_ill_g_lock);
6161 		return (ALL_ZONES);
6162 	}
6163 	ptp = B_TRUE;
6164 	goto repeat;
6165 }
6166 
6167 /*
6168  * Look for an ipif that matches the specified remote address i.e. the
6169  * ipif that would receive the specified packet.
6170  * First look for directly connected interfaces and then do a recursive
6171  * IRE lookup and pick the first ipif corresponding to the source address in the
6172  * ire.
6173  * Returns: held ipif
6174  */
6175 ipif_t *
6176 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6177 {
6178 	ipif_t	*ipif;
6179 	ire_t	*ire;
6180 	ip_stack_t	*ipst = ill->ill_ipst;
6181 
6182 	ASSERT(!ill->ill_isv6);
6183 
6184 	/*
6185 	 * Someone could be changing this ipif currently or change it
6186 	 * after we return this. Thus  a few packets could use the old
6187 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6188 	 * will atomically be updated or cleaned up with the new value
6189 	 * Thus we don't need a lock to check the flags or other attrs below.
6190 	 */
6191 	mutex_enter(&ill->ill_lock);
6192 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6193 		if (!IPIF_CAN_LOOKUP(ipif))
6194 			continue;
6195 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6196 		    ipif->ipif_zoneid != ALL_ZONES)
6197 			continue;
6198 		/* Allow the ipif to be down */
6199 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6200 			if ((ipif->ipif_pp_dst_addr == addr) ||
6201 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6202 			    ipif->ipif_lcl_addr == addr)) {
6203 				ipif_refhold_locked(ipif);
6204 				mutex_exit(&ill->ill_lock);
6205 				return (ipif);
6206 			}
6207 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6208 			ipif_refhold_locked(ipif);
6209 			mutex_exit(&ill->ill_lock);
6210 			return (ipif);
6211 		}
6212 	}
6213 	mutex_exit(&ill->ill_lock);
6214 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6215 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6216 	if (ire != NULL) {
6217 		/*
6218 		 * The callers of this function wants to know the
6219 		 * interface on which they have to send the replies
6220 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6221 		 * derived from different ills, we really don't care
6222 		 * what we return here.
6223 		 */
6224 		ipif = ire->ire_ipif;
6225 		if (ipif != NULL) {
6226 			ipif_refhold(ipif);
6227 			ire_refrele(ire);
6228 			return (ipif);
6229 		}
6230 		ire_refrele(ire);
6231 	}
6232 	/* Pick the first interface */
6233 	ipif = ipif_get_next_ipif(NULL, ill);
6234 	return (ipif);
6235 }
6236 
6237 /*
6238  * This func does not prevent refcnt from increasing. But if
6239  * the caller has taken steps to that effect, then this func
6240  * can be used to determine whether the ill has become quiescent
6241  */
6242 boolean_t
6243 ill_is_quiescent(ill_t *ill)
6244 {
6245 	ipif_t	*ipif;
6246 
6247 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6248 
6249 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6250 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6251 			return (B_FALSE);
6252 		}
6253 	}
6254 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6255 	    ill->ill_nce_cnt != 0) {
6256 		return (B_FALSE);
6257 	}
6258 	return (B_TRUE);
6259 }
6260 
6261 /*
6262  * This func does not prevent refcnt from increasing. But if
6263  * the caller has taken steps to that effect, then this func
6264  * can be used to determine whether the ipif has become quiescent
6265  */
6266 static boolean_t
6267 ipif_is_quiescent(ipif_t *ipif)
6268 {
6269 	ill_t *ill;
6270 
6271 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6272 
6273 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6274 		return (B_FALSE);
6275 	}
6276 
6277 	ill = ipif->ipif_ill;
6278 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6279 	    ill->ill_logical_down) {
6280 		return (B_TRUE);
6281 	}
6282 
6283 	/* This is the last ipif going down or being deleted on this ill */
6284 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6285 		return (B_FALSE);
6286 	}
6287 
6288 	return (B_TRUE);
6289 }
6290 
6291 /*
6292  * This func does not prevent refcnt from increasing. But if
6293  * the caller has taken steps to that effect, then this func
6294  * can be used to determine whether the ipifs marked with IPIF_MOVING
6295  * have become quiescent and can be moved in a failover/failback.
6296  */
6297 static ipif_t *
6298 ill_quiescent_to_move(ill_t *ill)
6299 {
6300 	ipif_t  *ipif;
6301 
6302 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6303 
6304 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6305 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6306 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6307 				return (ipif);
6308 			}
6309 		}
6310 	}
6311 	return (NULL);
6312 }
6313 
6314 /*
6315  * The ipif/ill/ire has been refreled. Do the tail processing.
6316  * Determine if the ipif or ill in question has become quiescent and if so
6317  * wakeup close and/or restart any queued pending ioctl that is waiting
6318  * for the ipif_down (or ill_down)
6319  */
6320 void
6321 ipif_ill_refrele_tail(ill_t *ill)
6322 {
6323 	mblk_t	*mp;
6324 	conn_t	*connp;
6325 	ipsq_t	*ipsq;
6326 	ipif_t	*ipif;
6327 	dl_notify_ind_t *dlindp;
6328 
6329 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6330 
6331 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6332 	    ill_is_quiescent(ill)) {
6333 		/* ill_close may be waiting */
6334 		cv_broadcast(&ill->ill_cv);
6335 	}
6336 
6337 	/* ipsq can't change because ill_lock  is held */
6338 	ipsq = ill->ill_phyint->phyint_ipsq;
6339 	if (ipsq->ipsq_waitfor == 0) {
6340 		/* Not waiting for anything, just return. */
6341 		mutex_exit(&ill->ill_lock);
6342 		return;
6343 	}
6344 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6345 	    ipsq->ipsq_pending_ipif != NULL);
6346 	/*
6347 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6348 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6349 	 * be zero for restarting an ioctl that ends up downing the ill.
6350 	 */
6351 	ipif = ipsq->ipsq_pending_ipif;
6352 	if (ipif->ipif_ill != ill) {
6353 		/* The ioctl is pending on some other ill. */
6354 		mutex_exit(&ill->ill_lock);
6355 		return;
6356 	}
6357 
6358 	switch (ipsq->ipsq_waitfor) {
6359 	case IPIF_DOWN:
6360 	case IPIF_FREE:
6361 		if (!ipif_is_quiescent(ipif)) {
6362 			mutex_exit(&ill->ill_lock);
6363 			return;
6364 		}
6365 		break;
6366 
6367 	case ILL_DOWN:
6368 	case ILL_FREE:
6369 		/*
6370 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6371 		 * waits synchronously in ip_close, and no message is queued in
6372 		 * ipsq_pending_mp at all in this case
6373 		 */
6374 		if (!ill_is_quiescent(ill)) {
6375 			mutex_exit(&ill->ill_lock);
6376 			return;
6377 		}
6378 
6379 		break;
6380 
6381 	case ILL_MOVE_OK:
6382 		if (ill_quiescent_to_move(ill) != NULL) {
6383 			mutex_exit(&ill->ill_lock);
6384 			return;
6385 		}
6386 
6387 		break;
6388 	default:
6389 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6390 		    (void *)ipsq, ipsq->ipsq_waitfor);
6391 	}
6392 
6393 	/*
6394 	 * Incr refcnt for the qwriter_ip call below which
6395 	 * does a refrele
6396 	 */
6397 	ill_refhold_locked(ill);
6398 	mutex_exit(&ill->ill_lock);
6399 
6400 	mp = ipsq_pending_mp_get(ipsq, &connp);
6401 	ASSERT(mp != NULL);
6402 
6403 	/*
6404 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6405 	 * we can only get here when the current operation decides it
6406 	 * it needs to quiesce via ipsq_pending_mp_add().
6407 	 */
6408 	switch (mp->b_datap->db_type) {
6409 	case M_PCPROTO:
6410 	case M_PROTO:
6411 		/*
6412 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6413 		 */
6414 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6415 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6416 
6417 		switch (dlindp->dl_notification) {
6418 		case DL_NOTE_PHYS_ADDR:
6419 			qwriter_ip(ill, ill->ill_rq, mp,
6420 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6421 			return;
6422 		default:
6423 			ASSERT(0);
6424 		}
6425 		break;
6426 
6427 	case M_ERROR:
6428 	case M_HANGUP:
6429 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6430 		    B_TRUE);
6431 		return;
6432 
6433 	case M_IOCTL:
6434 	case M_IOCDATA:
6435 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6436 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6437 		return;
6438 
6439 	default:
6440 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6441 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6442 	}
6443 }
6444 
6445 #ifdef ILL_DEBUG
6446 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6447 void
6448 th_trace_rrecord(th_trace_t *th_trace)
6449 {
6450 	tr_buf_t *tr_buf;
6451 	uint_t lastref;
6452 
6453 	lastref = th_trace->th_trace_lastref;
6454 	lastref++;
6455 	if (lastref == TR_BUF_MAX)
6456 		lastref = 0;
6457 	th_trace->th_trace_lastref = lastref;
6458 	tr_buf = &th_trace->th_trbuf[lastref];
6459 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
6460 }
6461 
6462 th_trace_t *
6463 th_trace_ipif_lookup(ipif_t *ipif)
6464 {
6465 	int bucket_id;
6466 	th_trace_t *th_trace;
6467 
6468 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6469 
6470 	bucket_id = IP_TR_HASH(curthread);
6471 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6472 
6473 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
6474 	    th_trace = th_trace->th_next) {
6475 		if (th_trace->th_id == curthread)
6476 			return (th_trace);
6477 	}
6478 	return (NULL);
6479 }
6480 
6481 void
6482 ipif_trace_ref(ipif_t *ipif)
6483 {
6484 	int bucket_id;
6485 	th_trace_t *th_trace;
6486 
6487 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6488 
6489 	if (ipif->ipif_trace_disable)
6490 		return;
6491 
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 ipif, at the head
6496 	 */
6497 	th_trace = th_trace_ipif_lookup(ipif);
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 			ipif->ipif_trace_disable = B_TRUE;
6504 			ipif_trace_cleanup(ipif);
6505 			return;
6506 		}
6507 		th_trace->th_id = curthread;
6508 		th_trace->th_next = ipif->ipif_trace[bucket_id];
6509 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
6510 		if (th_trace->th_next != NULL)
6511 			th_trace->th_next->th_prev = &th_trace->th_next;
6512 		ipif->ipif_trace[bucket_id] = th_trace;
6513 	}
6514 	ASSERT(th_trace->th_refcnt >= 0 &&
6515 	    th_trace->th_refcnt < TR_BUF_MAX -1);
6516 	th_trace->th_refcnt++;
6517 	th_trace_rrecord(th_trace);
6518 }
6519 
6520 void
6521 ipif_untrace_ref(ipif_t *ipif)
6522 {
6523 	th_trace_t *th_trace;
6524 
6525 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6526 
6527 	if (ipif->ipif_trace_disable)
6528 		return;
6529 	th_trace = th_trace_ipif_lookup(ipif);
6530 	ASSERT(th_trace != NULL);
6531 	ASSERT(th_trace->th_refcnt > 0);
6532 
6533 	th_trace->th_refcnt--;
6534 	th_trace_rrecord(th_trace);
6535 }
6536 
6537 th_trace_t *
6538 th_trace_ill_lookup(ill_t *ill)
6539 {
6540 	th_trace_t *th_trace;
6541 	int bucket_id;
6542 
6543 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6544 
6545 	bucket_id = IP_TR_HASH(curthread);
6546 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6547 
6548 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
6549 	    th_trace = th_trace->th_next) {
6550 		if (th_trace->th_id == curthread)
6551 			return (th_trace);
6552 	}
6553 	return (NULL);
6554 }
6555 
6556 void
6557 ill_trace_ref(ill_t *ill)
6558 {
6559 	int bucket_id;
6560 	th_trace_t *th_trace;
6561 
6562 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6563 	if (ill->ill_trace_disable)
6564 		return;
6565 	/*
6566 	 * Attempt to locate the trace buffer for the curthread.
6567 	 * If it does not exist, then allocate a new trace buffer
6568 	 * and link it in list of trace bufs for this ill, at the head
6569 	 */
6570 	th_trace = th_trace_ill_lookup(ill);
6571 	if (th_trace == NULL) {
6572 		bucket_id = IP_TR_HASH(curthread);
6573 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6574 		    KM_NOSLEEP);
6575 		if (th_trace == NULL) {
6576 			ill->ill_trace_disable = B_TRUE;
6577 			ill_trace_cleanup(ill);
6578 			return;
6579 		}
6580 		th_trace->th_id = curthread;
6581 		th_trace->th_next = ill->ill_trace[bucket_id];
6582 		th_trace->th_prev = &ill->ill_trace[bucket_id];
6583 		if (th_trace->th_next != NULL)
6584 			th_trace->th_next->th_prev = &th_trace->th_next;
6585 		ill->ill_trace[bucket_id] = th_trace;
6586 	}
6587 	ASSERT(th_trace->th_refcnt >= 0 &&
6588 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6589 
6590 	th_trace->th_refcnt++;
6591 	th_trace_rrecord(th_trace);
6592 }
6593 
6594 void
6595 ill_untrace_ref(ill_t *ill)
6596 {
6597 	th_trace_t *th_trace;
6598 
6599 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6600 
6601 	if (ill->ill_trace_disable)
6602 		return;
6603 	th_trace = th_trace_ill_lookup(ill);
6604 	ASSERT(th_trace != NULL);
6605 	ASSERT(th_trace->th_refcnt > 0);
6606 
6607 	th_trace->th_refcnt--;
6608 	th_trace_rrecord(th_trace);
6609 }
6610 
6611 /*
6612  * Verify that this thread has no refs to the ipif and free
6613  * the trace buffers
6614  */
6615 /* ARGSUSED */
6616 void
6617 ipif_thread_exit(ipif_t *ipif, void *dummy)
6618 {
6619 	th_trace_t *th_trace;
6620 
6621 	mutex_enter(&ipif->ipif_ill->ill_lock);
6622 
6623 	th_trace = th_trace_ipif_lookup(ipif);
6624 	if (th_trace == NULL) {
6625 		mutex_exit(&ipif->ipif_ill->ill_lock);
6626 		return;
6627 	}
6628 	ASSERT(th_trace->th_refcnt == 0);
6629 	/* unlink th_trace and free it */
6630 	*th_trace->th_prev = th_trace->th_next;
6631 	if (th_trace->th_next != NULL)
6632 		th_trace->th_next->th_prev = th_trace->th_prev;
6633 	th_trace->th_next = NULL;
6634 	th_trace->th_prev = NULL;
6635 	kmem_free(th_trace, sizeof (th_trace_t));
6636 
6637 	mutex_exit(&ipif->ipif_ill->ill_lock);
6638 }
6639 
6640 /*
6641  * Verify that this thread has no refs to the ill and free
6642  * the trace buffers
6643  */
6644 /* ARGSUSED */
6645 void
6646 ill_thread_exit(ill_t *ill, void *dummy)
6647 {
6648 	th_trace_t *th_trace;
6649 
6650 	mutex_enter(&ill->ill_lock);
6651 
6652 	th_trace = th_trace_ill_lookup(ill);
6653 	if (th_trace == NULL) {
6654 		mutex_exit(&ill->ill_lock);
6655 		return;
6656 	}
6657 	ASSERT(th_trace->th_refcnt == 0);
6658 	/* unlink th_trace and free it */
6659 	*th_trace->th_prev = th_trace->th_next;
6660 	if (th_trace->th_next != NULL)
6661 		th_trace->th_next->th_prev = th_trace->th_prev;
6662 	th_trace->th_next = NULL;
6663 	th_trace->th_prev = NULL;
6664 	kmem_free(th_trace, sizeof (th_trace_t));
6665 
6666 	mutex_exit(&ill->ill_lock);
6667 }
6668 #endif
6669 
6670 #ifdef ILL_DEBUG
6671 void
6672 ip_thread_exit_stack(ip_stack_t *ipst)
6673 {
6674 	ill_t	*ill;
6675 	ipif_t	*ipif;
6676 	ill_walk_context_t	ctx;
6677 
6678 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6679 	ill = ILL_START_WALK_ALL(&ctx, ipst);
6680 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6681 		for (ipif = ill->ill_ipif; ipif != NULL;
6682 		    ipif = ipif->ipif_next) {
6683 			ipif_thread_exit(ipif, NULL);
6684 		}
6685 		ill_thread_exit(ill, NULL);
6686 	}
6687 	rw_exit(&ipst->ips_ill_g_lock);
6688 
6689 	ire_walk(ire_thread_exit, NULL, ipst);
6690 	ndp_walk_common(ipst->ips_ndp4, NULL, nce_thread_exit, NULL, B_FALSE);
6691 	ndp_walk_common(ipst->ips_ndp6, NULL, nce_thread_exit, NULL, B_FALSE);
6692 }
6693 
6694 /*
6695  * This is a function which is called from thread_exit
6696  * that can be used to debug reference count issues in IP. See comment in
6697  * <inet/ip.h> on how it is used.
6698  */
6699 void
6700 ip_thread_exit(void)
6701 {
6702 	netstack_t *ns;
6703 
6704 	ns = netstack_get_current();
6705 	if (ns != NULL) {
6706 		ip_thread_exit_stack(ns->netstack_ip);
6707 		netstack_rele(ns);
6708 	}
6709 }
6710 
6711 /*
6712  * Called when ipif is unplumbed or when memory alloc fails
6713  */
6714 void
6715 ipif_trace_cleanup(ipif_t *ipif)
6716 {
6717 	int	i;
6718 	th_trace_t	*th_trace;
6719 	th_trace_t	*th_trace_next;
6720 
6721 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6722 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
6723 		    th_trace = th_trace_next) {
6724 			th_trace_next = th_trace->th_next;
6725 			kmem_free(th_trace, sizeof (th_trace_t));
6726 		}
6727 		ipif->ipif_trace[i] = NULL;
6728 	}
6729 }
6730 
6731 /*
6732  * Called when ill is unplumbed or when memory alloc fails
6733  */
6734 void
6735 ill_trace_cleanup(ill_t *ill)
6736 {
6737 	int	i;
6738 	th_trace_t	*th_trace;
6739 	th_trace_t	*th_trace_next;
6740 
6741 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6742 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6743 		    th_trace = th_trace_next) {
6744 			th_trace_next = th_trace->th_next;
6745 			kmem_free(th_trace, sizeof (th_trace_t));
6746 		}
6747 		ill->ill_trace[i] = NULL;
6748 	}
6749 }
6750 
6751 #else
6752 void ip_thread_exit(void) {}
6753 #endif
6754 
6755 void
6756 ipif_refhold_locked(ipif_t *ipif)
6757 {
6758 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6759 	ipif->ipif_refcnt++;
6760 	IPIF_TRACE_REF(ipif);
6761 }
6762 
6763 void
6764 ipif_refhold(ipif_t *ipif)
6765 {
6766 	ill_t	*ill;
6767 
6768 	ill = ipif->ipif_ill;
6769 	mutex_enter(&ill->ill_lock);
6770 	ipif->ipif_refcnt++;
6771 	IPIF_TRACE_REF(ipif);
6772 	mutex_exit(&ill->ill_lock);
6773 }
6774 
6775 /*
6776  * Must not be called while holding any locks. Otherwise if this is
6777  * the last reference to be released there is a chance of recursive mutex
6778  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6779  * to restart an ioctl.
6780  */
6781 void
6782 ipif_refrele(ipif_t *ipif)
6783 {
6784 	ill_t	*ill;
6785 
6786 	ill = ipif->ipif_ill;
6787 
6788 	mutex_enter(&ill->ill_lock);
6789 	ASSERT(ipif->ipif_refcnt != 0);
6790 	ipif->ipif_refcnt--;
6791 	IPIF_UNTRACE_REF(ipif);
6792 	if (ipif->ipif_refcnt != 0) {
6793 		mutex_exit(&ill->ill_lock);
6794 		return;
6795 	}
6796 
6797 	/* Drops the ill_lock */
6798 	ipif_ill_refrele_tail(ill);
6799 }
6800 
6801 ipif_t *
6802 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6803 {
6804 	ipif_t	*ipif;
6805 
6806 	mutex_enter(&ill->ill_lock);
6807 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6808 	    ipif != NULL; ipif = ipif->ipif_next) {
6809 		if (!IPIF_CAN_LOOKUP(ipif))
6810 			continue;
6811 		ipif_refhold_locked(ipif);
6812 		mutex_exit(&ill->ill_lock);
6813 		return (ipif);
6814 	}
6815 	mutex_exit(&ill->ill_lock);
6816 	return (NULL);
6817 }
6818 
6819 /*
6820  * TODO: make this table extendible at run time
6821  * Return a pointer to the mac type info for 'mac_type'
6822  */
6823 static ip_m_t *
6824 ip_m_lookup(t_uscalar_t mac_type)
6825 {
6826 	ip_m_t	*ipm;
6827 
6828 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6829 		if (ipm->ip_m_mac_type == mac_type)
6830 			return (ipm);
6831 	return (NULL);
6832 }
6833 
6834 /*
6835  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6836  * ipif_arg is passed in to associate it with the correct interface.
6837  * We may need to restart this operation if the ipif cannot be looked up
6838  * due to an exclusive operation that is currently in progress. The restart
6839  * entry point is specified by 'func'
6840  */
6841 int
6842 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6843     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6844     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6845     struct rtsa_s *sp, ip_stack_t *ipst)
6846 {
6847 	ire_t	*ire;
6848 	ire_t	*gw_ire = NULL;
6849 	ipif_t	*ipif = NULL;
6850 	boolean_t ipif_refheld = B_FALSE;
6851 	uint_t	type;
6852 	int	match_flags = MATCH_IRE_TYPE;
6853 	int	error;
6854 	tsol_gc_t *gc = NULL;
6855 	tsol_gcgrp_t *gcgrp = NULL;
6856 	boolean_t gcgrp_xtraref = B_FALSE;
6857 
6858 	ip1dbg(("ip_rt_add:"));
6859 
6860 	if (ire_arg != NULL)
6861 		*ire_arg = NULL;
6862 
6863 	/*
6864 	 * If this is the case of RTF_HOST being set, then we set the netmask
6865 	 * to all ones (regardless if one was supplied).
6866 	 */
6867 	if (flags & RTF_HOST)
6868 		mask = IP_HOST_MASK;
6869 
6870 	/*
6871 	 * Prevent routes with a zero gateway from being created (since
6872 	 * interfaces can currently be plumbed and brought up no assigned
6873 	 * address).
6874 	 */
6875 	if (gw_addr == 0)
6876 		return (ENETUNREACH);
6877 	/*
6878 	 * Get the ipif, if any, corresponding to the gw_addr
6879 	 */
6880 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6881 	    ipst);
6882 	if (ipif != NULL) {
6883 		if (IS_VNI(ipif->ipif_ill)) {
6884 			ipif_refrele(ipif);
6885 			return (EINVAL);
6886 		}
6887 		ipif_refheld = B_TRUE;
6888 	} else if (error == EINPROGRESS) {
6889 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6890 		return (EINPROGRESS);
6891 	} else {
6892 		error = 0;
6893 	}
6894 
6895 	if (ipif != NULL) {
6896 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6897 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6898 	} else {
6899 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6900 	}
6901 
6902 	/*
6903 	 * GateD will attempt to create routes with a loopback interface
6904 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6905 	 * these routes to be added, but create them as interface routes
6906 	 * since the gateway is an interface address.
6907 	 */
6908 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6909 		flags &= ~RTF_GATEWAY;
6910 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6911 		    mask == IP_HOST_MASK) {
6912 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6913 			    ALL_ZONES, NULL, match_flags, ipst);
6914 			if (ire != NULL) {
6915 				ire_refrele(ire);
6916 				if (ipif_refheld)
6917 					ipif_refrele(ipif);
6918 				return (EEXIST);
6919 			}
6920 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6921 			    "for 0x%x\n", (void *)ipif,
6922 			    ipif->ipif_ire_type,
6923 			    ntohl(ipif->ipif_lcl_addr)));
6924 			ire = ire_create(
6925 			    (uchar_t *)&dst_addr,	/* dest address */
6926 			    (uchar_t *)&mask,		/* mask */
6927 			    (uchar_t *)&ipif->ipif_src_addr,
6928 			    NULL,			/* no gateway */
6929 			    &ipif->ipif_mtu,
6930 			    NULL,
6931 			    ipif->ipif_rq,		/* recv-from queue */
6932 			    NULL,			/* no send-to queue */
6933 			    ipif->ipif_ire_type,	/* LOOPBACK */
6934 			    ipif,
6935 			    0,
6936 			    0,
6937 			    0,
6938 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6939 			    RTF_PRIVATE : 0,
6940 			    &ire_uinfo_null,
6941 			    NULL,
6942 			    NULL,
6943 			    ipst);
6944 
6945 			if (ire == NULL) {
6946 				if (ipif_refheld)
6947 					ipif_refrele(ipif);
6948 				return (ENOMEM);
6949 			}
6950 			error = ire_add(&ire, q, mp, func, B_FALSE);
6951 			if (error == 0)
6952 				goto save_ire;
6953 			if (ipif_refheld)
6954 				ipif_refrele(ipif);
6955 			return (error);
6956 
6957 		}
6958 	}
6959 
6960 	/*
6961 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6962 	 * and the gateway address provided is one of the system's interface
6963 	 * addresses.  By using the routing socket interface and supplying an
6964 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6965 	 * specifying an interface route to be created is available which uses
6966 	 * the interface index that specifies the outgoing interface rather than
6967 	 * the address of an outgoing interface (which may not be able to
6968 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6969 	 * flag, routes can be specified which not only specify the next-hop to
6970 	 * be used when routing to a certain prefix, but also which outgoing
6971 	 * interface should be used.
6972 	 *
6973 	 * Previously, interfaces would have unique addresses assigned to them
6974 	 * and so the address assigned to a particular interface could be used
6975 	 * to identify a particular interface.  One exception to this was the
6976 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6977 	 *
6978 	 * With the advent of IPv6 and its link-local addresses, this
6979 	 * restriction was relaxed and interfaces could share addresses between
6980 	 * themselves.  In fact, typically all of the link-local interfaces on
6981 	 * an IPv6 node or router will have the same link-local address.  In
6982 	 * order to differentiate between these interfaces, the use of an
6983 	 * interface index is necessary and this index can be carried inside a
6984 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6985 	 * of using the interface index, however, is that all of the ipif's that
6986 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6987 	 * cannot be used to differentiate between ipif's (or logical
6988 	 * interfaces) that belong to the same ill (physical interface).
6989 	 *
6990 	 * For example, in the following case involving IPv4 interfaces and
6991 	 * logical interfaces
6992 	 *
6993 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6994 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6995 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6996 	 *
6997 	 * the ipif's corresponding to each of these interface routes can be
6998 	 * uniquely identified by the "gateway" (actually interface address).
6999 	 *
7000 	 * In this case involving multiple IPv6 default routes to a particular
7001 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
7002 	 * default route is of interest:
7003 	 *
7004 	 *	default		fe80::123:4567:89ab:cdef	U	if0
7005 	 *	default		fe80::123:4567:89ab:cdef	U	if1
7006 	 */
7007 
7008 	/* RTF_GATEWAY not set */
7009 	if (!(flags & RTF_GATEWAY)) {
7010 		queue_t	*stq;
7011 
7012 		if (sp != NULL) {
7013 			ip2dbg(("ip_rt_add: gateway security attributes "
7014 			    "cannot be set with interface route\n"));
7015 			if (ipif_refheld)
7016 				ipif_refrele(ipif);
7017 			return (EINVAL);
7018 		}
7019 
7020 		/*
7021 		 * As the interface index specified with the RTA_IFP sockaddr is
7022 		 * the same for all ipif's off of an ill, the matching logic
7023 		 * below uses MATCH_IRE_ILL if such an index was specified.
7024 		 * This means that routes sharing the same prefix when added
7025 		 * using a RTA_IFP sockaddr must have distinct interface
7026 		 * indices (namely, they must be on distinct ill's).
7027 		 *
7028 		 * On the other hand, since the gateway address will usually be
7029 		 * different for each ipif on the system, the matching logic
7030 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
7031 		 * route.  This means that interface routes for the same prefix
7032 		 * can be created if they belong to distinct ipif's and if a
7033 		 * RTA_IFP sockaddr is not present.
7034 		 */
7035 		if (ipif_arg != NULL) {
7036 			if (ipif_refheld)  {
7037 				ipif_refrele(ipif);
7038 				ipif_refheld = B_FALSE;
7039 			}
7040 			ipif = ipif_arg;
7041 			match_flags |= MATCH_IRE_ILL;
7042 		} else {
7043 			/*
7044 			 * Check the ipif corresponding to the gw_addr
7045 			 */
7046 			if (ipif == NULL)
7047 				return (ENETUNREACH);
7048 			match_flags |= MATCH_IRE_IPIF;
7049 		}
7050 		ASSERT(ipif != NULL);
7051 
7052 		/*
7053 		 * We check for an existing entry at this point.
7054 		 *
7055 		 * Since a netmask isn't passed in via the ioctl interface
7056 		 * (SIOCADDRT), we don't check for a matching netmask in that
7057 		 * case.
7058 		 */
7059 		if (!ioctl_msg)
7060 			match_flags |= MATCH_IRE_MASK;
7061 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
7062 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7063 		if (ire != NULL) {
7064 			ire_refrele(ire);
7065 			if (ipif_refheld)
7066 				ipif_refrele(ipif);
7067 			return (EEXIST);
7068 		}
7069 
7070 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7071 		    ? ipif->ipif_rq : ipif->ipif_wq;
7072 
7073 		/*
7074 		 * Create a copy of the IRE_LOOPBACK,
7075 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7076 		 * the modified address and netmask.
7077 		 */
7078 		ire = ire_create(
7079 		    (uchar_t *)&dst_addr,
7080 		    (uint8_t *)&mask,
7081 		    (uint8_t *)&ipif->ipif_src_addr,
7082 		    NULL,
7083 		    &ipif->ipif_mtu,
7084 		    NULL,
7085 		    NULL,
7086 		    stq,
7087 		    ipif->ipif_net_type,
7088 		    ipif,
7089 		    0,
7090 		    0,
7091 		    0,
7092 		    flags,
7093 		    &ire_uinfo_null,
7094 		    NULL,
7095 		    NULL,
7096 		    ipst);
7097 		if (ire == NULL) {
7098 			if (ipif_refheld)
7099 				ipif_refrele(ipif);
7100 			return (ENOMEM);
7101 		}
7102 
7103 		/*
7104 		 * Some software (for example, GateD and Sun Cluster) attempts
7105 		 * to create (what amount to) IRE_PREFIX routes with the
7106 		 * loopback address as the gateway.  This is primarily done to
7107 		 * set up prefixes with the RTF_REJECT flag set (for example,
7108 		 * when generating aggregate routes.)
7109 		 *
7110 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7111 		 * IRE_LOOPBACK, then we map the request into a
7112 		 * IRE_IF_NORESOLVER.
7113 		 *
7114 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7115 		 * routine, but rather using ire_create() directly.
7116 		 *
7117 		 */
7118 		if (ipif->ipif_net_type == IRE_LOOPBACK)
7119 			ire->ire_type = IRE_IF_NORESOLVER;
7120 
7121 		error = ire_add(&ire, q, mp, func, B_FALSE);
7122 		if (error == 0)
7123 			goto save_ire;
7124 
7125 		/*
7126 		 * In the result of failure, ire_add() will have already
7127 		 * deleted the ire in question, so there is no need to
7128 		 * do that here.
7129 		 */
7130 		if (ipif_refheld)
7131 			ipif_refrele(ipif);
7132 		return (error);
7133 	}
7134 	if (ipif_refheld) {
7135 		ipif_refrele(ipif);
7136 		ipif_refheld = B_FALSE;
7137 	}
7138 
7139 	/*
7140 	 * Get an interface IRE for the specified gateway.
7141 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7142 	 * gateway, it is currently unreachable and we fail the request
7143 	 * accordingly.
7144 	 */
7145 	ipif = ipif_arg;
7146 	if (ipif_arg != NULL)
7147 		match_flags |= MATCH_IRE_ILL;
7148 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7149 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7150 	if (gw_ire == NULL)
7151 		return (ENETUNREACH);
7152 
7153 	/*
7154 	 * We create one of three types of IREs as a result of this request
7155 	 * based on the netmask.  A netmask of all ones (which is automatically
7156 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7157 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7158 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7159 	 * destination prefix.
7160 	 */
7161 	if (mask == IP_HOST_MASK)
7162 		type = IRE_HOST;
7163 	else if (mask == 0)
7164 		type = IRE_DEFAULT;
7165 	else
7166 		type = IRE_PREFIX;
7167 
7168 	/* check for a duplicate entry */
7169 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7170 	    NULL, ALL_ZONES, 0, NULL,
7171 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7172 	if (ire != NULL) {
7173 		ire_refrele(gw_ire);
7174 		ire_refrele(ire);
7175 		return (EEXIST);
7176 	}
7177 
7178 	/* Security attribute exists */
7179 	if (sp != NULL) {
7180 		tsol_gcgrp_addr_t ga;
7181 
7182 		/* find or create the gateway credentials group */
7183 		ga.ga_af = AF_INET;
7184 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7185 
7186 		/* we hold reference to it upon success */
7187 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7188 		if (gcgrp == NULL) {
7189 			ire_refrele(gw_ire);
7190 			return (ENOMEM);
7191 		}
7192 
7193 		/*
7194 		 * Create and add the security attribute to the group; a
7195 		 * reference to the group is made upon allocating a new
7196 		 * entry successfully.  If it finds an already-existing
7197 		 * entry for the security attribute in the group, it simply
7198 		 * returns it and no new reference is made to the group.
7199 		 */
7200 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7201 		if (gc == NULL) {
7202 			/* release reference held by gcgrp_lookup */
7203 			GCGRP_REFRELE(gcgrp);
7204 			ire_refrele(gw_ire);
7205 			return (ENOMEM);
7206 		}
7207 	}
7208 
7209 	/* Create the IRE. */
7210 	ire = ire_create(
7211 	    (uchar_t *)&dst_addr,		/* dest address */
7212 	    (uchar_t *)&mask,			/* mask */
7213 	    /* src address assigned by the caller? */
7214 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7215 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7216 	    (uchar_t *)&gw_addr,		/* gateway address */
7217 	    &gw_ire->ire_max_frag,
7218 	    NULL,				/* no src nce */
7219 	    NULL,				/* no recv-from queue */
7220 	    NULL,				/* no send-to queue */
7221 	    (ushort_t)type,			/* IRE type */
7222 	    ipif_arg,
7223 	    0,
7224 	    0,
7225 	    0,
7226 	    flags,
7227 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7228 	    gc,					/* security attribute */
7229 	    NULL,
7230 	    ipst);
7231 
7232 	/*
7233 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7234 	 * reference to the 'gcgrp'. We can now release the extra reference
7235 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7236 	 */
7237 	if (gcgrp_xtraref)
7238 		GCGRP_REFRELE(gcgrp);
7239 	if (ire == NULL) {
7240 		if (gc != NULL)
7241 			GC_REFRELE(gc);
7242 		ire_refrele(gw_ire);
7243 		return (ENOMEM);
7244 	}
7245 
7246 	/*
7247 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7248 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7249 	 */
7250 
7251 	/* Add the new IRE. */
7252 	error = ire_add(&ire, q, mp, func, B_FALSE);
7253 	if (error != 0) {
7254 		/*
7255 		 * In the result of failure, ire_add() will have already
7256 		 * deleted the ire in question, so there is no need to
7257 		 * do that here.
7258 		 */
7259 		ire_refrele(gw_ire);
7260 		return (error);
7261 	}
7262 
7263 	if (flags & RTF_MULTIRT) {
7264 		/*
7265 		 * Invoke the CGTP (multirouting) filtering module
7266 		 * to add the dst address in the filtering database.
7267 		 * Replicated inbound packets coming from that address
7268 		 * will be filtered to discard the duplicates.
7269 		 * It is not necessary to call the CGTP filter hook
7270 		 * when the dst address is a broadcast or multicast,
7271 		 * because an IP source address cannot be a broadcast
7272 		 * or a multicast.
7273 		 */
7274 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7275 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7276 		if (ire_dst != NULL) {
7277 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7278 			ire_refrele(ire_dst);
7279 			goto save_ire;
7280 		}
7281 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr) &&
7282 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
7283 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
7284 			    ire->ire_addr,
7285 			    ire->ire_gateway_addr,
7286 			    ire->ire_src_addr,
7287 			    gw_ire->ire_src_addr);
7288 			if (res != 0) {
7289 				ire_refrele(gw_ire);
7290 				ire_delete(ire);
7291 				return (res);
7292 			}
7293 		}
7294 	}
7295 
7296 	/*
7297 	 * Now that the prefix IRE entry has been created, delete any
7298 	 * existing gateway IRE cache entries as well as any IRE caches
7299 	 * using the gateway, and force them to be created through
7300 	 * ip_newroute.
7301 	 */
7302 	if (gc != NULL) {
7303 		ASSERT(gcgrp != NULL);
7304 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7305 	}
7306 
7307 save_ire:
7308 	if (gw_ire != NULL) {
7309 		ire_refrele(gw_ire);
7310 	}
7311 	if (ipif != NULL) {
7312 		/*
7313 		 * Save enough information so that we can recreate the IRE if
7314 		 * the interface goes down and then up.  The metrics associated
7315 		 * with the route will be saved as well when rts_setmetrics() is
7316 		 * called after the IRE has been created.  In the case where
7317 		 * memory cannot be allocated, none of this information will be
7318 		 * saved.
7319 		 */
7320 		ipif_save_ire(ipif, ire);
7321 	}
7322 	if (ioctl_msg)
7323 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7324 	if (ire_arg != NULL) {
7325 		/*
7326 		 * Store the ire that was successfully added into where ire_arg
7327 		 * points to so that callers don't have to look it up
7328 		 * themselves (but they are responsible for ire_refrele()ing
7329 		 * the ire when they are finished with it).
7330 		 */
7331 		*ire_arg = ire;
7332 	} else {
7333 		ire_refrele(ire);		/* Held in ire_add */
7334 	}
7335 	if (ipif_refheld)
7336 		ipif_refrele(ipif);
7337 	return (0);
7338 }
7339 
7340 /*
7341  * ip_rt_delete is called to delete an IPv4 route.
7342  * ipif_arg is passed in to associate it with the correct interface.
7343  * We may need to restart this operation if the ipif cannot be looked up
7344  * due to an exclusive operation that is currently in progress. The restart
7345  * entry point is specified by 'func'
7346  */
7347 /* ARGSUSED4 */
7348 int
7349 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7350     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7351     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7352 {
7353 	ire_t	*ire = NULL;
7354 	ipif_t	*ipif;
7355 	boolean_t ipif_refheld = B_FALSE;
7356 	uint_t	type;
7357 	uint_t	match_flags = MATCH_IRE_TYPE;
7358 	int	err = 0;
7359 
7360 	ip1dbg(("ip_rt_delete:"));
7361 	/*
7362 	 * If this is the case of RTF_HOST being set, then we set the netmask
7363 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7364 	 */
7365 	if (flags & RTF_HOST) {
7366 		mask = IP_HOST_MASK;
7367 		match_flags |= MATCH_IRE_MASK;
7368 	} else if (rtm_addrs & RTA_NETMASK) {
7369 		match_flags |= MATCH_IRE_MASK;
7370 	}
7371 
7372 	/*
7373 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7374 	 * we check if the gateway address is one of our interfaces first,
7375 	 * and fall back on RTF_GATEWAY routes.
7376 	 *
7377 	 * This makes it possible to delete an original
7378 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7379 	 *
7380 	 * As the interface index specified with the RTA_IFP sockaddr is the
7381 	 * same for all ipif's off of an ill, the matching logic below uses
7382 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7383 	 * sharing the same prefix and interface index as the the route
7384 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7385 	 * is specified in the request.
7386 	 *
7387 	 * On the other hand, since the gateway address will usually be
7388 	 * different for each ipif on the system, the matching logic
7389 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7390 	 * route.  This means that interface routes for the same prefix can be
7391 	 * uniquely identified if they belong to distinct ipif's and if a
7392 	 * RTA_IFP sockaddr is not present.
7393 	 *
7394 	 * For more detail on specifying routes by gateway address and by
7395 	 * interface index, see the comments in ip_rt_add().
7396 	 */
7397 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7398 	    ipst);
7399 	if (ipif != NULL)
7400 		ipif_refheld = B_TRUE;
7401 	else if (err == EINPROGRESS)
7402 		return (err);
7403 	else
7404 		err = 0;
7405 	if (ipif != NULL) {
7406 		if (ipif_arg != NULL) {
7407 			if (ipif_refheld) {
7408 				ipif_refrele(ipif);
7409 				ipif_refheld = B_FALSE;
7410 			}
7411 			ipif = ipif_arg;
7412 			match_flags |= MATCH_IRE_ILL;
7413 		} else {
7414 			match_flags |= MATCH_IRE_IPIF;
7415 		}
7416 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7417 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7418 			    ALL_ZONES, NULL, match_flags, ipst);
7419 		}
7420 		if (ire == NULL) {
7421 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7422 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7423 			    match_flags, ipst);
7424 		}
7425 	}
7426 
7427 	if (ire == NULL) {
7428 		/*
7429 		 * At this point, the gateway address is not one of our own
7430 		 * addresses or a matching interface route was not found.  We
7431 		 * set the IRE type to lookup based on whether
7432 		 * this is a host route, a default route or just a prefix.
7433 		 *
7434 		 * If an ipif_arg was passed in, then the lookup is based on an
7435 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7436 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7437 		 * set as the route being looked up is not a traditional
7438 		 * interface route.
7439 		 */
7440 		match_flags &= ~MATCH_IRE_IPIF;
7441 		match_flags |= MATCH_IRE_GW;
7442 		if (ipif_arg != NULL)
7443 			match_flags |= MATCH_IRE_ILL;
7444 		if (mask == IP_HOST_MASK)
7445 			type = IRE_HOST;
7446 		else if (mask == 0)
7447 			type = IRE_DEFAULT;
7448 		else
7449 			type = IRE_PREFIX;
7450 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7451 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7452 	}
7453 
7454 	if (ipif_refheld)
7455 		ipif_refrele(ipif);
7456 
7457 	/* ipif is not refheld anymore */
7458 	if (ire == NULL)
7459 		return (ESRCH);
7460 
7461 	if (ire->ire_flags & RTF_MULTIRT) {
7462 		/*
7463 		 * Invoke the CGTP (multirouting) filtering module
7464 		 * to remove the dst address from the filtering database.
7465 		 * Packets coming from that address will no longer be
7466 		 * filtered to remove duplicates.
7467 		 */
7468 		if (ip_cgtp_filter_ops != NULL &&
7469 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
7470 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(
7471 			    ire->ire_addr, ire->ire_gateway_addr);
7472 		}
7473 		ip_cgtp_bcast_delete(ire, ipst);
7474 	}
7475 
7476 	ipif = ire->ire_ipif;
7477 	if (ipif != NULL)
7478 		ipif_remove_ire(ipif, ire);
7479 	if (ioctl_msg)
7480 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7481 	ire_delete(ire);
7482 	ire_refrele(ire);
7483 	return (err);
7484 }
7485 
7486 /*
7487  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7488  */
7489 /* ARGSUSED */
7490 int
7491 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7492     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7493 {
7494 	ipaddr_t dst_addr;
7495 	ipaddr_t gw_addr;
7496 	ipaddr_t mask;
7497 	int error = 0;
7498 	mblk_t *mp1;
7499 	struct rtentry *rt;
7500 	ipif_t *ipif = NULL;
7501 	ip_stack_t	*ipst;
7502 
7503 	ASSERT(q->q_next == NULL);
7504 	ipst = CONNQ_TO_IPST(q);
7505 
7506 	ip1dbg(("ip_siocaddrt:"));
7507 	/* Existence of mp1 verified in ip_wput_nondata */
7508 	mp1 = mp->b_cont->b_cont;
7509 	rt = (struct rtentry *)mp1->b_rptr;
7510 
7511 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7512 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7513 
7514 	/*
7515 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7516 	 * to a particular host address.  In this case, we set the netmask to
7517 	 * all ones for the particular destination address.  Otherwise,
7518 	 * determine the netmask to be used based on dst_addr and the interfaces
7519 	 * in use.
7520 	 */
7521 	if (rt->rt_flags & RTF_HOST) {
7522 		mask = IP_HOST_MASK;
7523 	} else {
7524 		/*
7525 		 * Note that ip_subnet_mask returns a zero mask in the case of
7526 		 * default (an all-zeroes address).
7527 		 */
7528 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7529 	}
7530 
7531 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7532 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7533 	if (ipif != NULL)
7534 		ipif_refrele(ipif);
7535 	return (error);
7536 }
7537 
7538 /*
7539  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7540  */
7541 /* ARGSUSED */
7542 int
7543 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7544     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7545 {
7546 	ipaddr_t dst_addr;
7547 	ipaddr_t gw_addr;
7548 	ipaddr_t mask;
7549 	int error;
7550 	mblk_t *mp1;
7551 	struct rtentry *rt;
7552 	ipif_t *ipif = NULL;
7553 	ip_stack_t	*ipst;
7554 
7555 	ASSERT(q->q_next == NULL);
7556 	ipst = CONNQ_TO_IPST(q);
7557 
7558 	ip1dbg(("ip_siocdelrt:"));
7559 	/* Existence of mp1 verified in ip_wput_nondata */
7560 	mp1 = mp->b_cont->b_cont;
7561 	rt = (struct rtentry *)mp1->b_rptr;
7562 
7563 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7564 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7565 
7566 	/*
7567 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7568 	 * to a particular host address.  In this case, we set the netmask to
7569 	 * all ones for the particular destination address.  Otherwise,
7570 	 * determine the netmask to be used based on dst_addr and the interfaces
7571 	 * in use.
7572 	 */
7573 	if (rt->rt_flags & RTF_HOST) {
7574 		mask = IP_HOST_MASK;
7575 	} else {
7576 		/*
7577 		 * Note that ip_subnet_mask returns a zero mask in the case of
7578 		 * default (an all-zeroes address).
7579 		 */
7580 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7581 	}
7582 
7583 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7584 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7585 	    mp, ip_process_ioctl, ipst);
7586 	if (ipif != NULL)
7587 		ipif_refrele(ipif);
7588 	return (error);
7589 }
7590 
7591 /*
7592  * Enqueue the mp onto the ipsq, chained by b_next.
7593  * b_prev stores the function to be executed later, and b_queue the queue
7594  * where this mp originated.
7595  */
7596 void
7597 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7598     ill_t *pending_ill)
7599 {
7600 	conn_t	*connp = NULL;
7601 
7602 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7603 	ASSERT(func != NULL);
7604 
7605 	mp->b_queue = q;
7606 	mp->b_prev = (void *)func;
7607 	mp->b_next = NULL;
7608 
7609 	switch (type) {
7610 	case CUR_OP:
7611 		if (ipsq->ipsq_mptail != NULL) {
7612 			ASSERT(ipsq->ipsq_mphead != NULL);
7613 			ipsq->ipsq_mptail->b_next = mp;
7614 		} else {
7615 			ASSERT(ipsq->ipsq_mphead == NULL);
7616 			ipsq->ipsq_mphead = mp;
7617 		}
7618 		ipsq->ipsq_mptail = mp;
7619 		break;
7620 
7621 	case NEW_OP:
7622 		if (ipsq->ipsq_xopq_mptail != NULL) {
7623 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7624 			ipsq->ipsq_xopq_mptail->b_next = mp;
7625 		} else {
7626 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7627 			ipsq->ipsq_xopq_mphead = mp;
7628 		}
7629 		ipsq->ipsq_xopq_mptail = mp;
7630 		break;
7631 	default:
7632 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7633 	}
7634 
7635 	if (CONN_Q(q) && pending_ill != NULL) {
7636 		connp = Q_TO_CONN(q);
7637 
7638 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7639 		connp->conn_oper_pending_ill = pending_ill;
7640 	}
7641 }
7642 
7643 /*
7644  * Return the mp at the head of the ipsq. After emptying the ipsq
7645  * look at the next ioctl, if this ioctl is complete. Otherwise
7646  * return, we will resume when we complete the current ioctl.
7647  * The current ioctl will wait till it gets a response from the
7648  * driver below.
7649  */
7650 static mblk_t *
7651 ipsq_dq(ipsq_t *ipsq)
7652 {
7653 	mblk_t	*mp;
7654 
7655 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7656 
7657 	mp = ipsq->ipsq_mphead;
7658 	if (mp != NULL) {
7659 		ipsq->ipsq_mphead = mp->b_next;
7660 		if (ipsq->ipsq_mphead == NULL)
7661 			ipsq->ipsq_mptail = NULL;
7662 		mp->b_next = NULL;
7663 		return (mp);
7664 	}
7665 	if (ipsq->ipsq_current_ipif != NULL)
7666 		return (NULL);
7667 	mp = ipsq->ipsq_xopq_mphead;
7668 	if (mp != NULL) {
7669 		ipsq->ipsq_xopq_mphead = mp->b_next;
7670 		if (ipsq->ipsq_xopq_mphead == NULL)
7671 			ipsq->ipsq_xopq_mptail = NULL;
7672 		mp->b_next = NULL;
7673 		return (mp);
7674 	}
7675 	return (NULL);
7676 }
7677 
7678 /*
7679  * Enter the ipsq corresponding to ill, by waiting synchronously till
7680  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7681  * will have to drain completely before ipsq_enter returns success.
7682  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7683  * and the ipsq_exit logic will start the next enqueued ioctl after
7684  * completion of the current ioctl. If 'force' is used, we don't wait
7685  * for the enqueued ioctls. This is needed when a conn_close wants to
7686  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7687  * of an ill can also use this option. But we dont' use it currently.
7688  */
7689 #define	ENTER_SQ_WAIT_TICKS 100
7690 boolean_t
7691 ipsq_enter(ill_t *ill, boolean_t force)
7692 {
7693 	ipsq_t	*ipsq;
7694 	boolean_t waited_enough = B_FALSE;
7695 
7696 	/*
7697 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7698 	 * Since the <ill-ipsq> assocs could change while we wait for the
7699 	 * writer, it is easier to wait on a fixed global rather than try to
7700 	 * cv_wait on a changing ipsq.
7701 	 */
7702 	mutex_enter(&ill->ill_lock);
7703 	for (;;) {
7704 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7705 			mutex_exit(&ill->ill_lock);
7706 			return (B_FALSE);
7707 		}
7708 
7709 		ipsq = ill->ill_phyint->phyint_ipsq;
7710 		mutex_enter(&ipsq->ipsq_lock);
7711 		if (ipsq->ipsq_writer == NULL &&
7712 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7713 			break;
7714 		} else if (ipsq->ipsq_writer != NULL) {
7715 			mutex_exit(&ipsq->ipsq_lock);
7716 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7717 		} else {
7718 			mutex_exit(&ipsq->ipsq_lock);
7719 			if (force) {
7720 				(void) cv_timedwait(&ill->ill_cv,
7721 				    &ill->ill_lock,
7722 				    lbolt + ENTER_SQ_WAIT_TICKS);
7723 				waited_enough = B_TRUE;
7724 				continue;
7725 			} else {
7726 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7727 			}
7728 		}
7729 	}
7730 
7731 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7732 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7733 	ipsq->ipsq_writer = curthread;
7734 	ipsq->ipsq_reentry_cnt++;
7735 #ifdef ILL_DEBUG
7736 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7737 #endif
7738 	mutex_exit(&ipsq->ipsq_lock);
7739 	mutex_exit(&ill->ill_lock);
7740 	return (B_TRUE);
7741 }
7742 
7743 /*
7744  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7745  * certain critical operations like plumbing (i.e. most set ioctls),
7746  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7747  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7748  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7749  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7750  * threads executing in the ipsq. Responses from the driver pertain to the
7751  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7752  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7753  *
7754  * If a thread does not want to reenter the ipsq when it is already writer,
7755  * it must make sure that the specified reentry point to be called later
7756  * when the ipsq is empty, nor any code path starting from the specified reentry
7757  * point must never ever try to enter the ipsq again. Otherwise it can lead
7758  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7759  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7760  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7761  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7762  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7763  * ioctl if the current ioctl has completed. If the current ioctl is still
7764  * in progress it simply returns. The current ioctl could be waiting for
7765  * a response from another module (arp_ or the driver or could be waiting for
7766  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7767  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7768  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7769  * ipsq_current_ipif is clear which happens only on ioctl completion.
7770  */
7771 
7772 /*
7773  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7774  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7775  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7776  * completion.
7777  */
7778 ipsq_t *
7779 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7780     ipsq_func_t func, int type, boolean_t reentry_ok)
7781 {
7782 	ipsq_t	*ipsq;
7783 
7784 	/* Only 1 of ipif or ill can be specified */
7785 	ASSERT((ipif != NULL) ^ (ill != NULL));
7786 	if (ipif != NULL)
7787 		ill = ipif->ipif_ill;
7788 
7789 	/*
7790 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7791 	 * ipsq of an ill can't change when ill_lock is held.
7792 	 */
7793 	GRAB_CONN_LOCK(q);
7794 	mutex_enter(&ill->ill_lock);
7795 	ipsq = ill->ill_phyint->phyint_ipsq;
7796 	mutex_enter(&ipsq->ipsq_lock);
7797 
7798 	/*
7799 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7800 	 *    (Note: If the caller does not specify reentry_ok then neither
7801 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7802 	 *    again. Otherwise it can lead to an infinite loop
7803 	 * 2. Enter the ipsq if there is no current writer and this attempted
7804 	 *    entry is part of the current ioctl or operation
7805 	 * 3. Enter the ipsq if there is no current writer and this is a new
7806 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7807 	 *    empty and there is no ioctl (or operation) currently in progress
7808 	 */
7809 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7810 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7811 	    ipsq->ipsq_current_ipif == NULL))) ||
7812 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7813 		/* Success. */
7814 		ipsq->ipsq_reentry_cnt++;
7815 		ipsq->ipsq_writer = curthread;
7816 		mutex_exit(&ipsq->ipsq_lock);
7817 		mutex_exit(&ill->ill_lock);
7818 		RELEASE_CONN_LOCK(q);
7819 #ifdef ILL_DEBUG
7820 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7821 #endif
7822 		return (ipsq);
7823 	}
7824 
7825 	ipsq_enq(ipsq, q, mp, func, type, ill);
7826 
7827 	mutex_exit(&ipsq->ipsq_lock);
7828 	mutex_exit(&ill->ill_lock);
7829 	RELEASE_CONN_LOCK(q);
7830 	return (NULL);
7831 }
7832 
7833 /*
7834  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7835  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7836  * cannot be entered, the mp is queued for completion.
7837  */
7838 void
7839 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7840     boolean_t reentry_ok)
7841 {
7842 	ipsq_t	*ipsq;
7843 
7844 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7845 
7846 	/*
7847 	 * Drop the caller's refhold on the ill.  This is safe since we either
7848 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7849 	 * IPSQ, in which case we return without accessing ill anymore.  This
7850 	 * is needed because func needs to see the correct refcount.
7851 	 * e.g. removeif can work only then.
7852 	 */
7853 	ill_refrele(ill);
7854 	if (ipsq != NULL) {
7855 		(*func)(ipsq, q, mp, NULL);
7856 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7857 	}
7858 }
7859 
7860 /*
7861  * If there are more than ILL_GRP_CNT ills in a group,
7862  * we use kmem alloc'd buffers, else use the stack
7863  */
7864 #define	ILL_GRP_CNT	14
7865 /*
7866  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7867  * Called by a thread that is currently exclusive on this ipsq.
7868  */
7869 void
7870 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7871 {
7872 	queue_t	*q;
7873 	mblk_t	*mp;
7874 	ipsq_func_t	func;
7875 	int	next;
7876 	ill_t	**ill_list = NULL;
7877 	size_t	ill_list_size = 0;
7878 	int	cnt = 0;
7879 	boolean_t need_ipsq_free = B_FALSE;
7880 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7881 
7882 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7883 	mutex_enter(&ipsq->ipsq_lock);
7884 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7885 	if (ipsq->ipsq_reentry_cnt != 1) {
7886 		ipsq->ipsq_reentry_cnt--;
7887 		mutex_exit(&ipsq->ipsq_lock);
7888 		return;
7889 	}
7890 
7891 	mp = ipsq_dq(ipsq);
7892 	while (mp != NULL) {
7893 again:
7894 		mutex_exit(&ipsq->ipsq_lock);
7895 		func = (ipsq_func_t)mp->b_prev;
7896 		q = (queue_t *)mp->b_queue;
7897 		mp->b_prev = NULL;
7898 		mp->b_queue = NULL;
7899 
7900 		/*
7901 		 * If 'q' is an conn queue, it is valid, since we did a
7902 		 * a refhold on the connp, at the start of the ioctl.
7903 		 * If 'q' is an ill queue, it is valid, since close of an
7904 		 * ill will clean up the 'ipsq'.
7905 		 */
7906 		(*func)(ipsq, q, mp, NULL);
7907 
7908 		mutex_enter(&ipsq->ipsq_lock);
7909 		mp = ipsq_dq(ipsq);
7910 	}
7911 
7912 	mutex_exit(&ipsq->ipsq_lock);
7913 
7914 	/*
7915 	 * Need to grab the locks in the right order. Need to
7916 	 * atomically check (under ipsq_lock) that there are no
7917 	 * messages before relinquishing the ipsq. Also need to
7918 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7919 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7920 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7921 	 * to grab ill_g_lock as writer.
7922 	 */
7923 	rw_enter(&ipst->ips_ill_g_lock,
7924 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7925 
7926 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7927 	if (ipsq->ipsq_refs != 0) {
7928 		/* At most 2 ills v4/v6 per phyint */
7929 		cnt = ipsq->ipsq_refs << 1;
7930 		ill_list_size = cnt * sizeof (ill_t *);
7931 		/*
7932 		 * If memory allocation fails, we will do the split
7933 		 * the next time ipsq_exit is called for whatever reason.
7934 		 * As long as the ipsq_split flag is set the need to
7935 		 * split is remembered.
7936 		 */
7937 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7938 		if (ill_list != NULL)
7939 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7940 	}
7941 	mutex_enter(&ipsq->ipsq_lock);
7942 	mp = ipsq_dq(ipsq);
7943 	if (mp != NULL) {
7944 		/* oops, some message has landed up, we can't get out */
7945 		if (ill_list != NULL)
7946 			ill_unlock_ills(ill_list, cnt);
7947 		rw_exit(&ipst->ips_ill_g_lock);
7948 		if (ill_list != NULL)
7949 			kmem_free(ill_list, ill_list_size);
7950 		ill_list = NULL;
7951 		ill_list_size = 0;
7952 		cnt = 0;
7953 		goto again;
7954 	}
7955 
7956 	/*
7957 	 * Split only if no ioctl is pending and if memory alloc succeeded
7958 	 * above.
7959 	 */
7960 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7961 	    ill_list != NULL) {
7962 		/*
7963 		 * No new ill can join this ipsq since we are holding the
7964 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7965 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7966 		 * If so we will retry on the next ipsq_exit.
7967 		 */
7968 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7969 	}
7970 
7971 	/*
7972 	 * We are holding the ipsq lock, hence no new messages can
7973 	 * land up on the ipsq, and there are no messages currently.
7974 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7975 	 * atomically while holding ill locks.
7976 	 */
7977 	ipsq->ipsq_writer = NULL;
7978 	ipsq->ipsq_reentry_cnt--;
7979 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7980 #ifdef ILL_DEBUG
7981 	ipsq->ipsq_depth = 0;
7982 #endif
7983 	mutex_exit(&ipsq->ipsq_lock);
7984 	/*
7985 	 * For IPMP this should wake up all ills in this ipsq.
7986 	 * We need to hold the ill_lock while waking up waiters to
7987 	 * avoid missed wakeups. But there is no need to acquire all
7988 	 * the ill locks and then wakeup. If we have not acquired all
7989 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7990 	 * wakes up ills one at a time after getting the right ill_lock
7991 	 */
7992 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7993 	if (ill_list != NULL)
7994 		ill_unlock_ills(ill_list, cnt);
7995 	if (ipsq->ipsq_refs == 0)
7996 		need_ipsq_free = B_TRUE;
7997 	rw_exit(&ipst->ips_ill_g_lock);
7998 	if (ill_list != 0)
7999 		kmem_free(ill_list, ill_list_size);
8000 
8001 	if (need_ipsq_free) {
8002 		/*
8003 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
8004 		 * looked up. ipsq can be looked up only thru ill or phyint
8005 		 * and there are no ills/phyint on this ipsq.
8006 		 */
8007 		ipsq_delete(ipsq);
8008 	}
8009 	/*
8010 	 * Now start any igmp or mld timers that could not be started
8011 	 * while inside the ipsq. The timers can't be started while inside
8012 	 * the ipsq, since igmp_start_timers may need to call untimeout()
8013 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
8014 	 * there could be a deadlock since the timeout handlers
8015 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
8016 	 * wait in ipsq_enter() trying to get the ipsq.
8017 	 *
8018 	 * However there is one exception to the above. If this thread is
8019 	 * itself the igmp/mld timeout handler thread, then we don't want
8020 	 * to start any new timer until the current handler is done. The
8021 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
8022 	 * all others pass B_TRUE.
8023 	 */
8024 	if (start_igmp_timer) {
8025 		mutex_enter(&ipst->ips_igmp_timer_lock);
8026 		next = ipst->ips_igmp_deferred_next;
8027 		ipst->ips_igmp_deferred_next = INFINITY;
8028 		mutex_exit(&ipst->ips_igmp_timer_lock);
8029 
8030 		if (next != INFINITY)
8031 			igmp_start_timers(next, ipst);
8032 	}
8033 
8034 	if (start_mld_timer) {
8035 		mutex_enter(&ipst->ips_mld_timer_lock);
8036 		next = ipst->ips_mld_deferred_next;
8037 		ipst->ips_mld_deferred_next = INFINITY;
8038 		mutex_exit(&ipst->ips_mld_timer_lock);
8039 
8040 		if (next != INFINITY)
8041 			mld_start_timers(next, ipst);
8042 	}
8043 }
8044 
8045 /*
8046  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8047  * and `ioccmd'.
8048  */
8049 void
8050 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8051 {
8052 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8053 
8054 	mutex_enter(&ipsq->ipsq_lock);
8055 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8056 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8057 	ipsq->ipsq_current_ipif = ipif;
8058 	ipsq->ipsq_current_ioctl = ioccmd;
8059 	mutex_exit(&ipsq->ipsq_lock);
8060 }
8061 
8062 /*
8063  * Finish the current exclusive operation on `ipsq'.  Note that other
8064  * operations will not be able to proceed until an ipsq_exit() is done.
8065  */
8066 void
8067 ipsq_current_finish(ipsq_t *ipsq)
8068 {
8069 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8070 
8071 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8072 
8073 	/*
8074 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8075 	 * (but we're careful to never set IPIF_CHANGING in that case).
8076 	 */
8077 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8078 		mutex_enter(&ipif->ipif_ill->ill_lock);
8079 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8080 
8081 		/* Send any queued event */
8082 		ill_nic_info_dispatch(ipif->ipif_ill);
8083 		mutex_exit(&ipif->ipif_ill->ill_lock);
8084 	}
8085 
8086 	mutex_enter(&ipsq->ipsq_lock);
8087 	ASSERT(ipsq->ipsq_current_ipif != NULL);
8088 	ipsq->ipsq_current_ipif = NULL;
8089 	ipsq->ipsq_current_ioctl = 0;
8090 	mutex_exit(&ipsq->ipsq_lock);
8091 }
8092 
8093 /*
8094  * The ill is closing. Flush all messages on the ipsq that originated
8095  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8096  * for this ill since ipsq_enter could not have entered until then.
8097  * New messages can't be queued since the CONDEMNED flag is set.
8098  */
8099 static void
8100 ipsq_flush(ill_t *ill)
8101 {
8102 	queue_t	*q;
8103 	mblk_t	*prev;
8104 	mblk_t	*mp;
8105 	mblk_t	*mp_next;
8106 	ipsq_t	*ipsq;
8107 
8108 	ASSERT(IAM_WRITER_ILL(ill));
8109 	ipsq = ill->ill_phyint->phyint_ipsq;
8110 	/*
8111 	 * Flush any messages sent up by the driver.
8112 	 */
8113 	mutex_enter(&ipsq->ipsq_lock);
8114 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8115 		mp_next = mp->b_next;
8116 		q = mp->b_queue;
8117 		if (q == ill->ill_rq || q == ill->ill_wq) {
8118 			/* Remove the mp from the ipsq */
8119 			if (prev == NULL)
8120 				ipsq->ipsq_mphead = mp->b_next;
8121 			else
8122 				prev->b_next = mp->b_next;
8123 			if (ipsq->ipsq_mptail == mp) {
8124 				ASSERT(mp_next == NULL);
8125 				ipsq->ipsq_mptail = prev;
8126 			}
8127 			inet_freemsg(mp);
8128 		} else {
8129 			prev = mp;
8130 		}
8131 	}
8132 	mutex_exit(&ipsq->ipsq_lock);
8133 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8134 	ipsq_xopq_mp_cleanup(ill, NULL);
8135 	ill_pending_mp_cleanup(ill);
8136 }
8137 
8138 /* ARGSUSED */
8139 int
8140 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8141     ip_ioctl_cmd_t *ipip, void *ifreq)
8142 {
8143 	ill_t	*ill;
8144 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8145 	boolean_t isv6;
8146 	conn_t	*connp;
8147 	ip_stack_t	*ipst;
8148 
8149 	connp = Q_TO_CONN(q);
8150 	ipst = connp->conn_netstack->netstack_ip;
8151 	isv6 = connp->conn_af_isv6;
8152 	/*
8153 	 * Set original index.
8154 	 * Failover and failback move logical interfaces
8155 	 * from one physical interface to another.  The
8156 	 * original index indicates the parent of a logical
8157 	 * interface, in other words, the physical interface
8158 	 * the logical interface will be moved back to on
8159 	 * failback.
8160 	 */
8161 
8162 	/*
8163 	 * Don't allow the original index to be changed
8164 	 * for non-failover addresses, autoconfigured
8165 	 * addresses, or IPv6 link local addresses.
8166 	 */
8167 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8168 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8169 		return (EINVAL);
8170 	}
8171 	/*
8172 	 * The new original index must be in use by some
8173 	 * physical interface.
8174 	 */
8175 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8176 	    NULL, NULL, ipst);
8177 	if (ill == NULL)
8178 		return (ENXIO);
8179 	ill_refrele(ill);
8180 
8181 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8182 	/*
8183 	 * When this ipif gets failed back, don't
8184 	 * preserve the original id, as it is no
8185 	 * longer applicable.
8186 	 */
8187 	ipif->ipif_orig_ipifid = 0;
8188 	/*
8189 	 * For IPv4, change the original index of any
8190 	 * multicast addresses associated with the
8191 	 * ipif to the new value.
8192 	 */
8193 	if (!isv6) {
8194 		ilm_t *ilm;
8195 
8196 		mutex_enter(&ipif->ipif_ill->ill_lock);
8197 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8198 		    ilm = ilm->ilm_next) {
8199 			if (ilm->ilm_ipif == ipif) {
8200 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8201 			}
8202 		}
8203 		mutex_exit(&ipif->ipif_ill->ill_lock);
8204 	}
8205 	return (0);
8206 }
8207 
8208 /* ARGSUSED */
8209 int
8210 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8211     ip_ioctl_cmd_t *ipip, void *ifreq)
8212 {
8213 	struct lifreq *lifr = (struct lifreq *)ifreq;
8214 
8215 	/*
8216 	 * Get the original interface index i.e the one
8217 	 * before FAILOVER if it ever happened.
8218 	 */
8219 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8220 	return (0);
8221 }
8222 
8223 /*
8224  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8225  * refhold and return the associated ipif
8226  */
8227 int
8228 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
8229 {
8230 	boolean_t exists;
8231 	struct iftun_req *ta;
8232 	ipif_t	*ipif;
8233 	ill_t	*ill;
8234 	boolean_t isv6;
8235 	mblk_t	*mp1;
8236 	int	error;
8237 	conn_t	*connp;
8238 	ip_stack_t	*ipst;
8239 
8240 	/* Existence verified in ip_wput_nondata */
8241 	mp1 = mp->b_cont->b_cont;
8242 	ta = (struct iftun_req *)mp1->b_rptr;
8243 	/*
8244 	 * Null terminate the string to protect against buffer
8245 	 * overrun. String was generated by user code and may not
8246 	 * be trusted.
8247 	 */
8248 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8249 
8250 	connp = Q_TO_CONN(q);
8251 	isv6 = connp->conn_af_isv6;
8252 	ipst = connp->conn_netstack->netstack_ip;
8253 
8254 	/* Disallows implicit create */
8255 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8256 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8257 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8258 	if (ipif == NULL)
8259 		return (error);
8260 
8261 	if (ipif->ipif_id != 0) {
8262 		/*
8263 		 * We really don't want to set/get tunnel parameters
8264 		 * on virtual tunnel interfaces.  Only allow the
8265 		 * base tunnel to do these.
8266 		 */
8267 		ipif_refrele(ipif);
8268 		return (EINVAL);
8269 	}
8270 
8271 	/*
8272 	 * Send down to tunnel mod for ioctl processing.
8273 	 * Will finish ioctl in ip_rput_other().
8274 	 */
8275 	ill = ipif->ipif_ill;
8276 	if (ill->ill_net_type == IRE_LOOPBACK) {
8277 		ipif_refrele(ipif);
8278 		return (EOPNOTSUPP);
8279 	}
8280 
8281 	if (ill->ill_wq == NULL) {
8282 		ipif_refrele(ipif);
8283 		return (ENXIO);
8284 	}
8285 	/*
8286 	 * Mark the ioctl as coming from an IPv6 interface for
8287 	 * tun's convenience.
8288 	 */
8289 	if (ill->ill_isv6)
8290 		ta->ifta_flags |= 0x80000000;
8291 	*ipifp = ipif;
8292 	return (0);
8293 }
8294 
8295 /*
8296  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8297  * and return the associated ipif.
8298  * Return value:
8299  *	Non zero: An error has occurred. ci may not be filled out.
8300  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8301  *	a held ipif in ci.ci_ipif.
8302  */
8303 int
8304 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
8305     cmd_info_t *ci, ipsq_func_t func)
8306 {
8307 	sin_t		*sin;
8308 	sin6_t		*sin6;
8309 	char		*name;
8310 	struct ifreq    *ifr;
8311 	struct lifreq    *lifr;
8312 	ipif_t		*ipif = NULL;
8313 	ill_t		*ill;
8314 	conn_t		*connp;
8315 	boolean_t	isv6;
8316 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
8317 	boolean_t	exists;
8318 	int		err;
8319 	mblk_t		*mp1;
8320 	zoneid_t	zoneid;
8321 	ip_stack_t	*ipst;
8322 
8323 	if (q->q_next != NULL) {
8324 		ill = (ill_t *)q->q_ptr;
8325 		isv6 = ill->ill_isv6;
8326 		connp = NULL;
8327 		zoneid = ALL_ZONES;
8328 		ipst = ill->ill_ipst;
8329 	} else {
8330 		ill = NULL;
8331 		connp = Q_TO_CONN(q);
8332 		isv6 = connp->conn_af_isv6;
8333 		zoneid = connp->conn_zoneid;
8334 		if (zoneid == GLOBAL_ZONEID) {
8335 			/* global zone can access ipifs in all zones */
8336 			zoneid = ALL_ZONES;
8337 		}
8338 		ipst = connp->conn_netstack->netstack_ip;
8339 	}
8340 
8341 	/* Has been checked in ip_wput_nondata */
8342 	mp1 = mp->b_cont->b_cont;
8343 
8344 
8345 	if (cmd_type == IF_CMD) {
8346 		/* This a old style SIOC[GS]IF* command */
8347 		ifr = (struct ifreq *)mp1->b_rptr;
8348 		/*
8349 		 * Null terminate the string to protect against buffer
8350 		 * overrun. String was generated by user code and may not
8351 		 * be trusted.
8352 		 */
8353 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8354 		sin = (sin_t *)&ifr->ifr_addr;
8355 		name = ifr->ifr_name;
8356 		ci->ci_sin = sin;
8357 		ci->ci_sin6 = NULL;
8358 		ci->ci_lifr = (struct lifreq *)ifr;
8359 	} else {
8360 		/* This a new style SIOC[GS]LIF* command */
8361 		ASSERT(cmd_type == LIF_CMD);
8362 		lifr = (struct lifreq *)mp1->b_rptr;
8363 		/*
8364 		 * Null terminate the string to protect against buffer
8365 		 * overrun. String was generated by user code and may not
8366 		 * be trusted.
8367 		 */
8368 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8369 		name = lifr->lifr_name;
8370 		sin = (sin_t *)&lifr->lifr_addr;
8371 		sin6 = (sin6_t *)&lifr->lifr_addr;
8372 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
8373 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8374 			    LIFNAMSIZ);
8375 		}
8376 		ci->ci_sin = sin;
8377 		ci->ci_sin6 = sin6;
8378 		ci->ci_lifr = lifr;
8379 	}
8380 
8381 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
8382 		/*
8383 		 * The ioctl will be failed if the ioctl comes down
8384 		 * an conn stream
8385 		 */
8386 		if (ill == NULL) {
8387 			/*
8388 			 * Not an ill queue, return EINVAL same as the
8389 			 * old error code.
8390 			 */
8391 			return (ENXIO);
8392 		}
8393 		ipif = ill->ill_ipif;
8394 		ipif_refhold(ipif);
8395 	} else {
8396 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8397 		    &exists, isv6, zoneid,
8398 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8399 		    ipst);
8400 		if (ipif == NULL) {
8401 			if (err == EINPROGRESS)
8402 				return (err);
8403 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
8404 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
8405 				/*
8406 				 * Need to try both v4 and v6 since this
8407 				 * ioctl can come down either v4 or v6
8408 				 * socket. The lifreq.lifr_family passed
8409 				 * down by this ioctl is AF_UNSPEC.
8410 				 */
8411 				ipif = ipif_lookup_on_name(name,
8412 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8413 				    zoneid, (connp == NULL) ? q :
8414 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8415 				if (err == EINPROGRESS)
8416 					return (err);
8417 			}
8418 			err = 0;	/* Ensure we don't use it below */
8419 		}
8420 	}
8421 
8422 	/*
8423 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8424 	 */
8425 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
8426 		ipif_refrele(ipif);
8427 		return (ENXIO);
8428 	}
8429 
8430 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8431 	    name[0] == '\0') {
8432 		/*
8433 		 * Handle a or a SIOC?IF* with a null name
8434 		 * during plumb (on the ill queue before the I_PLINK).
8435 		 */
8436 		ipif = ill->ill_ipif;
8437 		ipif_refhold(ipif);
8438 	}
8439 
8440 	if (ipif == NULL)
8441 		return (ENXIO);
8442 
8443 	/*
8444 	 * Allow only GET operations if this ipif has been created
8445 	 * temporarily due to a MOVE operation.
8446 	 */
8447 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
8448 		ipif_refrele(ipif);
8449 		return (EINVAL);
8450 	}
8451 
8452 	ci->ci_ipif = ipif;
8453 	return (0);
8454 }
8455 
8456 /*
8457  * Return the total number of ipifs.
8458  */
8459 static uint_t
8460 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8461 {
8462 	uint_t numifs = 0;
8463 	ill_t	*ill;
8464 	ill_walk_context_t	ctx;
8465 	ipif_t	*ipif;
8466 
8467 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8468 	ill = ILL_START_WALK_V4(&ctx, ipst);
8469 
8470 	while (ill != NULL) {
8471 		for (ipif = ill->ill_ipif; ipif != NULL;
8472 		    ipif = ipif->ipif_next) {
8473 			if (ipif->ipif_zoneid == zoneid ||
8474 			    ipif->ipif_zoneid == ALL_ZONES)
8475 				numifs++;
8476 		}
8477 		ill = ill_next(&ctx, ill);
8478 	}
8479 	rw_exit(&ipst->ips_ill_g_lock);
8480 	return (numifs);
8481 }
8482 
8483 /*
8484  * Return the total number of ipifs.
8485  */
8486 static uint_t
8487 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8488 {
8489 	uint_t numifs = 0;
8490 	ill_t	*ill;
8491 	ipif_t	*ipif;
8492 	ill_walk_context_t	ctx;
8493 
8494 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8495 
8496 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8497 	if (family == AF_INET)
8498 		ill = ILL_START_WALK_V4(&ctx, ipst);
8499 	else if (family == AF_INET6)
8500 		ill = ILL_START_WALK_V6(&ctx, ipst);
8501 	else
8502 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8503 
8504 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8505 		for (ipif = ill->ill_ipif; ipif != NULL;
8506 		    ipif = ipif->ipif_next) {
8507 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8508 			    !(lifn_flags & LIFC_NOXMIT))
8509 				continue;
8510 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8511 			    !(lifn_flags & LIFC_TEMPORARY))
8512 				continue;
8513 			if (((ipif->ipif_flags &
8514 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8515 			    IPIF_DEPRECATED)) ||
8516 			    IS_LOOPBACK(ill) ||
8517 			    !(ipif->ipif_flags & IPIF_UP)) &&
8518 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8519 				continue;
8520 
8521 			if (zoneid != ipif->ipif_zoneid &&
8522 			    ipif->ipif_zoneid != ALL_ZONES &&
8523 			    (zoneid != GLOBAL_ZONEID ||
8524 			    !(lifn_flags & LIFC_ALLZONES)))
8525 				continue;
8526 
8527 			numifs++;
8528 		}
8529 	}
8530 	rw_exit(&ipst->ips_ill_g_lock);
8531 	return (numifs);
8532 }
8533 
8534 uint_t
8535 ip_get_lifsrcofnum(ill_t *ill)
8536 {
8537 	uint_t numifs = 0;
8538 	ill_t	*ill_head = ill;
8539 	ip_stack_t	*ipst = ill->ill_ipst;
8540 
8541 	/*
8542 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8543 	 * other thread may be trying to relink the ILLs in this usesrc group
8544 	 * and adjusting the ill_usesrc_grp_next pointers
8545 	 */
8546 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8547 	if ((ill->ill_usesrc_ifindex == 0) &&
8548 	    (ill->ill_usesrc_grp_next != NULL)) {
8549 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8550 		    ill = ill->ill_usesrc_grp_next)
8551 			numifs++;
8552 	}
8553 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8554 
8555 	return (numifs);
8556 }
8557 
8558 /* Null values are passed in for ipif, sin, and ifreq */
8559 /* ARGSUSED */
8560 int
8561 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8562     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8563 {
8564 	int *nump;
8565 	conn_t *connp = Q_TO_CONN(q);
8566 
8567 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8568 
8569 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8570 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8571 
8572 	*nump = ip_get_numifs(connp->conn_zoneid,
8573 	    connp->conn_netstack->netstack_ip);
8574 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8575 	return (0);
8576 }
8577 
8578 /* Null values are passed in for ipif, sin, and ifreq */
8579 /* ARGSUSED */
8580 int
8581 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8582     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8583 {
8584 	struct lifnum *lifn;
8585 	mblk_t	*mp1;
8586 	conn_t *connp = Q_TO_CONN(q);
8587 
8588 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8589 
8590 	/* Existence checked in ip_wput_nondata */
8591 	mp1 = mp->b_cont->b_cont;
8592 
8593 	lifn = (struct lifnum *)mp1->b_rptr;
8594 	switch (lifn->lifn_family) {
8595 	case AF_UNSPEC:
8596 	case AF_INET:
8597 	case AF_INET6:
8598 		break;
8599 	default:
8600 		return (EAFNOSUPPORT);
8601 	}
8602 
8603 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8604 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8605 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8606 	return (0);
8607 }
8608 
8609 /* ARGSUSED */
8610 int
8611 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8612     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8613 {
8614 	STRUCT_HANDLE(ifconf, ifc);
8615 	mblk_t *mp1;
8616 	struct iocblk *iocp;
8617 	struct ifreq *ifr;
8618 	ill_walk_context_t	ctx;
8619 	ill_t	*ill;
8620 	ipif_t	*ipif;
8621 	struct sockaddr_in *sin;
8622 	int32_t	ifclen;
8623 	zoneid_t zoneid;
8624 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8625 
8626 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8627 
8628 	ip1dbg(("ip_sioctl_get_ifconf"));
8629 	/* Existence verified in ip_wput_nondata */
8630 	mp1 = mp->b_cont->b_cont;
8631 	iocp = (struct iocblk *)mp->b_rptr;
8632 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8633 
8634 	/*
8635 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8636 	 * the user buffer address and length into which the list of struct
8637 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8638 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8639 	 * the SIOCGIFCONF operation was redefined to simply provide
8640 	 * a large output buffer into which we are supposed to jam the ifreq
8641 	 * array.  The same ioctl command code was used, despite the fact that
8642 	 * both the applications and the kernel code had to change, thus making
8643 	 * it impossible to support both interfaces.
8644 	 *
8645 	 * For reasons not good enough to try to explain, the following
8646 	 * algorithm is used for deciding what to do with one of these:
8647 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8648 	 * form with the output buffer coming down as the continuation message.
8649 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8650 	 * and we have to copy in the ifconf structure to find out how big the
8651 	 * output buffer is and where to copy out to.  Sure no problem...
8652 	 *
8653 	 */
8654 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8655 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8656 		int numifs = 0;
8657 		size_t ifc_bufsize;
8658 
8659 		/*
8660 		 * Must be (better be!) continuation of a TRANSPARENT
8661 		 * IOCTL.  We just copied in the ifconf structure.
8662 		 */
8663 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8664 		    (struct ifconf *)mp1->b_rptr);
8665 
8666 		/*
8667 		 * Allocate a buffer to hold requested information.
8668 		 *
8669 		 * If ifc_len is larger than what is needed, we only
8670 		 * allocate what we will use.
8671 		 *
8672 		 * If ifc_len is smaller than what is needed, return
8673 		 * EINVAL.
8674 		 *
8675 		 * XXX: the ill_t structure can hava 2 counters, for
8676 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8677 		 * number of interfaces for a device, so we don't need
8678 		 * to count them here...
8679 		 */
8680 		numifs = ip_get_numifs(zoneid, ipst);
8681 
8682 		ifclen = STRUCT_FGET(ifc, ifc_len);
8683 		ifc_bufsize = numifs * sizeof (struct ifreq);
8684 		if (ifc_bufsize > ifclen) {
8685 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8686 				/* old behaviour */
8687 				return (EINVAL);
8688 			} else {
8689 				ifc_bufsize = ifclen;
8690 			}
8691 		}
8692 
8693 		mp1 = mi_copyout_alloc(q, mp,
8694 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8695 		if (mp1 == NULL)
8696 			return (ENOMEM);
8697 
8698 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8699 	}
8700 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8701 	/*
8702 	 * the SIOCGIFCONF ioctl only knows about
8703 	 * IPv4 addresses, so don't try to tell
8704 	 * it about interfaces with IPv6-only
8705 	 * addresses. (Last parm 'isv6' is B_FALSE)
8706 	 */
8707 
8708 	ifr = (struct ifreq *)mp1->b_rptr;
8709 
8710 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8711 	ill = ILL_START_WALK_V4(&ctx, ipst);
8712 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8713 		for (ipif = ill->ill_ipif; ipif != NULL;
8714 		    ipif = ipif->ipif_next) {
8715 			if (zoneid != ipif->ipif_zoneid &&
8716 			    ipif->ipif_zoneid != ALL_ZONES)
8717 				continue;
8718 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8719 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8720 					/* old behaviour */
8721 					rw_exit(&ipst->ips_ill_g_lock);
8722 					return (EINVAL);
8723 				} else {
8724 					goto if_copydone;
8725 				}
8726 			}
8727 			(void) ipif_get_name(ipif,
8728 			    ifr->ifr_name,
8729 			    sizeof (ifr->ifr_name));
8730 			sin = (sin_t *)&ifr->ifr_addr;
8731 			*sin = sin_null;
8732 			sin->sin_family = AF_INET;
8733 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8734 			ifr++;
8735 		}
8736 	}
8737 if_copydone:
8738 	rw_exit(&ipst->ips_ill_g_lock);
8739 	mp1->b_wptr = (uchar_t *)ifr;
8740 
8741 	if (STRUCT_BUF(ifc) != NULL) {
8742 		STRUCT_FSET(ifc, ifc_len,
8743 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8744 	}
8745 	return (0);
8746 }
8747 
8748 /*
8749  * Get the interfaces using the address hosted on the interface passed in,
8750  * as a source adddress
8751  */
8752 /* ARGSUSED */
8753 int
8754 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8755     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8756 {
8757 	mblk_t *mp1;
8758 	ill_t	*ill, *ill_head;
8759 	ipif_t	*ipif, *orig_ipif;
8760 	int	numlifs = 0;
8761 	size_t	lifs_bufsize, lifsmaxlen;
8762 	struct	lifreq *lifr;
8763 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8764 	uint_t	ifindex;
8765 	zoneid_t zoneid;
8766 	int err = 0;
8767 	boolean_t isv6 = B_FALSE;
8768 	struct	sockaddr_in	*sin;
8769 	struct	sockaddr_in6	*sin6;
8770 	STRUCT_HANDLE(lifsrcof, lifs);
8771 	ip_stack_t		*ipst;
8772 
8773 	ipst = CONNQ_TO_IPST(q);
8774 
8775 	ASSERT(q->q_next == NULL);
8776 
8777 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8778 
8779 	/* Existence verified in ip_wput_nondata */
8780 	mp1 = mp->b_cont->b_cont;
8781 
8782 	/*
8783 	 * Must be (better be!) continuation of a TRANSPARENT
8784 	 * IOCTL.  We just copied in the lifsrcof structure.
8785 	 */
8786 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8787 	    (struct lifsrcof *)mp1->b_rptr);
8788 
8789 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8790 		return (EINVAL);
8791 
8792 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8793 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8794 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8795 	    ip_process_ioctl, &err, ipst);
8796 	if (ipif == NULL) {
8797 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8798 		    ifindex));
8799 		return (err);
8800 	}
8801 
8802 
8803 	/* Allocate a buffer to hold requested information */
8804 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8805 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8806 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8807 	/* The actual size needed is always returned in lifs_len */
8808 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8809 
8810 	/* If the amount we need is more than what is passed in, abort */
8811 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8812 		ipif_refrele(ipif);
8813 		return (0);
8814 	}
8815 
8816 	mp1 = mi_copyout_alloc(q, mp,
8817 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8818 	if (mp1 == NULL) {
8819 		ipif_refrele(ipif);
8820 		return (ENOMEM);
8821 	}
8822 
8823 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8824 	bzero(mp1->b_rptr, lifs_bufsize);
8825 
8826 	lifr = (struct lifreq *)mp1->b_rptr;
8827 
8828 	ill = ill_head = ipif->ipif_ill;
8829 	orig_ipif = ipif;
8830 
8831 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8832 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8833 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8834 
8835 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8836 	for (; (ill != NULL) && (ill != ill_head);
8837 	    ill = ill->ill_usesrc_grp_next) {
8838 
8839 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8840 			break;
8841 
8842 		ipif = ill->ill_ipif;
8843 		(void) ipif_get_name(ipif,
8844 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8845 		if (ipif->ipif_isv6) {
8846 			sin6 = (sin6_t *)&lifr->lifr_addr;
8847 			*sin6 = sin6_null;
8848 			sin6->sin6_family = AF_INET6;
8849 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8850 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8851 			    &ipif->ipif_v6net_mask);
8852 		} else {
8853 			sin = (sin_t *)&lifr->lifr_addr;
8854 			*sin = sin_null;
8855 			sin->sin_family = AF_INET;
8856 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8857 			lifr->lifr_addrlen = ip_mask_to_plen(
8858 			    ipif->ipif_net_mask);
8859 		}
8860 		lifr++;
8861 	}
8862 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8863 	rw_exit(&ipst->ips_ill_g_lock);
8864 	ipif_refrele(orig_ipif);
8865 	mp1->b_wptr = (uchar_t *)lifr;
8866 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8867 
8868 	return (0);
8869 }
8870 
8871 /* ARGSUSED */
8872 int
8873 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8874     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8875 {
8876 	mblk_t *mp1;
8877 	int	list;
8878 	ill_t	*ill;
8879 	ipif_t	*ipif;
8880 	int	flags;
8881 	int	numlifs = 0;
8882 	size_t	lifc_bufsize;
8883 	struct	lifreq *lifr;
8884 	sa_family_t	family;
8885 	struct	sockaddr_in	*sin;
8886 	struct	sockaddr_in6	*sin6;
8887 	ill_walk_context_t	ctx;
8888 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8889 	int32_t	lifclen;
8890 	zoneid_t zoneid;
8891 	STRUCT_HANDLE(lifconf, lifc);
8892 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8893 
8894 	ip1dbg(("ip_sioctl_get_lifconf"));
8895 
8896 	ASSERT(q->q_next == NULL);
8897 
8898 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8899 
8900 	/* Existence verified in ip_wput_nondata */
8901 	mp1 = mp->b_cont->b_cont;
8902 
8903 	/*
8904 	 * An extended version of SIOCGIFCONF that takes an
8905 	 * additional address family and flags field.
8906 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8907 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8908 	 * interfaces are omitted.
8909 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8910 	 * unless LIFC_TEMPORARY is specified.
8911 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8912 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8913 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8914 	 * has priority over LIFC_NOXMIT.
8915 	 */
8916 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8917 
8918 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8919 		return (EINVAL);
8920 
8921 	/*
8922 	 * Must be (better be!) continuation of a TRANSPARENT
8923 	 * IOCTL.  We just copied in the lifconf structure.
8924 	 */
8925 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8926 
8927 	family = STRUCT_FGET(lifc, lifc_family);
8928 	flags = STRUCT_FGET(lifc, lifc_flags);
8929 
8930 	switch (family) {
8931 	case AF_UNSPEC:
8932 		/*
8933 		 * walk all ILL's.
8934 		 */
8935 		list = MAX_G_HEADS;
8936 		break;
8937 	case AF_INET:
8938 		/*
8939 		 * walk only IPV4 ILL's.
8940 		 */
8941 		list = IP_V4_G_HEAD;
8942 		break;
8943 	case AF_INET6:
8944 		/*
8945 		 * walk only IPV6 ILL's.
8946 		 */
8947 		list = IP_V6_G_HEAD;
8948 		break;
8949 	default:
8950 		return (EAFNOSUPPORT);
8951 	}
8952 
8953 	/*
8954 	 * Allocate a buffer to hold requested information.
8955 	 *
8956 	 * If lifc_len is larger than what is needed, we only
8957 	 * allocate what we will use.
8958 	 *
8959 	 * If lifc_len is smaller than what is needed, return
8960 	 * EINVAL.
8961 	 */
8962 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8963 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8964 	lifclen = STRUCT_FGET(lifc, lifc_len);
8965 	if (lifc_bufsize > lifclen) {
8966 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8967 			return (EINVAL);
8968 		else
8969 			lifc_bufsize = lifclen;
8970 	}
8971 
8972 	mp1 = mi_copyout_alloc(q, mp,
8973 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8974 	if (mp1 == NULL)
8975 		return (ENOMEM);
8976 
8977 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8978 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8979 
8980 	lifr = (struct lifreq *)mp1->b_rptr;
8981 
8982 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8983 	ill = ill_first(list, list, &ctx, ipst);
8984 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8985 		for (ipif = ill->ill_ipif; ipif != NULL;
8986 		    ipif = ipif->ipif_next) {
8987 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8988 			    !(flags & LIFC_NOXMIT))
8989 				continue;
8990 
8991 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8992 			    !(flags & LIFC_TEMPORARY))
8993 				continue;
8994 
8995 			if (((ipif->ipif_flags &
8996 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8997 			    IPIF_DEPRECATED)) ||
8998 			    IS_LOOPBACK(ill) ||
8999 			    !(ipif->ipif_flags & IPIF_UP)) &&
9000 			    (flags & LIFC_EXTERNAL_SOURCE))
9001 				continue;
9002 
9003 			if (zoneid != ipif->ipif_zoneid &&
9004 			    ipif->ipif_zoneid != ALL_ZONES &&
9005 			    (zoneid != GLOBAL_ZONEID ||
9006 			    !(flags & LIFC_ALLZONES)))
9007 				continue;
9008 
9009 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
9010 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
9011 					rw_exit(&ipst->ips_ill_g_lock);
9012 					return (EINVAL);
9013 				} else {
9014 					goto lif_copydone;
9015 				}
9016 			}
9017 
9018 			(void) ipif_get_name(ipif, lifr->lifr_name,
9019 			    sizeof (lifr->lifr_name));
9020 			if (ipif->ipif_isv6) {
9021 				sin6 = (sin6_t *)&lifr->lifr_addr;
9022 				*sin6 = sin6_null;
9023 				sin6->sin6_family = AF_INET6;
9024 				sin6->sin6_addr =
9025 				    ipif->ipif_v6lcl_addr;
9026 				lifr->lifr_addrlen =
9027 				    ip_mask_to_plen_v6(
9028 				    &ipif->ipif_v6net_mask);
9029 			} else {
9030 				sin = (sin_t *)&lifr->lifr_addr;
9031 				*sin = sin_null;
9032 				sin->sin_family = AF_INET;
9033 				sin->sin_addr.s_addr =
9034 				    ipif->ipif_lcl_addr;
9035 				lifr->lifr_addrlen =
9036 				    ip_mask_to_plen(
9037 				    ipif->ipif_net_mask);
9038 			}
9039 			lifr++;
9040 		}
9041 	}
9042 lif_copydone:
9043 	rw_exit(&ipst->ips_ill_g_lock);
9044 
9045 	mp1->b_wptr = (uchar_t *)lifr;
9046 	if (STRUCT_BUF(lifc) != NULL) {
9047 		STRUCT_FSET(lifc, lifc_len,
9048 		    (int)((uchar_t *)lifr - mp1->b_rptr));
9049 	}
9050 	return (0);
9051 }
9052 
9053 /* ARGSUSED */
9054 int
9055 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9056     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9057 {
9058 	ip_stack_t	*ipst;
9059 
9060 	if (q->q_next == NULL)
9061 		ipst = CONNQ_TO_IPST(q);
9062 	else
9063 		ipst = ILLQ_TO_IPST(q);
9064 
9065 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9066 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9067 	return (0);
9068 }
9069 
9070 static void
9071 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9072 {
9073 	ip6_asp_t *table;
9074 	size_t table_size;
9075 	mblk_t *data_mp;
9076 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9077 	ip_stack_t	*ipst;
9078 
9079 	if (q->q_next == NULL)
9080 		ipst = CONNQ_TO_IPST(q);
9081 	else
9082 		ipst = ILLQ_TO_IPST(q);
9083 
9084 	/* These two ioctls are I_STR only */
9085 	if (iocp->ioc_count == TRANSPARENT) {
9086 		miocnak(q, mp, 0, EINVAL);
9087 		return;
9088 	}
9089 
9090 	data_mp = mp->b_cont;
9091 	if (data_mp == NULL) {
9092 		/* The user passed us a NULL argument */
9093 		table = NULL;
9094 		table_size = iocp->ioc_count;
9095 	} else {
9096 		/*
9097 		 * The user provided a table.  The stream head
9098 		 * may have copied in the user data in chunks,
9099 		 * so make sure everything is pulled up
9100 		 * properly.
9101 		 */
9102 		if (MBLKL(data_mp) < iocp->ioc_count) {
9103 			mblk_t *new_data_mp;
9104 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9105 			    NULL) {
9106 				miocnak(q, mp, 0, ENOMEM);
9107 				return;
9108 			}
9109 			freemsg(data_mp);
9110 			data_mp = new_data_mp;
9111 			mp->b_cont = data_mp;
9112 		}
9113 		table = (ip6_asp_t *)data_mp->b_rptr;
9114 		table_size = iocp->ioc_count;
9115 	}
9116 
9117 	switch (iocp->ioc_cmd) {
9118 	case SIOCGIP6ADDRPOLICY:
9119 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9120 		if (iocp->ioc_rval == -1)
9121 			iocp->ioc_error = EINVAL;
9122 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9123 		else if (table != NULL &&
9124 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9125 			ip6_asp_t *src = table;
9126 			ip6_asp32_t *dst = (void *)table;
9127 			int count = table_size / sizeof (ip6_asp_t);
9128 			int i;
9129 
9130 			/*
9131 			 * We need to do an in-place shrink of the array
9132 			 * to match the alignment attributes of the
9133 			 * 32-bit ABI looking at it.
9134 			 */
9135 			/* LINTED: logical expression always true: op "||" */
9136 			ASSERT(sizeof (*src) > sizeof (*dst));
9137 			for (i = 1; i < count; i++)
9138 				bcopy(src + i, dst + i, sizeof (*dst));
9139 		}
9140 #endif
9141 		break;
9142 
9143 	case SIOCSIP6ADDRPOLICY:
9144 		ASSERT(mp->b_prev == NULL);
9145 		mp->b_prev = (void *)q;
9146 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9147 		/*
9148 		 * We pass in the datamodel here so that the ip6_asp_replace()
9149 		 * routine can handle converting from 32-bit to native formats
9150 		 * where necessary.
9151 		 *
9152 		 * A better way to handle this might be to convert the inbound
9153 		 * data structure here, and hang it off a new 'mp'; thus the
9154 		 * ip6_asp_replace() logic would always be dealing with native
9155 		 * format data structures..
9156 		 *
9157 		 * (An even simpler way to handle these ioctls is to just
9158 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9159 		 * and just recompile everything that depends on it.)
9160 		 */
9161 #endif
9162 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9163 		    iocp->ioc_flag & IOC_MODELS);
9164 		return;
9165 	}
9166 
9167 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9168 	qreply(q, mp);
9169 }
9170 
9171 static void
9172 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9173 {
9174 	mblk_t 		*data_mp;
9175 	struct dstinforeq	*dir;
9176 	uint8_t		*end, *cur;
9177 	in6_addr_t	*daddr, *saddr;
9178 	ipaddr_t	v4daddr;
9179 	ire_t		*ire;
9180 	char		*slabel, *dlabel;
9181 	boolean_t	isipv4;
9182 	int		match_ire;
9183 	ill_t		*dst_ill;
9184 	ipif_t		*src_ipif, *ire_ipif;
9185 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9186 	zoneid_t	zoneid;
9187 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9188 
9189 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9190 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9191 
9192 	/*
9193 	 * This ioctl is I_STR only, and must have a
9194 	 * data mblk following the M_IOCTL mblk.
9195 	 */
9196 	data_mp = mp->b_cont;
9197 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9198 		miocnak(q, mp, 0, EINVAL);
9199 		return;
9200 	}
9201 
9202 	if (MBLKL(data_mp) < iocp->ioc_count) {
9203 		mblk_t *new_data_mp;
9204 
9205 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9206 			miocnak(q, mp, 0, ENOMEM);
9207 			return;
9208 		}
9209 		freemsg(data_mp);
9210 		data_mp = new_data_mp;
9211 		mp->b_cont = data_mp;
9212 	}
9213 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9214 
9215 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9216 	    end - cur >= sizeof (struct dstinforeq);
9217 	    cur += sizeof (struct dstinforeq)) {
9218 		dir = (struct dstinforeq *)cur;
9219 		daddr = &dir->dir_daddr;
9220 		saddr = &dir->dir_saddr;
9221 
9222 		/*
9223 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9224 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9225 		 * and ipif_select_source[_v6]() do not.
9226 		 */
9227 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9228 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9229 
9230 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9231 		if (isipv4) {
9232 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9233 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9234 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9235 		} else {
9236 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9237 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9238 		}
9239 		if (ire == NULL) {
9240 			dir->dir_dreachable = 0;
9241 
9242 			/* move on to next dst addr */
9243 			continue;
9244 		}
9245 		dir->dir_dreachable = 1;
9246 
9247 		ire_ipif = ire->ire_ipif;
9248 		if (ire_ipif == NULL)
9249 			goto next_dst;
9250 
9251 		/*
9252 		 * We expect to get back an interface ire or a
9253 		 * gateway ire cache entry.  For both types, the
9254 		 * output interface is ire_ipif->ipif_ill.
9255 		 */
9256 		dst_ill = ire_ipif->ipif_ill;
9257 		dir->dir_dmactype = dst_ill->ill_mactype;
9258 
9259 		if (isipv4) {
9260 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9261 		} else {
9262 			src_ipif = ipif_select_source_v6(dst_ill,
9263 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9264 			    zoneid);
9265 		}
9266 		if (src_ipif == NULL)
9267 			goto next_dst;
9268 
9269 		*saddr = src_ipif->ipif_v6lcl_addr;
9270 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9271 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9272 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9273 		dir->dir_sdeprecated =
9274 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9275 		ipif_refrele(src_ipif);
9276 next_dst:
9277 		ire_refrele(ire);
9278 	}
9279 	miocack(q, mp, iocp->ioc_count, 0);
9280 }
9281 
9282 
9283 /*
9284  * Check if this is an address assigned to this machine.
9285  * Skips interfaces that are down by using ire checks.
9286  * Translates mapped addresses to v4 addresses and then
9287  * treats them as such, returning true if the v4 address
9288  * associated with this mapped address is configured.
9289  * Note: Applications will have to be careful what they do
9290  * with the response; use of mapped addresses limits
9291  * what can be done with the socket, especially with
9292  * respect to socket options and ioctls - neither IPv4
9293  * options nor IPv6 sticky options/ancillary data options
9294  * may be used.
9295  */
9296 /* ARGSUSED */
9297 int
9298 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9299     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9300 {
9301 	struct sioc_addrreq *sia;
9302 	sin_t *sin;
9303 	ire_t *ire;
9304 	mblk_t *mp1;
9305 	zoneid_t zoneid;
9306 	ip_stack_t	*ipst;
9307 
9308 	ip1dbg(("ip_sioctl_tmyaddr"));
9309 
9310 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9311 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9312 	ipst = CONNQ_TO_IPST(q);
9313 
9314 	/* Existence verified in ip_wput_nondata */
9315 	mp1 = mp->b_cont->b_cont;
9316 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9317 	sin = (sin_t *)&sia->sa_addr;
9318 	switch (sin->sin_family) {
9319 	case AF_INET6: {
9320 		sin6_t *sin6 = (sin6_t *)sin;
9321 
9322 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9323 			ipaddr_t v4_addr;
9324 
9325 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9326 			    v4_addr);
9327 			ire = ire_ctable_lookup(v4_addr, 0,
9328 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9329 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9330 		} else {
9331 			in6_addr_t v6addr;
9332 
9333 			v6addr = sin6->sin6_addr;
9334 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9335 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9336 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9337 		}
9338 		break;
9339 	}
9340 	case AF_INET: {
9341 		ipaddr_t v4addr;
9342 
9343 		v4addr = sin->sin_addr.s_addr;
9344 		ire = ire_ctable_lookup(v4addr, 0,
9345 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9346 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9347 		break;
9348 	}
9349 	default:
9350 		return (EAFNOSUPPORT);
9351 	}
9352 	if (ire != NULL) {
9353 		sia->sa_res = 1;
9354 		ire_refrele(ire);
9355 	} else {
9356 		sia->sa_res = 0;
9357 	}
9358 	return (0);
9359 }
9360 
9361 /*
9362  * Check if this is an address assigned on-link i.e. neighbor,
9363  * and makes sure it's reachable from the current zone.
9364  * Returns true for my addresses as well.
9365  * Translates mapped addresses to v4 addresses and then
9366  * treats them as such, returning true if the v4 address
9367  * associated with this mapped address is configured.
9368  * Note: Applications will have to be careful what they do
9369  * with the response; use of mapped addresses limits
9370  * what can be done with the socket, especially with
9371  * respect to socket options and ioctls - neither IPv4
9372  * options nor IPv6 sticky options/ancillary data options
9373  * may be used.
9374  */
9375 /* ARGSUSED */
9376 int
9377 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9378     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9379 {
9380 	struct sioc_addrreq *sia;
9381 	sin_t *sin;
9382 	mblk_t	*mp1;
9383 	ire_t *ire = NULL;
9384 	zoneid_t zoneid;
9385 	ip_stack_t	*ipst;
9386 
9387 	ip1dbg(("ip_sioctl_tonlink"));
9388 
9389 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9390 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9391 	ipst = CONNQ_TO_IPST(q);
9392 
9393 	/* Existence verified in ip_wput_nondata */
9394 	mp1 = mp->b_cont->b_cont;
9395 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9396 	sin = (sin_t *)&sia->sa_addr;
9397 
9398 	/*
9399 	 * Match addresses with a zero gateway field to avoid
9400 	 * routes going through a router.
9401 	 * Exclude broadcast and multicast addresses.
9402 	 */
9403 	switch (sin->sin_family) {
9404 	case AF_INET6: {
9405 		sin6_t *sin6 = (sin6_t *)sin;
9406 
9407 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9408 			ipaddr_t v4_addr;
9409 
9410 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9411 			    v4_addr);
9412 			if (!CLASSD(v4_addr)) {
9413 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9414 				    NULL, NULL, zoneid, NULL,
9415 				    MATCH_IRE_GW, ipst);
9416 			}
9417 		} else {
9418 			in6_addr_t v6addr;
9419 			in6_addr_t v6gw;
9420 
9421 			v6addr = sin6->sin6_addr;
9422 			v6gw = ipv6_all_zeros;
9423 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9424 				ire = ire_route_lookup_v6(&v6addr, 0,
9425 				    &v6gw, 0, NULL, NULL, zoneid,
9426 				    NULL, MATCH_IRE_GW, ipst);
9427 			}
9428 		}
9429 		break;
9430 	}
9431 	case AF_INET: {
9432 		ipaddr_t v4addr;
9433 
9434 		v4addr = sin->sin_addr.s_addr;
9435 		if (!CLASSD(v4addr)) {
9436 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9437 			    NULL, NULL, zoneid, NULL,
9438 			    MATCH_IRE_GW, ipst);
9439 		}
9440 		break;
9441 	}
9442 	default:
9443 		return (EAFNOSUPPORT);
9444 	}
9445 	sia->sa_res = 0;
9446 	if (ire != NULL) {
9447 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9448 		    IRE_LOCAL|IRE_LOOPBACK)) {
9449 			sia->sa_res = 1;
9450 		}
9451 		ire_refrele(ire);
9452 	}
9453 	return (0);
9454 }
9455 
9456 /*
9457  * TBD: implement when kernel maintaines a list of site prefixes.
9458  */
9459 /* ARGSUSED */
9460 int
9461 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9462     ip_ioctl_cmd_t *ipip, void *ifreq)
9463 {
9464 	return (ENXIO);
9465 }
9466 
9467 /* ARGSUSED */
9468 int
9469 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9470     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9471 {
9472 	ill_t  		*ill;
9473 	mblk_t		*mp1;
9474 	conn_t		*connp;
9475 	boolean_t	success;
9476 
9477 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9478 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9479 	/* ioctl comes down on an conn */
9480 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9481 	connp = Q_TO_CONN(q);
9482 
9483 	mp->b_datap->db_type = M_IOCTL;
9484 
9485 	/*
9486 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9487 	 * The original mp contains contaminated b_next values due to 'mi',
9488 	 * which is needed to do the mi_copy_done. Unfortunately if we
9489 	 * send down the original mblk itself and if we are popped due to an
9490 	 * an unplumb before the response comes back from tunnel,
9491 	 * the streamhead (which does a freemsg) will see this contaminated
9492 	 * message and the assertion in freemsg about non-null b_next/b_prev
9493 	 * will panic a DEBUG kernel.
9494 	 */
9495 	mp1 = copymsg(mp);
9496 	if (mp1 == NULL)
9497 		return (ENOMEM);
9498 
9499 	ill = ipif->ipif_ill;
9500 	mutex_enter(&connp->conn_lock);
9501 	mutex_enter(&ill->ill_lock);
9502 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9503 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9504 		    mp, 0);
9505 	} else {
9506 		success = ill_pending_mp_add(ill, connp, mp);
9507 	}
9508 	mutex_exit(&ill->ill_lock);
9509 	mutex_exit(&connp->conn_lock);
9510 
9511 	if (success) {
9512 		ip1dbg(("sending down tunparam request "));
9513 		putnext(ill->ill_wq, mp1);
9514 		return (EINPROGRESS);
9515 	} else {
9516 		/* The conn has started closing */
9517 		freemsg(mp1);
9518 		return (EINTR);
9519 	}
9520 }
9521 
9522 static int
9523 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
9524     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
9525 {
9526 	mblk_t *mp1;
9527 	mblk_t *mp2;
9528 	mblk_t *pending_mp;
9529 	ipaddr_t ipaddr;
9530 	area_t *area;
9531 	struct iocblk *iocp;
9532 	conn_t *connp;
9533 	struct arpreq *ar;
9534 	struct xarpreq *xar;
9535 	boolean_t success;
9536 	int flags, alength;
9537 	char *lladdr;
9538 	ip_stack_t	*ipst;
9539 
9540 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9541 	connp = Q_TO_CONN(q);
9542 	ipst = connp->conn_netstack->netstack_ip;
9543 
9544 	iocp = (struct iocblk *)mp->b_rptr;
9545 	/*
9546 	 * ill has already been set depending on whether
9547 	 * bsd style or interface style ioctl.
9548 	 */
9549 	ASSERT(ill != NULL);
9550 
9551 	/*
9552 	 * Is this one of the new SIOC*XARP ioctls?
9553 	 */
9554 	if (x_arp_ioctl) {
9555 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9556 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9557 		ar = NULL;
9558 
9559 		flags = xar->xarp_flags;
9560 		lladdr = LLADDR(&xar->xarp_ha);
9561 		/*
9562 		 * Validate against user's link layer address length
9563 		 * input and name and addr length limits.
9564 		 */
9565 		alength = ill->ill_phys_addr_length;
9566 		if (iocp->ioc_cmd == SIOCSXARP) {
9567 			if (alength != xar->xarp_ha.sdl_alen ||
9568 			    (alength + xar->xarp_ha.sdl_nlen >
9569 			    sizeof (xar->xarp_ha.sdl_data)))
9570 				return (EINVAL);
9571 		}
9572 	} else {
9573 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9574 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9575 		xar = NULL;
9576 
9577 		flags = ar->arp_flags;
9578 		lladdr = ar->arp_ha.sa_data;
9579 		/*
9580 		 * Theoretically, the sa_family could tell us what link
9581 		 * layer type this operation is trying to deal with. By
9582 		 * common usage AF_UNSPEC means ethernet. We'll assume
9583 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9584 		 * for now. Our new SIOC*XARP ioctls can be used more
9585 		 * generally.
9586 		 *
9587 		 * If the underlying media happens to have a non 6 byte
9588 		 * address, arp module will fail set/get, but the del
9589 		 * operation will succeed.
9590 		 */
9591 		alength = 6;
9592 		if ((iocp->ioc_cmd != SIOCDARP) &&
9593 		    (alength != ill->ill_phys_addr_length)) {
9594 			return (EINVAL);
9595 		}
9596 	}
9597 
9598 	/*
9599 	 * We are going to pass up to ARP a packet chain that looks
9600 	 * like:
9601 	 *
9602 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9603 	 *
9604 	 * Get a copy of the original IOCTL mblk to head the chain,
9605 	 * to be sent up (in mp1). Also get another copy to store
9606 	 * in the ill_pending_mp list, for matching the response
9607 	 * when it comes back from ARP.
9608 	 */
9609 	mp1 = copyb(mp);
9610 	pending_mp = copymsg(mp);
9611 	if (mp1 == NULL || pending_mp == NULL) {
9612 		if (mp1 != NULL)
9613 			freeb(mp1);
9614 		if (pending_mp != NULL)
9615 			inet_freemsg(pending_mp);
9616 		return (ENOMEM);
9617 	}
9618 
9619 	ipaddr = sin->sin_addr.s_addr;
9620 
9621 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9622 	    (caddr_t)&ipaddr);
9623 	if (mp2 == NULL) {
9624 		freeb(mp1);
9625 		inet_freemsg(pending_mp);
9626 		return (ENOMEM);
9627 	}
9628 	/* Put together the chain. */
9629 	mp1->b_cont = mp2;
9630 	mp1->b_datap->db_type = M_IOCTL;
9631 	mp2->b_cont = mp;
9632 	mp2->b_datap->db_type = M_DATA;
9633 
9634 	iocp = (struct iocblk *)mp1->b_rptr;
9635 
9636 	/*
9637 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9638 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9639 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9640 	 * ioc_count field; set ioc_count to be correct.
9641 	 */
9642 	iocp->ioc_count = MBLKL(mp1->b_cont);
9643 
9644 	/*
9645 	 * Set the proper command in the ARP message.
9646 	 * Convert the SIOC{G|S|D}ARP calls into our
9647 	 * AR_ENTRY_xxx calls.
9648 	 */
9649 	area = (area_t *)mp2->b_rptr;
9650 	switch (iocp->ioc_cmd) {
9651 	case SIOCDARP:
9652 	case SIOCDXARP:
9653 		/*
9654 		 * We defer deleting the corresponding IRE until
9655 		 * we return from arp.
9656 		 */
9657 		area->area_cmd = AR_ENTRY_DELETE;
9658 		area->area_proto_mask_offset = 0;
9659 		break;
9660 	case SIOCGARP:
9661 	case SIOCGXARP:
9662 		area->area_cmd = AR_ENTRY_SQUERY;
9663 		area->area_proto_mask_offset = 0;
9664 		break;
9665 	case SIOCSARP:
9666 	case SIOCSXARP: {
9667 		/*
9668 		 * Delete the corresponding ire to make sure IP will
9669 		 * pick up any change from arp.
9670 		 */
9671 		if (!if_arp_ioctl) {
9672 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9673 			break;
9674 		} else {
9675 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9676 			if (ipif != NULL) {
9677 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9678 				    ipst);
9679 				ipif_refrele(ipif);
9680 			}
9681 			break;
9682 		}
9683 	}
9684 	}
9685 	iocp->ioc_cmd = area->area_cmd;
9686 
9687 	/*
9688 	 * Before sending 'mp' to ARP, we have to clear the b_next
9689 	 * and b_prev. Otherwise if STREAMS encounters such a message
9690 	 * in freemsg(), (because ARP can close any time) it can cause
9691 	 * a panic. But mi code needs the b_next and b_prev values of
9692 	 * mp->b_cont, to complete the ioctl. So we store it here
9693 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9694 	 * when the response comes down from ARP.
9695 	 */
9696 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9697 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9698 	mp->b_cont->b_next = NULL;
9699 	mp->b_cont->b_prev = NULL;
9700 
9701 	mutex_enter(&connp->conn_lock);
9702 	mutex_enter(&ill->ill_lock);
9703 	/* conn has not yet started closing, hence this can't fail */
9704 	success = ill_pending_mp_add(ill, connp, pending_mp);
9705 	ASSERT(success);
9706 	mutex_exit(&ill->ill_lock);
9707 	mutex_exit(&connp->conn_lock);
9708 
9709 	/*
9710 	 * Fill in the rest of the ARP operation fields.
9711 	 */
9712 	area->area_hw_addr_length = alength;
9713 	bcopy(lladdr,
9714 	    (char *)area + area->area_hw_addr_offset,
9715 	    area->area_hw_addr_length);
9716 	/* Translate the flags. */
9717 	if (flags & ATF_PERM)
9718 		area->area_flags |= ACE_F_PERMANENT;
9719 	if (flags & ATF_PUBL)
9720 		area->area_flags |= ACE_F_PUBLISH;
9721 	if (flags & ATF_AUTHORITY)
9722 		area->area_flags |= ACE_F_AUTHORITY;
9723 
9724 	/*
9725 	 * Up to ARP it goes.  The response will come
9726 	 * back in ip_wput as an M_IOCACK message, and
9727 	 * will be handed to ip_sioctl_iocack for
9728 	 * completion.
9729 	 */
9730 	putnext(ill->ill_rq, mp1);
9731 	return (EINPROGRESS);
9732 }
9733 
9734 /* ARGSUSED */
9735 int
9736 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9737     ip_ioctl_cmd_t *ipip, void *ifreq)
9738 {
9739 	struct xarpreq *xar;
9740 	boolean_t isv6;
9741 	mblk_t	*mp1;
9742 	int	err;
9743 	conn_t	*connp;
9744 	int ifnamelen;
9745 	ire_t	*ire = NULL;
9746 	ill_t	*ill = NULL;
9747 	struct sockaddr_in *sin;
9748 	boolean_t if_arp_ioctl = B_FALSE;
9749 	ip_stack_t	*ipst;
9750 
9751 	/* ioctl comes down on an conn */
9752 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9753 	connp = Q_TO_CONN(q);
9754 	isv6 = connp->conn_af_isv6;
9755 	ipst = connp->conn_netstack->netstack_ip;
9756 
9757 	/* Existance verified in ip_wput_nondata */
9758 	mp1 = mp->b_cont->b_cont;
9759 
9760 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
9761 	xar = (struct xarpreq *)mp1->b_rptr;
9762 	sin = (sin_t *)&xar->xarp_pa;
9763 
9764 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
9765 	    (xar->xarp_pa.ss_family != AF_INET))
9766 		return (ENXIO);
9767 
9768 	ifnamelen = xar->xarp_ha.sdl_nlen;
9769 	if (ifnamelen != 0) {
9770 		char	*cptr, cval;
9771 
9772 		if (ifnamelen >= LIFNAMSIZ)
9773 			return (EINVAL);
9774 
9775 		/*
9776 		 * Instead of bcopying a bunch of bytes,
9777 		 * null-terminate the string in-situ.
9778 		 */
9779 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9780 		cval = *cptr;
9781 		*cptr = '\0';
9782 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9783 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9784 		    &err, NULL, ipst);
9785 		*cptr = cval;
9786 		if (ill == NULL)
9787 			return (err);
9788 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9789 			ill_refrele(ill);
9790 			return (ENXIO);
9791 		}
9792 
9793 		if_arp_ioctl = B_TRUE;
9794 	} else {
9795 		/*
9796 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9797 		 * as an extended BSD ioctl. The kernel uses the IP address
9798 		 * to figure out the network interface.
9799 		 */
9800 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9801 		    ipst);
9802 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9803 		    ((ill = ire_to_ill(ire)) == NULL) ||
9804 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9805 			if (ire != NULL)
9806 				ire_refrele(ire);
9807 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9808 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9809 			    NULL, MATCH_IRE_TYPE, ipst);
9810 			if ((ire == NULL) ||
9811 			    ((ill = ire_to_ill(ire)) == NULL)) {
9812 				if (ire != NULL)
9813 					ire_refrele(ire);
9814 				return (ENXIO);
9815 			}
9816 		}
9817 		ASSERT(ire != NULL && ill != NULL);
9818 	}
9819 
9820 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9821 	if (if_arp_ioctl)
9822 		ill_refrele(ill);
9823 	if (ire != NULL)
9824 		ire_refrele(ire);
9825 
9826 	return (err);
9827 }
9828 
9829 /*
9830  * ARP IOCTLs.
9831  * How does IP get in the business of fronting ARP configuration/queries?
9832  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9833  * are by tradition passed in through a datagram socket.  That lands in IP.
9834  * As it happens, this is just as well since the interface is quite crude in
9835  * that it passes in no information about protocol or hardware types, or
9836  * interface association.  After making the protocol assumption, IP is in
9837  * the position to look up the name of the ILL, which ARP will need, and
9838  * format a request that can be handled by ARP.	 The request is passed up
9839  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9840  * back a response.  ARP supports its own set of more general IOCTLs, in
9841  * case anyone is interested.
9842  */
9843 /* ARGSUSED */
9844 int
9845 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9846     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9847 {
9848 	struct arpreq *ar;
9849 	struct sockaddr_in *sin;
9850 	ire_t	*ire;
9851 	boolean_t isv6;
9852 	mblk_t	*mp1;
9853 	int	err;
9854 	conn_t	*connp;
9855 	ill_t	*ill;
9856 	ip_stack_t	*ipst;
9857 
9858 	/* ioctl comes down on an conn */
9859 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9860 	connp = Q_TO_CONN(q);
9861 	ipst = CONNQ_TO_IPST(q);
9862 	isv6 = connp->conn_af_isv6;
9863 	if (isv6)
9864 		return (ENXIO);
9865 
9866 	/* Existance verified in ip_wput_nondata */
9867 	mp1 = mp->b_cont->b_cont;
9868 
9869 	ar = (struct arpreq *)mp1->b_rptr;
9870 	sin = (sin_t *)&ar->arp_pa;
9871 
9872 	/*
9873 	 * We need to let ARP know on which interface the IP
9874 	 * address has an ARP mapping. In the IPMP case, a
9875 	 * simple forwarding table lookup will return the
9876 	 * IRE_IF_RESOLVER for the first interface in the group,
9877 	 * which might not be the interface on which the
9878 	 * requested IP address was resolved due to the ill
9879 	 * selection algorithm (see ip_newroute_get_dst_ill()).
9880 	 * So we do a cache table lookup first: if the IRE cache
9881 	 * entry for the IP address is still there, it will
9882 	 * contain the ill pointer for the right interface, so
9883 	 * we use that. If the cache entry has been flushed, we
9884 	 * fall back to the forwarding table lookup. This should
9885 	 * be rare enough since IRE cache entries have a longer
9886 	 * life expectancy than ARP cache entries.
9887 	 */
9888 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL, ipst);
9889 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9890 	    ((ill = ire_to_ill(ire)) == NULL)) {
9891 		if (ire != NULL)
9892 			ire_refrele(ire);
9893 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9894 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9895 		    NULL, MATCH_IRE_TYPE, ipst);
9896 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
9897 			if (ire != NULL)
9898 				ire_refrele(ire);
9899 			return (ENXIO);
9900 		}
9901 	}
9902 	ASSERT(ire != NULL && ill != NULL);
9903 
9904 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
9905 	ire_refrele(ire);
9906 	return (err);
9907 }
9908 
9909 /*
9910  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9911  * atomically set/clear the muxids. Also complete the ioctl by acking or
9912  * naking it.  Note that the code is structured such that the link type,
9913  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9914  * its clones use the persistent link, while pppd(1M) and perhaps many
9915  * other daemons may use non-persistent link.  When combined with some
9916  * ill_t states, linking and unlinking lower streams may be used as
9917  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9918  */
9919 /* ARGSUSED */
9920 void
9921 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9922 {
9923 	mblk_t		*mp1, *mp2;
9924 	struct linkblk	*li;
9925 	struct ipmx_s	*ipmxp;
9926 	ill_t		*ill;
9927 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9928 	int		err = 0;
9929 	boolean_t	entered_ipsq = B_FALSE;
9930 	boolean_t	islink;
9931 	ip_stack_t	*ipst;
9932 
9933 	if (CONN_Q(q))
9934 		ipst = CONNQ_TO_IPST(q);
9935 	else
9936 		ipst = ILLQ_TO_IPST(q);
9937 
9938 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9939 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9940 
9941 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9942 
9943 	mp1 = mp->b_cont;	/* This is the linkblk info */
9944 	li = (struct linkblk *)mp1->b_rptr;
9945 
9946 	/*
9947 	 * ARP has added this special mblk, and the utility is asking us
9948 	 * to perform consistency checks, and also atomically set the
9949 	 * muxid. Ifconfig is an example.  It achieves this by using
9950 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9951 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9952 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9953 	 * and other comments in this routine for more details.
9954 	 */
9955 	mp2 = mp1->b_cont;	/* This is added by ARP */
9956 
9957 	/*
9958 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9959 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9960 	 * get the special mblk above.  For backward compatibility, we
9961 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9962 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9963 	 * not atomic, and can leave the streams unplumbable if the utility
9964 	 * is interrupted before it does the SIOCSLIFMUXID.
9965 	 */
9966 	if (mp2 == NULL) {
9967 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9968 		if (err == EINPROGRESS)
9969 			return;
9970 		goto done;
9971 	}
9972 
9973 	/*
9974 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9975 	 * ARP has appended this last mblk to tell us whether the lower stream
9976 	 * is an arp-dev stream or an IP module stream.
9977 	 */
9978 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9979 	if (ipmxp->ipmx_arpdev_stream) {
9980 		/*
9981 		 * The lower stream is the arp-dev stream.
9982 		 */
9983 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9984 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9985 		if (ill == NULL) {
9986 			if (err == EINPROGRESS)
9987 				return;
9988 			err = EINVAL;
9989 			goto done;
9990 		}
9991 
9992 		if (ipsq == NULL) {
9993 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9994 			    NEW_OP, B_TRUE);
9995 			if (ipsq == NULL) {
9996 				ill_refrele(ill);
9997 				return;
9998 			}
9999 			entered_ipsq = B_TRUE;
10000 		}
10001 		ASSERT(IAM_WRITER_ILL(ill));
10002 		ill_refrele(ill);
10003 
10004 		/*
10005 		 * To ensure consistency between IP and ARP, the following
10006 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
10007 		 * This is because the muxid's are stored in the IP stream on
10008 		 * the ill.
10009 		 *
10010 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
10011 		 * the ARP stream. On an arp-dev stream, IP checks that it is
10012 		 * not yet plinked, and it also checks that the corresponding
10013 		 * IP stream is already plinked.
10014 		 *
10015 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
10016 		 * punlinking the IP stream. IP does not allow punlink of the
10017 		 * IP stream unless the arp stream has been punlinked.
10018 		 */
10019 		if ((islink &&
10020 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
10021 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
10022 			err = EINVAL;
10023 			goto done;
10024 		}
10025 		ill->ill_arp_muxid = islink ? li->l_index : 0;
10026 	} else {
10027 		/*
10028 		 * The lower stream is probably an IP module stream.  Do
10029 		 * consistency checking.
10030 		 */
10031 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
10032 		if (err == EINPROGRESS)
10033 			return;
10034 	}
10035 done:
10036 	if (err == 0)
10037 		miocack(q, mp, 0, 0);
10038 	else
10039 		miocnak(q, mp, 0, err);
10040 
10041 	/* Conn was refheld in ip_sioctl_copyin_setup */
10042 	if (CONN_Q(q))
10043 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
10044 	if (entered_ipsq)
10045 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10046 }
10047 
10048 /*
10049  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
10050  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
10051  * module stream).  If `doconsist' is set, then do the extended consistency
10052  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
10053  * Returns zero on success, EINPROGRESS if the operation is still pending, or
10054  * an error code on failure.
10055  */
10056 static int
10057 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
10058     struct linkblk *li, boolean_t doconsist)
10059 {
10060 	ill_t  		*ill;
10061 	queue_t		*ipwq, *dwq;
10062 	const char	*name;
10063 	struct qinit	*qinfo;
10064 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
10065 
10066 	/*
10067 	 * Walk the lower stream to verify it's the IP module stream.
10068 	 * The IP module is identified by its name, wput function,
10069 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
10070 	 * (li->l_qbot) will not vanish until this ioctl completes.
10071 	 */
10072 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
10073 		qinfo = ipwq->q_qinfo;
10074 		name = qinfo->qi_minfo->mi_idname;
10075 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
10076 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
10077 			break;
10078 		}
10079 	}
10080 
10081 	/*
10082 	 * If this isn't an IP module stream, bail.
10083 	 */
10084 	if (ipwq == NULL)
10085 		return (0);
10086 
10087 	ill = ipwq->q_ptr;
10088 	ASSERT(ill != NULL);
10089 
10090 	if (ipsq == NULL) {
10091 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10092 		    NEW_OP, B_TRUE);
10093 		if (ipsq == NULL)
10094 			return (EINPROGRESS);
10095 	}
10096 	ASSERT(IAM_WRITER_ILL(ill));
10097 
10098 	if (doconsist) {
10099 		/*
10100 		 * Consistency checking requires that I_{P}LINK occurs
10101 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
10102 		 * occurs prior to clearing ill_arp_muxid.
10103 		 */
10104 		if ((islink && ill->ill_ip_muxid != 0) ||
10105 		    (!islink && ill->ill_arp_muxid != 0)) {
10106 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
10107 			return (EINVAL);
10108 		}
10109 	}
10110 
10111 	/*
10112 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10113 	 * the read queue of the module immediately below IP in the ill.
10114 	 * These are used during the capability negotiation below.
10115 	 */
10116 	ill->ill_lmod_rq = NULL;
10117 	ill->ill_lmod_cnt = 0;
10118 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10119 		ill->ill_lmod_rq = RD(dwq);
10120 		for (; dwq != NULL; dwq = dwq->q_next)
10121 			ill->ill_lmod_cnt++;
10122 	}
10123 
10124 	if (doconsist)
10125 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10126 
10127 	/*
10128 	 * If there's at least one up ipif on this ill, then we're bound to
10129 	 * the underlying driver via DLPI.  In that case, renegotiate
10130 	 * capabilities to account for any possible change in modules
10131 	 * interposed between IP and the driver.
10132 	 */
10133 	if (ill->ill_ipif_up_count > 0) {
10134 		if (islink)
10135 			ill_capability_probe(ill);
10136 		else
10137 			ill_capability_reset(ill);
10138 	}
10139 
10140 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10141 	return (0);
10142 }
10143 
10144 /*
10145  * Search the ioctl command in the ioctl tables and return a pointer
10146  * to the ioctl command information. The ioctl command tables are
10147  * static and fully populated at compile time.
10148  */
10149 ip_ioctl_cmd_t *
10150 ip_sioctl_lookup(int ioc_cmd)
10151 {
10152 	int index;
10153 	ip_ioctl_cmd_t *ipip;
10154 	ip_ioctl_cmd_t *ipip_end;
10155 
10156 	if (ioc_cmd == IPI_DONTCARE)
10157 		return (NULL);
10158 
10159 	/*
10160 	 * Do a 2 step search. First search the indexed table
10161 	 * based on the least significant byte of the ioctl cmd.
10162 	 * If we don't find a match, then search the misc table
10163 	 * serially.
10164 	 */
10165 	index = ioc_cmd & 0xFF;
10166 	if (index < ip_ndx_ioctl_count) {
10167 		ipip = &ip_ndx_ioctl_table[index];
10168 		if (ipip->ipi_cmd == ioc_cmd) {
10169 			/* Found a match in the ndx table */
10170 			return (ipip);
10171 		}
10172 	}
10173 
10174 	/* Search the misc table */
10175 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10176 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10177 		if (ipip->ipi_cmd == ioc_cmd)
10178 			/* Found a match in the misc table */
10179 			return (ipip);
10180 	}
10181 
10182 	return (NULL);
10183 }
10184 
10185 /*
10186  * Wrapper function for resuming deferred ioctl processing
10187  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10188  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10189  */
10190 /* ARGSUSED */
10191 void
10192 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10193     void *dummy_arg)
10194 {
10195 	ip_sioctl_copyin_setup(q, mp);
10196 }
10197 
10198 /*
10199  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10200  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10201  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10202  * We establish here the size of the block to be copied in.  mi_copyin
10203  * arranges for this to happen, an processing continues in ip_wput with
10204  * an M_IOCDATA message.
10205  */
10206 void
10207 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10208 {
10209 	int	copyin_size;
10210 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10211 	ip_ioctl_cmd_t *ipip;
10212 	cred_t *cr;
10213 	ip_stack_t	*ipst;
10214 
10215 	if (CONN_Q(q))
10216 		ipst = CONNQ_TO_IPST(q);
10217 	else
10218 		ipst = ILLQ_TO_IPST(q);
10219 
10220 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10221 	if (ipip == NULL) {
10222 		/*
10223 		 * The ioctl is not one we understand or own.
10224 		 * Pass it along to be processed down stream,
10225 		 * if this is a module instance of IP, else nak
10226 		 * the ioctl.
10227 		 */
10228 		if (q->q_next == NULL) {
10229 			goto nak;
10230 		} else {
10231 			putnext(q, mp);
10232 			return;
10233 		}
10234 	}
10235 
10236 	/*
10237 	 * If this is deferred, then we will do all the checks when we
10238 	 * come back.
10239 	 */
10240 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10241 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10242 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10243 		return;
10244 	}
10245 
10246 	/*
10247 	 * Only allow a very small subset of IP ioctls on this stream if
10248 	 * IP is a module and not a driver. Allowing ioctls to be processed
10249 	 * in this case may cause assert failures or data corruption.
10250 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10251 	 * ioctls allowed on an IP module stream, after which this stream
10252 	 * normally becomes a multiplexor (at which time the stream head
10253 	 * will fail all ioctls).
10254 	 */
10255 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10256 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10257 			/*
10258 			 * Pass common Streams ioctls which the IP
10259 			 * module does not own or consume along to
10260 			 * be processed down stream.
10261 			 */
10262 			putnext(q, mp);
10263 			return;
10264 		} else {
10265 			goto nak;
10266 		}
10267 	}
10268 
10269 	/* Make sure we have ioctl data to process. */
10270 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10271 		goto nak;
10272 
10273 	/*
10274 	 * Prefer dblk credential over ioctl credential; some synthesized
10275 	 * ioctls have kcred set because there's no way to crhold()
10276 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10277 	 * the framework; the caller of ioctl needs to hold the reference
10278 	 * for the duration of the call).
10279 	 */
10280 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10281 
10282 	/* Make sure normal users don't send down privileged ioctls */
10283 	if ((ipip->ipi_flags & IPI_PRIV) &&
10284 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10285 		/* We checked the privilege earlier but log it here */
10286 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10287 		return;
10288 	}
10289 
10290 	/*
10291 	 * The ioctl command tables can only encode fixed length
10292 	 * ioctl data. If the length is variable, the table will
10293 	 * encode the length as zero. Such special cases are handled
10294 	 * below in the switch.
10295 	 */
10296 	if (ipip->ipi_copyin_size != 0) {
10297 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10298 		return;
10299 	}
10300 
10301 	switch (iocp->ioc_cmd) {
10302 	case O_SIOCGIFCONF:
10303 	case SIOCGIFCONF:
10304 		/*
10305 		 * This IOCTL is hilarious.  See comments in
10306 		 * ip_sioctl_get_ifconf for the story.
10307 		 */
10308 		if (iocp->ioc_count == TRANSPARENT)
10309 			copyin_size = SIZEOF_STRUCT(ifconf,
10310 			    iocp->ioc_flag);
10311 		else
10312 			copyin_size = iocp->ioc_count;
10313 		mi_copyin(q, mp, NULL, copyin_size);
10314 		return;
10315 
10316 	case O_SIOCGLIFCONF:
10317 	case SIOCGLIFCONF:
10318 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10319 		mi_copyin(q, mp, NULL, copyin_size);
10320 		return;
10321 
10322 	case SIOCGLIFSRCOF:
10323 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10324 		mi_copyin(q, mp, NULL, copyin_size);
10325 		return;
10326 	case SIOCGIP6ADDRPOLICY:
10327 		ip_sioctl_ip6addrpolicy(q, mp);
10328 		ip6_asp_table_refrele(ipst);
10329 		return;
10330 
10331 	case SIOCSIP6ADDRPOLICY:
10332 		ip_sioctl_ip6addrpolicy(q, mp);
10333 		return;
10334 
10335 	case SIOCGDSTINFO:
10336 		ip_sioctl_dstinfo(q, mp);
10337 		ip6_asp_table_refrele(ipst);
10338 		return;
10339 
10340 	case I_PLINK:
10341 	case I_PUNLINK:
10342 	case I_LINK:
10343 	case I_UNLINK:
10344 		/*
10345 		 * We treat non-persistent link similarly as the persistent
10346 		 * link case, in terms of plumbing/unplumbing, as well as
10347 		 * dynamic re-plumbing events indicator.  See comments
10348 		 * in ip_sioctl_plink() for more.
10349 		 *
10350 		 * Request can be enqueued in the 'ipsq' while waiting
10351 		 * to become exclusive. So bump up the conn ref.
10352 		 */
10353 		if (CONN_Q(q))
10354 			CONN_INC_REF(Q_TO_CONN(q));
10355 		ip_sioctl_plink(NULL, q, mp, NULL);
10356 		return;
10357 
10358 	case ND_GET:
10359 	case ND_SET:
10360 		/*
10361 		 * Use of the nd table requires holding the reader lock.
10362 		 * Modifying the nd table thru nd_load/nd_unload requires
10363 		 * the writer lock.
10364 		 */
10365 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10366 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10367 			rw_exit(&ipst->ips_ip_g_nd_lock);
10368 
10369 			if (iocp->ioc_error)
10370 				iocp->ioc_count = 0;
10371 			mp->b_datap->db_type = M_IOCACK;
10372 			qreply(q, mp);
10373 			return;
10374 		}
10375 		rw_exit(&ipst->ips_ip_g_nd_lock);
10376 		/*
10377 		 * We don't understand this subioctl of ND_GET / ND_SET.
10378 		 * Maybe intended for some driver / module below us
10379 		 */
10380 		if (q->q_next) {
10381 			putnext(q, mp);
10382 		} else {
10383 			iocp->ioc_error = ENOENT;
10384 			mp->b_datap->db_type = M_IOCNAK;
10385 			iocp->ioc_count = 0;
10386 			qreply(q, mp);
10387 		}
10388 		return;
10389 
10390 	case IP_IOCTL:
10391 		ip_wput_ioctl(q, mp);
10392 		return;
10393 	default:
10394 		cmn_err(CE_PANIC, "should not happen ");
10395 	}
10396 nak:
10397 	if (mp->b_cont != NULL) {
10398 		freemsg(mp->b_cont);
10399 		mp->b_cont = NULL;
10400 	}
10401 	iocp->ioc_error = EINVAL;
10402 	mp->b_datap->db_type = M_IOCNAK;
10403 	iocp->ioc_count = 0;
10404 	qreply(q, mp);
10405 }
10406 
10407 /* ip_wput hands off ARP IOCTL responses to us */
10408 void
10409 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10410 {
10411 	struct arpreq *ar;
10412 	struct xarpreq *xar;
10413 	area_t	*area;
10414 	mblk_t	*area_mp;
10415 	struct iocblk *iocp;
10416 	mblk_t	*orig_ioc_mp, *tmp;
10417 	struct iocblk	*orig_iocp;
10418 	ill_t *ill;
10419 	conn_t *connp = NULL;
10420 	uint_t ioc_id;
10421 	mblk_t *pending_mp;
10422 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10423 	int *flagsp;
10424 	char *storage = NULL;
10425 	sin_t *sin;
10426 	ipaddr_t addr;
10427 	int err;
10428 	ip_stack_t *ipst;
10429 
10430 	ill = q->q_ptr;
10431 	ASSERT(ill != NULL);
10432 	ipst = ill->ill_ipst;
10433 
10434 	/*
10435 	 * We should get back from ARP a packet chain that looks like:
10436 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10437 	 */
10438 	if (!(area_mp = mp->b_cont) ||
10439 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10440 	    !(orig_ioc_mp = area_mp->b_cont) ||
10441 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10442 		freemsg(mp);
10443 		return;
10444 	}
10445 
10446 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10447 
10448 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10449 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10450 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10451 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10452 		x_arp_ioctl = B_TRUE;
10453 		xar = (struct xarpreq *)tmp->b_rptr;
10454 		sin = (sin_t *)&xar->xarp_pa;
10455 		flagsp = &xar->xarp_flags;
10456 		storage = xar->xarp_ha.sdl_data;
10457 		if (xar->xarp_ha.sdl_nlen != 0)
10458 			ifx_arp_ioctl = B_TRUE;
10459 	} else {
10460 		ar = (struct arpreq *)tmp->b_rptr;
10461 		sin = (sin_t *)&ar->arp_pa;
10462 		flagsp = &ar->arp_flags;
10463 		storage = ar->arp_ha.sa_data;
10464 	}
10465 
10466 	iocp = (struct iocblk *)mp->b_rptr;
10467 
10468 	/*
10469 	 * Pick out the originating queue based on the ioc_id.
10470 	 */
10471 	ioc_id = iocp->ioc_id;
10472 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10473 	if (pending_mp == NULL) {
10474 		ASSERT(connp == NULL);
10475 		inet_freemsg(mp);
10476 		return;
10477 	}
10478 	ASSERT(connp != NULL);
10479 	q = CONNP_TO_WQ(connp);
10480 
10481 	/* Uncouple the internally generated IOCTL from the original one */
10482 	area = (area_t *)area_mp->b_rptr;
10483 	area_mp->b_cont = NULL;
10484 
10485 	/*
10486 	 * Restore the b_next and b_prev used by mi code. This is needed
10487 	 * to complete the ioctl using mi* functions. We stored them in
10488 	 * the pending mp prior to sending the request to ARP.
10489 	 */
10490 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10491 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10492 	inet_freemsg(pending_mp);
10493 
10494 	/*
10495 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10496 	 * Catch the case where there is an IRE_CACHE by no entry in the
10497 	 * arp table.
10498 	 */
10499 	addr = sin->sin_addr.s_addr;
10500 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10501 		ire_t			*ire;
10502 		dl_unitdata_req_t	*dlup;
10503 		mblk_t			*llmp;
10504 		int			addr_len;
10505 		ill_t			*ipsqill = NULL;
10506 
10507 		if (ifx_arp_ioctl) {
10508 			/*
10509 			 * There's no need to lookup the ill, since
10510 			 * we've already done that when we started
10511 			 * processing the ioctl and sent the message
10512 			 * to ARP on that ill.  So use the ill that
10513 			 * is stored in q->q_ptr.
10514 			 */
10515 			ipsqill = ill;
10516 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10517 			    ipsqill->ill_ipif, ALL_ZONES,
10518 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10519 		} else {
10520 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10521 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10522 			if (ire != NULL)
10523 				ipsqill = ire_to_ill(ire);
10524 		}
10525 
10526 		if ((x_arp_ioctl) && (ipsqill != NULL))
10527 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10528 
10529 		if (ire != NULL) {
10530 			/*
10531 			 * Since the ire obtained from cachetable is used for
10532 			 * mac addr copying below, treat an incomplete ire as if
10533 			 * as if we never found it.
10534 			 */
10535 			if (ire->ire_nce != NULL &&
10536 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10537 				ire_refrele(ire);
10538 				ire = NULL;
10539 				ipsqill = NULL;
10540 				goto errack;
10541 			}
10542 			*flagsp = ATF_INUSE;
10543 			llmp = (ire->ire_nce != NULL ?
10544 			    ire->ire_nce->nce_res_mp : NULL);
10545 			if (llmp != NULL && ipsqill != NULL) {
10546 				uchar_t *macaddr;
10547 
10548 				addr_len = ipsqill->ill_phys_addr_length;
10549 				if (x_arp_ioctl && ((addr_len +
10550 				    ipsqill->ill_name_length) >
10551 				    sizeof (xar->xarp_ha.sdl_data))) {
10552 					ire_refrele(ire);
10553 					freemsg(mp);
10554 					ip_ioctl_finish(q, orig_ioc_mp,
10555 					    EINVAL, NO_COPYOUT, NULL);
10556 					return;
10557 				}
10558 				*flagsp |= ATF_COM;
10559 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10560 				if (ipsqill->ill_sap_length < 0)
10561 					macaddr = llmp->b_rptr +
10562 					    dlup->dl_dest_addr_offset;
10563 				else
10564 					macaddr = llmp->b_rptr +
10565 					    dlup->dl_dest_addr_offset +
10566 					    ipsqill->ill_sap_length;
10567 				/*
10568 				 * For SIOCGARP, MAC address length
10569 				 * validation has already been done
10570 				 * before the ioctl was issued to ARP to
10571 				 * allow it to progress only on 6 byte
10572 				 * addressable (ethernet like) media. Thus
10573 				 * the mac address copying can not overwrite
10574 				 * the sa_data area below.
10575 				 */
10576 				bcopy(macaddr, storage, addr_len);
10577 			}
10578 			/* Ditch the internal IOCTL. */
10579 			freemsg(mp);
10580 			ire_refrele(ire);
10581 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10582 			return;
10583 		}
10584 	}
10585 
10586 	/*
10587 	 * Delete the coresponding IRE_CACHE if any.
10588 	 * Reset the error if there was one (in case there was no entry
10589 	 * in arp.)
10590 	 */
10591 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10592 		ipif_t *ipintf = NULL;
10593 
10594 		if (ifx_arp_ioctl) {
10595 			/*
10596 			 * There's no need to lookup the ill, since
10597 			 * we've already done that when we started
10598 			 * processing the ioctl and sent the message
10599 			 * to ARP on that ill.  So use the ill that
10600 			 * is stored in q->q_ptr.
10601 			 */
10602 			ipintf = ill->ill_ipif;
10603 		}
10604 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10605 			/*
10606 			 * The address in "addr" may be an entry for a
10607 			 * router. If that's true, then any off-net
10608 			 * IRE_CACHE entries that go through the router
10609 			 * with address "addr" must be clobbered. Use
10610 			 * ire_walk to achieve this goal.
10611 			 */
10612 			if (ifx_arp_ioctl)
10613 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10614 				    ire_delete_cache_gw, (char *)&addr, ill);
10615 			else
10616 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10617 				    ALL_ZONES, ipst);
10618 			iocp->ioc_error = 0;
10619 		}
10620 	}
10621 errack:
10622 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10623 		err = iocp->ioc_error;
10624 		freemsg(mp);
10625 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10626 		return;
10627 	}
10628 
10629 	/*
10630 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10631 	 * the area_t into the struct {x}arpreq.
10632 	 */
10633 	if (x_arp_ioctl) {
10634 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10635 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10636 		    sizeof (xar->xarp_ha.sdl_data)) {
10637 			freemsg(mp);
10638 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10639 			    NULL);
10640 			return;
10641 		}
10642 	}
10643 	*flagsp = ATF_INUSE;
10644 	if (area->area_flags & ACE_F_PERMANENT)
10645 		*flagsp |= ATF_PERM;
10646 	if (area->area_flags & ACE_F_PUBLISH)
10647 		*flagsp |= ATF_PUBL;
10648 	if (area->area_flags & ACE_F_AUTHORITY)
10649 		*flagsp |= ATF_AUTHORITY;
10650 	if (area->area_hw_addr_length != 0) {
10651 		*flagsp |= ATF_COM;
10652 		/*
10653 		 * For SIOCGARP, MAC address length validation has
10654 		 * already been done before the ioctl was issued to ARP
10655 		 * to allow it to progress only on 6 byte addressable
10656 		 * (ethernet like) media. Thus the mac address copying
10657 		 * can not overwrite the sa_data area below.
10658 		 */
10659 		bcopy((char *)area + area->area_hw_addr_offset,
10660 		    storage, area->area_hw_addr_length);
10661 	}
10662 
10663 	/* Ditch the internal IOCTL. */
10664 	freemsg(mp);
10665 	/* Complete the original. */
10666 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10667 }
10668 
10669 /*
10670  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10671  * interface) create the next available logical interface for this
10672  * physical interface.
10673  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10674  * ipif with the specified name.
10675  *
10676  * If the address family is not AF_UNSPEC then set the address as well.
10677  *
10678  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10679  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10680  *
10681  * Executed as a writer on the ill or ill group.
10682  * So no lock is needed to traverse the ipif chain, or examine the
10683  * phyint flags.
10684  */
10685 /* ARGSUSED */
10686 int
10687 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10688     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10689 {
10690 	mblk_t	*mp1;
10691 	struct lifreq *lifr;
10692 	boolean_t	isv6;
10693 	boolean_t	exists;
10694 	char 	*name;
10695 	char	*endp;
10696 	char	*cp;
10697 	int	namelen;
10698 	ipif_t	*ipif;
10699 	long	id;
10700 	ipsq_t	*ipsq;
10701 	ill_t	*ill;
10702 	sin_t	*sin;
10703 	int	err = 0;
10704 	boolean_t found_sep = B_FALSE;
10705 	conn_t	*connp;
10706 	zoneid_t zoneid;
10707 	int	orig_ifindex = 0;
10708 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10709 
10710 	ASSERT(q->q_next == NULL);
10711 	ip1dbg(("ip_sioctl_addif\n"));
10712 	/* Existence of mp1 has been checked in ip_wput_nondata */
10713 	mp1 = mp->b_cont->b_cont;
10714 	/*
10715 	 * Null terminate the string to protect against buffer
10716 	 * overrun. String was generated by user code and may not
10717 	 * be trusted.
10718 	 */
10719 	lifr = (struct lifreq *)mp1->b_rptr;
10720 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10721 	name = lifr->lifr_name;
10722 	ASSERT(CONN_Q(q));
10723 	connp = Q_TO_CONN(q);
10724 	isv6 = connp->conn_af_isv6;
10725 	zoneid = connp->conn_zoneid;
10726 	namelen = mi_strlen(name);
10727 	if (namelen == 0)
10728 		return (EINVAL);
10729 
10730 	exists = B_FALSE;
10731 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10732 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10733 		/*
10734 		 * Allow creating lo0 using SIOCLIFADDIF.
10735 		 * can't be any other writer thread. So can pass null below
10736 		 * for the last 4 args to ipif_lookup_name.
10737 		 */
10738 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10739 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10740 		/* Prevent any further action */
10741 		if (ipif == NULL) {
10742 			return (ENOBUFS);
10743 		} else if (!exists) {
10744 			/* We created the ipif now and as writer */
10745 			ipif_refrele(ipif);
10746 			return (0);
10747 		} else {
10748 			ill = ipif->ipif_ill;
10749 			ill_refhold(ill);
10750 			ipif_refrele(ipif);
10751 		}
10752 	} else {
10753 		/* Look for a colon in the name. */
10754 		endp = &name[namelen];
10755 		for (cp = endp; --cp > name; ) {
10756 			if (*cp == IPIF_SEPARATOR_CHAR) {
10757 				found_sep = B_TRUE;
10758 				/*
10759 				 * Reject any non-decimal aliases for plumbing
10760 				 * of logical interfaces. Aliases with leading
10761 				 * zeroes are also rejected as they introduce
10762 				 * ambiguity in the naming of the interfaces.
10763 				 * Comparing with "0" takes care of all such
10764 				 * cases.
10765 				 */
10766 				if ((strncmp("0", cp+1, 1)) == 0)
10767 					return (EINVAL);
10768 
10769 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10770 				    id <= 0 || *endp != '\0') {
10771 					return (EINVAL);
10772 				}
10773 				*cp = '\0';
10774 				break;
10775 			}
10776 		}
10777 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10778 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10779 		if (found_sep)
10780 			*cp = IPIF_SEPARATOR_CHAR;
10781 		if (ill == NULL)
10782 			return (err);
10783 	}
10784 
10785 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10786 	    B_TRUE);
10787 
10788 	/*
10789 	 * Release the refhold due to the lookup, now that we are excl
10790 	 * or we are just returning
10791 	 */
10792 	ill_refrele(ill);
10793 
10794 	if (ipsq == NULL)
10795 		return (EINPROGRESS);
10796 
10797 	/*
10798 	 * If the interface is failed, inactive or offlined, look for a working
10799 	 * interface in the ill group and create the ipif there. If we can't
10800 	 * find a good interface, create the ipif anyway so that in.mpathd can
10801 	 * move it to the first repaired interface.
10802 	 */
10803 	if ((ill->ill_phyint->phyint_flags &
10804 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10805 	    ill->ill_phyint->phyint_groupname_len != 0) {
10806 		phyint_t *phyi;
10807 		char *groupname = ill->ill_phyint->phyint_groupname;
10808 
10809 		/*
10810 		 * We're looking for a working interface, but it doesn't matter
10811 		 * if it's up or down; so instead of following the group lists,
10812 		 * we look at each physical interface and compare the groupname.
10813 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10814 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10815 		 * Otherwise we create the ipif on the failed interface.
10816 		 */
10817 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10818 		phyi = avl_first(&ipst->ips_phyint_g_list->
10819 		    phyint_list_avl_by_index);
10820 		for (; phyi != NULL;
10821 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10822 		    phyint_list_avl_by_index,
10823 		    phyi, AVL_AFTER)) {
10824 			if (phyi->phyint_groupname_len == 0)
10825 				continue;
10826 			ASSERT(phyi->phyint_groupname != NULL);
10827 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10828 			    !(phyi->phyint_flags &
10829 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10830 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10831 			    (phyi->phyint_illv4 != NULL))) {
10832 				break;
10833 			}
10834 		}
10835 		rw_exit(&ipst->ips_ill_g_lock);
10836 
10837 		if (phyi != NULL) {
10838 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10839 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10840 			    phyi->phyint_illv4);
10841 		}
10842 	}
10843 
10844 	/*
10845 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10846 	 * before or after us.
10847 	 */
10848 	ASSERT(IAM_WRITER_ILL(ill));
10849 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10850 
10851 	if (found_sep && orig_ifindex == 0) {
10852 		/* Now see if there is an IPIF with this unit number. */
10853 		for (ipif = ill->ill_ipif; ipif != NULL;
10854 		    ipif = ipif->ipif_next) {
10855 			if (ipif->ipif_id == id) {
10856 				err = EEXIST;
10857 				goto done;
10858 			}
10859 		}
10860 	}
10861 
10862 	/*
10863 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10864 	 * of lo0. We never come here when we plumb lo0:0. It
10865 	 * happens in ipif_lookup_on_name.
10866 	 * The specified unit number is ignored when we create the ipif on a
10867 	 * different interface. However, we save it in ipif_orig_ipifid below so
10868 	 * that the ipif fails back to the right position.
10869 	 */
10870 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10871 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10872 		err = ENOBUFS;
10873 		goto done;
10874 	}
10875 
10876 	/* Return created name with ioctl */
10877 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10878 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10879 	ip1dbg(("created %s\n", lifr->lifr_name));
10880 
10881 	/* Set address */
10882 	sin = (sin_t *)&lifr->lifr_addr;
10883 	if (sin->sin_family != AF_UNSPEC) {
10884 		err = ip_sioctl_addr(ipif, sin, q, mp,
10885 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10886 	}
10887 
10888 	/* Set ifindex and unit number for failback */
10889 	if (err == 0 && orig_ifindex != 0) {
10890 		ipif->ipif_orig_ifindex = orig_ifindex;
10891 		if (found_sep) {
10892 			ipif->ipif_orig_ipifid = id;
10893 		}
10894 	}
10895 
10896 done:
10897 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10898 	return (err);
10899 }
10900 
10901 /*
10902  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10903  * interface) delete it based on the IP address (on this physical interface).
10904  * Otherwise delete it based on the ipif_id.
10905  * Also, special handling to allow a removeif of lo0.
10906  */
10907 /* ARGSUSED */
10908 int
10909 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10910     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10911 {
10912 	conn_t		*connp;
10913 	ill_t		*ill = ipif->ipif_ill;
10914 	boolean_t	 success;
10915 	ip_stack_t	*ipst;
10916 
10917 	ipst = CONNQ_TO_IPST(q);
10918 
10919 	ASSERT(q->q_next == NULL);
10920 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10921 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10922 	ASSERT(IAM_WRITER_IPIF(ipif));
10923 
10924 	connp = Q_TO_CONN(q);
10925 	/*
10926 	 * Special case for unplumbing lo0 (the loopback physical interface).
10927 	 * If unplumbing lo0, the incoming address structure has been
10928 	 * initialized to all zeros. When unplumbing lo0, all its logical
10929 	 * interfaces must be removed too.
10930 	 *
10931 	 * Note that this interface may be called to remove a specific
10932 	 * loopback logical interface (eg, lo0:1). But in that case
10933 	 * ipif->ipif_id != 0 so that the code path for that case is the
10934 	 * same as any other interface (meaning it skips the code directly
10935 	 * below).
10936 	 */
10937 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10938 		if (sin->sin_family == AF_UNSPEC &&
10939 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10940 			/*
10941 			 * Mark it condemned. No new ref. will be made to ill.
10942 			 */
10943 			mutex_enter(&ill->ill_lock);
10944 			ill->ill_state_flags |= ILL_CONDEMNED;
10945 			for (ipif = ill->ill_ipif; ipif != NULL;
10946 			    ipif = ipif->ipif_next) {
10947 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10948 			}
10949 			mutex_exit(&ill->ill_lock);
10950 
10951 			ipif = ill->ill_ipif;
10952 			/* unplumb the loopback interface */
10953 			ill_delete(ill);
10954 			mutex_enter(&connp->conn_lock);
10955 			mutex_enter(&ill->ill_lock);
10956 			ASSERT(ill->ill_group == NULL);
10957 
10958 			/* Are any references to this ill active */
10959 			if (ill_is_quiescent(ill)) {
10960 				mutex_exit(&ill->ill_lock);
10961 				mutex_exit(&connp->conn_lock);
10962 				ill_delete_tail(ill);
10963 				mi_free(ill);
10964 				return (0);
10965 			}
10966 			success = ipsq_pending_mp_add(connp, ipif,
10967 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10968 			mutex_exit(&connp->conn_lock);
10969 			mutex_exit(&ill->ill_lock);
10970 			if (success)
10971 				return (EINPROGRESS);
10972 			else
10973 				return (EINTR);
10974 		}
10975 	}
10976 
10977 	/*
10978 	 * We are exclusive on the ipsq, so an ill move will be serialized
10979 	 * before or after us.
10980 	 */
10981 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10982 
10983 	if (ipif->ipif_id == 0) {
10984 		/* Find based on address */
10985 		if (ipif->ipif_isv6) {
10986 			sin6_t *sin6;
10987 
10988 			if (sin->sin_family != AF_INET6)
10989 				return (EAFNOSUPPORT);
10990 
10991 			sin6 = (sin6_t *)sin;
10992 			/* We are a writer, so we should be able to lookup */
10993 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10994 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10995 			if (ipif == NULL) {
10996 				/*
10997 				 * Maybe the address in on another interface in
10998 				 * the same IPMP group? We check this below.
10999 				 */
11000 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11001 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
11002 				    ipst);
11003 			}
11004 		} else {
11005 			ipaddr_t addr;
11006 
11007 			if (sin->sin_family != AF_INET)
11008 				return (EAFNOSUPPORT);
11009 
11010 			addr = sin->sin_addr.s_addr;
11011 			/* We are a writer, so we should be able to lookup */
11012 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
11013 			    NULL, NULL, NULL, ipst);
11014 			if (ipif == NULL) {
11015 				/*
11016 				 * Maybe the address in on another interface in
11017 				 * the same IPMP group? We check this below.
11018 				 */
11019 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
11020 				    NULL, NULL, NULL, NULL, ipst);
11021 			}
11022 		}
11023 		if (ipif == NULL) {
11024 			return (EADDRNOTAVAIL);
11025 		}
11026 		/*
11027 		 * When the address to be removed is hosted on a different
11028 		 * interface, we check if the interface is in the same IPMP
11029 		 * group as the specified one; if so we proceed with the
11030 		 * removal.
11031 		 * ill->ill_group is NULL when the ill is down, so we have to
11032 		 * compare the group names instead.
11033 		 */
11034 		if (ipif->ipif_ill != ill &&
11035 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
11036 		    ill->ill_phyint->phyint_groupname_len == 0 ||
11037 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
11038 		    ill->ill_phyint->phyint_groupname) != 0)) {
11039 			ipif_refrele(ipif);
11040 			return (EADDRNOTAVAIL);
11041 		}
11042 
11043 		/* This is a writer */
11044 		ipif_refrele(ipif);
11045 	}
11046 
11047 	/*
11048 	 * Can not delete instance zero since it is tied to the ill.
11049 	 */
11050 	if (ipif->ipif_id == 0)
11051 		return (EBUSY);
11052 
11053 	mutex_enter(&ill->ill_lock);
11054 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
11055 	mutex_exit(&ill->ill_lock);
11056 
11057 	ipif_free(ipif);
11058 
11059 	mutex_enter(&connp->conn_lock);
11060 	mutex_enter(&ill->ill_lock);
11061 
11062 	/* Are any references to this ipif active */
11063 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
11064 		mutex_exit(&ill->ill_lock);
11065 		mutex_exit(&connp->conn_lock);
11066 		ipif_non_duplicate(ipif);
11067 		ipif_down_tail(ipif);
11068 		ipif_free_tail(ipif);
11069 		return (0);
11070 	}
11071 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
11072 	    IPIF_FREE);
11073 	mutex_exit(&ill->ill_lock);
11074 	mutex_exit(&connp->conn_lock);
11075 	if (success)
11076 		return (EINPROGRESS);
11077 	else
11078 		return (EINTR);
11079 }
11080 
11081 /*
11082  * Restart the removeif ioctl. The refcnt has gone down to 0.
11083  * The ipif is already condemned. So can't find it thru lookups.
11084  */
11085 /* ARGSUSED */
11086 int
11087 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11088     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11089 {
11090 	ill_t *ill;
11091 
11092 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11093 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11094 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11095 		ill = ipif->ipif_ill;
11096 		ASSERT(IAM_WRITER_ILL(ill));
11097 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
11098 		    (ill->ill_state_flags & IPIF_CONDEMNED));
11099 		ill_delete_tail(ill);
11100 		mi_free(ill);
11101 		return (0);
11102 	}
11103 
11104 	ill = ipif->ipif_ill;
11105 	ASSERT(IAM_WRITER_IPIF(ipif));
11106 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11107 
11108 	ipif_non_duplicate(ipif);
11109 	ipif_down_tail(ipif);
11110 	ipif_free_tail(ipif);
11111 
11112 	ILL_UNMARK_CHANGING(ill);
11113 	return (0);
11114 }
11115 
11116 /*
11117  * Set the local interface address.
11118  * Allow an address of all zero when the interface is down.
11119  */
11120 /* ARGSUSED */
11121 int
11122 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11123     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11124 {
11125 	int err = 0;
11126 	in6_addr_t v6addr;
11127 	boolean_t need_up = B_FALSE;
11128 
11129 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11130 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11131 
11132 	ASSERT(IAM_WRITER_IPIF(ipif));
11133 
11134 	if (ipif->ipif_isv6) {
11135 		sin6_t *sin6;
11136 		ill_t *ill;
11137 		phyint_t *phyi;
11138 
11139 		if (sin->sin_family != AF_INET6)
11140 			return (EAFNOSUPPORT);
11141 
11142 		sin6 = (sin6_t *)sin;
11143 		v6addr = sin6->sin6_addr;
11144 		ill = ipif->ipif_ill;
11145 		phyi = ill->ill_phyint;
11146 
11147 		/*
11148 		 * Enforce that true multicast interfaces have a link-local
11149 		 * address for logical unit 0.
11150 		 */
11151 		if (ipif->ipif_id == 0 &&
11152 		    (ill->ill_flags & ILLF_MULTICAST) &&
11153 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11154 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11155 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11156 			return (EADDRNOTAVAIL);
11157 		}
11158 
11159 		/*
11160 		 * up interfaces shouldn't have the unspecified address
11161 		 * unless they also have the IPIF_NOLOCAL flags set and
11162 		 * have a subnet assigned.
11163 		 */
11164 		if ((ipif->ipif_flags & IPIF_UP) &&
11165 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11166 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11167 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11168 			return (EADDRNOTAVAIL);
11169 		}
11170 
11171 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11172 			return (EADDRNOTAVAIL);
11173 	} else {
11174 		ipaddr_t addr;
11175 
11176 		if (sin->sin_family != AF_INET)
11177 			return (EAFNOSUPPORT);
11178 
11179 		addr = sin->sin_addr.s_addr;
11180 
11181 		/* Allow 0 as the local address. */
11182 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11183 			return (EADDRNOTAVAIL);
11184 
11185 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11186 	}
11187 
11188 
11189 	/*
11190 	 * Even if there is no change we redo things just to rerun
11191 	 * ipif_set_default.
11192 	 */
11193 	if (ipif->ipif_flags & IPIF_UP) {
11194 		/*
11195 		 * Setting a new local address, make sure
11196 		 * we have net and subnet bcast ire's for
11197 		 * the old address if we need them.
11198 		 */
11199 		if (!ipif->ipif_isv6)
11200 			ipif_check_bcast_ires(ipif);
11201 		/*
11202 		 * If the interface is already marked up,
11203 		 * we call ipif_down which will take care
11204 		 * of ditching any IREs that have been set
11205 		 * up based on the old interface address.
11206 		 */
11207 		err = ipif_logical_down(ipif, q, mp);
11208 		if (err == EINPROGRESS)
11209 			return (err);
11210 		ipif_down_tail(ipif);
11211 		need_up = 1;
11212 	}
11213 
11214 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11215 	return (err);
11216 }
11217 
11218 int
11219 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11220     boolean_t need_up)
11221 {
11222 	in6_addr_t v6addr;
11223 	in6_addr_t ov6addr;
11224 	ipaddr_t addr;
11225 	sin6_t	*sin6;
11226 	int	sinlen;
11227 	int	err = 0;
11228 	ill_t	*ill = ipif->ipif_ill;
11229 	boolean_t need_dl_down;
11230 	boolean_t need_arp_down;
11231 	struct iocblk *iocp;
11232 
11233 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11234 
11235 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11236 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11237 	ASSERT(IAM_WRITER_IPIF(ipif));
11238 
11239 	/* Must cancel any pending timer before taking the ill_lock */
11240 	if (ipif->ipif_recovery_id != 0)
11241 		(void) untimeout(ipif->ipif_recovery_id);
11242 	ipif->ipif_recovery_id = 0;
11243 
11244 	if (ipif->ipif_isv6) {
11245 		sin6 = (sin6_t *)sin;
11246 		v6addr = sin6->sin6_addr;
11247 		sinlen = sizeof (struct sockaddr_in6);
11248 	} else {
11249 		addr = sin->sin_addr.s_addr;
11250 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11251 		sinlen = sizeof (struct sockaddr_in);
11252 	}
11253 	mutex_enter(&ill->ill_lock);
11254 	ov6addr = ipif->ipif_v6lcl_addr;
11255 	ipif->ipif_v6lcl_addr = v6addr;
11256 	sctp_update_ipif_addr(ipif, ov6addr);
11257 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11258 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11259 	} else {
11260 		ipif->ipif_v6src_addr = v6addr;
11261 	}
11262 	ipif->ipif_addr_ready = 0;
11263 
11264 	/*
11265 	 * If the interface was previously marked as a duplicate, then since
11266 	 * we've now got a "new" address, it should no longer be considered a
11267 	 * duplicate -- even if the "new" address is the same as the old one.
11268 	 * Note that if all ipifs are down, we may have a pending ARP down
11269 	 * event to handle.  This is because we want to recover from duplicates
11270 	 * and thus delay tearing down ARP until the duplicates have been
11271 	 * removed or disabled.
11272 	 */
11273 	need_dl_down = need_arp_down = B_FALSE;
11274 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11275 		need_arp_down = !need_up;
11276 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11277 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11278 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11279 			need_dl_down = B_TRUE;
11280 		}
11281 	}
11282 
11283 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11284 	    !ill->ill_is_6to4tun) {
11285 		queue_t *wqp = ill->ill_wq;
11286 
11287 		/*
11288 		 * The local address of this interface is a 6to4 address,
11289 		 * check if this interface is in fact a 6to4 tunnel or just
11290 		 * an interface configured with a 6to4 address.  We are only
11291 		 * interested in the former.
11292 		 */
11293 		if (wqp != NULL) {
11294 			while ((wqp->q_next != NULL) &&
11295 			    (wqp->q_next->q_qinfo != NULL) &&
11296 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11297 
11298 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11299 				    == TUN6TO4_MODID) {
11300 					/* set for use in IP */
11301 					ill->ill_is_6to4tun = 1;
11302 					break;
11303 				}
11304 				wqp = wqp->q_next;
11305 			}
11306 		}
11307 	}
11308 
11309 	ipif_set_default(ipif);
11310 
11311 	/*
11312 	 * When publishing an interface address change event, we only notify
11313 	 * the event listeners of the new address.  It is assumed that if they
11314 	 * actively care about the addresses assigned that they will have
11315 	 * already discovered the previous address assigned (if there was one.)
11316 	 *
11317 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11318 	 */
11319 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11320 		hook_nic_event_t *info;
11321 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
11322 			ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d "
11323 			    "attached for %s\n", info->hne_event,
11324 			    ill->ill_name));
11325 			if (info->hne_data != NULL)
11326 				kmem_free(info->hne_data, info->hne_datalen);
11327 			kmem_free(info, sizeof (hook_nic_event_t));
11328 		}
11329 
11330 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
11331 		if (info != NULL) {
11332 			ip_stack_t	*ipst = ill->ill_ipst;
11333 
11334 			info->hne_nic =
11335 			    ipif->ipif_ill->ill_phyint->phyint_hook_ifindex;
11336 			info->hne_lif = MAP_IPIF_ID(ipif->ipif_id);
11337 			info->hne_event = NE_ADDRESS_CHANGE;
11338 			info->hne_family = ipif->ipif_isv6 ?
11339 			    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
11340 			info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP);
11341 			if (info->hne_data != NULL) {
11342 				info->hne_datalen = sinlen;
11343 				bcopy(sin, info->hne_data, sinlen);
11344 			} else {
11345 				ip2dbg(("ip_sioctl_addr_tail: could not attach "
11346 				    "address information for ADDRESS_CHANGE nic"
11347 				    " event of %s (ENOMEM)\n",
11348 				    ipif->ipif_ill->ill_name));
11349 				kmem_free(info, sizeof (hook_nic_event_t));
11350 			}
11351 		} else
11352 			ip2dbg(("ip_sioctl_addr_tail: could not attach "
11353 			    "ADDRESS_CHANGE nic event information for %s "
11354 			    "(ENOMEM)\n", ipif->ipif_ill->ill_name));
11355 
11356 		ipif->ipif_ill->ill_nic_event_info = info;
11357 	}
11358 
11359 	mutex_exit(&ill->ill_lock);
11360 
11361 	if (need_up) {
11362 		/*
11363 		 * Now bring the interface back up.  If this
11364 		 * is the only IPIF for the ILL, ipif_up
11365 		 * will have to re-bind to the device, so
11366 		 * we may get back EINPROGRESS, in which
11367 		 * case, this IOCTL will get completed in
11368 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11369 		 */
11370 		err = ipif_up(ipif, q, mp);
11371 	}
11372 
11373 	if (need_dl_down)
11374 		ill_dl_down(ill);
11375 	if (need_arp_down)
11376 		ipif_arp_down(ipif);
11377 
11378 	return (err);
11379 }
11380 
11381 
11382 /*
11383  * Restart entry point to restart the address set operation after the
11384  * refcounts have dropped to zero.
11385  */
11386 /* ARGSUSED */
11387 int
11388 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11389     ip_ioctl_cmd_t *ipip, void *ifreq)
11390 {
11391 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11392 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11393 	ASSERT(IAM_WRITER_IPIF(ipif));
11394 	ipif_down_tail(ipif);
11395 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11396 }
11397 
11398 /* ARGSUSED */
11399 int
11400 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11401     ip_ioctl_cmd_t *ipip, void *if_req)
11402 {
11403 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11404 	struct lifreq *lifr = (struct lifreq *)if_req;
11405 
11406 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11407 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11408 	/*
11409 	 * The net mask and address can't change since we have a
11410 	 * reference to the ipif. So no lock is necessary.
11411 	 */
11412 	if (ipif->ipif_isv6) {
11413 		*sin6 = sin6_null;
11414 		sin6->sin6_family = AF_INET6;
11415 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11416 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11417 		lifr->lifr_addrlen =
11418 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11419 	} else {
11420 		*sin = sin_null;
11421 		sin->sin_family = AF_INET;
11422 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11423 		if (ipip->ipi_cmd_type == LIF_CMD) {
11424 			lifr->lifr_addrlen =
11425 			    ip_mask_to_plen(ipif->ipif_net_mask);
11426 		}
11427 	}
11428 	return (0);
11429 }
11430 
11431 /*
11432  * Set the destination address for a pt-pt interface.
11433  */
11434 /* ARGSUSED */
11435 int
11436 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11437     ip_ioctl_cmd_t *ipip, void *if_req)
11438 {
11439 	int err = 0;
11440 	in6_addr_t v6addr;
11441 	boolean_t need_up = B_FALSE;
11442 
11443 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11444 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11445 	ASSERT(IAM_WRITER_IPIF(ipif));
11446 
11447 	if (ipif->ipif_isv6) {
11448 		sin6_t *sin6;
11449 
11450 		if (sin->sin_family != AF_INET6)
11451 			return (EAFNOSUPPORT);
11452 
11453 		sin6 = (sin6_t *)sin;
11454 		v6addr = sin6->sin6_addr;
11455 
11456 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11457 			return (EADDRNOTAVAIL);
11458 	} else {
11459 		ipaddr_t addr;
11460 
11461 		if (sin->sin_family != AF_INET)
11462 			return (EAFNOSUPPORT);
11463 
11464 		addr = sin->sin_addr.s_addr;
11465 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11466 			return (EADDRNOTAVAIL);
11467 
11468 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11469 	}
11470 
11471 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11472 		return (0);	/* No change */
11473 
11474 	if (ipif->ipif_flags & IPIF_UP) {
11475 		/*
11476 		 * If the interface is already marked up,
11477 		 * we call ipif_down which will take care
11478 		 * of ditching any IREs that have been set
11479 		 * up based on the old pp dst address.
11480 		 */
11481 		err = ipif_logical_down(ipif, q, mp);
11482 		if (err == EINPROGRESS)
11483 			return (err);
11484 		ipif_down_tail(ipif);
11485 		need_up = B_TRUE;
11486 	}
11487 	/*
11488 	 * could return EINPROGRESS. If so ioctl will complete in
11489 	 * ip_rput_dlpi_writer
11490 	 */
11491 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11492 	return (err);
11493 }
11494 
11495 static int
11496 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11497     boolean_t need_up)
11498 {
11499 	in6_addr_t v6addr;
11500 	ill_t	*ill = ipif->ipif_ill;
11501 	int	err = 0;
11502 	boolean_t need_dl_down;
11503 	boolean_t need_arp_down;
11504 
11505 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11506 	    ipif->ipif_id, (void *)ipif));
11507 
11508 	/* Must cancel any pending timer before taking the ill_lock */
11509 	if (ipif->ipif_recovery_id != 0)
11510 		(void) untimeout(ipif->ipif_recovery_id);
11511 	ipif->ipif_recovery_id = 0;
11512 
11513 	if (ipif->ipif_isv6) {
11514 		sin6_t *sin6;
11515 
11516 		sin6 = (sin6_t *)sin;
11517 		v6addr = sin6->sin6_addr;
11518 	} else {
11519 		ipaddr_t addr;
11520 
11521 		addr = sin->sin_addr.s_addr;
11522 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11523 	}
11524 	mutex_enter(&ill->ill_lock);
11525 	/* Set point to point destination address. */
11526 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11527 		/*
11528 		 * Allow this as a means of creating logical
11529 		 * pt-pt interfaces on top of e.g. an Ethernet.
11530 		 * XXX Undocumented HACK for testing.
11531 		 * pt-pt interfaces are created with NUD disabled.
11532 		 */
11533 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11534 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11535 		if (ipif->ipif_isv6)
11536 			ill->ill_flags |= ILLF_NONUD;
11537 	}
11538 
11539 	/*
11540 	 * If the interface was previously marked as a duplicate, then since
11541 	 * we've now got a "new" address, it should no longer be considered a
11542 	 * duplicate -- even if the "new" address is the same as the old one.
11543 	 * Note that if all ipifs are down, we may have a pending ARP down
11544 	 * event to handle.
11545 	 */
11546 	need_dl_down = need_arp_down = B_FALSE;
11547 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11548 		need_arp_down = !need_up;
11549 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11550 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11551 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11552 			need_dl_down = B_TRUE;
11553 		}
11554 	}
11555 
11556 	/* Set the new address. */
11557 	ipif->ipif_v6pp_dst_addr = v6addr;
11558 	/* Make sure subnet tracks pp_dst */
11559 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11560 	mutex_exit(&ill->ill_lock);
11561 
11562 	if (need_up) {
11563 		/*
11564 		 * Now bring the interface back up.  If this
11565 		 * is the only IPIF for the ILL, ipif_up
11566 		 * will have to re-bind to the device, so
11567 		 * we may get back EINPROGRESS, in which
11568 		 * case, this IOCTL will get completed in
11569 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11570 		 */
11571 		err = ipif_up(ipif, q, mp);
11572 	}
11573 
11574 	if (need_dl_down)
11575 		ill_dl_down(ill);
11576 
11577 	if (need_arp_down)
11578 		ipif_arp_down(ipif);
11579 	return (err);
11580 }
11581 
11582 /*
11583  * Restart entry point to restart the dstaddress set operation after the
11584  * refcounts have dropped to zero.
11585  */
11586 /* ARGSUSED */
11587 int
11588 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11589     ip_ioctl_cmd_t *ipip, void *ifreq)
11590 {
11591 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11592 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11593 	ipif_down_tail(ipif);
11594 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11595 }
11596 
11597 /* ARGSUSED */
11598 int
11599 ip_sioctl_get_dstaddr(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 
11604 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11605 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11606 	/*
11607 	 * Get point to point destination address. The addresses can't
11608 	 * change since we hold a reference to the ipif.
11609 	 */
11610 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11611 		return (EADDRNOTAVAIL);
11612 
11613 	if (ipif->ipif_isv6) {
11614 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11615 		*sin6 = sin6_null;
11616 		sin6->sin6_family = AF_INET6;
11617 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11618 	} else {
11619 		*sin = sin_null;
11620 		sin->sin_family = AF_INET;
11621 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11622 	}
11623 	return (0);
11624 }
11625 
11626 /*
11627  * part of ipmp, make this func return the active/inactive state and
11628  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11629  */
11630 /*
11631  * This function either sets or clears the IFF_INACTIVE flag.
11632  *
11633  * As long as there are some addresses or multicast memberships on the
11634  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11635  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11636  * will be used for outbound packets.
11637  *
11638  * Caller needs to verify the validity of setting IFF_INACTIVE.
11639  */
11640 static void
11641 phyint_inactive(phyint_t *phyi)
11642 {
11643 	ill_t *ill_v4;
11644 	ill_t *ill_v6;
11645 	ipif_t *ipif;
11646 	ilm_t *ilm;
11647 
11648 	ill_v4 = phyi->phyint_illv4;
11649 	ill_v6 = phyi->phyint_illv6;
11650 
11651 	/*
11652 	 * No need for a lock while traversing the list since iam
11653 	 * a writer
11654 	 */
11655 	if (ill_v4 != NULL) {
11656 		ASSERT(IAM_WRITER_ILL(ill_v4));
11657 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11658 		    ipif = ipif->ipif_next) {
11659 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11660 				mutex_enter(&phyi->phyint_lock);
11661 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11662 				mutex_exit(&phyi->phyint_lock);
11663 				return;
11664 			}
11665 		}
11666 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11667 		    ilm = ilm->ilm_next) {
11668 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11669 				mutex_enter(&phyi->phyint_lock);
11670 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11671 				mutex_exit(&phyi->phyint_lock);
11672 				return;
11673 			}
11674 		}
11675 	}
11676 	if (ill_v6 != NULL) {
11677 		ill_v6 = phyi->phyint_illv6;
11678 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11679 		    ipif = ipif->ipif_next) {
11680 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11681 				mutex_enter(&phyi->phyint_lock);
11682 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11683 				mutex_exit(&phyi->phyint_lock);
11684 				return;
11685 			}
11686 		}
11687 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11688 		    ilm = ilm->ilm_next) {
11689 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11690 				mutex_enter(&phyi->phyint_lock);
11691 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11692 				mutex_exit(&phyi->phyint_lock);
11693 				return;
11694 			}
11695 		}
11696 	}
11697 	mutex_enter(&phyi->phyint_lock);
11698 	phyi->phyint_flags |= PHYI_INACTIVE;
11699 	mutex_exit(&phyi->phyint_lock);
11700 }
11701 
11702 /*
11703  * This function is called only when the phyint flags change. Currently
11704  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11705  * that we can select a good ill.
11706  */
11707 static void
11708 ip_redo_nomination(phyint_t *phyi)
11709 {
11710 	ill_t *ill_v4;
11711 
11712 	ill_v4 = phyi->phyint_illv4;
11713 
11714 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11715 		ASSERT(IAM_WRITER_ILL(ill_v4));
11716 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11717 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11718 	}
11719 }
11720 
11721 /*
11722  * Heuristic to check if ill is INACTIVE.
11723  * Checks if ill has an ipif with an usable ip address.
11724  *
11725  * Return values:
11726  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11727  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11728  */
11729 static boolean_t
11730 ill_is_inactive(ill_t *ill)
11731 {
11732 	ipif_t *ipif;
11733 
11734 	/* Check whether it is in an IPMP group */
11735 	if (ill->ill_phyint->phyint_groupname == NULL)
11736 		return (B_FALSE);
11737 
11738 	if (ill->ill_ipif_up_count == 0)
11739 		return (B_TRUE);
11740 
11741 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11742 		uint64_t flags = ipif->ipif_flags;
11743 
11744 		/*
11745 		 * This ipif is usable if it is IPIF_UP and not a
11746 		 * dedicated test address.  A dedicated test address
11747 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11748 		 * (note in particular that V6 test addresses are
11749 		 * link-local data addresses and thus are marked
11750 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11751 		 */
11752 		if ((flags & IPIF_UP) &&
11753 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11754 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11755 			return (B_FALSE);
11756 	}
11757 	return (B_TRUE);
11758 }
11759 
11760 /*
11761  * Set interface flags.
11762  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11763  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11764  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11765  *
11766  * NOTE : We really don't enforce that ipif_id zero should be used
11767  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11768  *	  is because applications generally does SICGLIFFLAGS and
11769  *	  ORs in the new flags (that affects the logical) and does a
11770  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11771  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11772  *	  flags that will be turned on is correct with respect to
11773  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11774  */
11775 /* ARGSUSED */
11776 int
11777 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11778     ip_ioctl_cmd_t *ipip, void *if_req)
11779 {
11780 	uint64_t turn_on;
11781 	uint64_t turn_off;
11782 	int	err;
11783 	boolean_t need_up = B_FALSE;
11784 	phyint_t *phyi;
11785 	ill_t *ill;
11786 	uint64_t intf_flags;
11787 	boolean_t phyint_flags_modified = B_FALSE;
11788 	uint64_t flags;
11789 	struct ifreq *ifr;
11790 	struct lifreq *lifr;
11791 	boolean_t set_linklocal = B_FALSE;
11792 	boolean_t zero_source = B_FALSE;
11793 	ip_stack_t *ipst;
11794 
11795 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11796 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11797 
11798 	ASSERT(IAM_WRITER_IPIF(ipif));
11799 
11800 	ill = ipif->ipif_ill;
11801 	phyi = ill->ill_phyint;
11802 	ipst = ill->ill_ipst;
11803 
11804 	if (ipip->ipi_cmd_type == IF_CMD) {
11805 		ifr = (struct ifreq *)if_req;
11806 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11807 	} else {
11808 		lifr = (struct lifreq *)if_req;
11809 		flags = lifr->lifr_flags;
11810 	}
11811 
11812 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11813 
11814 	/*
11815 	 * Has the flags been set correctly till now ?
11816 	 */
11817 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11818 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11819 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11820 	/*
11821 	 * Compare the new flags to the old, and partition
11822 	 * into those coming on and those going off.
11823 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11824 	 */
11825 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11826 		flags |= intf_flags & ~0xFFFF;
11827 
11828 	/*
11829 	 * First check which bits will change and then which will
11830 	 * go on and off
11831 	 */
11832 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11833 	if (!turn_on)
11834 		return (0);	/* No change */
11835 
11836 	turn_off = intf_flags & turn_on;
11837 	turn_on ^= turn_off;
11838 	err = 0;
11839 
11840 	/*
11841 	 * Don't allow any bits belonging to the logical interface
11842 	 * to be set or cleared on the replacement ipif that was
11843 	 * created temporarily during a MOVE.
11844 	 */
11845 	if (ipif->ipif_replace_zero &&
11846 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11847 		return (EINVAL);
11848 	}
11849 
11850 	/*
11851 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11852 	 * IPv6 interfaces.
11853 	 */
11854 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11855 		return (EINVAL);
11856 
11857 	/*
11858 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11859 	 */
11860 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11861 		return (EINVAL);
11862 
11863 	/*
11864 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11865 	 * interfaces.  It makes no sense in that context.
11866 	 */
11867 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11868 		return (EINVAL);
11869 
11870 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11871 		zero_source = B_TRUE;
11872 
11873 	/*
11874 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11875 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11876 	 * If the link local address isn't set, and can be set, it will get
11877 	 * set later on in this function.
11878 	 */
11879 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11880 	    (flags & IFF_UP) && !zero_source &&
11881 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11882 		if (ipif_cant_setlinklocal(ipif))
11883 			return (EINVAL);
11884 		set_linklocal = B_TRUE;
11885 	}
11886 
11887 	/*
11888 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11889 	 * same time. No need to grab ill_g_usesrc_lock here, see
11890 	 * synchronization notes in ip.c
11891 	 */
11892 	if (turn_on & PHYI_STANDBY &&
11893 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11894 		return (EINVAL);
11895 	}
11896 
11897 	/*
11898 	 * If we modify physical interface flags, we'll potentially need to
11899 	 * send up two routing socket messages for the changes (one for the
11900 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11901 	 */
11902 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11903 		phyint_flags_modified = B_TRUE;
11904 
11905 	/*
11906 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11907 	 * we need to flush the IRE_CACHES belonging to this ill.
11908 	 * We handle this case here without doing the DOWN/UP dance
11909 	 * like it is done for other flags. If some other flags are
11910 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11911 	 * below will handle it by bringing it down and then
11912 	 * bringing it UP.
11913 	 */
11914 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11915 		ill_t *ill_v4, *ill_v6;
11916 
11917 		ill_v4 = phyi->phyint_illv4;
11918 		ill_v6 = phyi->phyint_illv6;
11919 
11920 		/*
11921 		 * First set the INACTIVE flag if needed. Then delete the ires.
11922 		 * ire_add will atomically prevent creating new IRE_CACHEs
11923 		 * unless hidden flag is set.
11924 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11925 		 */
11926 		if ((turn_on & PHYI_FAILED) &&
11927 		    ((intf_flags & PHYI_STANDBY) ||
11928 		    !ipst->ips_ipmp_enable_failback)) {
11929 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11930 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11931 		}
11932 		if ((turn_off & PHYI_FAILED) &&
11933 		    ((intf_flags & PHYI_STANDBY) ||
11934 		    (!ipst->ips_ipmp_enable_failback &&
11935 		    ill_is_inactive(ill)))) {
11936 			phyint_inactive(phyi);
11937 		}
11938 
11939 		if (turn_on & PHYI_STANDBY) {
11940 			/*
11941 			 * We implicitly set INACTIVE only when STANDBY is set.
11942 			 * INACTIVE is also set on non-STANDBY phyint when user
11943 			 * disables FAILBACK using configuration file.
11944 			 * Do not allow STANDBY to be set on such INACTIVE
11945 			 * phyint
11946 			 */
11947 			if (phyi->phyint_flags & PHYI_INACTIVE)
11948 				return (EINVAL);
11949 			if (!(phyi->phyint_flags & PHYI_FAILED))
11950 				phyint_inactive(phyi);
11951 		}
11952 		if (turn_off & PHYI_STANDBY) {
11953 			if (ipst->ips_ipmp_enable_failback) {
11954 				/*
11955 				 * Reset PHYI_INACTIVE.
11956 				 */
11957 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11958 			} else if (ill_is_inactive(ill) &&
11959 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11960 				/*
11961 				 * Need to set INACTIVE, when user sets
11962 				 * STANDBY on a non-STANDBY phyint and
11963 				 * later resets STANDBY
11964 				 */
11965 				phyint_inactive(phyi);
11966 			}
11967 		}
11968 		/*
11969 		 * We should always send up a message so that the
11970 		 * daemons come to know of it. Note that the zeroth
11971 		 * interface can be down and the check below for IPIF_UP
11972 		 * will not make sense as we are actually setting
11973 		 * a phyint flag here. We assume that the ipif used
11974 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11975 		 * send up any message for non-zero ipifs).
11976 		 */
11977 		phyint_flags_modified = B_TRUE;
11978 
11979 		if (ill_v4 != NULL) {
11980 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11981 			    IRE_CACHE, ill_stq_cache_delete,
11982 			    (char *)ill_v4, ill_v4);
11983 			illgrp_reset_schednext(ill_v4);
11984 		}
11985 		if (ill_v6 != NULL) {
11986 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11987 			    IRE_CACHE, ill_stq_cache_delete,
11988 			    (char *)ill_v6, ill_v6);
11989 			illgrp_reset_schednext(ill_v6);
11990 		}
11991 	}
11992 
11993 	/*
11994 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11995 	 * status of the interface and, if the interface is part of an IPMP
11996 	 * group, all other interfaces that are part of the same IPMP
11997 	 * group.
11998 	 */
11999 	if ((turn_on | turn_off) & ILLF_ROUTER)
12000 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
12001 
12002 	/*
12003 	 * If the interface is not UP and we are not going to
12004 	 * bring it UP, record the flags and return. When the
12005 	 * interface comes UP later, the right actions will be
12006 	 * taken.
12007 	 */
12008 	if (!(ipif->ipif_flags & IPIF_UP) &&
12009 	    !(turn_on & IPIF_UP)) {
12010 		/* Record new flags in their respective places. */
12011 		mutex_enter(&ill->ill_lock);
12012 		mutex_enter(&ill->ill_phyint->phyint_lock);
12013 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12014 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12015 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12016 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12017 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12018 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12019 		mutex_exit(&ill->ill_lock);
12020 		mutex_exit(&ill->ill_phyint->phyint_lock);
12021 
12022 		/*
12023 		 * We do the broadcast and nomination here rather
12024 		 * than waiting for a FAILOVER/FAILBACK to happen. In
12025 		 * the case of FAILBACK from INACTIVE standby to the
12026 		 * interface that has been repaired, PHYI_FAILED has not
12027 		 * been cleared yet. If there are only two interfaces in
12028 		 * that group, all we have is a FAILED and INACTIVE
12029 		 * interface. If we do the nomination soon after a failback,
12030 		 * the broadcast nomination code would select the
12031 		 * INACTIVE interface for receiving broadcasts as FAILED is
12032 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
12033 		 * receive broadcast packets, we need to redo nomination
12034 		 * when the FAILED is cleared here. Thus, in general we
12035 		 * always do the nomination here for FAILED, STANDBY
12036 		 * and OFFLINE.
12037 		 */
12038 		if (((turn_on | turn_off) &
12039 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
12040 			ip_redo_nomination(phyi);
12041 		}
12042 		if (phyint_flags_modified) {
12043 			if (phyi->phyint_illv4 != NULL) {
12044 				ip_rts_ifmsg(phyi->phyint_illv4->
12045 				    ill_ipif);
12046 			}
12047 			if (phyi->phyint_illv6 != NULL) {
12048 				ip_rts_ifmsg(phyi->phyint_illv6->
12049 				    ill_ipif);
12050 			}
12051 		}
12052 		return (0);
12053 	} else if (set_linklocal || zero_source) {
12054 		mutex_enter(&ill->ill_lock);
12055 		if (set_linklocal)
12056 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
12057 		if (zero_source)
12058 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
12059 		mutex_exit(&ill->ill_lock);
12060 	}
12061 
12062 	/*
12063 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
12064 	 * or point-to-point interfaces with an unspecified destination. We do
12065 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
12066 	 * have a subnet assigned, which is how in.ndpd currently manages its
12067 	 * onlink prefix list when no addresses are configured with those
12068 	 * prefixes.
12069 	 */
12070 	if (ipif->ipif_isv6 &&
12071 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
12072 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
12073 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
12074 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12075 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
12076 		return (EINVAL);
12077 	}
12078 
12079 	/*
12080 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
12081 	 * from being brought up.
12082 	 */
12083 	if (!ipif->ipif_isv6 &&
12084 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12085 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
12086 		return (EINVAL);
12087 	}
12088 
12089 	/*
12090 	 * The only flag changes that we currently take specific action on
12091 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
12092 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
12093 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
12094 	 * the flags and bringing it back up again.
12095 	 */
12096 	if ((turn_on|turn_off) &
12097 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
12098 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
12099 		/*
12100 		 * Taking this ipif down, make sure we have
12101 		 * valid net and subnet bcast ire's for other
12102 		 * logical interfaces, if we need them.
12103 		 */
12104 		if (!ipif->ipif_isv6)
12105 			ipif_check_bcast_ires(ipif);
12106 
12107 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
12108 		    !(turn_off & IPIF_UP)) {
12109 			need_up = B_TRUE;
12110 			if (ipif->ipif_flags & IPIF_UP)
12111 				ill->ill_logical_down = 1;
12112 			turn_on &= ~IPIF_UP;
12113 		}
12114 		err = ipif_down(ipif, q, mp);
12115 		ip1dbg(("ipif_down returns %d err ", err));
12116 		if (err == EINPROGRESS)
12117 			return (err);
12118 		ipif_down_tail(ipif);
12119 	}
12120 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12121 }
12122 
12123 static int
12124 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12125     boolean_t need_up)
12126 {
12127 	ill_t	*ill;
12128 	phyint_t *phyi;
12129 	uint64_t turn_on;
12130 	uint64_t turn_off;
12131 	uint64_t intf_flags;
12132 	boolean_t phyint_flags_modified = B_FALSE;
12133 	int	err = 0;
12134 	boolean_t set_linklocal = B_FALSE;
12135 	boolean_t zero_source = B_FALSE;
12136 
12137 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12138 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12139 
12140 	ASSERT(IAM_WRITER_IPIF(ipif));
12141 
12142 	ill = ipif->ipif_ill;
12143 	phyi = ill->ill_phyint;
12144 
12145 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12146 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12147 
12148 	turn_off = intf_flags & turn_on;
12149 	turn_on ^= turn_off;
12150 
12151 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12152 		phyint_flags_modified = B_TRUE;
12153 
12154 	/*
12155 	 * Now we change the flags. Track current value of
12156 	 * other flags in their respective places.
12157 	 */
12158 	mutex_enter(&ill->ill_lock);
12159 	mutex_enter(&phyi->phyint_lock);
12160 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12161 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12162 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12163 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12164 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12165 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12166 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12167 		set_linklocal = B_TRUE;
12168 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12169 	}
12170 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12171 		zero_source = B_TRUE;
12172 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12173 	}
12174 	mutex_exit(&ill->ill_lock);
12175 	mutex_exit(&phyi->phyint_lock);
12176 
12177 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12178 		ip_redo_nomination(phyi);
12179 
12180 	if (set_linklocal)
12181 		(void) ipif_setlinklocal(ipif);
12182 
12183 	if (zero_source)
12184 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12185 	else
12186 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12187 
12188 	if (need_up) {
12189 		/*
12190 		 * XXX ipif_up really does not know whether a phyint flags
12191 		 * was modified or not. So, it sends up information on
12192 		 * only one routing sockets message. As we don't bring up
12193 		 * the interface and also set STANDBY/FAILED simultaneously
12194 		 * it should be okay.
12195 		 */
12196 		err = ipif_up(ipif, q, mp);
12197 	} else {
12198 		/*
12199 		 * Make sure routing socket sees all changes to the flags.
12200 		 * ipif_up_done* handles this when we use ipif_up.
12201 		 */
12202 		if (phyint_flags_modified) {
12203 			if (phyi->phyint_illv4 != NULL) {
12204 				ip_rts_ifmsg(phyi->phyint_illv4->
12205 				    ill_ipif);
12206 			}
12207 			if (phyi->phyint_illv6 != NULL) {
12208 				ip_rts_ifmsg(phyi->phyint_illv6->
12209 				    ill_ipif);
12210 			}
12211 		} else {
12212 			ip_rts_ifmsg(ipif);
12213 		}
12214 		/*
12215 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12216 		 * this in need_up case.
12217 		 */
12218 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12219 	}
12220 	return (err);
12221 }
12222 
12223 /*
12224  * Restart entry point to restart the flags restart operation after the
12225  * refcounts have dropped to zero.
12226  */
12227 /* ARGSUSED */
12228 int
12229 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12230     ip_ioctl_cmd_t *ipip, void *if_req)
12231 {
12232 	int	err;
12233 	struct ifreq *ifr = (struct ifreq *)if_req;
12234 	struct lifreq *lifr = (struct lifreq *)if_req;
12235 
12236 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12237 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12238 
12239 	ipif_down_tail(ipif);
12240 	if (ipip->ipi_cmd_type == IF_CMD) {
12241 		/*
12242 		 * Since ip_sioctl_flags expects an int and ifr_flags
12243 		 * is a short we need to cast ifr_flags into an int
12244 		 * to avoid having sign extension cause bits to get
12245 		 * set that should not be.
12246 		 */
12247 		err = ip_sioctl_flags_tail(ipif,
12248 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12249 		    q, mp, B_TRUE);
12250 	} else {
12251 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12252 		    q, mp, B_TRUE);
12253 	}
12254 	return (err);
12255 }
12256 
12257 /*
12258  * Can operate on either a module or a driver queue.
12259  */
12260 /* ARGSUSED */
12261 int
12262 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12263     ip_ioctl_cmd_t *ipip, void *if_req)
12264 {
12265 	/*
12266 	 * Has the flags been set correctly till now ?
12267 	 */
12268 	ill_t *ill = ipif->ipif_ill;
12269 	phyint_t *phyi = ill->ill_phyint;
12270 
12271 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12272 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12273 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12274 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12275 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12276 
12277 	/*
12278 	 * Need a lock since some flags can be set even when there are
12279 	 * references to the ipif.
12280 	 */
12281 	mutex_enter(&ill->ill_lock);
12282 	if (ipip->ipi_cmd_type == IF_CMD) {
12283 		struct ifreq *ifr = (struct ifreq *)if_req;
12284 
12285 		/* Get interface flags (low 16 only). */
12286 		ifr->ifr_flags = ((ipif->ipif_flags |
12287 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12288 	} else {
12289 		struct lifreq *lifr = (struct lifreq *)if_req;
12290 
12291 		/* Get interface flags. */
12292 		lifr->lifr_flags = ipif->ipif_flags |
12293 		    ill->ill_flags | phyi->phyint_flags;
12294 	}
12295 	mutex_exit(&ill->ill_lock);
12296 	return (0);
12297 }
12298 
12299 /* ARGSUSED */
12300 int
12301 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12302     ip_ioctl_cmd_t *ipip, void *if_req)
12303 {
12304 	int mtu;
12305 	int ip_min_mtu;
12306 	struct ifreq	*ifr;
12307 	struct lifreq *lifr;
12308 	ire_t	*ire;
12309 	ip_stack_t *ipst;
12310 
12311 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12312 	    ipif->ipif_id, (void *)ipif));
12313 	if (ipip->ipi_cmd_type == IF_CMD) {
12314 		ifr = (struct ifreq *)if_req;
12315 		mtu = ifr->ifr_metric;
12316 	} else {
12317 		lifr = (struct lifreq *)if_req;
12318 		mtu = lifr->lifr_mtu;
12319 	}
12320 
12321 	if (ipif->ipif_isv6)
12322 		ip_min_mtu = IPV6_MIN_MTU;
12323 	else
12324 		ip_min_mtu = IP_MIN_MTU;
12325 
12326 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12327 		return (EINVAL);
12328 
12329 	/*
12330 	 * Change the MTU size in all relevant ire's.
12331 	 * Mtu change Vs. new ire creation - protocol below.
12332 	 * First change ipif_mtu and the ire_max_frag of the
12333 	 * interface ire. Then do an ire walk and change the
12334 	 * ire_max_frag of all affected ires. During ire_add
12335 	 * under the bucket lock, set the ire_max_frag of the
12336 	 * new ire being created from the ipif/ire from which
12337 	 * it is being derived. If an mtu change happens after
12338 	 * the ire is added, the new ire will be cleaned up.
12339 	 * Conversely if the mtu change happens before the ire
12340 	 * is added, ire_add will see the new value of the mtu.
12341 	 */
12342 	ipif->ipif_mtu = mtu;
12343 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12344 
12345 	if (ipif->ipif_isv6)
12346 		ire = ipif_to_ire_v6(ipif);
12347 	else
12348 		ire = ipif_to_ire(ipif);
12349 	if (ire != NULL) {
12350 		ire->ire_max_frag = ipif->ipif_mtu;
12351 		ire_refrele(ire);
12352 	}
12353 	ipst = ipif->ipif_ill->ill_ipst;
12354 	if (ipif->ipif_flags & IPIF_UP) {
12355 		if (ipif->ipif_isv6)
12356 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12357 			    ipst);
12358 		else
12359 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12360 			    ipst);
12361 	}
12362 	/* Update the MTU in SCTP's list */
12363 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12364 	return (0);
12365 }
12366 
12367 /* Get interface MTU. */
12368 /* ARGSUSED */
12369 int
12370 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12371 	ip_ioctl_cmd_t *ipip, void *if_req)
12372 {
12373 	struct ifreq	*ifr;
12374 	struct lifreq	*lifr;
12375 
12376 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12377 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12378 	if (ipip->ipi_cmd_type == IF_CMD) {
12379 		ifr = (struct ifreq *)if_req;
12380 		ifr->ifr_metric = ipif->ipif_mtu;
12381 	} else {
12382 		lifr = (struct lifreq *)if_req;
12383 		lifr->lifr_mtu = ipif->ipif_mtu;
12384 	}
12385 	return (0);
12386 }
12387 
12388 /* Set interface broadcast address. */
12389 /* ARGSUSED2 */
12390 int
12391 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12392 	ip_ioctl_cmd_t *ipip, void *if_req)
12393 {
12394 	ipaddr_t addr;
12395 	ire_t	*ire;
12396 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12397 
12398 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12399 	    ipif->ipif_id));
12400 
12401 	ASSERT(IAM_WRITER_IPIF(ipif));
12402 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12403 		return (EADDRNOTAVAIL);
12404 
12405 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12406 
12407 	if (sin->sin_family != AF_INET)
12408 		return (EAFNOSUPPORT);
12409 
12410 	addr = sin->sin_addr.s_addr;
12411 	if (ipif->ipif_flags & IPIF_UP) {
12412 		/*
12413 		 * If we are already up, make sure the new
12414 		 * broadcast address makes sense.  If it does,
12415 		 * there should be an IRE for it already.
12416 		 * Don't match on ipif, only on the ill
12417 		 * since we are sharing these now. Don't use
12418 		 * MATCH_IRE_ILL_GROUP as we are looking for
12419 		 * the broadcast ire on this ill and each ill
12420 		 * in the group has its own broadcast ire.
12421 		 */
12422 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12423 		    ipif, ALL_ZONES, NULL,
12424 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12425 		if (ire == NULL) {
12426 			return (EINVAL);
12427 		} else {
12428 			ire_refrele(ire);
12429 		}
12430 	}
12431 	/*
12432 	 * Changing the broadcast addr for this ipif.
12433 	 * Make sure we have valid net and subnet bcast
12434 	 * ire's for other logical interfaces, if needed.
12435 	 */
12436 	if (addr != ipif->ipif_brd_addr)
12437 		ipif_check_bcast_ires(ipif);
12438 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12439 	return (0);
12440 }
12441 
12442 /* Get interface broadcast address. */
12443 /* ARGSUSED */
12444 int
12445 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12446     ip_ioctl_cmd_t *ipip, void *if_req)
12447 {
12448 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12449 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12450 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12451 		return (EADDRNOTAVAIL);
12452 
12453 	/* IPIF_BROADCAST not possible with IPv6 */
12454 	ASSERT(!ipif->ipif_isv6);
12455 	*sin = sin_null;
12456 	sin->sin_family = AF_INET;
12457 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12458 	return (0);
12459 }
12460 
12461 /*
12462  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12463  */
12464 /* ARGSUSED */
12465 int
12466 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12467     ip_ioctl_cmd_t *ipip, void *if_req)
12468 {
12469 	int err = 0;
12470 	in6_addr_t v6mask;
12471 
12472 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12473 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12474 
12475 	ASSERT(IAM_WRITER_IPIF(ipif));
12476 
12477 	if (ipif->ipif_isv6) {
12478 		sin6_t *sin6;
12479 
12480 		if (sin->sin_family != AF_INET6)
12481 			return (EAFNOSUPPORT);
12482 
12483 		sin6 = (sin6_t *)sin;
12484 		v6mask = sin6->sin6_addr;
12485 	} else {
12486 		ipaddr_t mask;
12487 
12488 		if (sin->sin_family != AF_INET)
12489 			return (EAFNOSUPPORT);
12490 
12491 		mask = sin->sin_addr.s_addr;
12492 		V4MASK_TO_V6(mask, v6mask);
12493 	}
12494 
12495 	/*
12496 	 * No big deal if the interface isn't already up, or the mask
12497 	 * isn't really changing, or this is pt-pt.
12498 	 */
12499 	if (!(ipif->ipif_flags & IPIF_UP) ||
12500 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12501 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12502 		ipif->ipif_v6net_mask = v6mask;
12503 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12504 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12505 			    ipif->ipif_v6net_mask,
12506 			    ipif->ipif_v6subnet);
12507 		}
12508 		return (0);
12509 	}
12510 	/*
12511 	 * Make sure we have valid net and subnet broadcast ire's
12512 	 * for the old netmask, if needed by other logical interfaces.
12513 	 */
12514 	if (!ipif->ipif_isv6)
12515 		ipif_check_bcast_ires(ipif);
12516 
12517 	err = ipif_logical_down(ipif, q, mp);
12518 	if (err == EINPROGRESS)
12519 		return (err);
12520 	ipif_down_tail(ipif);
12521 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12522 	return (err);
12523 }
12524 
12525 static int
12526 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12527 {
12528 	in6_addr_t v6mask;
12529 	int err = 0;
12530 
12531 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12532 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12533 
12534 	if (ipif->ipif_isv6) {
12535 		sin6_t *sin6;
12536 
12537 		sin6 = (sin6_t *)sin;
12538 		v6mask = sin6->sin6_addr;
12539 	} else {
12540 		ipaddr_t mask;
12541 
12542 		mask = sin->sin_addr.s_addr;
12543 		V4MASK_TO_V6(mask, v6mask);
12544 	}
12545 
12546 	ipif->ipif_v6net_mask = v6mask;
12547 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12548 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12549 		    ipif->ipif_v6subnet);
12550 	}
12551 	err = ipif_up(ipif, q, mp);
12552 
12553 	if (err == 0 || err == EINPROGRESS) {
12554 		/*
12555 		 * The interface must be DL_BOUND if this packet has to
12556 		 * go out on the wire. Since we only go through a logical
12557 		 * down and are bound with the driver during an internal
12558 		 * down/up that is satisfied.
12559 		 */
12560 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12561 			/* Potentially broadcast an address mask reply. */
12562 			ipif_mask_reply(ipif);
12563 		}
12564 	}
12565 	return (err);
12566 }
12567 
12568 /* ARGSUSED */
12569 int
12570 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12571     ip_ioctl_cmd_t *ipip, void *if_req)
12572 {
12573 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12574 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12575 	ipif_down_tail(ipif);
12576 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12577 }
12578 
12579 /* Get interface net mask. */
12580 /* ARGSUSED */
12581 int
12582 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12583     ip_ioctl_cmd_t *ipip, void *if_req)
12584 {
12585 	struct lifreq *lifr = (struct lifreq *)if_req;
12586 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12587 
12588 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12589 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12590 
12591 	/*
12592 	 * net mask can't change since we have a reference to the ipif.
12593 	 */
12594 	if (ipif->ipif_isv6) {
12595 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12596 		*sin6 = sin6_null;
12597 		sin6->sin6_family = AF_INET6;
12598 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12599 		lifr->lifr_addrlen =
12600 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12601 	} else {
12602 		*sin = sin_null;
12603 		sin->sin_family = AF_INET;
12604 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12605 		if (ipip->ipi_cmd_type == LIF_CMD) {
12606 			lifr->lifr_addrlen =
12607 			    ip_mask_to_plen(ipif->ipif_net_mask);
12608 		}
12609 	}
12610 	return (0);
12611 }
12612 
12613 /* ARGSUSED */
12614 int
12615 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12616     ip_ioctl_cmd_t *ipip, void *if_req)
12617 {
12618 
12619 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12620 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12621 	/*
12622 	 * Set interface metric.  We don't use this for
12623 	 * anything but we keep track of it in case it is
12624 	 * important to routing applications or such.
12625 	 */
12626 	if (ipip->ipi_cmd_type == IF_CMD) {
12627 		struct ifreq    *ifr;
12628 
12629 		ifr = (struct ifreq *)if_req;
12630 		ipif->ipif_metric = ifr->ifr_metric;
12631 	} else {
12632 		struct lifreq   *lifr;
12633 
12634 		lifr = (struct lifreq *)if_req;
12635 		ipif->ipif_metric = lifr->lifr_metric;
12636 	}
12637 	return (0);
12638 }
12639 
12640 
12641 /* ARGSUSED */
12642 int
12643 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12644     ip_ioctl_cmd_t *ipip, void *if_req)
12645 {
12646 
12647 	/* Get interface metric. */
12648 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12649 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12650 	if (ipip->ipi_cmd_type == IF_CMD) {
12651 		struct ifreq    *ifr;
12652 
12653 		ifr = (struct ifreq *)if_req;
12654 		ifr->ifr_metric = ipif->ipif_metric;
12655 	} else {
12656 		struct lifreq   *lifr;
12657 
12658 		lifr = (struct lifreq *)if_req;
12659 		lifr->lifr_metric = ipif->ipif_metric;
12660 	}
12661 
12662 	return (0);
12663 }
12664 
12665 /* ARGSUSED */
12666 int
12667 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12668     ip_ioctl_cmd_t *ipip, void *if_req)
12669 {
12670 
12671 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12672 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12673 	/*
12674 	 * Set the muxid returned from I_PLINK.
12675 	 */
12676 	if (ipip->ipi_cmd_type == IF_CMD) {
12677 		struct ifreq *ifr = (struct ifreq *)if_req;
12678 
12679 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12680 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12681 	} else {
12682 		struct lifreq *lifr = (struct lifreq *)if_req;
12683 
12684 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12685 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12686 	}
12687 	return (0);
12688 }
12689 
12690 /* ARGSUSED */
12691 int
12692 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12693     ip_ioctl_cmd_t *ipip, void *if_req)
12694 {
12695 
12696 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12697 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12698 	/*
12699 	 * Get the muxid saved in ill for I_PUNLINK.
12700 	 */
12701 	if (ipip->ipi_cmd_type == IF_CMD) {
12702 		struct ifreq *ifr = (struct ifreq *)if_req;
12703 
12704 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12705 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12706 	} else {
12707 		struct lifreq *lifr = (struct lifreq *)if_req;
12708 
12709 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12710 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12711 	}
12712 	return (0);
12713 }
12714 
12715 /*
12716  * Set the subnet prefix. Does not modify the broadcast address.
12717  */
12718 /* ARGSUSED */
12719 int
12720 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12721     ip_ioctl_cmd_t *ipip, void *if_req)
12722 {
12723 	int err = 0;
12724 	in6_addr_t v6addr;
12725 	in6_addr_t v6mask;
12726 	boolean_t need_up = B_FALSE;
12727 	int addrlen;
12728 
12729 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12730 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12731 
12732 	ASSERT(IAM_WRITER_IPIF(ipif));
12733 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12734 
12735 	if (ipif->ipif_isv6) {
12736 		sin6_t *sin6;
12737 
12738 		if (sin->sin_family != AF_INET6)
12739 			return (EAFNOSUPPORT);
12740 
12741 		sin6 = (sin6_t *)sin;
12742 		v6addr = sin6->sin6_addr;
12743 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12744 			return (EADDRNOTAVAIL);
12745 	} else {
12746 		ipaddr_t addr;
12747 
12748 		if (sin->sin_family != AF_INET)
12749 			return (EAFNOSUPPORT);
12750 
12751 		addr = sin->sin_addr.s_addr;
12752 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12753 			return (EADDRNOTAVAIL);
12754 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12755 		/* Add 96 bits */
12756 		addrlen += IPV6_ABITS - IP_ABITS;
12757 	}
12758 
12759 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12760 		return (EINVAL);
12761 
12762 	/* Check if bits in the address is set past the mask */
12763 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12764 		return (EINVAL);
12765 
12766 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12767 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12768 		return (0);	/* No change */
12769 
12770 	if (ipif->ipif_flags & IPIF_UP) {
12771 		/*
12772 		 * If the interface is already marked up,
12773 		 * we call ipif_down which will take care
12774 		 * of ditching any IREs that have been set
12775 		 * up based on the old interface address.
12776 		 */
12777 		err = ipif_logical_down(ipif, q, mp);
12778 		if (err == EINPROGRESS)
12779 			return (err);
12780 		ipif_down_tail(ipif);
12781 		need_up = B_TRUE;
12782 	}
12783 
12784 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12785 	return (err);
12786 }
12787 
12788 static int
12789 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12790     queue_t *q, mblk_t *mp, boolean_t need_up)
12791 {
12792 	ill_t	*ill = ipif->ipif_ill;
12793 	int	err = 0;
12794 
12795 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12796 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12797 
12798 	/* Set the new address. */
12799 	mutex_enter(&ill->ill_lock);
12800 	ipif->ipif_v6net_mask = v6mask;
12801 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12802 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12803 		    ipif->ipif_v6subnet);
12804 	}
12805 	mutex_exit(&ill->ill_lock);
12806 
12807 	if (need_up) {
12808 		/*
12809 		 * Now bring the interface back up.  If this
12810 		 * is the only IPIF for the ILL, ipif_up
12811 		 * will have to re-bind to the device, so
12812 		 * we may get back EINPROGRESS, in which
12813 		 * case, this IOCTL will get completed in
12814 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12815 		 */
12816 		err = ipif_up(ipif, q, mp);
12817 		if (err == EINPROGRESS)
12818 			return (err);
12819 	}
12820 	return (err);
12821 }
12822 
12823 /* ARGSUSED */
12824 int
12825 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12826     ip_ioctl_cmd_t *ipip, void *if_req)
12827 {
12828 	int	addrlen;
12829 	in6_addr_t v6addr;
12830 	in6_addr_t v6mask;
12831 	struct lifreq *lifr = (struct lifreq *)if_req;
12832 
12833 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12834 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12835 	ipif_down_tail(ipif);
12836 
12837 	addrlen = lifr->lifr_addrlen;
12838 	if (ipif->ipif_isv6) {
12839 		sin6_t *sin6;
12840 
12841 		sin6 = (sin6_t *)sin;
12842 		v6addr = sin6->sin6_addr;
12843 	} else {
12844 		ipaddr_t addr;
12845 
12846 		addr = sin->sin_addr.s_addr;
12847 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12848 		addrlen += IPV6_ABITS - IP_ABITS;
12849 	}
12850 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12851 
12852 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12853 }
12854 
12855 /* ARGSUSED */
12856 int
12857 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12858     ip_ioctl_cmd_t *ipip, void *if_req)
12859 {
12860 	struct lifreq *lifr = (struct lifreq *)if_req;
12861 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12862 
12863 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12864 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12865 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12866 
12867 	if (ipif->ipif_isv6) {
12868 		*sin6 = sin6_null;
12869 		sin6->sin6_family = AF_INET6;
12870 		sin6->sin6_addr = ipif->ipif_v6subnet;
12871 		lifr->lifr_addrlen =
12872 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12873 	} else {
12874 		*sin = sin_null;
12875 		sin->sin_family = AF_INET;
12876 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12877 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12878 	}
12879 	return (0);
12880 }
12881 
12882 /*
12883  * Set the IPv6 address token.
12884  */
12885 /* ARGSUSED */
12886 int
12887 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12888     ip_ioctl_cmd_t *ipi, void *if_req)
12889 {
12890 	ill_t *ill = ipif->ipif_ill;
12891 	int err;
12892 	in6_addr_t v6addr;
12893 	in6_addr_t v6mask;
12894 	boolean_t need_up = B_FALSE;
12895 	int i;
12896 	sin6_t *sin6 = (sin6_t *)sin;
12897 	struct lifreq *lifr = (struct lifreq *)if_req;
12898 	int addrlen;
12899 
12900 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12901 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12902 	ASSERT(IAM_WRITER_IPIF(ipif));
12903 
12904 	addrlen = lifr->lifr_addrlen;
12905 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12906 	if (ipif->ipif_id != 0)
12907 		return (EINVAL);
12908 
12909 	if (!ipif->ipif_isv6)
12910 		return (EINVAL);
12911 
12912 	if (addrlen > IPV6_ABITS)
12913 		return (EINVAL);
12914 
12915 	v6addr = sin6->sin6_addr;
12916 
12917 	/*
12918 	 * The length of the token is the length from the end.  To get
12919 	 * the proper mask for this, compute the mask of the bits not
12920 	 * in the token; ie. the prefix, and then xor to get the mask.
12921 	 */
12922 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12923 		return (EINVAL);
12924 	for (i = 0; i < 4; i++) {
12925 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12926 	}
12927 
12928 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12929 	    ill->ill_token_length == addrlen)
12930 		return (0);	/* No change */
12931 
12932 	if (ipif->ipif_flags & IPIF_UP) {
12933 		err = ipif_logical_down(ipif, q, mp);
12934 		if (err == EINPROGRESS)
12935 			return (err);
12936 		ipif_down_tail(ipif);
12937 		need_up = B_TRUE;
12938 	}
12939 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12940 	return (err);
12941 }
12942 
12943 static int
12944 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12945     mblk_t *mp, boolean_t need_up)
12946 {
12947 	in6_addr_t v6addr;
12948 	in6_addr_t v6mask;
12949 	ill_t	*ill = ipif->ipif_ill;
12950 	int	i;
12951 	int	err = 0;
12952 
12953 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12954 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12955 	v6addr = sin6->sin6_addr;
12956 	/*
12957 	 * The length of the token is the length from the end.  To get
12958 	 * the proper mask for this, compute the mask of the bits not
12959 	 * in the token; ie. the prefix, and then xor to get the mask.
12960 	 */
12961 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12962 	for (i = 0; i < 4; i++)
12963 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12964 
12965 	mutex_enter(&ill->ill_lock);
12966 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12967 	ill->ill_token_length = addrlen;
12968 	mutex_exit(&ill->ill_lock);
12969 
12970 	if (need_up) {
12971 		/*
12972 		 * Now bring the interface back up.  If this
12973 		 * is the only IPIF for the ILL, ipif_up
12974 		 * will have to re-bind to the device, so
12975 		 * we may get back EINPROGRESS, in which
12976 		 * case, this IOCTL will get completed in
12977 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12978 		 */
12979 		err = ipif_up(ipif, q, mp);
12980 		if (err == EINPROGRESS)
12981 			return (err);
12982 	}
12983 	return (err);
12984 }
12985 
12986 /* ARGSUSED */
12987 int
12988 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12989     ip_ioctl_cmd_t *ipi, void *if_req)
12990 {
12991 	ill_t *ill;
12992 	sin6_t *sin6 = (sin6_t *)sin;
12993 	struct lifreq *lifr = (struct lifreq *)if_req;
12994 
12995 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12996 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12997 	if (ipif->ipif_id != 0)
12998 		return (EINVAL);
12999 
13000 	ill = ipif->ipif_ill;
13001 	if (!ill->ill_isv6)
13002 		return (ENXIO);
13003 
13004 	*sin6 = sin6_null;
13005 	sin6->sin6_family = AF_INET6;
13006 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
13007 	sin6->sin6_addr = ill->ill_token;
13008 	lifr->lifr_addrlen = ill->ill_token_length;
13009 	return (0);
13010 }
13011 
13012 /*
13013  * Set (hardware) link specific information that might override
13014  * what was acquired through the DL_INFO_ACK.
13015  * The logic is as follows.
13016  *
13017  * become exclusive
13018  * set CHANGING flag
13019  * change mtu on affected IREs
13020  * clear CHANGING flag
13021  *
13022  * An ire add that occurs before the CHANGING flag is set will have its mtu
13023  * changed by the ip_sioctl_lnkinfo.
13024  *
13025  * During the time the CHANGING flag is set, no new ires will be added to the
13026  * bucket, and ire add will fail (due the CHANGING flag).
13027  *
13028  * An ire add that occurs after the CHANGING flag is set will have the right mtu
13029  * before it is added to the bucket.
13030  *
13031  * Obviously only 1 thread can set the CHANGING flag and we need to become
13032  * exclusive to set the flag.
13033  */
13034 /* ARGSUSED */
13035 int
13036 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13037     ip_ioctl_cmd_t *ipi, void *if_req)
13038 {
13039 	ill_t		*ill = ipif->ipif_ill;
13040 	ipif_t		*nipif;
13041 	int		ip_min_mtu;
13042 	boolean_t	mtu_walk = B_FALSE;
13043 	struct lifreq	*lifr = (struct lifreq *)if_req;
13044 	lif_ifinfo_req_t *lir;
13045 	ire_t		*ire;
13046 
13047 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
13048 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13049 	lir = &lifr->lifr_ifinfo;
13050 	ASSERT(IAM_WRITER_IPIF(ipif));
13051 
13052 	/* Only allow for logical unit zero i.e. not on "le0:17" */
13053 	if (ipif->ipif_id != 0)
13054 		return (EINVAL);
13055 
13056 	/* Set interface MTU. */
13057 	if (ipif->ipif_isv6)
13058 		ip_min_mtu = IPV6_MIN_MTU;
13059 	else
13060 		ip_min_mtu = IP_MIN_MTU;
13061 
13062 	/*
13063 	 * Verify values before we set anything. Allow zero to
13064 	 * mean unspecified.
13065 	 */
13066 	if (lir->lir_maxmtu != 0 &&
13067 	    (lir->lir_maxmtu > ill->ill_max_frag ||
13068 	    lir->lir_maxmtu < ip_min_mtu))
13069 		return (EINVAL);
13070 	if (lir->lir_reachtime != 0 &&
13071 	    lir->lir_reachtime > ND_MAX_REACHTIME)
13072 		return (EINVAL);
13073 	if (lir->lir_reachretrans != 0 &&
13074 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
13075 		return (EINVAL);
13076 
13077 	mutex_enter(&ill->ill_lock);
13078 	ill->ill_state_flags |= ILL_CHANGING;
13079 	for (nipif = ill->ill_ipif; nipif != NULL;
13080 	    nipif = nipif->ipif_next) {
13081 		nipif->ipif_state_flags |= IPIF_CHANGING;
13082 	}
13083 
13084 	mutex_exit(&ill->ill_lock);
13085 
13086 	if (lir->lir_maxmtu != 0) {
13087 		ill->ill_max_mtu = lir->lir_maxmtu;
13088 		ill->ill_mtu_userspecified = 1;
13089 		mtu_walk = B_TRUE;
13090 	}
13091 
13092 	if (lir->lir_reachtime != 0)
13093 		ill->ill_reachable_time = lir->lir_reachtime;
13094 
13095 	if (lir->lir_reachretrans != 0)
13096 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
13097 
13098 	ill->ill_max_hops = lir->lir_maxhops;
13099 
13100 	ill->ill_max_buf = ND_MAX_Q;
13101 
13102 	if (mtu_walk) {
13103 		/*
13104 		 * Set the MTU on all ipifs associated with this ill except
13105 		 * for those whose MTU was fixed via SIOCSLIFMTU.
13106 		 */
13107 		for (nipif = ill->ill_ipif; nipif != NULL;
13108 		    nipif = nipif->ipif_next) {
13109 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
13110 				continue;
13111 
13112 			nipif->ipif_mtu = ill->ill_max_mtu;
13113 
13114 			if (!(nipif->ipif_flags & IPIF_UP))
13115 				continue;
13116 
13117 			if (nipif->ipif_isv6)
13118 				ire = ipif_to_ire_v6(nipif);
13119 			else
13120 				ire = ipif_to_ire(nipif);
13121 			if (ire != NULL) {
13122 				ire->ire_max_frag = ipif->ipif_mtu;
13123 				ire_refrele(ire);
13124 			}
13125 			if (ill->ill_isv6) {
13126 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13127 				    ipif_mtu_change, (char *)nipif,
13128 				    ill);
13129 			} else {
13130 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13131 				    ipif_mtu_change, (char *)nipif,
13132 				    ill);
13133 			}
13134 		}
13135 	}
13136 
13137 	mutex_enter(&ill->ill_lock);
13138 	for (nipif = ill->ill_ipif; nipif != NULL;
13139 	    nipif = nipif->ipif_next) {
13140 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13141 	}
13142 	ILL_UNMARK_CHANGING(ill);
13143 	mutex_exit(&ill->ill_lock);
13144 
13145 	return (0);
13146 }
13147 
13148 /* ARGSUSED */
13149 int
13150 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13151     ip_ioctl_cmd_t *ipi, void *if_req)
13152 {
13153 	struct lif_ifinfo_req *lir;
13154 	ill_t *ill = ipif->ipif_ill;
13155 
13156 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13157 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13158 	if (ipif->ipif_id != 0)
13159 		return (EINVAL);
13160 
13161 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13162 	lir->lir_maxhops = ill->ill_max_hops;
13163 	lir->lir_reachtime = ill->ill_reachable_time;
13164 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13165 	lir->lir_maxmtu = ill->ill_max_mtu;
13166 
13167 	return (0);
13168 }
13169 
13170 /*
13171  * Return best guess as to the subnet mask for the specified address.
13172  * Based on the subnet masks for all the configured interfaces.
13173  *
13174  * We end up returning a zero mask in the case of default, multicast or
13175  * experimental.
13176  */
13177 static ipaddr_t
13178 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13179 {
13180 	ipaddr_t net_mask;
13181 	ill_t	*ill;
13182 	ipif_t	*ipif;
13183 	ill_walk_context_t ctx;
13184 	ipif_t	*fallback_ipif = NULL;
13185 
13186 	net_mask = ip_net_mask(addr);
13187 	if (net_mask == 0) {
13188 		*ipifp = NULL;
13189 		return (0);
13190 	}
13191 
13192 	/* Let's check to see if this is maybe a local subnet route. */
13193 	/* this function only applies to IPv4 interfaces */
13194 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13195 	ill = ILL_START_WALK_V4(&ctx, ipst);
13196 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13197 		mutex_enter(&ill->ill_lock);
13198 		for (ipif = ill->ill_ipif; ipif != NULL;
13199 		    ipif = ipif->ipif_next) {
13200 			if (!IPIF_CAN_LOOKUP(ipif))
13201 				continue;
13202 			if (!(ipif->ipif_flags & IPIF_UP))
13203 				continue;
13204 			if ((ipif->ipif_subnet & net_mask) ==
13205 			    (addr & net_mask)) {
13206 				/*
13207 				 * Don't trust pt-pt interfaces if there are
13208 				 * other interfaces.
13209 				 */
13210 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13211 					if (fallback_ipif == NULL) {
13212 						ipif_refhold_locked(ipif);
13213 						fallback_ipif = ipif;
13214 					}
13215 					continue;
13216 				}
13217 
13218 				/*
13219 				 * Fine. Just assume the same net mask as the
13220 				 * directly attached subnet interface is using.
13221 				 */
13222 				ipif_refhold_locked(ipif);
13223 				mutex_exit(&ill->ill_lock);
13224 				rw_exit(&ipst->ips_ill_g_lock);
13225 				if (fallback_ipif != NULL)
13226 					ipif_refrele(fallback_ipif);
13227 				*ipifp = ipif;
13228 				return (ipif->ipif_net_mask);
13229 			}
13230 		}
13231 		mutex_exit(&ill->ill_lock);
13232 	}
13233 	rw_exit(&ipst->ips_ill_g_lock);
13234 
13235 	*ipifp = fallback_ipif;
13236 	return ((fallback_ipif != NULL) ?
13237 	    fallback_ipif->ipif_net_mask : net_mask);
13238 }
13239 
13240 /*
13241  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13242  */
13243 static void
13244 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13245 {
13246 	IOCP	iocp;
13247 	ipft_t	*ipft;
13248 	ipllc_t	*ipllc;
13249 	mblk_t	*mp1;
13250 	cred_t	*cr;
13251 	int	error = 0;
13252 	conn_t	*connp;
13253 
13254 	ip1dbg(("ip_wput_ioctl"));
13255 	iocp = (IOCP)mp->b_rptr;
13256 	mp1 = mp->b_cont;
13257 	if (mp1 == NULL) {
13258 		iocp->ioc_error = EINVAL;
13259 		mp->b_datap->db_type = M_IOCNAK;
13260 		iocp->ioc_count = 0;
13261 		qreply(q, mp);
13262 		return;
13263 	}
13264 
13265 	/*
13266 	 * These IOCTLs provide various control capabilities to
13267 	 * upstream agents such as ULPs and processes.	There
13268 	 * are currently two such IOCTLs implemented.  They
13269 	 * are used by TCP to provide update information for
13270 	 * existing IREs and to forcibly delete an IRE for a
13271 	 * host that is not responding, thereby forcing an
13272 	 * attempt at a new route.
13273 	 */
13274 	iocp->ioc_error = EINVAL;
13275 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13276 		goto done;
13277 
13278 	ipllc = (ipllc_t *)mp1->b_rptr;
13279 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13280 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13281 			break;
13282 	}
13283 	/*
13284 	 * prefer credential from mblk over ioctl;
13285 	 * see ip_sioctl_copyin_setup
13286 	 */
13287 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13288 
13289 	/*
13290 	 * Refhold the conn in case the request gets queued up in some lookup
13291 	 */
13292 	ASSERT(CONN_Q(q));
13293 	connp = Q_TO_CONN(q);
13294 	CONN_INC_REF(connp);
13295 	if (ipft->ipft_pfi &&
13296 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13297 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13298 		error = (*ipft->ipft_pfi)(q,
13299 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13300 	}
13301 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13302 		/*
13303 		 * CONN_OPER_PENDING_DONE happens in the function called
13304 		 * through ipft_pfi above.
13305 		 */
13306 		return;
13307 	}
13308 
13309 	CONN_OPER_PENDING_DONE(connp);
13310 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13311 		freemsg(mp);
13312 		return;
13313 	}
13314 	iocp->ioc_error = error;
13315 
13316 done:
13317 	mp->b_datap->db_type = M_IOCACK;
13318 	if (iocp->ioc_error)
13319 		iocp->ioc_count = 0;
13320 	qreply(q, mp);
13321 }
13322 
13323 /*
13324  * Lookup an ipif using the sequence id (ipif_seqid)
13325  */
13326 ipif_t *
13327 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13328 {
13329 	ipif_t *ipif;
13330 
13331 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13332 
13333 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13334 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13335 			return (ipif);
13336 	}
13337 	return (NULL);
13338 }
13339 
13340 /*
13341  * Assign a unique id for the ipif. This is used later when we send
13342  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13343  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13344  * IRE is added, we verify that ipif has not disappeared.
13345  */
13346 
13347 static void
13348 ipif_assign_seqid(ipif_t *ipif)
13349 {
13350 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13351 
13352 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13353 }
13354 
13355 /*
13356  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13357  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13358  * be inserted into the first space available in the list. The value of
13359  * ipif_id will then be set to the appropriate value for its position.
13360  */
13361 static int
13362 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13363 {
13364 	ill_t *ill;
13365 	ipif_t *tipif;
13366 	ipif_t **tipifp;
13367 	int id;
13368 	ip_stack_t	*ipst;
13369 
13370 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13371 	    IAM_WRITER_IPIF(ipif));
13372 
13373 	ill = ipif->ipif_ill;
13374 	ASSERT(ill != NULL);
13375 	ipst = ill->ill_ipst;
13376 
13377 	/*
13378 	 * In the case of lo0:0 we already hold the ill_g_lock.
13379 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13380 	 * ipif_insert. Another such caller is ipif_move.
13381 	 */
13382 	if (acquire_g_lock)
13383 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13384 	if (acquire_ill_lock)
13385 		mutex_enter(&ill->ill_lock);
13386 	id = ipif->ipif_id;
13387 	tipifp = &(ill->ill_ipif);
13388 	if (id == -1) {	/* need to find a real id */
13389 		id = 0;
13390 		while ((tipif = *tipifp) != NULL) {
13391 			ASSERT(tipif->ipif_id >= id);
13392 			if (tipif->ipif_id != id)
13393 				break; /* non-consecutive id */
13394 			id++;
13395 			tipifp = &(tipif->ipif_next);
13396 		}
13397 		/* limit number of logical interfaces */
13398 		if (id >= ipst->ips_ip_addrs_per_if) {
13399 			if (acquire_ill_lock)
13400 				mutex_exit(&ill->ill_lock);
13401 			if (acquire_g_lock)
13402 				rw_exit(&ipst->ips_ill_g_lock);
13403 			return (-1);
13404 		}
13405 		ipif->ipif_id = id; /* assign new id */
13406 	} else if (id < ipst->ips_ip_addrs_per_if) {
13407 		/* we have a real id; insert ipif in the right place */
13408 		while ((tipif = *tipifp) != NULL) {
13409 			ASSERT(tipif->ipif_id != id);
13410 			if (tipif->ipif_id > id)
13411 				break; /* found correct location */
13412 			tipifp = &(tipif->ipif_next);
13413 		}
13414 	} else {
13415 		if (acquire_ill_lock)
13416 			mutex_exit(&ill->ill_lock);
13417 		if (acquire_g_lock)
13418 			rw_exit(&ipst->ips_ill_g_lock);
13419 		return (-1);
13420 	}
13421 
13422 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13423 
13424 	ipif->ipif_next = tipif;
13425 	*tipifp = ipif;
13426 	if (acquire_ill_lock)
13427 		mutex_exit(&ill->ill_lock);
13428 	if (acquire_g_lock)
13429 		rw_exit(&ipst->ips_ill_g_lock);
13430 	return (0);
13431 }
13432 
13433 static void
13434 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13435 {
13436 	ipif_t	**ipifp;
13437 	ill_t	*ill = ipif->ipif_ill;
13438 
13439 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13440 	if (acquire_ill_lock)
13441 		mutex_enter(&ill->ill_lock);
13442 	else
13443 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13444 
13445 	ipifp = &ill->ill_ipif;
13446 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13447 		if (*ipifp == ipif) {
13448 			*ipifp = ipif->ipif_next;
13449 			break;
13450 		}
13451 	}
13452 
13453 	if (acquire_ill_lock)
13454 		mutex_exit(&ill->ill_lock);
13455 }
13456 
13457 /*
13458  * Allocate and initialize a new interface control structure.  (Always
13459  * called as writer.)
13460  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13461  * is not part of the global linked list of ills. ipif_seqid is unique
13462  * in the system and to preserve the uniqueness, it is assigned only
13463  * when ill becomes part of the global list. At that point ill will
13464  * have a name. If it doesn't get assigned here, it will get assigned
13465  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13466  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13467  * the interface flags or any other information from the DL_INFO_ACK for
13468  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13469  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13470  * second DL_INFO_ACK comes in from the driver.
13471  */
13472 static ipif_t *
13473 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13474 {
13475 	ipif_t	*ipif;
13476 	phyint_t *phyi;
13477 
13478 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13479 	    ill->ill_name, id, (void *)ill));
13480 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13481 
13482 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13483 		return (NULL);
13484 	*ipif = ipif_zero;	/* start clean */
13485 
13486 	ipif->ipif_ill = ill;
13487 	ipif->ipif_id = id;	/* could be -1 */
13488 	/*
13489 	 * Inherit the zoneid from the ill; for the shared stack instance
13490 	 * this is always the global zone
13491 	 */
13492 	ipif->ipif_zoneid = ill->ill_zoneid;
13493 
13494 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13495 
13496 	ipif->ipif_refcnt = 0;
13497 	ipif->ipif_saved_ire_cnt = 0;
13498 
13499 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13500 		mi_free(ipif);
13501 		return (NULL);
13502 	}
13503 	/* -1 id should have been replaced by real id */
13504 	id = ipif->ipif_id;
13505 	ASSERT(id >= 0);
13506 
13507 	if (ill->ill_name[0] != '\0')
13508 		ipif_assign_seqid(ipif);
13509 
13510 	/*
13511 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13512 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13513 	 * ioctl sets ipif_orig_ipifid to zero.
13514 	 */
13515 	ipif->ipif_orig_ipifid = id;
13516 
13517 	/*
13518 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13519 	 * The ipif is still not up and can't be looked up until the
13520 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13521 	 */
13522 	mutex_enter(&ill->ill_lock);
13523 	mutex_enter(&ill->ill_phyint->phyint_lock);
13524 	/*
13525 	 * Set the running flag when logical interface zero is created.
13526 	 * For subsequent logical interfaces, a DLPI link down
13527 	 * notification message may have cleared the running flag to
13528 	 * indicate the link is down, so we shouldn't just blindly set it.
13529 	 */
13530 	if (id == 0)
13531 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13532 	ipif->ipif_ire_type = ire_type;
13533 	phyi = ill->ill_phyint;
13534 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13535 
13536 	if (ipif->ipif_isv6) {
13537 		ill->ill_flags |= ILLF_IPV6;
13538 	} else {
13539 		ipaddr_t inaddr_any = INADDR_ANY;
13540 
13541 		ill->ill_flags |= ILLF_IPV4;
13542 
13543 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13544 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13545 		    &ipif->ipif_v6lcl_addr);
13546 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13547 		    &ipif->ipif_v6src_addr);
13548 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13549 		    &ipif->ipif_v6subnet);
13550 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13551 		    &ipif->ipif_v6net_mask);
13552 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13553 		    &ipif->ipif_v6brd_addr);
13554 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13555 		    &ipif->ipif_v6pp_dst_addr);
13556 	}
13557 
13558 	/*
13559 	 * Don't set the interface flags etc. now, will do it in
13560 	 * ip_ll_subnet_defaults.
13561 	 */
13562 	if (!initialize) {
13563 		mutex_exit(&ill->ill_lock);
13564 		mutex_exit(&ill->ill_phyint->phyint_lock);
13565 		return (ipif);
13566 	}
13567 	ipif->ipif_mtu = ill->ill_max_mtu;
13568 
13569 	if (ill->ill_bcast_addr_length != 0) {
13570 		/*
13571 		 * Later detect lack of DLPI driver multicast
13572 		 * capability by catching DL_ENABMULTI errors in
13573 		 * ip_rput_dlpi.
13574 		 */
13575 		ill->ill_flags |= ILLF_MULTICAST;
13576 		if (!ipif->ipif_isv6)
13577 			ipif->ipif_flags |= IPIF_BROADCAST;
13578 	} else {
13579 		if (ill->ill_net_type != IRE_LOOPBACK) {
13580 			if (ipif->ipif_isv6)
13581 				/*
13582 				 * Note: xresolv interfaces will eventually need
13583 				 * NOARP set here as well, but that will require
13584 				 * those external resolvers to have some
13585 				 * knowledge of that flag and act appropriately.
13586 				 * Not to be changed at present.
13587 				 */
13588 				ill->ill_flags |= ILLF_NONUD;
13589 			else
13590 				ill->ill_flags |= ILLF_NOARP;
13591 		}
13592 		if (ill->ill_phys_addr_length == 0) {
13593 			if (ill->ill_media &&
13594 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13595 				ipif->ipif_flags |= IPIF_NOXMIT;
13596 				phyi->phyint_flags |= PHYI_VIRTUAL;
13597 			} else {
13598 				/* pt-pt supports multicast. */
13599 				ill->ill_flags |= ILLF_MULTICAST;
13600 				if (ill->ill_net_type == IRE_LOOPBACK) {
13601 					phyi->phyint_flags |=
13602 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13603 				} else {
13604 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13605 				}
13606 			}
13607 		}
13608 	}
13609 	mutex_exit(&ill->ill_lock);
13610 	mutex_exit(&ill->ill_phyint->phyint_lock);
13611 	return (ipif);
13612 }
13613 
13614 /*
13615  * If appropriate, send a message up to the resolver delete the entry
13616  * for the address of this interface which is going out of business.
13617  * (Always called as writer).
13618  *
13619  * NOTE : We need to check for NULL mps as some of the fields are
13620  *	  initialized only for some interface types. See ipif_resolver_up()
13621  *	  for details.
13622  */
13623 void
13624 ipif_arp_down(ipif_t *ipif)
13625 {
13626 	mblk_t	*mp;
13627 	ill_t	*ill = ipif->ipif_ill;
13628 
13629 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13630 	ASSERT(IAM_WRITER_IPIF(ipif));
13631 
13632 	/* Delete the mapping for the local address */
13633 	mp = ipif->ipif_arp_del_mp;
13634 	if (mp != NULL) {
13635 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13636 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13637 		putnext(ill->ill_rq, mp);
13638 		ipif->ipif_arp_del_mp = NULL;
13639 	}
13640 
13641 	/*
13642 	 * If this is the last ipif that is going down and there are no
13643 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13644 	 * clean up ARP completely.
13645 	 */
13646 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13647 
13648 		/* Send up AR_INTERFACE_DOWN message */
13649 		mp = ill->ill_arp_down_mp;
13650 		if (mp != NULL) {
13651 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13652 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13653 			    ipif->ipif_id));
13654 			putnext(ill->ill_rq, mp);
13655 			ill->ill_arp_down_mp = NULL;
13656 		}
13657 
13658 		/* Tell ARP to delete the multicast mappings */
13659 		mp = ill->ill_arp_del_mapping_mp;
13660 		if (mp != NULL) {
13661 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13662 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13663 			    ipif->ipif_id));
13664 			putnext(ill->ill_rq, mp);
13665 			ill->ill_arp_del_mapping_mp = NULL;
13666 		}
13667 	}
13668 }
13669 
13670 /*
13671  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13672  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13673  * that it wants the add_mp allocated in this function to be returned
13674  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13675  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13676  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13677  * as it does a ipif_arp_down after calling this function - which will
13678  * remove what we add here.
13679  *
13680  * Returns -1 on failures and 0 on success.
13681  */
13682 int
13683 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13684 {
13685 	mblk_t	*del_mp = NULL;
13686 	mblk_t *add_mp = NULL;
13687 	mblk_t *mp;
13688 	ill_t	*ill = ipif->ipif_ill;
13689 	phyint_t *phyi = ill->ill_phyint;
13690 	ipaddr_t addr, mask, extract_mask = 0;
13691 	arma_t	*arma;
13692 	uint8_t *maddr, *bphys_addr;
13693 	uint32_t hw_start;
13694 	dl_unitdata_req_t *dlur;
13695 
13696 	ASSERT(IAM_WRITER_IPIF(ipif));
13697 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13698 		return (0);
13699 
13700 	/*
13701 	 * Delete the existing mapping from ARP. Normally ipif_down
13702 	 * -> ipif_arp_down should send this up to ARP. The only
13703 	 * reason we would find this when we are switching from
13704 	 * Multicast to Broadcast where we did not do a down.
13705 	 */
13706 	mp = ill->ill_arp_del_mapping_mp;
13707 	if (mp != NULL) {
13708 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13709 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13710 		putnext(ill->ill_rq, mp);
13711 		ill->ill_arp_del_mapping_mp = NULL;
13712 	}
13713 
13714 	if (arp_add_mapping_mp != NULL)
13715 		*arp_add_mapping_mp = NULL;
13716 
13717 	/*
13718 	 * Check that the address is not to long for the constant
13719 	 * length reserved in the template arma_t.
13720 	 */
13721 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13722 		return (-1);
13723 
13724 	/* Add mapping mblk */
13725 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13726 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13727 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13728 	    (caddr_t)&addr);
13729 	if (add_mp == NULL)
13730 		return (-1);
13731 	arma = (arma_t *)add_mp->b_rptr;
13732 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13733 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13734 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13735 
13736 	/*
13737 	 * Determine the broadcast address.
13738 	 */
13739 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13740 	if (ill->ill_sap_length < 0)
13741 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13742 	else
13743 		bphys_addr = (uchar_t *)dlur +
13744 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13745 	/*
13746 	 * Check PHYI_MULTI_BCAST and length of physical
13747 	 * address to determine if we use the mapping or the
13748 	 * broadcast address.
13749 	 */
13750 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13751 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13752 		    bphys_addr, maddr, &hw_start, &extract_mask))
13753 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13754 
13755 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13756 	    (ill->ill_flags & ILLF_MULTICAST)) {
13757 		/* Make sure this will not match the "exact" entry. */
13758 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13759 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13760 		    (caddr_t)&addr);
13761 		if (del_mp == NULL) {
13762 			freemsg(add_mp);
13763 			return (-1);
13764 		}
13765 		bcopy(&extract_mask, (char *)arma +
13766 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13767 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13768 			/* Use link-layer broadcast address for MULTI_BCAST */
13769 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13770 			ip2dbg(("ipif_arp_setup_multicast: adding"
13771 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13772 		} else {
13773 			arma->arma_hw_mapping_start = hw_start;
13774 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13775 			    " ARP setup for %s\n", ill->ill_name));
13776 		}
13777 	} else {
13778 		freemsg(add_mp);
13779 		ASSERT(del_mp == NULL);
13780 		/* It is neither MULTICAST nor MULTI_BCAST */
13781 		return (0);
13782 	}
13783 	ASSERT(add_mp != NULL && del_mp != NULL);
13784 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13785 	ill->ill_arp_del_mapping_mp = del_mp;
13786 	if (arp_add_mapping_mp != NULL) {
13787 		/* The caller just wants the mblks allocated */
13788 		*arp_add_mapping_mp = add_mp;
13789 	} else {
13790 		/* The caller wants us to send it to arp */
13791 		putnext(ill->ill_rq, add_mp);
13792 	}
13793 	return (0);
13794 }
13795 
13796 /*
13797  * Get the resolver set up for a new interface address.
13798  * (Always called as writer.)
13799  * Called both for IPv4 and IPv6 interfaces,
13800  * though it only sets up the resolver for v6
13801  * if it's an xresolv interface (one using an external resolver).
13802  * Honors ILLF_NOARP.
13803  * The enumerated value res_act is used to tune the behavior.
13804  * If set to Res_act_initial, then we set up all the resolver
13805  * structures for a new interface.  If set to Res_act_move, then
13806  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13807  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13808  * asynchronous hardware address change notification.  If set to
13809  * Res_act_defend, then we tell ARP that it needs to send a single
13810  * gratuitous message in defense of the address.
13811  * Returns error on failure.
13812  */
13813 int
13814 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13815 {
13816 	caddr_t	addr;
13817 	mblk_t	*arp_up_mp = NULL;
13818 	mblk_t	*arp_down_mp = NULL;
13819 	mblk_t	*arp_add_mp = NULL;
13820 	mblk_t	*arp_del_mp = NULL;
13821 	mblk_t	*arp_add_mapping_mp = NULL;
13822 	mblk_t	*arp_del_mapping_mp = NULL;
13823 	ill_t	*ill = ipif->ipif_ill;
13824 	uchar_t	*area_p = NULL;
13825 	uchar_t	*ared_p = NULL;
13826 	int	err = ENOMEM;
13827 	boolean_t was_dup;
13828 
13829 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13830 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13831 	ASSERT(IAM_WRITER_IPIF(ipif));
13832 
13833 	was_dup = B_FALSE;
13834 	if (res_act == Res_act_initial) {
13835 		ipif->ipif_addr_ready = 0;
13836 		/*
13837 		 * We're bringing an interface up here.  There's no way that we
13838 		 * should need to shut down ARP now.
13839 		 */
13840 		mutex_enter(&ill->ill_lock);
13841 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13842 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13843 			ill->ill_ipif_dup_count--;
13844 			was_dup = B_TRUE;
13845 		}
13846 		mutex_exit(&ill->ill_lock);
13847 	}
13848 	if (ipif->ipif_recovery_id != 0)
13849 		(void) untimeout(ipif->ipif_recovery_id);
13850 	ipif->ipif_recovery_id = 0;
13851 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13852 		ipif->ipif_addr_ready = 1;
13853 		return (0);
13854 	}
13855 	/* NDP will set the ipif_addr_ready flag when it's ready */
13856 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13857 		return (0);
13858 
13859 	if (ill->ill_isv6) {
13860 		/*
13861 		 * External resolver for IPv6
13862 		 */
13863 		ASSERT(res_act == Res_act_initial);
13864 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13865 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13866 			area_p = (uchar_t *)&ip6_area_template;
13867 			ared_p = (uchar_t *)&ip6_ared_template;
13868 		}
13869 	} else {
13870 		/*
13871 		 * IPv4 arp case. If the ARP stream has already started
13872 		 * closing, fail this request for ARP bringup. Else
13873 		 * record the fact that an ARP bringup is pending.
13874 		 */
13875 		mutex_enter(&ill->ill_lock);
13876 		if (ill->ill_arp_closing) {
13877 			mutex_exit(&ill->ill_lock);
13878 			err = EINVAL;
13879 			goto failed;
13880 		} else {
13881 			if (ill->ill_ipif_up_count == 0 &&
13882 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13883 				ill->ill_arp_bringup_pending = 1;
13884 			mutex_exit(&ill->ill_lock);
13885 		}
13886 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13887 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13888 			area_p = (uchar_t *)&ip_area_template;
13889 			ared_p = (uchar_t *)&ip_ared_template;
13890 		}
13891 	}
13892 
13893 	/*
13894 	 * Add an entry for the local address in ARP only if it
13895 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13896 	 */
13897 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13898 		area_t *area;
13899 
13900 		/* Now ask ARP to publish our address. */
13901 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13902 		if (arp_add_mp == NULL)
13903 			goto failed;
13904 		area = (area_t *)arp_add_mp->b_rptr;
13905 		if (res_act != Res_act_initial) {
13906 			/*
13907 			 * Copy the new hardware address and length into
13908 			 * arp_add_mp to be sent to ARP.
13909 			 */
13910 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13911 			bcopy(ill->ill_phys_addr,
13912 			    ((char *)area + area->area_hw_addr_offset),
13913 			    area->area_hw_addr_length);
13914 		}
13915 
13916 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13917 		    ACE_F_MYADDR;
13918 
13919 		if (res_act == Res_act_defend) {
13920 			area->area_flags |= ACE_F_DEFEND;
13921 			/*
13922 			 * If we're just defending our address now, then
13923 			 * there's no need to set up ARP multicast mappings.
13924 			 * The publish command is enough.
13925 			 */
13926 			goto done;
13927 		}
13928 
13929 		if (res_act != Res_act_initial)
13930 			goto arp_setup_multicast;
13931 
13932 		/*
13933 		 * Allocate an ARP deletion message so we know we can tell ARP
13934 		 * when the interface goes down.
13935 		 */
13936 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13937 		if (arp_del_mp == NULL)
13938 			goto failed;
13939 
13940 	} else {
13941 		if (res_act != Res_act_initial)
13942 			goto done;
13943 	}
13944 	/*
13945 	 * Need to bring up ARP or setup multicast mapping only
13946 	 * when the first interface is coming UP.
13947 	 */
13948 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13949 	    was_dup) {
13950 		goto done;
13951 	}
13952 
13953 	/*
13954 	 * Allocate an ARP down message (to be saved) and an ARP up
13955 	 * message.
13956 	 */
13957 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13958 	if (arp_down_mp == NULL)
13959 		goto failed;
13960 
13961 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13962 	if (arp_up_mp == NULL)
13963 		goto failed;
13964 
13965 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13966 		goto done;
13967 
13968 arp_setup_multicast:
13969 	/*
13970 	 * Setup the multicast mappings. This function initializes
13971 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13972 	 * IPv6.
13973 	 */
13974 	if (!ill->ill_isv6) {
13975 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13976 		if (err != 0)
13977 			goto failed;
13978 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13979 		ASSERT(arp_add_mapping_mp != NULL);
13980 	}
13981 
13982 done:
13983 	if (arp_del_mp != NULL) {
13984 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13985 		ipif->ipif_arp_del_mp = arp_del_mp;
13986 	}
13987 	if (arp_down_mp != NULL) {
13988 		ASSERT(ill->ill_arp_down_mp == NULL);
13989 		ill->ill_arp_down_mp = arp_down_mp;
13990 	}
13991 	if (arp_del_mapping_mp != NULL) {
13992 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13993 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13994 	}
13995 	if (arp_up_mp != NULL) {
13996 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13997 		    ill->ill_name, ipif->ipif_id));
13998 		putnext(ill->ill_rq, arp_up_mp);
13999 	}
14000 	if (arp_add_mp != NULL) {
14001 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
14002 		    ill->ill_name, ipif->ipif_id));
14003 		/*
14004 		 * If it's an extended ARP implementation, then we'll wait to
14005 		 * hear that DAD has finished before using the interface.
14006 		 */
14007 		if (!ill->ill_arp_extend)
14008 			ipif->ipif_addr_ready = 1;
14009 		putnext(ill->ill_rq, arp_add_mp);
14010 	} else {
14011 		ipif->ipif_addr_ready = 1;
14012 	}
14013 	if (arp_add_mapping_mp != NULL) {
14014 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
14015 		    ill->ill_name, ipif->ipif_id));
14016 		putnext(ill->ill_rq, arp_add_mapping_mp);
14017 	}
14018 	if (res_act != Res_act_initial)
14019 		return (0);
14020 
14021 	if (ill->ill_flags & ILLF_NOARP)
14022 		err = ill_arp_off(ill);
14023 	else
14024 		err = ill_arp_on(ill);
14025 	if (err != 0) {
14026 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
14027 		freemsg(ipif->ipif_arp_del_mp);
14028 		freemsg(ill->ill_arp_down_mp);
14029 		freemsg(ill->ill_arp_del_mapping_mp);
14030 		ipif->ipif_arp_del_mp = NULL;
14031 		ill->ill_arp_down_mp = NULL;
14032 		ill->ill_arp_del_mapping_mp = NULL;
14033 		return (err);
14034 	}
14035 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
14036 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
14037 
14038 failed:
14039 	ip1dbg(("ipif_resolver_up: FAILED\n"));
14040 	freemsg(arp_add_mp);
14041 	freemsg(arp_del_mp);
14042 	freemsg(arp_add_mapping_mp);
14043 	freemsg(arp_up_mp);
14044 	freemsg(arp_down_mp);
14045 	ill->ill_arp_bringup_pending = 0;
14046 	return (err);
14047 }
14048 
14049 /*
14050  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
14051  * just gone back up.
14052  */
14053 static void
14054 ipif_arp_start_dad(ipif_t *ipif)
14055 {
14056 	ill_t *ill = ipif->ipif_ill;
14057 	mblk_t *arp_add_mp;
14058 	area_t *area;
14059 
14060 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
14061 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14062 	    ipif->ipif_lcl_addr == INADDR_ANY ||
14063 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
14064 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
14065 		/*
14066 		 * If we can't contact ARP for some reason, that's not really a
14067 		 * problem.  Just send out the routing socket notification that
14068 		 * DAD completion would have done, and continue.
14069 		 */
14070 		ipif_mask_reply(ipif);
14071 		ip_rts_ifmsg(ipif);
14072 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14073 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14074 		ipif->ipif_addr_ready = 1;
14075 		return;
14076 	}
14077 
14078 	/* Setting the 'unverified' flag restarts DAD */
14079 	area = (area_t *)arp_add_mp->b_rptr;
14080 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
14081 	    ACE_F_UNVERIFIED;
14082 	putnext(ill->ill_rq, arp_add_mp);
14083 }
14084 
14085 static void
14086 ipif_ndp_start_dad(ipif_t *ipif)
14087 {
14088 	nce_t *nce;
14089 
14090 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
14091 	if (nce == NULL)
14092 		return;
14093 
14094 	if (!ndp_restart_dad(nce)) {
14095 		/*
14096 		 * If we can't restart DAD for some reason, that's not really a
14097 		 * problem.  Just send out the routing socket notification that
14098 		 * DAD completion would have done, and continue.
14099 		 */
14100 		ip_rts_ifmsg(ipif);
14101 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14102 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14103 		ipif->ipif_addr_ready = 1;
14104 	}
14105 	NCE_REFRELE(nce);
14106 }
14107 
14108 /*
14109  * Restart duplicate address detection on all interfaces on the given ill.
14110  *
14111  * This is called when an interface transitions from down to up
14112  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
14113  *
14114  * Note that since the underlying physical link has transitioned, we must cause
14115  * at least one routing socket message to be sent here, either via DAD
14116  * completion or just by default on the first ipif.  (If we don't do this, then
14117  * in.mpathd will see long delays when doing link-based failure recovery.)
14118  */
14119 void
14120 ill_restart_dad(ill_t *ill, boolean_t went_up)
14121 {
14122 	ipif_t *ipif;
14123 
14124 	if (ill == NULL)
14125 		return;
14126 
14127 	/*
14128 	 * If layer two doesn't support duplicate address detection, then just
14129 	 * send the routing socket message now and be done with it.
14130 	 */
14131 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14132 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14133 		ip_rts_ifmsg(ill->ill_ipif);
14134 		return;
14135 	}
14136 
14137 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14138 		if (went_up) {
14139 			if (ipif->ipif_flags & IPIF_UP) {
14140 				if (ill->ill_isv6)
14141 					ipif_ndp_start_dad(ipif);
14142 				else
14143 					ipif_arp_start_dad(ipif);
14144 			} else if (ill->ill_isv6 &&
14145 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14146 				/*
14147 				 * For IPv4, the ARP module itself will
14148 				 * automatically start the DAD process when it
14149 				 * sees DL_NOTE_LINK_UP.  We respond to the
14150 				 * AR_CN_READY at the completion of that task.
14151 				 * For IPv6, we must kick off the bring-up
14152 				 * process now.
14153 				 */
14154 				ndp_do_recovery(ipif);
14155 			} else {
14156 				/*
14157 				 * Unfortunately, the first ipif is "special"
14158 				 * and represents the underlying ill in the
14159 				 * routing socket messages.  Thus, when this
14160 				 * one ipif is down, we must still notify so
14161 				 * that the user knows the IFF_RUNNING status
14162 				 * change.  (If the first ipif is up, then
14163 				 * we'll handle eventual routing socket
14164 				 * notification via DAD completion.)
14165 				 */
14166 				if (ipif == ill->ill_ipif)
14167 					ip_rts_ifmsg(ill->ill_ipif);
14168 			}
14169 		} else {
14170 			/*
14171 			 * After link down, we'll need to send a new routing
14172 			 * message when the link comes back, so clear
14173 			 * ipif_addr_ready.
14174 			 */
14175 			ipif->ipif_addr_ready = 0;
14176 		}
14177 	}
14178 
14179 	/*
14180 	 * If we've torn down links, then notify the user right away.
14181 	 */
14182 	if (!went_up)
14183 		ip_rts_ifmsg(ill->ill_ipif);
14184 }
14185 
14186 /*
14187  * Wakeup all threads waiting to enter the ipsq, and sleeping
14188  * on any of the ills in this ipsq. The ill_lock of the ill
14189  * must be held so that waiters don't miss wakeups
14190  */
14191 static void
14192 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14193 {
14194 	phyint_t *phyint;
14195 
14196 	phyint = ipsq->ipsq_phyint_list;
14197 	while (phyint != NULL) {
14198 		if (phyint->phyint_illv4) {
14199 			if (!caller_holds_lock)
14200 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14201 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14202 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14203 			if (!caller_holds_lock)
14204 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14205 		}
14206 		if (phyint->phyint_illv6) {
14207 			if (!caller_holds_lock)
14208 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14209 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14210 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14211 			if (!caller_holds_lock)
14212 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14213 		}
14214 		phyint = phyint->phyint_ipsq_next;
14215 	}
14216 }
14217 
14218 static ipsq_t *
14219 ipsq_create(char *groupname, ip_stack_t *ipst)
14220 {
14221 	ipsq_t	*ipsq;
14222 
14223 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14224 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14225 	if (ipsq == NULL) {
14226 		return (NULL);
14227 	}
14228 
14229 	if (groupname != NULL)
14230 		(void) strcpy(ipsq->ipsq_name, groupname);
14231 	else
14232 		ipsq->ipsq_name[0] = '\0';
14233 
14234 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14235 	ipsq->ipsq_flags |= IPSQ_GROUP;
14236 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14237 	ipst->ips_ipsq_g_head = ipsq;
14238 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14239 	return (ipsq);
14240 }
14241 
14242 /*
14243  * Return an ipsq correspoding to the groupname. If 'create' is true
14244  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14245  * uniquely with an IPMP group. However during IPMP groupname operations,
14246  * multiple IPMP groups may be associated with a single ipsq. But no
14247  * IPMP group can be associated with more than 1 ipsq at any time.
14248  * For example
14249  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14250  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14251  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14252  *
14253  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14254  * status shown below during the execution of the above command.
14255  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14256  *
14257  * After the completion of the above groupname command we return to the stable
14258  * state shown below.
14259  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14260  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14261  *
14262  * Because of the above, we don't search based on the ipsq_name since that
14263  * would miss the correct ipsq during certain windows as shown above.
14264  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14265  * natural state.
14266  */
14267 static ipsq_t *
14268 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14269     ip_stack_t *ipst)
14270 {
14271 	ipsq_t	*ipsq;
14272 	int	group_len;
14273 	phyint_t *phyint;
14274 
14275 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14276 
14277 	group_len = strlen(groupname);
14278 	ASSERT(group_len != 0);
14279 	group_len++;
14280 
14281 	for (ipsq = ipst->ips_ipsq_g_head;
14282 	    ipsq != NULL;
14283 	    ipsq = ipsq->ipsq_next) {
14284 		/*
14285 		 * When an ipsq is being split, and ill_split_ipsq
14286 		 * calls this function, we exclude it from being considered.
14287 		 */
14288 		if (ipsq == exclude_ipsq)
14289 			continue;
14290 
14291 		/*
14292 		 * Compare against the ipsq_name. The groupname change happens
14293 		 * in 2 phases. The 1st phase merges the from group into
14294 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14295 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14296 		 * ipsq_name. At this point the phyint_groupname has not been
14297 		 * updated.
14298 		 */
14299 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14300 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14301 			/*
14302 			 * Verify that an ipmp groupname is exactly
14303 			 * part of 1 ipsq and is not found in any other
14304 			 * ipsq.
14305 			 */
14306 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14307 			    NULL);
14308 			return (ipsq);
14309 		}
14310 
14311 		/*
14312 		 * Comparison against ipsq_name alone is not sufficient.
14313 		 * In the case when groups are currently being
14314 		 * merged, the ipsq could hold other IPMP groups temporarily.
14315 		 * so we walk the phyint list and compare against the
14316 		 * phyint_groupname as well.
14317 		 */
14318 		phyint = ipsq->ipsq_phyint_list;
14319 		while (phyint != NULL) {
14320 			if ((group_len == phyint->phyint_groupname_len) &&
14321 			    (bcmp(phyint->phyint_groupname, groupname,
14322 			    group_len) == 0)) {
14323 				/*
14324 				 * Verify that an ipmp groupname is exactly
14325 				 * part of 1 ipsq and is not found in any other
14326 				 * ipsq.
14327 				 */
14328 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14329 				    ipst) == NULL);
14330 				return (ipsq);
14331 			}
14332 			phyint = phyint->phyint_ipsq_next;
14333 		}
14334 	}
14335 	if (create)
14336 		ipsq = ipsq_create(groupname, ipst);
14337 	return (ipsq);
14338 }
14339 
14340 static void
14341 ipsq_delete(ipsq_t *ipsq)
14342 {
14343 	ipsq_t *nipsq;
14344 	ipsq_t *pipsq = NULL;
14345 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14346 
14347 	/*
14348 	 * We don't hold the ipsq lock, but we are sure no new
14349 	 * messages can land up, since the ipsq_refs is zero.
14350 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14351 	 * is associated with this ipsq. (Lookups are based on ill_name
14352 	 * or phyint_groupname)
14353 	 */
14354 	ASSERT(ipsq->ipsq_refs == 0);
14355 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14356 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14357 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14358 		/*
14359 		 * This is not the ipsq of an IPMP group.
14360 		 */
14361 		ipsq->ipsq_ipst = NULL;
14362 		kmem_free(ipsq, sizeof (ipsq_t));
14363 		return;
14364 	}
14365 
14366 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14367 
14368 	/*
14369 	 * Locate the ipsq  before we can remove it from
14370 	 * the singly linked list of ipsq's.
14371 	 */
14372 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14373 	    nipsq = nipsq->ipsq_next) {
14374 		if (nipsq == ipsq) {
14375 			break;
14376 		}
14377 		pipsq = nipsq;
14378 	}
14379 
14380 	ASSERT(nipsq == ipsq);
14381 
14382 	/* unlink ipsq from the list */
14383 	if (pipsq != NULL)
14384 		pipsq->ipsq_next = ipsq->ipsq_next;
14385 	else
14386 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14387 	ipsq->ipsq_ipst = NULL;
14388 	kmem_free(ipsq, sizeof (ipsq_t));
14389 	rw_exit(&ipst->ips_ill_g_lock);
14390 }
14391 
14392 static void
14393 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14394     queue_t *q)
14395 {
14396 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14397 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14398 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14399 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14400 	ASSERT(current_mp != NULL);
14401 
14402 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14403 	    NEW_OP, NULL);
14404 
14405 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14406 	    new_ipsq->ipsq_xopq_mphead != NULL);
14407 
14408 	/*
14409 	 * move from old ipsq to the new ipsq.
14410 	 */
14411 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14412 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14413 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14414 
14415 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14416 }
14417 
14418 void
14419 ill_group_cleanup(ill_t *ill)
14420 {
14421 	ill_t *ill_v4;
14422 	ill_t *ill_v6;
14423 	ipif_t *ipif;
14424 
14425 	ill_v4 = ill->ill_phyint->phyint_illv4;
14426 	ill_v6 = ill->ill_phyint->phyint_illv6;
14427 
14428 	if (ill_v4 != NULL) {
14429 		mutex_enter(&ill_v4->ill_lock);
14430 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14431 		    ipif = ipif->ipif_next) {
14432 			IPIF_UNMARK_MOVING(ipif);
14433 		}
14434 		ill_v4->ill_up_ipifs = B_FALSE;
14435 		mutex_exit(&ill_v4->ill_lock);
14436 	}
14437 
14438 	if (ill_v6 != NULL) {
14439 		mutex_enter(&ill_v6->ill_lock);
14440 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14441 		    ipif = ipif->ipif_next) {
14442 			IPIF_UNMARK_MOVING(ipif);
14443 		}
14444 		ill_v6->ill_up_ipifs = B_FALSE;
14445 		mutex_exit(&ill_v6->ill_lock);
14446 	}
14447 }
14448 /*
14449  * This function is called when an ill has had a change in its group status
14450  * to bring up all the ipifs that were up before the change.
14451  */
14452 int
14453 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14454 {
14455 	ipif_t *ipif;
14456 	ill_t *ill_v4;
14457 	ill_t *ill_v6;
14458 	ill_t *from_ill;
14459 	int err = 0;
14460 
14461 
14462 	ASSERT(IAM_WRITER_ILL(ill));
14463 
14464 	/*
14465 	 * Except for ipif_state_flags and ill_state_flags the other
14466 	 * fields of the ipif/ill that are modified below are protected
14467 	 * implicitly since we are a writer. We would have tried to down
14468 	 * even an ipif that was already down, in ill_down_ipifs. So we
14469 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14470 	 */
14471 	ill_v4 = ill->ill_phyint->phyint_illv4;
14472 	ill_v6 = ill->ill_phyint->phyint_illv6;
14473 	if (ill_v4 != NULL) {
14474 		ill_v4->ill_up_ipifs = B_TRUE;
14475 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14476 		    ipif = ipif->ipif_next) {
14477 			mutex_enter(&ill_v4->ill_lock);
14478 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14479 			IPIF_UNMARK_MOVING(ipif);
14480 			mutex_exit(&ill_v4->ill_lock);
14481 			if (ipif->ipif_was_up) {
14482 				if (!(ipif->ipif_flags & IPIF_UP))
14483 					err = ipif_up(ipif, q, mp);
14484 				ipif->ipif_was_up = B_FALSE;
14485 				if (err != 0) {
14486 					/*
14487 					 * Can there be any other error ?
14488 					 */
14489 					ASSERT(err == EINPROGRESS);
14490 					return (err);
14491 				}
14492 			}
14493 		}
14494 		mutex_enter(&ill_v4->ill_lock);
14495 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14496 		mutex_exit(&ill_v4->ill_lock);
14497 		ill_v4->ill_up_ipifs = B_FALSE;
14498 		if (ill_v4->ill_move_in_progress) {
14499 			ASSERT(ill_v4->ill_move_peer != NULL);
14500 			ill_v4->ill_move_in_progress = B_FALSE;
14501 			from_ill = ill_v4->ill_move_peer;
14502 			from_ill->ill_move_in_progress = B_FALSE;
14503 			from_ill->ill_move_peer = NULL;
14504 			mutex_enter(&from_ill->ill_lock);
14505 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14506 			mutex_exit(&from_ill->ill_lock);
14507 			if (ill_v6 == NULL) {
14508 				if (from_ill->ill_phyint->phyint_flags &
14509 				    PHYI_STANDBY) {
14510 					phyint_inactive(from_ill->ill_phyint);
14511 				}
14512 				if (ill_v4->ill_phyint->phyint_flags &
14513 				    PHYI_STANDBY) {
14514 					phyint_inactive(ill_v4->ill_phyint);
14515 				}
14516 			}
14517 			ill_v4->ill_move_peer = NULL;
14518 		}
14519 	}
14520 
14521 	if (ill_v6 != NULL) {
14522 		ill_v6->ill_up_ipifs = B_TRUE;
14523 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14524 		    ipif = ipif->ipif_next) {
14525 			mutex_enter(&ill_v6->ill_lock);
14526 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14527 			IPIF_UNMARK_MOVING(ipif);
14528 			mutex_exit(&ill_v6->ill_lock);
14529 			if (ipif->ipif_was_up) {
14530 				if (!(ipif->ipif_flags & IPIF_UP))
14531 					err = ipif_up(ipif, q, mp);
14532 				ipif->ipif_was_up = B_FALSE;
14533 				if (err != 0) {
14534 					/*
14535 					 * Can there be any other error ?
14536 					 */
14537 					ASSERT(err == EINPROGRESS);
14538 					return (err);
14539 				}
14540 			}
14541 		}
14542 		mutex_enter(&ill_v6->ill_lock);
14543 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14544 		mutex_exit(&ill_v6->ill_lock);
14545 		ill_v6->ill_up_ipifs = B_FALSE;
14546 		if (ill_v6->ill_move_in_progress) {
14547 			ASSERT(ill_v6->ill_move_peer != NULL);
14548 			ill_v6->ill_move_in_progress = B_FALSE;
14549 			from_ill = ill_v6->ill_move_peer;
14550 			from_ill->ill_move_in_progress = B_FALSE;
14551 			from_ill->ill_move_peer = NULL;
14552 			mutex_enter(&from_ill->ill_lock);
14553 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14554 			mutex_exit(&from_ill->ill_lock);
14555 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14556 				phyint_inactive(from_ill->ill_phyint);
14557 			}
14558 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14559 				phyint_inactive(ill_v6->ill_phyint);
14560 			}
14561 			ill_v6->ill_move_peer = NULL;
14562 		}
14563 	}
14564 	return (0);
14565 }
14566 
14567 /*
14568  * bring down all the approriate ipifs.
14569  */
14570 /* ARGSUSED */
14571 static void
14572 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14573 {
14574 	ipif_t *ipif;
14575 
14576 	ASSERT(IAM_WRITER_ILL(ill));
14577 
14578 	/*
14579 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14580 	 * are modified below are protected implicitly since we are a writer
14581 	 */
14582 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14583 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14584 			continue;
14585 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14586 			/*
14587 			 * We go through the ipif_down logic even if the ipif
14588 			 * is already down, since routes can be added based
14589 			 * on down ipifs. Going through ipif_down once again
14590 			 * will delete any IREs created based on these routes.
14591 			 */
14592 			if (ipif->ipif_flags & IPIF_UP)
14593 				ipif->ipif_was_up = B_TRUE;
14594 			/*
14595 			 * If called with chk_nofailover true ipif is moving.
14596 			 */
14597 			mutex_enter(&ill->ill_lock);
14598 			if (chk_nofailover) {
14599 				ipif->ipif_state_flags |=
14600 				    IPIF_MOVING | IPIF_CHANGING;
14601 			} else {
14602 				ipif->ipif_state_flags |= IPIF_CHANGING;
14603 			}
14604 			mutex_exit(&ill->ill_lock);
14605 			/*
14606 			 * Need to re-create net/subnet bcast ires if
14607 			 * they are dependent on ipif.
14608 			 */
14609 			if (!ipif->ipif_isv6)
14610 				ipif_check_bcast_ires(ipif);
14611 			(void) ipif_logical_down(ipif, NULL, NULL);
14612 			ipif_non_duplicate(ipif);
14613 			ipif_down_tail(ipif);
14614 		}
14615 	}
14616 }
14617 
14618 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14619 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14620 	(ipsq)->ipsq_refs++;				\
14621 }
14622 
14623 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14624 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14625 	(ipsq)->ipsq_refs--;				\
14626 	if ((ipsq)->ipsq_refs == 0)				\
14627 		(ipsq)->ipsq_name[0] = '\0'; 		\
14628 }
14629 
14630 /*
14631  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14632  * new_ipsq.
14633  */
14634 static void
14635 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14636 {
14637 	phyint_t *phyint;
14638 	phyint_t *next_phyint;
14639 
14640 	/*
14641 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14642 	 * writer and the ill_lock of the ill in question. Also the dest
14643 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14644 	 */
14645 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14646 
14647 	phyint = cur_ipsq->ipsq_phyint_list;
14648 	cur_ipsq->ipsq_phyint_list = NULL;
14649 	while (phyint != NULL) {
14650 		next_phyint = phyint->phyint_ipsq_next;
14651 		IPSQ_DEC_REF(cur_ipsq, ipst);
14652 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14653 		new_ipsq->ipsq_phyint_list = phyint;
14654 		IPSQ_INC_REF(new_ipsq, ipst);
14655 		phyint->phyint_ipsq = new_ipsq;
14656 		phyint = next_phyint;
14657 	}
14658 }
14659 
14660 #define	SPLIT_SUCCESS		0
14661 #define	SPLIT_NOT_NEEDED	1
14662 #define	SPLIT_FAILED		2
14663 
14664 int
14665 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14666     ip_stack_t *ipst)
14667 {
14668 	ipsq_t *newipsq = NULL;
14669 
14670 	/*
14671 	 * Assertions denote pre-requisites for changing the ipsq of
14672 	 * a phyint
14673 	 */
14674 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14675 	/*
14676 	 * <ill-phyint> assocs can't change while ill_g_lock
14677 	 * is held as writer. See ill_phyint_reinit()
14678 	 */
14679 	ASSERT(phyint->phyint_illv4 == NULL ||
14680 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14681 	ASSERT(phyint->phyint_illv6 == NULL ||
14682 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14683 
14684 	if ((phyint->phyint_groupname_len !=
14685 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14686 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14687 	    phyint->phyint_groupname_len) != 0)) {
14688 		/*
14689 		 * Once we fail in creating a new ipsq due to memory shortage,
14690 		 * don't attempt to create new ipsq again, based on another
14691 		 * phyint, since we want all phyints belonging to an IPMP group
14692 		 * to be in the same ipsq even in the event of mem alloc fails.
14693 		 */
14694 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14695 		    cur_ipsq, ipst);
14696 		if (newipsq == NULL) {
14697 			/* Memory allocation failure */
14698 			return (SPLIT_FAILED);
14699 		} else {
14700 			/* ipsq_refs protected by ill_g_lock (writer) */
14701 			IPSQ_DEC_REF(cur_ipsq, ipst);
14702 			phyint->phyint_ipsq = newipsq;
14703 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14704 			newipsq->ipsq_phyint_list = phyint;
14705 			IPSQ_INC_REF(newipsq, ipst);
14706 			return (SPLIT_SUCCESS);
14707 		}
14708 	}
14709 	return (SPLIT_NOT_NEEDED);
14710 }
14711 
14712 /*
14713  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14714  * to do this split
14715  */
14716 static int
14717 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14718 {
14719 	ipsq_t *newipsq;
14720 
14721 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14722 	/*
14723 	 * <ill-phyint> assocs can't change while ill_g_lock
14724 	 * is held as writer. See ill_phyint_reinit()
14725 	 */
14726 
14727 	ASSERT(phyint->phyint_illv4 == NULL ||
14728 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14729 	ASSERT(phyint->phyint_illv6 == NULL ||
14730 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14731 
14732 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14733 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14734 		/*
14735 		 * ipsq_init failed due to no memory
14736 		 * caller will use the same ipsq
14737 		 */
14738 		return (SPLIT_FAILED);
14739 	}
14740 
14741 	/* ipsq_ref is protected by ill_g_lock (writer) */
14742 	IPSQ_DEC_REF(cur_ipsq, ipst);
14743 
14744 	/*
14745 	 * This is a new ipsq that is unknown to the world.
14746 	 * So we don't need to hold ipsq_lock,
14747 	 */
14748 	newipsq = phyint->phyint_ipsq;
14749 	newipsq->ipsq_writer = NULL;
14750 	newipsq->ipsq_reentry_cnt--;
14751 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14752 #ifdef ILL_DEBUG
14753 	newipsq->ipsq_depth = 0;
14754 #endif
14755 
14756 	return (SPLIT_SUCCESS);
14757 }
14758 
14759 /*
14760  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14761  * ipsq's representing their individual groups or themselves. Return
14762  * whether split needs to be retried again later.
14763  */
14764 static boolean_t
14765 ill_split_ipsq(ipsq_t *cur_ipsq)
14766 {
14767 	phyint_t *phyint;
14768 	phyint_t *next_phyint;
14769 	int	error;
14770 	boolean_t need_retry = B_FALSE;
14771 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14772 
14773 	phyint = cur_ipsq->ipsq_phyint_list;
14774 	cur_ipsq->ipsq_phyint_list = NULL;
14775 	while (phyint != NULL) {
14776 		next_phyint = phyint->phyint_ipsq_next;
14777 		/*
14778 		 * 'created' will tell us whether the callee actually
14779 		 * created an ipsq. Lack of memory may force the callee
14780 		 * to return without creating an ipsq.
14781 		 */
14782 		if (phyint->phyint_groupname == NULL) {
14783 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14784 		} else {
14785 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14786 			    need_retry, ipst);
14787 		}
14788 
14789 		switch (error) {
14790 		case SPLIT_FAILED:
14791 			need_retry = B_TRUE;
14792 			/* FALLTHRU */
14793 		case SPLIT_NOT_NEEDED:
14794 			/*
14795 			 * Keep it on the list.
14796 			 */
14797 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14798 			cur_ipsq->ipsq_phyint_list = phyint;
14799 			break;
14800 		case SPLIT_SUCCESS:
14801 			break;
14802 		default:
14803 			ASSERT(0);
14804 		}
14805 
14806 		phyint = next_phyint;
14807 	}
14808 	return (need_retry);
14809 }
14810 
14811 /*
14812  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14813  * and return the ills in the list. This list will be
14814  * needed to unlock all the ills later on by the caller.
14815  * The <ill-ipsq> associations could change between the
14816  * lock and unlock. Hence the unlock can't traverse the
14817  * ipsq to get the list of ills.
14818  */
14819 static int
14820 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14821 {
14822 	int	cnt = 0;
14823 	phyint_t	*phyint;
14824 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14825 
14826 	/*
14827 	 * The caller holds ill_g_lock to ensure that the ill memberships
14828 	 * of the ipsq don't change
14829 	 */
14830 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14831 
14832 	phyint = ipsq->ipsq_phyint_list;
14833 	while (phyint != NULL) {
14834 		if (phyint->phyint_illv4 != NULL) {
14835 			ASSERT(cnt < list_max);
14836 			list[cnt++] = phyint->phyint_illv4;
14837 		}
14838 		if (phyint->phyint_illv6 != NULL) {
14839 			ASSERT(cnt < list_max);
14840 			list[cnt++] = phyint->phyint_illv6;
14841 		}
14842 		phyint = phyint->phyint_ipsq_next;
14843 	}
14844 	ill_lock_ills(list, cnt);
14845 	return (cnt);
14846 }
14847 
14848 void
14849 ill_lock_ills(ill_t **list, int cnt)
14850 {
14851 	int	i;
14852 
14853 	if (cnt > 1) {
14854 		boolean_t try_again;
14855 		do {
14856 			try_again = B_FALSE;
14857 			for (i = 0; i < cnt - 1; i++) {
14858 				if (list[i] < list[i + 1]) {
14859 					ill_t	*tmp;
14860 
14861 					/* swap the elements */
14862 					tmp = list[i];
14863 					list[i] = list[i + 1];
14864 					list[i + 1] = tmp;
14865 					try_again = B_TRUE;
14866 				}
14867 			}
14868 		} while (try_again);
14869 	}
14870 
14871 	for (i = 0; i < cnt; i++) {
14872 		if (i == 0) {
14873 			if (list[i] != NULL)
14874 				mutex_enter(&list[i]->ill_lock);
14875 			else
14876 				return;
14877 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14878 			mutex_enter(&list[i]->ill_lock);
14879 		}
14880 	}
14881 }
14882 
14883 void
14884 ill_unlock_ills(ill_t **list, int cnt)
14885 {
14886 	int	i;
14887 
14888 	for (i = 0; i < cnt; i++) {
14889 		if ((i == 0) && (list[i] != NULL)) {
14890 			mutex_exit(&list[i]->ill_lock);
14891 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14892 			mutex_exit(&list[i]->ill_lock);
14893 		}
14894 	}
14895 }
14896 
14897 /*
14898  * Merge all the ills from 1 ipsq group into another ipsq group.
14899  * The source ipsq group is specified by the ipsq associated with
14900  * 'from_ill'. The destination ipsq group is specified by the ipsq
14901  * associated with 'to_ill' or 'groupname' respectively.
14902  * Note that ipsq itself does not have a reference count mechanism
14903  * and functions don't look up an ipsq and pass it around. Instead
14904  * functions pass around an ill or groupname, and the ipsq is looked
14905  * up from the ill or groupname and the required operation performed
14906  * atomically with the lookup on the ipsq.
14907  */
14908 static int
14909 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14910     queue_t *q)
14911 {
14912 	ipsq_t *old_ipsq;
14913 	ipsq_t *new_ipsq;
14914 	ill_t	**ill_list;
14915 	int	cnt;
14916 	size_t	ill_list_size;
14917 	boolean_t became_writer_on_new_sq = B_FALSE;
14918 	ip_stack_t	*ipst = from_ill->ill_ipst;
14919 
14920 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14921 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14922 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14923 
14924 	/*
14925 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14926 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14927 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14928 	 */
14929 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14930 
14931 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14932 	if (groupname != NULL)
14933 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14934 	else {
14935 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14936 	}
14937 
14938 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14939 
14940 	/*
14941 	 * both groups are on the same ipsq.
14942 	 */
14943 	if (old_ipsq == new_ipsq) {
14944 		rw_exit(&ipst->ips_ill_g_lock);
14945 		return (0);
14946 	}
14947 
14948 	cnt = old_ipsq->ipsq_refs << 1;
14949 	ill_list_size = cnt * sizeof (ill_t *);
14950 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14951 	if (ill_list == NULL) {
14952 		rw_exit(&ipst->ips_ill_g_lock);
14953 		return (ENOMEM);
14954 	}
14955 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14956 
14957 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14958 	mutex_enter(&new_ipsq->ipsq_lock);
14959 	if ((new_ipsq->ipsq_writer == NULL &&
14960 	    new_ipsq->ipsq_current_ipif == NULL) ||
14961 	    (new_ipsq->ipsq_writer == curthread)) {
14962 		new_ipsq->ipsq_writer = curthread;
14963 		new_ipsq->ipsq_reentry_cnt++;
14964 		became_writer_on_new_sq = B_TRUE;
14965 	}
14966 
14967 	/*
14968 	 * We are holding ill_g_lock as writer and all the ill locks of
14969 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14970 	 * message can land up on the old ipsq even though we don't hold the
14971 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14972 	 */
14973 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14974 
14975 	/*
14976 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14977 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14978 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14979 	 */
14980 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14981 
14982 	/*
14983 	 * Mark the new ipsq as needing a split since it is currently
14984 	 * being shared by more than 1 IPMP group. The split will
14985 	 * occur at the end of ipsq_exit
14986 	 */
14987 	new_ipsq->ipsq_split = B_TRUE;
14988 
14989 	/* Now release all the locks */
14990 	mutex_exit(&new_ipsq->ipsq_lock);
14991 	ill_unlock_ills(ill_list, cnt);
14992 	rw_exit(&ipst->ips_ill_g_lock);
14993 
14994 	kmem_free(ill_list, ill_list_size);
14995 
14996 	/*
14997 	 * If we succeeded in becoming writer on the new ipsq, then
14998 	 * drain the new ipsq and start processing  all enqueued messages
14999 	 * including the current ioctl we are processing which is either
15000 	 * a set groupname or failover/failback.
15001 	 */
15002 	if (became_writer_on_new_sq)
15003 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
15004 
15005 	/*
15006 	 * syncq has been changed and all the messages have been moved.
15007 	 */
15008 	mutex_enter(&old_ipsq->ipsq_lock);
15009 	old_ipsq->ipsq_current_ipif = NULL;
15010 	old_ipsq->ipsq_current_ioctl = 0;
15011 	mutex_exit(&old_ipsq->ipsq_lock);
15012 	return (EINPROGRESS);
15013 }
15014 
15015 /*
15016  * Delete and add the loopback copy and non-loopback copy of
15017  * the BROADCAST ire corresponding to ill and addr. Used to
15018  * group broadcast ires together when ill becomes part of
15019  * a group.
15020  *
15021  * This function is also called when ill is leaving the group
15022  * so that the ires belonging to the group gets re-grouped.
15023  */
15024 static void
15025 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
15026 {
15027 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
15028 	ire_t **ire_ptpn = &ire_head;
15029 	ip_stack_t	*ipst = ill->ill_ipst;
15030 
15031 	/*
15032 	 * The loopback and non-loopback IREs are inserted in the order in which
15033 	 * they're found, on the basis that they are correctly ordered (loopback
15034 	 * first).
15035 	 */
15036 	for (;;) {
15037 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15038 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15039 		if (ire == NULL)
15040 			break;
15041 
15042 		/*
15043 		 * we are passing in KM_SLEEP because it is not easy to
15044 		 * go back to a sane state in case of memory failure.
15045 		 */
15046 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
15047 		ASSERT(nire != NULL);
15048 		bzero(nire, sizeof (ire_t));
15049 		/*
15050 		 * Don't use ire_max_frag directly since we don't
15051 		 * hold on to 'ire' until we add the new ire 'nire' and
15052 		 * we don't want the new ire to have a dangling reference
15053 		 * to 'ire'. The ire_max_frag of a broadcast ire must
15054 		 * be in sync with the ipif_mtu of the associate ipif.
15055 		 * For eg. this happens as a result of SIOCSLIFNAME,
15056 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
15057 		 * the driver. A change in ire_max_frag triggered as
15058 		 * as a result of path mtu discovery, or due to an
15059 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
15060 		 * route change -mtu command does not apply to broadcast ires.
15061 		 *
15062 		 * XXX We need a recovery strategy here if ire_init fails
15063 		 */
15064 		if (ire_init(nire,
15065 		    (uchar_t *)&ire->ire_addr,
15066 		    (uchar_t *)&ire->ire_mask,
15067 		    (uchar_t *)&ire->ire_src_addr,
15068 		    (uchar_t *)&ire->ire_gateway_addr,
15069 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
15070 		    &ire->ire_ipif->ipif_mtu,
15071 		    ire->ire_nce,
15072 		    ire->ire_rfq,
15073 		    ire->ire_stq,
15074 		    ire->ire_type,
15075 		    ire->ire_ipif,
15076 		    ire->ire_cmask,
15077 		    ire->ire_phandle,
15078 		    ire->ire_ihandle,
15079 		    ire->ire_flags,
15080 		    &ire->ire_uinfo,
15081 		    NULL,
15082 		    NULL,
15083 		    ipst) == NULL) {
15084 			cmn_err(CE_PANIC, "ire_init() failed");
15085 		}
15086 		ire_delete(ire);
15087 		ire_refrele(ire);
15088 
15089 		/*
15090 		 * The newly created IREs are inserted at the tail of the list
15091 		 * starting with ire_head. As we've just allocated them no one
15092 		 * knows about them so it's safe.
15093 		 */
15094 		*ire_ptpn = nire;
15095 		ire_ptpn = &nire->ire_next;
15096 	}
15097 
15098 	for (nire = ire_head; nire != NULL; nire = nire_next) {
15099 		int error;
15100 		ire_t *oire;
15101 		/* unlink the IRE from our list before calling ire_add() */
15102 		nire_next = nire->ire_next;
15103 		nire->ire_next = NULL;
15104 
15105 		/* ire_add adds the ire at the right place in the list */
15106 		oire = nire;
15107 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
15108 		ASSERT(error == 0);
15109 		ASSERT(oire == nire);
15110 		ire_refrele(nire);	/* Held in ire_add */
15111 	}
15112 }
15113 
15114 /*
15115  * This function is usually called when an ill is inserted in
15116  * a group and all the ipifs are already UP. As all the ipifs
15117  * are already UP, the broadcast ires have already been created
15118  * and been inserted. But, ire_add_v4 would not have grouped properly.
15119  * We need to re-group for the benefit of ip_wput_ire which
15120  * expects BROADCAST ires to be grouped properly to avoid sending
15121  * more than one copy of the broadcast packet per group.
15122  *
15123  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
15124  *	  because when ipif_up_done ends up calling this, ires have
15125  *        already been added before illgrp_insert i.e before ill_group
15126  *	  has been initialized.
15127  */
15128 static void
15129 ill_group_bcast_for_xmit(ill_t *ill)
15130 {
15131 	ill_group_t *illgrp;
15132 	ipif_t *ipif;
15133 	ipaddr_t addr;
15134 	ipaddr_t net_mask;
15135 	ipaddr_t subnet_netmask;
15136 
15137 	illgrp = ill->ill_group;
15138 
15139 	/*
15140 	 * This function is called even when an ill is deleted from
15141 	 * the group. Hence, illgrp could be null.
15142 	 */
15143 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15144 		return;
15145 
15146 	/*
15147 	 * Delete all the BROADCAST ires matching this ill and add
15148 	 * them back. This time, ire_add_v4 should take care of
15149 	 * grouping them with others because ill is part of the
15150 	 * group.
15151 	 */
15152 	ill_bcast_delete_and_add(ill, 0);
15153 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15154 
15155 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15156 
15157 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15158 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15159 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15160 		} else {
15161 			net_mask = htonl(IN_CLASSA_NET);
15162 		}
15163 		addr = net_mask & ipif->ipif_subnet;
15164 		ill_bcast_delete_and_add(ill, addr);
15165 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15166 
15167 		subnet_netmask = ipif->ipif_net_mask;
15168 		addr = ipif->ipif_subnet;
15169 		ill_bcast_delete_and_add(ill, addr);
15170 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15171 	}
15172 }
15173 
15174 /*
15175  * This function is called from illgrp_delete when ill is being deleted
15176  * from the group.
15177  *
15178  * As ill is not there in the group anymore, any address belonging
15179  * to this ill should be cleared of IRE_MARK_NORECV.
15180  */
15181 static void
15182 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15183 {
15184 	ire_t *ire;
15185 	irb_t *irb;
15186 	ip_stack_t	*ipst = ill->ill_ipst;
15187 
15188 	ASSERT(ill->ill_group == NULL);
15189 
15190 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15191 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15192 
15193 	if (ire != NULL) {
15194 		/*
15195 		 * IPMP and plumbing operations are serialized on the ipsq, so
15196 		 * no one will insert or delete a broadcast ire under our feet.
15197 		 */
15198 		irb = ire->ire_bucket;
15199 		rw_enter(&irb->irb_lock, RW_READER);
15200 		ire_refrele(ire);
15201 
15202 		for (; ire != NULL; ire = ire->ire_next) {
15203 			if (ire->ire_addr != addr)
15204 				break;
15205 			if (ire_to_ill(ire) != ill)
15206 				continue;
15207 
15208 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15209 			ire->ire_marks &= ~IRE_MARK_NORECV;
15210 		}
15211 		rw_exit(&irb->irb_lock);
15212 	}
15213 }
15214 
15215 /*
15216  * This function must be called only after the broadcast ires
15217  * have been grouped together. For a given address addr, nominate
15218  * only one of the ires whose interface is not FAILED or OFFLINE.
15219  *
15220  * This is also called when an ipif goes down, so that we can nominate
15221  * a different ire with the same address for receiving.
15222  */
15223 static void
15224 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15225 {
15226 	irb_t *irb;
15227 	ire_t *ire;
15228 	ire_t *ire1;
15229 	ire_t *save_ire;
15230 	ire_t **irep = NULL;
15231 	boolean_t first = B_TRUE;
15232 	ire_t *clear_ire = NULL;
15233 	ire_t *start_ire = NULL;
15234 	ire_t	*new_lb_ire;
15235 	ire_t	*new_nlb_ire;
15236 	boolean_t new_lb_ire_used = B_FALSE;
15237 	boolean_t new_nlb_ire_used = B_FALSE;
15238 	uint64_t match_flags;
15239 	uint64_t phyi_flags;
15240 	boolean_t fallback = B_FALSE;
15241 	uint_t	max_frag;
15242 
15243 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15244 	    NULL, MATCH_IRE_TYPE, ipst);
15245 	/*
15246 	 * We may not be able to find some ires if a previous
15247 	 * ire_create failed. This happens when an ipif goes
15248 	 * down and we are unable to create BROADCAST ires due
15249 	 * to memory failure. Thus, we have to check for NULL
15250 	 * below. This should handle the case for LOOPBACK,
15251 	 * POINTOPOINT and interfaces with some POINTOPOINT
15252 	 * logicals for which there are no BROADCAST ires.
15253 	 */
15254 	if (ire == NULL)
15255 		return;
15256 	/*
15257 	 * Currently IRE_BROADCASTS are deleted when an ipif
15258 	 * goes down which runs exclusively. Thus, setting
15259 	 * IRE_MARK_RCVD should not race with ire_delete marking
15260 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15261 	 * be consistent with other parts of the code that walks
15262 	 * a given bucket.
15263 	 */
15264 	save_ire = ire;
15265 	irb = ire->ire_bucket;
15266 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15267 	if (new_lb_ire == NULL) {
15268 		ire_refrele(ire);
15269 		return;
15270 	}
15271 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15272 	if (new_nlb_ire == NULL) {
15273 		ire_refrele(ire);
15274 		kmem_cache_free(ire_cache, new_lb_ire);
15275 		return;
15276 	}
15277 	IRB_REFHOLD(irb);
15278 	rw_enter(&irb->irb_lock, RW_WRITER);
15279 	/*
15280 	 * Get to the first ire matching the address and the
15281 	 * group. If the address does not match we are done
15282 	 * as we could not find the IRE. If the address matches
15283 	 * we should get to the first one matching the group.
15284 	 */
15285 	while (ire != NULL) {
15286 		if (ire->ire_addr != addr ||
15287 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15288 			break;
15289 		}
15290 		ire = ire->ire_next;
15291 	}
15292 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15293 	start_ire = ire;
15294 redo:
15295 	while (ire != NULL && ire->ire_addr == addr &&
15296 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15297 		/*
15298 		 * The first ire for any address within a group
15299 		 * should always be the one with IRE_MARK_NORECV cleared
15300 		 * so that ip_wput_ire can avoid searching for one.
15301 		 * Note down the insertion point which will be used
15302 		 * later.
15303 		 */
15304 		if (first && (irep == NULL))
15305 			irep = ire->ire_ptpn;
15306 		/*
15307 		 * PHYI_FAILED is set when the interface fails.
15308 		 * This interface might have become good, but the
15309 		 * daemon has not yet detected. We should still
15310 		 * not receive on this. PHYI_OFFLINE should never
15311 		 * be picked as this has been offlined and soon
15312 		 * be removed.
15313 		 */
15314 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15315 		if (phyi_flags & PHYI_OFFLINE) {
15316 			ire->ire_marks |= IRE_MARK_NORECV;
15317 			ire = ire->ire_next;
15318 			continue;
15319 		}
15320 		if (phyi_flags & match_flags) {
15321 			ire->ire_marks |= IRE_MARK_NORECV;
15322 			ire = ire->ire_next;
15323 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15324 			    PHYI_INACTIVE) {
15325 				fallback = B_TRUE;
15326 			}
15327 			continue;
15328 		}
15329 		if (first) {
15330 			/*
15331 			 * We will move this to the front of the list later
15332 			 * on.
15333 			 */
15334 			clear_ire = ire;
15335 			ire->ire_marks &= ~IRE_MARK_NORECV;
15336 		} else {
15337 			ire->ire_marks |= IRE_MARK_NORECV;
15338 		}
15339 		first = B_FALSE;
15340 		ire = ire->ire_next;
15341 	}
15342 	/*
15343 	 * If we never nominated anybody, try nominating at least
15344 	 * an INACTIVE, if we found one. Do it only once though.
15345 	 */
15346 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15347 	    fallback) {
15348 		match_flags = PHYI_FAILED;
15349 		ire = start_ire;
15350 		irep = NULL;
15351 		goto redo;
15352 	}
15353 	ire_refrele(save_ire);
15354 
15355 	/*
15356 	 * irep non-NULL indicates that we entered the while loop
15357 	 * above. If clear_ire is at the insertion point, we don't
15358 	 * have to do anything. clear_ire will be NULL if all the
15359 	 * interfaces are failed.
15360 	 *
15361 	 * We cannot unlink and reinsert the ire at the right place
15362 	 * in the list since there can be other walkers of this bucket.
15363 	 * Instead we delete and recreate the ire
15364 	 */
15365 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15366 		ire_t *clear_ire_stq = NULL;
15367 
15368 		bzero(new_lb_ire, sizeof (ire_t));
15369 		/* XXX We need a recovery strategy here. */
15370 		if (ire_init(new_lb_ire,
15371 		    (uchar_t *)&clear_ire->ire_addr,
15372 		    (uchar_t *)&clear_ire->ire_mask,
15373 		    (uchar_t *)&clear_ire->ire_src_addr,
15374 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15375 		    &clear_ire->ire_max_frag,
15376 		    NULL, /* let ire_nce_init derive the resolver info */
15377 		    clear_ire->ire_rfq,
15378 		    clear_ire->ire_stq,
15379 		    clear_ire->ire_type,
15380 		    clear_ire->ire_ipif,
15381 		    clear_ire->ire_cmask,
15382 		    clear_ire->ire_phandle,
15383 		    clear_ire->ire_ihandle,
15384 		    clear_ire->ire_flags,
15385 		    &clear_ire->ire_uinfo,
15386 		    NULL,
15387 		    NULL,
15388 		    ipst) == NULL)
15389 			cmn_err(CE_PANIC, "ire_init() failed");
15390 		if (clear_ire->ire_stq == NULL) {
15391 			ire_t *ire_next = clear_ire->ire_next;
15392 			if (ire_next != NULL &&
15393 			    ire_next->ire_stq != NULL &&
15394 			    ire_next->ire_addr == clear_ire->ire_addr &&
15395 			    ire_next->ire_ipif->ipif_ill ==
15396 			    clear_ire->ire_ipif->ipif_ill) {
15397 				clear_ire_stq = ire_next;
15398 
15399 				bzero(new_nlb_ire, sizeof (ire_t));
15400 				/* XXX We need a recovery strategy here. */
15401 				if (ire_init(new_nlb_ire,
15402 				    (uchar_t *)&clear_ire_stq->ire_addr,
15403 				    (uchar_t *)&clear_ire_stq->ire_mask,
15404 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15405 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15406 				    &clear_ire_stq->ire_max_frag,
15407 				    NULL,
15408 				    clear_ire_stq->ire_rfq,
15409 				    clear_ire_stq->ire_stq,
15410 				    clear_ire_stq->ire_type,
15411 				    clear_ire_stq->ire_ipif,
15412 				    clear_ire_stq->ire_cmask,
15413 				    clear_ire_stq->ire_phandle,
15414 				    clear_ire_stq->ire_ihandle,
15415 				    clear_ire_stq->ire_flags,
15416 				    &clear_ire_stq->ire_uinfo,
15417 				    NULL,
15418 				    NULL,
15419 				    ipst) == NULL)
15420 					cmn_err(CE_PANIC, "ire_init() failed");
15421 			}
15422 		}
15423 
15424 		/*
15425 		 * Delete the ire. We can't call ire_delete() since
15426 		 * we are holding the bucket lock. We can't release the
15427 		 * bucket lock since we can't allow irep to change. So just
15428 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15429 		 * ire from the list and do the refrele.
15430 		 */
15431 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15432 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15433 
15434 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15435 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15436 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15437 		}
15438 
15439 		/*
15440 		 * Also take care of otherfields like ib/ob pkt count
15441 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15442 		 */
15443 
15444 		/* Set the max_frag before adding the ire */
15445 		max_frag = *new_lb_ire->ire_max_fragp;
15446 		new_lb_ire->ire_max_fragp = NULL;
15447 		new_lb_ire->ire_max_frag = max_frag;
15448 
15449 		/* Add the new ire's. Insert at *irep */
15450 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15451 		ire1 = *irep;
15452 		if (ire1 != NULL)
15453 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15454 		new_lb_ire->ire_next = ire1;
15455 		/* Link the new one in. */
15456 		new_lb_ire->ire_ptpn = irep;
15457 		membar_producer();
15458 		*irep = new_lb_ire;
15459 		new_lb_ire_used = B_TRUE;
15460 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15461 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15462 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15463 
15464 		if (clear_ire_stq != NULL) {
15465 			/* Set the max_frag before adding the ire */
15466 			max_frag = *new_nlb_ire->ire_max_fragp;
15467 			new_nlb_ire->ire_max_fragp = NULL;
15468 			new_nlb_ire->ire_max_frag = max_frag;
15469 
15470 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15471 			irep = &new_lb_ire->ire_next;
15472 			/* Add the new ire. Insert at *irep */
15473 			ire1 = *irep;
15474 			if (ire1 != NULL)
15475 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15476 			new_nlb_ire->ire_next = ire1;
15477 			/* Link the new one in. */
15478 			new_nlb_ire->ire_ptpn = irep;
15479 			membar_producer();
15480 			*irep = new_nlb_ire;
15481 			new_nlb_ire_used = B_TRUE;
15482 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15483 			    ire_stats_inserted);
15484 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15485 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15486 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15487 		}
15488 	}
15489 	rw_exit(&irb->irb_lock);
15490 	if (!new_lb_ire_used)
15491 		kmem_cache_free(ire_cache, new_lb_ire);
15492 	if (!new_nlb_ire_used)
15493 		kmem_cache_free(ire_cache, new_nlb_ire);
15494 	IRB_REFRELE(irb);
15495 }
15496 
15497 /*
15498  * Whenever an ipif goes down we have to renominate a different
15499  * broadcast ire to receive. Whenever an ipif comes up, we need
15500  * to make sure that we have only one nominated to receive.
15501  */
15502 static void
15503 ipif_renominate_bcast(ipif_t *ipif)
15504 {
15505 	ill_t *ill = ipif->ipif_ill;
15506 	ipaddr_t subnet_addr;
15507 	ipaddr_t net_addr;
15508 	ipaddr_t net_mask = 0;
15509 	ipaddr_t subnet_netmask;
15510 	ipaddr_t addr;
15511 	ill_group_t *illgrp;
15512 	ip_stack_t	*ipst = ill->ill_ipst;
15513 
15514 	illgrp = ill->ill_group;
15515 	/*
15516 	 * If this is the last ipif going down, it might take
15517 	 * the ill out of the group. In that case ipif_down ->
15518 	 * illgrp_delete takes care of doing the nomination.
15519 	 * ipif_down does not call for this case.
15520 	 */
15521 	ASSERT(illgrp != NULL);
15522 
15523 	/* There could not have been any ires associated with this */
15524 	if (ipif->ipif_subnet == 0)
15525 		return;
15526 
15527 	ill_mark_bcast(illgrp, 0, ipst);
15528 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15529 
15530 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15531 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15532 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15533 	} else {
15534 		net_mask = htonl(IN_CLASSA_NET);
15535 	}
15536 	addr = net_mask & ipif->ipif_subnet;
15537 	ill_mark_bcast(illgrp, addr, ipst);
15538 
15539 	net_addr = ~net_mask | addr;
15540 	ill_mark_bcast(illgrp, net_addr, ipst);
15541 
15542 	subnet_netmask = ipif->ipif_net_mask;
15543 	addr = ipif->ipif_subnet;
15544 	ill_mark_bcast(illgrp, addr, ipst);
15545 
15546 	subnet_addr = ~subnet_netmask | addr;
15547 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15548 }
15549 
15550 /*
15551  * Whenever we form or delete ill groups, we need to nominate one set of
15552  * BROADCAST ires for receiving in the group.
15553  *
15554  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15555  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15556  *    for ill_ipif_up_count to be non-zero. This is the only case where
15557  *    ill_ipif_up_count is zero and we would still find the ires.
15558  *
15559  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15560  *    ipif is UP and we just have to do the nomination.
15561  *
15562  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15563  *    from the group. So, we have to do the nomination.
15564  *
15565  * Because of (3), there could be just one ill in the group. But we have
15566  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15567  * Thus, this function does not optimize when there is only one ill as
15568  * it is not correct for (3).
15569  */
15570 static void
15571 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15572 {
15573 	ill_t *ill;
15574 	ipif_t *ipif;
15575 	ipaddr_t subnet_addr;
15576 	ipaddr_t prev_subnet_addr = 0;
15577 	ipaddr_t net_addr;
15578 	ipaddr_t prev_net_addr = 0;
15579 	ipaddr_t net_mask = 0;
15580 	ipaddr_t subnet_netmask;
15581 	ipaddr_t addr;
15582 	ip_stack_t	*ipst;
15583 
15584 	/*
15585 	 * When the last memeber is leaving, there is nothing to
15586 	 * nominate.
15587 	 */
15588 	if (illgrp->illgrp_ill_count == 0) {
15589 		ASSERT(illgrp->illgrp_ill == NULL);
15590 		return;
15591 	}
15592 
15593 	ill = illgrp->illgrp_ill;
15594 	ASSERT(!ill->ill_isv6);
15595 	ipst = ill->ill_ipst;
15596 	/*
15597 	 * We assume that ires with same address and belonging to the
15598 	 * same group, has been grouped together. Nominating a *single*
15599 	 * ill in the group for sending and receiving broadcast is done
15600 	 * by making sure that the first BROADCAST ire (which will be
15601 	 * the one returned by ire_ctable_lookup for ip_rput and the
15602 	 * one that will be used in ip_wput_ire) will be the one that
15603 	 * will not have IRE_MARK_NORECV set.
15604 	 *
15605 	 * 1) ip_rput checks and discards packets received on ires marked
15606 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15607 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15608 	 *    first ire in the group for every broadcast address in the group.
15609 	 *    ip_rput will accept packets only on the first ire i.e only
15610 	 *    one copy of the ill.
15611 	 *
15612 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15613 	 *    packet for the whole group. It needs to send out on the ill
15614 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15615 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15616 	 *    the copy echoed back on other port where the ire is not marked
15617 	 *    with IRE_MARK_NORECV.
15618 	 *
15619 	 * Note that we just need to have the first IRE either loopback or
15620 	 * non-loopback (either of them may not exist if ire_create failed
15621 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15622 	 * always hit the first one and hence will always accept one copy.
15623 	 *
15624 	 * We have a broadcast ire per ill for all the unique prefixes
15625 	 * hosted on that ill. As we don't have a way of knowing the
15626 	 * unique prefixes on a given ill and hence in the whole group,
15627 	 * we just call ill_mark_bcast on all the prefixes that exist
15628 	 * in the group. For the common case of one prefix, the code
15629 	 * below optimizes by remebering the last address used for
15630 	 * markng. In the case of multiple prefixes, this will still
15631 	 * optimize depending the order of prefixes.
15632 	 *
15633 	 * The only unique address across the whole group is 0.0.0.0 and
15634 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15635 	 * the first ire in the bucket for receiving and disables the
15636 	 * others.
15637 	 */
15638 	ill_mark_bcast(illgrp, 0, ipst);
15639 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15640 	for (; ill != NULL; ill = ill->ill_group_next) {
15641 
15642 		for (ipif = ill->ill_ipif; ipif != NULL;
15643 		    ipif = ipif->ipif_next) {
15644 
15645 			if (!(ipif->ipif_flags & IPIF_UP) ||
15646 			    ipif->ipif_subnet == 0) {
15647 				continue;
15648 			}
15649 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15650 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15651 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15652 			} else {
15653 				net_mask = htonl(IN_CLASSA_NET);
15654 			}
15655 			addr = net_mask & ipif->ipif_subnet;
15656 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15657 				ill_mark_bcast(illgrp, addr, ipst);
15658 				net_addr = ~net_mask | addr;
15659 				ill_mark_bcast(illgrp, net_addr, ipst);
15660 			}
15661 			prev_net_addr = addr;
15662 
15663 			subnet_netmask = ipif->ipif_net_mask;
15664 			addr = ipif->ipif_subnet;
15665 			if (prev_subnet_addr == 0 ||
15666 			    prev_subnet_addr != addr) {
15667 				ill_mark_bcast(illgrp, addr, ipst);
15668 				subnet_addr = ~subnet_netmask | addr;
15669 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15670 			}
15671 			prev_subnet_addr = addr;
15672 		}
15673 	}
15674 }
15675 
15676 /*
15677  * This function is called while forming ill groups.
15678  *
15679  * Currently, we handle only allmulti groups. We want to join
15680  * allmulti on only one of the ills in the groups. In future,
15681  * when we have link aggregation, we may have to join normal
15682  * multicast groups on multiple ills as switch does inbound load
15683  * balancing. Following are the functions that calls this
15684  * function :
15685  *
15686  * 1) ill_recover_multicast : Interface is coming back UP.
15687  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15688  *    will call ill_recover_multicast to recover all the multicast
15689  *    groups. We need to make sure that only one member is joined
15690  *    in the ill group.
15691  *
15692  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15693  *    Somebody is joining allmulti. We need to make sure that only one
15694  *    member is joined in the group.
15695  *
15696  * 3) illgrp_insert : If allmulti has already joined, we need to make
15697  *    sure that only one member is joined in the group.
15698  *
15699  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15700  *    allmulti who we have nominated. We need to pick someother ill.
15701  *
15702  * 5) illgrp_delete : The ill we nominated is leaving the group,
15703  *    we need to pick a new ill to join the group.
15704  *
15705  * For (1), (2), (5) - we just have to check whether there is
15706  * a good ill joined in the group. If we could not find any ills
15707  * joined the group, we should join.
15708  *
15709  * For (4), the one that was nominated to receive, left the group.
15710  * There could be nobody joined in the group when this function is
15711  * called.
15712  *
15713  * For (3) - we need to explicitly check whether there are multiple
15714  * ills joined in the group.
15715  *
15716  * For simplicity, we don't differentiate any of the above cases. We
15717  * just leave the group if it is joined on any of them and join on
15718  * the first good ill.
15719  */
15720 int
15721 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15722 {
15723 	ilm_t *ilm;
15724 	ill_t *ill;
15725 	ill_t *fallback_inactive_ill = NULL;
15726 	ill_t *fallback_failed_ill = NULL;
15727 	int ret = 0;
15728 
15729 	/*
15730 	 * Leave the allmulti on all the ills and start fresh.
15731 	 */
15732 	for (ill = illgrp->illgrp_ill; ill != NULL;
15733 	    ill = ill->ill_group_next) {
15734 		if (ill->ill_join_allmulti)
15735 			(void) ip_leave_allmulti(ill->ill_ipif);
15736 	}
15737 
15738 	/*
15739 	 * Choose a good ill. Fallback to inactive or failed if
15740 	 * none available. We need to fallback to FAILED in the
15741 	 * case where we have 2 interfaces in a group - where
15742 	 * one of them is failed and another is a good one and
15743 	 * the good one (not marked inactive) is leaving the group.
15744 	 */
15745 	ret = 0;
15746 	for (ill = illgrp->illgrp_ill; ill != NULL;
15747 	    ill = ill->ill_group_next) {
15748 		/* Never pick an offline interface */
15749 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15750 			continue;
15751 
15752 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15753 			fallback_failed_ill = ill;
15754 			continue;
15755 		}
15756 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15757 			fallback_inactive_ill = ill;
15758 			continue;
15759 		}
15760 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15761 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15762 				ret = ip_join_allmulti(ill->ill_ipif);
15763 				/*
15764 				 * ip_join_allmulti can fail because of memory
15765 				 * failures. So, make sure we join at least
15766 				 * on one ill.
15767 				 */
15768 				if (ill->ill_join_allmulti)
15769 					return (0);
15770 			}
15771 		}
15772 	}
15773 	if (ret != 0) {
15774 		/*
15775 		 * If we tried nominating above and failed to do so,
15776 		 * return error. We might have tried multiple times.
15777 		 * But, return the latest error.
15778 		 */
15779 		return (ret);
15780 	}
15781 	if ((ill = fallback_inactive_ill) != NULL) {
15782 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15783 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15784 				ret = ip_join_allmulti(ill->ill_ipif);
15785 				return (ret);
15786 			}
15787 		}
15788 	} else if ((ill = fallback_failed_ill) != NULL) {
15789 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15790 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15791 				ret = ip_join_allmulti(ill->ill_ipif);
15792 				return (ret);
15793 			}
15794 		}
15795 	}
15796 	return (0);
15797 }
15798 
15799 /*
15800  * This function is called from illgrp_delete after it is
15801  * deleted from the group to reschedule responsibilities
15802  * to a different ill.
15803  */
15804 static void
15805 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15806 {
15807 	ilm_t	*ilm;
15808 	ipif_t	*ipif;
15809 	ipaddr_t subnet_addr;
15810 	ipaddr_t net_addr;
15811 	ipaddr_t net_mask = 0;
15812 	ipaddr_t subnet_netmask;
15813 	ipaddr_t addr;
15814 	ip_stack_t *ipst = ill->ill_ipst;
15815 
15816 	ASSERT(ill->ill_group == NULL);
15817 	/*
15818 	 * Broadcast Responsibility:
15819 	 *
15820 	 * 1. If this ill has been nominated for receiving broadcast
15821 	 * packets, we need to find a new one. Before we find a new
15822 	 * one, we need to re-group the ires that are part of this new
15823 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15824 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15825 	 * thing for us.
15826 	 *
15827 	 * 2. If this ill was not nominated for receiving broadcast
15828 	 * packets, we need to clear the IRE_MARK_NORECV flag
15829 	 * so that we continue to send up broadcast packets.
15830 	 */
15831 	if (!ill->ill_isv6) {
15832 		/*
15833 		 * Case 1 above : No optimization here. Just redo the
15834 		 * nomination.
15835 		 */
15836 		ill_group_bcast_for_xmit(ill);
15837 		ill_nominate_bcast_rcv(illgrp);
15838 
15839 		/*
15840 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15841 		 */
15842 		ill_clear_bcast_mark(ill, 0);
15843 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15844 
15845 		for (ipif = ill->ill_ipif; ipif != NULL;
15846 		    ipif = ipif->ipif_next) {
15847 
15848 			if (!(ipif->ipif_flags & IPIF_UP) ||
15849 			    ipif->ipif_subnet == 0) {
15850 				continue;
15851 			}
15852 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15853 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15854 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15855 			} else {
15856 				net_mask = htonl(IN_CLASSA_NET);
15857 			}
15858 			addr = net_mask & ipif->ipif_subnet;
15859 			ill_clear_bcast_mark(ill, addr);
15860 
15861 			net_addr = ~net_mask | addr;
15862 			ill_clear_bcast_mark(ill, net_addr);
15863 
15864 			subnet_netmask = ipif->ipif_net_mask;
15865 			addr = ipif->ipif_subnet;
15866 			ill_clear_bcast_mark(ill, addr);
15867 
15868 			subnet_addr = ~subnet_netmask | addr;
15869 			ill_clear_bcast_mark(ill, subnet_addr);
15870 		}
15871 	}
15872 
15873 	/*
15874 	 * Multicast Responsibility.
15875 	 *
15876 	 * If we have joined allmulti on this one, find a new member
15877 	 * in the group to join allmulti. As this ill is already part
15878 	 * of allmulti, we don't have to join on this one.
15879 	 *
15880 	 * If we have not joined allmulti on this one, there is no
15881 	 * responsibility to handoff. But we need to take new
15882 	 * responsibility i.e, join allmulti on this one if we need
15883 	 * to.
15884 	 */
15885 	if (ill->ill_join_allmulti) {
15886 		(void) ill_nominate_mcast_rcv(illgrp);
15887 	} else {
15888 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15889 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15890 				(void) ip_join_allmulti(ill->ill_ipif);
15891 				break;
15892 			}
15893 		}
15894 	}
15895 
15896 	/*
15897 	 * We intentionally do the flushing of IRE_CACHES only matching
15898 	 * on the ill and not on groups. Note that we are already deleted
15899 	 * from the group.
15900 	 *
15901 	 * This will make sure that all IRE_CACHES whose stq is pointing
15902 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15903 	 * deleted and IRE_CACHES that are not pointing at this ill will
15904 	 * be left alone.
15905 	 */
15906 	if (ill->ill_isv6) {
15907 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15908 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15909 	} else {
15910 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15911 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15912 	}
15913 
15914 	/*
15915 	 * Some conn may have cached one of the IREs deleted above. By removing
15916 	 * the ire reference, we clean up the extra reference to the ill held in
15917 	 * ire->ire_stq.
15918 	 */
15919 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15920 
15921 	/*
15922 	 * Re-do source address selection for all the members in the
15923 	 * group, if they borrowed source address from one of the ipifs
15924 	 * in this ill.
15925 	 */
15926 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15927 		if (ill->ill_isv6) {
15928 			ipif_update_other_ipifs_v6(ipif, illgrp);
15929 		} else {
15930 			ipif_update_other_ipifs(ipif, illgrp);
15931 		}
15932 	}
15933 }
15934 
15935 /*
15936  * Delete the ill from the group. The caller makes sure that it is
15937  * in a group and it okay to delete from the group. So, we always
15938  * delete here.
15939  */
15940 static void
15941 illgrp_delete(ill_t *ill)
15942 {
15943 	ill_group_t *illgrp;
15944 	ill_group_t *tmpg;
15945 	ill_t *tmp_ill;
15946 	ip_stack_t	*ipst = ill->ill_ipst;
15947 
15948 	/*
15949 	 * Reset illgrp_ill_schednext if it was pointing at us.
15950 	 * We need to do this before we set ill_group to NULL.
15951 	 */
15952 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15953 	mutex_enter(&ill->ill_lock);
15954 
15955 	illgrp_reset_schednext(ill);
15956 
15957 	illgrp = ill->ill_group;
15958 
15959 	/* Delete the ill from illgrp. */
15960 	if (illgrp->illgrp_ill == ill) {
15961 		illgrp->illgrp_ill = ill->ill_group_next;
15962 	} else {
15963 		tmp_ill = illgrp->illgrp_ill;
15964 		while (tmp_ill->ill_group_next != ill) {
15965 			tmp_ill = tmp_ill->ill_group_next;
15966 			ASSERT(tmp_ill != NULL);
15967 		}
15968 		tmp_ill->ill_group_next = ill->ill_group_next;
15969 	}
15970 	ill->ill_group = NULL;
15971 	ill->ill_group_next = NULL;
15972 
15973 	illgrp->illgrp_ill_count--;
15974 	mutex_exit(&ill->ill_lock);
15975 	rw_exit(&ipst->ips_ill_g_lock);
15976 
15977 	/*
15978 	 * As this ill is leaving the group, we need to hand off
15979 	 * the responsibilities to the other ills in the group, if
15980 	 * this ill had some responsibilities.
15981 	 */
15982 
15983 	ill_handoff_responsibility(ill, illgrp);
15984 
15985 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15986 
15987 	if (illgrp->illgrp_ill_count == 0) {
15988 
15989 		ASSERT(illgrp->illgrp_ill == NULL);
15990 		if (ill->ill_isv6) {
15991 			if (illgrp == ipst->ips_illgrp_head_v6) {
15992 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15993 			} else {
15994 				tmpg = ipst->ips_illgrp_head_v6;
15995 				while (tmpg->illgrp_next != illgrp) {
15996 					tmpg = tmpg->illgrp_next;
15997 					ASSERT(tmpg != NULL);
15998 				}
15999 				tmpg->illgrp_next = illgrp->illgrp_next;
16000 			}
16001 		} else {
16002 			if (illgrp == ipst->ips_illgrp_head_v4) {
16003 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
16004 			} else {
16005 				tmpg = ipst->ips_illgrp_head_v4;
16006 				while (tmpg->illgrp_next != illgrp) {
16007 					tmpg = tmpg->illgrp_next;
16008 					ASSERT(tmpg != NULL);
16009 				}
16010 				tmpg->illgrp_next = illgrp->illgrp_next;
16011 			}
16012 		}
16013 		mutex_destroy(&illgrp->illgrp_lock);
16014 		mi_free(illgrp);
16015 	}
16016 	rw_exit(&ipst->ips_ill_g_lock);
16017 
16018 	/*
16019 	 * Even though the ill is out of the group its not necessary
16020 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
16021 	 * We will split the ipsq when phyint_groupname is set to NULL.
16022 	 */
16023 
16024 	/*
16025 	 * Send a routing sockets message if we are deleting from
16026 	 * groups with names.
16027 	 */
16028 	if (ill->ill_phyint->phyint_groupname_len != 0)
16029 		ip_rts_ifmsg(ill->ill_ipif);
16030 }
16031 
16032 /*
16033  * Re-do source address selection. This is normally called when
16034  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
16035  * ipif comes up.
16036  */
16037 void
16038 ill_update_source_selection(ill_t *ill)
16039 {
16040 	ipif_t *ipif;
16041 
16042 	ASSERT(IAM_WRITER_ILL(ill));
16043 
16044 	if (ill->ill_group != NULL)
16045 		ill = ill->ill_group->illgrp_ill;
16046 
16047 	for (; ill != NULL; ill = ill->ill_group_next) {
16048 		for (ipif = ill->ill_ipif; ipif != NULL;
16049 		    ipif = ipif->ipif_next) {
16050 			if (ill->ill_isv6)
16051 				ipif_recreate_interface_routes_v6(NULL, ipif);
16052 			else
16053 				ipif_recreate_interface_routes(NULL, ipif);
16054 		}
16055 	}
16056 }
16057 
16058 /*
16059  * Insert ill in a group headed by illgrp_head. The caller can either
16060  * pass a groupname in which case we search for a group with the
16061  * same name to insert in or pass a group to insert in. This function
16062  * would only search groups with names.
16063  *
16064  * NOTE : The caller should make sure that there is at least one ipif
16065  *	  UP on this ill so that illgrp_scheduler can pick this ill
16066  *	  for outbound packets. If ill_ipif_up_count is zero, we have
16067  *	  already sent a DL_UNBIND to the driver and we don't want to
16068  *	  send anymore packets. We don't assert for ipif_up_count
16069  *	  to be greater than zero, because ipif_up_done wants to call
16070  *	  this function before bumping up the ipif_up_count. See
16071  *	  ipif_up_done() for details.
16072  */
16073 int
16074 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
16075     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
16076 {
16077 	ill_group_t *illgrp;
16078 	ill_t *prev_ill;
16079 	phyint_t *phyi;
16080 	ip_stack_t	*ipst = ill->ill_ipst;
16081 
16082 	ASSERT(ill->ill_group == NULL);
16083 
16084 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16085 	mutex_enter(&ill->ill_lock);
16086 
16087 	if (groupname != NULL) {
16088 		/*
16089 		 * Look for a group with a matching groupname to insert.
16090 		 */
16091 		for (illgrp = *illgrp_head; illgrp != NULL;
16092 		    illgrp = illgrp->illgrp_next) {
16093 
16094 			ill_t *tmp_ill;
16095 
16096 			/*
16097 			 * If we have an ill_group_t in the list which has
16098 			 * no ill_t assigned then we must be in the process of
16099 			 * removing this group. We skip this as illgrp_delete()
16100 			 * will remove it from the list.
16101 			 */
16102 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
16103 				ASSERT(illgrp->illgrp_ill_count == 0);
16104 				continue;
16105 			}
16106 
16107 			ASSERT(tmp_ill->ill_phyint != NULL);
16108 			phyi = tmp_ill->ill_phyint;
16109 			/*
16110 			 * Look at groups which has names only.
16111 			 */
16112 			if (phyi->phyint_groupname_len == 0)
16113 				continue;
16114 			/*
16115 			 * Names are stored in the phyint common to both
16116 			 * IPv4 and IPv6.
16117 			 */
16118 			if (mi_strcmp(phyi->phyint_groupname,
16119 			    groupname) == 0) {
16120 				break;
16121 			}
16122 		}
16123 	} else {
16124 		/*
16125 		 * If the caller passes in a NULL "grp_to_insert", we
16126 		 * allocate one below and insert this singleton.
16127 		 */
16128 		illgrp = grp_to_insert;
16129 	}
16130 
16131 	ill->ill_group_next = NULL;
16132 
16133 	if (illgrp == NULL) {
16134 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16135 		if (illgrp == NULL) {
16136 			return (ENOMEM);
16137 		}
16138 		illgrp->illgrp_next = *illgrp_head;
16139 		*illgrp_head = illgrp;
16140 		illgrp->illgrp_ill = ill;
16141 		illgrp->illgrp_ill_count = 1;
16142 		ill->ill_group = illgrp;
16143 		/*
16144 		 * Used in illgrp_scheduler to protect multiple threads
16145 		 * from traversing the list.
16146 		 */
16147 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16148 	} else {
16149 		ASSERT(ill->ill_net_type ==
16150 		    illgrp->illgrp_ill->ill_net_type);
16151 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16152 
16153 		/* Insert ill at tail of this group */
16154 		prev_ill = illgrp->illgrp_ill;
16155 		while (prev_ill->ill_group_next != NULL)
16156 			prev_ill = prev_ill->ill_group_next;
16157 		prev_ill->ill_group_next = ill;
16158 		ill->ill_group = illgrp;
16159 		illgrp->illgrp_ill_count++;
16160 		/*
16161 		 * Inherit group properties. Currently only forwarding
16162 		 * is the property we try to keep the same with all the
16163 		 * ills. When there are more, we will abstract this into
16164 		 * a function.
16165 		 */
16166 		ill->ill_flags &= ~ILLF_ROUTER;
16167 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16168 	}
16169 	mutex_exit(&ill->ill_lock);
16170 	rw_exit(&ipst->ips_ill_g_lock);
16171 
16172 	/*
16173 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16174 	 *    may be zero as it has not yet been bumped. But the ires
16175 	 *    have already been added. So, we do the nomination here
16176 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16177 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16178 	 *    ill_ipif_up_count here while nominating broadcast ires for
16179 	 *    receive.
16180 	 *
16181 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16182 	 *    to group them properly as ire_add() has already happened
16183 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16184 	 *    case, we need to do it here anyway.
16185 	 */
16186 	if (!ill->ill_isv6) {
16187 		ill_group_bcast_for_xmit(ill);
16188 		ill_nominate_bcast_rcv(illgrp);
16189 	}
16190 
16191 	if (!ipif_is_coming_up) {
16192 		/*
16193 		 * When ipif_up_done() calls this function, the multicast
16194 		 * groups have not been joined yet. So, there is no point in
16195 		 * nomination. ip_join_allmulti will handle groups when
16196 		 * ill_recover_multicast is called from ipif_up_done() later.
16197 		 */
16198 		(void) ill_nominate_mcast_rcv(illgrp);
16199 		/*
16200 		 * ipif_up_done calls ill_update_source_selection
16201 		 * anyway. Moreover, we don't want to re-create
16202 		 * interface routes while ipif_up_done() still has reference
16203 		 * to them. Refer to ipif_up_done() for more details.
16204 		 */
16205 		ill_update_source_selection(ill);
16206 	}
16207 
16208 	/*
16209 	 * Send a routing sockets message if we are inserting into
16210 	 * groups with names.
16211 	 */
16212 	if (groupname != NULL)
16213 		ip_rts_ifmsg(ill->ill_ipif);
16214 	return (0);
16215 }
16216 
16217 /*
16218  * Return the first phyint matching the groupname. There could
16219  * be more than one when there are ill groups.
16220  *
16221  * If 'usable' is set, then we exclude ones that are marked with any of
16222  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16223  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16224  * emulation of ipmp.
16225  */
16226 phyint_t *
16227 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16228 {
16229 	phyint_t *phyi;
16230 
16231 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16232 	/*
16233 	 * Group names are stored in the phyint - a common structure
16234 	 * to both IPv4 and IPv6.
16235 	 */
16236 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16237 	for (; phyi != NULL;
16238 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16239 	    phyi, AVL_AFTER)) {
16240 		if (phyi->phyint_groupname_len == 0)
16241 			continue;
16242 		/*
16243 		 * Skip the ones that should not be used since the callers
16244 		 * sometime use this for sending packets.
16245 		 */
16246 		if (usable && (phyi->phyint_flags &
16247 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16248 			continue;
16249 
16250 		ASSERT(phyi->phyint_groupname != NULL);
16251 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16252 			return (phyi);
16253 	}
16254 	return (NULL);
16255 }
16256 
16257 
16258 /*
16259  * Return the first usable phyint matching the group index. By 'usable'
16260  * we exclude ones that are marked ununsable with any of
16261  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16262  *
16263  * Used only for the ipmp/netinfo emulation of ipmp.
16264  */
16265 phyint_t *
16266 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16267 {
16268 	phyint_t *phyi;
16269 
16270 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16271 
16272 	if (!ipst->ips_ipmp_hook_emulation)
16273 		return (NULL);
16274 
16275 	/*
16276 	 * Group indicies are stored in the phyint - a common structure
16277 	 * to both IPv4 and IPv6.
16278 	 */
16279 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16280 	for (; phyi != NULL;
16281 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16282 	    phyi, AVL_AFTER)) {
16283 		/* Ignore the ones that do not have a group */
16284 		if (phyi->phyint_groupname_len == 0)
16285 			continue;
16286 
16287 		ASSERT(phyi->phyint_group_ifindex != 0);
16288 		/*
16289 		 * Skip the ones that should not be used since the callers
16290 		 * sometime use this for sending packets.
16291 		 */
16292 		if (phyi->phyint_flags &
16293 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16294 			continue;
16295 		if (phyi->phyint_group_ifindex == group_ifindex)
16296 			return (phyi);
16297 	}
16298 	return (NULL);
16299 }
16300 
16301 
16302 /*
16303  * MT notes on creation and deletion of IPMP groups
16304  *
16305  * Creation and deletion of IPMP groups introduce the need to merge or
16306  * split the associated serialization objects i.e the ipsq's. Normally all
16307  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16308  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16309  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16310  * is a need to change the <ill-ipsq> association and we have to operate on both
16311  * the source and destination IPMP groups. For eg. attempting to set the
16312  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16313  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16314  * source or destination IPMP group are mapped to a single ipsq for executing
16315  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16316  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16317  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16318  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16319  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16320  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16321  *
16322  * In the above example the ioctl handling code locates the current ipsq of hme0
16323  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16324  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16325  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16326  * the destination ipsq. If the destination ipsq is not busy, it also enters
16327  * the destination ipsq exclusively. Now the actual groupname setting operation
16328  * can proceed. If the destination ipsq is busy, the operation is enqueued
16329  * on the destination (merged) ipsq and will be handled in the unwind from
16330  * ipsq_exit.
16331  *
16332  * To prevent other threads accessing the ill while the group name change is
16333  * in progres, we bring down the ipifs which also removes the ill from the
16334  * group. The group is changed in phyint and when the first ipif on the ill
16335  * is brought up, the ill is inserted into the right IPMP group by
16336  * illgrp_insert.
16337  */
16338 /* ARGSUSED */
16339 int
16340 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16341     ip_ioctl_cmd_t *ipip, void *ifreq)
16342 {
16343 	int i;
16344 	char *tmp;
16345 	int namelen;
16346 	ill_t *ill = ipif->ipif_ill;
16347 	ill_t *ill_v4, *ill_v6;
16348 	int err = 0;
16349 	phyint_t *phyi;
16350 	phyint_t *phyi_tmp;
16351 	struct lifreq *lifr;
16352 	mblk_t	*mp1;
16353 	char *groupname;
16354 	ipsq_t *ipsq;
16355 	ip_stack_t	*ipst = ill->ill_ipst;
16356 
16357 	ASSERT(IAM_WRITER_IPIF(ipif));
16358 
16359 	/* Existance verified in ip_wput_nondata */
16360 	mp1 = mp->b_cont->b_cont;
16361 	lifr = (struct lifreq *)mp1->b_rptr;
16362 	groupname = lifr->lifr_groupname;
16363 
16364 	if (ipif->ipif_id != 0)
16365 		return (EINVAL);
16366 
16367 	phyi = ill->ill_phyint;
16368 	ASSERT(phyi != NULL);
16369 
16370 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16371 		return (EINVAL);
16372 
16373 	tmp = groupname;
16374 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16375 		;
16376 
16377 	if (i == LIFNAMSIZ) {
16378 		/* no null termination */
16379 		return (EINVAL);
16380 	}
16381 
16382 	/*
16383 	 * Calculate the namelen exclusive of the null
16384 	 * termination character.
16385 	 */
16386 	namelen = tmp - groupname;
16387 
16388 	ill_v4 = phyi->phyint_illv4;
16389 	ill_v6 = phyi->phyint_illv6;
16390 
16391 	/*
16392 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16393 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16394 	 * synchronization notes in ip.c
16395 	 */
16396 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16397 		return (EINVAL);
16398 	}
16399 
16400 	/*
16401 	 * mark the ill as changing.
16402 	 * this should queue all new requests on the syncq.
16403 	 */
16404 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16405 
16406 	if (ill_v4 != NULL)
16407 		ill_v4->ill_state_flags |= ILL_CHANGING;
16408 	if (ill_v6 != NULL)
16409 		ill_v6->ill_state_flags |= ILL_CHANGING;
16410 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16411 
16412 	if (namelen == 0) {
16413 		/*
16414 		 * Null string means remove this interface from the
16415 		 * existing group.
16416 		 */
16417 		if (phyi->phyint_groupname_len == 0) {
16418 			/*
16419 			 * Never was in a group.
16420 			 */
16421 			err = 0;
16422 			goto done;
16423 		}
16424 
16425 		/*
16426 		 * IPv4 or IPv6 may be temporarily out of the group when all
16427 		 * the ipifs are down. Thus, we need to check for ill_group to
16428 		 * be non-NULL.
16429 		 */
16430 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16431 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16432 			mutex_enter(&ill_v4->ill_lock);
16433 			if (!ill_is_quiescent(ill_v4)) {
16434 				/*
16435 				 * ipsq_pending_mp_add will not fail since
16436 				 * connp is NULL
16437 				 */
16438 				(void) ipsq_pending_mp_add(NULL,
16439 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16440 				mutex_exit(&ill_v4->ill_lock);
16441 				err = EINPROGRESS;
16442 				goto done;
16443 			}
16444 			mutex_exit(&ill_v4->ill_lock);
16445 		}
16446 
16447 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16448 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16449 			mutex_enter(&ill_v6->ill_lock);
16450 			if (!ill_is_quiescent(ill_v6)) {
16451 				(void) ipsq_pending_mp_add(NULL,
16452 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16453 				mutex_exit(&ill_v6->ill_lock);
16454 				err = EINPROGRESS;
16455 				goto done;
16456 			}
16457 			mutex_exit(&ill_v6->ill_lock);
16458 		}
16459 
16460 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16461 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16462 		mutex_enter(&phyi->phyint_lock);
16463 		ASSERT(phyi->phyint_groupname != NULL);
16464 		mi_free(phyi->phyint_groupname);
16465 		phyi->phyint_groupname = NULL;
16466 		phyi->phyint_groupname_len = 0;
16467 
16468 		/* Restore the ifindex used to be the per interface one */
16469 		phyi->phyint_group_ifindex = 0;
16470 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16471 		mutex_exit(&phyi->phyint_lock);
16472 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16473 		rw_exit(&ipst->ips_ill_g_lock);
16474 		err = ill_up_ipifs(ill, q, mp);
16475 
16476 		/*
16477 		 * set the split flag so that the ipsq can be split
16478 		 */
16479 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16480 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16481 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16482 
16483 	} else {
16484 		if (phyi->phyint_groupname_len != 0) {
16485 			ASSERT(phyi->phyint_groupname != NULL);
16486 			/* Are we inserting in the same group ? */
16487 			if (mi_strcmp(groupname,
16488 			    phyi->phyint_groupname) == 0) {
16489 				err = 0;
16490 				goto done;
16491 			}
16492 		}
16493 
16494 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16495 		/*
16496 		 * Merge ipsq for the group's.
16497 		 * This check is here as multiple groups/ills might be
16498 		 * sharing the same ipsq.
16499 		 * If we have to merege than the operation is restarted
16500 		 * on the new ipsq.
16501 		 */
16502 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16503 		if (phyi->phyint_ipsq != ipsq) {
16504 			rw_exit(&ipst->ips_ill_g_lock);
16505 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16506 			goto done;
16507 		}
16508 		/*
16509 		 * Running exclusive on new ipsq.
16510 		 */
16511 
16512 		ASSERT(ipsq != NULL);
16513 		ASSERT(ipsq->ipsq_writer == curthread);
16514 
16515 		/*
16516 		 * Check whether the ill_type and ill_net_type matches before
16517 		 * we allocate any memory so that the cleanup is easier.
16518 		 *
16519 		 * We can't group dissimilar ones as we can't load spread
16520 		 * packets across the group because of potential link-level
16521 		 * header differences.
16522 		 */
16523 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16524 		if (phyi_tmp != NULL) {
16525 			if ((ill_v4 != NULL &&
16526 			    phyi_tmp->phyint_illv4 != NULL) &&
16527 			    ((ill_v4->ill_net_type !=
16528 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16529 			    (ill_v4->ill_type !=
16530 			    phyi_tmp->phyint_illv4->ill_type))) {
16531 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16532 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16533 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16534 				rw_exit(&ipst->ips_ill_g_lock);
16535 				return (EINVAL);
16536 			}
16537 			if ((ill_v6 != NULL &&
16538 			    phyi_tmp->phyint_illv6 != NULL) &&
16539 			    ((ill_v6->ill_net_type !=
16540 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16541 			    (ill_v6->ill_type !=
16542 			    phyi_tmp->phyint_illv6->ill_type))) {
16543 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16544 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16545 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16546 				rw_exit(&ipst->ips_ill_g_lock);
16547 				return (EINVAL);
16548 			}
16549 		}
16550 
16551 		rw_exit(&ipst->ips_ill_g_lock);
16552 
16553 		/*
16554 		 * bring down all v4 ipifs.
16555 		 */
16556 		if (ill_v4 != NULL) {
16557 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16558 		}
16559 
16560 		/*
16561 		 * bring down all v6 ipifs.
16562 		 */
16563 		if (ill_v6 != NULL) {
16564 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16565 		}
16566 
16567 		/*
16568 		 * make sure all ipifs are down and there are no active
16569 		 * references. Call to ipsq_pending_mp_add will not fail
16570 		 * since connp is NULL.
16571 		 */
16572 		if (ill_v4 != NULL) {
16573 			mutex_enter(&ill_v4->ill_lock);
16574 			if (!ill_is_quiescent(ill_v4)) {
16575 				(void) ipsq_pending_mp_add(NULL,
16576 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16577 				mutex_exit(&ill_v4->ill_lock);
16578 				err = EINPROGRESS;
16579 				goto done;
16580 			}
16581 			mutex_exit(&ill_v4->ill_lock);
16582 		}
16583 
16584 		if (ill_v6 != NULL) {
16585 			mutex_enter(&ill_v6->ill_lock);
16586 			if (!ill_is_quiescent(ill_v6)) {
16587 				(void) ipsq_pending_mp_add(NULL,
16588 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16589 				mutex_exit(&ill_v6->ill_lock);
16590 				err = EINPROGRESS;
16591 				goto done;
16592 			}
16593 			mutex_exit(&ill_v6->ill_lock);
16594 		}
16595 
16596 		/*
16597 		 * allocate including space for null terminator
16598 		 * before we insert.
16599 		 */
16600 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16601 		if (tmp == NULL)
16602 			return (ENOMEM);
16603 
16604 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16605 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16606 		mutex_enter(&phyi->phyint_lock);
16607 		if (phyi->phyint_groupname_len != 0) {
16608 			ASSERT(phyi->phyint_groupname != NULL);
16609 			mi_free(phyi->phyint_groupname);
16610 		}
16611 
16612 		/*
16613 		 * setup the new group name.
16614 		 */
16615 		phyi->phyint_groupname = tmp;
16616 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16617 		phyi->phyint_groupname_len = namelen + 1;
16618 
16619 		if (ipst->ips_ipmp_hook_emulation) {
16620 			/*
16621 			 * If the group already exists we use the existing
16622 			 * group_ifindex, otherwise we pick a new index here.
16623 			 */
16624 			if (phyi_tmp != NULL) {
16625 				phyi->phyint_group_ifindex =
16626 				    phyi_tmp->phyint_group_ifindex;
16627 			} else {
16628 				/* XXX We need a recovery strategy here. */
16629 				if (!ip_assign_ifindex(
16630 				    &phyi->phyint_group_ifindex, ipst))
16631 					cmn_err(CE_PANIC,
16632 					    "ip_assign_ifindex() failed");
16633 			}
16634 		}
16635 		/*
16636 		 * Select whether the netinfo and hook use the per-interface
16637 		 * or per-group ifindex.
16638 		 */
16639 		if (ipst->ips_ipmp_hook_emulation)
16640 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16641 		else
16642 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16643 
16644 		if (ipst->ips_ipmp_hook_emulation &&
16645 		    phyi_tmp != NULL) {
16646 			/* First phyint in group - group PLUMB event */
16647 			ill_nic_info_plumb(ill, B_TRUE);
16648 		}
16649 		mutex_exit(&phyi->phyint_lock);
16650 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16651 		rw_exit(&ipst->ips_ill_g_lock);
16652 
16653 		err = ill_up_ipifs(ill, q, mp);
16654 	}
16655 
16656 done:
16657 	/*
16658 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16659 	 */
16660 	if (err != EINPROGRESS) {
16661 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16662 		if (ill_v4 != NULL)
16663 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16664 		if (ill_v6 != NULL)
16665 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16666 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16667 	}
16668 	return (err);
16669 }
16670 
16671 /* ARGSUSED */
16672 int
16673 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16674     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16675 {
16676 	ill_t *ill;
16677 	phyint_t *phyi;
16678 	struct lifreq *lifr;
16679 	mblk_t	*mp1;
16680 
16681 	/* Existence verified in ip_wput_nondata */
16682 	mp1 = mp->b_cont->b_cont;
16683 	lifr = (struct lifreq *)mp1->b_rptr;
16684 	ill = ipif->ipif_ill;
16685 	phyi = ill->ill_phyint;
16686 
16687 	lifr->lifr_groupname[0] = '\0';
16688 	/*
16689 	 * ill_group may be null if all the interfaces
16690 	 * are down. But still, the phyint should always
16691 	 * hold the name.
16692 	 */
16693 	if (phyi->phyint_groupname_len != 0) {
16694 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16695 		    phyi->phyint_groupname_len);
16696 	}
16697 
16698 	return (0);
16699 }
16700 
16701 
16702 typedef struct conn_move_s {
16703 	ill_t	*cm_from_ill;
16704 	ill_t	*cm_to_ill;
16705 	int	cm_ifindex;
16706 } conn_move_t;
16707 
16708 /*
16709  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16710  */
16711 static void
16712 conn_move(conn_t *connp, caddr_t arg)
16713 {
16714 	conn_move_t *connm;
16715 	int ifindex;
16716 	int i;
16717 	ill_t *from_ill;
16718 	ill_t *to_ill;
16719 	ilg_t *ilg;
16720 	ilm_t *ret_ilm;
16721 
16722 	connm = (conn_move_t *)arg;
16723 	ifindex = connm->cm_ifindex;
16724 	from_ill = connm->cm_from_ill;
16725 	to_ill = connm->cm_to_ill;
16726 
16727 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16728 
16729 	/* All multicast fields protected by conn_lock */
16730 	mutex_enter(&connp->conn_lock);
16731 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16732 	if ((connp->conn_outgoing_ill == from_ill) &&
16733 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16734 		connp->conn_outgoing_ill = to_ill;
16735 		connp->conn_incoming_ill = to_ill;
16736 	}
16737 
16738 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16739 
16740 	if ((connp->conn_multicast_ill == from_ill) &&
16741 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16742 		connp->conn_multicast_ill = connm->cm_to_ill;
16743 	}
16744 
16745 	/* Change IP_XMIT_IF associations */
16746 	if ((connp->conn_xmit_if_ill == from_ill) &&
16747 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
16748 		connp->conn_xmit_if_ill = to_ill;
16749 	}
16750 	/*
16751 	 * Change the ilg_ill to point to the new one. This assumes
16752 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16753 	 * has been told to receive packets on this interface.
16754 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16755 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16756 	 * some ilms may not have moved. We check to see whether
16757 	 * the ilms have moved to to_ill. We can't check on from_ill
16758 	 * as in the process of moving, we could have split an ilm
16759 	 * in to two - which has the same orig_ifindex and v6group.
16760 	 *
16761 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16762 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16763 	 */
16764 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16765 		ilg = &connp->conn_ilg[i];
16766 		if ((ilg->ilg_ill == from_ill) &&
16767 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16768 			/* ifindex != 0 indicates failback */
16769 			if (ifindex != 0) {
16770 				connp->conn_ilg[i].ilg_ill = to_ill;
16771 				continue;
16772 			}
16773 
16774 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16775 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16776 			    connp->conn_zoneid);
16777 
16778 			if (ret_ilm != NULL)
16779 				connp->conn_ilg[i].ilg_ill = to_ill;
16780 		}
16781 	}
16782 	mutex_exit(&connp->conn_lock);
16783 }
16784 
16785 static void
16786 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16787 {
16788 	conn_move_t connm;
16789 	ip_stack_t	*ipst = from_ill->ill_ipst;
16790 
16791 	connm.cm_from_ill = from_ill;
16792 	connm.cm_to_ill = to_ill;
16793 	connm.cm_ifindex = ifindex;
16794 
16795 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16796 }
16797 
16798 /*
16799  * ilm has been moved from from_ill to to_ill.
16800  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16801  * appropriately.
16802  *
16803  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16804  *	  the code there de-references ipif_ill to get the ill to
16805  *	  send multicast requests. It does not work as ipif is on its
16806  *	  move and already moved when this function is called.
16807  *	  Thus, we need to use from_ill and to_ill send down multicast
16808  *	  requests.
16809  */
16810 static void
16811 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16812 {
16813 	ipif_t *ipif;
16814 	ilm_t *ilm;
16815 
16816 	/*
16817 	 * See whether we need to send down DL_ENABMULTI_REQ on
16818 	 * to_ill as ilm has just been added.
16819 	 */
16820 	ASSERT(IAM_WRITER_ILL(to_ill));
16821 	ASSERT(IAM_WRITER_ILL(from_ill));
16822 
16823 	ILM_WALKER_HOLD(to_ill);
16824 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16825 
16826 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16827 			continue;
16828 		/*
16829 		 * no locks held, ill/ipif cannot dissappear as long
16830 		 * as we are writer.
16831 		 */
16832 		ipif = to_ill->ill_ipif;
16833 		/*
16834 		 * No need to hold any lock as we are the writer and this
16835 		 * can only be changed by a writer.
16836 		 */
16837 		ilm->ilm_is_new = B_FALSE;
16838 
16839 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16840 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16841 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16842 			    "resolver\n"));
16843 			continue;		/* Must be IRE_IF_NORESOLVER */
16844 		}
16845 
16846 
16847 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16848 			ip1dbg(("ilm_send_multicast_reqs: "
16849 			    "to_ill MULTI_BCAST\n"));
16850 			goto from;
16851 		}
16852 
16853 		if (to_ill->ill_isv6)
16854 			mld_joingroup(ilm);
16855 		else
16856 			igmp_joingroup(ilm);
16857 
16858 		if (to_ill->ill_ipif_up_count == 0) {
16859 			/*
16860 			 * Nobody there. All multicast addresses will be
16861 			 * re-joined when we get the DL_BIND_ACK bringing the
16862 			 * interface up.
16863 			 */
16864 			ilm->ilm_notify_driver = B_FALSE;
16865 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16866 			goto from;
16867 		}
16868 
16869 		/*
16870 		 * For allmulti address, we want to join on only one interface.
16871 		 * Checking for ilm_numentries_v6 is not correct as you may
16872 		 * find an ilm with zero address on to_ill, but we may not
16873 		 * have nominated to_ill for receiving. Thus, if we have
16874 		 * nominated from_ill (ill_join_allmulti is set), nominate
16875 		 * only if to_ill is not already nominated (to_ill normally
16876 		 * should not have been nominated if "from_ill" has already
16877 		 * been nominated. As we don't prevent failovers from happening
16878 		 * across groups, we don't assert).
16879 		 */
16880 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16881 			/*
16882 			 * There is no need to hold ill locks as we are
16883 			 * writer on both ills and when ill_join_allmulti
16884 			 * is changed the thread is always a writer.
16885 			 */
16886 			if (from_ill->ill_join_allmulti &&
16887 			    !to_ill->ill_join_allmulti) {
16888 				(void) ip_join_allmulti(to_ill->ill_ipif);
16889 			}
16890 		} else if (ilm->ilm_notify_driver) {
16891 
16892 			/*
16893 			 * This is a newly moved ilm so we need to tell the
16894 			 * driver about the new group. There can be more than
16895 			 * one ilm's for the same group in the list each with a
16896 			 * different orig_ifindex. We have to inform the driver
16897 			 * once. In ilm_move_v[4,6] we only set the flag
16898 			 * ilm_notify_driver for the first ilm.
16899 			 */
16900 
16901 			(void) ip_ll_send_enabmulti_req(to_ill,
16902 			    &ilm->ilm_v6addr);
16903 		}
16904 
16905 		ilm->ilm_notify_driver = B_FALSE;
16906 
16907 		/*
16908 		 * See whether we need to send down DL_DISABMULTI_REQ on
16909 		 * from_ill as ilm has just been removed.
16910 		 */
16911 from:
16912 		ipif = from_ill->ill_ipif;
16913 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16914 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16915 			ip1dbg(("ilm_send_multicast_reqs: "
16916 			    "from_ill not resolver\n"));
16917 			continue;		/* Must be IRE_IF_NORESOLVER */
16918 		}
16919 
16920 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16921 			ip1dbg(("ilm_send_multicast_reqs: "
16922 			    "from_ill MULTI_BCAST\n"));
16923 			continue;
16924 		}
16925 
16926 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16927 			if (from_ill->ill_join_allmulti)
16928 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16929 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16930 			(void) ip_ll_send_disabmulti_req(from_ill,
16931 			    &ilm->ilm_v6addr);
16932 		}
16933 	}
16934 	ILM_WALKER_RELE(to_ill);
16935 }
16936 
16937 /*
16938  * This function is called when all multicast memberships needs
16939  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16940  * called only once unlike the IPv4 counterpart where it is called after
16941  * every logical interface is moved. The reason is due to multicast
16942  * memberships are joined using an interface address in IPv4 while in
16943  * IPv6, interface index is used.
16944  */
16945 static void
16946 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16947 {
16948 	ilm_t	*ilm;
16949 	ilm_t	*ilm_next;
16950 	ilm_t	*new_ilm;
16951 	ilm_t	**ilmp;
16952 	int	count;
16953 	char buf[INET6_ADDRSTRLEN];
16954 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16955 	ip_stack_t	*ipst = from_ill->ill_ipst;
16956 
16957 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16958 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16959 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16960 
16961 	if (ifindex == 0) {
16962 		/*
16963 		 * Form the solicited node mcast address which is used later.
16964 		 */
16965 		ipif_t *ipif;
16966 
16967 		ipif = from_ill->ill_ipif;
16968 		ASSERT(ipif->ipif_id == 0);
16969 
16970 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16971 	}
16972 
16973 	ilmp = &from_ill->ill_ilm;
16974 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16975 		ilm_next = ilm->ilm_next;
16976 
16977 		if (ilm->ilm_flags & ILM_DELETED) {
16978 			ilmp = &ilm->ilm_next;
16979 			continue;
16980 		}
16981 
16982 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16983 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16984 		ASSERT(ilm->ilm_orig_ifindex != 0);
16985 		if (ilm->ilm_orig_ifindex == ifindex) {
16986 			/*
16987 			 * We are failing back multicast memberships.
16988 			 * If the same ilm exists in to_ill, it means somebody
16989 			 * has joined the same group there e.g. ff02::1
16990 			 * is joined within the kernel when the interfaces
16991 			 * came UP.
16992 			 */
16993 			ASSERT(ilm->ilm_ipif == NULL);
16994 			if (new_ilm != NULL) {
16995 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16996 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16997 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16998 					new_ilm->ilm_is_new = B_TRUE;
16999 				}
17000 			} else {
17001 				/*
17002 				 * check if we can just move the ilm
17003 				 */
17004 				if (from_ill->ill_ilm_walker_cnt != 0) {
17005 					/*
17006 					 * We have walkers we cannot move
17007 					 * the ilm, so allocate a new ilm,
17008 					 * this (old) ilm will be marked
17009 					 * ILM_DELETED at the end of the loop
17010 					 * and will be freed when the
17011 					 * last walker exits.
17012 					 */
17013 					new_ilm = (ilm_t *)mi_zalloc
17014 					    (sizeof (ilm_t));
17015 					if (new_ilm == NULL) {
17016 						ip0dbg(("ilm_move_v6: "
17017 						    "FAILBACK of IPv6"
17018 						    " multicast address %s : "
17019 						    "from %s to"
17020 						    " %s failed : ENOMEM \n",
17021 						    inet_ntop(AF_INET6,
17022 						    &ilm->ilm_v6addr, buf,
17023 						    sizeof (buf)),
17024 						    from_ill->ill_name,
17025 						    to_ill->ill_name));
17026 
17027 							ilmp = &ilm->ilm_next;
17028 							continue;
17029 					}
17030 					*new_ilm = *ilm;
17031 					/*
17032 					 * we don't want new_ilm linked to
17033 					 * ilm's filter list.
17034 					 */
17035 					new_ilm->ilm_filter = NULL;
17036 				} else {
17037 					/*
17038 					 * No walkers we can move the ilm.
17039 					 * lets take it out of the list.
17040 					 */
17041 					*ilmp = ilm->ilm_next;
17042 					ilm->ilm_next = NULL;
17043 					new_ilm = ilm;
17044 				}
17045 
17046 				/*
17047 				 * if this is the first ilm for the group
17048 				 * set ilm_notify_driver so that we notify the
17049 				 * driver in ilm_send_multicast_reqs.
17050 				 */
17051 				if (ilm_lookup_ill_v6(to_ill,
17052 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17053 					new_ilm->ilm_notify_driver = B_TRUE;
17054 
17055 				new_ilm->ilm_ill = to_ill;
17056 				/* Add to the to_ill's list */
17057 				new_ilm->ilm_next = to_ill->ill_ilm;
17058 				to_ill->ill_ilm = new_ilm;
17059 				/*
17060 				 * set the flag so that mld_joingroup is
17061 				 * called in ilm_send_multicast_reqs().
17062 				 */
17063 				new_ilm->ilm_is_new = B_TRUE;
17064 			}
17065 			goto bottom;
17066 		} else if (ifindex != 0) {
17067 			/*
17068 			 * If this is FAILBACK (ifindex != 0) and the ifindex
17069 			 * has not matched above, look at the next ilm.
17070 			 */
17071 			ilmp = &ilm->ilm_next;
17072 			continue;
17073 		}
17074 		/*
17075 		 * If we are here, it means ifindex is 0. Failover
17076 		 * everything.
17077 		 *
17078 		 * We need to handle solicited node mcast address
17079 		 * and all_nodes mcast address differently as they
17080 		 * are joined witin the kenrel (ipif_multicast_up)
17081 		 * and potentially from the userland. We are called
17082 		 * after the ipifs of from_ill has been moved.
17083 		 * If we still find ilms on ill with solicited node
17084 		 * mcast address or all_nodes mcast address, it must
17085 		 * belong to the UP interface that has not moved e.g.
17086 		 * ipif_id 0 with the link local prefix does not move.
17087 		 * We join this on the new ill accounting for all the
17088 		 * userland memberships so that applications don't
17089 		 * see any failure.
17090 		 *
17091 		 * We need to make sure that we account only for the
17092 		 * solicited node and all node multicast addresses
17093 		 * that was brought UP on these. In the case of
17094 		 * a failover from A to B, we might have ilms belonging
17095 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
17096 		 * for the membership from the userland. If we are failing
17097 		 * over from B to C now, we will find the ones belonging
17098 		 * to A on B. These don't account for the ill_ipif_up_count.
17099 		 * They just move from B to C. The check below on
17100 		 * ilm_orig_ifindex ensures that.
17101 		 */
17102 		if ((ilm->ilm_orig_ifindex ==
17103 		    from_ill->ill_phyint->phyint_ifindex) &&
17104 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
17105 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
17106 		    &ilm->ilm_v6addr))) {
17107 			ASSERT(ilm->ilm_refcnt > 0);
17108 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
17109 			/*
17110 			 * For indentation reasons, we are not using a
17111 			 * "else" here.
17112 			 */
17113 			if (count == 0) {
17114 				ilmp = &ilm->ilm_next;
17115 				continue;
17116 			}
17117 			ilm->ilm_refcnt -= count;
17118 			if (new_ilm != NULL) {
17119 				/*
17120 				 * Can find one with the same
17121 				 * ilm_orig_ifindex, if we are failing
17122 				 * over to a STANDBY. This happens
17123 				 * when somebody wants to join a group
17124 				 * on a STANDBY interface and we
17125 				 * internally join on a different one.
17126 				 * If we had joined on from_ill then, a
17127 				 * failover now will find a new ilm
17128 				 * with this index.
17129 				 */
17130 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17131 				    " new ilm on %s, group address %s\n",
17132 				    to_ill->ill_name,
17133 				    inet_ntop(AF_INET6,
17134 				    &ilm->ilm_v6addr, buf,
17135 				    sizeof (buf))));
17136 				new_ilm->ilm_refcnt += count;
17137 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17138 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17139 					new_ilm->ilm_is_new = B_TRUE;
17140 				}
17141 			} else {
17142 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17143 				if (new_ilm == NULL) {
17144 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17145 					    " multicast address %s : from %s to"
17146 					    " %s failed : ENOMEM \n",
17147 					    inet_ntop(AF_INET6,
17148 					    &ilm->ilm_v6addr, buf,
17149 					    sizeof (buf)), from_ill->ill_name,
17150 					    to_ill->ill_name));
17151 					ilmp = &ilm->ilm_next;
17152 					continue;
17153 				}
17154 				*new_ilm = *ilm;
17155 				new_ilm->ilm_filter = NULL;
17156 				new_ilm->ilm_refcnt = count;
17157 				new_ilm->ilm_timer = INFINITY;
17158 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17159 				new_ilm->ilm_is_new = B_TRUE;
17160 				/*
17161 				 * If the to_ill has not joined this
17162 				 * group we need to tell the driver in
17163 				 * ill_send_multicast_reqs.
17164 				 */
17165 				if (ilm_lookup_ill_v6(to_ill,
17166 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17167 					new_ilm->ilm_notify_driver = B_TRUE;
17168 
17169 				new_ilm->ilm_ill = to_ill;
17170 				/* Add to the to_ill's list */
17171 				new_ilm->ilm_next = to_ill->ill_ilm;
17172 				to_ill->ill_ilm = new_ilm;
17173 				ASSERT(new_ilm->ilm_ipif == NULL);
17174 			}
17175 			if (ilm->ilm_refcnt == 0) {
17176 				goto bottom;
17177 			} else {
17178 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17179 				CLEAR_SLIST(new_ilm->ilm_filter);
17180 				ilmp = &ilm->ilm_next;
17181 			}
17182 			continue;
17183 		} else {
17184 			/*
17185 			 * ifindex = 0 means, move everything pointing at
17186 			 * from_ill. We are doing this becuase ill has
17187 			 * either FAILED or became INACTIVE.
17188 			 *
17189 			 * As we would like to move things later back to
17190 			 * from_ill, we want to retain the identity of this
17191 			 * ilm. Thus, we don't blindly increment the reference
17192 			 * count on the ilms matching the address alone. We
17193 			 * need to match on the ilm_orig_index also. new_ilm
17194 			 * was obtained by matching ilm_orig_index also.
17195 			 */
17196 			if (new_ilm != NULL) {
17197 				/*
17198 				 * This is possible only if a previous restore
17199 				 * was incomplete i.e restore to
17200 				 * ilm_orig_ifindex left some ilms because
17201 				 * of some failures. Thus when we are failing
17202 				 * again, we might find our old friends there.
17203 				 */
17204 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17205 				    " on %s, group address %s\n",
17206 				    to_ill->ill_name,
17207 				    inet_ntop(AF_INET6,
17208 				    &ilm->ilm_v6addr, buf,
17209 				    sizeof (buf))));
17210 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17211 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17212 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17213 					new_ilm->ilm_is_new = B_TRUE;
17214 				}
17215 			} else {
17216 				if (from_ill->ill_ilm_walker_cnt != 0) {
17217 					new_ilm = (ilm_t *)
17218 					    mi_zalloc(sizeof (ilm_t));
17219 					if (new_ilm == NULL) {
17220 						ip0dbg(("ilm_move_v6: "
17221 						    "FAILOVER of IPv6"
17222 						    " multicast address %s : "
17223 						    "from %s to"
17224 						    " %s failed : ENOMEM \n",
17225 						    inet_ntop(AF_INET6,
17226 						    &ilm->ilm_v6addr, buf,
17227 						    sizeof (buf)),
17228 						    from_ill->ill_name,
17229 						    to_ill->ill_name));
17230 
17231 							ilmp = &ilm->ilm_next;
17232 							continue;
17233 					}
17234 					*new_ilm = *ilm;
17235 					new_ilm->ilm_filter = NULL;
17236 				} else {
17237 					*ilmp = ilm->ilm_next;
17238 					new_ilm = ilm;
17239 				}
17240 				/*
17241 				 * If the to_ill has not joined this
17242 				 * group we need to tell the driver in
17243 				 * ill_send_multicast_reqs.
17244 				 */
17245 				if (ilm_lookup_ill_v6(to_ill,
17246 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17247 					new_ilm->ilm_notify_driver = B_TRUE;
17248 
17249 				/* Add to the to_ill's list */
17250 				new_ilm->ilm_next = to_ill->ill_ilm;
17251 				to_ill->ill_ilm = new_ilm;
17252 				ASSERT(ilm->ilm_ipif == NULL);
17253 				new_ilm->ilm_ill = to_ill;
17254 				new_ilm->ilm_is_new = B_TRUE;
17255 			}
17256 
17257 		}
17258 
17259 bottom:
17260 		/*
17261 		 * Revert multicast filter state to (EXCLUDE, NULL).
17262 		 * new_ilm->ilm_is_new should already be set if needed.
17263 		 */
17264 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17265 		CLEAR_SLIST(new_ilm->ilm_filter);
17266 		/*
17267 		 * We allocated/got a new ilm, free the old one.
17268 		 */
17269 		if (new_ilm != ilm) {
17270 			if (from_ill->ill_ilm_walker_cnt == 0) {
17271 				*ilmp = ilm->ilm_next;
17272 				ilm->ilm_next = NULL;
17273 				FREE_SLIST(ilm->ilm_filter);
17274 				FREE_SLIST(ilm->ilm_pendsrcs);
17275 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17276 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17277 				mi_free((char *)ilm);
17278 			} else {
17279 				ilm->ilm_flags |= ILM_DELETED;
17280 				from_ill->ill_ilm_cleanup_reqd = 1;
17281 				ilmp = &ilm->ilm_next;
17282 			}
17283 		}
17284 	}
17285 }
17286 
17287 /*
17288  * Move all the multicast memberships to to_ill. Called when
17289  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17290  * different from IPv6 counterpart as multicast memberships are associated
17291  * with ills in IPv6. This function is called after every ipif is moved
17292  * unlike IPv6, where it is moved only once.
17293  */
17294 static void
17295 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17296 {
17297 	ilm_t	*ilm;
17298 	ilm_t	*ilm_next;
17299 	ilm_t	*new_ilm;
17300 	ilm_t	**ilmp;
17301 	ip_stack_t	*ipst = from_ill->ill_ipst;
17302 
17303 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17304 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17305 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17306 
17307 	ilmp = &from_ill->ill_ilm;
17308 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17309 		ilm_next = ilm->ilm_next;
17310 
17311 		if (ilm->ilm_flags & ILM_DELETED) {
17312 			ilmp = &ilm->ilm_next;
17313 			continue;
17314 		}
17315 
17316 		ASSERT(ilm->ilm_ipif != NULL);
17317 
17318 		if (ilm->ilm_ipif != ipif) {
17319 			ilmp = &ilm->ilm_next;
17320 			continue;
17321 		}
17322 
17323 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17324 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17325 			new_ilm = ilm_lookup_ipif(ipif,
17326 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17327 			if (new_ilm != NULL) {
17328 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17329 				/*
17330 				 * We still need to deal with the from_ill.
17331 				 */
17332 				new_ilm->ilm_is_new = B_TRUE;
17333 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17334 				CLEAR_SLIST(new_ilm->ilm_filter);
17335 				goto delete_ilm;
17336 			}
17337 			/*
17338 			 * If we could not find one e.g. ipif is
17339 			 * still down on to_ill, we add this ilm
17340 			 * on ill_new to preserve the reference
17341 			 * count.
17342 			 */
17343 		}
17344 		/*
17345 		 * When ipifs move, ilms always move with it
17346 		 * to the NEW ill. Thus we should never be
17347 		 * able to find ilm till we really move it here.
17348 		 */
17349 		ASSERT(ilm_lookup_ipif(ipif,
17350 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17351 
17352 		if (from_ill->ill_ilm_walker_cnt != 0) {
17353 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17354 			if (new_ilm == NULL) {
17355 				char buf[INET6_ADDRSTRLEN];
17356 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17357 				    " multicast address %s : "
17358 				    "from %s to"
17359 				    " %s failed : ENOMEM \n",
17360 				    inet_ntop(AF_INET,
17361 				    &ilm->ilm_v6addr, buf,
17362 				    sizeof (buf)),
17363 				    from_ill->ill_name,
17364 				    to_ill->ill_name));
17365 
17366 				ilmp = &ilm->ilm_next;
17367 				continue;
17368 			}
17369 			*new_ilm = *ilm;
17370 			/* We don't want new_ilm linked to ilm's filter list */
17371 			new_ilm->ilm_filter = NULL;
17372 		} else {
17373 			/* Remove from the list */
17374 			*ilmp = ilm->ilm_next;
17375 			new_ilm = ilm;
17376 		}
17377 
17378 		/*
17379 		 * If we have never joined this group on the to_ill
17380 		 * make sure we tell the driver.
17381 		 */
17382 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17383 		    ALL_ZONES) == NULL)
17384 			new_ilm->ilm_notify_driver = B_TRUE;
17385 
17386 		/* Add to the to_ill's list */
17387 		new_ilm->ilm_next = to_ill->ill_ilm;
17388 		to_ill->ill_ilm = new_ilm;
17389 		new_ilm->ilm_is_new = B_TRUE;
17390 
17391 		/*
17392 		 * Revert multicast filter state to (EXCLUDE, NULL)
17393 		 */
17394 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17395 		CLEAR_SLIST(new_ilm->ilm_filter);
17396 
17397 		/*
17398 		 * Delete only if we have allocated a new ilm.
17399 		 */
17400 		if (new_ilm != ilm) {
17401 delete_ilm:
17402 			if (from_ill->ill_ilm_walker_cnt == 0) {
17403 				/* Remove from the list */
17404 				*ilmp = ilm->ilm_next;
17405 				ilm->ilm_next = NULL;
17406 				FREE_SLIST(ilm->ilm_filter);
17407 				FREE_SLIST(ilm->ilm_pendsrcs);
17408 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17409 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17410 				mi_free((char *)ilm);
17411 			} else {
17412 				ilm->ilm_flags |= ILM_DELETED;
17413 				from_ill->ill_ilm_cleanup_reqd = 1;
17414 				ilmp = &ilm->ilm_next;
17415 			}
17416 		}
17417 	}
17418 }
17419 
17420 static uint_t
17421 ipif_get_id(ill_t *ill, uint_t id)
17422 {
17423 	uint_t	unit;
17424 	ipif_t	*tipif;
17425 	boolean_t found = B_FALSE;
17426 	ip_stack_t	*ipst = ill->ill_ipst;
17427 
17428 	/*
17429 	 * During failback, we want to go back to the same id
17430 	 * instead of the smallest id so that the original
17431 	 * configuration is maintained. id is non-zero in that
17432 	 * case.
17433 	 */
17434 	if (id != 0) {
17435 		/*
17436 		 * While failing back, if we still have an ipif with
17437 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17438 		 * as soon as we return from this function. It was
17439 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17440 		 * we can choose the smallest id. Thus we return zero
17441 		 * in that case ignoring the hint.
17442 		 */
17443 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17444 			return (0);
17445 		for (tipif = ill->ill_ipif; tipif != NULL;
17446 		    tipif = tipif->ipif_next) {
17447 			if (tipif->ipif_id == id) {
17448 				found = B_TRUE;
17449 				break;
17450 			}
17451 		}
17452 		/*
17453 		 * If somebody already plumbed another logical
17454 		 * with the same id, we won't be able to find it.
17455 		 */
17456 		if (!found)
17457 			return (id);
17458 	}
17459 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17460 		found = B_FALSE;
17461 		for (tipif = ill->ill_ipif; tipif != NULL;
17462 		    tipif = tipif->ipif_next) {
17463 			if (tipif->ipif_id == unit) {
17464 				found = B_TRUE;
17465 				break;
17466 			}
17467 		}
17468 		if (!found)
17469 			break;
17470 	}
17471 	return (unit);
17472 }
17473 
17474 /* ARGSUSED */
17475 static int
17476 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17477     ipif_t **rep_ipif_ptr)
17478 {
17479 	ill_t	*from_ill;
17480 	ipif_t	*rep_ipif;
17481 	uint_t	unit;
17482 	int err = 0;
17483 	ipif_t	*to_ipif;
17484 	struct iocblk	*iocp;
17485 	boolean_t failback_cmd;
17486 	boolean_t remove_ipif;
17487 	int	rc;
17488 	ip_stack_t	*ipst;
17489 
17490 	ASSERT(IAM_WRITER_ILL(to_ill));
17491 	ASSERT(IAM_WRITER_IPIF(ipif));
17492 
17493 	iocp = (struct iocblk *)mp->b_rptr;
17494 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17495 	remove_ipif = B_FALSE;
17496 
17497 	from_ill = ipif->ipif_ill;
17498 	ipst = from_ill->ill_ipst;
17499 
17500 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17501 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17502 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17503 
17504 	/*
17505 	 * Don't move LINK LOCAL addresses as they are tied to
17506 	 * physical interface.
17507 	 */
17508 	if (from_ill->ill_isv6 &&
17509 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17510 		ipif->ipif_was_up = B_FALSE;
17511 		IPIF_UNMARK_MOVING(ipif);
17512 		return (0);
17513 	}
17514 
17515 	/*
17516 	 * We set the ipif_id to maximum so that the search for
17517 	 * ipif_id will pick the lowest number i.e 0 in the
17518 	 * following 2 cases :
17519 	 *
17520 	 * 1) We have a replacement ipif at the head of to_ill.
17521 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17522 	 *    on to_ill and hence the MOVE might fail. We want to
17523 	 *    remove it only if we could move the ipif. Thus, by
17524 	 *    setting it to the MAX value, we make the search in
17525 	 *    ipif_get_id return the zeroth id.
17526 	 *
17527 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17528 	 *    we might just have a zero address plumbed on the ipif
17529 	 *    with zero id in the case of IPv4. We remove that while
17530 	 *    doing the failback. We want to remove it only if we
17531 	 *    could move the ipif. Thus, by setting it to the MAX
17532 	 *    value, we make the search in ipif_get_id return the
17533 	 *    zeroth id.
17534 	 *
17535 	 * Both (1) and (2) are done only when when we are moving
17536 	 * an ipif (either due to failover/failback) which originally
17537 	 * belonged to this interface i.e the ipif_orig_ifindex is
17538 	 * the same as to_ill's ifindex. This is needed so that
17539 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17540 	 * from B -> A (B is being removed from the group) and
17541 	 * FAILBACK from A -> B restores the original configuration.
17542 	 * Without the check for orig_ifindex, the second FAILOVER
17543 	 * could make the ipif belonging to B replace the A's zeroth
17544 	 * ipif and the subsequent failback re-creating the replacement
17545 	 * ipif again.
17546 	 *
17547 	 * NOTE : We created the replacement ipif when we did a
17548 	 * FAILOVER (See below). We could check for FAILBACK and
17549 	 * then look for replacement ipif to be removed. But we don't
17550 	 * want to do that because we wan't to allow the possibility
17551 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17552 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17553 	 * from B -> A.
17554 	 */
17555 	to_ipif = to_ill->ill_ipif;
17556 	if ((to_ill->ill_phyint->phyint_ifindex ==
17557 	    ipif->ipif_orig_ifindex) &&
17558 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17559 		ASSERT(to_ipif->ipif_id == 0);
17560 		remove_ipif = B_TRUE;
17561 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17562 	}
17563 	/*
17564 	 * Find the lowest logical unit number on the to_ill.
17565 	 * If we are failing back, try to get the original id
17566 	 * rather than the lowest one so that the original
17567 	 * configuration is maintained.
17568 	 *
17569 	 * XXX need a better scheme for this.
17570 	 */
17571 	if (failback_cmd) {
17572 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17573 	} else {
17574 		unit = ipif_get_id(to_ill, 0);
17575 	}
17576 
17577 	/* Reset back to zero in case we fail below */
17578 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17579 		to_ipif->ipif_id = 0;
17580 
17581 	if (unit == ipst->ips_ip_addrs_per_if) {
17582 		ipif->ipif_was_up = B_FALSE;
17583 		IPIF_UNMARK_MOVING(ipif);
17584 		return (EINVAL);
17585 	}
17586 
17587 	/*
17588 	 * ipif is ready to move from "from_ill" to "to_ill".
17589 	 *
17590 	 * 1) If we are moving ipif with id zero, create a
17591 	 *    replacement ipif for this ipif on from_ill. If this fails
17592 	 *    fail the MOVE operation.
17593 	 *
17594 	 * 2) Remove the replacement ipif on to_ill if any.
17595 	 *    We could remove the replacement ipif when we are moving
17596 	 *    the ipif with id zero. But what if somebody already
17597 	 *    unplumbed it ? Thus we always remove it if it is present.
17598 	 *    We want to do it only if we are sure we are going to
17599 	 *    move the ipif to to_ill which is why there are no
17600 	 *    returns due to error till ipif is linked to to_ill.
17601 	 *    Note that the first ipif that we failback will always
17602 	 *    be zero if it is present.
17603 	 */
17604 	if (ipif->ipif_id == 0) {
17605 		ipaddr_t inaddr_any = INADDR_ANY;
17606 
17607 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17608 		if (rep_ipif == NULL) {
17609 			ipif->ipif_was_up = B_FALSE;
17610 			IPIF_UNMARK_MOVING(ipif);
17611 			return (ENOMEM);
17612 		}
17613 		*rep_ipif = ipif_zero;
17614 		/*
17615 		 * Before we put the ipif on the list, store the addresses
17616 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17617 		 * assumes so. This logic is not any different from what
17618 		 * ipif_allocate does.
17619 		 */
17620 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17621 		    &rep_ipif->ipif_v6lcl_addr);
17622 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17623 		    &rep_ipif->ipif_v6src_addr);
17624 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17625 		    &rep_ipif->ipif_v6subnet);
17626 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17627 		    &rep_ipif->ipif_v6net_mask);
17628 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17629 		    &rep_ipif->ipif_v6brd_addr);
17630 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17631 		    &rep_ipif->ipif_v6pp_dst_addr);
17632 		/*
17633 		 * We mark IPIF_NOFAILOVER so that this can never
17634 		 * move.
17635 		 */
17636 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17637 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17638 		rep_ipif->ipif_replace_zero = B_TRUE;
17639 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17640 		    MUTEX_DEFAULT, NULL);
17641 		rep_ipif->ipif_id = 0;
17642 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17643 		rep_ipif->ipif_ill = from_ill;
17644 		rep_ipif->ipif_orig_ifindex =
17645 		    from_ill->ill_phyint->phyint_ifindex;
17646 		/* Insert at head */
17647 		rep_ipif->ipif_next = from_ill->ill_ipif;
17648 		from_ill->ill_ipif = rep_ipif;
17649 		/*
17650 		 * We don't really care to let apps know about
17651 		 * this interface.
17652 		 */
17653 	}
17654 
17655 	if (remove_ipif) {
17656 		/*
17657 		 * We set to a max value above for this case to get
17658 		 * id zero. ASSERT that we did get one.
17659 		 */
17660 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17661 		rep_ipif = to_ipif;
17662 		to_ill->ill_ipif = rep_ipif->ipif_next;
17663 		rep_ipif->ipif_next = NULL;
17664 		/*
17665 		 * If some apps scanned and find this interface,
17666 		 * it is time to let them know, so that they can
17667 		 * delete it.
17668 		 */
17669 
17670 		*rep_ipif_ptr = rep_ipif;
17671 	}
17672 
17673 	/* Get it out of the ILL interface list. */
17674 	ipif_remove(ipif, B_FALSE);
17675 
17676 	/* Assign the new ill */
17677 	ipif->ipif_ill = to_ill;
17678 	ipif->ipif_id = unit;
17679 	/* id has already been checked */
17680 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17681 	ASSERT(rc == 0);
17682 	/* Let SCTP update its list */
17683 	sctp_move_ipif(ipif, from_ill, to_ill);
17684 	/*
17685 	 * Handle the failover and failback of ipif_t between
17686 	 * ill_t that have differing maximum mtu values.
17687 	 */
17688 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17689 		if (ipif->ipif_saved_mtu == 0) {
17690 			/*
17691 			 * As this ipif_t is moving to an ill_t
17692 			 * that has a lower ill_max_mtu, its
17693 			 * ipif_mtu needs to be saved so it can
17694 			 * be restored during failback or during
17695 			 * failover to an ill_t which has a
17696 			 * higher ill_max_mtu.
17697 			 */
17698 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17699 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17700 		} else {
17701 			/*
17702 			 * The ipif_t is, once again, moving to
17703 			 * an ill_t that has a lower maximum mtu
17704 			 * value.
17705 			 */
17706 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17707 		}
17708 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17709 	    ipif->ipif_saved_mtu != 0) {
17710 		/*
17711 		 * The mtu of this ipif_t had to be reduced
17712 		 * during an earlier failover; this is an
17713 		 * opportunity for it to be increased (either as
17714 		 * part of another failover or a failback).
17715 		 */
17716 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17717 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17718 			ipif->ipif_saved_mtu = 0;
17719 		} else {
17720 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17721 		}
17722 	}
17723 
17724 	/*
17725 	 * We preserve all the other fields of the ipif including
17726 	 * ipif_saved_ire_mp. The routes that are saved here will
17727 	 * be recreated on the new interface and back on the old
17728 	 * interface when we move back.
17729 	 */
17730 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17731 
17732 	return (err);
17733 }
17734 
17735 static int
17736 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17737     int ifindex, ipif_t **rep_ipif_ptr)
17738 {
17739 	ipif_t *mipif;
17740 	ipif_t *ipif_next;
17741 	int err;
17742 
17743 	/*
17744 	 * We don't really try to MOVE back things if some of the
17745 	 * operations fail. The daemon will take care of moving again
17746 	 * later on.
17747 	 */
17748 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17749 		ipif_next = mipif->ipif_next;
17750 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17751 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17752 
17753 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17754 
17755 			/*
17756 			 * When the MOVE fails, it is the job of the
17757 			 * application to take care of this properly
17758 			 * i.e try again if it is ENOMEM.
17759 			 */
17760 			if (mipif->ipif_ill != from_ill) {
17761 				/*
17762 				 * ipif has moved.
17763 				 *
17764 				 * Move the multicast memberships associated
17765 				 * with this ipif to the new ill. For IPv6, we
17766 				 * do it once after all the ipifs are moved
17767 				 * (in ill_move) as they are not associated
17768 				 * with ipifs.
17769 				 *
17770 				 * We need to move the ilms as the ipif has
17771 				 * already been moved to a new ill even
17772 				 * in the case of errors. Neither
17773 				 * ilm_free(ipif) will find the ilm
17774 				 * when somebody unplumbs this ipif nor
17775 				 * ilm_delete(ilm) will be able to find the
17776 				 * ilm, if we don't move now.
17777 				 */
17778 				if (!from_ill->ill_isv6)
17779 					ilm_move_v4(from_ill, to_ill, mipif);
17780 			}
17781 
17782 			if (err != 0)
17783 				return (err);
17784 		}
17785 	}
17786 	return (0);
17787 }
17788 
17789 static int
17790 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17791 {
17792 	int ifindex;
17793 	int err;
17794 	struct iocblk	*iocp;
17795 	ipif_t	*ipif;
17796 	ipif_t *rep_ipif_ptr = NULL;
17797 	ipif_t	*from_ipif = NULL;
17798 	boolean_t check_rep_if = B_FALSE;
17799 	ip_stack_t	*ipst = from_ill->ill_ipst;
17800 
17801 	iocp = (struct iocblk *)mp->b_rptr;
17802 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17803 		/*
17804 		 * Move everything pointing at from_ill to to_ill.
17805 		 * We acheive this by passing in 0 as ifindex.
17806 		 */
17807 		ifindex = 0;
17808 	} else {
17809 		/*
17810 		 * Move everything pointing at from_ill whose original
17811 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17812 		 * We acheive this by passing in ifindex rather than 0.
17813 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17814 		 */
17815 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17816 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17817 	}
17818 
17819 	/*
17820 	 * Determine if there is at least one ipif that would move from
17821 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17822 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17823 	 * the move, in which case we need to quiesce the replacement ipif also.
17824 	 */
17825 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17826 	    from_ipif = from_ipif->ipif_next) {
17827 		if (((ifindex == 0) ||
17828 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17829 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17830 			check_rep_if = B_TRUE;
17831 			break;
17832 		}
17833 	}
17834 
17835 
17836 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17837 
17838 	GRAB_ILL_LOCKS(from_ill, to_ill);
17839 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17840 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17841 		    mp, ILL_MOVE_OK);
17842 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17843 		return (EINPROGRESS);
17844 	}
17845 
17846 	/* Check if the replacement ipif is quiescent to delete */
17847 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17848 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17849 		to_ill->ill_ipif->ipif_state_flags |=
17850 		    IPIF_MOVING | IPIF_CHANGING;
17851 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17852 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17853 			    mp, ILL_MOVE_OK);
17854 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17855 			return (EINPROGRESS);
17856 		}
17857 	}
17858 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17859 
17860 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17861 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17862 	GRAB_ILL_LOCKS(from_ill, to_ill);
17863 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17864 
17865 	/* ilm_move is done inside ipif_move for IPv4 */
17866 	if (err == 0 && from_ill->ill_isv6)
17867 		ilm_move_v6(from_ill, to_ill, ifindex);
17868 
17869 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17870 	rw_exit(&ipst->ips_ill_g_lock);
17871 
17872 	/*
17873 	 * send rts messages and multicast messages.
17874 	 */
17875 	if (rep_ipif_ptr != NULL) {
17876 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17877 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17878 			rep_ipif_ptr->ipif_recovery_id = 0;
17879 		}
17880 		ip_rts_ifmsg(rep_ipif_ptr);
17881 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17882 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
17883 		mi_free(rep_ipif_ptr);
17884 	}
17885 
17886 	conn_move_ill(from_ill, to_ill, ifindex);
17887 
17888 	return (err);
17889 }
17890 
17891 /*
17892  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17893  * Also checks for the validity of the arguments.
17894  * Note: We are already exclusive inside the from group.
17895  * It is upto the caller to release refcnt on the to_ill's.
17896  */
17897 static int
17898 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17899     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17900 {
17901 	int dst_index;
17902 	ipif_t *ipif_v4, *ipif_v6;
17903 	struct lifreq *lifr;
17904 	mblk_t *mp1;
17905 	boolean_t exists;
17906 	sin_t	*sin;
17907 	int	err = 0;
17908 	ip_stack_t	*ipst;
17909 
17910 	if (CONN_Q(q))
17911 		ipst = CONNQ_TO_IPST(q);
17912 	else
17913 		ipst = ILLQ_TO_IPST(q);
17914 
17915 
17916 	if ((mp1 = mp->b_cont) == NULL)
17917 		return (EPROTO);
17918 
17919 	if ((mp1 = mp1->b_cont) == NULL)
17920 		return (EPROTO);
17921 
17922 	lifr = (struct lifreq *)mp1->b_rptr;
17923 	sin = (sin_t *)&lifr->lifr_addr;
17924 
17925 	/*
17926 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17927 	 * specific operations.
17928 	 */
17929 	if (sin->sin_family != AF_UNSPEC)
17930 		return (EINVAL);
17931 
17932 	/*
17933 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17934 	 * NULLs for the last 4 args and we know the lookup won't fail
17935 	 * with EINPROGRESS.
17936 	 */
17937 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17938 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17939 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17940 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17941 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17942 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17943 
17944 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17945 		return (ENXIO);
17946 
17947 	if (ipif_v4 != NULL) {
17948 		ASSERT(ipif_v4->ipif_refcnt != 0);
17949 		if (ipif_v4->ipif_id != 0) {
17950 			err = EINVAL;
17951 			goto done;
17952 		}
17953 
17954 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17955 		*ill_from_v4 = ipif_v4->ipif_ill;
17956 	}
17957 
17958 	if (ipif_v6 != NULL) {
17959 		ASSERT(ipif_v6->ipif_refcnt != 0);
17960 		if (ipif_v6->ipif_id != 0) {
17961 			err = EINVAL;
17962 			goto done;
17963 		}
17964 
17965 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17966 		*ill_from_v6 = ipif_v6->ipif_ill;
17967 	}
17968 
17969 	err = 0;
17970 	dst_index = lifr->lifr_movetoindex;
17971 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17972 	    q, mp, ip_process_ioctl, &err, ipst);
17973 	if (err != 0) {
17974 		/*
17975 		 * There could be only v6.
17976 		 */
17977 		if (err != ENXIO)
17978 			goto done;
17979 		err = 0;
17980 	}
17981 
17982 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17983 	    q, mp, ip_process_ioctl, &err, ipst);
17984 	if (err != 0) {
17985 		if (err != ENXIO)
17986 			goto done;
17987 		if (*ill_to_v4 == NULL) {
17988 			err = ENXIO;
17989 			goto done;
17990 		}
17991 		err = 0;
17992 	}
17993 
17994 	/*
17995 	 * If we have something to MOVE i.e "from" not NULL,
17996 	 * "to" should be non-NULL.
17997 	 */
17998 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17999 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
18000 		err = EINVAL;
18001 	}
18002 
18003 done:
18004 	if (ipif_v4 != NULL)
18005 		ipif_refrele(ipif_v4);
18006 	if (ipif_v6 != NULL)
18007 		ipif_refrele(ipif_v6);
18008 	return (err);
18009 }
18010 
18011 /*
18012  * FAILOVER and FAILBACK are modelled as MOVE operations.
18013  *
18014  * We don't check whether the MOVE is within the same group or
18015  * not, because this ioctl can be used as a generic mechanism
18016  * to failover from interface A to B, though things will function
18017  * only if they are really part of the same group. Moreover,
18018  * all ipifs may be down and hence temporarily out of the group.
18019  *
18020  * ipif's that need to be moved are first brought down; V4 ipifs are brought
18021  * down first and then V6.  For each we wait for the ipif's to become quiescent.
18022  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
18023  * have been deleted and there are no active references. Once quiescent the
18024  * ipif's are moved and brought up on the new ill.
18025  *
18026  * Normally the source ill and destination ill belong to the same IPMP group
18027  * and hence the same ipsq_t. In the event they don't belong to the same
18028  * same group the two ipsq's are first merged into one ipsq - that of the
18029  * to_ill. The multicast memberships on the source and destination ill cannot
18030  * change during the move operation since multicast joins/leaves also have to
18031  * execute on the same ipsq and are hence serialized.
18032  */
18033 /* ARGSUSED */
18034 int
18035 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18036     ip_ioctl_cmd_t *ipip, void *ifreq)
18037 {
18038 	ill_t *ill_to_v4 = NULL;
18039 	ill_t *ill_to_v6 = NULL;
18040 	ill_t *ill_from_v4 = NULL;
18041 	ill_t *ill_from_v6 = NULL;
18042 	int err = 0;
18043 
18044 	/*
18045 	 * setup from and to ill's, we can get EINPROGRESS only for
18046 	 * to_ill's.
18047 	 */
18048 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
18049 	    &ill_to_v4, &ill_to_v6);
18050 
18051 	if (err != 0) {
18052 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
18053 		goto done;
18054 	}
18055 
18056 	/*
18057 	 * nothing to do.
18058 	 */
18059 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
18060 		goto done;
18061 	}
18062 
18063 	/*
18064 	 * nothing to do.
18065 	 */
18066 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
18067 		goto done;
18068 	}
18069 
18070 	/*
18071 	 * Mark the ill as changing.
18072 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
18073 	 * in ill_up_ipifs in case of error they are cleared below.
18074 	 */
18075 
18076 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18077 	if (ill_from_v4 != NULL)
18078 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
18079 	if (ill_from_v6 != NULL)
18080 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
18081 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18082 
18083 	/*
18084 	 * Make sure that both src and dst are
18085 	 * in the same syncq group. If not make it happen.
18086 	 * We are not holding any locks because we are the writer
18087 	 * on the from_ipsq and we will hold locks in ill_merge_groups
18088 	 * to protect to_ipsq against changing.
18089 	 */
18090 	if (ill_from_v4 != NULL) {
18091 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
18092 		    ill_to_v4->ill_phyint->phyint_ipsq) {
18093 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
18094 			    NULL, mp, q);
18095 			goto err_ret;
18096 
18097 		}
18098 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18099 	} else {
18100 
18101 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18102 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18103 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18104 			    NULL, mp, q);
18105 			goto err_ret;
18106 
18107 		}
18108 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18109 	}
18110 
18111 	/*
18112 	 * Now that the ipsq's have been merged and we are the writer
18113 	 * lets mark to_ill as changing as well.
18114 	 */
18115 
18116 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18117 	if (ill_to_v4 != NULL)
18118 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18119 	if (ill_to_v6 != NULL)
18120 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18121 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18122 
18123 	/*
18124 	 * Its ok for us to proceed with the move even if
18125 	 * ill_pending_mp is non null on one of the from ill's as the reply
18126 	 * should not be looking at the ipif, it should only care about the
18127 	 * ill itself.
18128 	 */
18129 
18130 	/*
18131 	 * lets move ipv4 first.
18132 	 */
18133 	if (ill_from_v4 != NULL) {
18134 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18135 		ill_from_v4->ill_move_in_progress = B_TRUE;
18136 		ill_to_v4->ill_move_in_progress = B_TRUE;
18137 		ill_to_v4->ill_move_peer = ill_from_v4;
18138 		ill_from_v4->ill_move_peer = ill_to_v4;
18139 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18140 	}
18141 
18142 	/*
18143 	 * Now lets move ipv6.
18144 	 */
18145 	if (err == 0 && ill_from_v6 != NULL) {
18146 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18147 		ill_from_v6->ill_move_in_progress = B_TRUE;
18148 		ill_to_v6->ill_move_in_progress = B_TRUE;
18149 		ill_to_v6->ill_move_peer = ill_from_v6;
18150 		ill_from_v6->ill_move_peer = ill_to_v6;
18151 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18152 	}
18153 
18154 err_ret:
18155 	/*
18156 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18157 	 * moved to become quiescent.
18158 	 */
18159 	if (err == EINPROGRESS) {
18160 		goto done;
18161 	}
18162 
18163 	/*
18164 	 * if err is set ill_up_ipifs will not be called
18165 	 * lets clear the flags.
18166 	 */
18167 
18168 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18169 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18170 	/*
18171 	 * Some of the clearing may be redundant. But it is simple
18172 	 * not making any extra checks.
18173 	 */
18174 	if (ill_from_v6 != NULL) {
18175 		ill_from_v6->ill_move_in_progress = B_FALSE;
18176 		ill_from_v6->ill_move_peer = NULL;
18177 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18178 	}
18179 	if (ill_from_v4 != NULL) {
18180 		ill_from_v4->ill_move_in_progress = B_FALSE;
18181 		ill_from_v4->ill_move_peer = NULL;
18182 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18183 	}
18184 	if (ill_to_v6 != NULL) {
18185 		ill_to_v6->ill_move_in_progress = B_FALSE;
18186 		ill_to_v6->ill_move_peer = NULL;
18187 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18188 	}
18189 	if (ill_to_v4 != NULL) {
18190 		ill_to_v4->ill_move_in_progress = B_FALSE;
18191 		ill_to_v4->ill_move_peer = NULL;
18192 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18193 	}
18194 
18195 	/*
18196 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18197 	 * Do this always to maintain proper state i.e even in case of errors.
18198 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18199 	 * we need not call on both v4 and v6 interfaces.
18200 	 */
18201 	if (ill_from_v4 != NULL) {
18202 		if ((ill_from_v4->ill_phyint->phyint_flags &
18203 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18204 			phyint_inactive(ill_from_v4->ill_phyint);
18205 		}
18206 	} else if (ill_from_v6 != NULL) {
18207 		if ((ill_from_v6->ill_phyint->phyint_flags &
18208 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18209 			phyint_inactive(ill_from_v6->ill_phyint);
18210 		}
18211 	}
18212 
18213 	if (ill_to_v4 != NULL) {
18214 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18215 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18216 		}
18217 	} else if (ill_to_v6 != NULL) {
18218 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18219 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18220 		}
18221 	}
18222 
18223 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18224 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18225 
18226 no_err:
18227 	/*
18228 	 * lets bring the interfaces up on the to_ill.
18229 	 */
18230 	if (err == 0) {
18231 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18232 		    q, mp);
18233 	}
18234 
18235 	if (err == 0) {
18236 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18237 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18238 
18239 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18240 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18241 	}
18242 done:
18243 
18244 	if (ill_to_v4 != NULL) {
18245 		ill_refrele(ill_to_v4);
18246 	}
18247 	if (ill_to_v6 != NULL) {
18248 		ill_refrele(ill_to_v6);
18249 	}
18250 
18251 	return (err);
18252 }
18253 
18254 static void
18255 ill_dl_down(ill_t *ill)
18256 {
18257 	/*
18258 	 * The ill is down; unbind but stay attached since we're still
18259 	 * associated with a PPA. If we have negotiated DLPI capabilites
18260 	 * with the data link service provider (IDS_OK) then reset them.
18261 	 * The interval between unbinding and rebinding is potentially
18262 	 * unbounded hence we cannot assume things will be the same.
18263 	 * The DLPI capabilities will be probed again when the data link
18264 	 * is brought up.
18265 	 */
18266 	mblk_t	*mp = ill->ill_unbind_mp;
18267 	hook_nic_event_t *info;
18268 
18269 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18270 
18271 	ill->ill_unbind_mp = NULL;
18272 	if (mp != NULL) {
18273 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18274 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18275 		    ill->ill_name));
18276 		mutex_enter(&ill->ill_lock);
18277 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18278 		mutex_exit(&ill->ill_lock);
18279 		if (ill->ill_dlpi_capab_state == IDS_OK)
18280 			ill_capability_reset(ill);
18281 		ill_dlpi_send(ill, mp);
18282 	}
18283 
18284 	/*
18285 	 * Toss all of our multicast memberships.  We could keep them, but
18286 	 * then we'd have to do bookkeeping of any joins and leaves performed
18287 	 * by the application while the the interface is down (we can't just
18288 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18289 	 * on a downed interface).
18290 	 */
18291 	ill_leave_multicast(ill);
18292 
18293 	mutex_enter(&ill->ill_lock);
18294 
18295 	ill->ill_dl_up = 0;
18296 
18297 	if ((info = ill->ill_nic_event_info) != NULL) {
18298 		ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n",
18299 		    info->hne_event, ill->ill_name));
18300 		if (info->hne_data != NULL)
18301 			kmem_free(info->hne_data, info->hne_datalen);
18302 		kmem_free(info, sizeof (hook_nic_event_t));
18303 	}
18304 
18305 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
18306 	if (info != NULL) {
18307 		ip_stack_t	*ipst = ill->ill_ipst;
18308 
18309 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
18310 		info->hne_lif = 0;
18311 		info->hne_event = NE_DOWN;
18312 		info->hne_data = NULL;
18313 		info->hne_datalen = 0;
18314 		info->hne_family = ill->ill_isv6 ?
18315 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18316 	} else
18317 		ip2dbg(("ill_dl_down: could not attach DOWN nic event "
18318 		    "information for %s (ENOMEM)\n", ill->ill_name));
18319 
18320 	ill->ill_nic_event_info = info;
18321 
18322 	mutex_exit(&ill->ill_lock);
18323 }
18324 
18325 static void
18326 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18327 {
18328 	union DL_primitives *dlp;
18329 	t_uscalar_t prim;
18330 
18331 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18332 
18333 	dlp = (union DL_primitives *)mp->b_rptr;
18334 	prim = dlp->dl_primitive;
18335 
18336 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18337 	    dlpi_prim_str(prim), prim, ill->ill_name));
18338 
18339 	switch (prim) {
18340 	case DL_PHYS_ADDR_REQ:
18341 	{
18342 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18343 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18344 		break;
18345 	}
18346 	case DL_BIND_REQ:
18347 		mutex_enter(&ill->ill_lock);
18348 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18349 		mutex_exit(&ill->ill_lock);
18350 		break;
18351 	}
18352 
18353 	/*
18354 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18355 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18356 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18357 	 */
18358 	mutex_enter(&ill->ill_lock);
18359 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18360 	    (prim == DL_UNBIND_REQ)) {
18361 		ill->ill_dlpi_pending = prim;
18362 	}
18363 	mutex_exit(&ill->ill_lock);
18364 
18365 	putnext(ill->ill_wq, mp);
18366 }
18367 
18368 /*
18369  * Helper function for ill_dlpi_send().
18370  */
18371 /* ARGSUSED */
18372 static void
18373 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18374 {
18375 	ill_dlpi_send((ill_t *)q->q_ptr, mp);
18376 }
18377 
18378 /*
18379  * Send a DLPI control message to the driver but make sure there
18380  * is only one outstanding message. Uses ill_dlpi_pending to tell
18381  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18382  * when an ACK or a NAK is received to process the next queued message.
18383  */
18384 void
18385 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18386 {
18387 	mblk_t **mpp;
18388 
18389 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18390 
18391 	/*
18392 	 * To ensure that any DLPI requests for current exclusive operation
18393 	 * are always completely sent before any DLPI messages for other
18394 	 * operations, require writer access before enqueuing.
18395 	 */
18396 	if (!IAM_WRITER_ILL(ill)) {
18397 		ill_refhold(ill);
18398 		/* qwriter_ip() does the ill_refrele() */
18399 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18400 		    NEW_OP, B_TRUE);
18401 		return;
18402 	}
18403 
18404 	mutex_enter(&ill->ill_lock);
18405 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18406 		/* Must queue message. Tail insertion */
18407 		mpp = &ill->ill_dlpi_deferred;
18408 		while (*mpp != NULL)
18409 			mpp = &((*mpp)->b_next);
18410 
18411 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18412 		    ill->ill_name));
18413 
18414 		*mpp = mp;
18415 		mutex_exit(&ill->ill_lock);
18416 		return;
18417 	}
18418 	mutex_exit(&ill->ill_lock);
18419 	ill_dlpi_dispatch(ill, mp);
18420 }
18421 
18422 /*
18423  * Send all deferred DLPI messages without waiting for their ACKs.
18424  */
18425 void
18426 ill_dlpi_send_deferred(ill_t *ill)
18427 {
18428 	mblk_t *mp, *nextmp;
18429 
18430 	/*
18431 	 * Clear ill_dlpi_pending so that the message is not queued in
18432 	 * ill_dlpi_send().
18433 	 */
18434 	mutex_enter(&ill->ill_lock);
18435 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18436 	mp = ill->ill_dlpi_deferred;
18437 	ill->ill_dlpi_deferred = NULL;
18438 	mutex_exit(&ill->ill_lock);
18439 
18440 	for (; mp != NULL; mp = nextmp) {
18441 		nextmp = mp->b_next;
18442 		mp->b_next = NULL;
18443 		ill_dlpi_send(ill, mp);
18444 	}
18445 }
18446 
18447 /*
18448  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18449  */
18450 boolean_t
18451 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18452 {
18453 	t_uscalar_t prim_pending;
18454 
18455 	mutex_enter(&ill->ill_lock);
18456 	prim_pending = ill->ill_dlpi_pending;
18457 	mutex_exit(&ill->ill_lock);
18458 
18459 	/*
18460 	 * During teardown, ill_dlpi_send_deferred() will send requests
18461 	 * without waiting; don't bother printing any warnings in that case.
18462 	 */
18463 	if (!(ill->ill_flags & ILL_CONDEMNED) && prim_pending != prim) {
18464 		if (prim_pending == DL_PRIM_INVAL) {
18465 			(void) mi_strlog(ill->ill_rq, 1,
18466 			    SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received "
18467 			    "unsolicited ack for %s on %s\n",
18468 			    dlpi_prim_str(prim), ill->ill_name);
18469 		} else {
18470 			(void) mi_strlog(ill->ill_rq, 1,
18471 			    SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received "
18472 			    "unexpected ack for %s on %s (expecting %s)\n",
18473 			    dlpi_prim_str(prim), ill->ill_name,
18474 			    dlpi_prim_str(prim_pending));
18475 		}
18476 	}
18477 	return (prim_pending == prim);
18478 }
18479 
18480 /*
18481  * Called when an DLPI control message has been acked or nacked to
18482  * send down the next queued message (if any).
18483  */
18484 void
18485 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18486 {
18487 	mblk_t *mp;
18488 
18489 	ASSERT(IAM_WRITER_ILL(ill));
18490 	mutex_enter(&ill->ill_lock);
18491 
18492 	ASSERT(prim != DL_PRIM_INVAL);
18493 	ASSERT(ill->ill_dlpi_pending == prim);
18494 
18495 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18496 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18497 
18498 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18499 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18500 		cv_signal(&ill->ill_cv);
18501 		mutex_exit(&ill->ill_lock);
18502 		return;
18503 	}
18504 
18505 	ill->ill_dlpi_deferred = mp->b_next;
18506 	mp->b_next = NULL;
18507 	mutex_exit(&ill->ill_lock);
18508 
18509 	ill_dlpi_dispatch(ill, mp);
18510 }
18511 
18512 void
18513 conn_delete_ire(conn_t *connp, caddr_t arg)
18514 {
18515 	ipif_t	*ipif = (ipif_t *)arg;
18516 	ire_t	*ire;
18517 
18518 	/*
18519 	 * Look at the cached ires on conns which has pointers to ipifs.
18520 	 * We just call ire_refrele which clears up the reference
18521 	 * to ire. Called when a conn closes. Also called from ipif_free
18522 	 * to cleanup indirect references to the stale ipif via the cached ire.
18523 	 */
18524 	mutex_enter(&connp->conn_lock);
18525 	ire = connp->conn_ire_cache;
18526 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18527 		connp->conn_ire_cache = NULL;
18528 		mutex_exit(&connp->conn_lock);
18529 		IRE_REFRELE_NOTR(ire);
18530 		return;
18531 	}
18532 	mutex_exit(&connp->conn_lock);
18533 
18534 }
18535 
18536 /*
18537  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18538  * of IREs. Those IREs may have been previously cached in the conn structure.
18539  * This ipcl_walk() walker function releases all references to such IREs based
18540  * on the condemned flag.
18541  */
18542 /* ARGSUSED */
18543 void
18544 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18545 {
18546 	ire_t	*ire;
18547 
18548 	mutex_enter(&connp->conn_lock);
18549 	ire = connp->conn_ire_cache;
18550 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18551 		connp->conn_ire_cache = NULL;
18552 		mutex_exit(&connp->conn_lock);
18553 		IRE_REFRELE_NOTR(ire);
18554 		return;
18555 	}
18556 	mutex_exit(&connp->conn_lock);
18557 }
18558 
18559 /*
18560  * Take down a specific interface, but don't lose any information about it.
18561  * Also delete interface from its interface group (ifgrp).
18562  * (Always called as writer.)
18563  * This function goes through the down sequence even if the interface is
18564  * already down. There are 2 reasons.
18565  * a. Currently we permit interface routes that depend on down interfaces
18566  *    to be added. This behaviour itself is questionable. However it appears
18567  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18568  *    time. We go thru the cleanup in order to remove these routes.
18569  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18570  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18571  *    down, but we need to cleanup i.e. do ill_dl_down and
18572  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18573  *
18574  * IP-MT notes:
18575  *
18576  * Model of reference to interfaces.
18577  *
18578  * The following members in ipif_t track references to the ipif.
18579  *	int     ipif_refcnt;    Active reference count
18580  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18581  * The following members in ill_t track references to the ill.
18582  *	int             ill_refcnt;     active refcnt
18583  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18584  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18585  *
18586  * Reference to an ipif or ill can be obtained in any of the following ways.
18587  *
18588  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18589  * Pointers to ipif / ill from other data structures viz ire and conn.
18590  * Implicit reference to the ipif / ill by holding a reference to the ire.
18591  *
18592  * The ipif/ill lookup functions return a reference held ipif / ill.
18593  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18594  * This is a purely dynamic reference count associated with threads holding
18595  * references to the ipif / ill. Pointers from other structures do not
18596  * count towards this reference count.
18597  *
18598  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18599  * ipif/ill. This is incremented whenever a new ire is created referencing the
18600  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18601  * actually added to the ire hash table. The count is decremented in
18602  * ire_inactive where the ire is destroyed.
18603  *
18604  * nce's reference ill's thru nce_ill and the count of nce's associated with
18605  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18606  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18607  * table. Similarly it is decremented in ndp_inactive() where the nce
18608  * is destroyed.
18609  *
18610  * Flow of ioctls involving interface down/up
18611  *
18612  * The following is the sequence of an attempt to set some critical flags on an
18613  * up interface.
18614  * ip_sioctl_flags
18615  * ipif_down
18616  * wait for ipif to be quiescent
18617  * ipif_down_tail
18618  * ip_sioctl_flags_tail
18619  *
18620  * All set ioctls that involve down/up sequence would have a skeleton similar
18621  * to the above. All the *tail functions are called after the refcounts have
18622  * dropped to the appropriate values.
18623  *
18624  * The mechanism to quiesce an ipif is as follows.
18625  *
18626  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18627  * on the ipif. Callers either pass a flag requesting wait or the lookup
18628  *  functions will return NULL.
18629  *
18630  * Delete all ires referencing this ipif
18631  *
18632  * Any thread attempting to do an ipif_refhold on an ipif that has been
18633  * obtained thru a cached pointer will first make sure that
18634  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18635  * increment the refcount.
18636  *
18637  * The above guarantees that the ipif refcount will eventually come down to
18638  * zero and the ipif will quiesce, once all threads that currently hold a
18639  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18640  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18641  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
18642  * drop to zero.
18643  *
18644  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18645  *
18646  * Threads trying to lookup an ipif or ill can pass a flag requesting
18647  * wait and restart if the ipif / ill cannot be looked up currently.
18648  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18649  * failure if the ipif is currently undergoing an exclusive operation, and
18650  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18651  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18652  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18653  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18654  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18655  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18656  * until we release the ipsq_lock, even though the the ill/ipif state flags
18657  * can change after we drop the ill_lock.
18658  *
18659  * An attempt to send out a packet using an ipif that is currently
18660  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18661  * operation and restart it later when the exclusive condition on the ipif ends.
18662  * This is an example of not passing the wait flag to the lookup functions. For
18663  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18664  * out a multicast packet on that ipif will fail while the ipif is
18665  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18666  * currently IPIF_CHANGING will also fail.
18667  */
18668 int
18669 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18670 {
18671 	ill_t		*ill = ipif->ipif_ill;
18672 	phyint_t	*phyi;
18673 	conn_t		*connp;
18674 	boolean_t	success;
18675 	boolean_t	ipif_was_up = B_FALSE;
18676 	ip_stack_t	*ipst = ill->ill_ipst;
18677 
18678 	ASSERT(IAM_WRITER_IPIF(ipif));
18679 
18680 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18681 
18682 	if (ipif->ipif_flags & IPIF_UP) {
18683 		mutex_enter(&ill->ill_lock);
18684 		ipif->ipif_flags &= ~IPIF_UP;
18685 		ASSERT(ill->ill_ipif_up_count > 0);
18686 		--ill->ill_ipif_up_count;
18687 		mutex_exit(&ill->ill_lock);
18688 		ipif_was_up = B_TRUE;
18689 		/* Update status in SCTP's list */
18690 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18691 	}
18692 
18693 	/*
18694 	 * Blow away memberships we established in ipif_multicast_up().
18695 	 */
18696 	ipif_multicast_down(ipif);
18697 
18698 	/*
18699 	 * Remove from the mapping for __sin6_src_id. We insert only
18700 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18701 	 * stored as mapped addresses, we need to check for mapped
18702 	 * INADDR_ANY also.
18703 	 */
18704 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18705 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18706 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18707 		int err;
18708 
18709 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18710 		    ipif->ipif_zoneid, ipst);
18711 		if (err != 0) {
18712 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18713 		}
18714 	}
18715 
18716 	/*
18717 	 * Before we delete the ill from the group (if any), we need
18718 	 * to make sure that we delete all the routes dependent on
18719 	 * this and also any ipifs dependent on this ipif for
18720 	 * source address. We need to do before we delete from
18721 	 * the group because
18722 	 *
18723 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18724 	 *
18725 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18726 	 *    for re-doing source address selection. Note that
18727 	 *    ipif_select_source[_v6] called from
18728 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18729 	 *    because we have already marked down here i.e cleared
18730 	 *    IPIF_UP.
18731 	 */
18732 	if (ipif->ipif_isv6) {
18733 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18734 		    ipst);
18735 	} else {
18736 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18737 		    ipst);
18738 	}
18739 
18740 	/*
18741 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18742 	 * ires have been deleted above. Otherwise a thread could end up
18743 	 * caching an ire in a conn after we have finished the cleanup of the
18744 	 * conn. The caching is done after making sure that the ire is not yet
18745 	 * condemned. Also documented in the block comment above ip_output
18746 	 */
18747 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18748 	/* Also, delete the ires cached in SCTP */
18749 	sctp_ire_cache_flush(ipif);
18750 
18751 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
18752 	nattymod_clean_ipif(ipif);
18753 
18754 	/*
18755 	 * Update any other ipifs which have used "our" local address as
18756 	 * a source address. This entails removing and recreating IRE_INTERFACE
18757 	 * entries for such ipifs.
18758 	 */
18759 	if (ipif->ipif_isv6)
18760 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18761 	else
18762 		ipif_update_other_ipifs(ipif, ill->ill_group);
18763 
18764 	if (ipif_was_up) {
18765 		/*
18766 		 * Check whether it is last ipif to leave this group.
18767 		 * If this is the last ipif to leave, we should remove
18768 		 * this ill from the group as ipif_select_source will not
18769 		 * be able to find any useful ipifs if this ill is selected
18770 		 * for load balancing.
18771 		 *
18772 		 * For nameless groups, we should call ifgrp_delete if this
18773 		 * belongs to some group. As this ipif is going down, we may
18774 		 * need to reconstruct groups.
18775 		 */
18776 		phyi = ill->ill_phyint;
18777 		/*
18778 		 * If the phyint_groupname_len is 0, it may or may not
18779 		 * be in the nameless group. If the phyint_groupname_len is
18780 		 * not 0, then this ill should be part of some group.
18781 		 * As we always insert this ill in the group if
18782 		 * phyint_groupname_len is not zero when the first ipif
18783 		 * comes up (in ipif_up_done), it should be in a group
18784 		 * when the namelen is not 0.
18785 		 *
18786 		 * NOTE : When we delete the ill from the group,it will
18787 		 * blow away all the IRE_CACHES pointing either at this ipif or
18788 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18789 		 * should be pointing at this ill.
18790 		 */
18791 		ASSERT(phyi->phyint_groupname_len == 0 ||
18792 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18793 
18794 		if (phyi->phyint_groupname_len != 0) {
18795 			if (ill->ill_ipif_up_count == 0)
18796 				illgrp_delete(ill);
18797 		}
18798 
18799 		/*
18800 		 * If we have deleted some of the broadcast ires associated
18801 		 * with this ipif, we need to re-nominate somebody else if
18802 		 * the ires that we deleted were the nominated ones.
18803 		 */
18804 		if (ill->ill_group != NULL && !ill->ill_isv6)
18805 			ipif_renominate_bcast(ipif);
18806 	}
18807 
18808 	/*
18809 	 * neighbor-discovery or arp entries for this interface.
18810 	 */
18811 	ipif_ndp_down(ipif);
18812 
18813 	/*
18814 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18815 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18816 	 * and ill_delete -> ipif_free -> ipif_down
18817 	 */
18818 	if (mp == NULL) {
18819 		ASSERT(q == NULL);
18820 		return (0);
18821 	}
18822 
18823 	if (CONN_Q(q)) {
18824 		connp = Q_TO_CONN(q);
18825 		mutex_enter(&connp->conn_lock);
18826 	} else {
18827 		connp = NULL;
18828 	}
18829 	mutex_enter(&ill->ill_lock);
18830 	/*
18831 	 * Are there any ire's pointing to this ipif that are still active ?
18832 	 * If this is the last ipif going down, are there any ire's pointing
18833 	 * to this ill that are still active ?
18834 	 */
18835 	if (ipif_is_quiescent(ipif)) {
18836 		mutex_exit(&ill->ill_lock);
18837 		if (connp != NULL)
18838 			mutex_exit(&connp->conn_lock);
18839 		return (0);
18840 	}
18841 
18842 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18843 	    ill->ill_name, (void *)ill));
18844 	/*
18845 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18846 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18847 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18848 	 */
18849 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18850 	if (!success) {
18851 		/* The conn is closing. So just return */
18852 		ASSERT(connp != NULL);
18853 		mutex_exit(&ill->ill_lock);
18854 		mutex_exit(&connp->conn_lock);
18855 		return (EINTR);
18856 	}
18857 
18858 	mutex_exit(&ill->ill_lock);
18859 	if (connp != NULL)
18860 		mutex_exit(&connp->conn_lock);
18861 	return (EINPROGRESS);
18862 }
18863 
18864 void
18865 ipif_down_tail(ipif_t *ipif)
18866 {
18867 	ill_t	*ill = ipif->ipif_ill;
18868 
18869 	/*
18870 	 * Skip any loopback interface (null wq).
18871 	 * If this is the last logical interface on the ill
18872 	 * have ill_dl_down tell the driver we are gone (unbind)
18873 	 * Note that lun 0 can ipif_down even though
18874 	 * there are other logical units that are up.
18875 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18876 	 */
18877 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18878 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18879 	    ill->ill_dl_up) {
18880 		ill_dl_down(ill);
18881 	}
18882 	ill->ill_logical_down = 0;
18883 
18884 	/*
18885 	 * Have to be after removing the routes in ipif_down_delete_ire.
18886 	 */
18887 	if (ipif->ipif_isv6) {
18888 		if (ill->ill_flags & ILLF_XRESOLV)
18889 			ipif_arp_down(ipif);
18890 	} else {
18891 		ipif_arp_down(ipif);
18892 	}
18893 
18894 	ip_rts_ifmsg(ipif);
18895 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18896 }
18897 
18898 /*
18899  * Bring interface logically down without bringing the physical interface
18900  * down e.g. when the netmask is changed. This avoids long lasting link
18901  * negotiations between an ethernet interface and a certain switches.
18902  */
18903 static int
18904 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18905 {
18906 	/*
18907 	 * The ill_logical_down flag is a transient flag. It is set here
18908 	 * and is cleared once the down has completed in ipif_down_tail.
18909 	 * This flag does not indicate whether the ill stream is in the
18910 	 * DL_BOUND state with the driver. Instead this flag is used by
18911 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18912 	 * the driver. The state of the ill stream i.e. whether it is
18913 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18914 	 */
18915 	ipif->ipif_ill->ill_logical_down = 1;
18916 	return (ipif_down(ipif, q, mp));
18917 }
18918 
18919 /*
18920  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18921  * If the usesrc client ILL is already part of a usesrc group or not,
18922  * in either case a ire_stq with the matching usesrc client ILL will
18923  * locate the IRE's that need to be deleted. We want IREs to be created
18924  * with the new source address.
18925  */
18926 static void
18927 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18928 {
18929 	ill_t	*ucill = (ill_t *)ill_arg;
18930 
18931 	ASSERT(IAM_WRITER_ILL(ucill));
18932 
18933 	if (ire->ire_stq == NULL)
18934 		return;
18935 
18936 	if ((ire->ire_type == IRE_CACHE) &&
18937 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18938 		ire_delete(ire);
18939 }
18940 
18941 /*
18942  * ire_walk routine to delete every IRE dependent on the interface
18943  * address that is going down.	(Always called as writer.)
18944  * Works for both v4 and v6.
18945  * In addition for checking for ire_ipif matches it also checks for
18946  * IRE_CACHE entries which have the same source address as the
18947  * disappearing ipif since ipif_select_source might have picked
18948  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18949  * care of any IRE_INTERFACE with the disappearing source address.
18950  */
18951 static void
18952 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18953 {
18954 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18955 	ill_t *ire_ill;
18956 	ill_t *ipif_ill;
18957 
18958 	ASSERT(IAM_WRITER_IPIF(ipif));
18959 	if (ire->ire_ipif == NULL)
18960 		return;
18961 
18962 	/*
18963 	 * For IPv4, we derive source addresses for an IRE from ipif's
18964 	 * belonging to the same IPMP group as the IRE's outgoing
18965 	 * interface.  If an IRE's outgoing interface isn't in the
18966 	 * same IPMP group as a particular ipif, then that ipif
18967 	 * couldn't have been used as a source address for this IRE.
18968 	 *
18969 	 * For IPv6, source addresses are only restricted to the IPMP group
18970 	 * if the IRE is for a link-local address or a multicast address.
18971 	 * Otherwise, source addresses for an IRE can be chosen from
18972 	 * interfaces other than the the outgoing interface for that IRE.
18973 	 *
18974 	 * For source address selection details, see ipif_select_source()
18975 	 * and ipif_select_source_v6().
18976 	 */
18977 	if (ire->ire_ipversion == IPV4_VERSION ||
18978 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18979 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18980 		ire_ill = ire->ire_ipif->ipif_ill;
18981 		ipif_ill = ipif->ipif_ill;
18982 
18983 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18984 			return;
18985 		}
18986 	}
18987 
18988 
18989 	if (ire->ire_ipif != ipif) {
18990 		/*
18991 		 * Look for a matching source address.
18992 		 */
18993 		if (ire->ire_type != IRE_CACHE)
18994 			return;
18995 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18996 			return;
18997 
18998 		if (ire->ire_ipversion == IPV4_VERSION) {
18999 			if (ire->ire_src_addr != ipif->ipif_src_addr)
19000 				return;
19001 		} else {
19002 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
19003 			    &ipif->ipif_v6lcl_addr))
19004 				return;
19005 		}
19006 		ire_delete(ire);
19007 		return;
19008 	}
19009 	/*
19010 	 * ire_delete() will do an ire_flush_cache which will delete
19011 	 * all ire_ipif matches
19012 	 */
19013 	ire_delete(ire);
19014 }
19015 
19016 /*
19017  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
19018  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
19019  * 2) when an interface is brought up or down (on that ill).
19020  * This ensures that the IRE_CACHE entries don't retain stale source
19021  * address selection results.
19022  */
19023 void
19024 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
19025 {
19026 	ill_t	*ill = (ill_t *)ill_arg;
19027 	ill_t	*ipif_ill;
19028 
19029 	ASSERT(IAM_WRITER_ILL(ill));
19030 	/*
19031 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19032 	 * Hence this should be IRE_CACHE.
19033 	 */
19034 	ASSERT(ire->ire_type == IRE_CACHE);
19035 
19036 	/*
19037 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
19038 	 * We are only interested in IRE_CACHES that has borrowed
19039 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
19040 	 * for which we need to look at ire_ipif->ipif_ill match
19041 	 * with ill.
19042 	 */
19043 	ASSERT(ire->ire_ipif != NULL);
19044 	ipif_ill = ire->ire_ipif->ipif_ill;
19045 	if (ipif_ill == ill || (ill->ill_group != NULL &&
19046 	    ipif_ill->ill_group == ill->ill_group)) {
19047 		ire_delete(ire);
19048 	}
19049 }
19050 
19051 /*
19052  * Delete all the ire whose stq references ill_arg.
19053  */
19054 static void
19055 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
19056 {
19057 	ill_t	*ill = (ill_t *)ill_arg;
19058 	ill_t	*ire_ill;
19059 
19060 	ASSERT(IAM_WRITER_ILL(ill));
19061 	/*
19062 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19063 	 * Hence this should be IRE_CACHE.
19064 	 */
19065 	ASSERT(ire->ire_type == IRE_CACHE);
19066 
19067 	/*
19068 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19069 	 * matches ill. We are only interested in IRE_CACHES that
19070 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19071 	 * filtering here.
19072 	 */
19073 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19074 
19075 	if (ire_ill == ill)
19076 		ire_delete(ire);
19077 }
19078 
19079 /*
19080  * This is called when an ill leaves the group. We want to delete
19081  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19082  * pointing at ill.
19083  */
19084 static void
19085 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19086 {
19087 	ill_t	*ill = (ill_t *)ill_arg;
19088 
19089 	ASSERT(IAM_WRITER_ILL(ill));
19090 	ASSERT(ill->ill_group == NULL);
19091 	/*
19092 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19093 	 * Hence this should be IRE_CACHE.
19094 	 */
19095 	ASSERT(ire->ire_type == IRE_CACHE);
19096 	/*
19097 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19098 	 * matches ill. We are interested in both.
19099 	 */
19100 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19101 	    (ire->ire_ipif->ipif_ill == ill));
19102 
19103 	ire_delete(ire);
19104 }
19105 
19106 /*
19107  * Initiate deallocate of an IPIF. Always called as writer. Called by
19108  * ill_delete or ip_sioctl_removeif.
19109  */
19110 static void
19111 ipif_free(ipif_t *ipif)
19112 {
19113 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19114 
19115 	ASSERT(IAM_WRITER_IPIF(ipif));
19116 
19117 	if (ipif->ipif_recovery_id != 0)
19118 		(void) untimeout(ipif->ipif_recovery_id);
19119 	ipif->ipif_recovery_id = 0;
19120 
19121 	/* Remove conn references */
19122 	reset_conn_ipif(ipif);
19123 
19124 	/*
19125 	 * Make sure we have valid net and subnet broadcast ire's for the
19126 	 * other ipif's which share them with this ipif.
19127 	 */
19128 	if (!ipif->ipif_isv6)
19129 		ipif_check_bcast_ires(ipif);
19130 
19131 	/*
19132 	 * Take down the interface. We can be called either from ill_delete
19133 	 * or from ip_sioctl_removeif.
19134 	 */
19135 	(void) ipif_down(ipif, NULL, NULL);
19136 
19137 	/*
19138 	 * Now that the interface is down, there's no chance it can still
19139 	 * become a duplicate.  Cancel any timer that may have been set while
19140 	 * tearing down.
19141 	 */
19142 	if (ipif->ipif_recovery_id != 0)
19143 		(void) untimeout(ipif->ipif_recovery_id);
19144 	ipif->ipif_recovery_id = 0;
19145 
19146 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19147 	/* Remove pointers to this ill in the multicast routing tables */
19148 	reset_mrt_vif_ipif(ipif);
19149 	rw_exit(&ipst->ips_ill_g_lock);
19150 }
19151 
19152 /*
19153  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19154  * also ill_move().
19155  */
19156 static void
19157 ipif_free_tail(ipif_t *ipif)
19158 {
19159 	mblk_t	*mp;
19160 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19161 
19162 	/*
19163 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19164 	 */
19165 	mutex_enter(&ipif->ipif_saved_ire_lock);
19166 	mp = ipif->ipif_saved_ire_mp;
19167 	ipif->ipif_saved_ire_mp = NULL;
19168 	mutex_exit(&ipif->ipif_saved_ire_lock);
19169 	freemsg(mp);
19170 
19171 	/*
19172 	 * Need to hold both ill_g_lock and ill_lock while
19173 	 * inserting or removing an ipif from the linked list
19174 	 * of ipifs hanging off the ill.
19175 	 */
19176 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19177 	/*
19178 	 * Remove all IPv4 multicast memberships on the interface now.
19179 	 * IPv6 is not handled here as the multicast memberships are
19180 	 * tied to the ill rather than the ipif.
19181 	 */
19182 	ilm_free(ipif);
19183 
19184 	/*
19185 	 * Since we held the ill_g_lock while doing the ilm_free above,
19186 	 * we can assert the ilms were really deleted and not just marked
19187 	 * ILM_DELETED.
19188 	 */
19189 	ASSERT(ilm_walk_ipif(ipif) == 0);
19190 
19191 	IPIF_TRACE_CLEANUP(ipif);
19192 
19193 	/* Ask SCTP to take it out of it list */
19194 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19195 
19196 	/* Get it out of the ILL interface list. */
19197 	ipif_remove(ipif, B_TRUE);
19198 	rw_exit(&ipst->ips_ill_g_lock);
19199 
19200 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19201 
19202 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19203 	ASSERT(ipif->ipif_recovery_id == 0);
19204 
19205 	/* Free the memory. */
19206 	mi_free(ipif);
19207 }
19208 
19209 /*
19210  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
19211  * "ill_name" otherwise.
19212  */
19213 char *
19214 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19215 {
19216 	char	lbuf[32];
19217 	char	*name;
19218 	size_t	name_len;
19219 
19220 	buf[0] = '\0';
19221 	if (!ipif)
19222 		return (buf);
19223 	name = ipif->ipif_ill->ill_name;
19224 	name_len = ipif->ipif_ill->ill_name_length;
19225 	if (ipif->ipif_id != 0) {
19226 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19227 		    ipif->ipif_id);
19228 		name = lbuf;
19229 		name_len = mi_strlen(name) + 1;
19230 	}
19231 	len -= 1;
19232 	buf[len] = '\0';
19233 	len = MIN(len, name_len);
19234 	bcopy(name, buf, len);
19235 	return (buf);
19236 }
19237 
19238 /*
19239  * Find an IPIF based on the name passed in.  Names can be of the
19240  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19241  * The <phys> string can have forms like <dev><#> (e.g., le0),
19242  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19243  * When there is no colon, the implied unit id is zero. <phys> must
19244  * correspond to the name of an ILL.  (May be called as writer.)
19245  */
19246 static ipif_t *
19247 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19248     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19249     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19250 {
19251 	char	*cp;
19252 	char	*endp;
19253 	long	id;
19254 	ill_t	*ill;
19255 	ipif_t	*ipif;
19256 	uint_t	ire_type;
19257 	boolean_t did_alloc = B_FALSE;
19258 	ipsq_t	*ipsq;
19259 
19260 	if (error != NULL)
19261 		*error = 0;
19262 
19263 	/*
19264 	 * If the caller wants to us to create the ipif, make sure we have a
19265 	 * valid zoneid
19266 	 */
19267 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19268 
19269 	if (namelen == 0) {
19270 		if (error != NULL)
19271 			*error = ENXIO;
19272 		return (NULL);
19273 	}
19274 
19275 	*exists = B_FALSE;
19276 	/* Look for a colon in the name. */
19277 	endp = &name[namelen];
19278 	for (cp = endp; --cp > name; ) {
19279 		if (*cp == IPIF_SEPARATOR_CHAR)
19280 			break;
19281 	}
19282 
19283 	if (*cp == IPIF_SEPARATOR_CHAR) {
19284 		/*
19285 		 * Reject any non-decimal aliases for logical
19286 		 * interfaces. Aliases with leading zeroes
19287 		 * are also rejected as they introduce ambiguity
19288 		 * in the naming of the interfaces.
19289 		 * In order to confirm with existing semantics,
19290 		 * and to not break any programs/script relying
19291 		 * on that behaviour, if<0>:0 is considered to be
19292 		 * a valid interface.
19293 		 *
19294 		 * If alias has two or more digits and the first
19295 		 * is zero, fail.
19296 		 */
19297 		if (&cp[2] < endp && cp[1] == '0')
19298 			return (NULL);
19299 	}
19300 
19301 	if (cp <= name) {
19302 		cp = endp;
19303 	} else {
19304 		*cp = '\0';
19305 	}
19306 
19307 	/*
19308 	 * Look up the ILL, based on the portion of the name
19309 	 * before the slash. ill_lookup_on_name returns a held ill.
19310 	 * Temporary to check whether ill exists already. If so
19311 	 * ill_lookup_on_name will clear it.
19312 	 */
19313 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19314 	    q, mp, func, error, &did_alloc, ipst);
19315 	if (cp != endp)
19316 		*cp = IPIF_SEPARATOR_CHAR;
19317 	if (ill == NULL)
19318 		return (NULL);
19319 
19320 	/* Establish the unit number in the name. */
19321 	id = 0;
19322 	if (cp < endp && *endp == '\0') {
19323 		/* If there was a colon, the unit number follows. */
19324 		cp++;
19325 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19326 			ill_refrele(ill);
19327 			if (error != NULL)
19328 				*error = ENXIO;
19329 			return (NULL);
19330 		}
19331 	}
19332 
19333 	GRAB_CONN_LOCK(q);
19334 	mutex_enter(&ill->ill_lock);
19335 	/* Now see if there is an IPIF with this unit number. */
19336 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19337 		if (ipif->ipif_id == id) {
19338 			if (zoneid != ALL_ZONES &&
19339 			    zoneid != ipif->ipif_zoneid &&
19340 			    ipif->ipif_zoneid != ALL_ZONES) {
19341 				mutex_exit(&ill->ill_lock);
19342 				RELEASE_CONN_LOCK(q);
19343 				ill_refrele(ill);
19344 				if (error != NULL)
19345 					*error = ENXIO;
19346 				return (NULL);
19347 			}
19348 			/*
19349 			 * The block comment at the start of ipif_down
19350 			 * explains the use of the macros used below
19351 			 */
19352 			if (IPIF_CAN_LOOKUP(ipif)) {
19353 				ipif_refhold_locked(ipif);
19354 				mutex_exit(&ill->ill_lock);
19355 				if (!did_alloc)
19356 					*exists = B_TRUE;
19357 				/*
19358 				 * Drop locks before calling ill_refrele
19359 				 * since it can potentially call into
19360 				 * ipif_ill_refrele_tail which can end up
19361 				 * in trying to acquire any lock.
19362 				 */
19363 				RELEASE_CONN_LOCK(q);
19364 				ill_refrele(ill);
19365 				return (ipif);
19366 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19367 				ipsq = ill->ill_phyint->phyint_ipsq;
19368 				mutex_enter(&ipsq->ipsq_lock);
19369 				mutex_exit(&ill->ill_lock);
19370 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19371 				mutex_exit(&ipsq->ipsq_lock);
19372 				RELEASE_CONN_LOCK(q);
19373 				ill_refrele(ill);
19374 				*error = EINPROGRESS;
19375 				return (NULL);
19376 			}
19377 		}
19378 	}
19379 	RELEASE_CONN_LOCK(q);
19380 
19381 	if (!do_alloc) {
19382 		mutex_exit(&ill->ill_lock);
19383 		ill_refrele(ill);
19384 		if (error != NULL)
19385 			*error = ENXIO;
19386 		return (NULL);
19387 	}
19388 
19389 	/*
19390 	 * If none found, atomically allocate and return a new one.
19391 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19392 	 * to support "receive only" use of lo0:1 etc. as is still done
19393 	 * below as an initial guess.
19394 	 * However, this is now likely to be overriden later in ipif_up_done()
19395 	 * when we know for sure what address has been configured on the
19396 	 * interface, since we might have more than one loopback interface
19397 	 * with a loopback address, e.g. in the case of zones, and all the
19398 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19399 	 */
19400 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19401 		ire_type = IRE_LOOPBACK;
19402 	else
19403 		ire_type = IRE_LOCAL;
19404 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19405 	if (ipif != NULL)
19406 		ipif_refhold_locked(ipif);
19407 	else if (error != NULL)
19408 		*error = ENOMEM;
19409 	mutex_exit(&ill->ill_lock);
19410 	ill_refrele(ill);
19411 	return (ipif);
19412 }
19413 
19414 /*
19415  * This routine is called whenever a new address comes up on an ipif.  If
19416  * we are configured to respond to address mask requests, then we are supposed
19417  * to broadcast an address mask reply at this time.  This routine is also
19418  * called if we are already up, but a netmask change is made.  This is legal
19419  * but might not make the system manager very popular.	(May be called
19420  * as writer.)
19421  */
19422 void
19423 ipif_mask_reply(ipif_t *ipif)
19424 {
19425 	icmph_t	*icmph;
19426 	ipha_t	*ipha;
19427 	mblk_t	*mp;
19428 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19429 
19430 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19431 
19432 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19433 		return;
19434 
19435 	/* ICMP mask reply is IPv4 only */
19436 	ASSERT(!ipif->ipif_isv6);
19437 	/* ICMP mask reply is not for a loopback interface */
19438 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19439 
19440 	mp = allocb(REPLY_LEN, BPRI_HI);
19441 	if (mp == NULL)
19442 		return;
19443 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19444 
19445 	ipha = (ipha_t *)mp->b_rptr;
19446 	bzero(ipha, REPLY_LEN);
19447 	*ipha = icmp_ipha;
19448 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19449 	ipha->ipha_src = ipif->ipif_src_addr;
19450 	ipha->ipha_dst = ipif->ipif_brd_addr;
19451 	ipha->ipha_length = htons(REPLY_LEN);
19452 	ipha->ipha_ident = 0;
19453 
19454 	icmph = (icmph_t *)&ipha[1];
19455 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19456 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19457 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19458 
19459 	put(ipif->ipif_wq, mp);
19460 
19461 #undef	REPLY_LEN
19462 }
19463 
19464 /*
19465  * When the mtu in the ipif changes, we call this routine through ire_walk
19466  * to update all the relevant IREs.
19467  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19468  */
19469 static void
19470 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19471 {
19472 	ipif_t *ipif = (ipif_t *)ipif_arg;
19473 
19474 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19475 		return;
19476 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19477 }
19478 
19479 /*
19480  * When the mtu in the ill changes, we call this routine through ire_walk
19481  * to update all the relevant IREs.
19482  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19483  */
19484 void
19485 ill_mtu_change(ire_t *ire, char *ill_arg)
19486 {
19487 	ill_t	*ill = (ill_t *)ill_arg;
19488 
19489 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19490 		return;
19491 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19492 }
19493 
19494 /*
19495  * Join the ipif specific multicast groups.
19496  * Must be called after a mapping has been set up in the resolver.  (Always
19497  * called as writer.)
19498  */
19499 void
19500 ipif_multicast_up(ipif_t *ipif)
19501 {
19502 	int err, index;
19503 	ill_t *ill;
19504 
19505 	ASSERT(IAM_WRITER_IPIF(ipif));
19506 
19507 	ill = ipif->ipif_ill;
19508 	index = ill->ill_phyint->phyint_ifindex;
19509 
19510 	ip1dbg(("ipif_multicast_up\n"));
19511 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19512 		return;
19513 
19514 	if (ipif->ipif_isv6) {
19515 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19516 			return;
19517 
19518 		/* Join the all hosts multicast address */
19519 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19520 		/*
19521 		 * Passing B_TRUE means we have to join the multicast
19522 		 * membership on this interface even though this is
19523 		 * FAILED. If we join on a different one in the group,
19524 		 * we will not be able to delete the membership later
19525 		 * as we currently don't track where we join when we
19526 		 * join within the kernel unlike applications where
19527 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19528 		 * for more on this.
19529 		 */
19530 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19531 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19532 		if (err != 0) {
19533 			ip0dbg(("ipif_multicast_up: "
19534 			    "all_hosts_mcast failed %d\n",
19535 			    err));
19536 			return;
19537 		}
19538 		/*
19539 		 * Enable multicast for the solicited node multicast address
19540 		 */
19541 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19542 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19543 
19544 			ipv6_multi.s6_addr32[3] |=
19545 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19546 
19547 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19548 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19549 			    NULL);
19550 			if (err != 0) {
19551 				ip0dbg(("ipif_multicast_up: solicited MC"
19552 				    " failed %d\n", err));
19553 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19554 				    ill, ill->ill_phyint->phyint_ifindex,
19555 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19556 				return;
19557 			}
19558 		}
19559 	} else {
19560 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19561 			return;
19562 
19563 		/* Join the all hosts multicast address */
19564 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19565 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19566 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19567 		if (err) {
19568 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19569 			return;
19570 		}
19571 	}
19572 	ipif->ipif_multicast_up = 1;
19573 }
19574 
19575 /*
19576  * Blow away any multicast groups that we joined in ipif_multicast_up().
19577  * (Explicit memberships are blown away in ill_leave_multicast() when the
19578  * ill is brought down.)
19579  */
19580 static void
19581 ipif_multicast_down(ipif_t *ipif)
19582 {
19583 	int err;
19584 
19585 	ASSERT(IAM_WRITER_IPIF(ipif));
19586 
19587 	ip1dbg(("ipif_multicast_down\n"));
19588 	if (!ipif->ipif_multicast_up)
19589 		return;
19590 
19591 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19592 
19593 	if (!ipif->ipif_isv6) {
19594 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19595 		    B_TRUE);
19596 		if (err != 0)
19597 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19598 
19599 		ipif->ipif_multicast_up = 0;
19600 		return;
19601 	}
19602 
19603 	/*
19604 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19605 	 * we should look for ilms on this ill rather than the ones that have
19606 	 * been failed over here.  They are here temporarily. As
19607 	 * ipif_multicast_up has joined on this ill, we should delete only
19608 	 * from this ill.
19609 	 */
19610 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19611 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19612 	    B_TRUE, B_TRUE);
19613 	if (err != 0) {
19614 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19615 		    err));
19616 	}
19617 	/*
19618 	 * Disable multicast for the solicited node multicast address
19619 	 */
19620 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19621 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19622 
19623 		ipv6_multi.s6_addr32[3] |=
19624 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19625 
19626 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19627 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19628 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19629 
19630 		if (err != 0) {
19631 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19632 			    err));
19633 		}
19634 	}
19635 
19636 	ipif->ipif_multicast_up = 0;
19637 }
19638 
19639 /*
19640  * Used when an interface comes up to recreate any extra routes on this
19641  * interface.
19642  */
19643 static ire_t **
19644 ipif_recover_ire(ipif_t *ipif)
19645 {
19646 	mblk_t	*mp;
19647 	ire_t	**ipif_saved_irep;
19648 	ire_t	**irep;
19649 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19650 
19651 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19652 	    ipif->ipif_id));
19653 
19654 	mutex_enter(&ipif->ipif_saved_ire_lock);
19655 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19656 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19657 	if (ipif_saved_irep == NULL) {
19658 		mutex_exit(&ipif->ipif_saved_ire_lock);
19659 		return (NULL);
19660 	}
19661 
19662 	irep = ipif_saved_irep;
19663 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19664 		ire_t		*ire;
19665 		queue_t		*rfq;
19666 		queue_t		*stq;
19667 		ifrt_t		*ifrt;
19668 		uchar_t		*src_addr;
19669 		uchar_t		*gateway_addr;
19670 		ushort_t	type;
19671 
19672 		/*
19673 		 * When the ire was initially created and then added in
19674 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19675 		 * in the case of a traditional interface route, or as one of
19676 		 * the IRE_OFFSUBNET types (with the exception of
19677 		 * IRE_HOST types ire which is created by icmp_redirect() and
19678 		 * which we don't need to save or recover).  In the case where
19679 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19680 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19681 		 * to satisfy software like GateD and Sun Cluster which creates
19682 		 * routes using the the loopback interface's address as a
19683 		 * gateway.
19684 		 *
19685 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19686 		 * ire_create() will be called in the same way here as
19687 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19688 		 * the route looks like a traditional interface route (where
19689 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19690 		 * the saved ifrt->ifrt_type.  This means that in the case where
19691 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19692 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19693 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19694 		 */
19695 		ifrt = (ifrt_t *)mp->b_rptr;
19696 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19697 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19698 			rfq = NULL;
19699 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19700 			    ? ipif->ipif_rq : ipif->ipif_wq;
19701 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19702 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19703 			    : (uint8_t *)&ipif->ipif_src_addr;
19704 			gateway_addr = NULL;
19705 			type = ipif->ipif_net_type;
19706 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19707 			/* Recover multiroute broadcast IRE. */
19708 			rfq = ipif->ipif_rq;
19709 			stq = ipif->ipif_wq;
19710 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19711 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19712 			    : (uint8_t *)&ipif->ipif_src_addr;
19713 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19714 			type = ifrt->ifrt_type;
19715 		} else {
19716 			rfq = NULL;
19717 			stq = NULL;
19718 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19719 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19720 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19721 			type = ifrt->ifrt_type;
19722 		}
19723 
19724 		/*
19725 		 * Create a copy of the IRE with the saved address and netmask.
19726 		 */
19727 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19728 		    "0x%x/0x%x\n",
19729 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19730 		    ntohl(ifrt->ifrt_addr),
19731 		    ntohl(ifrt->ifrt_mask)));
19732 		ire = ire_create(
19733 		    (uint8_t *)&ifrt->ifrt_addr,
19734 		    (uint8_t *)&ifrt->ifrt_mask,
19735 		    src_addr,
19736 		    gateway_addr,
19737 		    &ifrt->ifrt_max_frag,
19738 		    NULL,
19739 		    rfq,
19740 		    stq,
19741 		    type,
19742 		    ipif,
19743 		    0,
19744 		    0,
19745 		    0,
19746 		    ifrt->ifrt_flags,
19747 		    &ifrt->ifrt_iulp_info,
19748 		    NULL,
19749 		    NULL,
19750 		    ipst);
19751 
19752 		if (ire == NULL) {
19753 			mutex_exit(&ipif->ipif_saved_ire_lock);
19754 			kmem_free(ipif_saved_irep,
19755 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19756 			return (NULL);
19757 		}
19758 
19759 		/*
19760 		 * Some software (for example, GateD and Sun Cluster) attempts
19761 		 * to create (what amount to) IRE_PREFIX routes with the
19762 		 * loopback address as the gateway.  This is primarily done to
19763 		 * set up prefixes with the RTF_REJECT flag set (for example,
19764 		 * when generating aggregate routes.)
19765 		 *
19766 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19767 		 * IRE_LOOPBACK, then we map the request into a
19768 		 * IRE_IF_NORESOLVER.
19769 		 */
19770 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19771 			ire->ire_type = IRE_IF_NORESOLVER;
19772 		/*
19773 		 * ire held by ire_add, will be refreled' towards the
19774 		 * the end of ipif_up_done
19775 		 */
19776 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19777 		*irep = ire;
19778 		irep++;
19779 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19780 	}
19781 	mutex_exit(&ipif->ipif_saved_ire_lock);
19782 	return (ipif_saved_irep);
19783 }
19784 
19785 /*
19786  * Used to set the netmask and broadcast address to default values when the
19787  * interface is brought up.  (Always called as writer.)
19788  */
19789 static void
19790 ipif_set_default(ipif_t *ipif)
19791 {
19792 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19793 
19794 	if (!ipif->ipif_isv6) {
19795 		/*
19796 		 * Interface holds an IPv4 address. Default
19797 		 * mask is the natural netmask.
19798 		 */
19799 		if (!ipif->ipif_net_mask) {
19800 			ipaddr_t	v4mask;
19801 
19802 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19803 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19804 		}
19805 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19806 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19807 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19808 		} else {
19809 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19810 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19811 		}
19812 		/*
19813 		 * NOTE: SunOS 4.X does this even if the broadcast address
19814 		 * has been already set thus we do the same here.
19815 		 */
19816 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19817 			ipaddr_t	v4addr;
19818 
19819 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19820 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19821 		}
19822 	} else {
19823 		/*
19824 		 * Interface holds an IPv6-only address.  Default
19825 		 * mask is all-ones.
19826 		 */
19827 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19828 			ipif->ipif_v6net_mask = ipv6_all_ones;
19829 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19830 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19831 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19832 		} else {
19833 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19834 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19835 		}
19836 	}
19837 }
19838 
19839 /*
19840  * Return 0 if this address can be used as local address without causing
19841  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19842  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19843  * Special checks are needed to allow the same IPv6 link-local address
19844  * on different ills.
19845  * TODO: allowing the same site-local address on different ill's.
19846  */
19847 int
19848 ip_addr_availability_check(ipif_t *new_ipif)
19849 {
19850 	in6_addr_t our_v6addr;
19851 	ill_t *ill;
19852 	ipif_t *ipif;
19853 	ill_walk_context_t ctx;
19854 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19855 
19856 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19857 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19858 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19859 
19860 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19861 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19862 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19863 		return (0);
19864 
19865 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19866 
19867 	if (new_ipif->ipif_isv6)
19868 		ill = ILL_START_WALK_V6(&ctx, ipst);
19869 	else
19870 		ill = ILL_START_WALK_V4(&ctx, ipst);
19871 
19872 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19873 		for (ipif = ill->ill_ipif; ipif != NULL;
19874 		    ipif = ipif->ipif_next) {
19875 			if ((ipif == new_ipif) ||
19876 			    !(ipif->ipif_flags & IPIF_UP) ||
19877 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19878 				continue;
19879 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19880 			    &our_v6addr)) {
19881 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19882 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19883 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19884 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19885 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19886 				    new_ipif->ipif_ill != ill)
19887 					continue;
19888 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19889 				    new_ipif->ipif_ill != ill)
19890 					continue;
19891 				else if (new_ipif->ipif_zoneid !=
19892 				    ipif->ipif_zoneid &&
19893 				    ipif->ipif_zoneid != ALL_ZONES &&
19894 				    IS_LOOPBACK(ill))
19895 					continue;
19896 				else if (new_ipif->ipif_ill == ill)
19897 					return (EADDRINUSE);
19898 				else
19899 					return (EADDRNOTAVAIL);
19900 			}
19901 		}
19902 	}
19903 
19904 	return (0);
19905 }
19906 
19907 /*
19908  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19909  * IREs for the ipif.
19910  * When the routine returns EINPROGRESS then mp has been consumed and
19911  * the ioctl will be acked from ip_rput_dlpi.
19912  */
19913 static int
19914 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19915 {
19916 	ill_t	*ill = ipif->ipif_ill;
19917 	boolean_t isv6 = ipif->ipif_isv6;
19918 	int	err = 0;
19919 	boolean_t success;
19920 
19921 	ASSERT(IAM_WRITER_IPIF(ipif));
19922 
19923 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19924 
19925 	/* Shouldn't get here if it is already up. */
19926 	if (ipif->ipif_flags & IPIF_UP)
19927 		return (EALREADY);
19928 
19929 	/* Skip arp/ndp for any loopback interface. */
19930 	if (ill->ill_wq != NULL) {
19931 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19932 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19933 
19934 		if (!ill->ill_dl_up) {
19935 			/*
19936 			 * ill_dl_up is not yet set. i.e. we are yet to
19937 			 * DL_BIND with the driver and this is the first
19938 			 * logical interface on the ill to become "up".
19939 			 * Tell the driver to get going (via DL_BIND_REQ).
19940 			 * Note that changing "significant" IFF_ flags
19941 			 * address/netmask etc cause a down/up dance, but
19942 			 * does not cause an unbind (DL_UNBIND) with the driver
19943 			 */
19944 			return (ill_dl_up(ill, ipif, mp, q));
19945 		}
19946 
19947 		/*
19948 		 * ipif_resolver_up may end up sending an
19949 		 * AR_INTERFACE_UP message to ARP, which would, in
19950 		 * turn send a DLPI message to the driver. ioctls are
19951 		 * serialized and so we cannot send more than one
19952 		 * interface up message at a time. If ipif_resolver_up
19953 		 * does send an interface up message to ARP, we get
19954 		 * EINPROGRESS and we will complete in ip_arp_done.
19955 		 */
19956 
19957 		ASSERT(connp != NULL || !CONN_Q(q));
19958 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19959 		if (connp != NULL)
19960 			mutex_enter(&connp->conn_lock);
19961 		mutex_enter(&ill->ill_lock);
19962 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19963 		mutex_exit(&ill->ill_lock);
19964 		if (connp != NULL)
19965 			mutex_exit(&connp->conn_lock);
19966 		if (!success)
19967 			return (EINTR);
19968 
19969 		/*
19970 		 * Crank up IPv6 neighbor discovery
19971 		 * Unlike ARP, this should complete when
19972 		 * ipif_ndp_up returns. However, for
19973 		 * ILLF_XRESOLV interfaces we also send a
19974 		 * AR_INTERFACE_UP to the external resolver.
19975 		 * That ioctl will complete in ip_rput.
19976 		 */
19977 		if (isv6) {
19978 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr);
19979 			if (err != 0) {
19980 				if (err != EINPROGRESS)
19981 					mp = ipsq_pending_mp_get(ipsq, &connp);
19982 				return (err);
19983 			}
19984 		}
19985 		/* Now, ARP */
19986 		err = ipif_resolver_up(ipif, Res_act_initial);
19987 		if (err == EINPROGRESS) {
19988 			/* We will complete it in ip_arp_done */
19989 			return (err);
19990 		}
19991 		mp = ipsq_pending_mp_get(ipsq, &connp);
19992 		ASSERT(mp != NULL);
19993 		if (err != 0)
19994 			return (err);
19995 	} else {
19996 		/*
19997 		 * Interfaces without underlying hardware don't do duplicate
19998 		 * address detection.
19999 		 */
20000 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
20001 		ipif->ipif_addr_ready = 1;
20002 	}
20003 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
20004 }
20005 
20006 /*
20007  * Perform a bind for the physical device.
20008  * When the routine returns EINPROGRESS then mp has been consumed and
20009  * the ioctl will be acked from ip_rput_dlpi.
20010  * Allocate an unbind message and save it until ipif_down.
20011  */
20012 static int
20013 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
20014 {
20015 	areq_t	*areq;
20016 	mblk_t	*areq_mp = NULL;
20017 	mblk_t	*bind_mp = NULL;
20018 	mblk_t	*unbind_mp = NULL;
20019 	conn_t	*connp;
20020 	boolean_t success;
20021 	uint16_t sap_addr;
20022 
20023 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
20024 	ASSERT(IAM_WRITER_ILL(ill));
20025 	ASSERT(mp != NULL);
20026 
20027 	/* Create a resolver cookie for ARP */
20028 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
20029 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
20030 		if (areq_mp == NULL)
20031 			return (ENOMEM);
20032 
20033 		freemsg(ill->ill_resolver_mp);
20034 		ill->ill_resolver_mp = areq_mp;
20035 		areq = (areq_t *)areq_mp->b_rptr;
20036 		sap_addr = ill->ill_sap;
20037 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
20038 	}
20039 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
20040 	    DL_BIND_REQ);
20041 	if (bind_mp == NULL)
20042 		goto bad;
20043 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
20044 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
20045 
20046 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
20047 	if (unbind_mp == NULL)
20048 		goto bad;
20049 
20050 	/*
20051 	 * Record state needed to complete this operation when the
20052 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
20053 	 */
20054 	ASSERT(WR(q)->q_next == NULL);
20055 	connp = Q_TO_CONN(q);
20056 
20057 	mutex_enter(&connp->conn_lock);
20058 	mutex_enter(&ipif->ipif_ill->ill_lock);
20059 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20060 	mutex_exit(&ipif->ipif_ill->ill_lock);
20061 	mutex_exit(&connp->conn_lock);
20062 	if (!success)
20063 		goto bad;
20064 
20065 	/*
20066 	 * Save the unbind message for ill_dl_down(); it will be consumed when
20067 	 * the interface goes down.
20068 	 */
20069 	ASSERT(ill->ill_unbind_mp == NULL);
20070 	ill->ill_unbind_mp = unbind_mp;
20071 
20072 	ill_dlpi_send(ill, bind_mp);
20073 	/* Send down link-layer capabilities probe if not already done. */
20074 	ill_capability_probe(ill);
20075 
20076 	/*
20077 	 * Sysid used to rely on the fact that netboots set domainname
20078 	 * and the like. Now that miniroot boots aren't strictly netboots
20079 	 * and miniroot network configuration is driven from userland
20080 	 * these things still need to be set. This situation can be detected
20081 	 * by comparing the interface being configured here to the one
20082 	 * dhcack was set to reference by the boot loader. Once sysid is
20083 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20084 	 */
20085 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
20086 	    (strcmp(ill->ill_name, dhcack) == 0) &&
20087 	    (strlen(srpc_domain) == 0)) {
20088 		if (dhcpinit() != 0)
20089 			cmn_err(CE_WARN, "no cached dhcp response");
20090 	}
20091 
20092 	/*
20093 	 * This operation will complete in ip_rput_dlpi with either
20094 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20095 	 */
20096 	return (EINPROGRESS);
20097 bad:
20098 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20099 	/*
20100 	 * We don't have to check for possible removal from illgrp
20101 	 * as we have not yet inserted in illgrp. For groups
20102 	 * without names, this ipif is still not UP and hence
20103 	 * this could not have possibly had any influence in forming
20104 	 * groups.
20105 	 */
20106 
20107 	freemsg(bind_mp);
20108 	freemsg(unbind_mp);
20109 	return (ENOMEM);
20110 }
20111 
20112 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20113 
20114 /*
20115  * DLPI and ARP is up.
20116  * Create all the IREs associated with an interface bring up multicast.
20117  * Set the interface flag and finish other initialization
20118  * that potentially had to be differed to after DL_BIND_ACK.
20119  */
20120 int
20121 ipif_up_done(ipif_t *ipif)
20122 {
20123 	ire_t	*ire_array[20];
20124 	ire_t	**irep = ire_array;
20125 	ire_t	**irep1;
20126 	ipaddr_t net_mask = 0;
20127 	ipaddr_t subnet_mask, route_mask;
20128 	ill_t	*ill = ipif->ipif_ill;
20129 	queue_t	*stq;
20130 	ipif_t	 *src_ipif;
20131 	ipif_t   *tmp_ipif;
20132 	boolean_t	flush_ire_cache = B_TRUE;
20133 	int	err = 0;
20134 	phyint_t *phyi;
20135 	ire_t	**ipif_saved_irep = NULL;
20136 	int ipif_saved_ire_cnt;
20137 	int	cnt;
20138 	boolean_t	src_ipif_held = B_FALSE;
20139 	boolean_t	ire_added = B_FALSE;
20140 	boolean_t	loopback = B_FALSE;
20141 	ip_stack_t	*ipst = ill->ill_ipst;
20142 
20143 	ip1dbg(("ipif_up_done(%s:%u)\n",
20144 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20145 	/* Check if this is a loopback interface */
20146 	if (ipif->ipif_ill->ill_wq == NULL)
20147 		loopback = B_TRUE;
20148 
20149 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20150 	/*
20151 	 * If all other interfaces for this ill are down or DEPRECATED,
20152 	 * or otherwise unsuitable for source address selection, remove
20153 	 * any IRE_CACHE entries for this ill to make sure source
20154 	 * address selection gets to take this new ipif into account.
20155 	 * No need to hold ill_lock while traversing the ipif list since
20156 	 * we are writer
20157 	 */
20158 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20159 	    tmp_ipif = tmp_ipif->ipif_next) {
20160 		if (((tmp_ipif->ipif_flags &
20161 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20162 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20163 		    (tmp_ipif == ipif))
20164 			continue;
20165 		/* first useable pre-existing interface */
20166 		flush_ire_cache = B_FALSE;
20167 		break;
20168 	}
20169 	if (flush_ire_cache)
20170 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20171 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20172 
20173 	/*
20174 	 * Figure out which way the send-to queue should go.  Only
20175 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20176 	 * should show up here.
20177 	 */
20178 	switch (ill->ill_net_type) {
20179 	case IRE_IF_RESOLVER:
20180 		stq = ill->ill_rq;
20181 		break;
20182 	case IRE_IF_NORESOLVER:
20183 	case IRE_LOOPBACK:
20184 		stq = ill->ill_wq;
20185 		break;
20186 	default:
20187 		return (EINVAL);
20188 	}
20189 
20190 	if (IS_LOOPBACK(ill)) {
20191 		/*
20192 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20193 		 * ipif_lookup_on_name(), but in the case of zones we can have
20194 		 * several loopback addresses on lo0. So all the interfaces with
20195 		 * loopback addresses need to be marked IRE_LOOPBACK.
20196 		 */
20197 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20198 		    htonl(INADDR_LOOPBACK))
20199 			ipif->ipif_ire_type = IRE_LOOPBACK;
20200 		else
20201 			ipif->ipif_ire_type = IRE_LOCAL;
20202 	}
20203 
20204 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20205 		/*
20206 		 * Can't use our source address. Select a different
20207 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20208 		 */
20209 		src_ipif = ipif_select_source(ipif->ipif_ill,
20210 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20211 		if (src_ipif == NULL)
20212 			src_ipif = ipif;	/* Last resort */
20213 		else
20214 			src_ipif_held = B_TRUE;
20215 	} else {
20216 		src_ipif = ipif;
20217 	}
20218 
20219 	/* Create all the IREs associated with this interface */
20220 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20221 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20222 
20223 		/*
20224 		 * If we're on a labeled system then make sure that zone-
20225 		 * private addresses have proper remote host database entries.
20226 		 */
20227 		if (is_system_labeled() &&
20228 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20229 		    !tsol_check_interface_address(ipif))
20230 			return (EINVAL);
20231 
20232 		/* Register the source address for __sin6_src_id */
20233 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20234 		    ipif->ipif_zoneid, ipst);
20235 		if (err != 0) {
20236 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20237 			return (err);
20238 		}
20239 
20240 		/* If the interface address is set, create the local IRE. */
20241 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20242 		    (void *)ipif,
20243 		    ipif->ipif_ire_type,
20244 		    ntohl(ipif->ipif_lcl_addr)));
20245 		*irep++ = ire_create(
20246 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20247 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20248 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20249 		    NULL,				/* no gateway */
20250 		    &ip_loopback_mtuplus,		/* max frag size */
20251 		    NULL,
20252 		    ipif->ipif_rq,			/* recv-from queue */
20253 		    NULL,				/* no send-to queue */
20254 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20255 		    ipif,
20256 		    0,
20257 		    0,
20258 		    0,
20259 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20260 		    RTF_PRIVATE : 0,
20261 		    &ire_uinfo_null,
20262 		    NULL,
20263 		    NULL,
20264 		    ipst);
20265 	} else {
20266 		ip1dbg((
20267 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20268 		    ipif->ipif_ire_type,
20269 		    ntohl(ipif->ipif_lcl_addr),
20270 		    (uint_t)ipif->ipif_flags));
20271 	}
20272 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20273 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20274 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20275 	} else {
20276 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20277 	}
20278 
20279 	subnet_mask = ipif->ipif_net_mask;
20280 
20281 	/*
20282 	 * If mask was not specified, use natural netmask of
20283 	 * interface address. Also, store this mask back into the
20284 	 * ipif struct.
20285 	 */
20286 	if (subnet_mask == 0) {
20287 		subnet_mask = net_mask;
20288 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20289 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20290 		    ipif->ipif_v6subnet);
20291 	}
20292 
20293 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20294 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20295 	    ipif->ipif_subnet != INADDR_ANY) {
20296 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20297 
20298 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20299 			route_mask = IP_HOST_MASK;
20300 		} else {
20301 			route_mask = subnet_mask;
20302 		}
20303 
20304 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20305 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20306 		    (void *)ipif, (void *)ill,
20307 		    ill->ill_net_type,
20308 		    ntohl(ipif->ipif_subnet)));
20309 		*irep++ = ire_create(
20310 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20311 		    (uchar_t *)&route_mask,		/* mask */
20312 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20313 		    NULL,				/* no gateway */
20314 		    &ipif->ipif_mtu,			/* max frag */
20315 		    NULL,
20316 		    NULL,				/* no recv queue */
20317 		    stq,				/* send-to queue */
20318 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20319 		    ipif,
20320 		    0,
20321 		    0,
20322 		    0,
20323 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20324 		    &ire_uinfo_null,
20325 		    NULL,
20326 		    NULL,
20327 		    ipst);
20328 	}
20329 
20330 	/*
20331 	 * Create any necessary broadcast IREs.
20332 	 */
20333 	if ((ipif->ipif_subnet != INADDR_ANY) &&
20334 	    (ipif->ipif_flags & IPIF_BROADCAST))
20335 		irep = ipif_create_bcast_ires(ipif, irep);
20336 
20337 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20338 
20339 	/* If an earlier ire_create failed, get out now */
20340 	for (irep1 = irep; irep1 > ire_array; ) {
20341 		irep1--;
20342 		if (*irep1 == NULL) {
20343 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20344 			err = ENOMEM;
20345 			goto bad;
20346 		}
20347 	}
20348 
20349 	/*
20350 	 * Need to atomically check for ip_addr_availablity_check
20351 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20352 	 * from group also.The ill_g_lock is grabbed as reader
20353 	 * just to make sure no new ills or new ipifs are being added
20354 	 * to the system while we are checking the uniqueness of addresses.
20355 	 */
20356 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20357 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20358 	/* Mark it up, and increment counters. */
20359 	ipif->ipif_flags |= IPIF_UP;
20360 	ill->ill_ipif_up_count++;
20361 	err = ip_addr_availability_check(ipif);
20362 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20363 	rw_exit(&ipst->ips_ill_g_lock);
20364 
20365 	if (err != 0) {
20366 		/*
20367 		 * Our address may already be up on the same ill. In this case,
20368 		 * the ARP entry for our ipif replaced the one for the other
20369 		 * ipif. So we don't want to delete it (otherwise the other ipif
20370 		 * would be unable to send packets).
20371 		 * ip_addr_availability_check() identifies this case for us and
20372 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20373 		 * which is the expected error code.
20374 		 */
20375 		if (err == EADDRINUSE) {
20376 			freemsg(ipif->ipif_arp_del_mp);
20377 			ipif->ipif_arp_del_mp = NULL;
20378 			err = EADDRNOTAVAIL;
20379 		}
20380 		ill->ill_ipif_up_count--;
20381 		ipif->ipif_flags &= ~IPIF_UP;
20382 		goto bad;
20383 	}
20384 
20385 	/*
20386 	 * Add in all newly created IREs.  ire_create_bcast() has
20387 	 * already checked for duplicates of the IRE_BROADCAST type.
20388 	 * We want to add before we call ifgrp_insert which wants
20389 	 * to know whether IRE_IF_RESOLVER exists or not.
20390 	 *
20391 	 * NOTE : We refrele the ire though we may branch to "bad"
20392 	 *	  later on where we do ire_delete. This is okay
20393 	 *	  because nobody can delete it as we are running
20394 	 *	  exclusively.
20395 	 */
20396 	for (irep1 = irep; irep1 > ire_array; ) {
20397 		irep1--;
20398 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20399 		/*
20400 		 * refheld by ire_add. refele towards the end of the func
20401 		 */
20402 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20403 	}
20404 	ire_added = B_TRUE;
20405 	/*
20406 	 * Form groups if possible.
20407 	 *
20408 	 * If we are supposed to be in a ill_group with a name, insert it
20409 	 * now as we know that at least one ipif is UP. Otherwise form
20410 	 * nameless groups.
20411 	 *
20412 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20413 	 * this ipif into the appropriate interface group, or create a
20414 	 * new one. If this is already in a nameless group, we try to form
20415 	 * a bigger group looking at other ills potentially sharing this
20416 	 * ipif's prefix.
20417 	 */
20418 	phyi = ill->ill_phyint;
20419 	if (phyi->phyint_groupname_len != 0) {
20420 		ASSERT(phyi->phyint_groupname != NULL);
20421 		if (ill->ill_ipif_up_count == 1) {
20422 			ASSERT(ill->ill_group == NULL);
20423 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20424 			    phyi->phyint_groupname, NULL, B_TRUE);
20425 			if (err != 0) {
20426 				ip1dbg(("ipif_up_done: illgrp allocation "
20427 				    "failed, error %d\n", err));
20428 				goto bad;
20429 			}
20430 		}
20431 		ASSERT(ill->ill_group != NULL);
20432 	}
20433 
20434 	/*
20435 	 * When this is part of group, we need to make sure that
20436 	 * any broadcast ires created because of this ipif coming
20437 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20438 	 * so that we don't receive duplicate broadcast packets.
20439 	 */
20440 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20441 		ipif_renominate_bcast(ipif);
20442 
20443 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20444 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20445 	ipif_saved_irep = ipif_recover_ire(ipif);
20446 
20447 	if (!loopback) {
20448 		/*
20449 		 * If the broadcast address has been set, make sure it makes
20450 		 * sense based on the interface address.
20451 		 * Only match on ill since we are sharing broadcast addresses.
20452 		 */
20453 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20454 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20455 			ire_t	*ire;
20456 
20457 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20458 			    IRE_BROADCAST, ipif, ALL_ZONES,
20459 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20460 
20461 			if (ire == NULL) {
20462 				/*
20463 				 * If there isn't a matching broadcast IRE,
20464 				 * revert to the default for this netmask.
20465 				 */
20466 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20467 				mutex_enter(&ipif->ipif_ill->ill_lock);
20468 				ipif_set_default(ipif);
20469 				mutex_exit(&ipif->ipif_ill->ill_lock);
20470 			} else {
20471 				ire_refrele(ire);
20472 			}
20473 		}
20474 
20475 	}
20476 
20477 	/* This is the first interface on this ill */
20478 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20479 		/*
20480 		 * Need to recover all multicast memberships in the driver.
20481 		 * This had to be deferred until we had attached.
20482 		 */
20483 		ill_recover_multicast(ill);
20484 	}
20485 	/* Join the allhosts multicast address */
20486 	ipif_multicast_up(ipif);
20487 
20488 	if (!loopback) {
20489 		/*
20490 		 * See whether anybody else would benefit from the
20491 		 * new ipif that we added. We call this always rather
20492 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20493 		 * ipif is for the benefit of illgrp_insert (done above)
20494 		 * which does not do source address selection as it does
20495 		 * not want to re-create interface routes that we are
20496 		 * having reference to it here.
20497 		 */
20498 		ill_update_source_selection(ill);
20499 	}
20500 
20501 	for (irep1 = irep; irep1 > ire_array; ) {
20502 		irep1--;
20503 		if (*irep1 != NULL) {
20504 			/* was held in ire_add */
20505 			ire_refrele(*irep1);
20506 		}
20507 	}
20508 
20509 	cnt = ipif_saved_ire_cnt;
20510 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20511 		if (*irep1 != NULL) {
20512 			/* was held in ire_add */
20513 			ire_refrele(*irep1);
20514 		}
20515 	}
20516 
20517 	if (!loopback && ipif->ipif_addr_ready) {
20518 		/* Broadcast an address mask reply. */
20519 		ipif_mask_reply(ipif);
20520 	}
20521 	if (ipif_saved_irep != NULL) {
20522 		kmem_free(ipif_saved_irep,
20523 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20524 	}
20525 	if (src_ipif_held)
20526 		ipif_refrele(src_ipif);
20527 
20528 	/*
20529 	 * This had to be deferred until we had bound.  Tell routing sockets and
20530 	 * others that this interface is up if it looks like the address has
20531 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20532 	 * duplicate address detection to do its thing.
20533 	 */
20534 	if (ipif->ipif_addr_ready) {
20535 		ip_rts_ifmsg(ipif);
20536 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20537 		/* Let SCTP update the status for this ipif */
20538 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20539 	}
20540 	return (0);
20541 
20542 bad:
20543 	ip1dbg(("ipif_up_done: FAILED \n"));
20544 	/*
20545 	 * We don't have to bother removing from ill groups because
20546 	 *
20547 	 * 1) For groups with names, we insert only when the first ipif
20548 	 *    comes up. In that case if it fails, it will not be in any
20549 	 *    group. So, we need not try to remove for that case.
20550 	 *
20551 	 * 2) For groups without names, either we tried to insert ipif_ill
20552 	 *    in a group as singleton or found some other group to become
20553 	 *    a bigger group. For the former, if it fails we don't have
20554 	 *    anything to do as ipif_ill is not in the group and for the
20555 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20556 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20557 	 */
20558 	while (irep > ire_array) {
20559 		irep--;
20560 		if (*irep != NULL) {
20561 			ire_delete(*irep);
20562 			if (ire_added)
20563 				ire_refrele(*irep);
20564 		}
20565 	}
20566 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20567 
20568 	if (ipif_saved_irep != NULL) {
20569 		kmem_free(ipif_saved_irep,
20570 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20571 	}
20572 	if (src_ipif_held)
20573 		ipif_refrele(src_ipif);
20574 
20575 	ipif_arp_down(ipif);
20576 	return (err);
20577 }
20578 
20579 /*
20580  * Turn off the ARP with the ILLF_NOARP flag.
20581  */
20582 static int
20583 ill_arp_off(ill_t *ill)
20584 {
20585 	mblk_t	*arp_off_mp = NULL;
20586 	mblk_t	*arp_on_mp = NULL;
20587 
20588 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20589 
20590 	ASSERT(IAM_WRITER_ILL(ill));
20591 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20592 
20593 	/*
20594 	 * If the on message is still around we've already done
20595 	 * an arp_off without doing an arp_on thus there is no
20596 	 * work needed.
20597 	 */
20598 	if (ill->ill_arp_on_mp != NULL)
20599 		return (0);
20600 
20601 	/*
20602 	 * Allocate an ARP on message (to be saved) and an ARP off message
20603 	 */
20604 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20605 	if (!arp_off_mp)
20606 		return (ENOMEM);
20607 
20608 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20609 	if (!arp_on_mp)
20610 		goto failed;
20611 
20612 	ASSERT(ill->ill_arp_on_mp == NULL);
20613 	ill->ill_arp_on_mp = arp_on_mp;
20614 
20615 	/* Send an AR_INTERFACE_OFF request */
20616 	putnext(ill->ill_rq, arp_off_mp);
20617 	return (0);
20618 failed:
20619 
20620 	if (arp_off_mp)
20621 		freemsg(arp_off_mp);
20622 	return (ENOMEM);
20623 }
20624 
20625 /*
20626  * Turn on ARP by turning off the ILLF_NOARP flag.
20627  */
20628 static int
20629 ill_arp_on(ill_t *ill)
20630 {
20631 	mblk_t	*mp;
20632 
20633 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20634 
20635 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20636 
20637 	ASSERT(IAM_WRITER_ILL(ill));
20638 	/*
20639 	 * Send an AR_INTERFACE_ON request if we have already done
20640 	 * an arp_off (which allocated the message).
20641 	 */
20642 	if (ill->ill_arp_on_mp != NULL) {
20643 		mp = ill->ill_arp_on_mp;
20644 		ill->ill_arp_on_mp = NULL;
20645 		putnext(ill->ill_rq, mp);
20646 	}
20647 	return (0);
20648 }
20649 
20650 /*
20651  * Called after either deleting ill from the group or when setting
20652  * FAILED or STANDBY on the interface.
20653  */
20654 static void
20655 illgrp_reset_schednext(ill_t *ill)
20656 {
20657 	ill_group_t *illgrp;
20658 	ill_t *save_ill;
20659 
20660 	ASSERT(IAM_WRITER_ILL(ill));
20661 	/*
20662 	 * When called from illgrp_delete, ill_group will be non-NULL.
20663 	 * But when called from ip_sioctl_flags, it could be NULL if
20664 	 * somebody is setting FAILED/INACTIVE on some interface which
20665 	 * is not part of a group.
20666 	 */
20667 	illgrp = ill->ill_group;
20668 	if (illgrp == NULL)
20669 		return;
20670 	if (illgrp->illgrp_ill_schednext != ill)
20671 		return;
20672 
20673 	illgrp->illgrp_ill_schednext = NULL;
20674 	save_ill = ill;
20675 	/*
20676 	 * Choose a good ill to be the next one for
20677 	 * outbound traffic. As the flags FAILED/STANDBY is
20678 	 * not yet marked when called from ip_sioctl_flags,
20679 	 * we check for ill separately.
20680 	 */
20681 	for (ill = illgrp->illgrp_ill; ill != NULL;
20682 	    ill = ill->ill_group_next) {
20683 		if ((ill != save_ill) &&
20684 		    !(ill->ill_phyint->phyint_flags &
20685 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20686 			illgrp->illgrp_ill_schednext = ill;
20687 			return;
20688 		}
20689 	}
20690 }
20691 
20692 /*
20693  * Given an ill, find the next ill in the group to be scheduled.
20694  * (This should be called by ip_newroute() before ire_create().)
20695  * The passed in ill may be pulled out of the group, after we have picked
20696  * up a different outgoing ill from the same group. However ire add will
20697  * atomically check this.
20698  */
20699 ill_t *
20700 illgrp_scheduler(ill_t *ill)
20701 {
20702 	ill_t *retill;
20703 	ill_group_t *illgrp;
20704 	int illcnt;
20705 	int i;
20706 	uint64_t flags;
20707 	ip_stack_t	*ipst = ill->ill_ipst;
20708 
20709 	/*
20710 	 * We don't use a lock to check for the ill_group. If this ill
20711 	 * is currently being inserted we may end up just returning this
20712 	 * ill itself. That is ok.
20713 	 */
20714 	if (ill->ill_group == NULL) {
20715 		ill_refhold(ill);
20716 		return (ill);
20717 	}
20718 
20719 	/*
20720 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20721 	 * a set of stable ills. No ill can be added or deleted or change
20722 	 * group while we hold the reader lock.
20723 	 */
20724 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20725 	if ((illgrp = ill->ill_group) == NULL) {
20726 		rw_exit(&ipst->ips_ill_g_lock);
20727 		ill_refhold(ill);
20728 		return (ill);
20729 	}
20730 
20731 	illcnt = illgrp->illgrp_ill_count;
20732 	mutex_enter(&illgrp->illgrp_lock);
20733 	retill = illgrp->illgrp_ill_schednext;
20734 
20735 	if (retill == NULL)
20736 		retill = illgrp->illgrp_ill;
20737 
20738 	/*
20739 	 * We do a circular search beginning at illgrp_ill_schednext
20740 	 * or illgrp_ill. We don't check the flags against the ill lock
20741 	 * since it can change anytime. The ire creation will be atomic
20742 	 * and will fail if the ill is FAILED or OFFLINE.
20743 	 */
20744 	for (i = 0; i < illcnt; i++) {
20745 		flags = retill->ill_phyint->phyint_flags;
20746 
20747 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20748 		    ILL_CAN_LOOKUP(retill)) {
20749 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20750 			ill_refhold(retill);
20751 			break;
20752 		}
20753 		retill = retill->ill_group_next;
20754 		if (retill == NULL)
20755 			retill = illgrp->illgrp_ill;
20756 	}
20757 	mutex_exit(&illgrp->illgrp_lock);
20758 	rw_exit(&ipst->ips_ill_g_lock);
20759 
20760 	return (i == illcnt ? NULL : retill);
20761 }
20762 
20763 /*
20764  * Checks for availbility of a usable source address (if there is one) when the
20765  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20766  * this selection is done regardless of the destination.
20767  */
20768 boolean_t
20769 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20770 {
20771 	uint_t	ifindex;
20772 	ipif_t	*ipif = NULL;
20773 	ill_t	*uill;
20774 	boolean_t isv6;
20775 	ip_stack_t	*ipst = ill->ill_ipst;
20776 
20777 	ASSERT(ill != NULL);
20778 
20779 	isv6 = ill->ill_isv6;
20780 	ifindex = ill->ill_usesrc_ifindex;
20781 	if (ifindex != 0) {
20782 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20783 		    NULL, ipst);
20784 		if (uill == NULL)
20785 			return (NULL);
20786 		mutex_enter(&uill->ill_lock);
20787 		for (ipif = uill->ill_ipif; ipif != NULL;
20788 		    ipif = ipif->ipif_next) {
20789 			if (!IPIF_CAN_LOOKUP(ipif))
20790 				continue;
20791 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20792 				continue;
20793 			if (!(ipif->ipif_flags & IPIF_UP))
20794 				continue;
20795 			if (ipif->ipif_zoneid != zoneid)
20796 				continue;
20797 			if ((isv6 &&
20798 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20799 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20800 				continue;
20801 			mutex_exit(&uill->ill_lock);
20802 			ill_refrele(uill);
20803 			return (B_TRUE);
20804 		}
20805 		mutex_exit(&uill->ill_lock);
20806 		ill_refrele(uill);
20807 	}
20808 	return (B_FALSE);
20809 }
20810 
20811 /*
20812  * Determine the best source address given a destination address and an ill.
20813  * Prefers non-deprecated over deprecated but will return a deprecated
20814  * address if there is no other choice. If there is a usable source address
20815  * on the interface pointed to by ill_usesrc_ifindex then that is given
20816  * first preference.
20817  *
20818  * Returns NULL if there is no suitable source address for the ill.
20819  * This only occurs when there is no valid source address for the ill.
20820  */
20821 ipif_t *
20822 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20823 {
20824 	ipif_t *ipif;
20825 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20826 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20827 	int index = 0;
20828 	boolean_t wrapped = B_FALSE;
20829 	boolean_t same_subnet_only = B_FALSE;
20830 	boolean_t ipif_same_found, ipif_other_found;
20831 	boolean_t specific_found;
20832 	ill_t	*till, *usill = NULL;
20833 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20834 	ip_stack_t	*ipst = ill->ill_ipst;
20835 
20836 	if (ill->ill_usesrc_ifindex != 0) {
20837 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20838 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20839 		if (usill != NULL)
20840 			ill = usill;	/* Select source from usesrc ILL */
20841 		else
20842 			return (NULL);
20843 	}
20844 
20845 	/*
20846 	 * If we're dealing with an unlabeled destination on a labeled system,
20847 	 * make sure that we ignore source addresses that are incompatible with
20848 	 * the destination's default label.  That destination's default label
20849 	 * must dominate the minimum label on the source address.
20850 	 */
20851 	dst_rhtp = NULL;
20852 	if (is_system_labeled()) {
20853 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20854 		if (dst_rhtp == NULL)
20855 			return (NULL);
20856 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20857 			TPC_RELE(dst_rhtp);
20858 			dst_rhtp = NULL;
20859 		}
20860 	}
20861 
20862 	/*
20863 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20864 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20865 	 * After selecting the right ipif, under ill_lock make sure ipif is
20866 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20867 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20868 	 * but not under a lock.
20869 	 */
20870 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20871 
20872 retry:
20873 	till = ill;
20874 	ipif_arr[0] = NULL;
20875 
20876 	if (till->ill_group != NULL)
20877 		till = till->ill_group->illgrp_ill;
20878 
20879 	/*
20880 	 * Choose one good source address from each ill across the group.
20881 	 * If possible choose a source address in the same subnet as
20882 	 * the destination address.
20883 	 *
20884 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20885 	 * This is okay because of the following.
20886 	 *
20887 	 *    If PHYI_FAILED is set and we still have non-deprecated
20888 	 *    addresses, it means the addresses have not yet been
20889 	 *    failed over to a different interface. We potentially
20890 	 *    select them to create IRE_CACHES, which will be later
20891 	 *    flushed when the addresses move over.
20892 	 *
20893 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20894 	 *    addresses, it means either the user has configured them
20895 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20896 	 *    been moved over. For the former, in.mpathd does a failover
20897 	 *    when the interface becomes INACTIVE and hence we should
20898 	 *    not find them. Once INACTIVE is set, we don't allow them
20899 	 *    to create logical interfaces anymore. For the latter, a
20900 	 *    flush will happen when INACTIVE is cleared which will
20901 	 *    flush the IRE_CACHES.
20902 	 *
20903 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20904 	 *    over soon. We potentially select them to create IRE_CACHEs,
20905 	 *    which will be later flushed when the addresses move over.
20906 	 *
20907 	 * NOTE : As ipif_select_source is called to borrow source address
20908 	 * for an ipif that is part of a group, source address selection
20909 	 * will be re-done whenever the group changes i.e either an
20910 	 * insertion/deletion in the group.
20911 	 *
20912 	 * Fill ipif_arr[] with source addresses, using these rules:
20913 	 *
20914 	 *	1. At most one source address from a given ill ends up
20915 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20916 	 *	   associated with a given ill ends up in ipif_arr[].
20917 	 *
20918 	 *	2. If there is at least one non-deprecated ipif in the
20919 	 *	   IPMP group with a source address on the same subnet as
20920 	 *	   our destination, then fill ipif_arr[] only with
20921 	 *	   source addresses on the same subnet as our destination.
20922 	 *	   Note that because of (1), only the first
20923 	 *	   non-deprecated ipif found with a source address
20924 	 *	   matching the destination ends up in ipif_arr[].
20925 	 *
20926 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20927 	 *	   addresses not in the same subnet as our destination.
20928 	 *	   Again, because of (1), only the first off-subnet source
20929 	 *	   address will be chosen.
20930 	 *
20931 	 *	4. If there are no non-deprecated ipifs, then just use
20932 	 *	   the source address associated with the last deprecated
20933 	 *	   one we find that happens to be on the same subnet,
20934 	 *	   otherwise the first one not in the same subnet.
20935 	 */
20936 	specific_found = B_FALSE;
20937 	for (; till != NULL; till = till->ill_group_next) {
20938 		ipif_same_found = B_FALSE;
20939 		ipif_other_found = B_FALSE;
20940 		for (ipif = till->ill_ipif; ipif != NULL;
20941 		    ipif = ipif->ipif_next) {
20942 			if (!IPIF_CAN_LOOKUP(ipif))
20943 				continue;
20944 			/* Always skip NOLOCAL and ANYCAST interfaces */
20945 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20946 				continue;
20947 			if (!(ipif->ipif_flags & IPIF_UP) ||
20948 			    !ipif->ipif_addr_ready)
20949 				continue;
20950 			if (ipif->ipif_zoneid != zoneid &&
20951 			    ipif->ipif_zoneid != ALL_ZONES)
20952 				continue;
20953 			/*
20954 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20955 			 * but are not valid as source addresses.
20956 			 */
20957 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20958 				continue;
20959 
20960 			/*
20961 			 * Check compatibility of local address for
20962 			 * destination's default label if we're on a labeled
20963 			 * system.  Incompatible addresses can't be used at
20964 			 * all.
20965 			 */
20966 			if (dst_rhtp != NULL) {
20967 				boolean_t incompat;
20968 
20969 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20970 				    IPV4_VERSION, B_FALSE);
20971 				if (src_rhtp == NULL)
20972 					continue;
20973 				incompat =
20974 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20975 				    src_rhtp->tpc_tp.tp_doi !=
20976 				    dst_rhtp->tpc_tp.tp_doi ||
20977 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20978 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20979 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20980 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20981 				TPC_RELE(src_rhtp);
20982 				if (incompat)
20983 					continue;
20984 			}
20985 
20986 			/*
20987 			 * We prefer not to use all all-zones addresses, if we
20988 			 * can avoid it, as they pose problems with unlabeled
20989 			 * destinations.
20990 			 */
20991 			if (ipif->ipif_zoneid != ALL_ZONES) {
20992 				if (!specific_found &&
20993 				    (!same_subnet_only ||
20994 				    (ipif->ipif_net_mask & dst) ==
20995 				    ipif->ipif_subnet)) {
20996 					index = 0;
20997 					specific_found = B_TRUE;
20998 					ipif_other_found = B_FALSE;
20999 				}
21000 			} else {
21001 				if (specific_found)
21002 					continue;
21003 			}
21004 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
21005 				if (ipif_dep == NULL ||
21006 				    (ipif->ipif_net_mask & dst) ==
21007 				    ipif->ipif_subnet)
21008 					ipif_dep = ipif;
21009 				continue;
21010 			}
21011 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
21012 				/* found a source address in the same subnet */
21013 				if (!same_subnet_only) {
21014 					same_subnet_only = B_TRUE;
21015 					index = 0;
21016 				}
21017 				ipif_same_found = B_TRUE;
21018 			} else {
21019 				if (same_subnet_only || ipif_other_found)
21020 					continue;
21021 				ipif_other_found = B_TRUE;
21022 			}
21023 			ipif_arr[index++] = ipif;
21024 			if (index == MAX_IPIF_SELECT_SOURCE) {
21025 				wrapped = B_TRUE;
21026 				index = 0;
21027 			}
21028 			if (ipif_same_found)
21029 				break;
21030 		}
21031 	}
21032 
21033 	if (ipif_arr[0] == NULL) {
21034 		ipif = ipif_dep;
21035 	} else {
21036 		if (wrapped)
21037 			index = MAX_IPIF_SELECT_SOURCE;
21038 		ipif = ipif_arr[ipif_rand(ipst) % index];
21039 		ASSERT(ipif != NULL);
21040 	}
21041 
21042 	if (ipif != NULL) {
21043 		mutex_enter(&ipif->ipif_ill->ill_lock);
21044 		if (!IPIF_CAN_LOOKUP(ipif)) {
21045 			mutex_exit(&ipif->ipif_ill->ill_lock);
21046 			goto retry;
21047 		}
21048 		ipif_refhold_locked(ipif);
21049 		mutex_exit(&ipif->ipif_ill->ill_lock);
21050 	}
21051 
21052 	rw_exit(&ipst->ips_ill_g_lock);
21053 	if (usill != NULL)
21054 		ill_refrele(usill);
21055 	if (dst_rhtp != NULL)
21056 		TPC_RELE(dst_rhtp);
21057 
21058 #ifdef DEBUG
21059 	if (ipif == NULL) {
21060 		char buf1[INET6_ADDRSTRLEN];
21061 
21062 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
21063 		    ill->ill_name,
21064 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
21065 	} else {
21066 		char buf1[INET6_ADDRSTRLEN];
21067 		char buf2[INET6_ADDRSTRLEN];
21068 
21069 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
21070 		    ipif->ipif_ill->ill_name,
21071 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21072 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21073 		    buf2, sizeof (buf2))));
21074 	}
21075 #endif /* DEBUG */
21076 	return (ipif);
21077 }
21078 
21079 
21080 /*
21081  * If old_ipif is not NULL, see if ipif was derived from old
21082  * ipif and if so, recreate the interface route by re-doing
21083  * source address selection. This happens when ipif_down ->
21084  * ipif_update_other_ipifs calls us.
21085  *
21086  * If old_ipif is NULL, just redo the source address selection
21087  * if needed. This happens when illgrp_insert or ipif_up_done
21088  * calls us.
21089  */
21090 static void
21091 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21092 {
21093 	ire_t *ire;
21094 	ire_t *ipif_ire;
21095 	queue_t *stq;
21096 	ipif_t *nipif;
21097 	ill_t *ill;
21098 	boolean_t need_rele = B_FALSE;
21099 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21100 
21101 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21102 	ASSERT(IAM_WRITER_IPIF(ipif));
21103 
21104 	ill = ipif->ipif_ill;
21105 	if (!(ipif->ipif_flags &
21106 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21107 		/*
21108 		 * Can't possibly have borrowed the source
21109 		 * from old_ipif.
21110 		 */
21111 		return;
21112 	}
21113 
21114 	/*
21115 	 * Is there any work to be done? No work if the address
21116 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21117 	 * ipif_select_source() does not borrow addresses from
21118 	 * NOLOCAL and ANYCAST interfaces).
21119 	 */
21120 	if ((old_ipif != NULL) &&
21121 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21122 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21123 	    (old_ipif->ipif_flags &
21124 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21125 		return;
21126 	}
21127 
21128 	/*
21129 	 * Perform the same checks as when creating the
21130 	 * IRE_INTERFACE in ipif_up_done.
21131 	 */
21132 	if (!(ipif->ipif_flags & IPIF_UP))
21133 		return;
21134 
21135 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21136 	    (ipif->ipif_subnet == INADDR_ANY))
21137 		return;
21138 
21139 	ipif_ire = ipif_to_ire(ipif);
21140 	if (ipif_ire == NULL)
21141 		return;
21142 
21143 	/*
21144 	 * We know that ipif uses some other source for its
21145 	 * IRE_INTERFACE. Is it using the source of this
21146 	 * old_ipif?
21147 	 */
21148 	if (old_ipif != NULL &&
21149 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21150 		ire_refrele(ipif_ire);
21151 		return;
21152 	}
21153 	if (ip_debug > 2) {
21154 		/* ip1dbg */
21155 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21156 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21157 	}
21158 
21159 	stq = ipif_ire->ire_stq;
21160 
21161 	/*
21162 	 * Can't use our source address. Select a different
21163 	 * source address for the IRE_INTERFACE.
21164 	 */
21165 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21166 	if (nipif == NULL) {
21167 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21168 		nipif = ipif;
21169 	} else {
21170 		need_rele = B_TRUE;
21171 	}
21172 
21173 	ire = ire_create(
21174 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21175 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21176 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21177 	    NULL,				/* no gateway */
21178 	    &ipif->ipif_mtu,			/* max frag */
21179 	    NULL,				/* no src nce */
21180 	    NULL,				/* no recv from queue */
21181 	    stq,				/* send-to queue */
21182 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21183 	    ipif,
21184 	    0,
21185 	    0,
21186 	    0,
21187 	    0,
21188 	    &ire_uinfo_null,
21189 	    NULL,
21190 	    NULL,
21191 	    ipst);
21192 
21193 	if (ire != NULL) {
21194 		ire_t *ret_ire;
21195 		int error;
21196 
21197 		/*
21198 		 * We don't need ipif_ire anymore. We need to delete
21199 		 * before we add so that ire_add does not detect
21200 		 * duplicates.
21201 		 */
21202 		ire_delete(ipif_ire);
21203 		ret_ire = ire;
21204 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21205 		ASSERT(error == 0);
21206 		ASSERT(ire == ret_ire);
21207 		/* Held in ire_add */
21208 		ire_refrele(ret_ire);
21209 	}
21210 	/*
21211 	 * Either we are falling through from above or could not
21212 	 * allocate a replacement.
21213 	 */
21214 	ire_refrele(ipif_ire);
21215 	if (need_rele)
21216 		ipif_refrele(nipif);
21217 }
21218 
21219 /*
21220  * This old_ipif is going away.
21221  *
21222  * Determine if any other ipif's is using our address as
21223  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21224  * IPIF_DEPRECATED).
21225  * Find the IRE_INTERFACE for such ipifs and recreate them
21226  * to use an different source address following the rules in
21227  * ipif_up_done.
21228  *
21229  * This function takes an illgrp as an argument so that illgrp_delete
21230  * can call this to update source address even after deleting the
21231  * old_ipif->ipif_ill from the ill group.
21232  */
21233 static void
21234 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21235 {
21236 	ipif_t *ipif;
21237 	ill_t *ill;
21238 	char	buf[INET6_ADDRSTRLEN];
21239 
21240 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21241 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21242 
21243 	ill = old_ipif->ipif_ill;
21244 
21245 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21246 	    ill->ill_name,
21247 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21248 	    buf, sizeof (buf))));
21249 	/*
21250 	 * If this part of a group, look at all ills as ipif_select_source
21251 	 * borrows source address across all the ills in the group.
21252 	 */
21253 	if (illgrp != NULL)
21254 		ill = illgrp->illgrp_ill;
21255 
21256 	for (; ill != NULL; ill = ill->ill_group_next) {
21257 		for (ipif = ill->ill_ipif; ipif != NULL;
21258 		    ipif = ipif->ipif_next) {
21259 
21260 			if (ipif == old_ipif)
21261 				continue;
21262 
21263 			ipif_recreate_interface_routes(old_ipif, ipif);
21264 		}
21265 	}
21266 }
21267 
21268 /* ARGSUSED */
21269 int
21270 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21271 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21272 {
21273 	/*
21274 	 * ill_phyint_reinit merged the v4 and v6 into a single
21275 	 * ipsq. Could also have become part of a ipmp group in the
21276 	 * process, and we might not have been able to complete the
21277 	 * operation in ipif_set_values, if we could not become
21278 	 * exclusive.  If so restart it here.
21279 	 */
21280 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21281 }
21282 
21283 
21284 /*
21285  * Can operate on either a module or a driver queue.
21286  * Returns an error if not a module queue.
21287  */
21288 /* ARGSUSED */
21289 int
21290 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21291     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21292 {
21293 	queue_t		*q1 = q;
21294 	char 		*cp;
21295 	char		interf_name[LIFNAMSIZ];
21296 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21297 
21298 	if (q->q_next == NULL) {
21299 		ip1dbg((
21300 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21301 		return (EINVAL);
21302 	}
21303 
21304 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21305 		return (EALREADY);
21306 
21307 	do {
21308 		q1 = q1->q_next;
21309 	} while (q1->q_next);
21310 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21311 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21312 
21313 	/*
21314 	 * Here we are not going to delay the ioack until after
21315 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21316 	 * original ioctl message before sending the requests.
21317 	 */
21318 	return (ipif_set_values(q, mp, interf_name, &ppa));
21319 }
21320 
21321 /* ARGSUSED */
21322 int
21323 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21324     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21325 {
21326 	return (ENXIO);
21327 }
21328 
21329 /*
21330  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21331  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21332  * ire_check_and_create_bcast()).
21333  */
21334 static ire_t **
21335 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21336 {
21337 	ipaddr_t addr;
21338 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21339 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21340 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21341 
21342 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21343 
21344 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21345 
21346 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21347 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21348 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21349 
21350 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21351 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21352 
21353 	/*
21354 	 * For backward compatibility, we create net broadcast IREs based on
21355 	 * the old "IP address class system", since some old machines only
21356 	 * respond to these class derived net broadcast.  However, we must not
21357 	 * create these net broadcast IREs if the subnetmask is shorter than
21358 	 * the IP address class based derived netmask.  Otherwise, we may
21359 	 * create a net broadcast address which is the same as an IP address
21360 	 * on the subnet -- and then TCP will refuse to talk to that address.
21361 	 */
21362 	if (netmask < subnetmask) {
21363 		addr = netmask & ipif->ipif_subnet;
21364 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21365 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21366 		    flags);
21367 	}
21368 
21369 	/*
21370 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21371 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21372 	 * created.  Creating these broadcast IREs will only create confusion
21373 	 * as `addr' will be the same as the IP address.
21374 	 */
21375 	if (subnetmask != 0xFFFFFFFF) {
21376 		addr = ipif->ipif_subnet;
21377 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21378 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21379 		    irep, flags);
21380 	}
21381 
21382 	return (irep);
21383 }
21384 
21385 /*
21386  * Broadcast IRE info structure used in the functions below.  Since we
21387  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21388  */
21389 typedef struct bcast_ireinfo {
21390 	uchar_t		bi_type;	/* BCAST_* value from below */
21391 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21392 			bi_needrep:1,	/* do we need to replace it? */
21393 			bi_haverep:1,	/* have we replaced it? */
21394 			bi_pad:5;
21395 	ipaddr_t	bi_addr;	/* IRE address */
21396 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21397 } bcast_ireinfo_t;
21398 
21399 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21400 
21401 /*
21402  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21403  * return B_TRUE if it should immediately be used to recreate the IRE.
21404  */
21405 static boolean_t
21406 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21407 {
21408 	ipaddr_t addr;
21409 
21410 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21411 
21412 	switch (bireinfop->bi_type) {
21413 	case BCAST_NET:
21414 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21415 		if (addr != bireinfop->bi_addr)
21416 			return (B_FALSE);
21417 		break;
21418 	case BCAST_SUBNET:
21419 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21420 			return (B_FALSE);
21421 		break;
21422 	}
21423 
21424 	bireinfop->bi_needrep = 1;
21425 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21426 		if (bireinfop->bi_backup == NULL)
21427 			bireinfop->bi_backup = ipif;
21428 		return (B_FALSE);
21429 	}
21430 	return (B_TRUE);
21431 }
21432 
21433 /*
21434  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21435  * them ala ire_check_and_create_bcast().
21436  */
21437 static ire_t **
21438 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21439 {
21440 	ipaddr_t mask, addr;
21441 
21442 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21443 
21444 	addr = bireinfop->bi_addr;
21445 	irep = ire_create_bcast(ipif, addr, irep);
21446 
21447 	switch (bireinfop->bi_type) {
21448 	case BCAST_NET:
21449 		mask = ip_net_mask(ipif->ipif_subnet);
21450 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21451 		break;
21452 	case BCAST_SUBNET:
21453 		mask = ipif->ipif_net_mask;
21454 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21455 		break;
21456 	}
21457 
21458 	bireinfop->bi_haverep = 1;
21459 	return (irep);
21460 }
21461 
21462 /*
21463  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21464  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21465  * that are going away are still needed.  If so, have ipif_create_bcast()
21466  * recreate them (except for the deprecated case, as explained below).
21467  */
21468 static ire_t **
21469 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21470     ire_t **irep)
21471 {
21472 	int i;
21473 	ipif_t *ipif;
21474 
21475 	ASSERT(!ill->ill_isv6);
21476 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21477 		/*
21478 		 * Skip this ipif if it's (a) the one being taken down, (b)
21479 		 * not in the same zone, or (c) has no valid local address.
21480 		 */
21481 		if (ipif == test_ipif ||
21482 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21483 		    ipif->ipif_subnet == 0 ||
21484 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21485 		    (IPIF_UP|IPIF_BROADCAST))
21486 			continue;
21487 
21488 		/*
21489 		 * For each dying IRE that hasn't yet been replaced, see if
21490 		 * `ipif' needs it and whether the IRE should be recreated on
21491 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21492 		 * will return B_FALSE even if `ipif' needs the IRE on the
21493 		 * hopes that we'll later find a needy non-deprecated ipif.
21494 		 * However, the ipif is recorded in bi_backup for possible
21495 		 * subsequent use by ipif_check_bcast_ires().
21496 		 */
21497 		for (i = 0; i < BCAST_COUNT; i++) {
21498 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21499 				continue;
21500 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21501 				continue;
21502 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21503 		}
21504 
21505 		/*
21506 		 * If we've replaced all of the broadcast IREs that are going
21507 		 * to be taken down, we know we're done.
21508 		 */
21509 		for (i = 0; i < BCAST_COUNT; i++) {
21510 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21511 				break;
21512 		}
21513 		if (i == BCAST_COUNT)
21514 			break;
21515 	}
21516 	return (irep);
21517 }
21518 
21519 /*
21520  * Check if `test_ipif' (which is going away) is associated with any existing
21521  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21522  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21523  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21524  *
21525  * This is necessary because broadcast IREs are shared.  In particular, a
21526  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21527  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21528  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21529  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21530  * same zone, they will share the same set of broadcast IREs.
21531  *
21532  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21533  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21534  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21535  */
21536 static void
21537 ipif_check_bcast_ires(ipif_t *test_ipif)
21538 {
21539 	ill_t		*ill = test_ipif->ipif_ill;
21540 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21541 	ire_t		**irep1, **irep = &ire_array[0];
21542 	uint_t 		i, willdie;
21543 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21544 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21545 
21546 	ASSERT(!test_ipif->ipif_isv6);
21547 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21548 
21549 	/*
21550 	 * No broadcast IREs for the LOOPBACK interface
21551 	 * or others such as point to point and IPIF_NOXMIT.
21552 	 */
21553 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21554 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21555 		return;
21556 
21557 	bzero(bireinfo, sizeof (bireinfo));
21558 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21559 	bireinfo[0].bi_addr = 0;
21560 
21561 	bireinfo[1].bi_type = BCAST_ALLONES;
21562 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21563 
21564 	bireinfo[2].bi_type = BCAST_NET;
21565 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21566 
21567 	if (test_ipif->ipif_net_mask != 0)
21568 		mask = test_ipif->ipif_net_mask;
21569 	bireinfo[3].bi_type = BCAST_SUBNET;
21570 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21571 
21572 	/*
21573 	 * Figure out what (if any) broadcast IREs will die as a result of
21574 	 * `test_ipif' going away.  If none will die, we're done.
21575 	 */
21576 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21577 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21578 		    test_ipif, ALL_ZONES, NULL,
21579 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21580 		if (ire != NULL) {
21581 			willdie++;
21582 			bireinfo[i].bi_willdie = 1;
21583 			ire_refrele(ire);
21584 		}
21585 	}
21586 
21587 	if (willdie == 0)
21588 		return;
21589 
21590 	/*
21591 	 * Walk through all the ipifs that will be affected by the dying IREs,
21592 	 * and recreate the IREs as necessary.
21593 	 */
21594 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21595 
21596 	/*
21597 	 * Scan through the set of broadcast IREs and see if there are any
21598 	 * that we need to replace that have not yet been replaced.  If so,
21599 	 * replace them using the appropriate backup ipif.
21600 	 */
21601 	for (i = 0; i < BCAST_COUNT; i++) {
21602 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21603 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21604 			    &bireinfo[i], irep);
21605 	}
21606 
21607 	/*
21608 	 * If we can't create all of them, don't add any of them.  (Code in
21609 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21610 	 * non-loopback copy and loopback copy for a given address.)
21611 	 */
21612 	for (irep1 = irep; irep1 > ire_array; ) {
21613 		irep1--;
21614 		if (*irep1 == NULL) {
21615 			ip0dbg(("ipif_check_bcast_ires: can't create "
21616 			    "IRE_BROADCAST, memory allocation failure\n"));
21617 			while (irep > ire_array) {
21618 				irep--;
21619 				if (*irep != NULL)
21620 					ire_delete(*irep);
21621 			}
21622 			return;
21623 		}
21624 	}
21625 
21626 	for (irep1 = irep; irep1 > ire_array; ) {
21627 		irep1--;
21628 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21629 			ire_refrele(*irep1);		/* Held in ire_add */
21630 	}
21631 }
21632 
21633 /*
21634  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21635  * from lifr_flags and the name from lifr_name.
21636  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21637  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21638  * Returns EINPROGRESS when mp has been consumed by queueing it on
21639  * ill_pending_mp and the ioctl will complete in ip_rput.
21640  *
21641  * Can operate on either a module or a driver queue.
21642  * Returns an error if not a module queue.
21643  */
21644 /* ARGSUSED */
21645 int
21646 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21647     ip_ioctl_cmd_t *ipip, void *if_req)
21648 {
21649 	int	err;
21650 	ill_t	*ill;
21651 	struct lifreq *lifr = (struct lifreq *)if_req;
21652 
21653 	ASSERT(ipif != NULL);
21654 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21655 
21656 	if (q->q_next == NULL) {
21657 		ip1dbg((
21658 		    "if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21659 		return (EINVAL);
21660 	}
21661 
21662 	ill = (ill_t *)q->q_ptr;
21663 	/*
21664 	 * If we are not writer on 'q' then this interface exists already
21665 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
21666 	 * So return EALREADY
21667 	 */
21668 	if (ill != ipif->ipif_ill)
21669 		return (EALREADY);
21670 
21671 	if (ill->ill_name[0] != '\0')
21672 		return (EALREADY);
21673 
21674 	/*
21675 	 * Set all the flags. Allows all kinds of override. Provide some
21676 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21677 	 * unless there is either multicast/broadcast support in the driver
21678 	 * or it is a pt-pt link.
21679 	 */
21680 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21681 		/* Meaningless to IP thus don't allow them to be set. */
21682 		ip1dbg(("ip_setname: EINVAL 1\n"));
21683 		return (EINVAL);
21684 	}
21685 	/*
21686 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21687 	 * ill_bcast_addr_length info.
21688 	 */
21689 	if (!ill->ill_needs_attach &&
21690 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21691 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21692 	    ill->ill_bcast_addr_length == 0)) {
21693 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21694 		ip1dbg(("ip_setname: EINVAL 2\n"));
21695 		return (EINVAL);
21696 	}
21697 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21698 	    ((lifr->lifr_flags & IFF_IPV6) ||
21699 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21700 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21701 		ip1dbg(("ip_setname: EINVAL 3\n"));
21702 		return (EINVAL);
21703 	}
21704 	if (lifr->lifr_flags & IFF_UP) {
21705 		/* Can only be set with SIOCSLIFFLAGS */
21706 		ip1dbg(("ip_setname: EINVAL 4\n"));
21707 		return (EINVAL);
21708 	}
21709 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21710 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21711 		ip1dbg(("ip_setname: EINVAL 5\n"));
21712 		return (EINVAL);
21713 	}
21714 	/*
21715 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21716 	 */
21717 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21718 	    !(lifr->lifr_flags & IFF_IPV6) &&
21719 	    !(ipif->ipif_isv6)) {
21720 		ip1dbg(("ip_setname: EINVAL 6\n"));
21721 		return (EINVAL);
21722 	}
21723 
21724 	/*
21725 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21726 	 * we have all the flags here. So, we assign rather than we OR.
21727 	 * We can't OR the flags here because we don't want to set
21728 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21729 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21730 	 * on lifr_flags value here.
21731 	 */
21732 	/*
21733 	 * This ill has not been inserted into the global list.
21734 	 * So we are still single threaded and don't need any lock
21735 	 */
21736 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
21737 	    ~IFF_DUPLICATE;
21738 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21739 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21740 
21741 	/* We started off as V4. */
21742 	if (ill->ill_flags & ILLF_IPV6) {
21743 		ill->ill_phyint->phyint_illv6 = ill;
21744 		ill->ill_phyint->phyint_illv4 = NULL;
21745 	}
21746 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
21747 	return (err);
21748 }
21749 
21750 /* ARGSUSED */
21751 int
21752 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21753     ip_ioctl_cmd_t *ipip, void *if_req)
21754 {
21755 	/*
21756 	 * ill_phyint_reinit merged the v4 and v6 into a single
21757 	 * ipsq. Could also have become part of a ipmp group in the
21758 	 * process, and we might not have been able to complete the
21759 	 * slifname in ipif_set_values, if we could not become
21760 	 * exclusive.  If so restart it here
21761 	 */
21762 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21763 }
21764 
21765 /*
21766  * Return a pointer to the ipif which matches the index, IP version type and
21767  * zoneid.
21768  */
21769 ipif_t *
21770 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21771     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21772 {
21773 	ill_t	*ill;
21774 	ipsq_t  *ipsq;
21775 	phyint_t *phyi;
21776 	ipif_t	*ipif;
21777 
21778 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21779 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21780 
21781 	if (err != NULL)
21782 		*err = 0;
21783 
21784 	/*
21785 	 * Indexes are stored in the phyint - a common structure
21786 	 * to both IPv4 and IPv6.
21787 	 */
21788 
21789 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21790 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
21791 	    (void *) &index, NULL);
21792 	if (phyi != NULL) {
21793 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
21794 		if (ill == NULL) {
21795 			rw_exit(&ipst->ips_ill_g_lock);
21796 			if (err != NULL)
21797 				*err = ENXIO;
21798 			return (NULL);
21799 		}
21800 		GRAB_CONN_LOCK(q);
21801 		mutex_enter(&ill->ill_lock);
21802 		if (ILL_CAN_LOOKUP(ill)) {
21803 			for (ipif = ill->ill_ipif; ipif != NULL;
21804 			    ipif = ipif->ipif_next) {
21805 				if (IPIF_CAN_LOOKUP(ipif) &&
21806 				    (zoneid == ALL_ZONES ||
21807 				    zoneid == ipif->ipif_zoneid ||
21808 				    ipif->ipif_zoneid == ALL_ZONES)) {
21809 					ipif_refhold_locked(ipif);
21810 					mutex_exit(&ill->ill_lock);
21811 					RELEASE_CONN_LOCK(q);
21812 					rw_exit(&ipst->ips_ill_g_lock);
21813 					return (ipif);
21814 				}
21815 			}
21816 		} else if (ILL_CAN_WAIT(ill, q)) {
21817 			ipsq = ill->ill_phyint->phyint_ipsq;
21818 			mutex_enter(&ipsq->ipsq_lock);
21819 			rw_exit(&ipst->ips_ill_g_lock);
21820 			mutex_exit(&ill->ill_lock);
21821 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
21822 			mutex_exit(&ipsq->ipsq_lock);
21823 			RELEASE_CONN_LOCK(q);
21824 			*err = EINPROGRESS;
21825 			return (NULL);
21826 		}
21827 		mutex_exit(&ill->ill_lock);
21828 		RELEASE_CONN_LOCK(q);
21829 	}
21830 	rw_exit(&ipst->ips_ill_g_lock);
21831 	if (err != NULL)
21832 		*err = ENXIO;
21833 	return (NULL);
21834 }
21835 
21836 typedef struct conn_change_s {
21837 	uint_t cc_old_ifindex;
21838 	uint_t cc_new_ifindex;
21839 } conn_change_t;
21840 
21841 /*
21842  * ipcl_walk function for changing interface index.
21843  */
21844 static void
21845 conn_change_ifindex(conn_t *connp, caddr_t arg)
21846 {
21847 	conn_change_t *connc;
21848 	uint_t old_ifindex;
21849 	uint_t new_ifindex;
21850 	int i;
21851 	ilg_t *ilg;
21852 
21853 	connc = (conn_change_t *)arg;
21854 	old_ifindex = connc->cc_old_ifindex;
21855 	new_ifindex = connc->cc_new_ifindex;
21856 
21857 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21858 		connp->conn_orig_bound_ifindex = new_ifindex;
21859 
21860 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21861 		connp->conn_orig_multicast_ifindex = new_ifindex;
21862 
21863 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
21864 		connp->conn_orig_xmit_ifindex = new_ifindex;
21865 
21866 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21867 		ilg = &connp->conn_ilg[i];
21868 		if (ilg->ilg_orig_ifindex == old_ifindex)
21869 			ilg->ilg_orig_ifindex = new_ifindex;
21870 	}
21871 }
21872 
21873 /*
21874  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21875  * to new_index if it matches the old_index.
21876  *
21877  * Failovers typically happen within a group of ills. But somebody
21878  * can remove an ill from the group after a failover happened. If
21879  * we are setting the ifindex after this, we potentially need to
21880  * look at all the ills rather than just the ones in the group.
21881  * We cut down the work by looking at matching ill_net_types
21882  * and ill_types as we could not possibly grouped them together.
21883  */
21884 static void
21885 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21886 {
21887 	ill_t *ill;
21888 	ipif_t *ipif;
21889 	uint_t old_ifindex;
21890 	uint_t new_ifindex;
21891 	ilm_t *ilm;
21892 	ill_walk_context_t ctx;
21893 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21894 
21895 	old_ifindex = connc->cc_old_ifindex;
21896 	new_ifindex = connc->cc_new_ifindex;
21897 
21898 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21899 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21900 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21901 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21902 		    (ill_orig->ill_type != ill->ill_type)) {
21903 			continue;
21904 		}
21905 		for (ipif = ill->ill_ipif; ipif != NULL;
21906 		    ipif = ipif->ipif_next) {
21907 			if (ipif->ipif_orig_ifindex == old_ifindex)
21908 				ipif->ipif_orig_ifindex = new_ifindex;
21909 		}
21910 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21911 			if (ilm->ilm_orig_ifindex == old_ifindex)
21912 				ilm->ilm_orig_ifindex = new_ifindex;
21913 		}
21914 	}
21915 	rw_exit(&ipst->ips_ill_g_lock);
21916 }
21917 
21918 /*
21919  * We first need to ensure that the new index is unique, and
21920  * then carry the change across both v4 and v6 ill representation
21921  * of the physical interface.
21922  */
21923 /* ARGSUSED */
21924 int
21925 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21926     ip_ioctl_cmd_t *ipip, void *ifreq)
21927 {
21928 	ill_t		*ill;
21929 	ill_t		*ill_other;
21930 	phyint_t	*phyi;
21931 	int		old_index;
21932 	conn_change_t	connc;
21933 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21934 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21935 	uint_t	index;
21936 	ill_t	*ill_v4;
21937 	ill_t	*ill_v6;
21938 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21939 
21940 	if (ipip->ipi_cmd_type == IF_CMD)
21941 		index = ifr->ifr_index;
21942 	else
21943 		index = lifr->lifr_index;
21944 
21945 	/*
21946 	 * Only allow on physical interface. Also, index zero is illegal.
21947 	 *
21948 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21949 	 *
21950 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21951 	 *    implies a possible failback might have to happen. As failback
21952 	 *    depends on the old index, we should fail setting the index.
21953 	 *
21954 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21955 	 *    any addresses or multicast memberships are failed over to
21956 	 *    a non-STANDBY interface. As failback depends on the old
21957 	 *    index, we should fail setting the index for this case also.
21958 	 *
21959 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21960 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21961 	 */
21962 	ill = ipif->ipif_ill;
21963 	phyi = ill->ill_phyint;
21964 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21965 	    ipif->ipif_id != 0 || index == 0) {
21966 		return (EINVAL);
21967 	}
21968 	old_index = phyi->phyint_ifindex;
21969 
21970 	/* If the index is not changing, no work to do */
21971 	if (old_index == index)
21972 		return (0);
21973 
21974 	/*
21975 	 * Use ill_lookup_on_ifindex to determine if the
21976 	 * new index is unused and if so allow the change.
21977 	 */
21978 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21979 	    ipst);
21980 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21981 	    ipst);
21982 	if (ill_v6 != NULL || ill_v4 != NULL) {
21983 		if (ill_v4 != NULL)
21984 			ill_refrele(ill_v4);
21985 		if (ill_v6 != NULL)
21986 			ill_refrele(ill_v6);
21987 		return (EBUSY);
21988 	}
21989 
21990 	/*
21991 	 * The new index is unused. Set it in the phyint.
21992 	 * Locate the other ill so that we can send a routing
21993 	 * sockets message.
21994 	 */
21995 	if (ill->ill_isv6) {
21996 		ill_other = phyi->phyint_illv4;
21997 	} else {
21998 		ill_other = phyi->phyint_illv6;
21999 	}
22000 
22001 	phyi->phyint_ifindex = index;
22002 
22003 	/* Update SCTP's ILL list */
22004 	sctp_ill_reindex(ill, old_index);
22005 
22006 	connc.cc_old_ifindex = old_index;
22007 	connc.cc_new_ifindex = index;
22008 	ip_change_ifindex(ill, &connc);
22009 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
22010 
22011 	/* Send the routing sockets message */
22012 	ip_rts_ifmsg(ipif);
22013 	if (ill_other != NULL)
22014 		ip_rts_ifmsg(ill_other->ill_ipif);
22015 
22016 	return (0);
22017 }
22018 
22019 /* ARGSUSED */
22020 int
22021 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22022     ip_ioctl_cmd_t *ipip, void *ifreq)
22023 {
22024 	struct ifreq	*ifr = (struct ifreq *)ifreq;
22025 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22026 
22027 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
22028 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22029 	/* Get the interface index */
22030 	if (ipip->ipi_cmd_type == IF_CMD) {
22031 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22032 	} else {
22033 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22034 	}
22035 	return (0);
22036 }
22037 
22038 /* ARGSUSED */
22039 int
22040 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22041     ip_ioctl_cmd_t *ipip, void *ifreq)
22042 {
22043 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22044 
22045 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
22046 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22047 	/* Get the interface zone */
22048 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22049 	lifr->lifr_zoneid = ipif->ipif_zoneid;
22050 	return (0);
22051 }
22052 
22053 /*
22054  * Set the zoneid of an interface.
22055  */
22056 /* ARGSUSED */
22057 int
22058 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22059     ip_ioctl_cmd_t *ipip, void *ifreq)
22060 {
22061 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22062 	int err = 0;
22063 	boolean_t need_up = B_FALSE;
22064 	zone_t *zptr;
22065 	zone_status_t status;
22066 	zoneid_t zoneid;
22067 
22068 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22069 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
22070 		if (!is_system_labeled())
22071 			return (ENOTSUP);
22072 		zoneid = GLOBAL_ZONEID;
22073 	}
22074 
22075 	/* cannot assign instance zero to a non-global zone */
22076 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
22077 		return (ENOTSUP);
22078 
22079 	/*
22080 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
22081 	 * the event of a race with the zone shutdown processing, since IP
22082 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22083 	 * interface will be cleaned up even if the zone is shut down
22084 	 * immediately after the status check. If the interface can't be brought
22085 	 * down right away, and the zone is shut down before the restart
22086 	 * function is called, we resolve the possible races by rechecking the
22087 	 * zone status in the restart function.
22088 	 */
22089 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22090 		return (EINVAL);
22091 	status = zone_status_get(zptr);
22092 	zone_rele(zptr);
22093 
22094 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22095 		return (EINVAL);
22096 
22097 	if (ipif->ipif_flags & IPIF_UP) {
22098 		/*
22099 		 * If the interface is already marked up,
22100 		 * we call ipif_down which will take care
22101 		 * of ditching any IREs that have been set
22102 		 * up based on the old interface address.
22103 		 */
22104 		err = ipif_logical_down(ipif, q, mp);
22105 		if (err == EINPROGRESS)
22106 			return (err);
22107 		ipif_down_tail(ipif);
22108 		need_up = B_TRUE;
22109 	}
22110 
22111 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22112 	return (err);
22113 }
22114 
22115 static int
22116 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22117     queue_t *q, mblk_t *mp, boolean_t need_up)
22118 {
22119 	int	err = 0;
22120 	ip_stack_t	*ipst;
22121 
22122 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22123 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22124 
22125 	if (CONN_Q(q))
22126 		ipst = CONNQ_TO_IPST(q);
22127 	else
22128 		ipst = ILLQ_TO_IPST(q);
22129 
22130 	/*
22131 	 * For exclusive stacks we don't allow a different zoneid than
22132 	 * global.
22133 	 */
22134 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22135 	    zoneid != GLOBAL_ZONEID)
22136 		return (EINVAL);
22137 
22138 	/* Set the new zone id. */
22139 	ipif->ipif_zoneid = zoneid;
22140 
22141 	/* Update sctp list */
22142 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22143 
22144 	if (need_up) {
22145 		/*
22146 		 * Now bring the interface back up.  If this
22147 		 * is the only IPIF for the ILL, ipif_up
22148 		 * will have to re-bind to the device, so
22149 		 * we may get back EINPROGRESS, in which
22150 		 * case, this IOCTL will get completed in
22151 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22152 		 */
22153 		err = ipif_up(ipif, q, mp);
22154 	}
22155 	return (err);
22156 }
22157 
22158 /* ARGSUSED */
22159 int
22160 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22161     ip_ioctl_cmd_t *ipip, void *if_req)
22162 {
22163 	struct lifreq *lifr = (struct lifreq *)if_req;
22164 	zoneid_t zoneid;
22165 	zone_t *zptr;
22166 	zone_status_t status;
22167 
22168 	ASSERT(ipif->ipif_id != 0);
22169 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22170 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22171 		zoneid = GLOBAL_ZONEID;
22172 
22173 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22174 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22175 
22176 	/*
22177 	 * We recheck the zone status to resolve the following race condition:
22178 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22179 	 * 2) hme0:1 is up and can't be brought down right away;
22180 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22181 	 * 3) zone "myzone" is halted; the zone status switches to
22182 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22183 	 * the interfaces to remove - hme0:1 is not returned because it's not
22184 	 * yet in "myzone", so it won't be removed;
22185 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22186 	 * status check here, we would have hme0:1 in "myzone" after it's been
22187 	 * destroyed.
22188 	 * Note that if the status check fails, we need to bring the interface
22189 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22190 	 * ipif_up_done[_v6]().
22191 	 */
22192 	status = ZONE_IS_UNINITIALIZED;
22193 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22194 		status = zone_status_get(zptr);
22195 		zone_rele(zptr);
22196 	}
22197 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22198 		if (ipif->ipif_isv6) {
22199 			(void) ipif_up_done_v6(ipif);
22200 		} else {
22201 			(void) ipif_up_done(ipif);
22202 		}
22203 		return (EINVAL);
22204 	}
22205 
22206 	ipif_down_tail(ipif);
22207 
22208 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22209 	    B_TRUE));
22210 }
22211 
22212 /* ARGSUSED */
22213 int
22214 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22215 	ip_ioctl_cmd_t *ipip, void *ifreq)
22216 {
22217 	struct lifreq	*lifr = ifreq;
22218 
22219 	ASSERT(q->q_next == NULL);
22220 	ASSERT(CONN_Q(q));
22221 
22222 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22223 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22224 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22225 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22226 
22227 	return (0);
22228 }
22229 
22230 
22231 /* Find the previous ILL in this usesrc group */
22232 static ill_t *
22233 ill_prev_usesrc(ill_t *uill)
22234 {
22235 	ill_t *ill;
22236 
22237 	for (ill = uill->ill_usesrc_grp_next;
22238 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22239 	    ill = ill->ill_usesrc_grp_next)
22240 		/* do nothing */;
22241 	return (ill);
22242 }
22243 
22244 /*
22245  * Release all members of the usesrc group. This routine is called
22246  * from ill_delete when the interface being unplumbed is the
22247  * group head.
22248  */
22249 static void
22250 ill_disband_usesrc_group(ill_t *uill)
22251 {
22252 	ill_t *next_ill, *tmp_ill;
22253 	ip_stack_t	*ipst = uill->ill_ipst;
22254 
22255 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22256 	next_ill = uill->ill_usesrc_grp_next;
22257 
22258 	do {
22259 		ASSERT(next_ill != NULL);
22260 		tmp_ill = next_ill->ill_usesrc_grp_next;
22261 		ASSERT(tmp_ill != NULL);
22262 		next_ill->ill_usesrc_grp_next = NULL;
22263 		next_ill->ill_usesrc_ifindex = 0;
22264 		next_ill = tmp_ill;
22265 	} while (next_ill->ill_usesrc_ifindex != 0);
22266 	uill->ill_usesrc_grp_next = NULL;
22267 }
22268 
22269 /*
22270  * Remove the client usesrc ILL from the list and relink to a new list
22271  */
22272 int
22273 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22274 {
22275 	ill_t *ill, *tmp_ill;
22276 	ip_stack_t	*ipst = ucill->ill_ipst;
22277 
22278 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22279 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22280 
22281 	/*
22282 	 * Check if the usesrc client ILL passed in is not already
22283 	 * in use as a usesrc ILL i.e one whose source address is
22284 	 * in use OR a usesrc ILL is not already in use as a usesrc
22285 	 * client ILL
22286 	 */
22287 	if ((ucill->ill_usesrc_ifindex == 0) ||
22288 	    (uill->ill_usesrc_ifindex != 0)) {
22289 		return (-1);
22290 	}
22291 
22292 	ill = ill_prev_usesrc(ucill);
22293 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22294 
22295 	/* Remove from the current list */
22296 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22297 		/* Only two elements in the list */
22298 		ASSERT(ill->ill_usesrc_ifindex == 0);
22299 		ill->ill_usesrc_grp_next = NULL;
22300 	} else {
22301 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22302 	}
22303 
22304 	if (ifindex == 0) {
22305 		ucill->ill_usesrc_ifindex = 0;
22306 		ucill->ill_usesrc_grp_next = NULL;
22307 		return (0);
22308 	}
22309 
22310 	ucill->ill_usesrc_ifindex = ifindex;
22311 	tmp_ill = uill->ill_usesrc_grp_next;
22312 	uill->ill_usesrc_grp_next = ucill;
22313 	ucill->ill_usesrc_grp_next =
22314 	    (tmp_ill != NULL) ? tmp_ill : uill;
22315 	return (0);
22316 }
22317 
22318 /*
22319  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22320  * ip.c for locking details.
22321  */
22322 /* ARGSUSED */
22323 int
22324 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22325     ip_ioctl_cmd_t *ipip, void *ifreq)
22326 {
22327 	struct lifreq *lifr = (struct lifreq *)ifreq;
22328 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22329 	    ill_flag_changed = B_FALSE;
22330 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22331 	int err = 0, ret;
22332 	uint_t ifindex;
22333 	phyint_t *us_phyint, *us_cli_phyint;
22334 	ipsq_t *ipsq = NULL;
22335 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22336 
22337 	ASSERT(IAM_WRITER_IPIF(ipif));
22338 	ASSERT(q->q_next == NULL);
22339 	ASSERT(CONN_Q(q));
22340 
22341 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22342 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22343 
22344 	ASSERT(us_cli_phyint != NULL);
22345 
22346 	/*
22347 	 * If the client ILL is being used for IPMP, abort.
22348 	 * Note, this can be done before ipsq_try_enter since we are already
22349 	 * exclusive on this ILL
22350 	 */
22351 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22352 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22353 		return (EINVAL);
22354 	}
22355 
22356 	ifindex = lifr->lifr_index;
22357 	if (ifindex == 0) {
22358 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22359 			/* non usesrc group interface, nothing to reset */
22360 			return (0);
22361 		}
22362 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22363 		/* valid reset request */
22364 		reset_flg = B_TRUE;
22365 	}
22366 
22367 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22368 	    ip_process_ioctl, &err, ipst);
22369 
22370 	if (usesrc_ill == NULL) {
22371 		return (err);
22372 	}
22373 
22374 	/*
22375 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22376 	 * group nor can either of the interfaces be used for standy. So
22377 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22378 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22379 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22380 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22381 	 * the usesrc_cli_ill
22382 	 */
22383 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22384 	    NEW_OP, B_TRUE);
22385 	if (ipsq == NULL) {
22386 		err = EINPROGRESS;
22387 		/* Operation enqueued on the ipsq of the usesrc ILL */
22388 		goto done;
22389 	}
22390 
22391 	/* Check if the usesrc_ill is used for IPMP */
22392 	us_phyint = usesrc_ill->ill_phyint;
22393 	if ((us_phyint->phyint_groupname != NULL) ||
22394 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22395 		err = EINVAL;
22396 		goto done;
22397 	}
22398 
22399 	/*
22400 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22401 	 * already a client then return EINVAL
22402 	 */
22403 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22404 		err = EINVAL;
22405 		goto done;
22406 	}
22407 
22408 	/*
22409 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22410 	 * be then this is a duplicate operation.
22411 	 */
22412 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22413 		err = 0;
22414 		goto done;
22415 	}
22416 
22417 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22418 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22419 	    usesrc_ill->ill_isv6));
22420 
22421 	/*
22422 	 * The next step ensures that no new ires will be created referencing
22423 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22424 	 * we go through an ire walk deleting all ire caches that reference
22425 	 * the client ill. New ires referencing the client ill that are added
22426 	 * to the ire table before the ILL_CHANGING flag is set, will be
22427 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22428 	 * the client ill while the ILL_CHANGING flag is set will be failed
22429 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22430 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22431 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22432 	 * belong to the same usesrc group.
22433 	 */
22434 	mutex_enter(&usesrc_cli_ill->ill_lock);
22435 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22436 	mutex_exit(&usesrc_cli_ill->ill_lock);
22437 	ill_flag_changed = B_TRUE;
22438 
22439 	if (ipif->ipif_isv6)
22440 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22441 		    ALL_ZONES, ipst);
22442 	else
22443 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22444 		    ALL_ZONES, ipst);
22445 
22446 	/*
22447 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22448 	 * and the ill_usesrc_ifindex fields
22449 	 */
22450 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22451 
22452 	if (reset_flg) {
22453 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22454 		if (ret != 0) {
22455 			err = EINVAL;
22456 		}
22457 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22458 		goto done;
22459 	}
22460 
22461 	/*
22462 	 * Four possibilities to consider:
22463 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22464 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22465 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22466 	 * 4. Both are part of their respective usesrc groups
22467 	 */
22468 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22469 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22470 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22471 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22472 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22473 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22474 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22475 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22476 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22477 		/* Insert at head of list */
22478 		usesrc_cli_ill->ill_usesrc_grp_next =
22479 		    usesrc_ill->ill_usesrc_grp_next;
22480 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22481 	} else {
22482 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22483 		    ifindex);
22484 		if (ret != 0)
22485 			err = EINVAL;
22486 	}
22487 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22488 
22489 done:
22490 	if (ill_flag_changed) {
22491 		mutex_enter(&usesrc_cli_ill->ill_lock);
22492 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22493 		mutex_exit(&usesrc_cli_ill->ill_lock);
22494 	}
22495 	if (ipsq != NULL)
22496 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22497 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22498 	ill_refrele(usesrc_ill);
22499 	return (err);
22500 }
22501 
22502 /*
22503  * comparison function used by avl.
22504  */
22505 static int
22506 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22507 {
22508 
22509 	uint_t index;
22510 
22511 	ASSERT(phyip != NULL && index_ptr != NULL);
22512 
22513 	index = *((uint_t *)index_ptr);
22514 	/*
22515 	 * let the phyint with the lowest index be on top.
22516 	 */
22517 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22518 		return (1);
22519 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22520 		return (-1);
22521 	return (0);
22522 }
22523 
22524 /*
22525  * comparison function used by avl.
22526  */
22527 static int
22528 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22529 {
22530 	ill_t *ill;
22531 	int res = 0;
22532 
22533 	ASSERT(phyip != NULL && name_ptr != NULL);
22534 
22535 	if (((phyint_t *)phyip)->phyint_illv4)
22536 		ill = ((phyint_t *)phyip)->phyint_illv4;
22537 	else
22538 		ill = ((phyint_t *)phyip)->phyint_illv6;
22539 	ASSERT(ill != NULL);
22540 
22541 	res = strcmp(ill->ill_name, (char *)name_ptr);
22542 	if (res > 0)
22543 		return (1);
22544 	else if (res < 0)
22545 		return (-1);
22546 	return (0);
22547 }
22548 /*
22549  * This function is called from ill_delete when the ill is being
22550  * unplumbed. We remove the reference from the phyint and we also
22551  * free the phyint when there are no more references to it.
22552  */
22553 static void
22554 ill_phyint_free(ill_t *ill)
22555 {
22556 	phyint_t *phyi;
22557 	phyint_t *next_phyint;
22558 	ipsq_t *cur_ipsq;
22559 	ip_stack_t	*ipst = ill->ill_ipst;
22560 
22561 	ASSERT(ill->ill_phyint != NULL);
22562 
22563 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22564 	phyi = ill->ill_phyint;
22565 	ill->ill_phyint = NULL;
22566 	/*
22567 	 * ill_init allocates a phyint always to store the copy
22568 	 * of flags relevant to phyint. At that point in time, we could
22569 	 * not assign the name and hence phyint_illv4/v6 could not be
22570 	 * initialized. Later in ipif_set_values, we assign the name to
22571 	 * the ill, at which point in time we assign phyint_illv4/v6.
22572 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22573 	 */
22574 	if (ill->ill_flags & ILLF_IPV6) {
22575 		phyi->phyint_illv6 = NULL;
22576 	} else {
22577 		phyi->phyint_illv4 = NULL;
22578 	}
22579 	/*
22580 	 * ipif_down removes it from the group when the last ipif goes
22581 	 * down.
22582 	 */
22583 	ASSERT(ill->ill_group == NULL);
22584 
22585 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22586 		return;
22587 
22588 	/*
22589 	 * Make sure this phyint was put in the list.
22590 	 */
22591 	if (phyi->phyint_ifindex > 0) {
22592 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22593 		    phyi);
22594 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22595 		    phyi);
22596 	}
22597 	/*
22598 	 * remove phyint from the ipsq list.
22599 	 */
22600 	cur_ipsq = phyi->phyint_ipsq;
22601 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22602 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22603 	} else {
22604 		next_phyint = cur_ipsq->ipsq_phyint_list;
22605 		while (next_phyint != NULL) {
22606 			if (next_phyint->phyint_ipsq_next == phyi) {
22607 				next_phyint->phyint_ipsq_next =
22608 				    phyi->phyint_ipsq_next;
22609 				break;
22610 			}
22611 			next_phyint = next_phyint->phyint_ipsq_next;
22612 		}
22613 		ASSERT(next_phyint != NULL);
22614 	}
22615 	IPSQ_DEC_REF(cur_ipsq, ipst);
22616 
22617 	if (phyi->phyint_groupname_len != 0) {
22618 		ASSERT(phyi->phyint_groupname != NULL);
22619 		mi_free(phyi->phyint_groupname);
22620 	}
22621 	mi_free(phyi);
22622 }
22623 
22624 /*
22625  * Attach the ill to the phyint structure which can be shared by both
22626  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22627  * function is called from ipif_set_values and ill_lookup_on_name (for
22628  * loopback) where we know the name of the ill. We lookup the ill and if
22629  * there is one present already with the name use that phyint. Otherwise
22630  * reuse the one allocated by ill_init.
22631  */
22632 static void
22633 ill_phyint_reinit(ill_t *ill)
22634 {
22635 	boolean_t isv6 = ill->ill_isv6;
22636 	phyint_t *phyi_old;
22637 	phyint_t *phyi;
22638 	avl_index_t where = 0;
22639 	ill_t	*ill_other = NULL;
22640 	ipsq_t	*ipsq;
22641 	ip_stack_t	*ipst = ill->ill_ipst;
22642 
22643 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22644 
22645 	phyi_old = ill->ill_phyint;
22646 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22647 	    phyi_old->phyint_illv6 == NULL));
22648 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22649 	    phyi_old->phyint_illv4 == NULL));
22650 	ASSERT(phyi_old->phyint_ifindex == 0);
22651 
22652 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22653 	    ill->ill_name, &where);
22654 
22655 	/*
22656 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22657 	 *    the global list of ills. So no other thread could have located
22658 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22659 	 * 2. Now locate the other protocol instance of this ill.
22660 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22661 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22662 	 *    of neither ill can change.
22663 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22664 	 *    other ill.
22665 	 * 5. Release all locks.
22666 	 */
22667 
22668 	/*
22669 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22670 	 * we are initializing IPv4.
22671 	 */
22672 	if (phyi != NULL) {
22673 		ill_other = (isv6) ? phyi->phyint_illv4 :
22674 		    phyi->phyint_illv6;
22675 		ASSERT(ill_other->ill_phyint != NULL);
22676 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22677 		    (!isv6 && ill_other->ill_isv6));
22678 		GRAB_ILL_LOCKS(ill, ill_other);
22679 		/*
22680 		 * We are potentially throwing away phyint_flags which
22681 		 * could be different from the one that we obtain from
22682 		 * ill_other->ill_phyint. But it is okay as we are assuming
22683 		 * that the state maintained within IP is correct.
22684 		 */
22685 		mutex_enter(&phyi->phyint_lock);
22686 		if (isv6) {
22687 			ASSERT(phyi->phyint_illv6 == NULL);
22688 			phyi->phyint_illv6 = ill;
22689 		} else {
22690 			ASSERT(phyi->phyint_illv4 == NULL);
22691 			phyi->phyint_illv4 = ill;
22692 		}
22693 		/*
22694 		 * This is a new ill, currently undergoing SLIFNAME
22695 		 * So we could not have joined an IPMP group until now.
22696 		 */
22697 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22698 		    phyi_old->phyint_groupname == NULL);
22699 
22700 		/*
22701 		 * This phyi_old is going away. Decref ipsq_refs and
22702 		 * assert it is zero. The ipsq itself will be freed in
22703 		 * ipsq_exit
22704 		 */
22705 		ipsq = phyi_old->phyint_ipsq;
22706 		IPSQ_DEC_REF(ipsq, ipst);
22707 		ASSERT(ipsq->ipsq_refs == 0);
22708 		/* Get the singleton phyint out of the ipsq list */
22709 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22710 		ipsq->ipsq_phyint_list = NULL;
22711 		phyi_old->phyint_illv4 = NULL;
22712 		phyi_old->phyint_illv6 = NULL;
22713 		mi_free(phyi_old);
22714 	} else {
22715 		mutex_enter(&ill->ill_lock);
22716 		/*
22717 		 * We don't need to acquire any lock, since
22718 		 * the ill is not yet visible globally  and we
22719 		 * have not yet released the ill_g_lock.
22720 		 */
22721 		phyi = phyi_old;
22722 		mutex_enter(&phyi->phyint_lock);
22723 		/* XXX We need a recovery strategy here. */
22724 		if (!phyint_assign_ifindex(phyi, ipst))
22725 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22726 
22727 		/* No IPMP group yet, thus the hook uses the ifindex */
22728 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22729 
22730 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22731 		    (void *)phyi, where);
22732 
22733 		(void) avl_find(&ipst->ips_phyint_g_list->
22734 		    phyint_list_avl_by_index,
22735 		    &phyi->phyint_ifindex, &where);
22736 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22737 		    (void *)phyi, where);
22738 	}
22739 
22740 	/*
22741 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22742 	 * pending mp is not affected because that is per ill basis.
22743 	 */
22744 	ill->ill_phyint = phyi;
22745 
22746 	/*
22747 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22748 	 * We do this here as when the first ipif was allocated,
22749 	 * ipif_allocate does not know the right interface index.
22750 	 */
22751 
22752 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22753 	/*
22754 	 * Now that the phyint's ifindex has been assigned, complete the
22755 	 * remaining
22756 	 */
22757 
22758 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22759 	if (ill->ill_isv6) {
22760 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22761 		    ill->ill_phyint->phyint_ifindex;
22762 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22763 	} else {
22764 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22765 	}
22766 
22767 	/*
22768 	 * Generate an event within the hooks framework to indicate that
22769 	 * a new interface has just been added to IP.  For this event to
22770 	 * be generated, the network interface must, at least, have an
22771 	 * ifindex assigned to it.
22772 	 *
22773 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22774 	 * that the ordering of delivered events to listeners matches the
22775 	 * order of them in the kernel.
22776 	 *
22777 	 * This function could be called from ill_lookup_on_name. In that case
22778 	 * the interface is loopback "lo", which will not generate a NIC event.
22779 	 */
22780 	if (ill->ill_name_length <= 2 ||
22781 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22782 		/*
22783 		 * Generate nic plumb event for ill_name even if
22784 		 * ipmp_hook_emulation is set. That avoids generating events
22785 		 * for the ill_names should ipmp_hook_emulation be turned on
22786 		 * later.
22787 		 */
22788 		ill_nic_info_plumb(ill, B_FALSE);
22789 	}
22790 	RELEASE_ILL_LOCKS(ill, ill_other);
22791 	mutex_exit(&phyi->phyint_lock);
22792 }
22793 
22794 /*
22795  * Allocate a NE_PLUMB nic info event and store in the ill.
22796  * If 'group' is set we do it for the group name, otherwise the ill name.
22797  * It will be sent when we leave the ipsq.
22798  */
22799 void
22800 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22801 {
22802 	phyint_t	*phyi = ill->ill_phyint;
22803 	ip_stack_t	*ipst = ill->ill_ipst;
22804 	hook_nic_event_t *info;
22805 	char		*name;
22806 	int		namelen;
22807 
22808 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22809 
22810 	if ((info = ill->ill_nic_event_info) != NULL) {
22811 		ip2dbg(("ill_nic_info_plumb: unexpected nic event %d "
22812 		    "attached for %s\n", info->hne_event,
22813 		    ill->ill_name));
22814 		if (info->hne_data != NULL)
22815 			kmem_free(info->hne_data, info->hne_datalen);
22816 		kmem_free(info, sizeof (hook_nic_event_t));
22817 		ill->ill_nic_event_info = NULL;
22818 	}
22819 
22820 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
22821 	if (info == NULL) {
22822 		ip2dbg(("ill_nic_info_plumb: could not attach PLUMB nic "
22823 		    "event information for %s (ENOMEM)\n",
22824 		    ill->ill_name));
22825 		return;
22826 	}
22827 
22828 	if (group) {
22829 		ASSERT(phyi->phyint_groupname_len != 0);
22830 		namelen = phyi->phyint_groupname_len;
22831 		name = phyi->phyint_groupname;
22832 	} else {
22833 		namelen = ill->ill_name_length;
22834 		name = ill->ill_name;
22835 	}
22836 
22837 	info->hne_nic = phyi->phyint_hook_ifindex;
22838 	info->hne_lif = 0;
22839 	info->hne_event = NE_PLUMB;
22840 	info->hne_family = ill->ill_isv6 ?
22841 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
22842 
22843 	info->hne_data = kmem_alloc(namelen, KM_NOSLEEP);
22844 	if (info->hne_data != NULL) {
22845 		info->hne_datalen = namelen;
22846 		bcopy(name, info->hne_data, info->hne_datalen);
22847 	} else {
22848 		ip2dbg(("ill_nic_info_plumb: could not attach "
22849 		    "name information for PLUMB nic event "
22850 		    "of %s (ENOMEM)\n", name));
22851 		kmem_free(info, sizeof (hook_nic_event_t));
22852 		info = NULL;
22853 	}
22854 	ill->ill_nic_event_info = info;
22855 }
22856 
22857 /*
22858  * Unhook the nic event message from the ill and enqueue it
22859  * into the nic event taskq.
22860  */
22861 void
22862 ill_nic_info_dispatch(ill_t *ill)
22863 {
22864 	hook_nic_event_t *info;
22865 
22866 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22867 
22868 	if ((info = ill->ill_nic_event_info) != NULL) {
22869 		if (ddi_taskq_dispatch(eventq_queue_nic,
22870 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22871 			ip2dbg(("ill_nic_info_dispatch: "
22872 			    "ddi_taskq_dispatch failed\n"));
22873 			if (info->hne_data != NULL)
22874 				kmem_free(info->hne_data, info->hne_datalen);
22875 			kmem_free(info, sizeof (hook_nic_event_t));
22876 		}
22877 		ill->ill_nic_event_info = NULL;
22878 	}
22879 }
22880 
22881 /*
22882  * Notify any downstream modules of the name of this interface.
22883  * An M_IOCTL is used even though we don't expect a successful reply.
22884  * Any reply message from the driver (presumably an M_IOCNAK) will
22885  * eventually get discarded somewhere upstream.  The message format is
22886  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22887  * to IP.
22888  */
22889 static void
22890 ip_ifname_notify(ill_t *ill, queue_t *q)
22891 {
22892 	mblk_t *mp1, *mp2;
22893 	struct iocblk *iocp;
22894 	struct lifreq *lifr;
22895 
22896 	mp1 = mkiocb(SIOCSLIFNAME);
22897 	if (mp1 == NULL)
22898 		return;
22899 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22900 	if (mp2 == NULL) {
22901 		freeb(mp1);
22902 		return;
22903 	}
22904 
22905 	mp1->b_cont = mp2;
22906 	iocp = (struct iocblk *)mp1->b_rptr;
22907 	iocp->ioc_count = sizeof (struct lifreq);
22908 
22909 	lifr = (struct lifreq *)mp2->b_rptr;
22910 	mp2->b_wptr += sizeof (struct lifreq);
22911 	bzero(lifr, sizeof (struct lifreq));
22912 
22913 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22914 	lifr->lifr_ppa = ill->ill_ppa;
22915 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22916 
22917 	putnext(q, mp1);
22918 }
22919 
22920 static int
22921 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22922 {
22923 	int err;
22924 	ip_stack_t	*ipst = ill->ill_ipst;
22925 
22926 	/* Set the obsolete NDD per-interface forwarding name. */
22927 	err = ill_set_ndd_name(ill);
22928 	if (err != 0) {
22929 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22930 		    err);
22931 	}
22932 
22933 	/* Tell downstream modules where they are. */
22934 	ip_ifname_notify(ill, q);
22935 
22936 	/*
22937 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22938 	 * Error cases are ENOMEM ...
22939 	 */
22940 	err = ill_dl_phys(ill, ipif, mp, q);
22941 
22942 	/*
22943 	 * If there is no IRE expiration timer running, get one started.
22944 	 * igmp and mld timers will be triggered by the first multicast
22945 	 */
22946 	if (ipst->ips_ip_ire_expire_id == 0) {
22947 		/*
22948 		 * acquire the lock and check again.
22949 		 */
22950 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22951 		if (ipst->ips_ip_ire_expire_id == 0) {
22952 			ipst->ips_ip_ire_expire_id = timeout(
22953 			    ip_trash_timer_expire, ipst,
22954 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22955 		}
22956 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22957 	}
22958 
22959 	if (ill->ill_isv6) {
22960 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22961 		if (ipst->ips_mld_slowtimeout_id == 0) {
22962 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22963 			    (void *)ipst,
22964 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22965 		}
22966 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22967 	} else {
22968 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22969 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22970 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22971 			    (void *)ipst,
22972 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22973 		}
22974 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22975 	}
22976 
22977 	return (err);
22978 }
22979 
22980 /*
22981  * Common routine for ppa and ifname setting. Should be called exclusive.
22982  *
22983  * Returns EINPROGRESS when mp has been consumed by queueing it on
22984  * ill_pending_mp and the ioctl will complete in ip_rput.
22985  *
22986  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22987  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22988  * For SLIFNAME, we pass these values back to the userland.
22989  */
22990 static int
22991 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22992 {
22993 	ill_t	*ill;
22994 	ipif_t	*ipif;
22995 	ipsq_t	*ipsq;
22996 	char	*ppa_ptr;
22997 	char	*old_ptr;
22998 	char	old_char;
22999 	int	error;
23000 	ip_stack_t	*ipst;
23001 
23002 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
23003 	ASSERT(q->q_next != NULL);
23004 	ASSERT(interf_name != NULL);
23005 
23006 	ill = (ill_t *)q->q_ptr;
23007 	ipst = ill->ill_ipst;
23008 
23009 	ASSERT(ill->ill_ipst != NULL);
23010 	ASSERT(ill->ill_name[0] == '\0');
23011 	ASSERT(IAM_WRITER_ILL(ill));
23012 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
23013 	ASSERT(ill->ill_ppa == UINT_MAX);
23014 
23015 	/* The ppa is sent down by ifconfig or is chosen */
23016 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
23017 		return (EINVAL);
23018 	}
23019 
23020 	/*
23021 	 * make sure ppa passed in is same as ppa in the name.
23022 	 * This check is not made when ppa == UINT_MAX in that case ppa
23023 	 * in the name could be anything. System will choose a ppa and
23024 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
23025 	 */
23026 	if (*new_ppa_ptr != UINT_MAX) {
23027 		/* stoi changes the pointer */
23028 		old_ptr = ppa_ptr;
23029 		/*
23030 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
23031 		 * (they don't have an externally visible ppa).  We assign one
23032 		 * here so that we can manage the interface.  Note that in
23033 		 * the past this value was always 0 for DLPI 1 drivers.
23034 		 */
23035 		if (*new_ppa_ptr == 0)
23036 			*new_ppa_ptr = stoi(&old_ptr);
23037 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
23038 			return (EINVAL);
23039 	}
23040 	/*
23041 	 * terminate string before ppa
23042 	 * save char at that location.
23043 	 */
23044 	old_char = ppa_ptr[0];
23045 	ppa_ptr[0] = '\0';
23046 
23047 	ill->ill_ppa = *new_ppa_ptr;
23048 	/*
23049 	 * Finish as much work now as possible before calling ill_glist_insert
23050 	 * which makes the ill globally visible and also merges it with the
23051 	 * other protocol instance of this phyint. The remaining work is
23052 	 * done after entering the ipsq which may happen sometime later.
23053 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
23054 	 */
23055 	ipif = ill->ill_ipif;
23056 
23057 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
23058 	ipif_assign_seqid(ipif);
23059 
23060 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
23061 		ill->ill_flags |= ILLF_IPV4;
23062 
23063 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
23064 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
23065 
23066 	if (ill->ill_flags & ILLF_IPV6) {
23067 
23068 		ill->ill_isv6 = B_TRUE;
23069 		if (ill->ill_rq != NULL) {
23070 			ill->ill_rq->q_qinfo = &rinit_ipv6;
23071 			ill->ill_wq->q_qinfo = &winit_ipv6;
23072 		}
23073 
23074 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
23075 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
23076 		ipif->ipif_v6src_addr = ipv6_all_zeros;
23077 		ipif->ipif_v6subnet = ipv6_all_zeros;
23078 		ipif->ipif_v6net_mask = ipv6_all_zeros;
23079 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
23080 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
23081 		/*
23082 		 * point-to-point or Non-mulicast capable
23083 		 * interfaces won't do NUD unless explicitly
23084 		 * configured to do so.
23085 		 */
23086 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
23087 		    !(ill->ill_flags & ILLF_MULTICAST)) {
23088 			ill->ill_flags |= ILLF_NONUD;
23089 		}
23090 		/* Make sure IPv4 specific flag is not set on IPv6 if */
23091 		if (ill->ill_flags & ILLF_NOARP) {
23092 			/*
23093 			 * Note: xresolv interfaces will eventually need
23094 			 * NOARP set here as well, but that will require
23095 			 * those external resolvers to have some
23096 			 * knowledge of that flag and act appropriately.
23097 			 * Not to be changed at present.
23098 			 */
23099 			ill->ill_flags &= ~ILLF_NOARP;
23100 		}
23101 		/*
23102 		 * Set the ILLF_ROUTER flag according to the global
23103 		 * IPv6 forwarding policy.
23104 		 */
23105 		if (ipst->ips_ipv6_forward != 0)
23106 			ill->ill_flags |= ILLF_ROUTER;
23107 	} else if (ill->ill_flags & ILLF_IPV4) {
23108 		ill->ill_isv6 = B_FALSE;
23109 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
23110 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
23111 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
23112 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
23113 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
23114 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
23115 		/*
23116 		 * Set the ILLF_ROUTER flag according to the global
23117 		 * IPv4 forwarding policy.
23118 		 */
23119 		if (ipst->ips_ip_g_forward != 0)
23120 			ill->ill_flags |= ILLF_ROUTER;
23121 	}
23122 
23123 	ASSERT(ill->ill_phyint != NULL);
23124 
23125 	/*
23126 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23127 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23128 	 */
23129 	if (!ill_allocate_mibs(ill))
23130 		return (ENOMEM);
23131 
23132 	/*
23133 	 * Pick a default sap until we get the DL_INFO_ACK back from
23134 	 * the driver.
23135 	 */
23136 	if (ill->ill_sap == 0) {
23137 		if (ill->ill_isv6)
23138 			ill->ill_sap  = IP6_DL_SAP;
23139 		else
23140 			ill->ill_sap  = IP_DL_SAP;
23141 	}
23142 
23143 	ill->ill_ifname_pending = 1;
23144 	ill->ill_ifname_pending_err = 0;
23145 
23146 	ill_refhold(ill);
23147 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23148 	if ((error = ill_glist_insert(ill, interf_name,
23149 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23150 		ill->ill_ppa = UINT_MAX;
23151 		ill->ill_name[0] = '\0';
23152 		/*
23153 		 * undo null termination done above.
23154 		 */
23155 		ppa_ptr[0] = old_char;
23156 		rw_exit(&ipst->ips_ill_g_lock);
23157 		ill_refrele(ill);
23158 		return (error);
23159 	}
23160 
23161 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23162 
23163 	/*
23164 	 * When we return the buffer pointed to by interf_name should contain
23165 	 * the same name as in ill_name.
23166 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23167 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23168 	 * so copy full name and update the ppa ptr.
23169 	 * When ppa passed in != UINT_MAX all values are correct just undo
23170 	 * null termination, this saves a bcopy.
23171 	 */
23172 	if (*new_ppa_ptr == UINT_MAX) {
23173 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23174 		*new_ppa_ptr = ill->ill_ppa;
23175 	} else {
23176 		/*
23177 		 * undo null termination done above.
23178 		 */
23179 		ppa_ptr[0] = old_char;
23180 	}
23181 
23182 	/* Let SCTP know about this ILL */
23183 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23184 
23185 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23186 	    B_TRUE);
23187 
23188 	rw_exit(&ipst->ips_ill_g_lock);
23189 	ill_refrele(ill);
23190 	if (ipsq == NULL)
23191 		return (EINPROGRESS);
23192 
23193 	/*
23194 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23195 	 */
23196 	if (ipsq->ipsq_current_ipif == NULL)
23197 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23198 	else
23199 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23200 
23201 	error = ipif_set_values_tail(ill, ipif, mp, q);
23202 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23203 	if (error != 0 && error != EINPROGRESS) {
23204 		/*
23205 		 * restore previous values
23206 		 */
23207 		ill->ill_isv6 = B_FALSE;
23208 	}
23209 	return (error);
23210 }
23211 
23212 
23213 void
23214 ipif_init(ip_stack_t *ipst)
23215 {
23216 	hrtime_t hrt;
23217 	int i;
23218 
23219 	/*
23220 	 * Can't call drv_getparm here as it is too early in the boot.
23221 	 * As we use ipif_src_random just for picking a different
23222 	 * source address everytime, this need not be really random.
23223 	 */
23224 	hrt = gethrtime();
23225 	ipst->ips_ipif_src_random =
23226 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23227 
23228 	for (i = 0; i < MAX_G_HEADS; i++) {
23229 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23230 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23231 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23232 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23233 	}
23234 
23235 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23236 	    ill_phyint_compare_index,
23237 	    sizeof (phyint_t),
23238 	    offsetof(struct phyint, phyint_avl_by_index));
23239 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23240 	    ill_phyint_compare_name,
23241 	    sizeof (phyint_t),
23242 	    offsetof(struct phyint, phyint_avl_by_name));
23243 }
23244 
23245 /*
23246  * Lookup the ipif corresponding to the onlink destination address. For
23247  * point-to-point interfaces, it matches with remote endpoint destination
23248  * address. For point-to-multipoint interfaces it only tries to match the
23249  * destination with the interface's subnet address. The longest, most specific
23250  * match is found to take care of such rare network configurations like -
23251  * le0: 129.146.1.1/16
23252  * le1: 129.146.2.2/24
23253  * It is used only by SO_DONTROUTE at the moment.
23254  */
23255 ipif_t *
23256 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23257 {
23258 	ipif_t	*ipif, *best_ipif;
23259 	ill_t	*ill;
23260 	ill_walk_context_t ctx;
23261 
23262 	ASSERT(zoneid != ALL_ZONES);
23263 	best_ipif = NULL;
23264 
23265 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23266 	ill = ILL_START_WALK_V4(&ctx, ipst);
23267 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23268 		mutex_enter(&ill->ill_lock);
23269 		for (ipif = ill->ill_ipif; ipif != NULL;
23270 		    ipif = ipif->ipif_next) {
23271 			if (!IPIF_CAN_LOOKUP(ipif))
23272 				continue;
23273 			if (ipif->ipif_zoneid != zoneid &&
23274 			    ipif->ipif_zoneid != ALL_ZONES)
23275 				continue;
23276 			/*
23277 			 * Point-to-point case. Look for exact match with
23278 			 * destination address.
23279 			 */
23280 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23281 				if (ipif->ipif_pp_dst_addr == addr) {
23282 					ipif_refhold_locked(ipif);
23283 					mutex_exit(&ill->ill_lock);
23284 					rw_exit(&ipst->ips_ill_g_lock);
23285 					if (best_ipif != NULL)
23286 						ipif_refrele(best_ipif);
23287 					return (ipif);
23288 				}
23289 			} else if (ipif->ipif_subnet == (addr &
23290 			    ipif->ipif_net_mask)) {
23291 				/*
23292 				 * Point-to-multipoint case. Looping through to
23293 				 * find the most specific match. If there are
23294 				 * multiple best match ipif's then prefer ipif's
23295 				 * that are UP. If there is only one best match
23296 				 * ipif and it is DOWN we must still return it.
23297 				 */
23298 				if ((best_ipif == NULL) ||
23299 				    (ipif->ipif_net_mask >
23300 				    best_ipif->ipif_net_mask) ||
23301 				    ((ipif->ipif_net_mask ==
23302 				    best_ipif->ipif_net_mask) &&
23303 				    ((ipif->ipif_flags & IPIF_UP) &&
23304 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23305 					ipif_refhold_locked(ipif);
23306 					mutex_exit(&ill->ill_lock);
23307 					rw_exit(&ipst->ips_ill_g_lock);
23308 					if (best_ipif != NULL)
23309 						ipif_refrele(best_ipif);
23310 					best_ipif = ipif;
23311 					rw_enter(&ipst->ips_ill_g_lock,
23312 					    RW_READER);
23313 					mutex_enter(&ill->ill_lock);
23314 				}
23315 			}
23316 		}
23317 		mutex_exit(&ill->ill_lock);
23318 	}
23319 	rw_exit(&ipst->ips_ill_g_lock);
23320 	return (best_ipif);
23321 }
23322 
23323 
23324 /*
23325  * Save enough information so that we can recreate the IRE if
23326  * the interface goes down and then up.
23327  */
23328 static void
23329 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23330 {
23331 	mblk_t	*save_mp;
23332 
23333 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23334 	if (save_mp != NULL) {
23335 		ifrt_t	*ifrt;
23336 
23337 		save_mp->b_wptr += sizeof (ifrt_t);
23338 		ifrt = (ifrt_t *)save_mp->b_rptr;
23339 		bzero(ifrt, sizeof (ifrt_t));
23340 		ifrt->ifrt_type = ire->ire_type;
23341 		ifrt->ifrt_addr = ire->ire_addr;
23342 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23343 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23344 		ifrt->ifrt_mask = ire->ire_mask;
23345 		ifrt->ifrt_flags = ire->ire_flags;
23346 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23347 		mutex_enter(&ipif->ipif_saved_ire_lock);
23348 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23349 		ipif->ipif_saved_ire_mp = save_mp;
23350 		ipif->ipif_saved_ire_cnt++;
23351 		mutex_exit(&ipif->ipif_saved_ire_lock);
23352 	}
23353 }
23354 
23355 
23356 static void
23357 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23358 {
23359 	mblk_t	**mpp;
23360 	mblk_t	*mp;
23361 	ifrt_t	*ifrt;
23362 
23363 	/* Remove from ipif_saved_ire_mp list if it is there */
23364 	mutex_enter(&ipif->ipif_saved_ire_lock);
23365 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23366 	    mpp = &(*mpp)->b_cont) {
23367 		/*
23368 		 * On a given ipif, the triple of address, gateway and
23369 		 * mask is unique for each saved IRE (in the case of
23370 		 * ordinary interface routes, the gateway address is
23371 		 * all-zeroes).
23372 		 */
23373 		mp = *mpp;
23374 		ifrt = (ifrt_t *)mp->b_rptr;
23375 		if (ifrt->ifrt_addr == ire->ire_addr &&
23376 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23377 		    ifrt->ifrt_mask == ire->ire_mask) {
23378 			*mpp = mp->b_cont;
23379 			ipif->ipif_saved_ire_cnt--;
23380 			freeb(mp);
23381 			break;
23382 		}
23383 	}
23384 	mutex_exit(&ipif->ipif_saved_ire_lock);
23385 }
23386 
23387 
23388 /*
23389  * IP multirouting broadcast routes handling
23390  * Append CGTP broadcast IREs to regular ones created
23391  * at ifconfig time.
23392  */
23393 static void
23394 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23395 {
23396 	ire_t *ire_prim;
23397 
23398 	ASSERT(ire != NULL);
23399 	ASSERT(ire_dst != NULL);
23400 
23401 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23402 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23403 	if (ire_prim != NULL) {
23404 		/*
23405 		 * We are in the special case of broadcasts for
23406 		 * CGTP. We add an IRE_BROADCAST that holds
23407 		 * the RTF_MULTIRT flag, the destination
23408 		 * address of ire_dst and the low level
23409 		 * info of ire_prim. In other words, CGTP
23410 		 * broadcast is added to the redundant ipif.
23411 		 */
23412 		ipif_t *ipif_prim;
23413 		ire_t  *bcast_ire;
23414 
23415 		ipif_prim = ire_prim->ire_ipif;
23416 
23417 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23418 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23419 		    (void *)ire_dst, (void *)ire_prim,
23420 		    (void *)ipif_prim));
23421 
23422 		bcast_ire = ire_create(
23423 		    (uchar_t *)&ire->ire_addr,
23424 		    (uchar_t *)&ip_g_all_ones,
23425 		    (uchar_t *)&ire_dst->ire_src_addr,
23426 		    (uchar_t *)&ire->ire_gateway_addr,
23427 		    &ipif_prim->ipif_mtu,
23428 		    NULL,
23429 		    ipif_prim->ipif_rq,
23430 		    ipif_prim->ipif_wq,
23431 		    IRE_BROADCAST,
23432 		    ipif_prim,
23433 		    0,
23434 		    0,
23435 		    0,
23436 		    ire->ire_flags,
23437 		    &ire_uinfo_null,
23438 		    NULL,
23439 		    NULL,
23440 		    ipst);
23441 
23442 		if (bcast_ire != NULL) {
23443 
23444 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23445 			    B_FALSE) == 0) {
23446 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23447 				    "added bcast_ire %p\n",
23448 				    (void *)bcast_ire));
23449 
23450 				ipif_save_ire(bcast_ire->ire_ipif,
23451 				    bcast_ire);
23452 				ire_refrele(bcast_ire);
23453 			}
23454 		}
23455 		ire_refrele(ire_prim);
23456 	}
23457 }
23458 
23459 
23460 /*
23461  * IP multirouting broadcast routes handling
23462  * Remove the broadcast ire
23463  */
23464 static void
23465 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23466 {
23467 	ire_t *ire_dst;
23468 
23469 	ASSERT(ire != NULL);
23470 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23471 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23472 	if (ire_dst != NULL) {
23473 		ire_t *ire_prim;
23474 
23475 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23476 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23477 		if (ire_prim != NULL) {
23478 			ipif_t *ipif_prim;
23479 			ire_t  *bcast_ire;
23480 
23481 			ipif_prim = ire_prim->ire_ipif;
23482 
23483 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23484 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23485 			    (void *)ire_dst, (void *)ire_prim,
23486 			    (void *)ipif_prim));
23487 
23488 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23489 			    ire->ire_gateway_addr,
23490 			    IRE_BROADCAST,
23491 			    ipif_prim, ALL_ZONES,
23492 			    NULL,
23493 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23494 			    MATCH_IRE_MASK, ipst);
23495 
23496 			if (bcast_ire != NULL) {
23497 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23498 				    "looked up bcast_ire %p\n",
23499 				    (void *)bcast_ire));
23500 				ipif_remove_ire(bcast_ire->ire_ipif,
23501 				    bcast_ire);
23502 				ire_delete(bcast_ire);
23503 			}
23504 			ire_refrele(ire_prim);
23505 		}
23506 		ire_refrele(ire_dst);
23507 	}
23508 }
23509 
23510 /*
23511  * IPsec hardware acceleration capabilities related functions.
23512  */
23513 
23514 /*
23515  * Free a per-ill IPsec capabilities structure.
23516  */
23517 static void
23518 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23519 {
23520 	if (capab->auth_hw_algs != NULL)
23521 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23522 	if (capab->encr_hw_algs != NULL)
23523 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23524 	if (capab->encr_algparm != NULL)
23525 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23526 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23527 }
23528 
23529 /*
23530  * Allocate a new per-ill IPsec capabilities structure. This structure
23531  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23532  * an array which specifies, for each algorithm, whether this algorithm
23533  * is supported by the ill or not.
23534  */
23535 static ill_ipsec_capab_t *
23536 ill_ipsec_capab_alloc(void)
23537 {
23538 	ill_ipsec_capab_t *capab;
23539 	uint_t nelems;
23540 
23541 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23542 	if (capab == NULL)
23543 		return (NULL);
23544 
23545 	/* we need one bit per algorithm */
23546 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23547 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23548 
23549 	/* allocate memory to store algorithm flags */
23550 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23551 	if (capab->encr_hw_algs == NULL)
23552 		goto nomem;
23553 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23554 	if (capab->auth_hw_algs == NULL)
23555 		goto nomem;
23556 	/*
23557 	 * Leave encr_algparm NULL for now since we won't need it half
23558 	 * the time
23559 	 */
23560 	return (capab);
23561 
23562 nomem:
23563 	ill_ipsec_capab_free(capab);
23564 	return (NULL);
23565 }
23566 
23567 /*
23568  * Resize capability array.  Since we're exclusive, this is OK.
23569  */
23570 static boolean_t
23571 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23572 {
23573 	ipsec_capab_algparm_t *nalp, *oalp;
23574 	uint32_t olen, nlen;
23575 
23576 	oalp = capab->encr_algparm;
23577 	olen = capab->encr_algparm_size;
23578 
23579 	if (oalp != NULL) {
23580 		if (algid < capab->encr_algparm_end)
23581 			return (B_TRUE);
23582 	}
23583 
23584 	nlen = (algid + 1) * sizeof (*nalp);
23585 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23586 	if (nalp == NULL)
23587 		return (B_FALSE);
23588 
23589 	if (oalp != NULL) {
23590 		bcopy(oalp, nalp, olen);
23591 		kmem_free(oalp, olen);
23592 	}
23593 	capab->encr_algparm = nalp;
23594 	capab->encr_algparm_size = nlen;
23595 	capab->encr_algparm_end = algid + 1;
23596 
23597 	return (B_TRUE);
23598 }
23599 
23600 /*
23601  * Compare the capabilities of the specified ill with the protocol
23602  * and algorithms specified by the SA passed as argument.
23603  * If they match, returns B_TRUE, B_FALSE if they do not match.
23604  *
23605  * The ill can be passed as a pointer to it, or by specifying its index
23606  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23607  *
23608  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23609  * packet is eligible for hardware acceleration, and by
23610  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23611  * to a particular ill.
23612  */
23613 boolean_t
23614 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23615     ipsa_t *sa, netstack_t *ns)
23616 {
23617 	boolean_t sa_isv6;
23618 	uint_t algid;
23619 	struct ill_ipsec_capab_s *cpp;
23620 	boolean_t need_refrele = B_FALSE;
23621 	ip_stack_t	*ipst = ns->netstack_ip;
23622 
23623 	if (ill == NULL) {
23624 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23625 		    NULL, NULL, NULL, ipst);
23626 		if (ill == NULL) {
23627 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23628 			return (B_FALSE);
23629 		}
23630 		need_refrele = B_TRUE;
23631 	}
23632 
23633 	/*
23634 	 * Use the address length specified by the SA to determine
23635 	 * if it corresponds to a IPv6 address, and fail the matching
23636 	 * if the isv6 flag passed as argument does not match.
23637 	 * Note: this check is used for SADB capability checking before
23638 	 * sending SA information to an ill.
23639 	 */
23640 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23641 	if (sa_isv6 != ill_isv6)
23642 		/* protocol mismatch */
23643 		goto done;
23644 
23645 	/*
23646 	 * Check if the ill supports the protocol, algorithm(s) and
23647 	 * key size(s) specified by the SA, and get the pointers to
23648 	 * the algorithms supported by the ill.
23649 	 */
23650 	switch (sa->ipsa_type) {
23651 
23652 	case SADB_SATYPE_ESP:
23653 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23654 			/* ill does not support ESP acceleration */
23655 			goto done;
23656 		cpp = ill->ill_ipsec_capab_esp;
23657 		algid = sa->ipsa_auth_alg;
23658 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23659 			goto done;
23660 		algid = sa->ipsa_encr_alg;
23661 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23662 			goto done;
23663 		if (algid < cpp->encr_algparm_end) {
23664 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23665 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23666 				goto done;
23667 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23668 				goto done;
23669 		}
23670 		break;
23671 
23672 	case SADB_SATYPE_AH:
23673 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23674 			/* ill does not support AH acceleration */
23675 			goto done;
23676 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23677 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23678 			goto done;
23679 		break;
23680 	}
23681 
23682 	if (need_refrele)
23683 		ill_refrele(ill);
23684 	return (B_TRUE);
23685 done:
23686 	if (need_refrele)
23687 		ill_refrele(ill);
23688 	return (B_FALSE);
23689 }
23690 
23691 
23692 /*
23693  * Add a new ill to the list of IPsec capable ills.
23694  * Called from ill_capability_ipsec_ack() when an ACK was received
23695  * indicating that IPsec hardware processing was enabled for an ill.
23696  *
23697  * ill must point to the ill for which acceleration was enabled.
23698  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23699  */
23700 static void
23701 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23702 {
23703 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23704 	uint_t sa_type;
23705 	uint_t ipproto;
23706 	ip_stack_t	*ipst = ill->ill_ipst;
23707 
23708 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23709 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23710 
23711 	switch (dl_cap) {
23712 	case DL_CAPAB_IPSEC_AH:
23713 		sa_type = SADB_SATYPE_AH;
23714 		ills = &ipst->ips_ipsec_capab_ills_ah;
23715 		ipproto = IPPROTO_AH;
23716 		break;
23717 	case DL_CAPAB_IPSEC_ESP:
23718 		sa_type = SADB_SATYPE_ESP;
23719 		ills = &ipst->ips_ipsec_capab_ills_esp;
23720 		ipproto = IPPROTO_ESP;
23721 		break;
23722 	}
23723 
23724 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23725 
23726 	/*
23727 	 * Add ill index to list of hardware accelerators. If
23728 	 * already in list, do nothing.
23729 	 */
23730 	for (cur_ill = *ills; cur_ill != NULL &&
23731 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23732 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23733 		;
23734 
23735 	if (cur_ill == NULL) {
23736 		/* if this is a new entry for this ill */
23737 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23738 		if (new_ill == NULL) {
23739 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23740 			return;
23741 		}
23742 
23743 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23744 		new_ill->ill_isv6 = ill->ill_isv6;
23745 		new_ill->next = *ills;
23746 		*ills = new_ill;
23747 	} else if (!sadb_resync) {
23748 		/* not resync'ing SADB and an entry exists for this ill */
23749 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23750 		return;
23751 	}
23752 
23753 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23754 
23755 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23756 		/*
23757 		 * IPsec module for protocol loaded, initiate dump
23758 		 * of the SADB to this ill.
23759 		 */
23760 		sadb_ill_download(ill, sa_type);
23761 }
23762 
23763 /*
23764  * Remove an ill from the list of IPsec capable ills.
23765  */
23766 static void
23767 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23768 {
23769 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23770 	ip_stack_t	*ipst = ill->ill_ipst;
23771 
23772 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23773 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23774 
23775 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23776 	    &ipst->ips_ipsec_capab_ills_esp;
23777 
23778 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23779 
23780 	prev_ill = NULL;
23781 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23782 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23783 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23784 		;
23785 	if (cur_ill == NULL) {
23786 		/* entry not found */
23787 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23788 		return;
23789 	}
23790 	if (prev_ill == NULL) {
23791 		/* entry at front of list */
23792 		*ills = NULL;
23793 	} else {
23794 		prev_ill->next = cur_ill->next;
23795 	}
23796 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23797 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23798 }
23799 
23800 /*
23801  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23802  * supporting the specified IPsec protocol acceleration.
23803  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23804  * We free the mblk and, if sa is non-null, release the held referece.
23805  */
23806 void
23807 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23808     netstack_t *ns)
23809 {
23810 	ipsec_capab_ill_t *ici, *cur_ici;
23811 	ill_t *ill;
23812 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23813 	ip_stack_t	*ipst = ns->netstack_ip;
23814 
23815 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23816 	    ipst->ips_ipsec_capab_ills_esp;
23817 
23818 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23819 
23820 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23821 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23822 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23823 
23824 		/*
23825 		 * Handle the case where the ill goes away while the SADB is
23826 		 * attempting to send messages.  If it's going away, it's
23827 		 * nuking its shadow SADB, so we don't care..
23828 		 */
23829 
23830 		if (ill == NULL)
23831 			continue;
23832 
23833 		if (sa != NULL) {
23834 			/*
23835 			 * Make sure capabilities match before
23836 			 * sending SA to ill.
23837 			 */
23838 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23839 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23840 				ill_refrele(ill);
23841 				continue;
23842 			}
23843 
23844 			mutex_enter(&sa->ipsa_lock);
23845 			sa->ipsa_flags |= IPSA_F_HW;
23846 			mutex_exit(&sa->ipsa_lock);
23847 		}
23848 
23849 		/*
23850 		 * Copy template message, and add it to the front
23851 		 * of the mblk ship list. We want to avoid holding
23852 		 * the ipsec_capab_ills_lock while sending the
23853 		 * message to the ills.
23854 		 *
23855 		 * The b_next and b_prev are temporarily used
23856 		 * to build a list of mblks to be sent down, and to
23857 		 * save the ill to which they must be sent.
23858 		 */
23859 		nmp = copymsg(mp);
23860 		if (nmp == NULL) {
23861 			ill_refrele(ill);
23862 			continue;
23863 		}
23864 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23865 		nmp->b_next = mp_ship_list;
23866 		mp_ship_list = nmp;
23867 		nmp->b_prev = (mblk_t *)ill;
23868 	}
23869 
23870 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23871 
23872 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23873 		/* restore the mblk to a sane state */
23874 		next_mp = nmp->b_next;
23875 		nmp->b_next = NULL;
23876 		ill = (ill_t *)nmp->b_prev;
23877 		nmp->b_prev = NULL;
23878 
23879 		ill_dlpi_send(ill, nmp);
23880 		ill_refrele(ill);
23881 	}
23882 
23883 	if (sa != NULL)
23884 		IPSA_REFRELE(sa);
23885 	freemsg(mp);
23886 }
23887 
23888 /*
23889  * Derive an interface id from the link layer address.
23890  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23891  */
23892 static boolean_t
23893 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23894 {
23895 	char		*addr;
23896 
23897 	if (phys_length != ETHERADDRL)
23898 		return (B_FALSE);
23899 
23900 	/* Form EUI-64 like address */
23901 	addr = (char *)&v6addr->s6_addr32[2];
23902 	bcopy((char *)phys_addr, addr, 3);
23903 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23904 	addr[3] = (char)0xff;
23905 	addr[4] = (char)0xfe;
23906 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23907 	return (B_TRUE);
23908 }
23909 
23910 /* ARGSUSED */
23911 static boolean_t
23912 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23913 {
23914 	return (B_FALSE);
23915 }
23916 
23917 /* ARGSUSED */
23918 static boolean_t
23919 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23920     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23921 {
23922 	/*
23923 	 * Multicast address mappings used over Ethernet/802.X.
23924 	 * This address is used as a base for mappings.
23925 	 */
23926 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23927 	    0x00, 0x00, 0x00};
23928 
23929 	/*
23930 	 * Extract low order 32 bits from IPv6 multicast address.
23931 	 * Or that into the link layer address, starting from the
23932 	 * second byte.
23933 	 */
23934 	*hw_start = 2;
23935 	v6_extract_mask->s6_addr32[0] = 0;
23936 	v6_extract_mask->s6_addr32[1] = 0;
23937 	v6_extract_mask->s6_addr32[2] = 0;
23938 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23939 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23940 	return (B_TRUE);
23941 }
23942 
23943 /*
23944  * Indicate by return value whether multicast is supported. If not,
23945  * this code should not touch/change any parameters.
23946  */
23947 /* ARGSUSED */
23948 static boolean_t
23949 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23950     uint32_t *hw_start, ipaddr_t *extract_mask)
23951 {
23952 	/*
23953 	 * Multicast address mappings used over Ethernet/802.X.
23954 	 * This address is used as a base for mappings.
23955 	 */
23956 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23957 	    0x00, 0x00, 0x00 };
23958 
23959 	if (phys_length != ETHERADDRL)
23960 		return (B_FALSE);
23961 
23962 	*extract_mask = htonl(0x007fffff);
23963 	*hw_start = 2;
23964 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23965 	return (B_TRUE);
23966 }
23967 
23968 /*
23969  * Derive IPoIB interface id from the link layer address.
23970  */
23971 static boolean_t
23972 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23973 {
23974 	char		*addr;
23975 
23976 	if (phys_length != 20)
23977 		return (B_FALSE);
23978 	addr = (char *)&v6addr->s6_addr32[2];
23979 	bcopy(phys_addr + 12, addr, 8);
23980 	/*
23981 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23982 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23983 	 * rules. In these cases, the IBA considers these GUIDs to be in
23984 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23985 	 * required; vendors are required not to assign global EUI-64's
23986 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23987 	 * of the interface identifier. Whether the GUID is in modified
23988 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23989 	 * bit set to 1.
23990 	 */
23991 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23992 	return (B_TRUE);
23993 }
23994 
23995 /*
23996  * Note on mapping from multicast IP addresses to IPoIB multicast link
23997  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23998  * The format of an IPoIB multicast address is:
23999  *
24000  *  4 byte QPN      Scope Sign.  Pkey
24001  * +--------------------------------------------+
24002  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
24003  * +--------------------------------------------+
24004  *
24005  * The Scope and Pkey components are properties of the IBA port and
24006  * network interface. They can be ascertained from the broadcast address.
24007  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
24008  */
24009 
24010 static boolean_t
24011 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
24012     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
24013 {
24014 	/*
24015 	 * Base IPoIB IPv6 multicast address used for mappings.
24016 	 * Does not contain the IBA scope/Pkey values.
24017 	 */
24018 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24019 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
24020 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24021 
24022 	/*
24023 	 * Extract low order 80 bits from IPv6 multicast address.
24024 	 * Or that into the link layer address, starting from the
24025 	 * sixth byte.
24026 	 */
24027 	*hw_start = 6;
24028 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
24029 
24030 	/*
24031 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24032 	 */
24033 	*(maddr + 5) = *(bphys_addr + 5);
24034 	*(maddr + 8) = *(bphys_addr + 8);
24035 	*(maddr + 9) = *(bphys_addr + 9);
24036 
24037 	v6_extract_mask->s6_addr32[0] = 0;
24038 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
24039 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
24040 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24041 	return (B_TRUE);
24042 }
24043 
24044 static boolean_t
24045 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24046     uint32_t *hw_start, ipaddr_t *extract_mask)
24047 {
24048 	/*
24049 	 * Base IPoIB IPv4 multicast address used for mappings.
24050 	 * Does not contain the IBA scope/Pkey values.
24051 	 */
24052 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24053 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
24054 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24055 
24056 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
24057 		return (B_FALSE);
24058 
24059 	/*
24060 	 * Extract low order 28 bits from IPv4 multicast address.
24061 	 * Or that into the link layer address, starting from the
24062 	 * sixteenth byte.
24063 	 */
24064 	*extract_mask = htonl(0x0fffffff);
24065 	*hw_start = 16;
24066 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
24067 
24068 	/*
24069 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24070 	 */
24071 	*(maddr + 5) = *(bphys_addr + 5);
24072 	*(maddr + 8) = *(bphys_addr + 8);
24073 	*(maddr + 9) = *(bphys_addr + 9);
24074 	return (B_TRUE);
24075 }
24076 
24077 /*
24078  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
24079  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
24080  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
24081  * the link-local address is preferred.
24082  */
24083 boolean_t
24084 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24085 {
24086 	ipif_t	*ipif;
24087 	ipif_t	*maybe_ipif = NULL;
24088 
24089 	mutex_enter(&ill->ill_lock);
24090 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24091 		mutex_exit(&ill->ill_lock);
24092 		if (ipifp != NULL)
24093 			*ipifp = NULL;
24094 		return (B_FALSE);
24095 	}
24096 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24097 		if (!IPIF_CAN_LOOKUP(ipif))
24098 			continue;
24099 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
24100 		    ipif->ipif_zoneid != ALL_ZONES)
24101 			continue;
24102 		if ((ipif->ipif_flags & flags) != flags)
24103 			continue;
24104 
24105 		if (ipifp == NULL) {
24106 			mutex_exit(&ill->ill_lock);
24107 			ASSERT(maybe_ipif == NULL);
24108 			return (B_TRUE);
24109 		}
24110 		if (!ill->ill_isv6 ||
24111 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
24112 			ipif_refhold_locked(ipif);
24113 			mutex_exit(&ill->ill_lock);
24114 			*ipifp = ipif;
24115 			return (B_TRUE);
24116 		}
24117 		if (maybe_ipif == NULL)
24118 			maybe_ipif = ipif;
24119 	}
24120 	if (ipifp != NULL) {
24121 		if (maybe_ipif != NULL)
24122 			ipif_refhold_locked(maybe_ipif);
24123 		*ipifp = maybe_ipif;
24124 	}
24125 	mutex_exit(&ill->ill_lock);
24126 	return (maybe_ipif != NULL);
24127 }
24128 
24129 /*
24130  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24131  */
24132 boolean_t
24133 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24134 {
24135 	ill_t *illg;
24136 	ip_stack_t	*ipst = ill->ill_ipst;
24137 
24138 	/*
24139 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24140 	 */
24141 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24142 		return (B_TRUE);
24143 	}
24144 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24145 	if (ill->ill_group == NULL) {
24146 		/* ill not in a group */
24147 		rw_exit(&ipst->ips_ill_g_lock);
24148 		return (B_FALSE);
24149 	}
24150 
24151 	/*
24152 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24153 	 * group. We need to look for an ipif in the zone on all the ills in the
24154 	 * group.
24155 	 */
24156 	illg = ill->ill_group->illgrp_ill;
24157 	do {
24158 		/*
24159 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24160 		 * that it's not there.
24161 		 */
24162 		if (illg != ill &&
24163 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24164 			break;
24165 		}
24166 	} while ((illg = illg->ill_group_next) != NULL);
24167 	rw_exit(&ipst->ips_ill_g_lock);
24168 	return (illg != NULL);
24169 }
24170 
24171 /*
24172  * Check if this ill is only being used to send ICMP probes for IPMP
24173  */
24174 boolean_t
24175 ill_is_probeonly(ill_t *ill)
24176 {
24177 	/*
24178 	 * Check if the interface is FAILED, or INACTIVE
24179 	 */
24180 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24181 		return (B_TRUE);
24182 
24183 	return (B_FALSE);
24184 }
24185 
24186 /*
24187  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24188  * If a pointer to an ipif_t is returned then the caller will need to do
24189  * an ill_refrele().
24190  *
24191  * If there is no real interface which matches the ifindex, then it looks
24192  * for a group that has a matching index. In the case of a group match the
24193  * lifidx must be zero. We don't need emulate the logical interfaces
24194  * since IP Filter's use of netinfo doesn't use that.
24195  */
24196 ipif_t *
24197 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24198     ip_stack_t *ipst)
24199 {
24200 	ipif_t *ipif;
24201 	ill_t *ill;
24202 
24203 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24204 	    ipst);
24205 
24206 	if (ill == NULL) {
24207 		/* Fallback to group names only if hook_emulation set */
24208 		if (!ipst->ips_ipmp_hook_emulation)
24209 			return (NULL);
24210 
24211 		if (lifidx != 0)
24212 			return (NULL);
24213 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24214 		if (ill == NULL)
24215 			return (NULL);
24216 	}
24217 
24218 	mutex_enter(&ill->ill_lock);
24219 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24220 		mutex_exit(&ill->ill_lock);
24221 		ill_refrele(ill);
24222 		return (NULL);
24223 	}
24224 
24225 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24226 		if (!IPIF_CAN_LOOKUP(ipif))
24227 			continue;
24228 		if (lifidx == ipif->ipif_id) {
24229 			ipif_refhold_locked(ipif);
24230 			break;
24231 		}
24232 	}
24233 
24234 	mutex_exit(&ill->ill_lock);
24235 	ill_refrele(ill);
24236 	return (ipif);
24237 }
24238 
24239 /*
24240  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24241  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24242  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24243  * for details.
24244  */
24245 void
24246 ill_fastpath_flush(ill_t *ill)
24247 {
24248 	ip_stack_t *ipst = ill->ill_ipst;
24249 
24250 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24251 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24252 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24253 }
24254 
24255 /*
24256  * Set the physical address information for `ill' to the contents of the
24257  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24258  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24259  * EINPROGRESS will be returned.
24260  */
24261 int
24262 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24263 {
24264 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24265 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24266 
24267 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24268 
24269 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24270 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24271 		/* Changing DL_IPV6_TOKEN is not yet supported */
24272 		return (0);
24273 	}
24274 
24275 	/*
24276 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24277 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24278 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24279 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24280 	 */
24281 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24282 		freemsg(mp);
24283 		return (ENOMEM);
24284 	}
24285 
24286 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24287 
24288 	/*
24289 	 * If we can quiesce the ill, then set the address.  If not, then
24290 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24291 	 */
24292 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24293 	mutex_enter(&ill->ill_lock);
24294 	if (!ill_is_quiescent(ill)) {
24295 		/* call cannot fail since `conn_t *' argument is NULL */
24296 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24297 		    mp, ILL_DOWN);
24298 		mutex_exit(&ill->ill_lock);
24299 		return (EINPROGRESS);
24300 	}
24301 	mutex_exit(&ill->ill_lock);
24302 
24303 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24304 	return (0);
24305 }
24306 
24307 /*
24308  * Once the ill associated with `q' has quiesced, set its physical address
24309  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24310  * are passed (linked by b_cont), since we sometimes need to save two distinct
24311  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24312  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24313  * is quiesced, we know any stale IREs with the old address information have
24314  * already been removed, so we don't need to call ill_fastpath_flush().
24315  */
24316 /* ARGSUSED */
24317 static void
24318 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24319 {
24320 	ill_t		*ill = q->q_ptr;
24321 	mblk_t		*addrmp2 = unlinkb(addrmp);
24322 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24323 	uint_t		addrlen, addroff;
24324 
24325 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24326 
24327 	addroff	= dlindp->dl_addr_offset;
24328 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24329 
24330 	switch (dlindp->dl_data) {
24331 	case DL_IPV6_LINK_LAYER_ADDR:
24332 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24333 		freemsg(addrmp2);
24334 		break;
24335 
24336 	case DL_CURR_PHYS_ADDR:
24337 		freemsg(ill->ill_phys_addr_mp);
24338 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24339 		ill->ill_phys_addr_mp = addrmp;
24340 		ill->ill_phys_addr_length = addrlen;
24341 
24342 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24343 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24344 		else
24345 			freemsg(addrmp2);
24346 		break;
24347 	default:
24348 		ASSERT(0);
24349 	}
24350 
24351 	/*
24352 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24353 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24354 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24355 	 * brought up.
24356 	 */
24357 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24358 		ipsq_current_finish(ipsq);
24359 }
24360 
24361 /*
24362  * Helper routine for setting the ill_nd_lla fields.
24363  */
24364 void
24365 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24366 {
24367 	freemsg(ill->ill_nd_lla_mp);
24368 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24369 	ill->ill_nd_lla_mp = ndmp;
24370 	ill->ill_nd_lla_len = addrlen;
24371 }
24372 
24373 major_t IP_MAJ;
24374 #define	IP	"ip"
24375 
24376 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24377 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24378 
24379 /*
24380  * Issue REMOVEIF ioctls to have the loopback interfaces
24381  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24382  * the former going away when the user-level processes in the zone
24383  * are killed  * and the latter are cleaned up by the stream head
24384  * str_stack_shutdown callback that undoes all I_PLINKs.
24385  */
24386 void
24387 ip_loopback_cleanup(ip_stack_t *ipst)
24388 {
24389 	int error;
24390 	ldi_handle_t	lh = NULL;
24391 	ldi_ident_t	li = NULL;
24392 	int		rval;
24393 	cred_t		*cr;
24394 	struct strioctl iocb;
24395 	struct lifreq	lifreq;
24396 
24397 	IP_MAJ = ddi_name_to_major(IP);
24398 
24399 #ifdef NS_DEBUG
24400 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24401 	    ipst->ips_netstack->netstack_stackid);
24402 #endif
24403 
24404 	bzero(&lifreq, sizeof (lifreq));
24405 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24406 
24407 	error = ldi_ident_from_major(IP_MAJ, &li);
24408 	if (error) {
24409 #ifdef DEBUG
24410 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24411 		    error);
24412 #endif
24413 		return;
24414 	}
24415 
24416 	cr = zone_get_kcred(netstackid_to_zoneid(
24417 	    ipst->ips_netstack->netstack_stackid));
24418 	ASSERT(cr != NULL);
24419 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24420 	if (error) {
24421 #ifdef DEBUG
24422 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24423 		    error);
24424 #endif
24425 		goto out;
24426 	}
24427 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24428 	iocb.ic_timout = 15;
24429 	iocb.ic_len = sizeof (lifreq);
24430 	iocb.ic_dp = (char *)&lifreq;
24431 
24432 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24433 	/* LINTED - statement has no consequent */
24434 	if (error) {
24435 #ifdef NS_DEBUG
24436 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24437 		    "UDP6 error %d\n", error);
24438 #endif
24439 	}
24440 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24441 	lh = NULL;
24442 
24443 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24444 	if (error) {
24445 #ifdef NS_DEBUG
24446 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24447 		    error);
24448 #endif
24449 		goto out;
24450 	}
24451 
24452 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24453 	iocb.ic_timout = 15;
24454 	iocb.ic_len = sizeof (lifreq);
24455 	iocb.ic_dp = (char *)&lifreq;
24456 
24457 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24458 	/* LINTED - statement has no consequent */
24459 	if (error) {
24460 #ifdef NS_DEBUG
24461 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24462 		    "UDP error %d\n", error);
24463 #endif
24464 	}
24465 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24466 	lh = NULL;
24467 
24468 out:
24469 	/* Close layered handles */
24470 	if (lh)
24471 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24472 	if (li)
24473 		ldi_ident_release(li);
24474 
24475 	crfree(cr);
24476 }
24477