xref: /titanic_51/usr/src/uts/common/inet/ip/ip_if.c (revision 65cf7c958eb9457cf2c83a853d128e4ecfc187a6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 /*
28  * This file contains the interface control functions for IP.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/dlpi.h>
34 #include <sys/stropts.h>
35 #include <sys/strsun.h>
36 #include <sys/sysmacros.h>
37 #include <sys/strlog.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/cmn_err.h>
41 #include <sys/kstat.h>
42 #include <sys/debug.h>
43 #include <sys/zone.h>
44 #include <sys/sunldi.h>
45 #include <sys/file.h>
46 #include <sys/bitmap.h>
47 #include <sys/cpuvar.h>
48 #include <sys/time.h>
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 #include <sys/callb.h>
68 
69 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
70 #include <inet/mi.h>
71 #include <inet/nd.h>
72 #include <inet/arp.h>
73 #include <inet/mib2.h>
74 #include <inet/ip.h>
75 #include <inet/ip6.h>
76 #include <inet/ip6_asp.h>
77 #include <inet/tcp.h>
78 #include <inet/ip_multi.h>
79 #include <inet/ip_ire.h>
80 #include <inet/ip_ftable.h>
81 #include <inet/ip_rts.h>
82 #include <inet/ip_ndp.h>
83 #include <inet/ip_if.h>
84 #include <inet/ip_impl.h>
85 #include <inet/tun.h>
86 #include <inet/sctp_ip.h>
87 #include <inet/ip_netinfo.h>
88 #include <inet/mib2.h>
89 
90 #include <net/pfkeyv2.h>
91 #include <inet/ipsec_info.h>
92 #include <inet/sadb.h>
93 #include <inet/ipsec_impl.h>
94 #include <sys/iphada.h>
95 
96 
97 #include <netinet/igmp.h>
98 #include <inet/ip_listutils.h>
99 #include <inet/ipclassifier.h>
100 #include <sys/mac_client.h>
101 #include <sys/dld.h>
102 
103 #include <sys/systeminfo.h>
104 #include <sys/bootconf.h>
105 
106 #include <sys/tsol/tndb.h>
107 #include <sys/tsol/tnet.h>
108 
109 /* The character which tells where the ill_name ends */
110 #define	IPIF_SEPARATOR_CHAR	':'
111 
112 /* IP ioctl function table entry */
113 typedef struct ipft_s {
114 	int	ipft_cmd;
115 	pfi_t	ipft_pfi;
116 	int	ipft_min_size;
117 	int	ipft_flags;
118 } ipft_t;
119 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
120 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
121 
122 typedef struct ip_sock_ar_s {
123 	union {
124 		area_t	ip_sock_area;
125 		ared_t	ip_sock_ared;
126 		areq_t	ip_sock_areq;
127 	} ip_sock_ar_u;
128 	queue_t	*ip_sock_ar_q;
129 } ip_sock_ar_t;
130 
131 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
132 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
133 		    char *value, caddr_t cp, cred_t *ioc_cr);
134 
135 static boolean_t ill_is_quiescent(ill_t *);
136 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
137 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
138 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
139     mblk_t *mp, boolean_t need_up);
140 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
141     mblk_t *mp, boolean_t need_up);
142 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
143     queue_t *q, mblk_t *mp, boolean_t need_up);
144 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
145     mblk_t *mp);
146 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
147     mblk_t *mp);
148 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
149     queue_t *q, mblk_t *mp, boolean_t need_up);
150 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
151     int ioccmd, struct linkblk *li, boolean_t doconsist);
152 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
153 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
154 static void	ipsq_flush(ill_t *ill);
155 
156 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
157     queue_t *q, mblk_t *mp, boolean_t need_up);
158 static void	ipsq_delete(ipsq_t *);
159 
160 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
161 		    boolean_t initialize);
162 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
163 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
164 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
165 		    boolean_t isv6);
166 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
167 static void	ipif_delete_cache_ire(ire_t *, char *);
168 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
169 static void	ipif_free(ipif_t *ipif);
170 static void	ipif_free_tail(ipif_t *ipif);
171 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
172 static void	ipif_multicast_down(ipif_t *ipif);
173 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
174 static void	ipif_set_default(ipif_t *ipif);
175 static int	ipif_set_values(queue_t *q, mblk_t *mp,
176     char *interf_name, uint_t *ppa);
177 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
178     queue_t *q);
179 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
180     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
181     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
182 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
183 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
184 
185 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
186 static int	ill_arp_off(ill_t *ill);
187 static int	ill_arp_on(ill_t *ill);
188 static void	ill_delete_interface_type(ill_if_t *);
189 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
190 static void	ill_dl_down(ill_t *ill);
191 static void	ill_down(ill_t *ill);
192 static void	ill_downi(ire_t *ire, char *ill_arg);
193 static void	ill_free_mib(ill_t *ill);
194 static void	ill_glist_delete(ill_t *);
195 static boolean_t ill_has_usable_ipif(ill_t *);
196 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
197 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
198 static void	ill_phyint_free(ill_t *ill);
199 static void	ill_phyint_reinit(ill_t *ill);
200 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
201 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
202 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
203 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
204 static void	ill_stq_cache_delete(ire_t *, char *);
205 
206 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
207 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
208 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
209     in6_addr_t *);
210 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
211     ipaddr_t *);
212 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
213 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
214     in6_addr_t *);
215 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
216     ipaddr_t *);
217 
218 static void	ipif_save_ire(ipif_t *, ire_t *);
219 static void	ipif_remove_ire(ipif_t *, ire_t *);
220 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
221 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
222 
223 /*
224  * Per-ill IPsec capabilities management.
225  */
226 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
227 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
228 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
229 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
230 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
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_fill(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_fill(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_fill(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_fill(ill_t *, mblk_t *);
243 static int  ill_capability_ipsec_reset_size(ill_t *, int *, int *, int *,
244     int *);
245 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
246 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
247 		    dl_capability_sub_t *);
248 static void	ill_capability_dld_enable(ill_t *);
249 static void	ill_capability_ack_thr(void *);
250 static void	ill_capability_lso_enable(ill_t *);
251 static void	ill_capability_send(ill_t *, mblk_t *);
252 
253 static void	illgrp_cache_delete(ire_t *, char *);
254 static void	illgrp_delete(ill_t *ill);
255 static void	illgrp_reset_schednext(ill_t *ill);
256 
257 static ill_t	*ill_prev_usesrc(ill_t *);
258 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
259 static void	ill_disband_usesrc_group(ill_t *);
260 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
261 
262 #ifdef DEBUG
263 static	void	ill_trace_cleanup(const ill_t *);
264 static	void	ipif_trace_cleanup(const ipif_t *);
265 #endif
266 
267 /*
268  * if we go over the memory footprint limit more than once in this msec
269  * interval, we'll start pruning aggressively.
270  */
271 int ip_min_frag_prune_time = 0;
272 
273 /*
274  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
275  * and the IPsec DOI
276  */
277 #define	MAX_IPSEC_ALGS	256
278 
279 #define	BITSPERBYTE	8
280 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
281 
282 #define	IPSEC_ALG_ENABLE(algs, algid) \
283 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
284 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
285 
286 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
287 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
288 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
289 
290 typedef uint8_t ipsec_capab_elem_t;
291 
292 /*
293  * Per-algorithm parameters.  Note that at present, only encryption
294  * algorithms have variable keysize (IKE does not provide a way to negotiate
295  * auth algorithm keysize).
296  *
297  * All sizes here are in bits.
298  */
299 typedef struct
300 {
301 	uint16_t	minkeylen;
302 	uint16_t	maxkeylen;
303 } ipsec_capab_algparm_t;
304 
305 /*
306  * Per-ill capabilities.
307  */
308 struct ill_ipsec_capab_s {
309 	ipsec_capab_elem_t *encr_hw_algs;
310 	ipsec_capab_elem_t *auth_hw_algs;
311 	uint32_t algs_size;	/* size of _hw_algs in bytes */
312 	/* algorithm key lengths */
313 	ipsec_capab_algparm_t *encr_algparm;
314 	uint32_t encr_algparm_size;
315 	uint32_t encr_algparm_end;
316 };
317 
318 /*
319  * The field values are larger than strictly necessary for simple
320  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
321  */
322 static area_t	ip_area_template = {
323 	AR_ENTRY_ADD,			/* area_cmd */
324 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
325 					/* area_name_offset */
326 	/* area_name_length temporarily holds this structure length */
327 	sizeof (area_t),			/* area_name_length */
328 	IP_ARP_PROTO_TYPE,		/* area_proto */
329 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
330 	IP_ADDR_LEN,			/* area_proto_addr_length */
331 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
332 					/* area_proto_mask_offset */
333 	0,				/* area_flags */
334 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
335 					/* area_hw_addr_offset */
336 	/* Zero length hw_addr_length means 'use your idea of the address' */
337 	0				/* area_hw_addr_length */
338 };
339 
340 /*
341  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
342  * support
343  */
344 static area_t	ip6_area_template = {
345 	AR_ENTRY_ADD,			/* area_cmd */
346 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
347 					/* area_name_offset */
348 	/* area_name_length temporarily holds this structure length */
349 	sizeof (area_t),			/* area_name_length */
350 	IP_ARP_PROTO_TYPE,		/* area_proto */
351 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
352 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
353 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
354 					/* area_proto_mask_offset */
355 	0,				/* area_flags */
356 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
357 					/* area_hw_addr_offset */
358 	/* Zero length hw_addr_length means 'use your idea of the address' */
359 	0				/* area_hw_addr_length */
360 };
361 
362 static ared_t	ip_ared_template = {
363 	AR_ENTRY_DELETE,
364 	sizeof (ared_t) + IP_ADDR_LEN,
365 	sizeof (ared_t),
366 	IP_ARP_PROTO_TYPE,
367 	sizeof (ared_t),
368 	IP_ADDR_LEN,
369 	0
370 };
371 
372 static ared_t	ip6_ared_template = {
373 	AR_ENTRY_DELETE,
374 	sizeof (ared_t) + IPV6_ADDR_LEN,
375 	sizeof (ared_t),
376 	IP_ARP_PROTO_TYPE,
377 	sizeof (ared_t),
378 	IPV6_ADDR_LEN,
379 	0
380 };
381 
382 /*
383  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
384  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
385  * areq is used).
386  */
387 static areq_t	ip_areq_template = {
388 	AR_ENTRY_QUERY,			/* cmd */
389 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
390 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
391 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
392 	sizeof (areq_t),			/* target addr offset */
393 	IP_ADDR_LEN,			/* target addr_length */
394 	0,				/* flags */
395 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
396 	IP_ADDR_LEN,			/* sender addr length */
397 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
398 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
399 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
400 	/* anything else filled in by the code */
401 };
402 
403 static arc_t	ip_aru_template = {
404 	AR_INTERFACE_UP,
405 	sizeof (arc_t),		/* Name offset */
406 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
407 };
408 
409 static arc_t	ip_ard_template = {
410 	AR_INTERFACE_DOWN,
411 	sizeof (arc_t),		/* Name offset */
412 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
413 };
414 
415 static arc_t	ip_aron_template = {
416 	AR_INTERFACE_ON,
417 	sizeof (arc_t),		/* Name offset */
418 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
419 };
420 
421 static arc_t	ip_aroff_template = {
422 	AR_INTERFACE_OFF,
423 	sizeof (arc_t),		/* Name offset */
424 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
425 };
426 
427 static arma_t	ip_arma_multi_template = {
428 	AR_MAPPING_ADD,
429 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
430 				/* Name offset */
431 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
432 	IP_ARP_PROTO_TYPE,
433 	sizeof (arma_t),			/* proto_addr_offset */
434 	IP_ADDR_LEN,				/* proto_addr_length */
435 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
436 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
437 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
438 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
439 	IP_MAX_HW_LEN,				/* hw_addr_length */
440 	0,					/* hw_mapping_start */
441 };
442 
443 static ipft_t	ip_ioctl_ftbl[] = {
444 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
445 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
446 		IPFT_F_NO_REPLY },
447 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
448 		IPFT_F_NO_REPLY },
449 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
450 	{ 0 }
451 };
452 
453 /* Simple ICMP IP Header Template */
454 static ipha_t icmp_ipha = {
455 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
456 };
457 
458 /* Flag descriptors for ip_ipif_report */
459 static nv_t	ipif_nv_tbl[] = {
460 	{ IPIF_UP,		"UP" },
461 	{ IPIF_BROADCAST,	"BROADCAST" },
462 	{ ILLF_DEBUG,		"DEBUG" },
463 	{ PHYI_LOOPBACK,	"LOOPBACK" },
464 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
465 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
466 	{ PHYI_RUNNING,		"RUNNING" },
467 	{ ILLF_NOARP,		"NOARP" },
468 	{ PHYI_PROMISC,		"PROMISC" },
469 	{ PHYI_ALLMULTI,	"ALLMULTI" },
470 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
471 	{ ILLF_MULTICAST,	"MULTICAST" },
472 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
473 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
474 	{ IPIF_DHCPRUNNING,	"DHCP" },
475 	{ IPIF_PRIVATE,		"PRIVATE" },
476 	{ IPIF_NOXMIT,		"NOXMIT" },
477 	{ IPIF_NOLOCAL,		"NOLOCAL" },
478 	{ IPIF_DEPRECATED,	"DEPRECATED" },
479 	{ IPIF_PREFERRED,	"PREFERRED" },
480 	{ IPIF_TEMPORARY,	"TEMPORARY" },
481 	{ IPIF_ADDRCONF,	"ADDRCONF" },
482 	{ PHYI_VIRTUAL,		"VIRTUAL" },
483 	{ ILLF_ROUTER,		"ROUTER" },
484 	{ ILLF_NONUD,		"NONUD" },
485 	{ IPIF_ANYCAST,		"ANYCAST" },
486 	{ ILLF_NORTEXCH,	"NORTEXCH" },
487 	{ ILLF_IPV4,		"IPV4" },
488 	{ ILLF_IPV6,		"IPV6" },
489 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
490 	{ PHYI_FAILED,		"FAILED" },
491 	{ PHYI_STANDBY,		"STANDBY" },
492 	{ PHYI_INACTIVE,	"INACTIVE" },
493 	{ PHYI_OFFLINE,		"OFFLINE" },
494 };
495 
496 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
497 
498 static ip_m_t   ip_m_tbl[] = {
499 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_ether_v6intfid },
501 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
502 	    ip_nodef_v6intfid },
503 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
504 	    ip_nodef_v6intfid },
505 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
506 	    ip_nodef_v6intfid },
507 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
508 	    ip_ether_v6intfid },
509 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
510 	    ip_ib_v6intfid },
511 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
512 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
513 	    ip_nodef_v6intfid }
514 };
515 
516 static ill_t	ill_null;		/* Empty ILL for init. */
517 char	ipif_loopback_name[] = "lo0";
518 static char *ipv4_forward_suffix = ":ip_forwarding";
519 static char *ipv6_forward_suffix = ":ip6_forwarding";
520 static	sin6_t	sin6_null;	/* Zero address for quick clears */
521 static	sin_t	sin_null;	/* Zero address for quick clears */
522 
523 /* When set search for unused ipif_seqid */
524 static ipif_t	ipif_zero;
525 
526 /*
527  * ppa arena is created after these many
528  * interfaces have been plumbed.
529  */
530 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
531 
532 static uint_t
533 ipif_rand(ip_stack_t *ipst)
534 {
535 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
536 	    12345;
537 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
538 }
539 
540 /*
541  * Allocate per-interface mibs.
542  * Returns true if ok. False otherwise.
543  *  ipsq  may not yet be allocated (loopback case ).
544  */
545 static boolean_t
546 ill_allocate_mibs(ill_t *ill)
547 {
548 	/* Already allocated? */
549 	if (ill->ill_ip_mib != NULL) {
550 		if (ill->ill_isv6)
551 			ASSERT(ill->ill_icmp6_mib != NULL);
552 		return (B_TRUE);
553 	}
554 
555 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
556 	    KM_NOSLEEP);
557 	if (ill->ill_ip_mib == NULL) {
558 		return (B_FALSE);
559 	}
560 
561 	/* Setup static information */
562 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
563 	    sizeof (mib2_ipIfStatsEntry_t));
564 	if (ill->ill_isv6) {
565 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
566 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
567 		    sizeof (mib2_ipv6AddrEntry_t));
568 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
569 		    sizeof (mib2_ipv6RouteEntry_t));
570 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
571 		    sizeof (mib2_ipv6NetToMediaEntry_t));
572 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
573 		    sizeof (ipv6_member_t));
574 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
575 		    sizeof (ipv6_grpsrc_t));
576 	} else {
577 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
578 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
579 		    sizeof (mib2_ipAddrEntry_t));
580 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
581 		    sizeof (mib2_ipRouteEntry_t));
582 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
583 		    sizeof (mib2_ipNetToMediaEntry_t));
584 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
585 		    sizeof (ip_member_t));
586 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
587 		    sizeof (ip_grpsrc_t));
588 
589 		/*
590 		 * For a v4 ill, we are done at this point, because per ill
591 		 * icmp mibs are only used for v6.
592 		 */
593 		return (B_TRUE);
594 	}
595 
596 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
597 	    KM_NOSLEEP);
598 	if (ill->ill_icmp6_mib == NULL) {
599 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
600 		ill->ill_ip_mib = NULL;
601 		return (B_FALSE);
602 	}
603 	/* static icmp info */
604 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
605 	    sizeof (mib2_ipv6IfIcmpEntry_t);
606 	/*
607 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
608 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
609 	 * -> ill_phyint_reinit
610 	 */
611 	return (B_TRUE);
612 }
613 
614 /*
615  * Common code for preparation of ARP commands.  Two points to remember:
616  * 	1) The ill_name is tacked on at the end of the allocated space so
617  *	   the templates name_offset field must contain the total space
618  *	   to allocate less the name length.
619  *
620  *	2) The templates name_length field should contain the *template*
621  *	   length.  We use it as a parameter to bcopy() and then write
622  *	   the real ill_name_length into the name_length field of the copy.
623  * (Always called as writer.)
624  */
625 mblk_t *
626 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
627 {
628 	arc_t	*arc = (arc_t *)template;
629 	char	*cp;
630 	int	len;
631 	mblk_t	*mp;
632 	uint_t	name_length = ill->ill_name_length;
633 	uint_t	template_len = arc->arc_name_length;
634 
635 	len = arc->arc_name_offset + name_length;
636 	mp = allocb(len, BPRI_HI);
637 	if (mp == NULL)
638 		return (NULL);
639 	cp = (char *)mp->b_rptr;
640 	mp->b_wptr = (uchar_t *)&cp[len];
641 	if (template_len)
642 		bcopy(template, cp, template_len);
643 	if (len > template_len)
644 		bzero(&cp[template_len], len - template_len);
645 	mp->b_datap->db_type = M_PROTO;
646 
647 	arc = (arc_t *)cp;
648 	arc->arc_name_length = name_length;
649 	cp = (char *)arc + arc->arc_name_offset;
650 	bcopy(ill->ill_name, cp, name_length);
651 
652 	if (addr) {
653 		area_t	*area = (area_t *)mp->b_rptr;
654 
655 		cp = (char *)area + area->area_proto_addr_offset;
656 		bcopy(addr, cp, area->area_proto_addr_length);
657 		if (area->area_cmd == AR_ENTRY_ADD) {
658 			cp = (char *)area;
659 			len = area->area_proto_addr_length;
660 			if (area->area_proto_mask_offset)
661 				cp += area->area_proto_mask_offset;
662 			else
663 				cp += area->area_proto_addr_offset + len;
664 			while (len-- > 0)
665 				*cp++ = (char)~0;
666 		}
667 	}
668 	return (mp);
669 }
670 
671 mblk_t *
672 ipif_area_alloc(ipif_t *ipif)
673 {
674 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
675 	    (char *)&ipif->ipif_lcl_addr));
676 }
677 
678 mblk_t *
679 ipif_ared_alloc(ipif_t *ipif)
680 {
681 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
682 	    (char *)&ipif->ipif_lcl_addr));
683 }
684 
685 mblk_t *
686 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
687 {
688 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
689 	    (char *)&addr));
690 }
691 
692 /*
693  * Completely vaporize a lower level tap and all associated interfaces.
694  * ill_delete is called only out of ip_close when the device control
695  * stream is being closed.
696  */
697 void
698 ill_delete(ill_t *ill)
699 {
700 	ipif_t	*ipif;
701 	ill_t	*prev_ill;
702 	ip_stack_t	*ipst = ill->ill_ipst;
703 
704 	/*
705 	 * ill_delete may be forcibly entering the ipsq. The previous
706 	 * ioctl may not have completed and may need to be aborted.
707 	 * ipsq_flush takes care of it. If we don't need to enter the
708 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
709 	 * ill_delete_tail is sufficient.
710 	 */
711 	ipsq_flush(ill);
712 
713 	/*
714 	 * Nuke all interfaces.  ipif_free will take down the interface,
715 	 * remove it from the list, and free the data structure.
716 	 * Walk down the ipif list and remove the logical interfaces
717 	 * first before removing the main ipif. We can't unplumb
718 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
719 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
720 	 * POINTOPOINT.
721 	 *
722 	 * If ill_ipif was not properly initialized (i.e low on memory),
723 	 * then no interfaces to clean up. In this case just clean up the
724 	 * ill.
725 	 */
726 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
727 		ipif_free(ipif);
728 
729 	/*
730 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
731 	 * So nobody can be using this mp now. Free the mp allocated for
732 	 * honoring ILLF_NOARP
733 	 */
734 	freemsg(ill->ill_arp_on_mp);
735 	ill->ill_arp_on_mp = NULL;
736 
737 	/* Clean up msgs on pending upcalls for mrouted */
738 	reset_mrt_ill(ill);
739 
740 	/*
741 	 * ipif_free -> reset_conn_ipif will remove all multicast
742 	 * references for IPv4. For IPv6, we need to do it here as
743 	 * it points only at ills.
744 	 */
745 	reset_conn_ill(ill);
746 
747 	/*
748 	 * Remove multicast references added as a result of calls to
749 	 * ip_join_allmulti().
750 	 */
751 	ip_purge_allmulti(ill);
752 
753 	/*
754 	 * ill_down will arrange to blow off any IRE's dependent on this
755 	 * ILL, and shut down fragmentation reassembly.
756 	 */
757 	ill_down(ill);
758 
759 	/* Let SCTP know, so that it can remove this from its list. */
760 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
761 
762 	/*
763 	 * If an address on this ILL is being used as a source address then
764 	 * clear out the pointers in other ILLs that point to this ILL.
765 	 */
766 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
767 	if (ill->ill_usesrc_grp_next != NULL) {
768 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
769 			ill_disband_usesrc_group(ill);
770 		} else {	/* consumer of the usesrc ILL */
771 			prev_ill = ill_prev_usesrc(ill);
772 			prev_ill->ill_usesrc_grp_next =
773 			    ill->ill_usesrc_grp_next;
774 		}
775 	}
776 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
777 }
778 
779 static void
780 ipif_non_duplicate(ipif_t *ipif)
781 {
782 	ill_t *ill = ipif->ipif_ill;
783 	mutex_enter(&ill->ill_lock);
784 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
785 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
786 		ASSERT(ill->ill_ipif_dup_count > 0);
787 		ill->ill_ipif_dup_count--;
788 	}
789 	mutex_exit(&ill->ill_lock);
790 }
791 
792 /*
793  * ill_delete_tail is called from ip_modclose after all references
794  * to the closing ill are gone. The wait is done in ip_modclose
795  */
796 void
797 ill_delete_tail(ill_t *ill)
798 {
799 	mblk_t	**mpp;
800 	ipif_t	*ipif;
801 	ip_stack_t	*ipst = ill->ill_ipst;
802 
803 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
804 		ipif_non_duplicate(ipif);
805 		ipif_down_tail(ipif);
806 	}
807 
808 	ASSERT(ill->ill_ipif_dup_count == 0 &&
809 	    ill->ill_arp_down_mp == NULL &&
810 	    ill->ill_arp_del_mapping_mp == NULL);
811 
812 	/*
813 	 * If polling capability is enabled (which signifies direct
814 	 * upcall into IP and driver has ill saved as a handle),
815 	 * we need to make sure that unbind has completed before we
816 	 * let the ill disappear and driver no longer has any reference
817 	 * to this ill.
818 	 */
819 	mutex_enter(&ill->ill_lock);
820 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
821 		cv_wait(&ill->ill_cv, &ill->ill_lock);
822 	mutex_exit(&ill->ill_lock);
823 	ASSERT(!(ill->ill_capabilities &
824 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
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_dld_capab != NULL) {
875 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
876 		ill->ill_dld_capab = NULL;
877 	}
878 
879 	while (ill->ill_ipif != NULL)
880 		ipif_free_tail(ill->ill_ipif);
881 
882 	/*
883 	 * We have removed all references to ilm from conn and the ones joined
884 	 * within the kernel.
885 	 *
886 	 * We don't walk conns, mrts and ires because
887 	 *
888 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
889 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
890 	 *    ill references.
891 	 */
892 	ASSERT(ilm_walk_ill(ill) == 0);
893 	/*
894 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
895 	 * could free the phyint. No more reference to the phyint after this
896 	 * point.
897 	 */
898 	(void) ill_glist_delete(ill);
899 
900 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
901 	if (ill->ill_ndd_name != NULL)
902 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
903 	rw_exit(&ipst->ips_ip_g_nd_lock);
904 
905 	if (ill->ill_frag_ptr != NULL) {
906 		uint_t count;
907 
908 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
909 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
910 		}
911 		mi_free(ill->ill_frag_ptr);
912 		ill->ill_frag_ptr = NULL;
913 		ill->ill_frag_hash_tbl = NULL;
914 	}
915 
916 	freemsg(ill->ill_nd_lla_mp);
917 	/* Free all retained control messages. */
918 	mpp = &ill->ill_first_mp_to_free;
919 	do {
920 		while (mpp[0]) {
921 			mblk_t  *mp;
922 			mblk_t  *mp1;
923 
924 			mp = mpp[0];
925 			mpp[0] = mp->b_next;
926 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
927 				mp1->b_next = NULL;
928 				mp1->b_prev = NULL;
929 			}
930 			freemsg(mp);
931 		}
932 	} while (mpp++ != &ill->ill_last_mp_to_free);
933 
934 	ill_free_mib(ill);
935 
936 #ifdef DEBUG
937 	ill_trace_cleanup(ill);
938 #endif
939 
940 	/* Drop refcnt here */
941 	netstack_rele(ill->ill_ipst->ips_netstack);
942 	ill->ill_ipst = NULL;
943 }
944 
945 static void
946 ill_free_mib(ill_t *ill)
947 {
948 	ip_stack_t *ipst = ill->ill_ipst;
949 
950 	/*
951 	 * MIB statistics must not be lost, so when an interface
952 	 * goes away the counter values will be added to the global
953 	 * MIBs.
954 	 */
955 	if (ill->ill_ip_mib != NULL) {
956 		if (ill->ill_isv6) {
957 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
958 			    ill->ill_ip_mib);
959 		} else {
960 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
961 			    ill->ill_ip_mib);
962 		}
963 
964 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
965 		ill->ill_ip_mib = NULL;
966 	}
967 	if (ill->ill_icmp6_mib != NULL) {
968 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
969 		    ill->ill_icmp6_mib);
970 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
971 		ill->ill_icmp6_mib = NULL;
972 	}
973 }
974 
975 /*
976  * Concatenate together a physical address and a sap.
977  *
978  * Sap_lengths are interpreted as follows:
979  *   sap_length == 0	==>	no sap
980  *   sap_length > 0	==>	sap is at the head of the dlpi address
981  *   sap_length < 0	==>	sap is at the tail of the dlpi address
982  */
983 static void
984 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
985     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
986 {
987 	uint16_t sap_addr = (uint16_t)sap_src;
988 
989 	if (sap_length == 0) {
990 		if (phys_src == NULL)
991 			bzero(dst, phys_length);
992 		else
993 			bcopy(phys_src, dst, phys_length);
994 	} else if (sap_length < 0) {
995 		if (phys_src == NULL)
996 			bzero(dst, phys_length);
997 		else
998 			bcopy(phys_src, dst, phys_length);
999 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1000 	} else {
1001 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1002 		if (phys_src == NULL)
1003 			bzero((char *)dst + sap_length, phys_length);
1004 		else
1005 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1006 	}
1007 }
1008 
1009 /*
1010  * Generate a dl_unitdata_req mblk for the device and address given.
1011  * addr_length is the length of the physical portion of the address.
1012  * If addr is NULL include an all zero address of the specified length.
1013  * TRUE? In any case, addr_length is taken to be the entire length of the
1014  * dlpi address, including the absolute value of sap_length.
1015  */
1016 mblk_t *
1017 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1018 		t_scalar_t sap_length)
1019 {
1020 	dl_unitdata_req_t *dlur;
1021 	mblk_t	*mp;
1022 	t_scalar_t	abs_sap_length;		/* absolute value */
1023 
1024 	abs_sap_length = ABS(sap_length);
1025 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1026 	    DL_UNITDATA_REQ);
1027 	if (mp == NULL)
1028 		return (NULL);
1029 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1030 	/* HACK: accomodate incompatible DLPI drivers */
1031 	if (addr_length == 8)
1032 		addr_length = 6;
1033 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1034 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1035 	dlur->dl_priority.dl_min = 0;
1036 	dlur->dl_priority.dl_max = 0;
1037 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1038 	    (uchar_t *)&dlur[1]);
1039 	return (mp);
1040 }
1041 
1042 /*
1043  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1044  * Return an error if we already have 1 or more ioctls in progress.
1045  * This is used only for non-exclusive ioctls. Currently this is used
1046  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1047  * and thus need to use ipsq_pending_mp_add.
1048  */
1049 boolean_t
1050 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1051 {
1052 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1053 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1054 	/*
1055 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1056 	 */
1057 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1058 	    (add_mp->b_datap->db_type == M_IOCTL));
1059 
1060 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1061 	/*
1062 	 * Return error if the conn has started closing. The conn
1063 	 * could have finished cleaning up the pending mp list,
1064 	 * If so we should not add another mp to the list negating
1065 	 * the cleanup.
1066 	 */
1067 	if (connp->conn_state_flags & CONN_CLOSING)
1068 		return (B_FALSE);
1069 	/*
1070 	 * Add the pending mp to the head of the list, chained by b_next.
1071 	 * Note down the conn on which the ioctl request came, in b_prev.
1072 	 * This will be used to later get the conn, when we get a response
1073 	 * on the ill queue, from some other module (typically arp)
1074 	 */
1075 	add_mp->b_next = (void *)ill->ill_pending_mp;
1076 	add_mp->b_queue = CONNP_TO_WQ(connp);
1077 	ill->ill_pending_mp = add_mp;
1078 	if (connp != NULL)
1079 		connp->conn_oper_pending_ill = ill;
1080 	return (B_TRUE);
1081 }
1082 
1083 /*
1084  * Retrieve the ill_pending_mp and return it. We have to walk the list
1085  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1086  */
1087 mblk_t *
1088 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1089 {
1090 	mblk_t	*prev = NULL;
1091 	mblk_t	*curr = NULL;
1092 	uint_t	id;
1093 	conn_t	*connp;
1094 
1095 	/*
1096 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1097 	 * up the pending mp, but it does not know the ioc_id and
1098 	 * passes in a zero for it.
1099 	 */
1100 	mutex_enter(&ill->ill_lock);
1101 	if (ioc_id != 0)
1102 		*connpp = NULL;
1103 
1104 	/* Search the list for the appropriate ioctl based on ioc_id */
1105 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1106 	    prev = curr, curr = curr->b_next) {
1107 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1108 		connp = Q_TO_CONN(curr->b_queue);
1109 		/* Match based on the ioc_id or based on the conn */
1110 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1111 			break;
1112 	}
1113 
1114 	if (curr != NULL) {
1115 		/* Unlink the mblk from the pending mp list */
1116 		if (prev != NULL) {
1117 			prev->b_next = curr->b_next;
1118 		} else {
1119 			ASSERT(ill->ill_pending_mp == curr);
1120 			ill->ill_pending_mp = curr->b_next;
1121 		}
1122 
1123 		/*
1124 		 * conn refcnt must have been bumped up at the start of
1125 		 * the ioctl. So we can safely access the conn.
1126 		 */
1127 		ASSERT(CONN_Q(curr->b_queue));
1128 		*connpp = Q_TO_CONN(curr->b_queue);
1129 		curr->b_next = NULL;
1130 		curr->b_queue = NULL;
1131 	}
1132 
1133 	mutex_exit(&ill->ill_lock);
1134 
1135 	return (curr);
1136 }
1137 
1138 /*
1139  * Add the pending mp to the list. There can be only 1 pending mp
1140  * in the list. Any exclusive ioctl that needs to wait for a response
1141  * from another module or driver needs to use this function to set
1142  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1143  * the other module/driver. This is also used while waiting for the
1144  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1145  */
1146 boolean_t
1147 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1148     int waitfor)
1149 {
1150 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1151 
1152 	ASSERT(IAM_WRITER_IPIF(ipif));
1153 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1154 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1155 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1156 	/*
1157 	 * The caller may be using a different ipif than the one passed into
1158 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1159 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1160 	 * that `ipsq_current_ipif == ipif'.
1161 	 */
1162 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1163 
1164 	/*
1165 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1166 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1167 	 */
1168 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1169 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1170 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1171 
1172 	if (connp != NULL) {
1173 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1174 		/*
1175 		 * Return error if the conn has started closing. The conn
1176 		 * could have finished cleaning up the pending mp list,
1177 		 * If so we should not add another mp to the list negating
1178 		 * the cleanup.
1179 		 */
1180 		if (connp->conn_state_flags & CONN_CLOSING)
1181 			return (B_FALSE);
1182 	}
1183 	mutex_enter(&ipsq->ipsq_lock);
1184 	ipsq->ipsq_pending_ipif = ipif;
1185 	/*
1186 	 * Note down the queue in b_queue. This will be returned by
1187 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1188 	 * the processing
1189 	 */
1190 	add_mp->b_next = NULL;
1191 	add_mp->b_queue = q;
1192 	ipsq->ipsq_pending_mp = add_mp;
1193 	ipsq->ipsq_waitfor = waitfor;
1194 
1195 	if (connp != NULL)
1196 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1197 	mutex_exit(&ipsq->ipsq_lock);
1198 	return (B_TRUE);
1199 }
1200 
1201 /*
1202  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1203  * queued in the list.
1204  */
1205 mblk_t *
1206 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1207 {
1208 	mblk_t	*curr = NULL;
1209 
1210 	mutex_enter(&ipsq->ipsq_lock);
1211 	*connpp = NULL;
1212 	if (ipsq->ipsq_pending_mp == NULL) {
1213 		mutex_exit(&ipsq->ipsq_lock);
1214 		return (NULL);
1215 	}
1216 
1217 	/* There can be only 1 such excl message */
1218 	curr = ipsq->ipsq_pending_mp;
1219 	ASSERT(curr != NULL && curr->b_next == NULL);
1220 	ipsq->ipsq_pending_ipif = NULL;
1221 	ipsq->ipsq_pending_mp = NULL;
1222 	ipsq->ipsq_waitfor = 0;
1223 	mutex_exit(&ipsq->ipsq_lock);
1224 
1225 	if (CONN_Q(curr->b_queue)) {
1226 		/*
1227 		 * This mp did a refhold on the conn, at the start of the ioctl.
1228 		 * So we can safely return a pointer to the conn to the caller.
1229 		 */
1230 		*connpp = Q_TO_CONN(curr->b_queue);
1231 	} else {
1232 		*connpp = NULL;
1233 	}
1234 	curr->b_next = NULL;
1235 	curr->b_prev = NULL;
1236 	return (curr);
1237 }
1238 
1239 /*
1240  * Cleanup the ioctl mp queued in ipsq_pending_mp
1241  * - Called in the ill_delete path
1242  * - Called in the M_ERROR or M_HANGUP path on the ill.
1243  * - Called in the conn close path.
1244  */
1245 boolean_t
1246 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1247 {
1248 	mblk_t	*mp;
1249 	ipsq_t	*ipsq;
1250 	queue_t	*q;
1251 	ipif_t	*ipif;
1252 
1253 	ASSERT(IAM_WRITER_ILL(ill));
1254 	ipsq = ill->ill_phyint->phyint_ipsq;
1255 	mutex_enter(&ipsq->ipsq_lock);
1256 	/*
1257 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1258 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1259 	 * even if it is meant for another ill, since we have to enqueue
1260 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1261 	 * If connp is non-null we are called from the conn close path.
1262 	 */
1263 	mp = ipsq->ipsq_pending_mp;
1264 	if (mp == NULL || (connp != NULL &&
1265 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1266 		mutex_exit(&ipsq->ipsq_lock);
1267 		return (B_FALSE);
1268 	}
1269 	/* Now remove from the ipsq_pending_mp */
1270 	ipsq->ipsq_pending_mp = NULL;
1271 	q = mp->b_queue;
1272 	mp->b_next = NULL;
1273 	mp->b_prev = NULL;
1274 	mp->b_queue = NULL;
1275 
1276 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1277 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1278 	if (ill->ill_move_in_progress) {
1279 		ILL_CLEAR_MOVE(ill);
1280 	} else if (ill->ill_up_ipifs) {
1281 		ill_group_cleanup(ill);
1282 	}
1283 
1284 	ipif = ipsq->ipsq_pending_ipif;
1285 	ipsq->ipsq_pending_ipif = NULL;
1286 	ipsq->ipsq_waitfor = 0;
1287 	ipsq->ipsq_current_ipif = NULL;
1288 	ipsq->ipsq_current_ioctl = 0;
1289 	ipsq->ipsq_current_done = B_TRUE;
1290 	mutex_exit(&ipsq->ipsq_lock);
1291 
1292 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1293 		if (connp == NULL) {
1294 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1295 		} else {
1296 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1297 			mutex_enter(&ipif->ipif_ill->ill_lock);
1298 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1299 			mutex_exit(&ipif->ipif_ill->ill_lock);
1300 		}
1301 	} else {
1302 		/*
1303 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1304 		 * be just inet_freemsg. we have to restart it
1305 		 * otherwise the thread will be stuck.
1306 		 */
1307 		inet_freemsg(mp);
1308 	}
1309 	return (B_TRUE);
1310 }
1311 
1312 /*
1313  * The ill is closing. Cleanup all the pending mps. Called exclusively
1314  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1315  * knows this ill, and hence nobody can add an mp to this list
1316  */
1317 static void
1318 ill_pending_mp_cleanup(ill_t *ill)
1319 {
1320 	mblk_t	*mp;
1321 	queue_t	*q;
1322 
1323 	ASSERT(IAM_WRITER_ILL(ill));
1324 
1325 	mutex_enter(&ill->ill_lock);
1326 	/*
1327 	 * Every mp on the pending mp list originating from an ioctl
1328 	 * added 1 to the conn refcnt, at the start of the ioctl.
1329 	 * So bump it down now.  See comments in ip_wput_nondata()
1330 	 */
1331 	while (ill->ill_pending_mp != NULL) {
1332 		mp = ill->ill_pending_mp;
1333 		ill->ill_pending_mp = mp->b_next;
1334 		mutex_exit(&ill->ill_lock);
1335 
1336 		q = mp->b_queue;
1337 		ASSERT(CONN_Q(q));
1338 		mp->b_next = NULL;
1339 		mp->b_prev = NULL;
1340 		mp->b_queue = NULL;
1341 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1342 		mutex_enter(&ill->ill_lock);
1343 	}
1344 	ill->ill_pending_ipif = NULL;
1345 
1346 	mutex_exit(&ill->ill_lock);
1347 }
1348 
1349 /*
1350  * Called in the conn close path and ill delete path
1351  */
1352 static void
1353 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1354 {
1355 	ipsq_t	*ipsq;
1356 	mblk_t	*prev;
1357 	mblk_t	*curr;
1358 	mblk_t	*next;
1359 	queue_t	*q;
1360 	mblk_t	*tmp_list = NULL;
1361 
1362 	ASSERT(IAM_WRITER_ILL(ill));
1363 	if (connp != NULL)
1364 		q = CONNP_TO_WQ(connp);
1365 	else
1366 		q = ill->ill_wq;
1367 
1368 	ipsq = ill->ill_phyint->phyint_ipsq;
1369 	/*
1370 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1371 	 * In the case of ioctl from a conn, there can be only 1 mp
1372 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1373 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1374 	 * ioctls meant for this ill form conn's are not flushed. They will
1375 	 * be processed during ipsq_exit and will not find the ill and will
1376 	 * return error.
1377 	 */
1378 	mutex_enter(&ipsq->ipsq_lock);
1379 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1380 	    curr = next) {
1381 		next = curr->b_next;
1382 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1383 			/* Unlink the mblk from the pending mp list */
1384 			if (prev != NULL) {
1385 				prev->b_next = curr->b_next;
1386 			} else {
1387 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1388 				ipsq->ipsq_xopq_mphead = curr->b_next;
1389 			}
1390 			if (ipsq->ipsq_xopq_mptail == curr)
1391 				ipsq->ipsq_xopq_mptail = prev;
1392 			/*
1393 			 * Create a temporary list and release the ipsq lock
1394 			 * New elements are added to the head of the tmp_list
1395 			 */
1396 			curr->b_next = tmp_list;
1397 			tmp_list = curr;
1398 		} else {
1399 			prev = curr;
1400 		}
1401 	}
1402 	mutex_exit(&ipsq->ipsq_lock);
1403 
1404 	while (tmp_list != NULL) {
1405 		curr = tmp_list;
1406 		tmp_list = curr->b_next;
1407 		curr->b_next = NULL;
1408 		curr->b_prev = NULL;
1409 		curr->b_queue = NULL;
1410 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1411 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1412 			    CONN_CLOSE : NO_COPYOUT, NULL);
1413 		} else {
1414 			/*
1415 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1416 			 * this can't be just inet_freemsg. we have to
1417 			 * restart it otherwise the thread will be stuck.
1418 			 */
1419 			inet_freemsg(curr);
1420 		}
1421 	}
1422 }
1423 
1424 /*
1425  * This conn has started closing. Cleanup any pending ioctl from this conn.
1426  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1427  */
1428 void
1429 conn_ioctl_cleanup(conn_t *connp)
1430 {
1431 	mblk_t *curr;
1432 	ipsq_t	*ipsq;
1433 	ill_t	*ill;
1434 	boolean_t refheld;
1435 
1436 	/*
1437 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1438 	 * ioctl has not yet started, the mp is pending in the list headed by
1439 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1440 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1441 	 * is currently executing now the mp is not queued anywhere but
1442 	 * conn_oper_pending_ill is null. The conn close will wait
1443 	 * till the conn_ref drops to zero.
1444 	 */
1445 	mutex_enter(&connp->conn_lock);
1446 	ill = connp->conn_oper_pending_ill;
1447 	if (ill == NULL) {
1448 		mutex_exit(&connp->conn_lock);
1449 		return;
1450 	}
1451 
1452 	curr = ill_pending_mp_get(ill, &connp, 0);
1453 	if (curr != NULL) {
1454 		mutex_exit(&connp->conn_lock);
1455 		CONN_DEC_REF(connp);
1456 		inet_freemsg(curr);
1457 		return;
1458 	}
1459 	/*
1460 	 * We may not be able to refhold the ill if the ill/ipif
1461 	 * is changing. But we need to make sure that the ill will
1462 	 * not vanish. So we just bump up the ill_waiter count.
1463 	 */
1464 	refheld = ill_waiter_inc(ill);
1465 	mutex_exit(&connp->conn_lock);
1466 	if (refheld) {
1467 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1468 			ill_waiter_dcr(ill);
1469 			/*
1470 			 * Check whether this ioctl has started and is
1471 			 * pending now in ipsq_pending_mp. If it is not
1472 			 * found there then check whether this ioctl has
1473 			 * not even started and is in the ipsq_xopq list.
1474 			 */
1475 			if (!ipsq_pending_mp_cleanup(ill, connp))
1476 				ipsq_xopq_mp_cleanup(ill, connp);
1477 			ipsq = ill->ill_phyint->phyint_ipsq;
1478 			ipsq_exit(ipsq);
1479 			return;
1480 		}
1481 	}
1482 
1483 	/*
1484 	 * The ill is also closing and we could not bump up the
1485 	 * ill_waiter_count or we could not enter the ipsq. Leave
1486 	 * the cleanup to ill_delete
1487 	 */
1488 	mutex_enter(&connp->conn_lock);
1489 	while (connp->conn_oper_pending_ill != NULL)
1490 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1491 	mutex_exit(&connp->conn_lock);
1492 	if (refheld)
1493 		ill_waiter_dcr(ill);
1494 }
1495 
1496 /*
1497  * ipcl_walk function for cleaning up conn_*_ill fields.
1498  */
1499 static void
1500 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1501 {
1502 	ill_t	*ill = (ill_t *)arg;
1503 	ire_t	*ire;
1504 
1505 	mutex_enter(&connp->conn_lock);
1506 	if (connp->conn_multicast_ill == ill) {
1507 		/* Revert to late binding */
1508 		connp->conn_multicast_ill = NULL;
1509 		connp->conn_orig_multicast_ifindex = 0;
1510 	}
1511 	if (connp->conn_incoming_ill == ill)
1512 		connp->conn_incoming_ill = NULL;
1513 	if (connp->conn_outgoing_ill == ill)
1514 		connp->conn_outgoing_ill = NULL;
1515 	if (connp->conn_outgoing_pill == ill)
1516 		connp->conn_outgoing_pill = NULL;
1517 	if (connp->conn_nofailover_ill == ill)
1518 		connp->conn_nofailover_ill = NULL;
1519 	if (connp->conn_dhcpinit_ill == ill) {
1520 		connp->conn_dhcpinit_ill = NULL;
1521 		ASSERT(ill->ill_dhcpinit != 0);
1522 		atomic_dec_32(&ill->ill_dhcpinit);
1523 	}
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 /* ARGSUSED */
1546 void
1547 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1548 {
1549 	ill_t	*ill = q->q_ptr;
1550 	ipif_t	*ipif;
1551 
1552 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1553 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1554 		ipif_non_duplicate(ipif);
1555 		ipif_down_tail(ipif);
1556 	}
1557 	freemsg(mp);
1558 	ipsq_current_finish(ipsq);
1559 }
1560 
1561 /*
1562  * ill_down_start is called when we want to down this ill and bring it up again
1563  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1564  * all interfaces, but don't tear down any plumbing.
1565  */
1566 boolean_t
1567 ill_down_start(queue_t *q, mblk_t *mp)
1568 {
1569 	ill_t	*ill = q->q_ptr;
1570 	ipif_t	*ipif;
1571 
1572 	ASSERT(IAM_WRITER_ILL(ill));
1573 
1574 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1575 		(void) ipif_down(ipif, NULL, NULL);
1576 
1577 	ill_down(ill);
1578 
1579 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1580 
1581 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1582 
1583 	/*
1584 	 * Atomically test and add the pending mp if references are active.
1585 	 */
1586 	mutex_enter(&ill->ill_lock);
1587 	if (!ill_is_quiescent(ill)) {
1588 		/* call cannot fail since `conn_t *' argument is NULL */
1589 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1590 		    mp, ILL_DOWN);
1591 		mutex_exit(&ill->ill_lock);
1592 		return (B_FALSE);
1593 	}
1594 	mutex_exit(&ill->ill_lock);
1595 	return (B_TRUE);
1596 }
1597 
1598 static void
1599 ill_down(ill_t *ill)
1600 {
1601 	ip_stack_t	*ipst = ill->ill_ipst;
1602 
1603 	/* Blow off any IREs dependent on this ILL. */
1604 	ire_walk(ill_downi, (char *)ill, ipst);
1605 
1606 	/* Remove any conn_*_ill depending on this ill */
1607 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1608 
1609 	if (ill->ill_group != NULL) {
1610 		illgrp_delete(ill);
1611 	}
1612 }
1613 
1614 /*
1615  * ire_walk routine used to delete every IRE that depends on queues
1616  * associated with 'ill'.  (Always called as writer.)
1617  */
1618 static void
1619 ill_downi(ire_t *ire, char *ill_arg)
1620 {
1621 	ill_t	*ill = (ill_t *)ill_arg;
1622 
1623 	/*
1624 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1625 	 * interface X and ipif coming from interface Y, if interface
1626 	 * X and Y are part of the same IPMP group. Thus whenever interface
1627 	 * X goes down, remove all references to it by checking both
1628 	 * on ire_ipif and ire_stq.
1629 	 */
1630 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1631 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1632 		ire_delete(ire);
1633 	}
1634 }
1635 
1636 /*
1637  * Remove ire/nce from the fastpath list.
1638  */
1639 void
1640 ill_fastpath_nack(ill_t *ill)
1641 {
1642 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1643 }
1644 
1645 /* Consume an M_IOCACK of the fastpath probe. */
1646 void
1647 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1648 {
1649 	mblk_t	*mp1 = mp;
1650 
1651 	/*
1652 	 * If this was the first attempt turn on the fastpath probing.
1653 	 */
1654 	mutex_enter(&ill->ill_lock);
1655 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1656 		ill->ill_dlpi_fastpath_state = IDS_OK;
1657 	mutex_exit(&ill->ill_lock);
1658 
1659 	/* Free the M_IOCACK mblk, hold on to the data */
1660 	mp = mp->b_cont;
1661 	freeb(mp1);
1662 	if (mp == NULL)
1663 		return;
1664 	if (mp->b_cont != NULL) {
1665 		/*
1666 		 * Update all IRE's or NCE's that are waiting for
1667 		 * fastpath update.
1668 		 */
1669 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1670 		mp1 = mp->b_cont;
1671 		freeb(mp);
1672 		mp = mp1;
1673 	} else {
1674 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1675 	}
1676 
1677 	freeb(mp);
1678 }
1679 
1680 /*
1681  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1682  * The data portion of the request is a dl_unitdata_req_t template for
1683  * what we would send downstream in the absence of a fastpath confirmation.
1684  */
1685 int
1686 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1687 {
1688 	struct iocblk	*ioc;
1689 	mblk_t	*mp;
1690 
1691 	if (dlur_mp == NULL)
1692 		return (EINVAL);
1693 
1694 	mutex_enter(&ill->ill_lock);
1695 	switch (ill->ill_dlpi_fastpath_state) {
1696 	case IDS_FAILED:
1697 		/*
1698 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1699 		 * support it.
1700 		 */
1701 		mutex_exit(&ill->ill_lock);
1702 		return (ENOTSUP);
1703 	case IDS_UNKNOWN:
1704 		/* This is the first probe */
1705 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1706 		break;
1707 	default:
1708 		break;
1709 	}
1710 	mutex_exit(&ill->ill_lock);
1711 
1712 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1713 		return (EAGAIN);
1714 
1715 	mp->b_cont = copyb(dlur_mp);
1716 	if (mp->b_cont == NULL) {
1717 		freeb(mp);
1718 		return (EAGAIN);
1719 	}
1720 
1721 	ioc = (struct iocblk *)mp->b_rptr;
1722 	ioc->ioc_count = msgdsize(mp->b_cont);
1723 
1724 	putnext(ill->ill_wq, mp);
1725 	return (0);
1726 }
1727 
1728 void
1729 ill_capability_probe(ill_t *ill)
1730 {
1731 	mblk_t	*mp;
1732 
1733 	ASSERT(IAM_WRITER_ILL(ill));
1734 
1735 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1736 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1737 		return;
1738 
1739 	/*
1740 	 * We are starting a new cycle of capability negotiation.
1741 	 * Free up the capab reset messages of any previous incarnation.
1742 	 * We will do a fresh allocation when we get the response to our probe
1743 	 */
1744 	if (ill->ill_capab_reset_mp != NULL) {
1745 		freemsg(ill->ill_capab_reset_mp);
1746 		ill->ill_capab_reset_mp = NULL;
1747 	}
1748 
1749 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1750 
1751 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1752 	if (mp == NULL)
1753 		return;
1754 
1755 	ill_capability_send(ill, mp);
1756 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1757 }
1758 
1759 void
1760 ill_capability_reset(ill_t *ill, boolean_t reneg)
1761 {
1762 	ASSERT(IAM_WRITER_ILL(ill));
1763 
1764 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1765 		return;
1766 
1767 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1768 
1769 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1770 	ill->ill_capab_reset_mp = NULL;
1771 	/*
1772 	 * We turn off all capabilities except those pertaining to
1773 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1774 	 * which will be turned off by the corresponding reset functions.
1775 	 */
1776 	ill->ill_capabilities &= ~(ILL_CAPAB_MDT | ILL_CAPAB_HCKSUM  |
1777 	    ILL_CAPAB_ZEROCOPY | ILL_CAPAB_AH | ILL_CAPAB_ESP);
1778 }
1779 
1780 static void
1781 ill_capability_reset_alloc(ill_t *ill)
1782 {
1783 	mblk_t *mp;
1784 	size_t	size = 0;
1785 	int	err;
1786 	dl_capability_req_t	*capb;
1787 
1788 	ASSERT(IAM_WRITER_ILL(ill));
1789 	ASSERT(ill->ill_capab_reset_mp == NULL);
1790 
1791 	if (ILL_MDT_CAPABLE(ill))
1792 		size += sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1793 
1794 	if (ILL_HCKSUM_CAPABLE(ill)) {
1795 		size += sizeof (dl_capability_sub_t) +
1796 		    sizeof (dl_capab_hcksum_t);
1797 	}
1798 
1799 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1800 		size += sizeof (dl_capability_sub_t) +
1801 		    sizeof (dl_capab_zerocopy_t);
1802 	}
1803 
1804 	if (ill->ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)) {
1805 		size += sizeof (dl_capability_sub_t);
1806 		size += ill_capability_ipsec_reset_size(ill, NULL, NULL,
1807 		    NULL, NULL);
1808 	}
1809 
1810 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1811 		size += sizeof (dl_capability_sub_t) +
1812 		    sizeof (dl_capab_dld_t);
1813 	}
1814 
1815 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1816 	    STR_NOSIG, &err);
1817 
1818 	mp->b_datap->db_type = M_PROTO;
1819 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1820 
1821 	capb = (dl_capability_req_t *)mp->b_rptr;
1822 	capb->dl_primitive = DL_CAPABILITY_REQ;
1823 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1824 	capb->dl_sub_length = size;
1825 
1826 	mp->b_wptr += sizeof (dl_capability_req_t);
1827 
1828 	/*
1829 	 * Each handler fills in the corresponding dl_capability_sub_t
1830 	 * inside the mblk,
1831 	 */
1832 	ill_capability_mdt_reset_fill(ill, mp);
1833 	ill_capability_hcksum_reset_fill(ill, mp);
1834 	ill_capability_zerocopy_reset_fill(ill, mp);
1835 	ill_capability_ipsec_reset_fill(ill, mp);
1836 	ill_capability_dld_reset_fill(ill, mp);
1837 
1838 	ill->ill_capab_reset_mp = mp;
1839 }
1840 
1841 static void
1842 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1843 {
1844 	dl_capab_id_t *id_ic;
1845 	uint_t sub_dl_cap = outers->dl_cap;
1846 	dl_capability_sub_t *inners;
1847 	uint8_t *capend;
1848 
1849 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1850 
1851 	/*
1852 	 * Note: range checks here are not absolutely sufficient to
1853 	 * make us robust against malformed messages sent by drivers;
1854 	 * this is in keeping with the rest of IP's dlpi handling.
1855 	 * (Remember, it's coming from something else in the kernel
1856 	 * address space)
1857 	 */
1858 
1859 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1860 	if (capend > mp->b_wptr) {
1861 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1862 		    "malformed sub-capability too long for mblk");
1863 		return;
1864 	}
1865 
1866 	id_ic = (dl_capab_id_t *)(outers + 1);
1867 
1868 	if (outers->dl_length < sizeof (*id_ic) ||
1869 	    (inners = &id_ic->id_subcap,
1870 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1871 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1872 		    "encapsulated capab type %d too long for mblk",
1873 		    inners->dl_cap);
1874 		return;
1875 	}
1876 
1877 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1878 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1879 		    "isn't as expected; pass-thru module(s) detected, "
1880 		    "discarding capability\n", inners->dl_cap));
1881 		return;
1882 	}
1883 
1884 	/* Process the encapsulated sub-capability */
1885 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1886 }
1887 
1888 /*
1889  * Process Multidata Transmit capability negotiation ack received from a
1890  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1891  * DL_CAPABILITY_ACK message.
1892  */
1893 static void
1894 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1895 {
1896 	mblk_t *nmp = NULL;
1897 	dl_capability_req_t *oc;
1898 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1899 	ill_mdt_capab_t **ill_mdt_capab;
1900 	uint_t sub_dl_cap = isub->dl_cap;
1901 	uint8_t *capend;
1902 
1903 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1904 
1905 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1906 
1907 	/*
1908 	 * Note: range checks here are not absolutely sufficient to
1909 	 * make us robust against malformed messages sent by drivers;
1910 	 * this is in keeping with the rest of IP's dlpi handling.
1911 	 * (Remember, it's coming from something else in the kernel
1912 	 * address space)
1913 	 */
1914 
1915 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1916 	if (capend > mp->b_wptr) {
1917 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1918 		    "malformed sub-capability too long for mblk");
1919 		return;
1920 	}
1921 
1922 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1923 
1924 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1925 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1926 		    "unsupported MDT sub-capability (version %d, expected %d)",
1927 		    mdt_ic->mdt_version, MDT_VERSION_2);
1928 		return;
1929 	}
1930 
1931 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1932 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1933 		    "capability isn't as expected; pass-thru module(s) "
1934 		    "detected, discarding capability\n"));
1935 		return;
1936 	}
1937 
1938 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1939 
1940 		if (*ill_mdt_capab == NULL) {
1941 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1942 			    KM_NOSLEEP);
1943 			if (*ill_mdt_capab == NULL) {
1944 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1945 				    "could not enable MDT version %d "
1946 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1947 				    ill->ill_name);
1948 				return;
1949 			}
1950 		}
1951 
1952 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1953 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1954 		    "header spaces, %d max pld bufs, %d span limit)\n",
1955 		    ill->ill_name, MDT_VERSION_2,
1956 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1957 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1958 
1959 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1960 		(*ill_mdt_capab)->ill_mdt_on = 1;
1961 		/*
1962 		 * Round the following values to the nearest 32-bit; ULP
1963 		 * may further adjust them to accomodate for additional
1964 		 * protocol headers.  We pass these values to ULP during
1965 		 * bind time.
1966 		 */
1967 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1968 		    roundup(mdt_ic->mdt_hdr_head, 4);
1969 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1970 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1971 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1972 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1973 
1974 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1975 	} else {
1976 		uint_t size;
1977 		uchar_t *rptr;
1978 
1979 		size = sizeof (dl_capability_req_t) +
1980 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1981 
1982 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1983 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1984 			    "could not enable MDT for %s (ENOMEM)\n",
1985 			    ill->ill_name);
1986 			return;
1987 		}
1988 
1989 		rptr = nmp->b_rptr;
1990 		/* initialize dl_capability_req_t */
1991 		oc = (dl_capability_req_t *)nmp->b_rptr;
1992 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1993 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1994 		    sizeof (dl_capab_mdt_t);
1995 		nmp->b_rptr += sizeof (dl_capability_req_t);
1996 
1997 		/* initialize dl_capability_sub_t */
1998 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1999 		nmp->b_rptr += sizeof (*isub);
2000 
2001 		/* initialize dl_capab_mdt_t */
2002 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2003 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2004 
2005 		nmp->b_rptr = rptr;
2006 
2007 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2008 		    "to enable MDT version %d\n", ill->ill_name,
2009 		    MDT_VERSION_2));
2010 
2011 		/* set ENABLE flag */
2012 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2013 
2014 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2015 		ill_capability_send(ill, nmp);
2016 	}
2017 }
2018 
2019 static void
2020 ill_capability_mdt_reset_fill(ill_t *ill, mblk_t *mp)
2021 {
2022 	dl_capab_mdt_t *mdt_subcap;
2023 	dl_capability_sub_t *dl_subcap;
2024 
2025 	if (!ILL_MDT_CAPABLE(ill))
2026 		return;
2027 
2028 	ASSERT(ill->ill_mdt_capab != NULL);
2029 
2030 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2031 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2032 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2033 
2034 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2035 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2036 	mdt_subcap->mdt_flags = 0;
2037 	mdt_subcap->mdt_hdr_head = 0;
2038 	mdt_subcap->mdt_hdr_tail = 0;
2039 
2040 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2041 }
2042 
2043 static void
2044 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
2045 {
2046 	dl_capability_sub_t *dl_subcap;
2047 
2048 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2049 		return;
2050 
2051 	/*
2052 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
2053 	 * initialized below since it is not used by DLD.
2054 	 */
2055 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2056 	dl_subcap->dl_cap = DL_CAPAB_DLD;
2057 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
2058 
2059 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
2060 }
2061 
2062 /*
2063  * Send a DL_NOTIFY_REQ to the specified ill to enable
2064  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2065  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2066  * acceleration.
2067  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2068  */
2069 static boolean_t
2070 ill_enable_promisc_notify(ill_t *ill)
2071 {
2072 	mblk_t *mp;
2073 	dl_notify_req_t *req;
2074 
2075 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2076 
2077 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2078 	if (mp == NULL)
2079 		return (B_FALSE);
2080 
2081 	req = (dl_notify_req_t *)mp->b_rptr;
2082 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2083 	    DL_NOTE_PROMISC_OFF_PHYS;
2084 
2085 	ill_dlpi_send(ill, mp);
2086 
2087 	return (B_TRUE);
2088 }
2089 
2090 /*
2091  * Allocate an IPsec capability request which will be filled by our
2092  * caller to turn on support for one or more algorithms.
2093  */
2094 static mblk_t *
2095 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2096 {
2097 	mblk_t *nmp;
2098 	dl_capability_req_t	*ocap;
2099 	dl_capab_ipsec_t	*ocip;
2100 	dl_capab_ipsec_t	*icip;
2101 	uint8_t			*ptr;
2102 	icip = (dl_capab_ipsec_t *)(isub + 1);
2103 
2104 	/*
2105 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2106 	 * PROMISC_ON/OFF notification from the provider. We need to
2107 	 * do this before enabling the algorithms to avoid leakage of
2108 	 * cleartext packets.
2109 	 */
2110 
2111 	if (!ill_enable_promisc_notify(ill))
2112 		return (NULL);
2113 
2114 	/*
2115 	 * Allocate new mblk which will contain a new capability
2116 	 * request to enable the capabilities.
2117 	 */
2118 
2119 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2120 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2121 	if (nmp == NULL)
2122 		return (NULL);
2123 
2124 	ptr = nmp->b_rptr;
2125 
2126 	/* initialize dl_capability_req_t */
2127 	ocap = (dl_capability_req_t *)ptr;
2128 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2129 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2130 	ptr += sizeof (dl_capability_req_t);
2131 
2132 	/* initialize dl_capability_sub_t */
2133 	bcopy(isub, ptr, sizeof (*isub));
2134 	ptr += sizeof (*isub);
2135 
2136 	/* initialize dl_capab_ipsec_t */
2137 	ocip = (dl_capab_ipsec_t *)ptr;
2138 	bcopy(icip, ocip, sizeof (*icip));
2139 
2140 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2141 	return (nmp);
2142 }
2143 
2144 /*
2145  * Process an IPsec capability negotiation ack received from a DLS Provider.
2146  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2147  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2148  */
2149 static void
2150 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2151 {
2152 	dl_capab_ipsec_t	*icip;
2153 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2154 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2155 	uint_t cipher, nciphers;
2156 	mblk_t *nmp;
2157 	uint_t alg_len;
2158 	boolean_t need_sadb_dump;
2159 	uint_t sub_dl_cap = isub->dl_cap;
2160 	ill_ipsec_capab_t **ill_capab;
2161 	uint64_t ill_capab_flag;
2162 	uint8_t *capend, *ciphend;
2163 	boolean_t sadb_resync;
2164 
2165 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2166 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2167 
2168 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2169 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2170 		ill_capab_flag = ILL_CAPAB_AH;
2171 	} else {
2172 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2173 		ill_capab_flag = ILL_CAPAB_ESP;
2174 	}
2175 
2176 	/*
2177 	 * If the ill capability structure exists, then this incoming
2178 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2179 	 * If this is so, then we'd need to resynchronize the SADB
2180 	 * after re-enabling the offloaded ciphers.
2181 	 */
2182 	sadb_resync = (*ill_capab != NULL);
2183 
2184 	/*
2185 	 * Note: range checks here are not absolutely sufficient to
2186 	 * make us robust against malformed messages sent by drivers;
2187 	 * this is in keeping with the rest of IP's dlpi handling.
2188 	 * (Remember, it's coming from something else in the kernel
2189 	 * address space)
2190 	 */
2191 
2192 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2193 	if (capend > mp->b_wptr) {
2194 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2195 		    "malformed sub-capability too long for mblk");
2196 		return;
2197 	}
2198 
2199 	/*
2200 	 * There are two types of acks we process here:
2201 	 * 1. acks in reply to a (first form) generic capability req
2202 	 *    (no ENABLE flag set)
2203 	 * 2. acks in reply to a ENABLE capability req.
2204 	 *    (ENABLE flag set)
2205 	 *
2206 	 * We process the subcapability passed as argument as follows:
2207 	 * 1 do initializations
2208 	 *   1.1 initialize nmp = NULL
2209 	 *   1.2 set need_sadb_dump to B_FALSE
2210 	 * 2 for each cipher in subcapability:
2211 	 *   2.1 if ENABLE flag is set:
2212 	 *	2.1.1 update per-ill ipsec capabilities info
2213 	 *	2.1.2 set need_sadb_dump to B_TRUE
2214 	 *   2.2 if ENABLE flag is not set:
2215 	 *	2.2.1 if nmp is NULL:
2216 	 *		2.2.1.1 allocate and initialize nmp
2217 	 *		2.2.1.2 init current pos in nmp
2218 	 *	2.2.2 copy current cipher to current pos in nmp
2219 	 *	2.2.3 set ENABLE flag in nmp
2220 	 *	2.2.4 update current pos
2221 	 * 3 if nmp is not equal to NULL, send enable request
2222 	 *   3.1 send capability request
2223 	 * 4 if need_sadb_dump is B_TRUE
2224 	 *   4.1 enable promiscuous on/off notifications
2225 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2226 	 *	AH or ESP SA's to interface.
2227 	 */
2228 
2229 	nmp = NULL;
2230 	oalg = NULL;
2231 	need_sadb_dump = B_FALSE;
2232 	icip = (dl_capab_ipsec_t *)(isub + 1);
2233 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2234 
2235 	nciphers = icip->cip_nciphers;
2236 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2237 
2238 	if (ciphend > capend) {
2239 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2240 		    "too many ciphers for sub-capability len");
2241 		return;
2242 	}
2243 
2244 	for (cipher = 0; cipher < nciphers; cipher++) {
2245 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2246 
2247 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2248 			/*
2249 			 * TBD: when we provide a way to disable capabilities
2250 			 * from above, need to manage the request-pending state
2251 			 * and fail if we were not expecting this ACK.
2252 			 */
2253 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2254 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2255 
2256 			/*
2257 			 * Update IPsec capabilities for this ill
2258 			 */
2259 
2260 			if (*ill_capab == NULL) {
2261 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2262 				    ("ill_capability_ipsec_ack: "
2263 				    "allocating ipsec_capab for ill\n"));
2264 				*ill_capab = ill_ipsec_capab_alloc();
2265 
2266 				if (*ill_capab == NULL) {
2267 					cmn_err(CE_WARN,
2268 					    "ill_capability_ipsec_ack: "
2269 					    "could not enable IPsec Hardware "
2270 					    "acceleration for %s (ENOMEM)\n",
2271 					    ill->ill_name);
2272 					return;
2273 				}
2274 			}
2275 
2276 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2277 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2278 
2279 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2280 				cmn_err(CE_WARN,
2281 				    "ill_capability_ipsec_ack: "
2282 				    "malformed IPsec algorithm id %d",
2283 				    ialg->alg_prim);
2284 				continue;
2285 			}
2286 
2287 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2288 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2289 				    ialg->alg_prim);
2290 			} else {
2291 				ipsec_capab_algparm_t *alp;
2292 
2293 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2294 				    ialg->alg_prim);
2295 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2296 				    ialg->alg_prim)) {
2297 					cmn_err(CE_WARN,
2298 					    "ill_capability_ipsec_ack: "
2299 					    "no space for IPsec alg id %d",
2300 					    ialg->alg_prim);
2301 					continue;
2302 				}
2303 				alp = &((*ill_capab)->encr_algparm[
2304 				    ialg->alg_prim]);
2305 				alp->minkeylen = ialg->alg_minbits;
2306 				alp->maxkeylen = ialg->alg_maxbits;
2307 			}
2308 			ill->ill_capabilities |= ill_capab_flag;
2309 			/*
2310 			 * indicate that a capability was enabled, which
2311 			 * will be used below to kick off a SADB dump
2312 			 * to the ill.
2313 			 */
2314 			need_sadb_dump = B_TRUE;
2315 		} else {
2316 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2317 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2318 			    ialg->alg_prim));
2319 
2320 			if (nmp == NULL) {
2321 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2322 				if (nmp == NULL) {
2323 					/*
2324 					 * Sending the PROMISC_ON/OFF
2325 					 * notification request failed.
2326 					 * We cannot enable the algorithms
2327 					 * since the Provider will not
2328 					 * notify IP of promiscous mode
2329 					 * changes, which could lead
2330 					 * to leakage of packets.
2331 					 */
2332 					cmn_err(CE_WARN,
2333 					    "ill_capability_ipsec_ack: "
2334 					    "could not enable IPsec Hardware "
2335 					    "acceleration for %s (ENOMEM)\n",
2336 					    ill->ill_name);
2337 					return;
2338 				}
2339 				/* ptr to current output alg specifier */
2340 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2341 			}
2342 
2343 			/*
2344 			 * Copy current alg specifier, set ENABLE
2345 			 * flag, and advance to next output alg.
2346 			 * For now we enable all IPsec capabilities.
2347 			 */
2348 			ASSERT(oalg != NULL);
2349 			bcopy(ialg, oalg, alg_len);
2350 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2351 			nmp->b_wptr += alg_len;
2352 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2353 		}
2354 
2355 		/* move to next input algorithm specifier */
2356 		ialg = (dl_capab_ipsec_alg_t *)
2357 		    ((char *)ialg + alg_len);
2358 	}
2359 
2360 	if (nmp != NULL)
2361 		/*
2362 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2363 		 * IPsec hardware acceleration.
2364 		 */
2365 		ill_capability_send(ill, nmp);
2366 
2367 	if (need_sadb_dump)
2368 		/*
2369 		 * An acknowledgement corresponding to a request to
2370 		 * enable acceleration was received, notify SADB.
2371 		 */
2372 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2373 }
2374 
2375 /*
2376  * Given an mblk with enough space in it, create sub-capability entries for
2377  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2378  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2379  * in preparation for the reset the DL_CAPABILITY_REQ message.
2380  */
2381 static void
2382 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2383     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2384 {
2385 	dl_capab_ipsec_t *oipsec;
2386 	dl_capab_ipsec_alg_t *oalg;
2387 	dl_capability_sub_t *dl_subcap;
2388 	int i, k;
2389 
2390 	ASSERT(nciphers > 0);
2391 	ASSERT(ill_cap != NULL);
2392 	ASSERT(mp != NULL);
2393 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2394 
2395 	/* dl_capability_sub_t for "stype" */
2396 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2397 	dl_subcap->dl_cap = stype;
2398 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2399 	mp->b_wptr += sizeof (dl_capability_sub_t);
2400 
2401 	/* dl_capab_ipsec_t for "stype" */
2402 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2403 	oipsec->cip_version = 1;
2404 	oipsec->cip_nciphers = nciphers;
2405 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2406 
2407 	/* create entries for "stype" AUTH ciphers */
2408 	for (i = 0; i < ill_cap->algs_size; i++) {
2409 		for (k = 0; k < BITSPERBYTE; k++) {
2410 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2411 				continue;
2412 
2413 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2414 			bzero((void *)oalg, sizeof (*oalg));
2415 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2416 			oalg->alg_prim = k + (BITSPERBYTE * i);
2417 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2418 		}
2419 	}
2420 	/* create entries for "stype" ENCR ciphers */
2421 	for (i = 0; i < ill_cap->algs_size; i++) {
2422 		for (k = 0; k < BITSPERBYTE; k++) {
2423 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2424 				continue;
2425 
2426 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2427 			bzero((void *)oalg, sizeof (*oalg));
2428 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2429 			oalg->alg_prim = k + (BITSPERBYTE * i);
2430 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2431 		}
2432 	}
2433 }
2434 
2435 /*
2436  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2437  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2438  * POPC instruction, but our macro is more flexible for an arbitrary length
2439  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2440  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2441  * stays that way, we can reduce the number of iterations required.
2442  */
2443 #define	COUNT_1S(val, sum) {					\
2444 	uint8_t x = val & 0xff;					\
2445 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2446 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2447 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2448 }
2449 
2450 /* ARGSUSED */
2451 static int
2452 ill_capability_ipsec_reset_size(ill_t *ill, int *ah_cntp, int *ah_lenp,
2453     int *esp_cntp, int *esp_lenp)
2454 {
2455 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2456 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2457 	uint64_t ill_capabilities = ill->ill_capabilities;
2458 	int ah_cnt = 0, esp_cnt = 0;
2459 	int ah_len = 0, esp_len = 0;
2460 	int i, size = 0;
2461 
2462 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2463 		return (0);
2464 
2465 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2466 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2467 
2468 	/* Find out the number of ciphers for AH */
2469 	if (cap_ah != NULL) {
2470 		for (i = 0; i < cap_ah->algs_size; i++) {
2471 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2472 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2473 		}
2474 		if (ah_cnt > 0) {
2475 			size += sizeof (dl_capability_sub_t) +
2476 			    sizeof (dl_capab_ipsec_t);
2477 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2478 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2479 			size += ah_len;
2480 		}
2481 	}
2482 
2483 	/* Find out the number of ciphers for ESP */
2484 	if (cap_esp != NULL) {
2485 		for (i = 0; i < cap_esp->algs_size; i++) {
2486 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2487 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2488 		}
2489 		if (esp_cnt > 0) {
2490 			size += sizeof (dl_capability_sub_t) +
2491 			    sizeof (dl_capab_ipsec_t);
2492 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2493 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2494 			size += esp_len;
2495 		}
2496 	}
2497 
2498 	if (ah_cntp != NULL)
2499 		*ah_cntp = ah_cnt;
2500 	if (ah_lenp != NULL)
2501 		*ah_lenp = ah_len;
2502 	if (esp_cntp != NULL)
2503 		*esp_cntp = esp_cnt;
2504 	if (esp_lenp != NULL)
2505 		*esp_lenp = esp_len;
2506 
2507 	return (size);
2508 }
2509 
2510 /* ARGSUSED */
2511 static void
2512 ill_capability_ipsec_reset_fill(ill_t *ill, mblk_t *mp)
2513 {
2514 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2515 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2516 	int ah_cnt = 0, esp_cnt = 0;
2517 	int ah_len = 0, esp_len = 0;
2518 	int size;
2519 
2520 	size = ill_capability_ipsec_reset_size(ill, &ah_cnt, &ah_len,
2521 	    &esp_cnt, &esp_len);
2522 	if (size == 0)
2523 		return;
2524 
2525 	/*
2526 	 * Clear the capability flags for IPsec HA but retain the ill
2527 	 * capability structures since it's possible that another thread
2528 	 * is still referring to them.  The structures only get deallocated
2529 	 * when we destroy the ill.
2530 	 *
2531 	 * Various places check the flags to see if the ill is capable of
2532 	 * hardware acceleration, and by clearing them we ensure that new
2533 	 * outbound IPsec packets are sent down encrypted.
2534 	 */
2535 
2536 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2537 	if (ah_cnt > 0) {
2538 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2539 		    cap_ah, mp);
2540 	}
2541 
2542 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2543 	if (esp_cnt > 0) {
2544 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2545 		    cap_esp, mp);
2546 	}
2547 
2548 	/*
2549 	 * At this point we've composed a bunch of sub-capabilities to be
2550 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2551 	 * by the caller.  Upon receiving this reset message, the driver
2552 	 * must stop inbound decryption (by destroying all inbound SAs)
2553 	 * and let the corresponding packets come in encrypted.
2554 	 */
2555 }
2556 
2557 static void
2558 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2559     boolean_t encapsulated)
2560 {
2561 	boolean_t legacy = B_FALSE;
2562 
2563 	/*
2564 	 * Note that only the following two sub-capabilities may be
2565 	 * considered as "legacy", since their original definitions
2566 	 * do not incorporate the dl_mid_t module ID token, and hence
2567 	 * may require the use of the wrapper sub-capability.
2568 	 */
2569 	switch (subp->dl_cap) {
2570 	case DL_CAPAB_IPSEC_AH:
2571 	case DL_CAPAB_IPSEC_ESP:
2572 		legacy = B_TRUE;
2573 		break;
2574 	}
2575 
2576 	/*
2577 	 * For legacy sub-capabilities which don't incorporate a queue_t
2578 	 * pointer in their structures, discard them if we detect that
2579 	 * there are intermediate modules in between IP and the driver.
2580 	 */
2581 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2582 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2583 		    "%d discarded; %d module(s) present below IP\n",
2584 		    subp->dl_cap, ill->ill_lmod_cnt));
2585 		return;
2586 	}
2587 
2588 	switch (subp->dl_cap) {
2589 	case DL_CAPAB_IPSEC_AH:
2590 	case DL_CAPAB_IPSEC_ESP:
2591 		ill_capability_ipsec_ack(ill, mp, subp);
2592 		break;
2593 	case DL_CAPAB_MDT:
2594 		ill_capability_mdt_ack(ill, mp, subp);
2595 		break;
2596 	case DL_CAPAB_HCKSUM:
2597 		ill_capability_hcksum_ack(ill, mp, subp);
2598 		break;
2599 	case DL_CAPAB_ZEROCOPY:
2600 		ill_capability_zerocopy_ack(ill, mp, subp);
2601 		break;
2602 	case DL_CAPAB_DLD:
2603 		ill_capability_dld_ack(ill, mp, subp);
2604 		break;
2605 	default:
2606 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2607 		    subp->dl_cap));
2608 	}
2609 }
2610 
2611 /*
2612  * Process a hardware checksum offload capability negotiation ack received
2613  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
2614  * of a DL_CAPABILITY_ACK message.
2615  */
2616 static void
2617 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2618 {
2619 	dl_capability_req_t	*ocap;
2620 	dl_capab_hcksum_t	*ihck, *ohck;
2621 	ill_hcksum_capab_t	**ill_hcksum;
2622 	mblk_t			*nmp = NULL;
2623 	uint_t			sub_dl_cap = isub->dl_cap;
2624 	uint8_t			*capend;
2625 
2626 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
2627 
2628 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
2629 
2630 	/*
2631 	 * Note: range checks here are not absolutely sufficient to
2632 	 * make us robust against malformed messages sent by drivers;
2633 	 * this is in keeping with the rest of IP's dlpi handling.
2634 	 * (Remember, it's coming from something else in the kernel
2635 	 * address space)
2636 	 */
2637 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2638 	if (capend > mp->b_wptr) {
2639 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2640 		    "malformed sub-capability too long for mblk");
2641 		return;
2642 	}
2643 
2644 	/*
2645 	 * There are two types of acks we process here:
2646 	 * 1. acks in reply to a (first form) generic capability req
2647 	 *    (no ENABLE flag set)
2648 	 * 2. acks in reply to a ENABLE capability req.
2649 	 *    (ENABLE flag set)
2650 	 */
2651 	ihck = (dl_capab_hcksum_t *)(isub + 1);
2652 
2653 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
2654 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
2655 		    "unsupported hardware checksum "
2656 		    "sub-capability (version %d, expected %d)",
2657 		    ihck->hcksum_version, HCKSUM_VERSION_1);
2658 		return;
2659 	}
2660 
2661 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
2662 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
2663 		    "checksum capability isn't as expected; pass-thru "
2664 		    "module(s) detected, discarding capability\n"));
2665 		return;
2666 	}
2667 
2668 #define	CURR_HCKSUM_CAPAB				\
2669 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
2670 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
2671 
2672 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
2673 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
2674 		/* do ENABLE processing */
2675 		if (*ill_hcksum == NULL) {
2676 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
2677 			    KM_NOSLEEP);
2678 
2679 			if (*ill_hcksum == NULL) {
2680 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2681 				    "could not enable hcksum version %d "
2682 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
2683 				    ill->ill_name);
2684 				return;
2685 			}
2686 		}
2687 
2688 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
2689 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
2690 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
2691 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
2692 		    "has enabled hardware checksumming\n ",
2693 		    ill->ill_name));
2694 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
2695 		/*
2696 		 * Enabling hardware checksum offload
2697 		 * Currently IP supports {TCP,UDP}/IPv4
2698 		 * partial and full cksum offload and
2699 		 * IPv4 header checksum offload.
2700 		 * Allocate new mblk which will
2701 		 * contain a new capability request
2702 		 * to enable hardware checksum offload.
2703 		 */
2704 		uint_t	size;
2705 		uchar_t	*rptr;
2706 
2707 		size = sizeof (dl_capability_req_t) +
2708 		    sizeof (dl_capability_sub_t) + isub->dl_length;
2709 
2710 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2711 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2712 			    "could not enable hardware cksum for %s (ENOMEM)\n",
2713 			    ill->ill_name);
2714 			return;
2715 		}
2716 
2717 		rptr = nmp->b_rptr;
2718 		/* initialize dl_capability_req_t */
2719 		ocap = (dl_capability_req_t *)nmp->b_rptr;
2720 		ocap->dl_sub_offset =
2721 		    sizeof (dl_capability_req_t);
2722 		ocap->dl_sub_length =
2723 		    sizeof (dl_capability_sub_t) +
2724 		    isub->dl_length;
2725 		nmp->b_rptr += sizeof (dl_capability_req_t);
2726 
2727 		/* initialize dl_capability_sub_t */
2728 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2729 		nmp->b_rptr += sizeof (*isub);
2730 
2731 		/* initialize dl_capab_hcksum_t */
2732 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
2733 		bcopy(ihck, ohck, sizeof (*ihck));
2734 
2735 		nmp->b_rptr = rptr;
2736 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2737 
2738 		/* Set ENABLE flag */
2739 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
2740 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
2741 
2742 		/*
2743 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2744 		 * hardware checksum acceleration.
2745 		 */
2746 		ill_capability_send(ill, nmp);
2747 	} else {
2748 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
2749 		    "advertised %x hardware checksum capability flags\n",
2750 		    ill->ill_name, ihck->hcksum_txflags));
2751 	}
2752 }
2753 
2754 static void
2755 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
2756 {
2757 	dl_capab_hcksum_t *hck_subcap;
2758 	dl_capability_sub_t *dl_subcap;
2759 
2760 	if (!ILL_HCKSUM_CAPABLE(ill))
2761 		return;
2762 
2763 	ASSERT(ill->ill_hcksum_capab != NULL);
2764 
2765 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2766 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
2767 	dl_subcap->dl_length = sizeof (*hck_subcap);
2768 
2769 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
2770 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
2771 	hck_subcap->hcksum_txflags = 0;
2772 
2773 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
2774 }
2775 
2776 static void
2777 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2778 {
2779 	mblk_t *nmp = NULL;
2780 	dl_capability_req_t *oc;
2781 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
2782 	ill_zerocopy_capab_t **ill_zerocopy_capab;
2783 	uint_t sub_dl_cap = isub->dl_cap;
2784 	uint8_t *capend;
2785 
2786 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
2787 
2788 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
2789 
2790 	/*
2791 	 * Note: range checks here are not absolutely sufficient to
2792 	 * make us robust against malformed messages sent by drivers;
2793 	 * this is in keeping with the rest of IP's dlpi handling.
2794 	 * (Remember, it's coming from something else in the kernel
2795 	 * address space)
2796 	 */
2797 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2798 	if (capend > mp->b_wptr) {
2799 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2800 		    "malformed sub-capability too long for mblk");
2801 		return;
2802 	}
2803 
2804 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
2805 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
2806 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
2807 		    "unsupported ZEROCOPY sub-capability (version %d, "
2808 		    "expected %d)", zc_ic->zerocopy_version,
2809 		    ZEROCOPY_VERSION_1);
2810 		return;
2811 	}
2812 
2813 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
2814 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
2815 		    "capability isn't as expected; pass-thru module(s) "
2816 		    "detected, discarding capability\n"));
2817 		return;
2818 	}
2819 
2820 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
2821 		if (*ill_zerocopy_capab == NULL) {
2822 			*ill_zerocopy_capab =
2823 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
2824 			    KM_NOSLEEP);
2825 
2826 			if (*ill_zerocopy_capab == NULL) {
2827 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2828 				    "could not enable Zero-copy version %d "
2829 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
2830 				    ill->ill_name);
2831 				return;
2832 			}
2833 		}
2834 
2835 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
2836 		    "supports Zero-copy version %d\n", ill->ill_name,
2837 		    ZEROCOPY_VERSION_1));
2838 
2839 		(*ill_zerocopy_capab)->ill_zerocopy_version =
2840 		    zc_ic->zerocopy_version;
2841 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
2842 		    zc_ic->zerocopy_flags;
2843 
2844 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
2845 	} else {
2846 		uint_t size;
2847 		uchar_t *rptr;
2848 
2849 		size = sizeof (dl_capability_req_t) +
2850 		    sizeof (dl_capability_sub_t) +
2851 		    sizeof (dl_capab_zerocopy_t);
2852 
2853 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2854 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2855 			    "could not enable zerocopy for %s (ENOMEM)\n",
2856 			    ill->ill_name);
2857 			return;
2858 		}
2859 
2860 		rptr = nmp->b_rptr;
2861 		/* initialize dl_capability_req_t */
2862 		oc = (dl_capability_req_t *)rptr;
2863 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2864 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2865 		    sizeof (dl_capab_zerocopy_t);
2866 		rptr += sizeof (dl_capability_req_t);
2867 
2868 		/* initialize dl_capability_sub_t */
2869 		bcopy(isub, rptr, sizeof (*isub));
2870 		rptr += sizeof (*isub);
2871 
2872 		/* initialize dl_capab_zerocopy_t */
2873 		zc_oc = (dl_capab_zerocopy_t *)rptr;
2874 		*zc_oc = *zc_ic;
2875 
2876 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
2877 		    "to enable zero-copy version %d\n", ill->ill_name,
2878 		    ZEROCOPY_VERSION_1));
2879 
2880 		/* set VMSAFE_MEM flag */
2881 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
2882 
2883 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
2884 		ill_capability_send(ill, nmp);
2885 	}
2886 }
2887 
2888 static void
2889 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
2890 {
2891 	dl_capab_zerocopy_t *zerocopy_subcap;
2892 	dl_capability_sub_t *dl_subcap;
2893 
2894 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
2895 		return;
2896 
2897 	ASSERT(ill->ill_zerocopy_capab != NULL);
2898 
2899 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2900 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
2901 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
2902 
2903 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
2904 	zerocopy_subcap->zerocopy_version =
2905 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
2906 	zerocopy_subcap->zerocopy_flags = 0;
2907 
2908 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
2909 }
2910 
2911 /*
2912  * DLD capability
2913  * Refer to dld.h for more information regarding the purpose and usage
2914  * of this capability.
2915  */
2916 static void
2917 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2918 {
2919 	dl_capab_dld_t		*dld_ic, dld;
2920 	uint_t			sub_dl_cap = isub->dl_cap;
2921 	uint8_t			*capend;
2922 	ill_dld_capab_t		*idc;
2923 
2924 	ASSERT(IAM_WRITER_ILL(ill));
2925 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
2926 
2927 	/*
2928 	 * Note: range checks here are not absolutely sufficient to
2929 	 * make us robust against malformed messages sent by drivers;
2930 	 * this is in keeping with the rest of IP's dlpi handling.
2931 	 * (Remember, it's coming from something else in the kernel
2932 	 * address space)
2933 	 */
2934 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2935 	if (capend > mp->b_wptr) {
2936 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
2937 		    "malformed sub-capability too long for mblk");
2938 		return;
2939 	}
2940 	dld_ic = (dl_capab_dld_t *)(isub + 1);
2941 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
2942 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
2943 		    "unsupported DLD sub-capability (version %d, "
2944 		    "expected %d)", dld_ic->dld_version,
2945 		    DLD_CURRENT_VERSION);
2946 		return;
2947 	}
2948 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
2949 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
2950 		    "capability isn't as expected; pass-thru module(s) "
2951 		    "detected, discarding capability\n"));
2952 		return;
2953 	}
2954 
2955 	/*
2956 	 * Copy locally to ensure alignment.
2957 	 */
2958 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
2959 
2960 	if ((idc = ill->ill_dld_capab) == NULL) {
2961 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
2962 		if (idc == NULL) {
2963 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
2964 			    "could not enable DLD version %d "
2965 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
2966 			    ill->ill_name);
2967 			return;
2968 		}
2969 		idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
2970 		idc->idc_capab_dh = (void *)dld.dld_capab_handle;
2971 		ill->ill_dld_capab = idc;
2972 	}
2973 	ip1dbg(("ill_capability_dld_ack: interface %s "
2974 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
2975 
2976 	ill_capability_dld_enable(ill);
2977 }
2978 
2979 /*
2980  * Typically capability negotiation between IP and the driver happens via
2981  * DLPI message exchange. However GLD also offers a direct function call
2982  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
2983  * But arbitrary function calls into IP or GLD are not permitted, since both
2984  * of them are protected by their own perimeter mechanism. The perimeter can
2985  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
2986  * these perimeters is IP -> MAC. Thus for example to enable the squeue
2987  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
2988  * to enter the mac perimeter and then do the direct function calls into
2989  * GLD to enable squeue polling. The ring related callbacks from the mac into
2990  * the stack to add, bind, quiesce, restart or cleanup a ring are all
2991  * protected by the mac perimeter.
2992  */
2993 static void
2994 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
2995 {
2996 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2997 	int			err;
2998 
2999 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
3000 	    DLD_ENABLE);
3001 	ASSERT(err == 0);
3002 }
3003 
3004 static void
3005 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
3006 {
3007 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3008 	int			err;
3009 
3010 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
3011 	    DLD_DISABLE);
3012 	ASSERT(err == 0);
3013 }
3014 
3015 boolean_t
3016 ill_mac_perim_held(ill_t *ill)
3017 {
3018 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3019 
3020 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
3021 	    DLD_QUERY));
3022 }
3023 
3024 static void
3025 ill_capability_direct_enable(ill_t *ill)
3026 {
3027 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3028 	ill_dld_direct_t	*idd = &idc->idc_direct;
3029 	dld_capab_direct_t	direct;
3030 	int			rc;
3031 
3032 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3033 
3034 	bzero(&direct, sizeof (direct));
3035 	direct.di_rx_cf = (uintptr_t)ip_input;
3036 	direct.di_rx_ch = ill;
3037 
3038 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
3039 	    DLD_ENABLE);
3040 	if (rc == 0) {
3041 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
3042 		idd->idd_tx_dh = direct.di_tx_dh;
3043 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
3044 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
3045 		/*
3046 		 * One time registration of flow enable callback function
3047 		 */
3048 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
3049 		    ill_flow_enable, ill);
3050 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
3051 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
3052 	} else {
3053 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
3054 		    "capability, rc = %d\n", rc);
3055 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
3056 	}
3057 }
3058 
3059 static void
3060 ill_capability_poll_enable(ill_t *ill)
3061 {
3062 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3063 	dld_capab_poll_t	poll;
3064 	int			rc;
3065 
3066 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3067 
3068 	bzero(&poll, sizeof (poll));
3069 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
3070 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
3071 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
3072 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
3073 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
3074 	poll.poll_ring_ch = ill;
3075 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
3076 	    DLD_ENABLE);
3077 	if (rc == 0) {
3078 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
3079 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
3080 	} else {
3081 		ip1dbg(("warning: could not enable POLL "
3082 		    "capability, rc = %d\n", rc));
3083 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
3084 	}
3085 }
3086 
3087 /*
3088  * Enable the LSO capability.
3089  */
3090 static void
3091 ill_capability_lso_enable(ill_t *ill)
3092 {
3093 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
3094 	dld_capab_lso_t	lso;
3095 	int rc;
3096 
3097 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3098 
3099 	if (ill->ill_lso_capab == NULL) {
3100 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3101 		    KM_NOSLEEP);
3102 		if (ill->ill_lso_capab == NULL) {
3103 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
3104 			    "could not enable LSO for %s (ENOMEM)\n",
3105 			    ill->ill_name);
3106 			return;
3107 		}
3108 	}
3109 
3110 	bzero(&lso, sizeof (lso));
3111 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
3112 	    DLD_ENABLE)) == 0) {
3113 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
3114 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
3115 		ill->ill_capabilities |= ILL_CAPAB_DLD_LSO;
3116 		ip1dbg(("ill_capability_lso_enable: interface %s "
3117 		    "has enabled LSO\n ", ill->ill_name));
3118 	} else {
3119 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
3120 		ill->ill_lso_capab = NULL;
3121 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
3122 	}
3123 }
3124 
3125 static void
3126 ill_capability_dld_enable(ill_t *ill)
3127 {
3128 	mac_perim_handle_t mph;
3129 
3130 	ASSERT(IAM_WRITER_ILL(ill));
3131 
3132 	if (ill->ill_isv6)
3133 		return;
3134 
3135 	ill_mac_perim_enter(ill, &mph);
3136 	if (!ill->ill_isv6) {
3137 		ill_capability_direct_enable(ill);
3138 		ill_capability_poll_enable(ill);
3139 		ill_capability_lso_enable(ill);
3140 	}
3141 	ill->ill_capabilities |= ILL_CAPAB_DLD;
3142 	ill_mac_perim_exit(ill, mph);
3143 }
3144 
3145 static void
3146 ill_capability_dld_disable(ill_t *ill)
3147 {
3148 	ill_dld_capab_t	*idc;
3149 	ill_dld_direct_t *idd;
3150 	mac_perim_handle_t	mph;
3151 
3152 	ASSERT(IAM_WRITER_ILL(ill));
3153 
3154 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
3155 		return;
3156 
3157 	ill_mac_perim_enter(ill, &mph);
3158 
3159 	idc = ill->ill_dld_capab;
3160 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
3161 		/*
3162 		 * For performance we avoid locks in the transmit data path
3163 		 * and don't maintain a count of the number of threads using
3164 		 * direct calls. Thus some threads could be using direct
3165 		 * transmit calls to GLD, even after the capability mechanism
3166 		 * turns it off. This is still safe since the handles used in
3167 		 * the direct calls continue to be valid until the unplumb is
3168 		 * completed. Remove the callback that was added (1-time) at
3169 		 * capab enable time.
3170 		 */
3171 		mutex_enter(&ill->ill_lock);
3172 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
3173 		mutex_exit(&ill->ill_lock);
3174 		if (ill->ill_flownotify_mh != NULL) {
3175 			idd = &idc->idc_direct;
3176 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
3177 			    ill->ill_flownotify_mh);
3178 			ill->ill_flownotify_mh = NULL;
3179 		}
3180 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
3181 		    NULL, DLD_DISABLE);
3182 	}
3183 
3184 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
3185 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
3186 		ip_squeue_clean_all(ill);
3187 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
3188 		    NULL, DLD_DISABLE);
3189 	}
3190 
3191 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_LSO) != 0) {
3192 		ASSERT(ill->ill_lso_capab != NULL);
3193 		/*
3194 		 * Clear the capability flag for LSO but retain the
3195 		 * ill_lso_capab structure since it's possible that another
3196 		 * thread is still referring to it.  The structure only gets
3197 		 * deallocated when we destroy the ill.
3198 		 */
3199 
3200 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_LSO;
3201 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
3202 		    NULL, DLD_DISABLE);
3203 	}
3204 
3205 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
3206 	ill_mac_perim_exit(ill, mph);
3207 }
3208 
3209 /*
3210  * Capability Negotiation protocol
3211  *
3212  * We don't wait for DLPI capability operations to finish during interface
3213  * bringup or teardown. Doing so would introduce more asynchrony and the
3214  * interface up/down operations will need multiple return and restarts.
3215  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
3216  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
3217  * exclusive operation won't start until the DLPI operations of the previous
3218  * exclusive operation complete.
3219  *
3220  * The capability state machine is shown below.
3221  *
3222  * state		next state		event, action
3223  *
3224  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
3225  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
3226  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
3227  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
3228  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
3229  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
3230  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
3231  *						    ill_capability_probe.
3232  */
3233 
3234 /*
3235  * Dedicated thread started from ip_stack_init that handles capability
3236  * disable. This thread ensures the taskq dispatch does not fail by waiting
3237  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
3238  * that direct calls to DLD are done in a cv_waitable context.
3239  */
3240 void
3241 ill_taskq_dispatch(ip_stack_t *ipst)
3242 {
3243 	callb_cpr_t cprinfo;
3244 	char 	name[64];
3245 	mblk_t	*mp;
3246 
3247 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
3248 	    ipst->ips_netstack->netstack_stackid);
3249 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
3250 	    name);
3251 	mutex_enter(&ipst->ips_capab_taskq_lock);
3252 
3253 	for (;;) {
3254 		mp = list_head(&ipst->ips_capab_taskq_list);
3255 		while (mp != NULL) {
3256 			list_remove(&ipst->ips_capab_taskq_list, mp);
3257 			mutex_exit(&ipst->ips_capab_taskq_lock);
3258 			VERIFY(taskq_dispatch(system_taskq,
3259 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
3260 			mutex_enter(&ipst->ips_capab_taskq_lock);
3261 			mp = list_head(&ipst->ips_capab_taskq_list);
3262 		}
3263 
3264 		if (ipst->ips_capab_taskq_quit)
3265 			break;
3266 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
3267 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
3268 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
3269 	}
3270 	VERIFY(list_head(&ipst->ips_capab_taskq_list) == NULL);
3271 	CALLB_CPR_EXIT(&cprinfo);
3272 	thread_exit();
3273 }
3274 
3275 /*
3276  * Consume a new-style hardware capabilities negotiation ack.
3277  * Called via taskq on receipt of DL_CAPABBILITY_ACK.
3278  */
3279 static void
3280 ill_capability_ack_thr(void *arg)
3281 {
3282 	mblk_t	*mp = arg;
3283 	dl_capability_ack_t *capp;
3284 	dl_capability_sub_t *subp, *endp;
3285 	ill_t	*ill;
3286 	boolean_t reneg;
3287 
3288 	ill = (ill_t *)mp->b_prev;
3289 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
3290 
3291 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
3292 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
3293 		/*
3294 		 * We have received the ack for our DL_CAPAB reset request.
3295 		 * There isnt' anything in the message that needs processing.
3296 		 * All message based capabilities have been disabled, now
3297 		 * do the function call based capability disable.
3298 		 */
3299 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
3300 		ill_capability_dld_disable(ill);
3301 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
3302 		if (reneg)
3303 			ill_capability_probe(ill);
3304 		goto done;
3305 	}
3306 
3307 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
3308 		ill->ill_dlpi_capab_state = IDCS_OK;
3309 
3310 	capp = (dl_capability_ack_t *)mp->b_rptr;
3311 
3312 	if (capp->dl_sub_length == 0) {
3313 		/* no new-style capabilities */
3314 		goto done;
3315 	}
3316 
3317 	/* make sure the driver supplied correct dl_sub_length */
3318 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3319 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3320 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3321 		goto done;
3322 	}
3323 
3324 
3325 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3326 	/*
3327 	 * There are sub-capabilities. Process the ones we know about.
3328 	 * Loop until we don't have room for another sub-cap header..
3329 	 */
3330 	for (subp = SC(capp, capp->dl_sub_offset),
3331 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3332 	    subp <= endp;
3333 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3334 
3335 		switch (subp->dl_cap) {
3336 		case DL_CAPAB_ID_WRAPPER:
3337 			ill_capability_id_ack(ill, mp, subp);
3338 			break;
3339 		default:
3340 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3341 			break;
3342 		}
3343 	}
3344 #undef SC
3345 done:
3346 	inet_freemsg(mp);
3347 	ill_capability_done(ill);
3348 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
3349 }
3350 
3351 /*
3352  * This needs to be started in a taskq thread to provide a cv_waitable
3353  * context.
3354  */
3355 void
3356 ill_capability_ack(ill_t *ill, mblk_t *mp)
3357 {
3358 	ip_stack_t	*ipst = ill->ill_ipst;
3359 
3360 	mp->b_prev = (mblk_t *)ill;
3361 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
3362 	    TQ_NOSLEEP) != 0)
3363 		return;
3364 
3365 	/*
3366 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
3367 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
3368 	 */
3369 	mutex_enter(&ipst->ips_capab_taskq_lock);
3370 	list_insert_tail(&ipst->ips_capab_taskq_list, mp);
3371 	cv_signal(&ipst->ips_capab_taskq_cv);
3372 	mutex_exit(&ipst->ips_capab_taskq_lock);
3373 }
3374 
3375 /*
3376  * This routine is called to scan the fragmentation reassembly table for
3377  * the specified ILL for any packets that are starting to smell.
3378  * dead_interval is the maximum time in seconds that will be tolerated.  It
3379  * will either be the value specified in ip_g_frag_timeout, or zero if the
3380  * ILL is shutting down and it is time to blow everything off.
3381  *
3382  * It returns the number of seconds (as a time_t) that the next frag timer
3383  * should be scheduled for, 0 meaning that the timer doesn't need to be
3384  * re-started.  Note that the method of calculating next_timeout isn't
3385  * entirely accurate since time will flow between the time we grab
3386  * current_time and the time we schedule the next timeout.  This isn't a
3387  * big problem since this is the timer for sending an ICMP reassembly time
3388  * exceeded messages, and it doesn't have to be exactly accurate.
3389  *
3390  * This function is
3391  * sometimes called as writer, although this is not required.
3392  */
3393 time_t
3394 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3395 {
3396 	ipfb_t	*ipfb;
3397 	ipfb_t	*endp;
3398 	ipf_t	*ipf;
3399 	ipf_t	*ipfnext;
3400 	mblk_t	*mp;
3401 	time_t	current_time = gethrestime_sec();
3402 	time_t	next_timeout = 0;
3403 	uint32_t	hdr_length;
3404 	mblk_t	*send_icmp_head;
3405 	mblk_t	*send_icmp_head_v6;
3406 	zoneid_t zoneid;
3407 	ip_stack_t *ipst = ill->ill_ipst;
3408 
3409 	ipfb = ill->ill_frag_hash_tbl;
3410 	if (ipfb == NULL)
3411 		return (B_FALSE);
3412 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3413 	/* Walk the frag hash table. */
3414 	for (; ipfb < endp; ipfb++) {
3415 		send_icmp_head = NULL;
3416 		send_icmp_head_v6 = NULL;
3417 		mutex_enter(&ipfb->ipfb_lock);
3418 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3419 			time_t frag_time = current_time - ipf->ipf_timestamp;
3420 			time_t frag_timeout;
3421 
3422 			if (frag_time < dead_interval) {
3423 				/*
3424 				 * There are some outstanding fragments
3425 				 * that will timeout later.  Make note of
3426 				 * the time so that we can reschedule the
3427 				 * next timeout appropriately.
3428 				 */
3429 				frag_timeout = dead_interval - frag_time;
3430 				if (next_timeout == 0 ||
3431 				    frag_timeout < next_timeout) {
3432 					next_timeout = frag_timeout;
3433 				}
3434 				break;
3435 			}
3436 			/* Time's up.  Get it out of here. */
3437 			hdr_length = ipf->ipf_nf_hdr_len;
3438 			ipfnext = ipf->ipf_hash_next;
3439 			if (ipfnext)
3440 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3441 			*ipf->ipf_ptphn = ipfnext;
3442 			mp = ipf->ipf_mp->b_cont;
3443 			for (; mp; mp = mp->b_cont) {
3444 				/* Extra points for neatness. */
3445 				IP_REASS_SET_START(mp, 0);
3446 				IP_REASS_SET_END(mp, 0);
3447 			}
3448 			mp = ipf->ipf_mp->b_cont;
3449 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
3450 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3451 			ipfb->ipfb_count -= ipf->ipf_count;
3452 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3453 			ipfb->ipfb_frag_pkts--;
3454 			/*
3455 			 * We do not send any icmp message from here because
3456 			 * we currently are holding the ipfb_lock for this
3457 			 * hash chain. If we try and send any icmp messages
3458 			 * from here we may end up via a put back into ip
3459 			 * trying to get the same lock, causing a recursive
3460 			 * mutex panic. Instead we build a list and send all
3461 			 * the icmp messages after we have dropped the lock.
3462 			 */
3463 			if (ill->ill_isv6) {
3464 				if (hdr_length != 0) {
3465 					mp->b_next = send_icmp_head_v6;
3466 					send_icmp_head_v6 = mp;
3467 				} else {
3468 					freemsg(mp);
3469 				}
3470 			} else {
3471 				if (hdr_length != 0) {
3472 					mp->b_next = send_icmp_head;
3473 					send_icmp_head = mp;
3474 				} else {
3475 					freemsg(mp);
3476 				}
3477 			}
3478 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3479 			freeb(ipf->ipf_mp);
3480 		}
3481 		mutex_exit(&ipfb->ipfb_lock);
3482 		/*
3483 		 * Now need to send any icmp messages that we delayed from
3484 		 * above.
3485 		 */
3486 		while (send_icmp_head_v6 != NULL) {
3487 			ip6_t *ip6h;
3488 
3489 			mp = send_icmp_head_v6;
3490 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3491 			mp->b_next = NULL;
3492 			if (mp->b_datap->db_type == M_CTL)
3493 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3494 			else
3495 				ip6h = (ip6_t *)mp->b_rptr;
3496 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3497 			    ill, ipst);
3498 			if (zoneid == ALL_ZONES) {
3499 				freemsg(mp);
3500 			} else {
3501 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3502 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3503 				    B_FALSE, zoneid, ipst);
3504 			}
3505 		}
3506 		while (send_icmp_head != NULL) {
3507 			ipaddr_t dst;
3508 
3509 			mp = send_icmp_head;
3510 			send_icmp_head = send_icmp_head->b_next;
3511 			mp->b_next = NULL;
3512 
3513 			if (mp->b_datap->db_type == M_CTL)
3514 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3515 			else
3516 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3517 
3518 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3519 			if (zoneid == ALL_ZONES) {
3520 				freemsg(mp);
3521 			} else {
3522 				icmp_time_exceeded(ill->ill_wq, mp,
3523 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3524 				    ipst);
3525 			}
3526 		}
3527 	}
3528 	/*
3529 	 * A non-dying ILL will use the return value to decide whether to
3530 	 * restart the frag timer, and for how long.
3531 	 */
3532 	return (next_timeout);
3533 }
3534 
3535 /*
3536  * This routine is called when the approximate count of mblk memory used
3537  * for the specified ILL has exceeded max_count.
3538  */
3539 void
3540 ill_frag_prune(ill_t *ill, uint_t max_count)
3541 {
3542 	ipfb_t	*ipfb;
3543 	ipf_t	*ipf;
3544 	size_t	count;
3545 
3546 	/*
3547 	 * If we are here within ip_min_frag_prune_time msecs remove
3548 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3549 	 * ill_frag_free_num_pkts.
3550 	 */
3551 	mutex_enter(&ill->ill_lock);
3552 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3553 	    (ip_min_frag_prune_time != 0 ?
3554 	    ip_min_frag_prune_time : msec_per_tick)) {
3555 
3556 		ill->ill_frag_free_num_pkts++;
3557 
3558 	} else {
3559 		ill->ill_frag_free_num_pkts = 0;
3560 	}
3561 	ill->ill_last_frag_clean_time = lbolt;
3562 	mutex_exit(&ill->ill_lock);
3563 
3564 	/*
3565 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3566 	 */
3567 	if (ill->ill_frag_free_num_pkts != 0) {
3568 		int ix;
3569 
3570 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3571 			ipfb = &ill->ill_frag_hash_tbl[ix];
3572 			mutex_enter(&ipfb->ipfb_lock);
3573 			if (ipfb->ipfb_ipf != NULL) {
3574 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3575 				    ill->ill_frag_free_num_pkts);
3576 			}
3577 			mutex_exit(&ipfb->ipfb_lock);
3578 		}
3579 	}
3580 	/*
3581 	 * While the reassembly list for this ILL is too big, prune a fragment
3582 	 * queue by age, oldest first.
3583 	 */
3584 	while (ill->ill_frag_count > max_count) {
3585 		int	ix;
3586 		ipfb_t	*oipfb = NULL;
3587 		uint_t	oldest = UINT_MAX;
3588 
3589 		count = 0;
3590 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3591 			ipfb = &ill->ill_frag_hash_tbl[ix];
3592 			mutex_enter(&ipfb->ipfb_lock);
3593 			ipf = ipfb->ipfb_ipf;
3594 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3595 				oldest = ipf->ipf_gen;
3596 				oipfb = ipfb;
3597 			}
3598 			count += ipfb->ipfb_count;
3599 			mutex_exit(&ipfb->ipfb_lock);
3600 		}
3601 		if (oipfb == NULL)
3602 			break;
3603 
3604 		if (count <= max_count)
3605 			return;	/* Somebody beat us to it, nothing to do */
3606 		mutex_enter(&oipfb->ipfb_lock);
3607 		ipf = oipfb->ipfb_ipf;
3608 		if (ipf != NULL) {
3609 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3610 		}
3611 		mutex_exit(&oipfb->ipfb_lock);
3612 	}
3613 }
3614 
3615 /*
3616  * free 'free_cnt' fragmented packets starting at ipf.
3617  */
3618 void
3619 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3620 {
3621 	size_t	count;
3622 	mblk_t	*mp;
3623 	mblk_t	*tmp;
3624 	ipf_t **ipfp = ipf->ipf_ptphn;
3625 
3626 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3627 	ASSERT(ipfp != NULL);
3628 	ASSERT(ipf != NULL);
3629 
3630 	while (ipf != NULL && free_cnt-- > 0) {
3631 		count = ipf->ipf_count;
3632 		mp = ipf->ipf_mp;
3633 		ipf = ipf->ipf_hash_next;
3634 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3635 			IP_REASS_SET_START(tmp, 0);
3636 			IP_REASS_SET_END(tmp, 0);
3637 		}
3638 		atomic_add_32(&ill->ill_frag_count, -count);
3639 		ASSERT(ipfb->ipfb_count >= count);
3640 		ipfb->ipfb_count -= count;
3641 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3642 		ipfb->ipfb_frag_pkts--;
3643 		freemsg(mp);
3644 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3645 	}
3646 
3647 	if (ipf)
3648 		ipf->ipf_ptphn = ipfp;
3649 	ipfp[0] = ipf;
3650 }
3651 
3652 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3653 	"obsolete and may be removed in a future release of Solaris.  Use " \
3654 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3655 
3656 /*
3657  * For obsolete per-interface forwarding configuration;
3658  * called in response to ND_GET.
3659  */
3660 /* ARGSUSED */
3661 static int
3662 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3663 {
3664 	ill_t *ill = (ill_t *)cp;
3665 
3666 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3667 
3668 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3669 	return (0);
3670 }
3671 
3672 /*
3673  * For obsolete per-interface forwarding configuration;
3674  * called in response to ND_SET.
3675  */
3676 /* ARGSUSED */
3677 static int
3678 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3679     cred_t *ioc_cr)
3680 {
3681 	long value;
3682 	int retval;
3683 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3684 
3685 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3686 
3687 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3688 	    value < 0 || value > 1) {
3689 		return (EINVAL);
3690 	}
3691 
3692 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3693 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3694 	rw_exit(&ipst->ips_ill_g_lock);
3695 	return (retval);
3696 }
3697 
3698 /*
3699  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3700  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3701  * up RTS_IFINFO routing socket messages for each interface whose flags we
3702  * change.
3703  */
3704 int
3705 ill_forward_set(ill_t *ill, boolean_t enable)
3706 {
3707 	ill_group_t *illgrp;
3708 	ip_stack_t	*ipst = ill->ill_ipst;
3709 
3710 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3711 
3712 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3713 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3714 		return (0);
3715 
3716 	if (IS_LOOPBACK(ill))
3717 		return (EINVAL);
3718 
3719 	/*
3720 	 * If the ill is in an IPMP group, set the forwarding policy on all
3721 	 * members of the group to the same value.
3722 	 */
3723 	illgrp = ill->ill_group;
3724 	if (illgrp != NULL) {
3725 		ill_t *tmp_ill;
3726 
3727 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3728 		    tmp_ill = tmp_ill->ill_group_next) {
3729 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3730 			    (enable ? "Enabling" : "Disabling"),
3731 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3732 			    tmp_ill->ill_name));
3733 			mutex_enter(&tmp_ill->ill_lock);
3734 			if (enable)
3735 				tmp_ill->ill_flags |= ILLF_ROUTER;
3736 			else
3737 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3738 			mutex_exit(&tmp_ill->ill_lock);
3739 			if (tmp_ill->ill_isv6)
3740 				ill_set_nce_router_flags(tmp_ill, enable);
3741 			/* Notify routing socket listeners of this change. */
3742 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3743 		}
3744 	} else {
3745 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3746 		    (enable ? "Enabling" : "Disabling"),
3747 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3748 		mutex_enter(&ill->ill_lock);
3749 		if (enable)
3750 			ill->ill_flags |= ILLF_ROUTER;
3751 		else
3752 			ill->ill_flags &= ~ILLF_ROUTER;
3753 		mutex_exit(&ill->ill_lock);
3754 		if (ill->ill_isv6)
3755 			ill_set_nce_router_flags(ill, enable);
3756 		/* Notify routing socket listeners of this change. */
3757 		ip_rts_ifmsg(ill->ill_ipif);
3758 	}
3759 
3760 	return (0);
3761 }
3762 
3763 /*
3764  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3765  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3766  * set or clear.
3767  */
3768 static void
3769 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3770 {
3771 	ipif_t *ipif;
3772 	nce_t *nce;
3773 
3774 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3775 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3776 		if (nce != NULL) {
3777 			mutex_enter(&nce->nce_lock);
3778 			if (enable)
3779 				nce->nce_flags |= NCE_F_ISROUTER;
3780 			else
3781 				nce->nce_flags &= ~NCE_F_ISROUTER;
3782 			mutex_exit(&nce->nce_lock);
3783 			NCE_REFRELE(nce);
3784 		}
3785 	}
3786 }
3787 
3788 /*
3789  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3790  * for this ill.  Make sure the v6/v4 question has been answered about this
3791  * ill.  The creation of this ndd variable is only for backwards compatibility.
3792  * The preferred way to control per-interface IP forwarding is through the
3793  * ILLF_ROUTER interface flag.
3794  */
3795 static int
3796 ill_set_ndd_name(ill_t *ill)
3797 {
3798 	char *suffix;
3799 	ip_stack_t	*ipst = ill->ill_ipst;
3800 
3801 	ASSERT(IAM_WRITER_ILL(ill));
3802 
3803 	if (ill->ill_isv6)
3804 		suffix = ipv6_forward_suffix;
3805 	else
3806 		suffix = ipv4_forward_suffix;
3807 
3808 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
3809 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
3810 	/*
3811 	 * Copies over the '\0'.
3812 	 * Note that strlen(suffix) is always bounded.
3813 	 */
3814 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
3815 	    strlen(suffix) + 1);
3816 
3817 	/*
3818 	 * Use of the nd table requires holding the reader lock.
3819 	 * Modifying the nd table thru nd_load/nd_unload requires
3820 	 * the writer lock.
3821 	 */
3822 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
3823 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
3824 	    nd_ill_forward_set, (caddr_t)ill)) {
3825 		/*
3826 		 * If the nd_load failed, it only meant that it could not
3827 		 * allocate a new bunch of room for further NDD expansion.
3828 		 * Because of that, the ill_ndd_name will be set to 0, and
3829 		 * this interface is at the mercy of the global ip_forwarding
3830 		 * variable.
3831 		 */
3832 		rw_exit(&ipst->ips_ip_g_nd_lock);
3833 		ill->ill_ndd_name = NULL;
3834 		return (ENOMEM);
3835 	}
3836 	rw_exit(&ipst->ips_ip_g_nd_lock);
3837 	return (0);
3838 }
3839 
3840 /*
3841  * Intializes the context structure and returns the first ill in the list
3842  * cuurently start_list and end_list can have values:
3843  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
3844  * IP_V4_G_HEAD		Traverse IPV4 list only.
3845  * IP_V6_G_HEAD		Traverse IPV6 list only.
3846  */
3847 
3848 /*
3849  * We don't check for CONDEMNED ills here. Caller must do that if
3850  * necessary under the ill lock.
3851  */
3852 ill_t *
3853 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
3854     ip_stack_t *ipst)
3855 {
3856 	ill_if_t *ifp;
3857 	ill_t *ill;
3858 	avl_tree_t *avl_tree;
3859 
3860 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3861 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
3862 
3863 	/*
3864 	 * setup the lists to search
3865 	 */
3866 	if (end_list != MAX_G_HEADS) {
3867 		ctx->ctx_current_list = start_list;
3868 		ctx->ctx_last_list = end_list;
3869 	} else {
3870 		ctx->ctx_last_list = MAX_G_HEADS - 1;
3871 		ctx->ctx_current_list = 0;
3872 	}
3873 
3874 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
3875 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
3876 		if (ifp != (ill_if_t *)
3877 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
3878 			avl_tree = &ifp->illif_avl_by_ppa;
3879 			ill = avl_first(avl_tree);
3880 			/*
3881 			 * ill is guaranteed to be non NULL or ifp should have
3882 			 * not existed.
3883 			 */
3884 			ASSERT(ill != NULL);
3885 			return (ill);
3886 		}
3887 		ctx->ctx_current_list++;
3888 	}
3889 
3890 	return (NULL);
3891 }
3892 
3893 /*
3894  * returns the next ill in the list. ill_first() must have been called
3895  * before calling ill_next() or bad things will happen.
3896  */
3897 
3898 /*
3899  * We don't check for CONDEMNED ills here. Caller must do that if
3900  * necessary under the ill lock.
3901  */
3902 ill_t *
3903 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
3904 {
3905 	ill_if_t *ifp;
3906 	ill_t *ill;
3907 	ip_stack_t	*ipst = lastill->ill_ipst;
3908 
3909 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
3910 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
3911 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
3912 	    AVL_AFTER)) != NULL) {
3913 		return (ill);
3914 	}
3915 
3916 	/* goto next ill_ifp in the list. */
3917 	ifp = lastill->ill_ifptr->illif_next;
3918 
3919 	/* make sure not at end of circular list */
3920 	while (ifp ==
3921 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
3922 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
3923 			return (NULL);
3924 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
3925 	}
3926 
3927 	return (avl_first(&ifp->illif_avl_by_ppa));
3928 }
3929 
3930 /*
3931  * Check interface name for correct format which is name+ppa.
3932  * name can contain characters and digits, the right most digits
3933  * make up the ppa number. use of octal is not allowed, name must contain
3934  * a ppa, return pointer to the start of ppa.
3935  * In case of error return NULL.
3936  */
3937 static char *
3938 ill_get_ppa_ptr(char *name)
3939 {
3940 	int namelen = mi_strlen(name);
3941 
3942 	int len = namelen;
3943 
3944 	name += len;
3945 	while (len > 0) {
3946 		name--;
3947 		if (*name < '0' || *name > '9')
3948 			break;
3949 		len--;
3950 	}
3951 
3952 	/* empty string, all digits, or no trailing digits */
3953 	if (len == 0 || len == (int)namelen)
3954 		return (NULL);
3955 
3956 	name++;
3957 	/* check for attempted use of octal */
3958 	if (*name == '0' && len != (int)namelen - 1)
3959 		return (NULL);
3960 	return (name);
3961 }
3962 
3963 /*
3964  * use avl tree to locate the ill.
3965  */
3966 static ill_t *
3967 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
3968     ipsq_func_t func, int *error, ip_stack_t *ipst)
3969 {
3970 	char *ppa_ptr = NULL;
3971 	int len;
3972 	uint_t ppa;
3973 	ill_t *ill = NULL;
3974 	ill_if_t *ifp;
3975 	int list;
3976 	ipsq_t *ipsq;
3977 
3978 	if (error != NULL)
3979 		*error = 0;
3980 
3981 	/*
3982 	 * get ppa ptr
3983 	 */
3984 	if (isv6)
3985 		list = IP_V6_G_HEAD;
3986 	else
3987 		list = IP_V4_G_HEAD;
3988 
3989 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
3990 		if (error != NULL)
3991 			*error = ENXIO;
3992 		return (NULL);
3993 	}
3994 
3995 	len = ppa_ptr - name + 1;
3996 
3997 	ppa = stoi(&ppa_ptr);
3998 
3999 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4000 
4001 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4002 		/*
4003 		 * match is done on len - 1 as the name is not null
4004 		 * terminated it contains ppa in addition to the interface
4005 		 * name.
4006 		 */
4007 		if ((ifp->illif_name_len == len) &&
4008 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4009 			break;
4010 		} else {
4011 			ifp = ifp->illif_next;
4012 		}
4013 	}
4014 
4015 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4016 		/*
4017 		 * Even the interface type does not exist.
4018 		 */
4019 		if (error != NULL)
4020 			*error = ENXIO;
4021 		return (NULL);
4022 	}
4023 
4024 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4025 	if (ill != NULL) {
4026 		/*
4027 		 * The block comment at the start of ipif_down
4028 		 * explains the use of the macros used below
4029 		 */
4030 		GRAB_CONN_LOCK(q);
4031 		mutex_enter(&ill->ill_lock);
4032 		if (ILL_CAN_LOOKUP(ill)) {
4033 			ill_refhold_locked(ill);
4034 			mutex_exit(&ill->ill_lock);
4035 			RELEASE_CONN_LOCK(q);
4036 			return (ill);
4037 		} else if (ILL_CAN_WAIT(ill, q)) {
4038 			ipsq = ill->ill_phyint->phyint_ipsq;
4039 			mutex_enter(&ipsq->ipsq_lock);
4040 			mutex_exit(&ill->ill_lock);
4041 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4042 			mutex_exit(&ipsq->ipsq_lock);
4043 			RELEASE_CONN_LOCK(q);
4044 			if (error != NULL)
4045 				*error = EINPROGRESS;
4046 			return (NULL);
4047 		}
4048 		mutex_exit(&ill->ill_lock);
4049 		RELEASE_CONN_LOCK(q);
4050 	}
4051 	if (error != NULL)
4052 		*error = ENXIO;
4053 	return (NULL);
4054 }
4055 
4056 /*
4057  * comparison function for use with avl.
4058  */
4059 static int
4060 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4061 {
4062 	uint_t ppa;
4063 	uint_t ill_ppa;
4064 
4065 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4066 
4067 	ppa = *((uint_t *)ppa_ptr);
4068 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4069 	/*
4070 	 * We want the ill with the lowest ppa to be on the
4071 	 * top.
4072 	 */
4073 	if (ill_ppa < ppa)
4074 		return (1);
4075 	if (ill_ppa > ppa)
4076 		return (-1);
4077 	return (0);
4078 }
4079 
4080 /*
4081  * remove an interface type from the global list.
4082  */
4083 static void
4084 ill_delete_interface_type(ill_if_t *interface)
4085 {
4086 	ASSERT(interface != NULL);
4087 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4088 
4089 	avl_destroy(&interface->illif_avl_by_ppa);
4090 	if (interface->illif_ppa_arena != NULL)
4091 		vmem_destroy(interface->illif_ppa_arena);
4092 
4093 	remque(interface);
4094 
4095 	mi_free(interface);
4096 }
4097 
4098 /*
4099  * remove ill from the global list.
4100  */
4101 static void
4102 ill_glist_delete(ill_t *ill)
4103 {
4104 	ip_stack_t	*ipst;
4105 
4106 	if (ill == NULL)
4107 		return;
4108 	ipst = ill->ill_ipst;
4109 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4110 
4111 	/*
4112 	 * If the ill was never inserted into the AVL tree
4113 	 * we skip the if branch.
4114 	 */
4115 	if (ill->ill_ifptr != NULL) {
4116 		/*
4117 		 * remove from AVL tree and free ppa number
4118 		 */
4119 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4120 
4121 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4122 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4123 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4124 		}
4125 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4126 			ill_delete_interface_type(ill->ill_ifptr);
4127 		}
4128 
4129 		/*
4130 		 * Indicate ill is no longer in the list.
4131 		 */
4132 		ill->ill_ifptr = NULL;
4133 		ill->ill_name_length = 0;
4134 		ill->ill_name[0] = '\0';
4135 		ill->ill_ppa = UINT_MAX;
4136 	}
4137 
4138 	/* Generate one last event for this ill. */
4139 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
4140 	    ill->ill_name_length);
4141 
4142 	ill_phyint_free(ill);
4143 	rw_exit(&ipst->ips_ill_g_lock);
4144 }
4145 
4146 /*
4147  * allocate a ppa, if the number of plumbed interfaces of this type are
4148  * less than ill_no_arena do a linear search to find a unused ppa.
4149  * When the number goes beyond ill_no_arena switch to using an arena.
4150  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4151  * is the return value for an error condition, so allocation starts at one
4152  * and is decremented by one.
4153  */
4154 static int
4155 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4156 {
4157 	ill_t *tmp_ill;
4158 	uint_t start, end;
4159 	int ppa;
4160 
4161 	if (ifp->illif_ppa_arena == NULL &&
4162 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4163 		/*
4164 		 * Create an arena.
4165 		 */
4166 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4167 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4168 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4169 			/* allocate what has already been assigned */
4170 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4171 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4172 		    tmp_ill, AVL_AFTER)) {
4173 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4174 			    1,		/* size */
4175 			    1,		/* align/quantum */
4176 			    0,		/* phase */
4177 			    0,		/* nocross */
4178 			    /* minaddr */
4179 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4180 			    /* maxaddr */
4181 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4182 			    VM_NOSLEEP|VM_FIRSTFIT);
4183 			if (ppa == 0) {
4184 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4185 				    " failed while switching"));
4186 				vmem_destroy(ifp->illif_ppa_arena);
4187 				ifp->illif_ppa_arena = NULL;
4188 				break;
4189 			}
4190 		}
4191 	}
4192 
4193 	if (ifp->illif_ppa_arena != NULL) {
4194 		if (ill->ill_ppa == UINT_MAX) {
4195 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4196 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4197 			if (ppa == 0)
4198 				return (EAGAIN);
4199 			ill->ill_ppa = --ppa;
4200 		} else {
4201 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4202 			    1, 		/* size */
4203 			    1, 		/* align/quantum */
4204 			    0, 		/* phase */
4205 			    0, 		/* nocross */
4206 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4207 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4208 			    VM_NOSLEEP|VM_FIRSTFIT);
4209 			/*
4210 			 * Most likely the allocation failed because
4211 			 * the requested ppa was in use.
4212 			 */
4213 			if (ppa == 0)
4214 				return (EEXIST);
4215 		}
4216 		return (0);
4217 	}
4218 
4219 	/*
4220 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4221 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4222 	 */
4223 	if (ill->ill_ppa == UINT_MAX) {
4224 		end = UINT_MAX - 1;
4225 		start = 0;
4226 	} else {
4227 		end = start = ill->ill_ppa;
4228 	}
4229 
4230 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4231 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4232 		if (start++ >= end) {
4233 			if (ill->ill_ppa == UINT_MAX)
4234 				return (EAGAIN);
4235 			else
4236 				return (EEXIST);
4237 		}
4238 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4239 	}
4240 	ill->ill_ppa = start;
4241 	return (0);
4242 }
4243 
4244 /*
4245  * Insert ill into the list of configured ill's. Once this function completes,
4246  * the ill is globally visible and is available through lookups. More precisely
4247  * this happens after the caller drops the ill_g_lock.
4248  */
4249 static int
4250 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4251 {
4252 	ill_if_t *ill_interface;
4253 	avl_index_t where = 0;
4254 	int error;
4255 	int name_length;
4256 	int index;
4257 	boolean_t check_length = B_FALSE;
4258 	ip_stack_t	*ipst = ill->ill_ipst;
4259 
4260 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4261 
4262 	name_length = mi_strlen(name) + 1;
4263 
4264 	if (isv6)
4265 		index = IP_V6_G_HEAD;
4266 	else
4267 		index = IP_V4_G_HEAD;
4268 
4269 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4270 	/*
4271 	 * Search for interface type based on name
4272 	 */
4273 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4274 		if ((ill_interface->illif_name_len == name_length) &&
4275 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4276 			break;
4277 		}
4278 		ill_interface = ill_interface->illif_next;
4279 	}
4280 
4281 	/*
4282 	 * Interface type not found, create one.
4283 	 */
4284 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4285 
4286 		ill_g_head_t ghead;
4287 
4288 		/*
4289 		 * allocate ill_if_t structure
4290 		 */
4291 
4292 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4293 		if (ill_interface == NULL) {
4294 			return (ENOMEM);
4295 		}
4296 
4297 
4298 
4299 		(void) strcpy(ill_interface->illif_name, name);
4300 		ill_interface->illif_name_len = name_length;
4301 
4302 		avl_create(&ill_interface->illif_avl_by_ppa,
4303 		    ill_compare_ppa, sizeof (ill_t),
4304 		    offsetof(struct ill_s, ill_avl_byppa));
4305 
4306 		/*
4307 		 * link the structure in the back to maintain order
4308 		 * of configuration for ifconfig output.
4309 		 */
4310 		ghead = ipst->ips_ill_g_heads[index];
4311 		insque(ill_interface, ghead.ill_g_list_tail);
4312 
4313 	}
4314 
4315 	if (ill->ill_ppa == UINT_MAX)
4316 		check_length = B_TRUE;
4317 
4318 	error = ill_alloc_ppa(ill_interface, ill);
4319 	if (error != 0) {
4320 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4321 			ill_delete_interface_type(ill->ill_ifptr);
4322 		return (error);
4323 	}
4324 
4325 	/*
4326 	 * When the ppa is choosen by the system, check that there is
4327 	 * enough space to insert ppa. if a specific ppa was passed in this
4328 	 * check is not required as the interface name passed in will have
4329 	 * the right ppa in it.
4330 	 */
4331 	if (check_length) {
4332 		/*
4333 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4334 		 */
4335 		char buf[sizeof (uint_t) * 3];
4336 
4337 		/*
4338 		 * convert ppa to string to calculate the amount of space
4339 		 * required for it in the name.
4340 		 */
4341 		numtos(ill->ill_ppa, buf);
4342 
4343 		/* Do we have enough space to insert ppa ? */
4344 
4345 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4346 			/* Free ppa and interface type struct */
4347 			if (ill_interface->illif_ppa_arena != NULL) {
4348 				vmem_free(ill_interface->illif_ppa_arena,
4349 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4350 			}
4351 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4352 				ill_delete_interface_type(ill->ill_ifptr);
4353 
4354 			return (EINVAL);
4355 		}
4356 	}
4357 
4358 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4359 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4360 
4361 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4362 	    &where);
4363 	ill->ill_ifptr = ill_interface;
4364 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4365 
4366 	ill_phyint_reinit(ill);
4367 	return (0);
4368 }
4369 
4370 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4371 static boolean_t
4372 ipsq_init(ill_t *ill)
4373 {
4374 	ipsq_t  *ipsq;
4375 
4376 	/* Init the ipsq and impicitly enter as writer */
4377 	ill->ill_phyint->phyint_ipsq =
4378 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4379 	if (ill->ill_phyint->phyint_ipsq == NULL)
4380 		return (B_FALSE);
4381 	ipsq = ill->ill_phyint->phyint_ipsq;
4382 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4383 	ill->ill_phyint->phyint_ipsq_next = NULL;
4384 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4385 	ipsq->ipsq_refs = 1;
4386 	ipsq->ipsq_writer = curthread;
4387 	ipsq->ipsq_reentry_cnt = 1;
4388 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4389 #ifdef DEBUG
4390 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack,
4391 	    IPSQ_STACK_DEPTH);
4392 #endif
4393 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4394 	return (B_TRUE);
4395 }
4396 
4397 /*
4398  * ill_init is called by ip_open when a device control stream is opened.
4399  * It does a few initializations, and shoots a DL_INFO_REQ message down
4400  * to the driver.  The response is later picked up in ip_rput_dlpi and
4401  * used to set up default mechanisms for talking to the driver.  (Always
4402  * called as writer.)
4403  *
4404  * If this function returns error, ip_open will call ip_close which in
4405  * turn will call ill_delete to clean up any memory allocated here that
4406  * is not yet freed.
4407  */
4408 int
4409 ill_init(queue_t *q, ill_t *ill)
4410 {
4411 	int	count;
4412 	dl_info_req_t	*dlir;
4413 	mblk_t	*info_mp;
4414 	uchar_t *frag_ptr;
4415 
4416 	/*
4417 	 * The ill is initialized to zero by mi_alloc*(). In addition
4418 	 * some fields already contain valid values, initialized in
4419 	 * ip_open(), before we reach here.
4420 	 */
4421 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4422 
4423 	ill->ill_rq = q;
4424 	ill->ill_wq = WR(q);
4425 
4426 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4427 	    BPRI_HI);
4428 	if (info_mp == NULL)
4429 		return (ENOMEM);
4430 
4431 	/*
4432 	 * Allocate sufficient space to contain our fragment hash table and
4433 	 * the device name.
4434 	 */
4435 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4436 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4437 	if (frag_ptr == NULL) {
4438 		freemsg(info_mp);
4439 		return (ENOMEM);
4440 	}
4441 	ill->ill_frag_ptr = frag_ptr;
4442 	ill->ill_frag_free_num_pkts = 0;
4443 	ill->ill_last_frag_clean_time = 0;
4444 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4445 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4446 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4447 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4448 		    NULL, MUTEX_DEFAULT, NULL);
4449 	}
4450 
4451 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4452 	if (ill->ill_phyint == NULL) {
4453 		freemsg(info_mp);
4454 		mi_free(frag_ptr);
4455 		return (ENOMEM);
4456 	}
4457 
4458 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4459 	/*
4460 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4461 	 * at this point because of the following reason. If we can't
4462 	 * enter the ipsq at some point and cv_wait, the writer that
4463 	 * wakes us up tries to locate us using the list of all phyints
4464 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4465 	 * If we don't set it now, we risk a missed wakeup.
4466 	 */
4467 	ill->ill_phyint->phyint_illv4 = ill;
4468 	ill->ill_ppa = UINT_MAX;
4469 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4470 
4471 	if (!ipsq_init(ill)) {
4472 		freemsg(info_mp);
4473 		mi_free(frag_ptr);
4474 		mi_free(ill->ill_phyint);
4475 		return (ENOMEM);
4476 	}
4477 
4478 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4479 
4480 	/* Frag queue limit stuff */
4481 	ill->ill_frag_count = 0;
4482 	ill->ill_ipf_gen = 0;
4483 
4484 	ill->ill_global_timer = INFINITY;
4485 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4486 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4487 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4488 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4489 
4490 	/*
4491 	 * Initialize IPv6 configuration variables.  The IP module is always
4492 	 * opened as an IPv4 module.  Instead tracking down the cases where
4493 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4494 	 * here for convenience, this has no effect until the ill is set to do
4495 	 * IPv6.
4496 	 */
4497 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4498 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4499 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4500 	ill->ill_max_buf = ND_MAX_Q;
4501 	ill->ill_refcnt = 0;
4502 
4503 	/* Send down the Info Request to the driver. */
4504 	info_mp->b_datap->db_type = M_PCPROTO;
4505 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4506 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4507 	dlir->dl_primitive = DL_INFO_REQ;
4508 
4509 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4510 
4511 	qprocson(q);
4512 	ill_dlpi_send(ill, info_mp);
4513 
4514 	return (0);
4515 }
4516 
4517 /*
4518  * ill_dls_info
4519  * creates datalink socket info from the device.
4520  */
4521 int
4522 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4523 {
4524 	size_t	len;
4525 	ill_t	*ill = ipif->ipif_ill;
4526 
4527 	sdl->sdl_family = AF_LINK;
4528 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4529 	sdl->sdl_type = ill->ill_type;
4530 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4531 	len = strlen(sdl->sdl_data);
4532 	ASSERT(len < 256);
4533 	sdl->sdl_nlen = (uchar_t)len;
4534 	sdl->sdl_alen = ill->ill_phys_addr_length;
4535 	sdl->sdl_slen = 0;
4536 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4537 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4538 
4539 	return (sizeof (struct sockaddr_dl));
4540 }
4541 
4542 /*
4543  * ill_xarp_info
4544  * creates xarp info from the device.
4545  */
4546 static int
4547 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4548 {
4549 	sdl->sdl_family = AF_LINK;
4550 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4551 	sdl->sdl_type = ill->ill_type;
4552 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4553 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4554 	sdl->sdl_alen = ill->ill_phys_addr_length;
4555 	sdl->sdl_slen = 0;
4556 	return (sdl->sdl_nlen);
4557 }
4558 
4559 static int
4560 loopback_kstat_update(kstat_t *ksp, int rw)
4561 {
4562 	kstat_named_t *kn;
4563 	netstackid_t	stackid;
4564 	netstack_t	*ns;
4565 	ip_stack_t	*ipst;
4566 
4567 	if (ksp == NULL || ksp->ks_data == NULL)
4568 		return (EIO);
4569 
4570 	if (rw == KSTAT_WRITE)
4571 		return (EACCES);
4572 
4573 	kn = KSTAT_NAMED_PTR(ksp);
4574 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4575 
4576 	ns = netstack_find_by_stackid(stackid);
4577 	if (ns == NULL)
4578 		return (-1);
4579 
4580 	ipst = ns->netstack_ip;
4581 	if (ipst == NULL) {
4582 		netstack_rele(ns);
4583 		return (-1);
4584 	}
4585 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4586 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4587 	netstack_rele(ns);
4588 	return (0);
4589 }
4590 
4591 /*
4592  * Has ifindex been plumbed already.
4593  * Compares both phyint_ifindex and phyint_group_ifindex.
4594  */
4595 static boolean_t
4596 phyint_exists(uint_t index, ip_stack_t *ipst)
4597 {
4598 	phyint_t *phyi;
4599 
4600 	ASSERT(index != 0);
4601 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4602 	/*
4603 	 * Indexes are stored in the phyint - a common structure
4604 	 * to both IPv4 and IPv6.
4605 	 */
4606 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4607 	for (; phyi != NULL;
4608 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4609 	    phyi, AVL_AFTER)) {
4610 		if (phyi->phyint_ifindex == index ||
4611 		    phyi->phyint_group_ifindex == index)
4612 			return (B_TRUE);
4613 	}
4614 	return (B_FALSE);
4615 }
4616 
4617 /* Pick a unique ifindex */
4618 boolean_t
4619 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4620 {
4621 	uint_t starting_index;
4622 
4623 	if (!ipst->ips_ill_index_wrap) {
4624 		*indexp = ipst->ips_ill_index++;
4625 		if (ipst->ips_ill_index == 0) {
4626 			/* Reached the uint_t limit Next time wrap  */
4627 			ipst->ips_ill_index_wrap = B_TRUE;
4628 		}
4629 		return (B_TRUE);
4630 	}
4631 
4632 	/*
4633 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4634 	 * at this point and don't want to call any function that attempts
4635 	 * to get the lock again.
4636 	 */
4637 	starting_index = ipst->ips_ill_index++;
4638 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4639 		if (ipst->ips_ill_index != 0 &&
4640 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4641 			/* found unused index - use it */
4642 			*indexp = ipst->ips_ill_index;
4643 			return (B_TRUE);
4644 		}
4645 	}
4646 
4647 	/*
4648 	 * all interface indicies are inuse.
4649 	 */
4650 	return (B_FALSE);
4651 }
4652 
4653 /*
4654  * Assign a unique interface index for the phyint.
4655  */
4656 static boolean_t
4657 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4658 {
4659 	ASSERT(phyi->phyint_ifindex == 0);
4660 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4661 }
4662 
4663 /*
4664  * Return a pointer to the ill which matches the supplied name.  Note that
4665  * the ill name length includes the null termination character.  (May be
4666  * called as writer.)
4667  * If do_alloc and the interface is "lo0" it will be automatically created.
4668  * Cannot bump up reference on condemned ills. So dup detect can't be done
4669  * using this func.
4670  */
4671 ill_t *
4672 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4673     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4674     ip_stack_t *ipst)
4675 {
4676 	ill_t	*ill;
4677 	ipif_t	*ipif;
4678 	kstat_named_t	*kn;
4679 	boolean_t isloopback;
4680 	ipsq_t *old_ipsq;
4681 	in6_addr_t ov6addr;
4682 
4683 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4684 
4685 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4686 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4687 	rw_exit(&ipst->ips_ill_g_lock);
4688 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4689 		return (ill);
4690 
4691 	/*
4692 	 * Couldn't find it.  Does this happen to be a lookup for the
4693 	 * loopback device and are we allowed to allocate it?
4694 	 */
4695 	if (!isloopback || !do_alloc)
4696 		return (NULL);
4697 
4698 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4699 
4700 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4701 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4702 		rw_exit(&ipst->ips_ill_g_lock);
4703 		return (ill);
4704 	}
4705 
4706 	/* Create the loopback device on demand */
4707 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4708 	    sizeof (ipif_loopback_name), BPRI_MED));
4709 	if (ill == NULL)
4710 		goto done;
4711 
4712 	*ill = ill_null;
4713 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4714 	ill->ill_ipst = ipst;
4715 	netstack_hold(ipst->ips_netstack);
4716 	/*
4717 	 * For exclusive stacks we set the zoneid to zero
4718 	 * to make IP operate as if in the global zone.
4719 	 */
4720 	ill->ill_zoneid = GLOBAL_ZONEID;
4721 
4722 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4723 	if (ill->ill_phyint == NULL)
4724 		goto done;
4725 
4726 	if (isv6)
4727 		ill->ill_phyint->phyint_illv6 = ill;
4728 	else
4729 		ill->ill_phyint->phyint_illv4 = ill;
4730 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4731 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4732 	/* Add room for tcp+ip headers */
4733 	if (isv6) {
4734 		ill->ill_isv6 = B_TRUE;
4735 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4736 	} else {
4737 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4738 	}
4739 	if (!ill_allocate_mibs(ill))
4740 		goto done;
4741 	ill->ill_max_mtu = ill->ill_max_frag;
4742 	/*
4743 	 * ipif_loopback_name can't be pointed at directly because its used
4744 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4745 	 * from the glist, ill_glist_delete() sets the first character of
4746 	 * ill_name to '\0'.
4747 	 */
4748 	ill->ill_name = (char *)ill + sizeof (*ill);
4749 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4750 	ill->ill_name_length = sizeof (ipif_loopback_name);
4751 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4752 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
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 	/* No resolver here. */
4761 	ill->ill_net_type = IRE_LOOPBACK;
4762 
4763 	/* Initialize the ipsq */
4764 	if (!ipsq_init(ill))
4765 		goto done;
4766 
4767 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
4768 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
4769 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
4770 #ifdef DEBUG
4771 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
4772 #endif
4773 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
4774 	if (ipif == NULL)
4775 		goto done;
4776 
4777 	ill->ill_flags = ILLF_MULTICAST;
4778 
4779 	ov6addr = ipif->ipif_v6lcl_addr;
4780 	/* Set up default loopback address and mask. */
4781 	if (!isv6) {
4782 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4783 
4784 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4785 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4786 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4787 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4788 		    ipif->ipif_v6subnet);
4789 		ill->ill_flags |= ILLF_IPV4;
4790 	} else {
4791 		ipif->ipif_v6lcl_addr = ipv6_loopback;
4792 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4793 		ipif->ipif_v6net_mask = ipv6_all_ones;
4794 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4795 		    ipif->ipif_v6subnet);
4796 		ill->ill_flags |= ILLF_IPV6;
4797 	}
4798 
4799 	/*
4800 	 * Chain us in at the end of the ill list. hold the ill
4801 	 * before we make it globally visible. 1 for the lookup.
4802 	 */
4803 	ill->ill_refcnt = 0;
4804 	ill_refhold(ill);
4805 
4806 	ill->ill_frag_count = 0;
4807 	ill->ill_frag_free_num_pkts = 0;
4808 	ill->ill_last_frag_clean_time = 0;
4809 
4810 	old_ipsq = ill->ill_phyint->phyint_ipsq;
4811 
4812 	if (ill_glist_insert(ill, "lo", isv6) != 0)
4813 		cmn_err(CE_PANIC, "cannot insert loopback interface");
4814 
4815 	/* Let SCTP know so that it can add this to its list */
4816 	sctp_update_ill(ill, SCTP_ILL_INSERT);
4817 
4818 	/*
4819 	 * We have already assigned ipif_v6lcl_addr above, but we need to
4820 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
4821 	 * requires to be after ill_glist_insert() since we need the
4822 	 * ill_index set. Pass on ipv6_loopback as the old address.
4823 	 */
4824 	sctp_update_ipif_addr(ipif, ov6addr);
4825 
4826 	/*
4827 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
4828 	 */
4829 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
4830 		/* Loopback ills aren't in any IPMP group */
4831 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
4832 		ipsq_delete(old_ipsq);
4833 	}
4834 
4835 	/*
4836 	 * Delay this till the ipif is allocated as ipif_allocate
4837 	 * de-references ill_phyint for getting the ifindex. We
4838 	 * can't do this before ipif_allocate because ill_phyint_reinit
4839 	 * -> phyint_assign_ifindex expects ipif to be present.
4840 	 */
4841 	mutex_enter(&ill->ill_phyint->phyint_lock);
4842 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
4843 	mutex_exit(&ill->ill_phyint->phyint_lock);
4844 
4845 	if (ipst->ips_loopback_ksp == NULL) {
4846 		/* Export loopback interface statistics */
4847 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
4848 		    ipif_loopback_name, "net",
4849 		    KSTAT_TYPE_NAMED, 2, 0,
4850 		    ipst->ips_netstack->netstack_stackid);
4851 		if (ipst->ips_loopback_ksp != NULL) {
4852 			ipst->ips_loopback_ksp->ks_update =
4853 			    loopback_kstat_update;
4854 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
4855 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
4856 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
4857 			ipst->ips_loopback_ksp->ks_private =
4858 			    (void *)(uintptr_t)ipst->ips_netstack->
4859 			    netstack_stackid;
4860 			kstat_install(ipst->ips_loopback_ksp);
4861 		}
4862 	}
4863 
4864 	if (error != NULL)
4865 		*error = 0;
4866 	*did_alloc = B_TRUE;
4867 	rw_exit(&ipst->ips_ill_g_lock);
4868 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
4869 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
4870 	return (ill);
4871 done:
4872 	if (ill != NULL) {
4873 		if (ill->ill_phyint != NULL) {
4874 			ipsq_t	*ipsq;
4875 
4876 			ipsq = ill->ill_phyint->phyint_ipsq;
4877 			if (ipsq != NULL) {
4878 				ipsq->ipsq_ipst = NULL;
4879 				kmem_free(ipsq, sizeof (ipsq_t));
4880 			}
4881 			mi_free(ill->ill_phyint);
4882 		}
4883 		ill_free_mib(ill);
4884 		if (ill->ill_ipst != NULL)
4885 			netstack_rele(ill->ill_ipst->ips_netstack);
4886 		mi_free(ill);
4887 	}
4888 	rw_exit(&ipst->ips_ill_g_lock);
4889 	if (error != NULL)
4890 		*error = ENOMEM;
4891 	return (NULL);
4892 }
4893 
4894 /*
4895  * For IPP calls - use the ip_stack_t for global stack.
4896  */
4897 ill_t *
4898 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
4899     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
4900 {
4901 	ip_stack_t	*ipst;
4902 	ill_t		*ill;
4903 
4904 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
4905 	if (ipst == NULL) {
4906 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
4907 		return (NULL);
4908 	}
4909 
4910 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
4911 	netstack_rele(ipst->ips_netstack);
4912 	return (ill);
4913 }
4914 
4915 /*
4916  * Return a pointer to the ill which matches the index and IP version type.
4917  */
4918 ill_t *
4919 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
4920     ipsq_func_t func, int *err, ip_stack_t *ipst)
4921 {
4922 	ill_t	*ill;
4923 	ipsq_t  *ipsq;
4924 	phyint_t *phyi;
4925 
4926 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
4927 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
4928 
4929 	if (err != NULL)
4930 		*err = 0;
4931 
4932 	/*
4933 	 * Indexes are stored in the phyint - a common structure
4934 	 * to both IPv4 and IPv6.
4935 	 */
4936 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4937 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4938 	    (void *) &index, NULL);
4939 	if (phyi != NULL) {
4940 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
4941 		if (ill != NULL) {
4942 			/*
4943 			 * The block comment at the start of ipif_down
4944 			 * explains the use of the macros used below
4945 			 */
4946 			GRAB_CONN_LOCK(q);
4947 			mutex_enter(&ill->ill_lock);
4948 			if (ILL_CAN_LOOKUP(ill)) {
4949 				ill_refhold_locked(ill);
4950 				mutex_exit(&ill->ill_lock);
4951 				RELEASE_CONN_LOCK(q);
4952 				rw_exit(&ipst->ips_ill_g_lock);
4953 				return (ill);
4954 			} else if (ILL_CAN_WAIT(ill, q)) {
4955 				ipsq = ill->ill_phyint->phyint_ipsq;
4956 				mutex_enter(&ipsq->ipsq_lock);
4957 				rw_exit(&ipst->ips_ill_g_lock);
4958 				mutex_exit(&ill->ill_lock);
4959 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4960 				mutex_exit(&ipsq->ipsq_lock);
4961 				RELEASE_CONN_LOCK(q);
4962 				if (err != NULL)
4963 					*err = EINPROGRESS;
4964 				return (NULL);
4965 			}
4966 			RELEASE_CONN_LOCK(q);
4967 			mutex_exit(&ill->ill_lock);
4968 		}
4969 	}
4970 	rw_exit(&ipst->ips_ill_g_lock);
4971 	if (err != NULL)
4972 		*err = ENXIO;
4973 	return (NULL);
4974 }
4975 
4976 /*
4977  * Return the ifindex next in sequence after the passed in ifindex.
4978  * If there is no next ifindex for the given protocol, return 0.
4979  */
4980 uint_t
4981 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
4982 {
4983 	phyint_t *phyi;
4984 	phyint_t *phyi_initial;
4985 	uint_t   ifindex;
4986 
4987 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4988 
4989 	if (index == 0) {
4990 		phyi = avl_first(
4991 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4992 	} else {
4993 		phyi = phyi_initial = avl_find(
4994 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4995 		    (void *) &index, NULL);
4996 	}
4997 
4998 	for (; phyi != NULL;
4999 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5000 	    phyi, AVL_AFTER)) {
5001 		/*
5002 		 * If we're not returning the first interface in the tree
5003 		 * and we still haven't moved past the phyint_t that
5004 		 * corresponds to index, avl_walk needs to be called again
5005 		 */
5006 		if (!((index != 0) && (phyi == phyi_initial))) {
5007 			if (isv6) {
5008 				if ((phyi->phyint_illv6) &&
5009 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5010 				    (phyi->phyint_illv6->ill_isv6 == 1))
5011 					break;
5012 			} else {
5013 				if ((phyi->phyint_illv4) &&
5014 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5015 				    (phyi->phyint_illv4->ill_isv6 == 0))
5016 					break;
5017 			}
5018 		}
5019 	}
5020 
5021 	rw_exit(&ipst->ips_ill_g_lock);
5022 
5023 	if (phyi != NULL)
5024 		ifindex = phyi->phyint_ifindex;
5025 	else
5026 		ifindex = 0;
5027 
5028 	return (ifindex);
5029 }
5030 
5031 /*
5032  * Return the ifindex for the named interface.
5033  * If there is no next ifindex for the interface, return 0.
5034  */
5035 uint_t
5036 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5037 {
5038 	phyint_t	*phyi;
5039 	avl_index_t	where = 0;
5040 	uint_t		ifindex;
5041 
5042 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5043 
5044 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5045 	    name, &where)) == NULL) {
5046 		rw_exit(&ipst->ips_ill_g_lock);
5047 		return (0);
5048 	}
5049 
5050 	ifindex = phyi->phyint_ifindex;
5051 
5052 	rw_exit(&ipst->ips_ill_g_lock);
5053 
5054 	return (ifindex);
5055 }
5056 
5057 /*
5058  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5059  * that gives a running thread a reference to the ill. This reference must be
5060  * released by the thread when it is done accessing the ill and related
5061  * objects. ill_refcnt can not be used to account for static references
5062  * such as other structures pointing to an ill. Callers must generally
5063  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5064  * or be sure that the ill is not being deleted or changing state before
5065  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5066  * ill won't change any of its critical state such as address, netmask etc.
5067  */
5068 void
5069 ill_refhold(ill_t *ill)
5070 {
5071 	mutex_enter(&ill->ill_lock);
5072 	ill->ill_refcnt++;
5073 	ILL_TRACE_REF(ill);
5074 	mutex_exit(&ill->ill_lock);
5075 }
5076 
5077 void
5078 ill_refhold_locked(ill_t *ill)
5079 {
5080 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5081 	ill->ill_refcnt++;
5082 	ILL_TRACE_REF(ill);
5083 }
5084 
5085 int
5086 ill_check_and_refhold(ill_t *ill)
5087 {
5088 	mutex_enter(&ill->ill_lock);
5089 	if (ILL_CAN_LOOKUP(ill)) {
5090 		ill_refhold_locked(ill);
5091 		mutex_exit(&ill->ill_lock);
5092 		return (0);
5093 	}
5094 	mutex_exit(&ill->ill_lock);
5095 	return (ILL_LOOKUP_FAILED);
5096 }
5097 
5098 /*
5099  * Must not be called while holding any locks. Otherwise if this is
5100  * the last reference to be released, there is a chance of recursive mutex
5101  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5102  * to restart an ioctl.
5103  */
5104 void
5105 ill_refrele(ill_t *ill)
5106 {
5107 	mutex_enter(&ill->ill_lock);
5108 	ASSERT(ill->ill_refcnt != 0);
5109 	ill->ill_refcnt--;
5110 	ILL_UNTRACE_REF(ill);
5111 	if (ill->ill_refcnt != 0) {
5112 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5113 		mutex_exit(&ill->ill_lock);
5114 		return;
5115 	}
5116 
5117 	/* Drops the ill_lock */
5118 	ipif_ill_refrele_tail(ill);
5119 }
5120 
5121 /*
5122  * Obtain a weak reference count on the ill. This reference ensures the
5123  * ill won't be freed, but the ill may change any of its critical state
5124  * such as netmask, address etc. Returns an error if the ill has started
5125  * closing.
5126  */
5127 boolean_t
5128 ill_waiter_inc(ill_t *ill)
5129 {
5130 	mutex_enter(&ill->ill_lock);
5131 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5132 		mutex_exit(&ill->ill_lock);
5133 		return (B_FALSE);
5134 	}
5135 	ill->ill_waiters++;
5136 	mutex_exit(&ill->ill_lock);
5137 	return (B_TRUE);
5138 }
5139 
5140 void
5141 ill_waiter_dcr(ill_t *ill)
5142 {
5143 	mutex_enter(&ill->ill_lock);
5144 	ill->ill_waiters--;
5145 	if (ill->ill_waiters == 0)
5146 		cv_broadcast(&ill->ill_cv);
5147 	mutex_exit(&ill->ill_lock);
5148 }
5149 
5150 /*
5151  * Named Dispatch routine to produce a formatted report on all ILLs.
5152  * This report is accessed by using the ndd utility to "get" ND variable
5153  * "ip_ill_status".
5154  */
5155 /* ARGSUSED */
5156 int
5157 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5158 {
5159 	ill_t		*ill;
5160 	ill_walk_context_t ctx;
5161 	ip_stack_t	*ipst;
5162 
5163 	ipst = CONNQ_TO_IPST(q);
5164 
5165 	(void) mi_mpprintf(mp,
5166 	    "ILL      " MI_COL_HDRPAD_STR
5167 	/*   01234567[89ABCDEF] */
5168 	    "rq       " MI_COL_HDRPAD_STR
5169 	/*   01234567[89ABCDEF] */
5170 	    "wq       " MI_COL_HDRPAD_STR
5171 	/*   01234567[89ABCDEF] */
5172 	    "upcnt mxfrg err name");
5173 	/*   12345 12345 123 xxxxxxxx  */
5174 
5175 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5176 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5177 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5178 		(void) mi_mpprintf(mp,
5179 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5180 		    "%05u %05u %03d %s",
5181 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5182 		    ill->ill_ipif_up_count,
5183 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5184 	}
5185 	rw_exit(&ipst->ips_ill_g_lock);
5186 
5187 	return (0);
5188 }
5189 
5190 /*
5191  * Named Dispatch routine to produce a formatted report on all IPIFs.
5192  * This report is accessed by using the ndd utility to "get" ND variable
5193  * "ip_ipif_status".
5194  */
5195 /* ARGSUSED */
5196 int
5197 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5198 {
5199 	char	buf1[INET6_ADDRSTRLEN];
5200 	char	buf2[INET6_ADDRSTRLEN];
5201 	char	buf3[INET6_ADDRSTRLEN];
5202 	char	buf4[INET6_ADDRSTRLEN];
5203 	char	buf5[INET6_ADDRSTRLEN];
5204 	char	buf6[INET6_ADDRSTRLEN];
5205 	char	buf[LIFNAMSIZ];
5206 	ill_t	*ill;
5207 	ipif_t	*ipif;
5208 	nv_t	*nvp;
5209 	uint64_t flags;
5210 	zoneid_t zoneid;
5211 	ill_walk_context_t ctx;
5212 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5213 
5214 	(void) mi_mpprintf(mp,
5215 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5216 	    "\tlocal address\n"
5217 	    "\tsrc address\n"
5218 	    "\tsubnet\n"
5219 	    "\tmask\n"
5220 	    "\tbroadcast\n"
5221 	    "\tp-p-dst");
5222 
5223 	ASSERT(q->q_next == NULL);
5224 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5225 
5226 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5227 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5228 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5229 		for (ipif = ill->ill_ipif; ipif != NULL;
5230 		    ipif = ipif->ipif_next) {
5231 			if (zoneid != GLOBAL_ZONEID &&
5232 			    zoneid != ipif->ipif_zoneid &&
5233 			    ipif->ipif_zoneid != ALL_ZONES)
5234 				continue;
5235 
5236 			ipif_get_name(ipif, buf, sizeof (buf));
5237 			(void) mi_mpprintf(mp,
5238 			    MI_COL_PTRFMT_STR
5239 			    "%04u %05u %u/%u/%u %s %d",
5240 			    (void *)ipif,
5241 			    ipif->ipif_metric, ipif->ipif_mtu,
5242 			    ipif->ipif_ib_pkt_count,
5243 			    ipif->ipif_ob_pkt_count,
5244 			    ipif->ipif_fo_pkt_count,
5245 			    buf,
5246 			    ipif->ipif_zoneid);
5247 
5248 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5249 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5250 
5251 		/* Tack on text strings for any flags. */
5252 		nvp = ipif_nv_tbl;
5253 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5254 			if (nvp->nv_value & flags)
5255 				(void) mi_mpprintf_nr(mp, " %s",
5256 				    nvp->nv_name);
5257 		}
5258 		(void) mi_mpprintf(mp,
5259 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5260 		    inet_ntop(AF_INET6,
5261 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5262 		    inet_ntop(AF_INET6,
5263 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5264 		    inet_ntop(AF_INET6,
5265 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5266 		    inet_ntop(AF_INET6,
5267 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5268 		    inet_ntop(AF_INET6,
5269 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5270 		    inet_ntop(AF_INET6,
5271 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5272 		}
5273 	}
5274 	rw_exit(&ipst->ips_ill_g_lock);
5275 	return (0);
5276 }
5277 
5278 /*
5279  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5280  * driver.  We construct best guess defaults for lower level information that
5281  * we need.  If an interface is brought up without injection of any overriding
5282  * information from outside, we have to be ready to go with these defaults.
5283  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5284  * we primarely want the dl_provider_style.
5285  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5286  * at which point we assume the other part of the information is valid.
5287  */
5288 void
5289 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5290 {
5291 	uchar_t		*brdcst_addr;
5292 	uint_t		brdcst_addr_length, phys_addr_length;
5293 	t_scalar_t	sap_length;
5294 	dl_info_ack_t	*dlia;
5295 	ip_m_t		*ipm;
5296 	dl_qos_cl_sel1_t *sel1;
5297 
5298 	ASSERT(IAM_WRITER_ILL(ill));
5299 
5300 	/*
5301 	 * Till the ill is fully up ILL_CHANGING will be set and
5302 	 * the ill is not globally visible. So no need for a lock.
5303 	 */
5304 	dlia = (dl_info_ack_t *)mp->b_rptr;
5305 	ill->ill_mactype = dlia->dl_mac_type;
5306 
5307 	ipm = ip_m_lookup(dlia->dl_mac_type);
5308 	if (ipm == NULL) {
5309 		ipm = ip_m_lookup(DL_OTHER);
5310 		ASSERT(ipm != NULL);
5311 	}
5312 	ill->ill_media = ipm;
5313 
5314 	/*
5315 	 * When the new DLPI stuff is ready we'll pull lengths
5316 	 * from dlia.
5317 	 */
5318 	if (dlia->dl_version == DL_VERSION_2) {
5319 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5320 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5321 		    brdcst_addr_length);
5322 		if (brdcst_addr == NULL) {
5323 			brdcst_addr_length = 0;
5324 		}
5325 		sap_length = dlia->dl_sap_length;
5326 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5327 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5328 		    brdcst_addr_length, sap_length, phys_addr_length));
5329 	} else {
5330 		brdcst_addr_length = 6;
5331 		brdcst_addr = ip_six_byte_all_ones;
5332 		sap_length = -2;
5333 		phys_addr_length = brdcst_addr_length;
5334 	}
5335 
5336 	ill->ill_bcast_addr_length = brdcst_addr_length;
5337 	ill->ill_phys_addr_length = phys_addr_length;
5338 	ill->ill_sap_length = sap_length;
5339 	ill->ill_max_frag = dlia->dl_max_sdu;
5340 	ill->ill_max_mtu = ill->ill_max_frag;
5341 
5342 	ill->ill_type = ipm->ip_m_type;
5343 
5344 	if (!ill->ill_dlpi_style_set) {
5345 		if (dlia->dl_provider_style == DL_STYLE2)
5346 			ill->ill_needs_attach = 1;
5347 
5348 		/*
5349 		 * Allocate the first ipif on this ill. We don't delay it
5350 		 * further as ioctl handling assumes atleast one ipif to
5351 		 * be present.
5352 		 *
5353 		 * At this point we don't know whether the ill is v4 or v6.
5354 		 * We will know this whan the SIOCSLIFNAME happens and
5355 		 * the correct value for ill_isv6 will be assigned in
5356 		 * ipif_set_values(). We need to hold the ill lock and
5357 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5358 		 * the wakeup.
5359 		 */
5360 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5361 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5362 		mutex_enter(&ill->ill_lock);
5363 		ASSERT(ill->ill_dlpi_style_set == 0);
5364 		ill->ill_dlpi_style_set = 1;
5365 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5366 		cv_broadcast(&ill->ill_cv);
5367 		mutex_exit(&ill->ill_lock);
5368 		freemsg(mp);
5369 		return;
5370 	}
5371 	ASSERT(ill->ill_ipif != NULL);
5372 	/*
5373 	 * We know whether it is IPv4 or IPv6 now, as this is the
5374 	 * second DL_INFO_ACK we are recieving in response to the
5375 	 * DL_INFO_REQ sent in ipif_set_values.
5376 	 */
5377 	if (ill->ill_isv6)
5378 		ill->ill_sap = IP6_DL_SAP;
5379 	else
5380 		ill->ill_sap = IP_DL_SAP;
5381 	/*
5382 	 * Set ipif_mtu which is used to set the IRE's
5383 	 * ire_max_frag value. The driver could have sent
5384 	 * a different mtu from what it sent last time. No
5385 	 * need to call ipif_mtu_change because IREs have
5386 	 * not yet been created.
5387 	 */
5388 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5389 	/*
5390 	 * Clear all the flags that were set based on ill_bcast_addr_length
5391 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5392 	 * changed now and we need to re-evaluate.
5393 	 */
5394 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5395 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5396 
5397 	/*
5398 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5399 	 * changed now.
5400 	 */
5401 	if (ill->ill_bcast_addr_length == 0) {
5402 		if (ill->ill_resolver_mp != NULL)
5403 			freemsg(ill->ill_resolver_mp);
5404 		if (ill->ill_bcast_mp != NULL)
5405 			freemsg(ill->ill_bcast_mp);
5406 		if (ill->ill_flags & ILLF_XRESOLV)
5407 			ill->ill_net_type = IRE_IF_RESOLVER;
5408 		else
5409 			ill->ill_net_type = IRE_IF_NORESOLVER;
5410 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5411 		    ill->ill_phys_addr_length,
5412 		    ill->ill_sap,
5413 		    ill->ill_sap_length);
5414 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5415 
5416 		if (ill->ill_isv6)
5417 			/*
5418 			 * Note: xresolv interfaces will eventually need NOARP
5419 			 * set here as well, but that will require those
5420 			 * external resolvers to have some knowledge of
5421 			 * that flag and act appropriately. Not to be changed
5422 			 * at present.
5423 			 */
5424 			ill->ill_flags |= ILLF_NONUD;
5425 		else
5426 			ill->ill_flags |= ILLF_NOARP;
5427 
5428 		if (ill->ill_phys_addr_length == 0) {
5429 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5430 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5431 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5432 			} else {
5433 				/* pt-pt supports multicast. */
5434 				ill->ill_flags |= ILLF_MULTICAST;
5435 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5436 			}
5437 		}
5438 	} else {
5439 		ill->ill_net_type = IRE_IF_RESOLVER;
5440 		if (ill->ill_bcast_mp != NULL)
5441 			freemsg(ill->ill_bcast_mp);
5442 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5443 		    ill->ill_bcast_addr_length, ill->ill_sap,
5444 		    ill->ill_sap_length);
5445 		/*
5446 		 * Later detect lack of DLPI driver multicast
5447 		 * capability by catching DL_ENABMULTI errors in
5448 		 * ip_rput_dlpi.
5449 		 */
5450 		ill->ill_flags |= ILLF_MULTICAST;
5451 		if (!ill->ill_isv6)
5452 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5453 	}
5454 	/* By default an interface does not support any CoS marking */
5455 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5456 
5457 	/*
5458 	 * If we get QoS information in DL_INFO_ACK, the device supports
5459 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5460 	 */
5461 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5462 	    dlia->dl_qos_length);
5463 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5464 		ill->ill_flags |= ILLF_COS_ENABLED;
5465 	}
5466 
5467 	/* Clear any previous error indication. */
5468 	ill->ill_error = 0;
5469 	freemsg(mp);
5470 }
5471 
5472 /*
5473  * Perform various checks to verify that an address would make sense as a
5474  * local, remote, or subnet interface address.
5475  */
5476 static boolean_t
5477 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5478 {
5479 	ipaddr_t	net_mask;
5480 
5481 	/*
5482 	 * Don't allow all zeroes, or all ones, but allow
5483 	 * all ones netmask.
5484 	 */
5485 	if ((net_mask = ip_net_mask(addr)) == 0)
5486 		return (B_FALSE);
5487 	/* A given netmask overrides the "guess" netmask */
5488 	if (subnet_mask != 0)
5489 		net_mask = subnet_mask;
5490 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5491 	    (addr == (addr | ~net_mask)))) {
5492 		return (B_FALSE);
5493 	}
5494 
5495 	/*
5496 	 * Even if the netmask is all ones, we do not allow address to be
5497 	 * 255.255.255.255
5498 	 */
5499 	if (addr == INADDR_BROADCAST)
5500 		return (B_FALSE);
5501 
5502 	if (CLASSD(addr))
5503 		return (B_FALSE);
5504 
5505 	return (B_TRUE);
5506 }
5507 
5508 #define	V6_IPIF_LINKLOCAL(p)	\
5509 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5510 
5511 /*
5512  * Compare two given ipifs and check if the second one is better than
5513  * the first one using the order of preference (not taking deprecated
5514  * into acount) specified in ipif_lookup_multicast().
5515  */
5516 static boolean_t
5517 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5518 {
5519 	/* Check the least preferred first. */
5520 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5521 		/* If both ipifs are the same, use the first one. */
5522 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5523 			return (B_FALSE);
5524 		else
5525 			return (B_TRUE);
5526 	}
5527 
5528 	/* For IPv6, check for link local address. */
5529 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5530 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5531 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5532 			/* The second one is equal or less preferred. */
5533 			return (B_FALSE);
5534 		} else {
5535 			return (B_TRUE);
5536 		}
5537 	}
5538 
5539 	/* Then check for point to point interface. */
5540 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5541 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5542 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5543 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5544 			return (B_FALSE);
5545 		} else {
5546 			return (B_TRUE);
5547 		}
5548 	}
5549 
5550 	/* old_ipif is a normal interface, so no need to use the new one. */
5551 	return (B_FALSE);
5552 }
5553 
5554 /*
5555  * Find any non-virtual, not condemned, and up multicast capable interface
5556  * given an IP instance and zoneid.  Order of preference is:
5557  *
5558  * 1. normal
5559  * 1.1 normal, but deprecated
5560  * 2. point to point
5561  * 2.1 point to point, but deprecated
5562  * 3. link local
5563  * 3.1 link local, but deprecated
5564  * 4. loopback.
5565  */
5566 ipif_t *
5567 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5568 {
5569 	ill_t			*ill;
5570 	ill_walk_context_t	ctx;
5571 	ipif_t			*ipif;
5572 	ipif_t			*saved_ipif = NULL;
5573 	ipif_t			*dep_ipif = NULL;
5574 
5575 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5576 	if (isv6)
5577 		ill = ILL_START_WALK_V6(&ctx, ipst);
5578 	else
5579 		ill = ILL_START_WALK_V4(&ctx, ipst);
5580 
5581 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5582 		mutex_enter(&ill->ill_lock);
5583 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5584 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5585 			mutex_exit(&ill->ill_lock);
5586 			continue;
5587 		}
5588 		for (ipif = ill->ill_ipif; ipif != NULL;
5589 		    ipif = ipif->ipif_next) {
5590 			if (zoneid != ipif->ipif_zoneid &&
5591 			    zoneid != ALL_ZONES &&
5592 			    ipif->ipif_zoneid != ALL_ZONES) {
5593 				continue;
5594 			}
5595 			if (!(ipif->ipif_flags & IPIF_UP) ||
5596 			    !IPIF_CAN_LOOKUP(ipif)) {
5597 				continue;
5598 			}
5599 
5600 			/*
5601 			 * Found one candidate.  If it is deprecated,
5602 			 * remember it in dep_ipif.  If it is not deprecated,
5603 			 * remember it in saved_ipif.
5604 			 */
5605 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5606 				if (dep_ipif == NULL) {
5607 					dep_ipif = ipif;
5608 				} else if (ipif_comp_multi(dep_ipif, ipif,
5609 				    isv6)) {
5610 					/*
5611 					 * If the previous dep_ipif does not
5612 					 * belong to the same ill, we've done
5613 					 * a ipif_refhold() on it.  So we need
5614 					 * to release it.
5615 					 */
5616 					if (dep_ipif->ipif_ill != ill)
5617 						ipif_refrele(dep_ipif);
5618 					dep_ipif = ipif;
5619 				}
5620 				continue;
5621 			}
5622 			if (saved_ipif == NULL) {
5623 				saved_ipif = ipif;
5624 			} else {
5625 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5626 					if (saved_ipif->ipif_ill != ill)
5627 						ipif_refrele(saved_ipif);
5628 					saved_ipif = ipif;
5629 				}
5630 			}
5631 		}
5632 		/*
5633 		 * Before going to the next ill, do a ipif_refhold() on the
5634 		 * saved ones.
5635 		 */
5636 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5637 			ipif_refhold_locked(saved_ipif);
5638 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5639 			ipif_refhold_locked(dep_ipif);
5640 		mutex_exit(&ill->ill_lock);
5641 	}
5642 	rw_exit(&ipst->ips_ill_g_lock);
5643 
5644 	/*
5645 	 * If we have only the saved_ipif, return it.  But if we have both
5646 	 * saved_ipif and dep_ipif, check to see which one is better.
5647 	 */
5648 	if (saved_ipif != NULL) {
5649 		if (dep_ipif != NULL) {
5650 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5651 				ipif_refrele(saved_ipif);
5652 				return (dep_ipif);
5653 			} else {
5654 				ipif_refrele(dep_ipif);
5655 				return (saved_ipif);
5656 			}
5657 		}
5658 		return (saved_ipif);
5659 	} else {
5660 		return (dep_ipif);
5661 	}
5662 }
5663 
5664 /*
5665  * This function is called when an application does not specify an interface
5666  * to be used for multicast traffic (joining a group/sending data).  It
5667  * calls ire_lookup_multi() to look for an interface route for the
5668  * specified multicast group.  Doing this allows the administrator to add
5669  * prefix routes for multicast to indicate which interface to be used for
5670  * multicast traffic in the above scenario.  The route could be for all
5671  * multicast (224.0/4), for a single multicast group (a /32 route) or
5672  * anything in between.  If there is no such multicast route, we just find
5673  * any multicast capable interface and return it.  The returned ipif
5674  * is refhold'ed.
5675  */
5676 ipif_t *
5677 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5678 {
5679 	ire_t			*ire;
5680 	ipif_t			*ipif;
5681 
5682 	ire = ire_lookup_multi(group, zoneid, ipst);
5683 	if (ire != NULL) {
5684 		ipif = ire->ire_ipif;
5685 		ipif_refhold(ipif);
5686 		ire_refrele(ire);
5687 		return (ipif);
5688 	}
5689 
5690 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5691 }
5692 
5693 /*
5694  * Look for an ipif with the specified interface address and destination.
5695  * The destination address is used only for matching point-to-point interfaces.
5696  */
5697 ipif_t *
5698 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5699     ipsq_func_t func, int *error, ip_stack_t *ipst)
5700 {
5701 	ipif_t	*ipif;
5702 	ill_t	*ill;
5703 	ill_walk_context_t ctx;
5704 	ipsq_t	*ipsq;
5705 
5706 	if (error != NULL)
5707 		*error = 0;
5708 
5709 	/*
5710 	 * First match all the point-to-point interfaces
5711 	 * before looking at non-point-to-point interfaces.
5712 	 * This is done to avoid returning non-point-to-point
5713 	 * ipif instead of unnumbered point-to-point ipif.
5714 	 */
5715 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5716 	ill = ILL_START_WALK_V4(&ctx, ipst);
5717 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5718 		GRAB_CONN_LOCK(q);
5719 		mutex_enter(&ill->ill_lock);
5720 		for (ipif = ill->ill_ipif; ipif != NULL;
5721 		    ipif = ipif->ipif_next) {
5722 			/* Allow the ipif to be down */
5723 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5724 			    (ipif->ipif_lcl_addr == if_addr) &&
5725 			    (ipif->ipif_pp_dst_addr == dst)) {
5726 				/*
5727 				 * The block comment at the start of ipif_down
5728 				 * explains the use of the macros used below
5729 				 */
5730 				if (IPIF_CAN_LOOKUP(ipif)) {
5731 					ipif_refhold_locked(ipif);
5732 					mutex_exit(&ill->ill_lock);
5733 					RELEASE_CONN_LOCK(q);
5734 					rw_exit(&ipst->ips_ill_g_lock);
5735 					return (ipif);
5736 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5737 					ipsq = ill->ill_phyint->phyint_ipsq;
5738 					mutex_enter(&ipsq->ipsq_lock);
5739 					mutex_exit(&ill->ill_lock);
5740 					rw_exit(&ipst->ips_ill_g_lock);
5741 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5742 					    ill);
5743 					mutex_exit(&ipsq->ipsq_lock);
5744 					RELEASE_CONN_LOCK(q);
5745 					if (error != NULL)
5746 						*error = EINPROGRESS;
5747 					return (NULL);
5748 				}
5749 			}
5750 		}
5751 		mutex_exit(&ill->ill_lock);
5752 		RELEASE_CONN_LOCK(q);
5753 	}
5754 	rw_exit(&ipst->ips_ill_g_lock);
5755 
5756 	/* lookup the ipif based on interface address */
5757 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5758 	    ipst);
5759 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5760 	return (ipif);
5761 }
5762 
5763 /*
5764  * Look for an ipif with the specified address. For point-point links
5765  * we look for matches on either the destination address and the local
5766  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5767  * is set.
5768  * Matches on a specific ill if match_ill is set.
5769  */
5770 ipif_t *
5771 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5772     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5773 {
5774 	ipif_t  *ipif;
5775 	ill_t   *ill;
5776 	boolean_t ptp = B_FALSE;
5777 	ipsq_t	*ipsq;
5778 	ill_walk_context_t	ctx;
5779 
5780 	if (error != NULL)
5781 		*error = 0;
5782 
5783 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5784 	/*
5785 	 * Repeat twice, first based on local addresses and
5786 	 * next time for pointopoint.
5787 	 */
5788 repeat:
5789 	ill = ILL_START_WALK_V4(&ctx, ipst);
5790 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5791 		if (match_ill != NULL && ill != match_ill) {
5792 			continue;
5793 		}
5794 		GRAB_CONN_LOCK(q);
5795 		mutex_enter(&ill->ill_lock);
5796 		for (ipif = ill->ill_ipif; ipif != NULL;
5797 		    ipif = ipif->ipif_next) {
5798 			if (zoneid != ALL_ZONES &&
5799 			    zoneid != ipif->ipif_zoneid &&
5800 			    ipif->ipif_zoneid != ALL_ZONES)
5801 				continue;
5802 			/* Allow the ipif to be down */
5803 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5804 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5805 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5806 			    (ipif->ipif_pp_dst_addr == addr))) {
5807 				/*
5808 				 * The block comment at the start of ipif_down
5809 				 * explains the use of the macros used below
5810 				 */
5811 				if (IPIF_CAN_LOOKUP(ipif)) {
5812 					ipif_refhold_locked(ipif);
5813 					mutex_exit(&ill->ill_lock);
5814 					RELEASE_CONN_LOCK(q);
5815 					rw_exit(&ipst->ips_ill_g_lock);
5816 					return (ipif);
5817 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5818 					ipsq = ill->ill_phyint->phyint_ipsq;
5819 					mutex_enter(&ipsq->ipsq_lock);
5820 					mutex_exit(&ill->ill_lock);
5821 					rw_exit(&ipst->ips_ill_g_lock);
5822 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5823 					    ill);
5824 					mutex_exit(&ipsq->ipsq_lock);
5825 					RELEASE_CONN_LOCK(q);
5826 					if (error != NULL)
5827 						*error = EINPROGRESS;
5828 					return (NULL);
5829 				}
5830 			}
5831 		}
5832 		mutex_exit(&ill->ill_lock);
5833 		RELEASE_CONN_LOCK(q);
5834 	}
5835 
5836 	/* If we already did the ptp case, then we are done */
5837 	if (ptp) {
5838 		rw_exit(&ipst->ips_ill_g_lock);
5839 		if (error != NULL)
5840 			*error = ENXIO;
5841 		return (NULL);
5842 	}
5843 	ptp = B_TRUE;
5844 	goto repeat;
5845 }
5846 
5847 /*
5848  * Check if the address exists in the system.
5849  * We don't hold the conn_lock as we will not perform defered ipsqueue
5850  * operation.
5851  */
5852 boolean_t
5853 ip_addr_exists(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
5854 {
5855 	ipif_t  *ipif;
5856 	ill_t   *ill;
5857 	ill_walk_context_t	ctx;
5858 
5859 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5860 
5861 	ill = ILL_START_WALK_V4(&ctx, ipst);
5862 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5863 		mutex_enter(&ill->ill_lock);
5864 		for (ipif = ill->ill_ipif; ipif != NULL;
5865 		    ipif = ipif->ipif_next) {
5866 			if (zoneid != ALL_ZONES &&
5867 			    zoneid != ipif->ipif_zoneid &&
5868 			    ipif->ipif_zoneid != ALL_ZONES)
5869 				continue;
5870 			/* Allow the ipif to be down */
5871 			/*
5872 			 * XXX Different from ipif_lookup_addr(), we don't do
5873 			 * twice lookups. As from bind()'s point of view, we
5874 			 * may return once we find a match.
5875 			 */
5876 			if (((ipif->ipif_lcl_addr == addr) &&
5877 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5878 			    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5879 			    (ipif->ipif_pp_dst_addr == addr))) {
5880 				/*
5881 				 * Allow bind() to be successful even if the
5882 				 * ipif is with IPIF_CHANGING bit set.
5883 				 */
5884 				mutex_exit(&ill->ill_lock);
5885 				rw_exit(&ipst->ips_ill_g_lock);
5886 				return (B_TRUE);
5887 			}
5888 		}
5889 		mutex_exit(&ill->ill_lock);
5890 	}
5891 
5892 	rw_exit(&ipst->ips_ill_g_lock);
5893 	return (B_FALSE);
5894 }
5895 
5896 /*
5897  * Look for an ipif with the specified address. For point-point links
5898  * we look for matches on either the destination address and the local
5899  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5900  * is set.
5901  * Matches on a specific ill if match_ill is set.
5902  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
5903  */
5904 zoneid_t
5905 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
5906 {
5907 	zoneid_t zoneid;
5908 	ipif_t  *ipif;
5909 	ill_t   *ill;
5910 	boolean_t ptp = B_FALSE;
5911 	ill_walk_context_t	ctx;
5912 
5913 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5914 	/*
5915 	 * Repeat twice, first based on local addresses and
5916 	 * next time for pointopoint.
5917 	 */
5918 repeat:
5919 	ill = ILL_START_WALK_V4(&ctx, ipst);
5920 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5921 		if (match_ill != NULL && ill != match_ill) {
5922 			continue;
5923 		}
5924 		mutex_enter(&ill->ill_lock);
5925 		for (ipif = ill->ill_ipif; ipif != NULL;
5926 		    ipif = ipif->ipif_next) {
5927 			/* Allow the ipif to be down */
5928 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5929 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5930 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5931 			    (ipif->ipif_pp_dst_addr == addr)) &&
5932 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
5933 				zoneid = ipif->ipif_zoneid;
5934 				mutex_exit(&ill->ill_lock);
5935 				rw_exit(&ipst->ips_ill_g_lock);
5936 				/*
5937 				 * If ipif_zoneid was ALL_ZONES then we have
5938 				 * a trusted extensions shared IP address.
5939 				 * In that case GLOBAL_ZONEID works to send.
5940 				 */
5941 				if (zoneid == ALL_ZONES)
5942 					zoneid = GLOBAL_ZONEID;
5943 				return (zoneid);
5944 			}
5945 		}
5946 		mutex_exit(&ill->ill_lock);
5947 	}
5948 
5949 	/* If we already did the ptp case, then we are done */
5950 	if (ptp) {
5951 		rw_exit(&ipst->ips_ill_g_lock);
5952 		return (ALL_ZONES);
5953 	}
5954 	ptp = B_TRUE;
5955 	goto repeat;
5956 }
5957 
5958 /*
5959  * Look for an ipif that matches the specified remote address i.e. the
5960  * ipif that would receive the specified packet.
5961  * First look for directly connected interfaces and then do a recursive
5962  * IRE lookup and pick the first ipif corresponding to the source address in the
5963  * ire.
5964  * Returns: held ipif
5965  */
5966 ipif_t *
5967 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
5968 {
5969 	ipif_t	*ipif;
5970 	ire_t	*ire;
5971 	ip_stack_t	*ipst = ill->ill_ipst;
5972 
5973 	ASSERT(!ill->ill_isv6);
5974 
5975 	/*
5976 	 * Someone could be changing this ipif currently or change it
5977 	 * after we return this. Thus  a few packets could use the old
5978 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
5979 	 * will atomically be updated or cleaned up with the new value
5980 	 * Thus we don't need a lock to check the flags or other attrs below.
5981 	 */
5982 	mutex_enter(&ill->ill_lock);
5983 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5984 		if (!IPIF_CAN_LOOKUP(ipif))
5985 			continue;
5986 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
5987 		    ipif->ipif_zoneid != ALL_ZONES)
5988 			continue;
5989 		/* Allow the ipif to be down */
5990 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
5991 			if ((ipif->ipif_pp_dst_addr == addr) ||
5992 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
5993 			    ipif->ipif_lcl_addr == addr)) {
5994 				ipif_refhold_locked(ipif);
5995 				mutex_exit(&ill->ill_lock);
5996 				return (ipif);
5997 			}
5998 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
5999 			ipif_refhold_locked(ipif);
6000 			mutex_exit(&ill->ill_lock);
6001 			return (ipif);
6002 		}
6003 	}
6004 	mutex_exit(&ill->ill_lock);
6005 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6006 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6007 	if (ire != NULL) {
6008 		/*
6009 		 * The callers of this function wants to know the
6010 		 * interface on which they have to send the replies
6011 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6012 		 * derived from different ills, we really don't care
6013 		 * what we return here.
6014 		 */
6015 		ipif = ire->ire_ipif;
6016 		if (ipif != NULL) {
6017 			ipif_refhold(ipif);
6018 			ire_refrele(ire);
6019 			return (ipif);
6020 		}
6021 		ire_refrele(ire);
6022 	}
6023 	/* Pick the first interface */
6024 	ipif = ipif_get_next_ipif(NULL, ill);
6025 	return (ipif);
6026 }
6027 
6028 /*
6029  * This func does not prevent refcnt from increasing. But if
6030  * the caller has taken steps to that effect, then this func
6031  * can be used to determine whether the ill has become quiescent
6032  */
6033 static boolean_t
6034 ill_is_quiescent(ill_t *ill)
6035 {
6036 	ipif_t	*ipif;
6037 
6038 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6039 
6040 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6041 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6042 			return (B_FALSE);
6043 		}
6044 	}
6045 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6046 		return (B_FALSE);
6047 	}
6048 	return (B_TRUE);
6049 }
6050 
6051 boolean_t
6052 ill_is_freeable(ill_t *ill)
6053 {
6054 	ipif_t	*ipif;
6055 
6056 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6057 
6058 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6059 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6060 			return (B_FALSE);
6061 		}
6062 	}
6063 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6064 		return (B_FALSE);
6065 	}
6066 	return (B_TRUE);
6067 }
6068 
6069 /*
6070  * This func does not prevent refcnt from increasing. But if
6071  * the caller has taken steps to that effect, then this func
6072  * can be used to determine whether the ipif has become quiescent
6073  */
6074 static boolean_t
6075 ipif_is_quiescent(ipif_t *ipif)
6076 {
6077 	ill_t *ill;
6078 
6079 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6080 
6081 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6082 		return (B_FALSE);
6083 	}
6084 
6085 	ill = ipif->ipif_ill;
6086 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6087 	    ill->ill_logical_down) {
6088 		return (B_TRUE);
6089 	}
6090 
6091 	/* This is the last ipif going down or being deleted on this ill */
6092 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6093 		return (B_FALSE);
6094 	}
6095 
6096 	return (B_TRUE);
6097 }
6098 
6099 /*
6100  * return true if the ipif can be destroyed: the ipif has to be quiescent
6101  * with zero references from ire/nce/ilm to it.
6102  */
6103 static boolean_t
6104 ipif_is_freeable(ipif_t *ipif)
6105 {
6106 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6107 	ASSERT(ipif->ipif_id != 0);
6108 	return (ipif->ipif_refcnt == 0 && IPIF_FREE_OK(ipif));
6109 }
6110 
6111 /*
6112  * This func does not prevent refcnt from increasing. But if
6113  * the caller has taken steps to that effect, then this func
6114  * can be used to determine whether the ipifs marked with IPIF_MOVING
6115  * have become quiescent and can be moved in a failover/failback.
6116  */
6117 static ipif_t *
6118 ill_quiescent_to_move(ill_t *ill)
6119 {
6120 	ipif_t  *ipif;
6121 
6122 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6123 
6124 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6125 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6126 			if (ipif->ipif_refcnt != 0 ||
6127 			    !IPIF_DOWN_OK(ipif)) {
6128 				return (ipif);
6129 			}
6130 		}
6131 	}
6132 	return (NULL);
6133 }
6134 
6135 /*
6136  * The ipif/ill/ire has been refreled. Do the tail processing.
6137  * Determine if the ipif or ill in question has become quiescent and if so
6138  * wakeup close and/or restart any queued pending ioctl that is waiting
6139  * for the ipif_down (or ill_down)
6140  */
6141 void
6142 ipif_ill_refrele_tail(ill_t *ill)
6143 {
6144 	mblk_t	*mp;
6145 	conn_t	*connp;
6146 	ipsq_t	*ipsq;
6147 	ipif_t	*ipif;
6148 	dl_notify_ind_t *dlindp;
6149 
6150 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6151 
6152 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6153 	    ill_is_freeable(ill)) {
6154 		/* ill_close may be waiting */
6155 		cv_broadcast(&ill->ill_cv);
6156 	}
6157 
6158 	/* ipsq can't change because ill_lock  is held */
6159 	ipsq = ill->ill_phyint->phyint_ipsq;
6160 	if (ipsq->ipsq_waitfor == 0) {
6161 		/* Not waiting for anything, just return. */
6162 		mutex_exit(&ill->ill_lock);
6163 		return;
6164 	}
6165 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6166 	    ipsq->ipsq_pending_ipif != NULL);
6167 	/*
6168 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6169 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6170 	 * be zero for restarting an ioctl that ends up downing the ill.
6171 	 */
6172 	ipif = ipsq->ipsq_pending_ipif;
6173 	if (ipif->ipif_ill != ill) {
6174 		/* The ioctl is pending on some other ill. */
6175 		mutex_exit(&ill->ill_lock);
6176 		return;
6177 	}
6178 
6179 	switch (ipsq->ipsq_waitfor) {
6180 	case IPIF_DOWN:
6181 		if (!ipif_is_quiescent(ipif)) {
6182 			mutex_exit(&ill->ill_lock);
6183 			return;
6184 		}
6185 		break;
6186 	case IPIF_FREE:
6187 		if (!ipif_is_freeable(ipif)) {
6188 			mutex_exit(&ill->ill_lock);
6189 			return;
6190 		}
6191 		break;
6192 
6193 	case ILL_DOWN:
6194 		if (!ill_is_quiescent(ill)) {
6195 			mutex_exit(&ill->ill_lock);
6196 			return;
6197 		}
6198 		break;
6199 	case ILL_FREE:
6200 		/*
6201 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6202 		 * waits synchronously in ip_close, and no message is queued in
6203 		 * ipsq_pending_mp at all in this case
6204 		 */
6205 		if (!ill_is_freeable(ill)) {
6206 			mutex_exit(&ill->ill_lock);
6207 			return;
6208 		}
6209 		break;
6210 
6211 	case ILL_MOVE_OK:
6212 		if (ill_quiescent_to_move(ill) != NULL) {
6213 			mutex_exit(&ill->ill_lock);
6214 			return;
6215 		}
6216 		break;
6217 	default:
6218 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6219 		    (void *)ipsq, ipsq->ipsq_waitfor);
6220 	}
6221 
6222 	/*
6223 	 * Incr refcnt for the qwriter_ip call below which
6224 	 * does a refrele
6225 	 */
6226 	ill_refhold_locked(ill);
6227 	mp = ipsq_pending_mp_get(ipsq, &connp);
6228 	mutex_exit(&ill->ill_lock);
6229 
6230 	ASSERT(mp != NULL);
6231 	/*
6232 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6233 	 * we can only get here when the current operation decides it
6234 	 * it needs to quiesce via ipsq_pending_mp_add().
6235 	 */
6236 	switch (mp->b_datap->db_type) {
6237 	case M_PCPROTO:
6238 	case M_PROTO:
6239 		/*
6240 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6241 		 */
6242 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6243 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6244 
6245 		switch (dlindp->dl_notification) {
6246 		case DL_NOTE_PHYS_ADDR:
6247 			qwriter_ip(ill, ill->ill_rq, mp,
6248 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6249 			return;
6250 		default:
6251 			ASSERT(0);
6252 		}
6253 		break;
6254 
6255 	case M_ERROR:
6256 	case M_HANGUP:
6257 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6258 		    B_TRUE);
6259 		return;
6260 
6261 	case M_IOCTL:
6262 	case M_IOCDATA:
6263 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6264 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6265 		return;
6266 
6267 	default:
6268 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6269 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6270 	}
6271 }
6272 
6273 #ifdef DEBUG
6274 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6275 static void
6276 th_trace_rrecord(th_trace_t *th_trace)
6277 {
6278 	tr_buf_t *tr_buf;
6279 	uint_t lastref;
6280 
6281 	lastref = th_trace->th_trace_lastref;
6282 	lastref++;
6283 	if (lastref == TR_BUF_MAX)
6284 		lastref = 0;
6285 	th_trace->th_trace_lastref = lastref;
6286 	tr_buf = &th_trace->th_trbuf[lastref];
6287 	tr_buf->tr_time = lbolt;
6288 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6289 }
6290 
6291 static void
6292 th_trace_free(void *value)
6293 {
6294 	th_trace_t *th_trace = value;
6295 
6296 	ASSERT(th_trace->th_refcnt == 0);
6297 	kmem_free(th_trace, sizeof (*th_trace));
6298 }
6299 
6300 /*
6301  * Find or create the per-thread hash table used to track object references.
6302  * The ipst argument is NULL if we shouldn't allocate.
6303  *
6304  * Accesses per-thread data, so there's no need to lock here.
6305  */
6306 static mod_hash_t *
6307 th_trace_gethash(ip_stack_t *ipst)
6308 {
6309 	th_hash_t *thh;
6310 
6311 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6312 		mod_hash_t *mh;
6313 		char name[256];
6314 		size_t objsize, rshift;
6315 		int retv;
6316 
6317 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6318 			return (NULL);
6319 		(void) snprintf(name, sizeof (name), "th_trace_%p",
6320 		    (void *)curthread);
6321 
6322 		/*
6323 		 * We use mod_hash_create_extended here rather than the more
6324 		 * obvious mod_hash_create_ptrhash because the latter has a
6325 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6326 		 * block.
6327 		 */
6328 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6329 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6330 		rshift = highbit(objsize);
6331 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6332 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6333 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6334 		if (mh == NULL) {
6335 			kmem_free(thh, sizeof (*thh));
6336 			return (NULL);
6337 		}
6338 		thh->thh_hash = mh;
6339 		thh->thh_ipst = ipst;
6340 		/*
6341 		 * We trace ills, ipifs, ires, and nces.  All of these are
6342 		 * per-IP-stack, so the lock on the thread list is as well.
6343 		 */
6344 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6345 		list_insert_tail(&ip_thread_list, thh);
6346 		rw_exit(&ip_thread_rwlock);
6347 		retv = tsd_set(ip_thread_data, thh);
6348 		ASSERT(retv == 0);
6349 	}
6350 	return (thh != NULL ? thh->thh_hash : NULL);
6351 }
6352 
6353 boolean_t
6354 th_trace_ref(const void *obj, ip_stack_t *ipst)
6355 {
6356 	th_trace_t *th_trace;
6357 	mod_hash_t *mh;
6358 	mod_hash_val_t val;
6359 
6360 	if ((mh = th_trace_gethash(ipst)) == NULL)
6361 		return (B_FALSE);
6362 
6363 	/*
6364 	 * Attempt to locate the trace buffer for this obj and thread.
6365 	 * If it does not exist, then allocate a new trace buffer and
6366 	 * insert into the hash.
6367 	 */
6368 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6369 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6370 		if (th_trace == NULL)
6371 			return (B_FALSE);
6372 
6373 		th_trace->th_id = curthread;
6374 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6375 		    (mod_hash_val_t)th_trace) != 0) {
6376 			kmem_free(th_trace, sizeof (th_trace_t));
6377 			return (B_FALSE);
6378 		}
6379 	} else {
6380 		th_trace = (th_trace_t *)val;
6381 	}
6382 
6383 	ASSERT(th_trace->th_refcnt >= 0 &&
6384 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6385 
6386 	th_trace->th_refcnt++;
6387 	th_trace_rrecord(th_trace);
6388 	return (B_TRUE);
6389 }
6390 
6391 /*
6392  * For the purpose of tracing a reference release, we assume that global
6393  * tracing is always on and that the same thread initiated the reference hold
6394  * is releasing.
6395  */
6396 void
6397 th_trace_unref(const void *obj)
6398 {
6399 	int retv;
6400 	mod_hash_t *mh;
6401 	th_trace_t *th_trace;
6402 	mod_hash_val_t val;
6403 
6404 	mh = th_trace_gethash(NULL);
6405 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6406 	ASSERT(retv == 0);
6407 	th_trace = (th_trace_t *)val;
6408 
6409 	ASSERT(th_trace->th_refcnt > 0);
6410 	th_trace->th_refcnt--;
6411 	th_trace_rrecord(th_trace);
6412 }
6413 
6414 /*
6415  * If tracing has been disabled, then we assume that the reference counts are
6416  * now useless, and we clear them out before destroying the entries.
6417  */
6418 void
6419 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6420 {
6421 	th_hash_t	*thh;
6422 	mod_hash_t	*mh;
6423 	mod_hash_val_t	val;
6424 	th_trace_t	*th_trace;
6425 	int		retv;
6426 
6427 	rw_enter(&ip_thread_rwlock, RW_READER);
6428 	for (thh = list_head(&ip_thread_list); thh != NULL;
6429 	    thh = list_next(&ip_thread_list, thh)) {
6430 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6431 		    &val) == 0) {
6432 			th_trace = (th_trace_t *)val;
6433 			if (trace_disable)
6434 				th_trace->th_refcnt = 0;
6435 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6436 			ASSERT(retv == 0);
6437 		}
6438 	}
6439 	rw_exit(&ip_thread_rwlock);
6440 }
6441 
6442 void
6443 ipif_trace_ref(ipif_t *ipif)
6444 {
6445 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6446 
6447 	if (ipif->ipif_trace_disable)
6448 		return;
6449 
6450 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6451 		ipif->ipif_trace_disable = B_TRUE;
6452 		ipif_trace_cleanup(ipif);
6453 	}
6454 }
6455 
6456 void
6457 ipif_untrace_ref(ipif_t *ipif)
6458 {
6459 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6460 
6461 	if (!ipif->ipif_trace_disable)
6462 		th_trace_unref(ipif);
6463 }
6464 
6465 void
6466 ill_trace_ref(ill_t *ill)
6467 {
6468 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6469 
6470 	if (ill->ill_trace_disable)
6471 		return;
6472 
6473 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6474 		ill->ill_trace_disable = B_TRUE;
6475 		ill_trace_cleanup(ill);
6476 	}
6477 }
6478 
6479 void
6480 ill_untrace_ref(ill_t *ill)
6481 {
6482 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6483 
6484 	if (!ill->ill_trace_disable)
6485 		th_trace_unref(ill);
6486 }
6487 
6488 /*
6489  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6490  * failure, ipif_trace_disable is set.
6491  */
6492 static void
6493 ipif_trace_cleanup(const ipif_t *ipif)
6494 {
6495 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6496 }
6497 
6498 /*
6499  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6500  * failure, ill_trace_disable is set.
6501  */
6502 static void
6503 ill_trace_cleanup(const ill_t *ill)
6504 {
6505 	th_trace_cleanup(ill, ill->ill_trace_disable);
6506 }
6507 #endif /* DEBUG */
6508 
6509 void
6510 ipif_refhold_locked(ipif_t *ipif)
6511 {
6512 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6513 	ipif->ipif_refcnt++;
6514 	IPIF_TRACE_REF(ipif);
6515 }
6516 
6517 void
6518 ipif_refhold(ipif_t *ipif)
6519 {
6520 	ill_t	*ill;
6521 
6522 	ill = ipif->ipif_ill;
6523 	mutex_enter(&ill->ill_lock);
6524 	ipif->ipif_refcnt++;
6525 	IPIF_TRACE_REF(ipif);
6526 	mutex_exit(&ill->ill_lock);
6527 }
6528 
6529 /*
6530  * Must not be called while holding any locks. Otherwise if this is
6531  * the last reference to be released there is a chance of recursive mutex
6532  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6533  * to restart an ioctl.
6534  */
6535 void
6536 ipif_refrele(ipif_t *ipif)
6537 {
6538 	ill_t	*ill;
6539 
6540 	ill = ipif->ipif_ill;
6541 
6542 	mutex_enter(&ill->ill_lock);
6543 	ASSERT(ipif->ipif_refcnt != 0);
6544 	ipif->ipif_refcnt--;
6545 	IPIF_UNTRACE_REF(ipif);
6546 	if (ipif->ipif_refcnt != 0) {
6547 		mutex_exit(&ill->ill_lock);
6548 		return;
6549 	}
6550 
6551 	/* Drops the ill_lock */
6552 	ipif_ill_refrele_tail(ill);
6553 }
6554 
6555 ipif_t *
6556 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6557 {
6558 	ipif_t	*ipif;
6559 
6560 	mutex_enter(&ill->ill_lock);
6561 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6562 	    ipif != NULL; ipif = ipif->ipif_next) {
6563 		if (!IPIF_CAN_LOOKUP(ipif))
6564 			continue;
6565 		ipif_refhold_locked(ipif);
6566 		mutex_exit(&ill->ill_lock);
6567 		return (ipif);
6568 	}
6569 	mutex_exit(&ill->ill_lock);
6570 	return (NULL);
6571 }
6572 
6573 /*
6574  * TODO: make this table extendible at run time
6575  * Return a pointer to the mac type info for 'mac_type'
6576  */
6577 static ip_m_t *
6578 ip_m_lookup(t_uscalar_t mac_type)
6579 {
6580 	ip_m_t	*ipm;
6581 
6582 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6583 		if (ipm->ip_m_mac_type == mac_type)
6584 			return (ipm);
6585 	return (NULL);
6586 }
6587 
6588 /*
6589  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6590  * ipif_arg is passed in to associate it with the correct interface.
6591  * We may need to restart this operation if the ipif cannot be looked up
6592  * due to an exclusive operation that is currently in progress. The restart
6593  * entry point is specified by 'func'
6594  */
6595 int
6596 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6597     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6598     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6599     struct rtsa_s *sp, ip_stack_t *ipst)
6600 {
6601 	ire_t	*ire;
6602 	ire_t	*gw_ire = NULL;
6603 	ipif_t	*ipif = NULL;
6604 	boolean_t ipif_refheld = B_FALSE;
6605 	uint_t	type;
6606 	int	match_flags = MATCH_IRE_TYPE;
6607 	int	error;
6608 	tsol_gc_t *gc = NULL;
6609 	tsol_gcgrp_t *gcgrp = NULL;
6610 	boolean_t gcgrp_xtraref = B_FALSE;
6611 
6612 	ip1dbg(("ip_rt_add:"));
6613 
6614 	if (ire_arg != NULL)
6615 		*ire_arg = NULL;
6616 
6617 	/*
6618 	 * If this is the case of RTF_HOST being set, then we set the netmask
6619 	 * to all ones (regardless if one was supplied).
6620 	 */
6621 	if (flags & RTF_HOST)
6622 		mask = IP_HOST_MASK;
6623 
6624 	/*
6625 	 * Prevent routes with a zero gateway from being created (since
6626 	 * interfaces can currently be plumbed and brought up no assigned
6627 	 * address).
6628 	 */
6629 	if (gw_addr == 0)
6630 		return (ENETUNREACH);
6631 	/*
6632 	 * Get the ipif, if any, corresponding to the gw_addr
6633 	 */
6634 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6635 	    ipst);
6636 	if (ipif != NULL) {
6637 		if (IS_VNI(ipif->ipif_ill)) {
6638 			ipif_refrele(ipif);
6639 			return (EINVAL);
6640 		}
6641 		ipif_refheld = B_TRUE;
6642 	} else if (error == EINPROGRESS) {
6643 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6644 		return (EINPROGRESS);
6645 	} else {
6646 		error = 0;
6647 	}
6648 
6649 	if (ipif != NULL) {
6650 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6651 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6652 	} else {
6653 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6654 	}
6655 
6656 	/*
6657 	 * GateD will attempt to create routes with a loopback interface
6658 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6659 	 * these routes to be added, but create them as interface routes
6660 	 * since the gateway is an interface address.
6661 	 */
6662 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6663 		flags &= ~RTF_GATEWAY;
6664 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6665 		    mask == IP_HOST_MASK) {
6666 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6667 			    ALL_ZONES, NULL, match_flags, ipst);
6668 			if (ire != NULL) {
6669 				ire_refrele(ire);
6670 				if (ipif_refheld)
6671 					ipif_refrele(ipif);
6672 				return (EEXIST);
6673 			}
6674 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6675 			    "for 0x%x\n", (void *)ipif,
6676 			    ipif->ipif_ire_type,
6677 			    ntohl(ipif->ipif_lcl_addr)));
6678 			ire = ire_create(
6679 			    (uchar_t *)&dst_addr,	/* dest address */
6680 			    (uchar_t *)&mask,		/* mask */
6681 			    (uchar_t *)&ipif->ipif_src_addr,
6682 			    NULL,			/* no gateway */
6683 			    &ipif->ipif_mtu,
6684 			    NULL,
6685 			    ipif->ipif_rq,		/* recv-from queue */
6686 			    NULL,			/* no send-to queue */
6687 			    ipif->ipif_ire_type,	/* LOOPBACK */
6688 			    ipif,
6689 			    0,
6690 			    0,
6691 			    0,
6692 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6693 			    RTF_PRIVATE : 0,
6694 			    &ire_uinfo_null,
6695 			    NULL,
6696 			    NULL,
6697 			    ipst);
6698 
6699 			if (ire == NULL) {
6700 				if (ipif_refheld)
6701 					ipif_refrele(ipif);
6702 				return (ENOMEM);
6703 			}
6704 			error = ire_add(&ire, q, mp, func, B_FALSE);
6705 			if (error == 0)
6706 				goto save_ire;
6707 			if (ipif_refheld)
6708 				ipif_refrele(ipif);
6709 			return (error);
6710 
6711 		}
6712 	}
6713 
6714 	/*
6715 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6716 	 * and the gateway address provided is one of the system's interface
6717 	 * addresses.  By using the routing socket interface and supplying an
6718 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6719 	 * specifying an interface route to be created is available which uses
6720 	 * the interface index that specifies the outgoing interface rather than
6721 	 * the address of an outgoing interface (which may not be able to
6722 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6723 	 * flag, routes can be specified which not only specify the next-hop to
6724 	 * be used when routing to a certain prefix, but also which outgoing
6725 	 * interface should be used.
6726 	 *
6727 	 * Previously, interfaces would have unique addresses assigned to them
6728 	 * and so the address assigned to a particular interface could be used
6729 	 * to identify a particular interface.  One exception to this was the
6730 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6731 	 *
6732 	 * With the advent of IPv6 and its link-local addresses, this
6733 	 * restriction was relaxed and interfaces could share addresses between
6734 	 * themselves.  In fact, typically all of the link-local interfaces on
6735 	 * an IPv6 node or router will have the same link-local address.  In
6736 	 * order to differentiate between these interfaces, the use of an
6737 	 * interface index is necessary and this index can be carried inside a
6738 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6739 	 * of using the interface index, however, is that all of the ipif's that
6740 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6741 	 * cannot be used to differentiate between ipif's (or logical
6742 	 * interfaces) that belong to the same ill (physical interface).
6743 	 *
6744 	 * For example, in the following case involving IPv4 interfaces and
6745 	 * logical interfaces
6746 	 *
6747 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6748 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6749 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6750 	 *
6751 	 * the ipif's corresponding to each of these interface routes can be
6752 	 * uniquely identified by the "gateway" (actually interface address).
6753 	 *
6754 	 * In this case involving multiple IPv6 default routes to a particular
6755 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6756 	 * default route is of interest:
6757 	 *
6758 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6759 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6760 	 */
6761 
6762 	/* RTF_GATEWAY not set */
6763 	if (!(flags & RTF_GATEWAY)) {
6764 		queue_t	*stq;
6765 
6766 		if (sp != NULL) {
6767 			ip2dbg(("ip_rt_add: gateway security attributes "
6768 			    "cannot be set with interface route\n"));
6769 			if (ipif_refheld)
6770 				ipif_refrele(ipif);
6771 			return (EINVAL);
6772 		}
6773 
6774 		/*
6775 		 * As the interface index specified with the RTA_IFP sockaddr is
6776 		 * the same for all ipif's off of an ill, the matching logic
6777 		 * below uses MATCH_IRE_ILL if such an index was specified.
6778 		 * This means that routes sharing the same prefix when added
6779 		 * using a RTA_IFP sockaddr must have distinct interface
6780 		 * indices (namely, they must be on distinct ill's).
6781 		 *
6782 		 * On the other hand, since the gateway address will usually be
6783 		 * different for each ipif on the system, the matching logic
6784 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6785 		 * route.  This means that interface routes for the same prefix
6786 		 * can be created if they belong to distinct ipif's and if a
6787 		 * RTA_IFP sockaddr is not present.
6788 		 */
6789 		if (ipif_arg != NULL) {
6790 			if (ipif_refheld)  {
6791 				ipif_refrele(ipif);
6792 				ipif_refheld = B_FALSE;
6793 			}
6794 			ipif = ipif_arg;
6795 			match_flags |= MATCH_IRE_ILL;
6796 		} else {
6797 			/*
6798 			 * Check the ipif corresponding to the gw_addr
6799 			 */
6800 			if (ipif == NULL)
6801 				return (ENETUNREACH);
6802 			match_flags |= MATCH_IRE_IPIF;
6803 		}
6804 		ASSERT(ipif != NULL);
6805 
6806 		/*
6807 		 * We check for an existing entry at this point.
6808 		 *
6809 		 * Since a netmask isn't passed in via the ioctl interface
6810 		 * (SIOCADDRT), we don't check for a matching netmask in that
6811 		 * case.
6812 		 */
6813 		if (!ioctl_msg)
6814 			match_flags |= MATCH_IRE_MASK;
6815 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
6816 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
6817 		if (ire != NULL) {
6818 			ire_refrele(ire);
6819 			if (ipif_refheld)
6820 				ipif_refrele(ipif);
6821 			return (EEXIST);
6822 		}
6823 
6824 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6825 		    ? ipif->ipif_rq : ipif->ipif_wq;
6826 
6827 		/*
6828 		 * Create a copy of the IRE_LOOPBACK,
6829 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6830 		 * the modified address and netmask.
6831 		 */
6832 		ire = ire_create(
6833 		    (uchar_t *)&dst_addr,
6834 		    (uint8_t *)&mask,
6835 		    (uint8_t *)&ipif->ipif_src_addr,
6836 		    NULL,
6837 		    &ipif->ipif_mtu,
6838 		    NULL,
6839 		    NULL,
6840 		    stq,
6841 		    ipif->ipif_net_type,
6842 		    ipif,
6843 		    0,
6844 		    0,
6845 		    0,
6846 		    flags,
6847 		    &ire_uinfo_null,
6848 		    NULL,
6849 		    NULL,
6850 		    ipst);
6851 		if (ire == NULL) {
6852 			if (ipif_refheld)
6853 				ipif_refrele(ipif);
6854 			return (ENOMEM);
6855 		}
6856 
6857 		/*
6858 		 * Some software (for example, GateD and Sun Cluster) attempts
6859 		 * to create (what amount to) IRE_PREFIX routes with the
6860 		 * loopback address as the gateway.  This is primarily done to
6861 		 * set up prefixes with the RTF_REJECT flag set (for example,
6862 		 * when generating aggregate routes.)
6863 		 *
6864 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6865 		 * IRE_LOOPBACK, then we map the request into a
6866 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
6867 		 * these interface routes, by definition, can only be that.
6868 		 *
6869 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6870 		 * routine, but rather using ire_create() directly.
6871 		 *
6872 		 */
6873 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
6874 			ire->ire_type = IRE_IF_NORESOLVER;
6875 			ire->ire_flags |= RTF_BLACKHOLE;
6876 		}
6877 
6878 		error = ire_add(&ire, q, mp, func, B_FALSE);
6879 		if (error == 0)
6880 			goto save_ire;
6881 
6882 		/*
6883 		 * In the result of failure, ire_add() will have already
6884 		 * deleted the ire in question, so there is no need to
6885 		 * do that here.
6886 		 */
6887 		if (ipif_refheld)
6888 			ipif_refrele(ipif);
6889 		return (error);
6890 	}
6891 	if (ipif_refheld) {
6892 		ipif_refrele(ipif);
6893 		ipif_refheld = B_FALSE;
6894 	}
6895 
6896 	/*
6897 	 * Get an interface IRE for the specified gateway.
6898 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6899 	 * gateway, it is currently unreachable and we fail the request
6900 	 * accordingly.
6901 	 */
6902 	ipif = ipif_arg;
6903 	if (ipif_arg != NULL)
6904 		match_flags |= MATCH_IRE_ILL;
6905 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6906 	    ALL_ZONES, 0, NULL, match_flags, ipst);
6907 	if (gw_ire == NULL)
6908 		return (ENETUNREACH);
6909 
6910 	/*
6911 	 * We create one of three types of IREs as a result of this request
6912 	 * based on the netmask.  A netmask of all ones (which is automatically
6913 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
6914 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
6915 	 * created.  Otherwise, an IRE_PREFIX route is created for the
6916 	 * destination prefix.
6917 	 */
6918 	if (mask == IP_HOST_MASK)
6919 		type = IRE_HOST;
6920 	else if (mask == 0)
6921 		type = IRE_DEFAULT;
6922 	else
6923 		type = IRE_PREFIX;
6924 
6925 	/* check for a duplicate entry */
6926 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
6927 	    NULL, ALL_ZONES, 0, NULL,
6928 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
6929 	if (ire != NULL) {
6930 		ire_refrele(gw_ire);
6931 		ire_refrele(ire);
6932 		return (EEXIST);
6933 	}
6934 
6935 	/* Security attribute exists */
6936 	if (sp != NULL) {
6937 		tsol_gcgrp_addr_t ga;
6938 
6939 		/* find or create the gateway credentials group */
6940 		ga.ga_af = AF_INET;
6941 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
6942 
6943 		/* we hold reference to it upon success */
6944 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
6945 		if (gcgrp == NULL) {
6946 			ire_refrele(gw_ire);
6947 			return (ENOMEM);
6948 		}
6949 
6950 		/*
6951 		 * Create and add the security attribute to the group; a
6952 		 * reference to the group is made upon allocating a new
6953 		 * entry successfully.  If it finds an already-existing
6954 		 * entry for the security attribute in the group, it simply
6955 		 * returns it and no new reference is made to the group.
6956 		 */
6957 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
6958 		if (gc == NULL) {
6959 			/* release reference held by gcgrp_lookup */
6960 			GCGRP_REFRELE(gcgrp);
6961 			ire_refrele(gw_ire);
6962 			return (ENOMEM);
6963 		}
6964 	}
6965 
6966 	/* Create the IRE. */
6967 	ire = ire_create(
6968 	    (uchar_t *)&dst_addr,		/* dest address */
6969 	    (uchar_t *)&mask,			/* mask */
6970 	    /* src address assigned by the caller? */
6971 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
6972 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
6973 	    (uchar_t *)&gw_addr,		/* gateway address */
6974 	    &gw_ire->ire_max_frag,
6975 	    NULL,				/* no src nce */
6976 	    NULL,				/* no recv-from queue */
6977 	    NULL,				/* no send-to queue */
6978 	    (ushort_t)type,			/* IRE type */
6979 	    ipif_arg,
6980 	    0,
6981 	    0,
6982 	    0,
6983 	    flags,
6984 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
6985 	    gc,					/* security attribute */
6986 	    NULL,
6987 	    ipst);
6988 
6989 	/*
6990 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
6991 	 * reference to the 'gcgrp'. We can now release the extra reference
6992 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
6993 	 */
6994 	if (gcgrp_xtraref)
6995 		GCGRP_REFRELE(gcgrp);
6996 	if (ire == NULL) {
6997 		if (gc != NULL)
6998 			GC_REFRELE(gc);
6999 		ire_refrele(gw_ire);
7000 		return (ENOMEM);
7001 	}
7002 
7003 	/*
7004 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7005 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7006 	 */
7007 
7008 	/* Add the new IRE. */
7009 	error = ire_add(&ire, q, mp, func, B_FALSE);
7010 	if (error != 0) {
7011 		/*
7012 		 * In the result of failure, ire_add() will have already
7013 		 * deleted the ire in question, so there is no need to
7014 		 * do that here.
7015 		 */
7016 		ire_refrele(gw_ire);
7017 		return (error);
7018 	}
7019 
7020 	if (flags & RTF_MULTIRT) {
7021 		/*
7022 		 * Invoke the CGTP (multirouting) filtering module
7023 		 * to add the dst address in the filtering database.
7024 		 * Replicated inbound packets coming from that address
7025 		 * will be filtered to discard the duplicates.
7026 		 * It is not necessary to call the CGTP filter hook
7027 		 * when the dst address is a broadcast or multicast,
7028 		 * because an IP source address cannot be a broadcast
7029 		 * or a multicast.
7030 		 */
7031 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7032 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7033 		if (ire_dst != NULL) {
7034 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7035 			ire_refrele(ire_dst);
7036 			goto save_ire;
7037 		}
7038 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7039 		    !CLASSD(ire->ire_addr)) {
7040 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7041 			    ipst->ips_netstack->netstack_stackid,
7042 			    ire->ire_addr,
7043 			    ire->ire_gateway_addr,
7044 			    ire->ire_src_addr,
7045 			    gw_ire->ire_src_addr);
7046 			if (res != 0) {
7047 				ire_refrele(gw_ire);
7048 				ire_delete(ire);
7049 				return (res);
7050 			}
7051 		}
7052 	}
7053 
7054 	/*
7055 	 * Now that the prefix IRE entry has been created, delete any
7056 	 * existing gateway IRE cache entries as well as any IRE caches
7057 	 * using the gateway, and force them to be created through
7058 	 * ip_newroute.
7059 	 */
7060 	if (gc != NULL) {
7061 		ASSERT(gcgrp != NULL);
7062 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7063 	}
7064 
7065 save_ire:
7066 	if (gw_ire != NULL) {
7067 		ire_refrele(gw_ire);
7068 	}
7069 	if (ipif != NULL) {
7070 		/*
7071 		 * Save enough information so that we can recreate the IRE if
7072 		 * the interface goes down and then up.  The metrics associated
7073 		 * with the route will be saved as well when rts_setmetrics() is
7074 		 * called after the IRE has been created.  In the case where
7075 		 * memory cannot be allocated, none of this information will be
7076 		 * saved.
7077 		 */
7078 		ipif_save_ire(ipif, ire);
7079 	}
7080 	if (ioctl_msg)
7081 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7082 	if (ire_arg != NULL) {
7083 		/*
7084 		 * Store the ire that was successfully added into where ire_arg
7085 		 * points to so that callers don't have to look it up
7086 		 * themselves (but they are responsible for ire_refrele()ing
7087 		 * the ire when they are finished with it).
7088 		 */
7089 		*ire_arg = ire;
7090 	} else {
7091 		ire_refrele(ire);		/* Held in ire_add */
7092 	}
7093 	if (ipif_refheld)
7094 		ipif_refrele(ipif);
7095 	return (0);
7096 }
7097 
7098 /*
7099  * ip_rt_delete is called to delete an IPv4 route.
7100  * ipif_arg is passed in to associate it with the correct interface.
7101  * We may need to restart this operation if the ipif cannot be looked up
7102  * due to an exclusive operation that is currently in progress. The restart
7103  * entry point is specified by 'func'
7104  */
7105 /* ARGSUSED4 */
7106 int
7107 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7108     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7109     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7110 {
7111 	ire_t	*ire = NULL;
7112 	ipif_t	*ipif;
7113 	boolean_t ipif_refheld = B_FALSE;
7114 	uint_t	type;
7115 	uint_t	match_flags = MATCH_IRE_TYPE;
7116 	int	err = 0;
7117 
7118 	ip1dbg(("ip_rt_delete:"));
7119 	/*
7120 	 * If this is the case of RTF_HOST being set, then we set the netmask
7121 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7122 	 */
7123 	if (flags & RTF_HOST) {
7124 		mask = IP_HOST_MASK;
7125 		match_flags |= MATCH_IRE_MASK;
7126 	} else if (rtm_addrs & RTA_NETMASK) {
7127 		match_flags |= MATCH_IRE_MASK;
7128 	}
7129 
7130 	/*
7131 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7132 	 * we check if the gateway address is one of our interfaces first,
7133 	 * and fall back on RTF_GATEWAY routes.
7134 	 *
7135 	 * This makes it possible to delete an original
7136 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7137 	 *
7138 	 * As the interface index specified with the RTA_IFP sockaddr is the
7139 	 * same for all ipif's off of an ill, the matching logic below uses
7140 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7141 	 * sharing the same prefix and interface index as the the route
7142 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7143 	 * is specified in the request.
7144 	 *
7145 	 * On the other hand, since the gateway address will usually be
7146 	 * different for each ipif on the system, the matching logic
7147 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7148 	 * route.  This means that interface routes for the same prefix can be
7149 	 * uniquely identified if they belong to distinct ipif's and if a
7150 	 * RTA_IFP sockaddr is not present.
7151 	 *
7152 	 * For more detail on specifying routes by gateway address and by
7153 	 * interface index, see the comments in ip_rt_add().
7154 	 */
7155 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7156 	    ipst);
7157 	if (ipif != NULL)
7158 		ipif_refheld = B_TRUE;
7159 	else if (err == EINPROGRESS)
7160 		return (err);
7161 	else
7162 		err = 0;
7163 	if (ipif != NULL) {
7164 		if (ipif_arg != NULL) {
7165 			if (ipif_refheld) {
7166 				ipif_refrele(ipif);
7167 				ipif_refheld = B_FALSE;
7168 			}
7169 			ipif = ipif_arg;
7170 			match_flags |= MATCH_IRE_ILL;
7171 		} else {
7172 			match_flags |= MATCH_IRE_IPIF;
7173 		}
7174 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7175 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7176 			    ALL_ZONES, NULL, match_flags, ipst);
7177 		}
7178 		if (ire == NULL) {
7179 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7180 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7181 			    match_flags, ipst);
7182 		}
7183 	}
7184 
7185 	if (ire == NULL) {
7186 		/*
7187 		 * At this point, the gateway address is not one of our own
7188 		 * addresses or a matching interface route was not found.  We
7189 		 * set the IRE type to lookup based on whether
7190 		 * this is a host route, a default route or just a prefix.
7191 		 *
7192 		 * If an ipif_arg was passed in, then the lookup is based on an
7193 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7194 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7195 		 * set as the route being looked up is not a traditional
7196 		 * interface route.
7197 		 */
7198 		match_flags &= ~MATCH_IRE_IPIF;
7199 		match_flags |= MATCH_IRE_GW;
7200 		if (ipif_arg != NULL)
7201 			match_flags |= MATCH_IRE_ILL;
7202 		if (mask == IP_HOST_MASK)
7203 			type = IRE_HOST;
7204 		else if (mask == 0)
7205 			type = IRE_DEFAULT;
7206 		else
7207 			type = IRE_PREFIX;
7208 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7209 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7210 	}
7211 
7212 	if (ipif_refheld)
7213 		ipif_refrele(ipif);
7214 
7215 	/* ipif is not refheld anymore */
7216 	if (ire == NULL)
7217 		return (ESRCH);
7218 
7219 	if (ire->ire_flags & RTF_MULTIRT) {
7220 		/*
7221 		 * Invoke the CGTP (multirouting) filtering module
7222 		 * to remove the dst address from the filtering database.
7223 		 * Packets coming from that address will no longer be
7224 		 * filtered to remove duplicates.
7225 		 */
7226 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7227 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7228 			    ipst->ips_netstack->netstack_stackid,
7229 			    ire->ire_addr, ire->ire_gateway_addr);
7230 		}
7231 		ip_cgtp_bcast_delete(ire, ipst);
7232 	}
7233 
7234 	ipif = ire->ire_ipif;
7235 	if (ipif != NULL)
7236 		ipif_remove_ire(ipif, ire);
7237 	if (ioctl_msg)
7238 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7239 	ire_delete(ire);
7240 	ire_refrele(ire);
7241 	return (err);
7242 }
7243 
7244 /*
7245  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7246  */
7247 /* ARGSUSED */
7248 int
7249 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7250     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7251 {
7252 	ipaddr_t dst_addr;
7253 	ipaddr_t gw_addr;
7254 	ipaddr_t mask;
7255 	int error = 0;
7256 	mblk_t *mp1;
7257 	struct rtentry *rt;
7258 	ipif_t *ipif = NULL;
7259 	ip_stack_t	*ipst;
7260 
7261 	ASSERT(q->q_next == NULL);
7262 	ipst = CONNQ_TO_IPST(q);
7263 
7264 	ip1dbg(("ip_siocaddrt:"));
7265 	/* Existence of mp1 verified in ip_wput_nondata */
7266 	mp1 = mp->b_cont->b_cont;
7267 	rt = (struct rtentry *)mp1->b_rptr;
7268 
7269 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7270 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7271 
7272 	/*
7273 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7274 	 * to a particular host address.  In this case, we set the netmask to
7275 	 * all ones for the particular destination address.  Otherwise,
7276 	 * determine the netmask to be used based on dst_addr and the interfaces
7277 	 * in use.
7278 	 */
7279 	if (rt->rt_flags & RTF_HOST) {
7280 		mask = IP_HOST_MASK;
7281 	} else {
7282 		/*
7283 		 * Note that ip_subnet_mask returns a zero mask in the case of
7284 		 * default (an all-zeroes address).
7285 		 */
7286 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7287 	}
7288 
7289 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7290 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7291 	if (ipif != NULL)
7292 		ipif_refrele(ipif);
7293 	return (error);
7294 }
7295 
7296 /*
7297  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7298  */
7299 /* ARGSUSED */
7300 int
7301 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7302     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7303 {
7304 	ipaddr_t dst_addr;
7305 	ipaddr_t gw_addr;
7306 	ipaddr_t mask;
7307 	int error;
7308 	mblk_t *mp1;
7309 	struct rtentry *rt;
7310 	ipif_t *ipif = NULL;
7311 	ip_stack_t	*ipst;
7312 
7313 	ASSERT(q->q_next == NULL);
7314 	ipst = CONNQ_TO_IPST(q);
7315 
7316 	ip1dbg(("ip_siocdelrt:"));
7317 	/* Existence of mp1 verified in ip_wput_nondata */
7318 	mp1 = mp->b_cont->b_cont;
7319 	rt = (struct rtentry *)mp1->b_rptr;
7320 
7321 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7322 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7323 
7324 	/*
7325 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7326 	 * to a particular host address.  In this case, we set the netmask to
7327 	 * all ones for the particular destination address.  Otherwise,
7328 	 * determine the netmask to be used based on dst_addr and the interfaces
7329 	 * in use.
7330 	 */
7331 	if (rt->rt_flags & RTF_HOST) {
7332 		mask = IP_HOST_MASK;
7333 	} else {
7334 		/*
7335 		 * Note that ip_subnet_mask returns a zero mask in the case of
7336 		 * default (an all-zeroes address).
7337 		 */
7338 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7339 	}
7340 
7341 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7342 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7343 	    mp, ip_process_ioctl, ipst);
7344 	if (ipif != NULL)
7345 		ipif_refrele(ipif);
7346 	return (error);
7347 }
7348 
7349 /*
7350  * Enqueue the mp onto the ipsq, chained by b_next.
7351  * b_prev stores the function to be executed later, and b_queue the queue
7352  * where this mp originated.
7353  */
7354 void
7355 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7356     ill_t *pending_ill)
7357 {
7358 	conn_t	*connp = NULL;
7359 
7360 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7361 	ASSERT(func != NULL);
7362 
7363 	mp->b_queue = q;
7364 	mp->b_prev = (void *)func;
7365 	mp->b_next = NULL;
7366 
7367 	switch (type) {
7368 	case CUR_OP:
7369 		if (ipsq->ipsq_mptail != NULL) {
7370 			ASSERT(ipsq->ipsq_mphead != NULL);
7371 			ipsq->ipsq_mptail->b_next = mp;
7372 		} else {
7373 			ASSERT(ipsq->ipsq_mphead == NULL);
7374 			ipsq->ipsq_mphead = mp;
7375 		}
7376 		ipsq->ipsq_mptail = mp;
7377 		break;
7378 
7379 	case NEW_OP:
7380 		if (ipsq->ipsq_xopq_mptail != NULL) {
7381 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7382 			ipsq->ipsq_xopq_mptail->b_next = mp;
7383 		} else {
7384 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7385 			ipsq->ipsq_xopq_mphead = mp;
7386 		}
7387 		ipsq->ipsq_xopq_mptail = mp;
7388 		break;
7389 	default:
7390 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7391 	}
7392 
7393 	if (CONN_Q(q) && pending_ill != NULL) {
7394 		connp = Q_TO_CONN(q);
7395 
7396 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7397 		connp->conn_oper_pending_ill = pending_ill;
7398 	}
7399 }
7400 
7401 /*
7402  * Return the mp at the head of the ipsq. After emptying the ipsq
7403  * look at the next ioctl, if this ioctl is complete. Otherwise
7404  * return, we will resume when we complete the current ioctl.
7405  * The current ioctl will wait till it gets a response from the
7406  * driver below.
7407  */
7408 static mblk_t *
7409 ipsq_dq(ipsq_t *ipsq)
7410 {
7411 	mblk_t	*mp;
7412 
7413 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7414 
7415 	mp = ipsq->ipsq_mphead;
7416 	if (mp != NULL) {
7417 		ipsq->ipsq_mphead = mp->b_next;
7418 		if (ipsq->ipsq_mphead == NULL)
7419 			ipsq->ipsq_mptail = NULL;
7420 		mp->b_next = NULL;
7421 		return (mp);
7422 	}
7423 	if (ipsq->ipsq_current_ipif != NULL)
7424 		return (NULL);
7425 	mp = ipsq->ipsq_xopq_mphead;
7426 	if (mp != NULL) {
7427 		ipsq->ipsq_xopq_mphead = mp->b_next;
7428 		if (ipsq->ipsq_xopq_mphead == NULL)
7429 			ipsq->ipsq_xopq_mptail = NULL;
7430 		mp->b_next = NULL;
7431 		return (mp);
7432 	}
7433 	return (NULL);
7434 }
7435 
7436 /*
7437  * Enter the ipsq corresponding to ill, by waiting synchronously till
7438  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7439  * will have to drain completely before ipsq_enter returns success.
7440  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7441  * and the ipsq_exit logic will start the next enqueued ioctl after
7442  * completion of the current ioctl. If 'force' is used, we don't wait
7443  * for the enqueued ioctls. This is needed when a conn_close wants to
7444  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7445  * of an ill can also use this option. But we dont' use it currently.
7446  */
7447 #define	ENTER_SQ_WAIT_TICKS 100
7448 boolean_t
7449 ipsq_enter(ill_t *ill, boolean_t force, int type)
7450 {
7451 	ipsq_t	*ipsq;
7452 	boolean_t waited_enough = B_FALSE;
7453 
7454 	/*
7455 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7456 	 * Since the <ill-ipsq> assocs could change while we wait for the
7457 	 * writer, it is easier to wait on a fixed global rather than try to
7458 	 * cv_wait on a changing ipsq.
7459 	 */
7460 	mutex_enter(&ill->ill_lock);
7461 	for (;;) {
7462 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7463 			mutex_exit(&ill->ill_lock);
7464 			return (B_FALSE);
7465 		}
7466 
7467 		ipsq = ill->ill_phyint->phyint_ipsq;
7468 		mutex_enter(&ipsq->ipsq_lock);
7469 		if (ipsq->ipsq_writer == NULL &&
7470 		    (type == CUR_OP || ipsq->ipsq_current_ipif == NULL ||
7471 		    waited_enough)) {
7472 			break;
7473 		} else if (ipsq->ipsq_writer != NULL) {
7474 			mutex_exit(&ipsq->ipsq_lock);
7475 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7476 		} else {
7477 			mutex_exit(&ipsq->ipsq_lock);
7478 			if (force) {
7479 				(void) cv_timedwait(&ill->ill_cv,
7480 				    &ill->ill_lock,
7481 				    lbolt + ENTER_SQ_WAIT_TICKS);
7482 				waited_enough = B_TRUE;
7483 				continue;
7484 			} else {
7485 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7486 			}
7487 		}
7488 	}
7489 
7490 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7491 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7492 	ipsq->ipsq_writer = curthread;
7493 	ipsq->ipsq_reentry_cnt++;
7494 #ifdef DEBUG
7495 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IPSQ_STACK_DEPTH);
7496 #endif
7497 	mutex_exit(&ipsq->ipsq_lock);
7498 	mutex_exit(&ill->ill_lock);
7499 	return (B_TRUE);
7500 }
7501 
7502 boolean_t
7503 ill_perim_enter(ill_t *ill)
7504 {
7505 	return (ipsq_enter(ill, B_FALSE, CUR_OP));
7506 }
7507 
7508 void
7509 ill_perim_exit(ill_t *ill)
7510 {
7511 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
7512 }
7513 
7514 /*
7515  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7516  * certain critical operations like plumbing (i.e. most set ioctls),
7517  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7518  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7519  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7520  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7521  * threads executing in the ipsq. Responses from the driver pertain to the
7522  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7523  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7524  *
7525  * If a thread does not want to reenter the ipsq when it is already writer,
7526  * it must make sure that the specified reentry point to be called later
7527  * when the ipsq is empty, nor any code path starting from the specified reentry
7528  * point must never ever try to enter the ipsq again. Otherwise it can lead
7529  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7530  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7531  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7532  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7533  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7534  * ioctl if the current ioctl has completed. If the current ioctl is still
7535  * in progress it simply returns. The current ioctl could be waiting for
7536  * a response from another module (arp_ or the driver or could be waiting for
7537  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7538  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7539  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7540  * ipsq_current_ipif is clear which happens only on ioctl completion.
7541  */
7542 
7543 /*
7544  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7545  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7546  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7547  * completion.
7548  */
7549 ipsq_t *
7550 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7551     ipsq_func_t func, int type, boolean_t reentry_ok)
7552 {
7553 	ipsq_t	*ipsq;
7554 
7555 	/* Only 1 of ipif or ill can be specified */
7556 	ASSERT((ipif != NULL) ^ (ill != NULL));
7557 	if (ipif != NULL)
7558 		ill = ipif->ipif_ill;
7559 
7560 	/*
7561 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7562 	 * ipsq of an ill can't change when ill_lock is held.
7563 	 */
7564 	GRAB_CONN_LOCK(q);
7565 	mutex_enter(&ill->ill_lock);
7566 	ipsq = ill->ill_phyint->phyint_ipsq;
7567 	mutex_enter(&ipsq->ipsq_lock);
7568 
7569 	/*
7570 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7571 	 *    (Note: If the caller does not specify reentry_ok then neither
7572 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7573 	 *    again. Otherwise it can lead to an infinite loop
7574 	 * 2. Enter the ipsq if there is no current writer and this attempted
7575 	 *    entry is part of the current ioctl or operation
7576 	 * 3. Enter the ipsq if there is no current writer and this is a new
7577 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7578 	 *    empty and there is no ioctl (or operation) currently in progress
7579 	 */
7580 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7581 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7582 	    ipsq->ipsq_current_ipif == NULL))) ||
7583 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7584 		/* Success. */
7585 		ipsq->ipsq_reentry_cnt++;
7586 		ipsq->ipsq_writer = curthread;
7587 		mutex_exit(&ipsq->ipsq_lock);
7588 		mutex_exit(&ill->ill_lock);
7589 		RELEASE_CONN_LOCK(q);
7590 #ifdef DEBUG
7591 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack,
7592 		    IPSQ_STACK_DEPTH);
7593 #endif
7594 		return (ipsq);
7595 	}
7596 
7597 	ipsq_enq(ipsq, q, mp, func, type, ill);
7598 
7599 	mutex_exit(&ipsq->ipsq_lock);
7600 	mutex_exit(&ill->ill_lock);
7601 	RELEASE_CONN_LOCK(q);
7602 	return (NULL);
7603 }
7604 
7605 /*
7606  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7607  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7608  * cannot be entered, the mp is queued for completion.
7609  */
7610 void
7611 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7612     boolean_t reentry_ok)
7613 {
7614 	ipsq_t	*ipsq;
7615 
7616 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7617 
7618 	/*
7619 	 * Drop the caller's refhold on the ill.  This is safe since we either
7620 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7621 	 * IPSQ, in which case we return without accessing ill anymore.  This
7622 	 * is needed because func needs to see the correct refcount.
7623 	 * e.g. removeif can work only then.
7624 	 */
7625 	ill_refrele(ill);
7626 	if (ipsq != NULL) {
7627 		(*func)(ipsq, q, mp, NULL);
7628 		ipsq_exit(ipsq);
7629 	}
7630 }
7631 
7632 /*
7633  * If there are more than ILL_GRP_CNT ills in a group,
7634  * we use kmem alloc'd buffers, else use the stack
7635  */
7636 #define	ILL_GRP_CNT	14
7637 /*
7638  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7639  * Called by a thread that is currently exclusive on this ipsq.
7640  */
7641 void
7642 ipsq_exit(ipsq_t *ipsq)
7643 {
7644 	queue_t	*q;
7645 	mblk_t	*mp;
7646 	ipsq_func_t	func;
7647 	int	next;
7648 	ill_t	**ill_list = NULL;
7649 	size_t	ill_list_size = 0;
7650 	int	cnt = 0;
7651 	boolean_t need_ipsq_free = B_FALSE;
7652 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7653 
7654 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7655 	mutex_enter(&ipsq->ipsq_lock);
7656 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7657 	if (ipsq->ipsq_reentry_cnt != 1) {
7658 		ipsq->ipsq_reentry_cnt--;
7659 		mutex_exit(&ipsq->ipsq_lock);
7660 		return;
7661 	}
7662 
7663 	mp = ipsq_dq(ipsq);
7664 	while (mp != NULL) {
7665 again:
7666 		mutex_exit(&ipsq->ipsq_lock);
7667 		func = (ipsq_func_t)mp->b_prev;
7668 		q = (queue_t *)mp->b_queue;
7669 		mp->b_prev = NULL;
7670 		mp->b_queue = NULL;
7671 
7672 		/*
7673 		 * If 'q' is an conn queue, it is valid, since we did a
7674 		 * a refhold on the connp, at the start of the ioctl.
7675 		 * If 'q' is an ill queue, it is valid, since close of an
7676 		 * ill will clean up the 'ipsq'.
7677 		 */
7678 		(*func)(ipsq, q, mp, NULL);
7679 
7680 		mutex_enter(&ipsq->ipsq_lock);
7681 		mp = ipsq_dq(ipsq);
7682 	}
7683 
7684 	mutex_exit(&ipsq->ipsq_lock);
7685 
7686 	/*
7687 	 * Need to grab the locks in the right order. Need to
7688 	 * atomically check (under ipsq_lock) that there are no
7689 	 * messages before relinquishing the ipsq. Also need to
7690 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7691 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7692 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7693 	 * to grab ill_g_lock as writer.
7694 	 */
7695 	rw_enter(&ipst->ips_ill_g_lock,
7696 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7697 
7698 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7699 	if (ipsq->ipsq_refs != 0) {
7700 		/* At most 2 ills v4/v6 per phyint */
7701 		cnt = ipsq->ipsq_refs << 1;
7702 		ill_list_size = cnt * sizeof (ill_t *);
7703 		/*
7704 		 * If memory allocation fails, we will do the split
7705 		 * the next time ipsq_exit is called for whatever reason.
7706 		 * As long as the ipsq_split flag is set the need to
7707 		 * split is remembered.
7708 		 */
7709 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7710 		if (ill_list != NULL)
7711 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7712 	}
7713 	mutex_enter(&ipsq->ipsq_lock);
7714 	mp = ipsq_dq(ipsq);
7715 	if (mp != NULL) {
7716 		/* oops, some message has landed up, we can't get out */
7717 		if (ill_list != NULL)
7718 			ill_unlock_ills(ill_list, cnt);
7719 		rw_exit(&ipst->ips_ill_g_lock);
7720 		if (ill_list != NULL)
7721 			kmem_free(ill_list, ill_list_size);
7722 		ill_list = NULL;
7723 		ill_list_size = 0;
7724 		cnt = 0;
7725 		goto again;
7726 	}
7727 
7728 	/*
7729 	 * Split only if no ioctl is pending and if memory alloc succeeded
7730 	 * above.
7731 	 */
7732 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7733 	    ill_list != NULL) {
7734 		/*
7735 		 * No new ill can join this ipsq since we are holding the
7736 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7737 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7738 		 * If so we will retry on the next ipsq_exit.
7739 		 */
7740 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7741 	}
7742 
7743 	/*
7744 	 * We are holding the ipsq lock, hence no new messages can
7745 	 * land up on the ipsq, and there are no messages currently.
7746 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7747 	 * atomically while holding ill locks.
7748 	 */
7749 	ipsq->ipsq_writer = NULL;
7750 	ipsq->ipsq_reentry_cnt--;
7751 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7752 #ifdef DEBUG
7753 	ipsq->ipsq_depth = 0;
7754 #endif
7755 	mutex_exit(&ipsq->ipsq_lock);
7756 	/*
7757 	 * For IPMP this should wake up all ills in this ipsq.
7758 	 * We need to hold the ill_lock while waking up waiters to
7759 	 * avoid missed wakeups. But there is no need to acquire all
7760 	 * the ill locks and then wakeup. If we have not acquired all
7761 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7762 	 * wakes up ills one at a time after getting the right ill_lock
7763 	 */
7764 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7765 	if (ill_list != NULL)
7766 		ill_unlock_ills(ill_list, cnt);
7767 	if (ipsq->ipsq_refs == 0)
7768 		need_ipsq_free = B_TRUE;
7769 	rw_exit(&ipst->ips_ill_g_lock);
7770 	if (ill_list != 0)
7771 		kmem_free(ill_list, ill_list_size);
7772 
7773 	if (need_ipsq_free) {
7774 		/*
7775 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7776 		 * looked up. ipsq can be looked up only thru ill or phyint
7777 		 * and there are no ills/phyint on this ipsq.
7778 		 */
7779 		ipsq_delete(ipsq);
7780 	}
7781 
7782 	/*
7783 	 * Now that we're outside the IPSQ, start any IGMP/MLD timers.  We
7784 	 * can't start these inside the IPSQ since e.g. igmp_start_timers() ->
7785 	 * untimeout() (inside the IPSQ, waiting for an executing timeout to
7786 	 * finish) could deadlock with igmp_timeout_handler() -> ipsq_enter()
7787 	 * (executing the timeout, waiting to get inside the IPSQ).
7788 	 *
7789 	 * However, there is one exception to the above: if this thread *is*
7790 	 * the IGMP/MLD timeout handler thread, then we must not start its
7791 	 * timer until the current handler is done.
7792 	 */
7793 	mutex_enter(&ipst->ips_igmp_timer_lock);
7794 	if (curthread != ipst->ips_igmp_timer_thread) {
7795 		next = ipst->ips_igmp_deferred_next;
7796 		ipst->ips_igmp_deferred_next = INFINITY;
7797 		mutex_exit(&ipst->ips_igmp_timer_lock);
7798 
7799 		if (next != INFINITY)
7800 			igmp_start_timers(next, ipst);
7801 	} else {
7802 		mutex_exit(&ipst->ips_igmp_timer_lock);
7803 	}
7804 
7805 	mutex_enter(&ipst->ips_mld_timer_lock);
7806 	if (curthread != ipst->ips_mld_timer_thread) {
7807 		next = ipst->ips_mld_deferred_next;
7808 		ipst->ips_mld_deferred_next = INFINITY;
7809 		mutex_exit(&ipst->ips_mld_timer_lock);
7810 
7811 		if (next != INFINITY)
7812 			mld_start_timers(next, ipst);
7813 	} else {
7814 		mutex_exit(&ipst->ips_mld_timer_lock);
7815 	}
7816 }
7817 
7818 /*
7819  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7820  * and `ioccmd'.
7821  */
7822 void
7823 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
7824 {
7825 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7826 
7827 	mutex_enter(&ipsq->ipsq_lock);
7828 	ASSERT(ipsq->ipsq_current_ipif == NULL);
7829 	ASSERT(ipsq->ipsq_current_ioctl == 0);
7830 	ipsq->ipsq_current_done = B_FALSE;
7831 	ipsq->ipsq_current_ipif = ipif;
7832 	ipsq->ipsq_current_ioctl = ioccmd;
7833 	mutex_exit(&ipsq->ipsq_lock);
7834 }
7835 
7836 /*
7837  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
7838  * the next exclusive operation to begin once we ipsq_exit().  However, if
7839  * pending DLPI operations remain, then we will wait for the queue to drain
7840  * before allowing the next exclusive operation to begin.  This ensures that
7841  * DLPI operations from one exclusive operation are never improperly processed
7842  * as part of a subsequent exclusive operation.
7843  */
7844 void
7845 ipsq_current_finish(ipsq_t *ipsq)
7846 {
7847 	ipif_t *ipif = ipsq->ipsq_current_ipif;
7848 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
7849 
7850 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7851 
7852 	/*
7853 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
7854 	 * (but in that case, IPIF_CHANGING will already be clear and no
7855 	 * pending DLPI messages can remain).
7856 	 */
7857 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
7858 		ill_t *ill = ipif->ipif_ill;
7859 
7860 		mutex_enter(&ill->ill_lock);
7861 		dlpi_pending = ill->ill_dlpi_pending;
7862 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
7863 		mutex_exit(&ill->ill_lock);
7864 	}
7865 
7866 	mutex_enter(&ipsq->ipsq_lock);
7867 	ipsq->ipsq_current_ioctl = 0;
7868 	ipsq->ipsq_current_done = B_TRUE;
7869 	if (dlpi_pending == DL_PRIM_INVAL)
7870 		ipsq->ipsq_current_ipif = NULL;
7871 	mutex_exit(&ipsq->ipsq_lock);
7872 }
7873 
7874 /*
7875  * The ill is closing. Flush all messages on the ipsq that originated
7876  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7877  * for this ill since ipsq_enter could not have entered until then.
7878  * New messages can't be queued since the CONDEMNED flag is set.
7879  */
7880 static void
7881 ipsq_flush(ill_t *ill)
7882 {
7883 	queue_t	*q;
7884 	mblk_t	*prev;
7885 	mblk_t	*mp;
7886 	mblk_t	*mp_next;
7887 	ipsq_t	*ipsq;
7888 
7889 	ASSERT(IAM_WRITER_ILL(ill));
7890 	ipsq = ill->ill_phyint->phyint_ipsq;
7891 	/*
7892 	 * Flush any messages sent up by the driver.
7893 	 */
7894 	mutex_enter(&ipsq->ipsq_lock);
7895 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
7896 		mp_next = mp->b_next;
7897 		q = mp->b_queue;
7898 		if (q == ill->ill_rq || q == ill->ill_wq) {
7899 			/* Remove the mp from the ipsq */
7900 			if (prev == NULL)
7901 				ipsq->ipsq_mphead = mp->b_next;
7902 			else
7903 				prev->b_next = mp->b_next;
7904 			if (ipsq->ipsq_mptail == mp) {
7905 				ASSERT(mp_next == NULL);
7906 				ipsq->ipsq_mptail = prev;
7907 			}
7908 			inet_freemsg(mp);
7909 		} else {
7910 			prev = mp;
7911 		}
7912 	}
7913 	mutex_exit(&ipsq->ipsq_lock);
7914 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7915 	ipsq_xopq_mp_cleanup(ill, NULL);
7916 	ill_pending_mp_cleanup(ill);
7917 }
7918 
7919 /* ARGSUSED */
7920 int
7921 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7922     ip_ioctl_cmd_t *ipip, void *ifreq)
7923 {
7924 	ill_t	*ill;
7925 	struct lifreq	*lifr = (struct lifreq *)ifreq;
7926 	boolean_t isv6;
7927 	conn_t	*connp;
7928 	ip_stack_t	*ipst;
7929 
7930 	connp = Q_TO_CONN(q);
7931 	ipst = connp->conn_netstack->netstack_ip;
7932 	isv6 = connp->conn_af_isv6;
7933 	/*
7934 	 * Set original index.
7935 	 * Failover and failback move logical interfaces
7936 	 * from one physical interface to another.  The
7937 	 * original index indicates the parent of a logical
7938 	 * interface, in other words, the physical interface
7939 	 * the logical interface will be moved back to on
7940 	 * failback.
7941 	 */
7942 
7943 	/*
7944 	 * Don't allow the original index to be changed
7945 	 * for non-failover addresses, autoconfigured
7946 	 * addresses, or IPv6 link local addresses.
7947 	 */
7948 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
7949 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
7950 		return (EINVAL);
7951 	}
7952 	/*
7953 	 * The new original index must be in use by some
7954 	 * physical interface.
7955 	 */
7956 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
7957 	    NULL, NULL, ipst);
7958 	if (ill == NULL)
7959 		return (ENXIO);
7960 	ill_refrele(ill);
7961 
7962 	ipif->ipif_orig_ifindex = lifr->lifr_index;
7963 	/*
7964 	 * When this ipif gets failed back, don't
7965 	 * preserve the original id, as it is no
7966 	 * longer applicable.
7967 	 */
7968 	ipif->ipif_orig_ipifid = 0;
7969 	/*
7970 	 * For IPv4, change the original index of any
7971 	 * multicast addresses associated with the
7972 	 * ipif to the new value.
7973 	 */
7974 	if (!isv6) {
7975 		ilm_t *ilm;
7976 
7977 		mutex_enter(&ipif->ipif_ill->ill_lock);
7978 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
7979 		    ilm = ilm->ilm_next) {
7980 			if (ilm->ilm_ipif == ipif) {
7981 				ilm->ilm_orig_ifindex = lifr->lifr_index;
7982 			}
7983 		}
7984 		mutex_exit(&ipif->ipif_ill->ill_lock);
7985 	}
7986 	return (0);
7987 }
7988 
7989 /* ARGSUSED */
7990 int
7991 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7992     ip_ioctl_cmd_t *ipip, void *ifreq)
7993 {
7994 	struct lifreq *lifr = (struct lifreq *)ifreq;
7995 
7996 	/*
7997 	 * Get the original interface index i.e the one
7998 	 * before FAILOVER if it ever happened.
7999 	 */
8000 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8001 	return (0);
8002 }
8003 
8004 /*
8005  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8006  * refhold and return the associated ipif
8007  */
8008 /* ARGSUSED */
8009 int
8010 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8011     cmd_info_t *ci, ipsq_func_t func)
8012 {
8013 	boolean_t exists;
8014 	struct iftun_req *ta;
8015 	ipif_t  *ipif;
8016 	ill_t   *ill;
8017 	boolean_t isv6;
8018 	mblk_t  *mp1;
8019 	int error;
8020 	conn_t  *connp;
8021 	ip_stack_t  *ipst;
8022 
8023 	/* Existence verified in ip_wput_nondata */
8024 	mp1 = mp->b_cont->b_cont;
8025 	ta = (struct iftun_req *)mp1->b_rptr;
8026 	/*
8027 	 * Null terminate the string to protect against buffer
8028 	 * overrun. String was generated by user code and may not
8029 	 * be trusted.
8030 	 */
8031 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8032 
8033 	connp = Q_TO_CONN(q);
8034 	isv6 = connp->conn_af_isv6;
8035 	ipst = connp->conn_netstack->netstack_ip;
8036 
8037 	/* Disallows implicit create */
8038 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8039 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8040 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8041 	if (ipif == NULL)
8042 		return (error);
8043 
8044 	if (ipif->ipif_id != 0) {
8045 		/*
8046 		 * We really don't want to set/get tunnel parameters
8047 		 * on virtual tunnel interfaces.  Only allow the
8048 		 * base tunnel to do these.
8049 		 */
8050 		ipif_refrele(ipif);
8051 		return (EINVAL);
8052 	}
8053 
8054 	/*
8055 	 * Send down to tunnel mod for ioctl processing.
8056 	 * Will finish ioctl in ip_rput_other().
8057 	 */
8058 	ill = ipif->ipif_ill;
8059 	if (ill->ill_net_type == IRE_LOOPBACK) {
8060 		ipif_refrele(ipif);
8061 		return (EOPNOTSUPP);
8062 	}
8063 
8064 	if (ill->ill_wq == NULL) {
8065 		ipif_refrele(ipif);
8066 		return (ENXIO);
8067 	}
8068 	/*
8069 	 * Mark the ioctl as coming from an IPv6 interface for
8070 	 * tun's convenience.
8071 	 */
8072 	if (ill->ill_isv6)
8073 		ta->ifta_flags |= 0x80000000;
8074 	ci->ci_ipif = ipif;
8075 	return (0);
8076 }
8077 
8078 /*
8079  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8080  * and return the associated ipif.
8081  * Return value:
8082  *	Non zero: An error has occurred. ci may not be filled out.
8083  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8084  *	a held ipif in ci.ci_ipif.
8085  */
8086 int
8087 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8088     cmd_info_t *ci, ipsq_func_t func)
8089 {
8090 	sin_t		*sin;
8091 	sin6_t		*sin6;
8092 	char		*name;
8093 	struct ifreq    *ifr;
8094 	struct lifreq    *lifr;
8095 	ipif_t		*ipif = NULL;
8096 	ill_t		*ill;
8097 	conn_t		*connp;
8098 	boolean_t	isv6;
8099 	boolean_t	exists;
8100 	int		err;
8101 	mblk_t		*mp1;
8102 	zoneid_t	zoneid;
8103 	ip_stack_t	*ipst;
8104 
8105 	if (q->q_next != NULL) {
8106 		ill = (ill_t *)q->q_ptr;
8107 		isv6 = ill->ill_isv6;
8108 		connp = NULL;
8109 		zoneid = ALL_ZONES;
8110 		ipst = ill->ill_ipst;
8111 	} else {
8112 		ill = NULL;
8113 		connp = Q_TO_CONN(q);
8114 		isv6 = connp->conn_af_isv6;
8115 		zoneid = connp->conn_zoneid;
8116 		if (zoneid == GLOBAL_ZONEID) {
8117 			/* global zone can access ipifs in all zones */
8118 			zoneid = ALL_ZONES;
8119 		}
8120 		ipst = connp->conn_netstack->netstack_ip;
8121 	}
8122 
8123 	/* Has been checked in ip_wput_nondata */
8124 	mp1 = mp->b_cont->b_cont;
8125 
8126 	if (ipip->ipi_cmd_type == IF_CMD) {
8127 		/* This a old style SIOC[GS]IF* command */
8128 		ifr = (struct ifreq *)mp1->b_rptr;
8129 		/*
8130 		 * Null terminate the string to protect against buffer
8131 		 * overrun. String was generated by user code and may not
8132 		 * be trusted.
8133 		 */
8134 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8135 		sin = (sin_t *)&ifr->ifr_addr;
8136 		name = ifr->ifr_name;
8137 		ci->ci_sin = sin;
8138 		ci->ci_sin6 = NULL;
8139 		ci->ci_lifr = (struct lifreq *)ifr;
8140 	} else {
8141 		/* This a new style SIOC[GS]LIF* command */
8142 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8143 		lifr = (struct lifreq *)mp1->b_rptr;
8144 		/*
8145 		 * Null terminate the string to protect against buffer
8146 		 * overrun. String was generated by user code and may not
8147 		 * be trusted.
8148 		 */
8149 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8150 		name = lifr->lifr_name;
8151 		sin = (sin_t *)&lifr->lifr_addr;
8152 		sin6 = (sin6_t *)&lifr->lifr_addr;
8153 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8154 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8155 			    LIFNAMSIZ);
8156 		}
8157 		ci->ci_sin = sin;
8158 		ci->ci_sin6 = sin6;
8159 		ci->ci_lifr = lifr;
8160 	}
8161 
8162 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8163 		/*
8164 		 * The ioctl will be failed if the ioctl comes down
8165 		 * an conn stream
8166 		 */
8167 		if (ill == NULL) {
8168 			/*
8169 			 * Not an ill queue, return EINVAL same as the
8170 			 * old error code.
8171 			 */
8172 			return (ENXIO);
8173 		}
8174 		ipif = ill->ill_ipif;
8175 		ipif_refhold(ipif);
8176 	} else {
8177 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8178 		    &exists, isv6, zoneid,
8179 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8180 		    ipst);
8181 		if (ipif == NULL) {
8182 			if (err == EINPROGRESS)
8183 				return (err);
8184 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8185 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8186 				/*
8187 				 * Need to try both v4 and v6 since this
8188 				 * ioctl can come down either v4 or v6
8189 				 * socket. The lifreq.lifr_family passed
8190 				 * down by this ioctl is AF_UNSPEC.
8191 				 */
8192 				ipif = ipif_lookup_on_name(name,
8193 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8194 				    zoneid, (connp == NULL) ? q :
8195 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8196 				if (err == EINPROGRESS)
8197 					return (err);
8198 			}
8199 			err = 0;	/* Ensure we don't use it below */
8200 		}
8201 	}
8202 
8203 	/*
8204 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8205 	 */
8206 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8207 		ipif_refrele(ipif);
8208 		return (ENXIO);
8209 	}
8210 
8211 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8212 	    name[0] == '\0') {
8213 		/*
8214 		 * Handle a or a SIOC?IF* with a null name
8215 		 * during plumb (on the ill queue before the I_PLINK).
8216 		 */
8217 		ipif = ill->ill_ipif;
8218 		ipif_refhold(ipif);
8219 	}
8220 
8221 	if (ipif == NULL)
8222 		return (ENXIO);
8223 
8224 	/*
8225 	 * Allow only GET operations if this ipif has been created
8226 	 * temporarily due to a MOVE operation.
8227 	 */
8228 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8229 		ipif_refrele(ipif);
8230 		return (EINVAL);
8231 	}
8232 
8233 	ci->ci_ipif = ipif;
8234 	return (0);
8235 }
8236 
8237 /*
8238  * Return the total number of ipifs.
8239  */
8240 static uint_t
8241 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8242 {
8243 	uint_t numifs = 0;
8244 	ill_t	*ill;
8245 	ill_walk_context_t	ctx;
8246 	ipif_t	*ipif;
8247 
8248 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8249 	ill = ILL_START_WALK_V4(&ctx, ipst);
8250 
8251 	while (ill != NULL) {
8252 		for (ipif = ill->ill_ipif; ipif != NULL;
8253 		    ipif = ipif->ipif_next) {
8254 			if (ipif->ipif_zoneid == zoneid ||
8255 			    ipif->ipif_zoneid == ALL_ZONES)
8256 				numifs++;
8257 		}
8258 		ill = ill_next(&ctx, ill);
8259 	}
8260 	rw_exit(&ipst->ips_ill_g_lock);
8261 	return (numifs);
8262 }
8263 
8264 /*
8265  * Return the total number of ipifs.
8266  */
8267 static uint_t
8268 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8269 {
8270 	uint_t numifs = 0;
8271 	ill_t	*ill;
8272 	ipif_t	*ipif;
8273 	ill_walk_context_t	ctx;
8274 
8275 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8276 
8277 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8278 	if (family == AF_INET)
8279 		ill = ILL_START_WALK_V4(&ctx, ipst);
8280 	else if (family == AF_INET6)
8281 		ill = ILL_START_WALK_V6(&ctx, ipst);
8282 	else
8283 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8284 
8285 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8286 		for (ipif = ill->ill_ipif; ipif != NULL;
8287 		    ipif = ipif->ipif_next) {
8288 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8289 			    !(lifn_flags & LIFC_NOXMIT))
8290 				continue;
8291 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8292 			    !(lifn_flags & LIFC_TEMPORARY))
8293 				continue;
8294 			if (((ipif->ipif_flags &
8295 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8296 			    IPIF_DEPRECATED)) ||
8297 			    IS_LOOPBACK(ill) ||
8298 			    !(ipif->ipif_flags & IPIF_UP)) &&
8299 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8300 				continue;
8301 
8302 			if (zoneid != ipif->ipif_zoneid &&
8303 			    ipif->ipif_zoneid != ALL_ZONES &&
8304 			    (zoneid != GLOBAL_ZONEID ||
8305 			    !(lifn_flags & LIFC_ALLZONES)))
8306 				continue;
8307 
8308 			numifs++;
8309 		}
8310 	}
8311 	rw_exit(&ipst->ips_ill_g_lock);
8312 	return (numifs);
8313 }
8314 
8315 uint_t
8316 ip_get_lifsrcofnum(ill_t *ill)
8317 {
8318 	uint_t numifs = 0;
8319 	ill_t	*ill_head = ill;
8320 	ip_stack_t	*ipst = ill->ill_ipst;
8321 
8322 	/*
8323 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8324 	 * other thread may be trying to relink the ILLs in this usesrc group
8325 	 * and adjusting the ill_usesrc_grp_next pointers
8326 	 */
8327 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8328 	if ((ill->ill_usesrc_ifindex == 0) &&
8329 	    (ill->ill_usesrc_grp_next != NULL)) {
8330 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8331 		    ill = ill->ill_usesrc_grp_next)
8332 			numifs++;
8333 	}
8334 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8335 
8336 	return (numifs);
8337 }
8338 
8339 /* Null values are passed in for ipif, sin, and ifreq */
8340 /* ARGSUSED */
8341 int
8342 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8343     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8344 {
8345 	int *nump;
8346 	conn_t *connp = Q_TO_CONN(q);
8347 
8348 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8349 
8350 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8351 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8352 
8353 	*nump = ip_get_numifs(connp->conn_zoneid,
8354 	    connp->conn_netstack->netstack_ip);
8355 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8356 	return (0);
8357 }
8358 
8359 /* Null values are passed in for ipif, sin, and ifreq */
8360 /* ARGSUSED */
8361 int
8362 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8363     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8364 {
8365 	struct lifnum *lifn;
8366 	mblk_t	*mp1;
8367 	conn_t *connp = Q_TO_CONN(q);
8368 
8369 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8370 
8371 	/* Existence checked in ip_wput_nondata */
8372 	mp1 = mp->b_cont->b_cont;
8373 
8374 	lifn = (struct lifnum *)mp1->b_rptr;
8375 	switch (lifn->lifn_family) {
8376 	case AF_UNSPEC:
8377 	case AF_INET:
8378 	case AF_INET6:
8379 		break;
8380 	default:
8381 		return (EAFNOSUPPORT);
8382 	}
8383 
8384 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8385 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8386 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8387 	return (0);
8388 }
8389 
8390 /* ARGSUSED */
8391 int
8392 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8393     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8394 {
8395 	STRUCT_HANDLE(ifconf, ifc);
8396 	mblk_t *mp1;
8397 	struct iocblk *iocp;
8398 	struct ifreq *ifr;
8399 	ill_walk_context_t	ctx;
8400 	ill_t	*ill;
8401 	ipif_t	*ipif;
8402 	struct sockaddr_in *sin;
8403 	int32_t	ifclen;
8404 	zoneid_t zoneid;
8405 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8406 
8407 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8408 
8409 	ip1dbg(("ip_sioctl_get_ifconf"));
8410 	/* Existence verified in ip_wput_nondata */
8411 	mp1 = mp->b_cont->b_cont;
8412 	iocp = (struct iocblk *)mp->b_rptr;
8413 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8414 
8415 	/*
8416 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8417 	 * the user buffer address and length into which the list of struct
8418 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8419 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8420 	 * the SIOCGIFCONF operation was redefined to simply provide
8421 	 * a large output buffer into which we are supposed to jam the ifreq
8422 	 * array.  The same ioctl command code was used, despite the fact that
8423 	 * both the applications and the kernel code had to change, thus making
8424 	 * it impossible to support both interfaces.
8425 	 *
8426 	 * For reasons not good enough to try to explain, the following
8427 	 * algorithm is used for deciding what to do with one of these:
8428 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8429 	 * form with the output buffer coming down as the continuation message.
8430 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8431 	 * and we have to copy in the ifconf structure to find out how big the
8432 	 * output buffer is and where to copy out to.  Sure no problem...
8433 	 *
8434 	 */
8435 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8436 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8437 		int numifs = 0;
8438 		size_t ifc_bufsize;
8439 
8440 		/*
8441 		 * Must be (better be!) continuation of a TRANSPARENT
8442 		 * IOCTL.  We just copied in the ifconf structure.
8443 		 */
8444 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8445 		    (struct ifconf *)mp1->b_rptr);
8446 
8447 		/*
8448 		 * Allocate a buffer to hold requested information.
8449 		 *
8450 		 * If ifc_len is larger than what is needed, we only
8451 		 * allocate what we will use.
8452 		 *
8453 		 * If ifc_len is smaller than what is needed, return
8454 		 * EINVAL.
8455 		 *
8456 		 * XXX: the ill_t structure can hava 2 counters, for
8457 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8458 		 * number of interfaces for a device, so we don't need
8459 		 * to count them here...
8460 		 */
8461 		numifs = ip_get_numifs(zoneid, ipst);
8462 
8463 		ifclen = STRUCT_FGET(ifc, ifc_len);
8464 		ifc_bufsize = numifs * sizeof (struct ifreq);
8465 		if (ifc_bufsize > ifclen) {
8466 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8467 				/* old behaviour */
8468 				return (EINVAL);
8469 			} else {
8470 				ifc_bufsize = ifclen;
8471 			}
8472 		}
8473 
8474 		mp1 = mi_copyout_alloc(q, mp,
8475 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8476 		if (mp1 == NULL)
8477 			return (ENOMEM);
8478 
8479 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8480 	}
8481 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8482 	/*
8483 	 * the SIOCGIFCONF ioctl only knows about
8484 	 * IPv4 addresses, so don't try to tell
8485 	 * it about interfaces with IPv6-only
8486 	 * addresses. (Last parm 'isv6' is B_FALSE)
8487 	 */
8488 
8489 	ifr = (struct ifreq *)mp1->b_rptr;
8490 
8491 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8492 	ill = ILL_START_WALK_V4(&ctx, ipst);
8493 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8494 		for (ipif = ill->ill_ipif; ipif != NULL;
8495 		    ipif = ipif->ipif_next) {
8496 			if (zoneid != ipif->ipif_zoneid &&
8497 			    ipif->ipif_zoneid != ALL_ZONES)
8498 				continue;
8499 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8500 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8501 					/* old behaviour */
8502 					rw_exit(&ipst->ips_ill_g_lock);
8503 					return (EINVAL);
8504 				} else {
8505 					goto if_copydone;
8506 				}
8507 			}
8508 			ipif_get_name(ipif, ifr->ifr_name,
8509 			    sizeof (ifr->ifr_name));
8510 			sin = (sin_t *)&ifr->ifr_addr;
8511 			*sin = sin_null;
8512 			sin->sin_family = AF_INET;
8513 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8514 			ifr++;
8515 		}
8516 	}
8517 if_copydone:
8518 	rw_exit(&ipst->ips_ill_g_lock);
8519 	mp1->b_wptr = (uchar_t *)ifr;
8520 
8521 	if (STRUCT_BUF(ifc) != NULL) {
8522 		STRUCT_FSET(ifc, ifc_len,
8523 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8524 	}
8525 	return (0);
8526 }
8527 
8528 /*
8529  * Get the interfaces using the address hosted on the interface passed in,
8530  * as a source adddress
8531  */
8532 /* ARGSUSED */
8533 int
8534 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8535     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8536 {
8537 	mblk_t *mp1;
8538 	ill_t	*ill, *ill_head;
8539 	ipif_t	*ipif, *orig_ipif;
8540 	int	numlifs = 0;
8541 	size_t	lifs_bufsize, lifsmaxlen;
8542 	struct	lifreq *lifr;
8543 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8544 	uint_t	ifindex;
8545 	zoneid_t zoneid;
8546 	int err = 0;
8547 	boolean_t isv6 = B_FALSE;
8548 	struct	sockaddr_in	*sin;
8549 	struct	sockaddr_in6	*sin6;
8550 	STRUCT_HANDLE(lifsrcof, lifs);
8551 	ip_stack_t		*ipst;
8552 
8553 	ipst = CONNQ_TO_IPST(q);
8554 
8555 	ASSERT(q->q_next == NULL);
8556 
8557 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8558 
8559 	/* Existence verified in ip_wput_nondata */
8560 	mp1 = mp->b_cont->b_cont;
8561 
8562 	/*
8563 	 * Must be (better be!) continuation of a TRANSPARENT
8564 	 * IOCTL.  We just copied in the lifsrcof structure.
8565 	 */
8566 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8567 	    (struct lifsrcof *)mp1->b_rptr);
8568 
8569 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8570 		return (EINVAL);
8571 
8572 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8573 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8574 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8575 	    ip_process_ioctl, &err, ipst);
8576 	if (ipif == NULL) {
8577 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8578 		    ifindex));
8579 		return (err);
8580 	}
8581 
8582 	/* Allocate a buffer to hold requested information */
8583 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8584 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8585 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8586 	/* The actual size needed is always returned in lifs_len */
8587 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8588 
8589 	/* If the amount we need is more than what is passed in, abort */
8590 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8591 		ipif_refrele(ipif);
8592 		return (0);
8593 	}
8594 
8595 	mp1 = mi_copyout_alloc(q, mp,
8596 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8597 	if (mp1 == NULL) {
8598 		ipif_refrele(ipif);
8599 		return (ENOMEM);
8600 	}
8601 
8602 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8603 	bzero(mp1->b_rptr, lifs_bufsize);
8604 
8605 	lifr = (struct lifreq *)mp1->b_rptr;
8606 
8607 	ill = ill_head = ipif->ipif_ill;
8608 	orig_ipif = ipif;
8609 
8610 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8611 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8612 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8613 
8614 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8615 	for (; (ill != NULL) && (ill != ill_head);
8616 	    ill = ill->ill_usesrc_grp_next) {
8617 
8618 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8619 			break;
8620 
8621 		ipif = ill->ill_ipif;
8622 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8623 		if (ipif->ipif_isv6) {
8624 			sin6 = (sin6_t *)&lifr->lifr_addr;
8625 			*sin6 = sin6_null;
8626 			sin6->sin6_family = AF_INET6;
8627 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8628 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8629 			    &ipif->ipif_v6net_mask);
8630 		} else {
8631 			sin = (sin_t *)&lifr->lifr_addr;
8632 			*sin = sin_null;
8633 			sin->sin_family = AF_INET;
8634 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8635 			lifr->lifr_addrlen = ip_mask_to_plen(
8636 			    ipif->ipif_net_mask);
8637 		}
8638 		lifr++;
8639 	}
8640 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8641 	rw_exit(&ipst->ips_ill_g_lock);
8642 	ipif_refrele(orig_ipif);
8643 	mp1->b_wptr = (uchar_t *)lifr;
8644 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8645 
8646 	return (0);
8647 }
8648 
8649 /* ARGSUSED */
8650 int
8651 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8652     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8653 {
8654 	mblk_t *mp1;
8655 	int	list;
8656 	ill_t	*ill;
8657 	ipif_t	*ipif;
8658 	int	flags;
8659 	int	numlifs = 0;
8660 	size_t	lifc_bufsize;
8661 	struct	lifreq *lifr;
8662 	sa_family_t	family;
8663 	struct	sockaddr_in	*sin;
8664 	struct	sockaddr_in6	*sin6;
8665 	ill_walk_context_t	ctx;
8666 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8667 	int32_t	lifclen;
8668 	zoneid_t zoneid;
8669 	STRUCT_HANDLE(lifconf, lifc);
8670 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8671 
8672 	ip1dbg(("ip_sioctl_get_lifconf"));
8673 
8674 	ASSERT(q->q_next == NULL);
8675 
8676 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8677 
8678 	/* Existence verified in ip_wput_nondata */
8679 	mp1 = mp->b_cont->b_cont;
8680 
8681 	/*
8682 	 * An extended version of SIOCGIFCONF that takes an
8683 	 * additional address family and flags field.
8684 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8685 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8686 	 * interfaces are omitted.
8687 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8688 	 * unless LIFC_TEMPORARY is specified.
8689 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8690 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8691 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8692 	 * has priority over LIFC_NOXMIT.
8693 	 */
8694 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8695 
8696 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8697 		return (EINVAL);
8698 
8699 	/*
8700 	 * Must be (better be!) continuation of a TRANSPARENT
8701 	 * IOCTL.  We just copied in the lifconf structure.
8702 	 */
8703 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8704 
8705 	family = STRUCT_FGET(lifc, lifc_family);
8706 	flags = STRUCT_FGET(lifc, lifc_flags);
8707 
8708 	switch (family) {
8709 	case AF_UNSPEC:
8710 		/*
8711 		 * walk all ILL's.
8712 		 */
8713 		list = MAX_G_HEADS;
8714 		break;
8715 	case AF_INET:
8716 		/*
8717 		 * walk only IPV4 ILL's.
8718 		 */
8719 		list = IP_V4_G_HEAD;
8720 		break;
8721 	case AF_INET6:
8722 		/*
8723 		 * walk only IPV6 ILL's.
8724 		 */
8725 		list = IP_V6_G_HEAD;
8726 		break;
8727 	default:
8728 		return (EAFNOSUPPORT);
8729 	}
8730 
8731 	/*
8732 	 * Allocate a buffer to hold requested information.
8733 	 *
8734 	 * If lifc_len is larger than what is needed, we only
8735 	 * allocate what we will use.
8736 	 *
8737 	 * If lifc_len is smaller than what is needed, return
8738 	 * EINVAL.
8739 	 */
8740 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8741 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8742 	lifclen = STRUCT_FGET(lifc, lifc_len);
8743 	if (lifc_bufsize > lifclen) {
8744 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8745 			return (EINVAL);
8746 		else
8747 			lifc_bufsize = lifclen;
8748 	}
8749 
8750 	mp1 = mi_copyout_alloc(q, mp,
8751 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8752 	if (mp1 == NULL)
8753 		return (ENOMEM);
8754 
8755 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8756 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8757 
8758 	lifr = (struct lifreq *)mp1->b_rptr;
8759 
8760 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8761 	ill = ill_first(list, list, &ctx, ipst);
8762 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8763 		for (ipif = ill->ill_ipif; ipif != NULL;
8764 		    ipif = ipif->ipif_next) {
8765 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8766 			    !(flags & LIFC_NOXMIT))
8767 				continue;
8768 
8769 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8770 			    !(flags & LIFC_TEMPORARY))
8771 				continue;
8772 
8773 			if (((ipif->ipif_flags &
8774 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8775 			    IPIF_DEPRECATED)) ||
8776 			    IS_LOOPBACK(ill) ||
8777 			    !(ipif->ipif_flags & IPIF_UP)) &&
8778 			    (flags & LIFC_EXTERNAL_SOURCE))
8779 				continue;
8780 
8781 			if (zoneid != ipif->ipif_zoneid &&
8782 			    ipif->ipif_zoneid != ALL_ZONES &&
8783 			    (zoneid != GLOBAL_ZONEID ||
8784 			    !(flags & LIFC_ALLZONES)))
8785 				continue;
8786 
8787 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8788 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8789 					rw_exit(&ipst->ips_ill_g_lock);
8790 					return (EINVAL);
8791 				} else {
8792 					goto lif_copydone;
8793 				}
8794 			}
8795 
8796 			ipif_get_name(ipif, lifr->lifr_name,
8797 			    sizeof (lifr->lifr_name));
8798 			if (ipif->ipif_isv6) {
8799 				sin6 = (sin6_t *)&lifr->lifr_addr;
8800 				*sin6 = sin6_null;
8801 				sin6->sin6_family = AF_INET6;
8802 				sin6->sin6_addr =
8803 				    ipif->ipif_v6lcl_addr;
8804 				lifr->lifr_addrlen =
8805 				    ip_mask_to_plen_v6(
8806 				    &ipif->ipif_v6net_mask);
8807 			} else {
8808 				sin = (sin_t *)&lifr->lifr_addr;
8809 				*sin = sin_null;
8810 				sin->sin_family = AF_INET;
8811 				sin->sin_addr.s_addr =
8812 				    ipif->ipif_lcl_addr;
8813 				lifr->lifr_addrlen =
8814 				    ip_mask_to_plen(
8815 				    ipif->ipif_net_mask);
8816 			}
8817 			lifr++;
8818 		}
8819 	}
8820 lif_copydone:
8821 	rw_exit(&ipst->ips_ill_g_lock);
8822 
8823 	mp1->b_wptr = (uchar_t *)lifr;
8824 	if (STRUCT_BUF(lifc) != NULL) {
8825 		STRUCT_FSET(lifc, lifc_len,
8826 		    (int)((uchar_t *)lifr - mp1->b_rptr));
8827 	}
8828 	return (0);
8829 }
8830 
8831 /* ARGSUSED */
8832 int
8833 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
8834     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8835 {
8836 	ip_stack_t	*ipst;
8837 
8838 	if (q->q_next == NULL)
8839 		ipst = CONNQ_TO_IPST(q);
8840 	else
8841 		ipst = ILLQ_TO_IPST(q);
8842 
8843 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8844 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
8845 	return (0);
8846 }
8847 
8848 static void
8849 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8850 {
8851 	ip6_asp_t *table;
8852 	size_t table_size;
8853 	mblk_t *data_mp;
8854 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8855 	ip_stack_t	*ipst;
8856 
8857 	if (q->q_next == NULL)
8858 		ipst = CONNQ_TO_IPST(q);
8859 	else
8860 		ipst = ILLQ_TO_IPST(q);
8861 
8862 	/* These two ioctls are I_STR only */
8863 	if (iocp->ioc_count == TRANSPARENT) {
8864 		miocnak(q, mp, 0, EINVAL);
8865 		return;
8866 	}
8867 
8868 	data_mp = mp->b_cont;
8869 	if (data_mp == NULL) {
8870 		/* The user passed us a NULL argument */
8871 		table = NULL;
8872 		table_size = iocp->ioc_count;
8873 	} else {
8874 		/*
8875 		 * The user provided a table.  The stream head
8876 		 * may have copied in the user data in chunks,
8877 		 * so make sure everything is pulled up
8878 		 * properly.
8879 		 */
8880 		if (MBLKL(data_mp) < iocp->ioc_count) {
8881 			mblk_t *new_data_mp;
8882 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8883 			    NULL) {
8884 				miocnak(q, mp, 0, ENOMEM);
8885 				return;
8886 			}
8887 			freemsg(data_mp);
8888 			data_mp = new_data_mp;
8889 			mp->b_cont = data_mp;
8890 		}
8891 		table = (ip6_asp_t *)data_mp->b_rptr;
8892 		table_size = iocp->ioc_count;
8893 	}
8894 
8895 	switch (iocp->ioc_cmd) {
8896 	case SIOCGIP6ADDRPOLICY:
8897 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
8898 		if (iocp->ioc_rval == -1)
8899 			iocp->ioc_error = EINVAL;
8900 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8901 		else if (table != NULL &&
8902 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8903 			ip6_asp_t *src = table;
8904 			ip6_asp32_t *dst = (void *)table;
8905 			int count = table_size / sizeof (ip6_asp_t);
8906 			int i;
8907 
8908 			/*
8909 			 * We need to do an in-place shrink of the array
8910 			 * to match the alignment attributes of the
8911 			 * 32-bit ABI looking at it.
8912 			 */
8913 			/* LINTED: logical expression always true: op "||" */
8914 			ASSERT(sizeof (*src) > sizeof (*dst));
8915 			for (i = 1; i < count; i++)
8916 				bcopy(src + i, dst + i, sizeof (*dst));
8917 		}
8918 #endif
8919 		break;
8920 
8921 	case SIOCSIP6ADDRPOLICY:
8922 		ASSERT(mp->b_prev == NULL);
8923 		mp->b_prev = (void *)q;
8924 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8925 		/*
8926 		 * We pass in the datamodel here so that the ip6_asp_replace()
8927 		 * routine can handle converting from 32-bit to native formats
8928 		 * where necessary.
8929 		 *
8930 		 * A better way to handle this might be to convert the inbound
8931 		 * data structure here, and hang it off a new 'mp'; thus the
8932 		 * ip6_asp_replace() logic would always be dealing with native
8933 		 * format data structures..
8934 		 *
8935 		 * (An even simpler way to handle these ioctls is to just
8936 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
8937 		 * and just recompile everything that depends on it.)
8938 		 */
8939 #endif
8940 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
8941 		    iocp->ioc_flag & IOC_MODELS);
8942 		return;
8943 	}
8944 
8945 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
8946 	qreply(q, mp);
8947 }
8948 
8949 static void
8950 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
8951 {
8952 	mblk_t 		*data_mp;
8953 	struct dstinforeq	*dir;
8954 	uint8_t		*end, *cur;
8955 	in6_addr_t	*daddr, *saddr;
8956 	ipaddr_t	v4daddr;
8957 	ire_t		*ire;
8958 	char		*slabel, *dlabel;
8959 	boolean_t	isipv4;
8960 	int		match_ire;
8961 	ill_t		*dst_ill;
8962 	ipif_t		*src_ipif, *ire_ipif;
8963 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8964 	zoneid_t	zoneid;
8965 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
8966 
8967 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8968 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8969 
8970 	/*
8971 	 * This ioctl is I_STR only, and must have a
8972 	 * data mblk following the M_IOCTL mblk.
8973 	 */
8974 	data_mp = mp->b_cont;
8975 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
8976 		miocnak(q, mp, 0, EINVAL);
8977 		return;
8978 	}
8979 
8980 	if (MBLKL(data_mp) < iocp->ioc_count) {
8981 		mblk_t *new_data_mp;
8982 
8983 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
8984 			miocnak(q, mp, 0, ENOMEM);
8985 			return;
8986 		}
8987 		freemsg(data_mp);
8988 		data_mp = new_data_mp;
8989 		mp->b_cont = data_mp;
8990 	}
8991 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
8992 
8993 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
8994 	    end - cur >= sizeof (struct dstinforeq);
8995 	    cur += sizeof (struct dstinforeq)) {
8996 		dir = (struct dstinforeq *)cur;
8997 		daddr = &dir->dir_daddr;
8998 		saddr = &dir->dir_saddr;
8999 
9000 		/*
9001 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9002 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9003 		 * and ipif_select_source[_v6]() do not.
9004 		 */
9005 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9006 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9007 
9008 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9009 		if (isipv4) {
9010 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9011 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9012 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9013 		} else {
9014 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9015 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9016 		}
9017 		if (ire == NULL) {
9018 			dir->dir_dreachable = 0;
9019 
9020 			/* move on to next dst addr */
9021 			continue;
9022 		}
9023 		dir->dir_dreachable = 1;
9024 
9025 		ire_ipif = ire->ire_ipif;
9026 		if (ire_ipif == NULL)
9027 			goto next_dst;
9028 
9029 		/*
9030 		 * We expect to get back an interface ire or a
9031 		 * gateway ire cache entry.  For both types, the
9032 		 * output interface is ire_ipif->ipif_ill.
9033 		 */
9034 		dst_ill = ire_ipif->ipif_ill;
9035 		dir->dir_dmactype = dst_ill->ill_mactype;
9036 
9037 		if (isipv4) {
9038 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9039 		} else {
9040 			src_ipif = ipif_select_source_v6(dst_ill,
9041 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9042 			    zoneid);
9043 		}
9044 		if (src_ipif == NULL)
9045 			goto next_dst;
9046 
9047 		*saddr = src_ipif->ipif_v6lcl_addr;
9048 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9049 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9050 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9051 		dir->dir_sdeprecated =
9052 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9053 		ipif_refrele(src_ipif);
9054 next_dst:
9055 		ire_refrele(ire);
9056 	}
9057 	miocack(q, mp, iocp->ioc_count, 0);
9058 }
9059 
9060 /*
9061  * Check if this is an address assigned to this machine.
9062  * Skips interfaces that are down by using ire checks.
9063  * Translates mapped addresses to v4 addresses and then
9064  * treats them as such, returning true if the v4 address
9065  * associated with this mapped address is configured.
9066  * Note: Applications will have to be careful what they do
9067  * with the response; use of mapped addresses limits
9068  * what can be done with the socket, especially with
9069  * respect to socket options and ioctls - neither IPv4
9070  * options nor IPv6 sticky options/ancillary data options
9071  * may be used.
9072  */
9073 /* ARGSUSED */
9074 int
9075 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9076     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9077 {
9078 	struct sioc_addrreq *sia;
9079 	sin_t *sin;
9080 	ire_t *ire;
9081 	mblk_t *mp1;
9082 	zoneid_t zoneid;
9083 	ip_stack_t	*ipst;
9084 
9085 	ip1dbg(("ip_sioctl_tmyaddr"));
9086 
9087 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9088 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9089 	ipst = CONNQ_TO_IPST(q);
9090 
9091 	/* Existence verified in ip_wput_nondata */
9092 	mp1 = mp->b_cont->b_cont;
9093 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9094 	sin = (sin_t *)&sia->sa_addr;
9095 	switch (sin->sin_family) {
9096 	case AF_INET6: {
9097 		sin6_t *sin6 = (sin6_t *)sin;
9098 
9099 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9100 			ipaddr_t v4_addr;
9101 
9102 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9103 			    v4_addr);
9104 			ire = ire_ctable_lookup(v4_addr, 0,
9105 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9106 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9107 		} else {
9108 			in6_addr_t v6addr;
9109 
9110 			v6addr = sin6->sin6_addr;
9111 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9112 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9113 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9114 		}
9115 		break;
9116 	}
9117 	case AF_INET: {
9118 		ipaddr_t v4addr;
9119 
9120 		v4addr = sin->sin_addr.s_addr;
9121 		ire = ire_ctable_lookup(v4addr, 0,
9122 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9123 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9124 		break;
9125 	}
9126 	default:
9127 		return (EAFNOSUPPORT);
9128 	}
9129 	if (ire != NULL) {
9130 		sia->sa_res = 1;
9131 		ire_refrele(ire);
9132 	} else {
9133 		sia->sa_res = 0;
9134 	}
9135 	return (0);
9136 }
9137 
9138 /*
9139  * Check if this is an address assigned on-link i.e. neighbor,
9140  * and makes sure it's reachable from the current zone.
9141  * Returns true for my addresses as well.
9142  * Translates mapped addresses to v4 addresses and then
9143  * treats them as such, returning true if the v4 address
9144  * associated with this mapped address is configured.
9145  * Note: Applications will have to be careful what they do
9146  * with the response; use of mapped addresses limits
9147  * what can be done with the socket, especially with
9148  * respect to socket options and ioctls - neither IPv4
9149  * options nor IPv6 sticky options/ancillary data options
9150  * may be used.
9151  */
9152 /* ARGSUSED */
9153 int
9154 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9155     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9156 {
9157 	struct sioc_addrreq *sia;
9158 	sin_t *sin;
9159 	mblk_t	*mp1;
9160 	ire_t *ire = NULL;
9161 	zoneid_t zoneid;
9162 	ip_stack_t	*ipst;
9163 
9164 	ip1dbg(("ip_sioctl_tonlink"));
9165 
9166 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9167 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9168 	ipst = CONNQ_TO_IPST(q);
9169 
9170 	/* Existence verified in ip_wput_nondata */
9171 	mp1 = mp->b_cont->b_cont;
9172 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9173 	sin = (sin_t *)&sia->sa_addr;
9174 
9175 	/*
9176 	 * Match addresses with a zero gateway field to avoid
9177 	 * routes going through a router.
9178 	 * Exclude broadcast and multicast addresses.
9179 	 */
9180 	switch (sin->sin_family) {
9181 	case AF_INET6: {
9182 		sin6_t *sin6 = (sin6_t *)sin;
9183 
9184 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9185 			ipaddr_t v4_addr;
9186 
9187 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9188 			    v4_addr);
9189 			if (!CLASSD(v4_addr)) {
9190 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9191 				    NULL, NULL, zoneid, NULL,
9192 				    MATCH_IRE_GW, ipst);
9193 			}
9194 		} else {
9195 			in6_addr_t v6addr;
9196 			in6_addr_t v6gw;
9197 
9198 			v6addr = sin6->sin6_addr;
9199 			v6gw = ipv6_all_zeros;
9200 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9201 				ire = ire_route_lookup_v6(&v6addr, 0,
9202 				    &v6gw, 0, NULL, NULL, zoneid,
9203 				    NULL, MATCH_IRE_GW, ipst);
9204 			}
9205 		}
9206 		break;
9207 	}
9208 	case AF_INET: {
9209 		ipaddr_t v4addr;
9210 
9211 		v4addr = sin->sin_addr.s_addr;
9212 		if (!CLASSD(v4addr)) {
9213 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9214 			    NULL, NULL, zoneid, NULL,
9215 			    MATCH_IRE_GW, ipst);
9216 		}
9217 		break;
9218 	}
9219 	default:
9220 		return (EAFNOSUPPORT);
9221 	}
9222 	sia->sa_res = 0;
9223 	if (ire != NULL) {
9224 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9225 		    IRE_LOCAL|IRE_LOOPBACK)) {
9226 			sia->sa_res = 1;
9227 		}
9228 		ire_refrele(ire);
9229 	}
9230 	return (0);
9231 }
9232 
9233 /*
9234  * TBD: implement when kernel maintaines a list of site prefixes.
9235  */
9236 /* ARGSUSED */
9237 int
9238 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9239     ip_ioctl_cmd_t *ipip, void *ifreq)
9240 {
9241 	return (ENXIO);
9242 }
9243 
9244 /* ARGSUSED */
9245 int
9246 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9247     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9248 {
9249 	ill_t		*ill;
9250 	mblk_t		*mp1;
9251 	conn_t		*connp;
9252 	boolean_t	success;
9253 
9254 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9255 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9256 	/* ioctl comes down on an conn */
9257 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9258 	connp = Q_TO_CONN(q);
9259 
9260 	mp->b_datap->db_type = M_IOCTL;
9261 
9262 	/*
9263 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9264 	 * The original mp contains contaminated b_next values due to 'mi',
9265 	 * which is needed to do the mi_copy_done. Unfortunately if we
9266 	 * send down the original mblk itself and if we are popped due to an
9267 	 * an unplumb before the response comes back from tunnel,
9268 	 * the streamhead (which does a freemsg) will see this contaminated
9269 	 * message and the assertion in freemsg about non-null b_next/b_prev
9270 	 * will panic a DEBUG kernel.
9271 	 */
9272 	mp1 = copymsg(mp);
9273 	if (mp1 == NULL)
9274 		return (ENOMEM);
9275 
9276 	ill = ipif->ipif_ill;
9277 	mutex_enter(&connp->conn_lock);
9278 	mutex_enter(&ill->ill_lock);
9279 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9280 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9281 		    mp, 0);
9282 	} else {
9283 		success = ill_pending_mp_add(ill, connp, mp);
9284 	}
9285 	mutex_exit(&ill->ill_lock);
9286 	mutex_exit(&connp->conn_lock);
9287 
9288 	if (success) {
9289 		ip1dbg(("sending down tunparam request "));
9290 		putnext(ill->ill_wq, mp1);
9291 		return (EINPROGRESS);
9292 	} else {
9293 		/* The conn has started closing */
9294 		freemsg(mp1);
9295 		return (EINTR);
9296 	}
9297 }
9298 
9299 /*
9300  * ARP IOCTLs.
9301  * How does IP get in the business of fronting ARP configuration/queries?
9302  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9303  * are by tradition passed in through a datagram socket.  That lands in IP.
9304  * As it happens, this is just as well since the interface is quite crude in
9305  * that it passes in no information about protocol or hardware types, or
9306  * interface association.  After making the protocol assumption, IP is in
9307  * the position to look up the name of the ILL, which ARP will need, and
9308  * format a request that can be handled by ARP.  The request is passed up
9309  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9310  * back a response.  ARP supports its own set of more general IOCTLs, in
9311  * case anyone is interested.
9312  */
9313 /* ARGSUSED */
9314 int
9315 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9316     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9317 {
9318 	mblk_t *mp1;
9319 	mblk_t *mp2;
9320 	mblk_t *pending_mp;
9321 	ipaddr_t ipaddr;
9322 	area_t *area;
9323 	struct iocblk *iocp;
9324 	conn_t *connp;
9325 	struct arpreq *ar;
9326 	struct xarpreq *xar;
9327 	int flags, alength;
9328 	char *lladdr;
9329 	ip_stack_t	*ipst;
9330 	ill_t *ill = ipif->ipif_ill;
9331 	boolean_t if_arp_ioctl = B_FALSE;
9332 
9333 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9334 	connp = Q_TO_CONN(q);
9335 	ipst = connp->conn_netstack->netstack_ip;
9336 
9337 	if (ipip->ipi_cmd_type == XARP_CMD) {
9338 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9339 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9340 		ar = NULL;
9341 
9342 		flags = xar->xarp_flags;
9343 		lladdr = LLADDR(&xar->xarp_ha);
9344 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9345 		/*
9346 		 * Validate against user's link layer address length
9347 		 * input and name and addr length limits.
9348 		 */
9349 		alength = ill->ill_phys_addr_length;
9350 		if (ipip->ipi_cmd == SIOCSXARP) {
9351 			if (alength != xar->xarp_ha.sdl_alen ||
9352 			    (alength + xar->xarp_ha.sdl_nlen >
9353 			    sizeof (xar->xarp_ha.sdl_data)))
9354 				return (EINVAL);
9355 		}
9356 	} else {
9357 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9358 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9359 		xar = NULL;
9360 
9361 		flags = ar->arp_flags;
9362 		lladdr = ar->arp_ha.sa_data;
9363 		/*
9364 		 * Theoretically, the sa_family could tell us what link
9365 		 * layer type this operation is trying to deal with. By
9366 		 * common usage AF_UNSPEC means ethernet. We'll assume
9367 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9368 		 * for now. Our new SIOC*XARP ioctls can be used more
9369 		 * generally.
9370 		 *
9371 		 * If the underlying media happens to have a non 6 byte
9372 		 * address, arp module will fail set/get, but the del
9373 		 * operation will succeed.
9374 		 */
9375 		alength = 6;
9376 		if ((ipip->ipi_cmd != SIOCDARP) &&
9377 		    (alength != ill->ill_phys_addr_length)) {
9378 			return (EINVAL);
9379 		}
9380 	}
9381 
9382 	/*
9383 	 * We are going to pass up to ARP a packet chain that looks
9384 	 * like:
9385 	 *
9386 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9387 	 *
9388 	 * Get a copy of the original IOCTL mblk to head the chain,
9389 	 * to be sent up (in mp1). Also get another copy to store
9390 	 * in the ill_pending_mp list, for matching the response
9391 	 * when it comes back from ARP.
9392 	 */
9393 	mp1 = copyb(mp);
9394 	pending_mp = copymsg(mp);
9395 	if (mp1 == NULL || pending_mp == NULL) {
9396 		if (mp1 != NULL)
9397 			freeb(mp1);
9398 		if (pending_mp != NULL)
9399 			inet_freemsg(pending_mp);
9400 		return (ENOMEM);
9401 	}
9402 
9403 	ipaddr = sin->sin_addr.s_addr;
9404 
9405 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9406 	    (caddr_t)&ipaddr);
9407 	if (mp2 == NULL) {
9408 		freeb(mp1);
9409 		inet_freemsg(pending_mp);
9410 		return (ENOMEM);
9411 	}
9412 	/* Put together the chain. */
9413 	mp1->b_cont = mp2;
9414 	mp1->b_datap->db_type = M_IOCTL;
9415 	mp2->b_cont = mp;
9416 	mp2->b_datap->db_type = M_DATA;
9417 
9418 	iocp = (struct iocblk *)mp1->b_rptr;
9419 
9420 	/*
9421 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9422 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9423 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9424 	 * ioc_count field; set ioc_count to be correct.
9425 	 */
9426 	iocp->ioc_count = MBLKL(mp1->b_cont);
9427 
9428 	/*
9429 	 * Set the proper command in the ARP message.
9430 	 * Convert the SIOC{G|S|D}ARP calls into our
9431 	 * AR_ENTRY_xxx calls.
9432 	 */
9433 	area = (area_t *)mp2->b_rptr;
9434 	switch (iocp->ioc_cmd) {
9435 	case SIOCDARP:
9436 	case SIOCDXARP:
9437 		/*
9438 		 * We defer deleting the corresponding IRE until
9439 		 * we return from arp.
9440 		 */
9441 		area->area_cmd = AR_ENTRY_DELETE;
9442 		area->area_proto_mask_offset = 0;
9443 		break;
9444 	case SIOCGARP:
9445 	case SIOCGXARP:
9446 		area->area_cmd = AR_ENTRY_SQUERY;
9447 		area->area_proto_mask_offset = 0;
9448 		break;
9449 	case SIOCSARP:
9450 	case SIOCSXARP:
9451 		/*
9452 		 * Delete the corresponding ire to make sure IP will
9453 		 * pick up any change from arp.
9454 		 */
9455 		if (!if_arp_ioctl) {
9456 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9457 		} else {
9458 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9459 			if (ipif != NULL) {
9460 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9461 				    ipst);
9462 				ipif_refrele(ipif);
9463 			}
9464 		}
9465 		break;
9466 	}
9467 	iocp->ioc_cmd = area->area_cmd;
9468 
9469 	/*
9470 	 * Fill in the rest of the ARP operation fields.
9471 	 */
9472 	area->area_hw_addr_length = alength;
9473 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9474 
9475 	/* Translate the flags. */
9476 	if (flags & ATF_PERM)
9477 		area->area_flags |= ACE_F_PERMANENT;
9478 	if (flags & ATF_PUBL)
9479 		area->area_flags |= ACE_F_PUBLISH;
9480 	if (flags & ATF_AUTHORITY)
9481 		area->area_flags |= ACE_F_AUTHORITY;
9482 
9483 	/*
9484 	 * Before sending 'mp' to ARP, we have to clear the b_next
9485 	 * and b_prev. Otherwise if STREAMS encounters such a message
9486 	 * in freemsg(), (because ARP can close any time) it can cause
9487 	 * a panic. But mi code needs the b_next and b_prev values of
9488 	 * mp->b_cont, to complete the ioctl. So we store it here
9489 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9490 	 * when the response comes down from ARP.
9491 	 */
9492 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9493 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9494 	mp->b_cont->b_next = NULL;
9495 	mp->b_cont->b_prev = NULL;
9496 
9497 	mutex_enter(&connp->conn_lock);
9498 	mutex_enter(&ill->ill_lock);
9499 	/* conn has not yet started closing, hence this can't fail */
9500 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9501 	mutex_exit(&ill->ill_lock);
9502 	mutex_exit(&connp->conn_lock);
9503 
9504 	/*
9505 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9506 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9507 	 */
9508 	putnext(ill->ill_rq, mp1);
9509 	return (EINPROGRESS);
9510 }
9511 
9512 /*
9513  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9514  * the associated sin and refhold and return the associated ipif via `ci'.
9515  */
9516 int
9517 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9518     cmd_info_t *ci, ipsq_func_t func)
9519 {
9520 	mblk_t	*mp1;
9521 	int	err;
9522 	sin_t	*sin;
9523 	conn_t	*connp;
9524 	ipif_t	*ipif;
9525 	ire_t	*ire = NULL;
9526 	ill_t	*ill = NULL;
9527 	boolean_t exists;
9528 	ip_stack_t *ipst;
9529 	struct arpreq *ar;
9530 	struct xarpreq *xar;
9531 	struct sockaddr_dl *sdl;
9532 
9533 	/* ioctl comes down on a conn */
9534 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9535 	connp = Q_TO_CONN(q);
9536 	if (connp->conn_af_isv6)
9537 		return (ENXIO);
9538 
9539 	ipst = connp->conn_netstack->netstack_ip;
9540 
9541 	/* Verified in ip_wput_nondata */
9542 	mp1 = mp->b_cont->b_cont;
9543 
9544 	if (ipip->ipi_cmd_type == XARP_CMD) {
9545 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9546 		xar = (struct xarpreq *)mp1->b_rptr;
9547 		sin = (sin_t *)&xar->xarp_pa;
9548 		sdl = &xar->xarp_ha;
9549 
9550 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9551 			return (ENXIO);
9552 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9553 			return (EINVAL);
9554 	} else {
9555 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9556 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9557 		ar = (struct arpreq *)mp1->b_rptr;
9558 		sin = (sin_t *)&ar->arp_pa;
9559 	}
9560 
9561 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9562 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9563 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9564 		    mp, func, &err, ipst);
9565 		if (ipif == NULL)
9566 			return (err);
9567 		if (ipif->ipif_id != 0 ||
9568 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9569 			ipif_refrele(ipif);
9570 			return (ENXIO);
9571 		}
9572 	} else {
9573 		/*
9574 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9575 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9576 		 * case, a simple forwarding table lookup will return the
9577 		 * IRE_IF_RESOLVER for the first interface in the group, which
9578 		 * might not be the interface on which the requested IP
9579 		 * address was resolved due to the ill selection algorithm
9580 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9581 		 * lookup first: if the IRE cache entry for the IP address is
9582 		 * still there, it will contain the ill pointer for the right
9583 		 * interface, so we use that. If the cache entry has been
9584 		 * flushed, we fall back to the forwarding table lookup. This
9585 		 * should be rare enough since IRE cache entries have a longer
9586 		 * life expectancy than ARP cache entries.
9587 		 */
9588 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9589 		    ipst);
9590 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9591 		    ((ill = ire_to_ill(ire)) == NULL) ||
9592 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9593 			if (ire != NULL)
9594 				ire_refrele(ire);
9595 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9596 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9597 			    NULL, MATCH_IRE_TYPE, ipst);
9598 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9599 
9600 				if (ire != NULL)
9601 					ire_refrele(ire);
9602 				return (ENXIO);
9603 			}
9604 		}
9605 		ASSERT(ire != NULL && ill != NULL);
9606 		ipif = ill->ill_ipif;
9607 		ipif_refhold(ipif);
9608 		ire_refrele(ire);
9609 	}
9610 	ci->ci_sin = sin;
9611 	ci->ci_ipif = ipif;
9612 	return (0);
9613 }
9614 
9615 /*
9616  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9617  * atomically set/clear the muxids. Also complete the ioctl by acking or
9618  * naking it.  Note that the code is structured such that the link type,
9619  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9620  * its clones use the persistent link, while pppd(1M) and perhaps many
9621  * other daemons may use non-persistent link.  When combined with some
9622  * ill_t states, linking and unlinking lower streams may be used as
9623  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9624  */
9625 /* ARGSUSED */
9626 void
9627 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9628 {
9629 	mblk_t		*mp1, *mp2;
9630 	struct linkblk	*li;
9631 	struct ipmx_s	*ipmxp;
9632 	ill_t		*ill;
9633 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9634 	int		err = 0;
9635 	boolean_t	entered_ipsq = B_FALSE;
9636 	boolean_t	islink;
9637 	ip_stack_t	*ipst;
9638 
9639 	if (CONN_Q(q))
9640 		ipst = CONNQ_TO_IPST(q);
9641 	else
9642 		ipst = ILLQ_TO_IPST(q);
9643 
9644 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9645 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9646 
9647 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9648 
9649 	mp1 = mp->b_cont;	/* This is the linkblk info */
9650 	li = (struct linkblk *)mp1->b_rptr;
9651 
9652 	/*
9653 	 * ARP has added this special mblk, and the utility is asking us
9654 	 * to perform consistency checks, and also atomically set the
9655 	 * muxid. Ifconfig is an example.  It achieves this by using
9656 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9657 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9658 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9659 	 * and other comments in this routine for more details.
9660 	 */
9661 	mp2 = mp1->b_cont;	/* This is added by ARP */
9662 
9663 	/*
9664 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9665 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9666 	 * get the special mblk above.  For backward compatibility, we
9667 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9668 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9669 	 * not atomic, and can leave the streams unplumbable if the utility
9670 	 * is interrupted before it does the SIOCSLIFMUXID.
9671 	 */
9672 	if (mp2 == NULL) {
9673 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9674 		if (err == EINPROGRESS)
9675 			return;
9676 		goto done;
9677 	}
9678 
9679 	/*
9680 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9681 	 * ARP has appended this last mblk to tell us whether the lower stream
9682 	 * is an arp-dev stream or an IP module stream.
9683 	 */
9684 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9685 	if (ipmxp->ipmx_arpdev_stream) {
9686 		/*
9687 		 * The lower stream is the arp-dev stream.
9688 		 */
9689 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9690 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9691 		if (ill == NULL) {
9692 			if (err == EINPROGRESS)
9693 				return;
9694 			err = EINVAL;
9695 			goto done;
9696 		}
9697 
9698 		if (ipsq == NULL) {
9699 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9700 			    NEW_OP, B_TRUE);
9701 			if (ipsq == NULL) {
9702 				ill_refrele(ill);
9703 				return;
9704 			}
9705 			entered_ipsq = B_TRUE;
9706 		}
9707 		ASSERT(IAM_WRITER_ILL(ill));
9708 		ill_refrele(ill);
9709 
9710 		/*
9711 		 * To ensure consistency between IP and ARP, the following
9712 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9713 		 * This is because the muxid's are stored in the IP stream on
9714 		 * the ill.
9715 		 *
9716 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9717 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9718 		 * not yet plinked, and it also checks that the corresponding
9719 		 * IP stream is already plinked.
9720 		 *
9721 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9722 		 * punlinking the IP stream. IP does not allow punlink of the
9723 		 * IP stream unless the arp stream has been punlinked.
9724 		 */
9725 		if ((islink &&
9726 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9727 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9728 			err = EINVAL;
9729 			goto done;
9730 		}
9731 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9732 	} else {
9733 		/*
9734 		 * The lower stream is probably an IP module stream.  Do
9735 		 * consistency checking.
9736 		 */
9737 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9738 		if (err == EINPROGRESS)
9739 			return;
9740 	}
9741 done:
9742 	if (err == 0)
9743 		miocack(q, mp, 0, 0);
9744 	else
9745 		miocnak(q, mp, 0, err);
9746 
9747 	/* Conn was refheld in ip_sioctl_copyin_setup */
9748 	if (CONN_Q(q))
9749 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9750 	if (entered_ipsq)
9751 		ipsq_exit(ipsq);
9752 }
9753 
9754 /*
9755  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9756  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9757  * module stream).  If `doconsist' is set, then do the extended consistency
9758  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9759  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9760  * an error code on failure.
9761  */
9762 static int
9763 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9764     struct linkblk *li, boolean_t doconsist)
9765 {
9766 	ill_t  		*ill;
9767 	queue_t		*ipwq, *dwq;
9768 	const char	*name;
9769 	struct qinit	*qinfo;
9770 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9771 	boolean_t	entered_ipsq = B_FALSE;
9772 
9773 	/*
9774 	 * Walk the lower stream to verify it's the IP module stream.
9775 	 * The IP module is identified by its name, wput function,
9776 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9777 	 * (li->l_qbot) will not vanish until this ioctl completes.
9778 	 */
9779 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9780 		qinfo = ipwq->q_qinfo;
9781 		name = qinfo->qi_minfo->mi_idname;
9782 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9783 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9784 			break;
9785 		}
9786 	}
9787 
9788 	/*
9789 	 * If this isn't an IP module stream, bail.
9790 	 */
9791 	if (ipwq == NULL)
9792 		return (0);
9793 
9794 	ill = ipwq->q_ptr;
9795 	ASSERT(ill != NULL);
9796 
9797 	if (ipsq == NULL) {
9798 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9799 		    NEW_OP, B_TRUE);
9800 		if (ipsq == NULL)
9801 			return (EINPROGRESS);
9802 		entered_ipsq = B_TRUE;
9803 	}
9804 	ASSERT(IAM_WRITER_ILL(ill));
9805 
9806 	if (doconsist) {
9807 		/*
9808 		 * Consistency checking requires that I_{P}LINK occurs
9809 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9810 		 * occurs prior to clearing ill_arp_muxid.
9811 		 */
9812 		if ((islink && ill->ill_ip_muxid != 0) ||
9813 		    (!islink && ill->ill_arp_muxid != 0)) {
9814 			if (entered_ipsq)
9815 				ipsq_exit(ipsq);
9816 			return (EINVAL);
9817 		}
9818 	}
9819 
9820 	/*
9821 	 * As part of I_{P}LINKing, stash the number of downstream modules and
9822 	 * the read queue of the module immediately below IP in the ill.
9823 	 * These are used during the capability negotiation below.
9824 	 */
9825 	ill->ill_lmod_rq = NULL;
9826 	ill->ill_lmod_cnt = 0;
9827 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
9828 		ill->ill_lmod_rq = RD(dwq);
9829 		for (; dwq != NULL; dwq = dwq->q_next)
9830 			ill->ill_lmod_cnt++;
9831 	}
9832 
9833 	if (doconsist)
9834 		ill->ill_ip_muxid = islink ? li->l_index : 0;
9835 
9836 	/*
9837 	 * Mark the ipsq busy until the capability operations initiated below
9838 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
9839 	 * returns, but the capability operation may complete asynchronously
9840 	 * much later.
9841 	 */
9842 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
9843 	/*
9844 	 * If there's at least one up ipif on this ill, then we're bound to
9845 	 * the underlying driver via DLPI.  In that case, renegotiate
9846 	 * capabilities to account for any possible change in modules
9847 	 * interposed between IP and the driver.
9848 	 */
9849 	if (ill->ill_ipif_up_count > 0) {
9850 		if (islink)
9851 			ill_capability_probe(ill);
9852 		else
9853 			ill_capability_reset(ill, B_FALSE);
9854 	}
9855 	ipsq_current_finish(ipsq);
9856 
9857 	if (entered_ipsq)
9858 		ipsq_exit(ipsq);
9859 
9860 	return (0);
9861 }
9862 
9863 /*
9864  * Search the ioctl command in the ioctl tables and return a pointer
9865  * to the ioctl command information. The ioctl command tables are
9866  * static and fully populated at compile time.
9867  */
9868 ip_ioctl_cmd_t *
9869 ip_sioctl_lookup(int ioc_cmd)
9870 {
9871 	int index;
9872 	ip_ioctl_cmd_t *ipip;
9873 	ip_ioctl_cmd_t *ipip_end;
9874 
9875 	if (ioc_cmd == IPI_DONTCARE)
9876 		return (NULL);
9877 
9878 	/*
9879 	 * Do a 2 step search. First search the indexed table
9880 	 * based on the least significant byte of the ioctl cmd.
9881 	 * If we don't find a match, then search the misc table
9882 	 * serially.
9883 	 */
9884 	index = ioc_cmd & 0xFF;
9885 	if (index < ip_ndx_ioctl_count) {
9886 		ipip = &ip_ndx_ioctl_table[index];
9887 		if (ipip->ipi_cmd == ioc_cmd) {
9888 			/* Found a match in the ndx table */
9889 			return (ipip);
9890 		}
9891 	}
9892 
9893 	/* Search the misc table */
9894 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
9895 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
9896 		if (ipip->ipi_cmd == ioc_cmd)
9897 			/* Found a match in the misc table */
9898 			return (ipip);
9899 	}
9900 
9901 	return (NULL);
9902 }
9903 
9904 /*
9905  * Wrapper function for resuming deferred ioctl processing
9906  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
9907  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
9908  */
9909 /* ARGSUSED */
9910 void
9911 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
9912     void *dummy_arg)
9913 {
9914 	ip_sioctl_copyin_setup(q, mp);
9915 }
9916 
9917 /*
9918  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
9919  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
9920  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
9921  * We establish here the size of the block to be copied in.  mi_copyin
9922  * arranges for this to happen, an processing continues in ip_wput with
9923  * an M_IOCDATA message.
9924  */
9925 void
9926 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9927 {
9928 	int	copyin_size;
9929 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9930 	ip_ioctl_cmd_t *ipip;
9931 	cred_t *cr;
9932 	ip_stack_t	*ipst;
9933 
9934 	if (CONN_Q(q))
9935 		ipst = CONNQ_TO_IPST(q);
9936 	else
9937 		ipst = ILLQ_TO_IPST(q);
9938 
9939 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9940 	if (ipip == NULL) {
9941 		/*
9942 		 * The ioctl is not one we understand or own.
9943 		 * Pass it along to be processed down stream,
9944 		 * if this is a module instance of IP, else nak
9945 		 * the ioctl.
9946 		 */
9947 		if (q->q_next == NULL) {
9948 			goto nak;
9949 		} else {
9950 			putnext(q, mp);
9951 			return;
9952 		}
9953 	}
9954 
9955 	/*
9956 	 * If this is deferred, then we will do all the checks when we
9957 	 * come back.
9958 	 */
9959 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9960 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
9961 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9962 		return;
9963 	}
9964 
9965 	/*
9966 	 * Only allow a very small subset of IP ioctls on this stream if
9967 	 * IP is a module and not a driver. Allowing ioctls to be processed
9968 	 * in this case may cause assert failures or data corruption.
9969 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9970 	 * ioctls allowed on an IP module stream, after which this stream
9971 	 * normally becomes a multiplexor (at which time the stream head
9972 	 * will fail all ioctls).
9973 	 */
9974 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9975 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
9976 			/*
9977 			 * Pass common Streams ioctls which the IP
9978 			 * module does not own or consume along to
9979 			 * be processed down stream.
9980 			 */
9981 			putnext(q, mp);
9982 			return;
9983 		} else {
9984 			goto nak;
9985 		}
9986 	}
9987 
9988 	/* Make sure we have ioctl data to process. */
9989 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9990 		goto nak;
9991 
9992 	/*
9993 	 * Prefer dblk credential over ioctl credential; some synthesized
9994 	 * ioctls have kcred set because there's no way to crhold()
9995 	 * a credential in some contexts.  (ioc_cr is not crfree() by
9996 	 * the framework; the caller of ioctl needs to hold the reference
9997 	 * for the duration of the call).
9998 	 */
9999 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10000 
10001 	/* Make sure normal users don't send down privileged ioctls */
10002 	if ((ipip->ipi_flags & IPI_PRIV) &&
10003 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10004 		/* We checked the privilege earlier but log it here */
10005 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10006 		return;
10007 	}
10008 
10009 	/*
10010 	 * The ioctl command tables can only encode fixed length
10011 	 * ioctl data. If the length is variable, the table will
10012 	 * encode the length as zero. Such special cases are handled
10013 	 * below in the switch.
10014 	 */
10015 	if (ipip->ipi_copyin_size != 0) {
10016 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10017 		return;
10018 	}
10019 
10020 	switch (iocp->ioc_cmd) {
10021 	case O_SIOCGIFCONF:
10022 	case SIOCGIFCONF:
10023 		/*
10024 		 * This IOCTL is hilarious.  See comments in
10025 		 * ip_sioctl_get_ifconf for the story.
10026 		 */
10027 		if (iocp->ioc_count == TRANSPARENT)
10028 			copyin_size = SIZEOF_STRUCT(ifconf,
10029 			    iocp->ioc_flag);
10030 		else
10031 			copyin_size = iocp->ioc_count;
10032 		mi_copyin(q, mp, NULL, copyin_size);
10033 		return;
10034 
10035 	case O_SIOCGLIFCONF:
10036 	case SIOCGLIFCONF:
10037 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10038 		mi_copyin(q, mp, NULL, copyin_size);
10039 		return;
10040 
10041 	case SIOCGLIFSRCOF:
10042 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10043 		mi_copyin(q, mp, NULL, copyin_size);
10044 		return;
10045 	case SIOCGIP6ADDRPOLICY:
10046 		ip_sioctl_ip6addrpolicy(q, mp);
10047 		ip6_asp_table_refrele(ipst);
10048 		return;
10049 
10050 	case SIOCSIP6ADDRPOLICY:
10051 		ip_sioctl_ip6addrpolicy(q, mp);
10052 		return;
10053 
10054 	case SIOCGDSTINFO:
10055 		ip_sioctl_dstinfo(q, mp);
10056 		ip6_asp_table_refrele(ipst);
10057 		return;
10058 
10059 	case I_PLINK:
10060 	case I_PUNLINK:
10061 	case I_LINK:
10062 	case I_UNLINK:
10063 		/*
10064 		 * We treat non-persistent link similarly as the persistent
10065 		 * link case, in terms of plumbing/unplumbing, as well as
10066 		 * dynamic re-plumbing events indicator.  See comments
10067 		 * in ip_sioctl_plink() for more.
10068 		 *
10069 		 * Request can be enqueued in the 'ipsq' while waiting
10070 		 * to become exclusive. So bump up the conn ref.
10071 		 */
10072 		if (CONN_Q(q))
10073 			CONN_INC_REF(Q_TO_CONN(q));
10074 		ip_sioctl_plink(NULL, q, mp, NULL);
10075 		return;
10076 
10077 	case ND_GET:
10078 	case ND_SET:
10079 		/*
10080 		 * Use of the nd table requires holding the reader lock.
10081 		 * Modifying the nd table thru nd_load/nd_unload requires
10082 		 * the writer lock.
10083 		 */
10084 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10085 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10086 			rw_exit(&ipst->ips_ip_g_nd_lock);
10087 
10088 			if (iocp->ioc_error)
10089 				iocp->ioc_count = 0;
10090 			mp->b_datap->db_type = M_IOCACK;
10091 			qreply(q, mp);
10092 			return;
10093 		}
10094 		rw_exit(&ipst->ips_ip_g_nd_lock);
10095 		/*
10096 		 * We don't understand this subioctl of ND_GET / ND_SET.
10097 		 * Maybe intended for some driver / module below us
10098 		 */
10099 		if (q->q_next) {
10100 			putnext(q, mp);
10101 		} else {
10102 			iocp->ioc_error = ENOENT;
10103 			mp->b_datap->db_type = M_IOCNAK;
10104 			iocp->ioc_count = 0;
10105 			qreply(q, mp);
10106 		}
10107 		return;
10108 
10109 	case IP_IOCTL:
10110 		ip_wput_ioctl(q, mp);
10111 		return;
10112 	default:
10113 		cmn_err(CE_PANIC, "should not happen ");
10114 	}
10115 nak:
10116 	if (mp->b_cont != NULL) {
10117 		freemsg(mp->b_cont);
10118 		mp->b_cont = NULL;
10119 	}
10120 	iocp->ioc_error = EINVAL;
10121 	mp->b_datap->db_type = M_IOCNAK;
10122 	iocp->ioc_count = 0;
10123 	qreply(q, mp);
10124 }
10125 
10126 /* ip_wput hands off ARP IOCTL responses to us */
10127 void
10128 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10129 {
10130 	struct arpreq *ar;
10131 	struct xarpreq *xar;
10132 	area_t	*area;
10133 	mblk_t	*area_mp;
10134 	struct iocblk *iocp;
10135 	mblk_t	*orig_ioc_mp, *tmp;
10136 	struct iocblk	*orig_iocp;
10137 	ill_t *ill;
10138 	conn_t *connp = NULL;
10139 	uint_t ioc_id;
10140 	mblk_t *pending_mp;
10141 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10142 	int *flagsp;
10143 	char *storage = NULL;
10144 	sin_t *sin;
10145 	ipaddr_t addr;
10146 	int err;
10147 	ip_stack_t *ipst;
10148 
10149 	ill = q->q_ptr;
10150 	ASSERT(ill != NULL);
10151 	ipst = ill->ill_ipst;
10152 
10153 	/*
10154 	 * We should get back from ARP a packet chain that looks like:
10155 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10156 	 */
10157 	if (!(area_mp = mp->b_cont) ||
10158 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10159 	    !(orig_ioc_mp = area_mp->b_cont) ||
10160 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10161 		freemsg(mp);
10162 		return;
10163 	}
10164 
10165 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10166 
10167 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10168 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10169 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10170 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10171 		x_arp_ioctl = B_TRUE;
10172 		xar = (struct xarpreq *)tmp->b_rptr;
10173 		sin = (sin_t *)&xar->xarp_pa;
10174 		flagsp = &xar->xarp_flags;
10175 		storage = xar->xarp_ha.sdl_data;
10176 		if (xar->xarp_ha.sdl_nlen != 0)
10177 			ifx_arp_ioctl = B_TRUE;
10178 	} else {
10179 		ar = (struct arpreq *)tmp->b_rptr;
10180 		sin = (sin_t *)&ar->arp_pa;
10181 		flagsp = &ar->arp_flags;
10182 		storage = ar->arp_ha.sa_data;
10183 	}
10184 
10185 	iocp = (struct iocblk *)mp->b_rptr;
10186 
10187 	/*
10188 	 * Pick out the originating queue based on the ioc_id.
10189 	 */
10190 	ioc_id = iocp->ioc_id;
10191 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10192 	if (pending_mp == NULL) {
10193 		ASSERT(connp == NULL);
10194 		inet_freemsg(mp);
10195 		return;
10196 	}
10197 	ASSERT(connp != NULL);
10198 	q = CONNP_TO_WQ(connp);
10199 
10200 	/* Uncouple the internally generated IOCTL from the original one */
10201 	area = (area_t *)area_mp->b_rptr;
10202 	area_mp->b_cont = NULL;
10203 
10204 	/*
10205 	 * Restore the b_next and b_prev used by mi code. This is needed
10206 	 * to complete the ioctl using mi* functions. We stored them in
10207 	 * the pending mp prior to sending the request to ARP.
10208 	 */
10209 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10210 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10211 	inet_freemsg(pending_mp);
10212 
10213 	/*
10214 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10215 	 * Catch the case where there is an IRE_CACHE by no entry in the
10216 	 * arp table.
10217 	 */
10218 	addr = sin->sin_addr.s_addr;
10219 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10220 		ire_t			*ire;
10221 		dl_unitdata_req_t	*dlup;
10222 		mblk_t			*llmp;
10223 		int			addr_len;
10224 		ill_t			*ipsqill = NULL;
10225 
10226 		if (ifx_arp_ioctl) {
10227 			/*
10228 			 * There's no need to lookup the ill, since
10229 			 * we've already done that when we started
10230 			 * processing the ioctl and sent the message
10231 			 * to ARP on that ill.  So use the ill that
10232 			 * is stored in q->q_ptr.
10233 			 */
10234 			ipsqill = ill;
10235 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10236 			    ipsqill->ill_ipif, ALL_ZONES,
10237 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10238 		} else {
10239 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10240 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10241 			if (ire != NULL)
10242 				ipsqill = ire_to_ill(ire);
10243 		}
10244 
10245 		if ((x_arp_ioctl) && (ipsqill != NULL))
10246 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10247 
10248 		if (ire != NULL) {
10249 			/*
10250 			 * Since the ire obtained from cachetable is used for
10251 			 * mac addr copying below, treat an incomplete ire as if
10252 			 * as if we never found it.
10253 			 */
10254 			if (ire->ire_nce != NULL &&
10255 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10256 				ire_refrele(ire);
10257 				ire = NULL;
10258 				ipsqill = NULL;
10259 				goto errack;
10260 			}
10261 			*flagsp = ATF_INUSE;
10262 			llmp = (ire->ire_nce != NULL ?
10263 			    ire->ire_nce->nce_res_mp : NULL);
10264 			if (llmp != NULL && ipsqill != NULL) {
10265 				uchar_t *macaddr;
10266 
10267 				addr_len = ipsqill->ill_phys_addr_length;
10268 				if (x_arp_ioctl && ((addr_len +
10269 				    ipsqill->ill_name_length) >
10270 				    sizeof (xar->xarp_ha.sdl_data))) {
10271 					ire_refrele(ire);
10272 					freemsg(mp);
10273 					ip_ioctl_finish(q, orig_ioc_mp,
10274 					    EINVAL, NO_COPYOUT, NULL);
10275 					return;
10276 				}
10277 				*flagsp |= ATF_COM;
10278 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10279 				if (ipsqill->ill_sap_length < 0)
10280 					macaddr = llmp->b_rptr +
10281 					    dlup->dl_dest_addr_offset;
10282 				else
10283 					macaddr = llmp->b_rptr +
10284 					    dlup->dl_dest_addr_offset +
10285 					    ipsqill->ill_sap_length;
10286 				/*
10287 				 * For SIOCGARP, MAC address length
10288 				 * validation has already been done
10289 				 * before the ioctl was issued to ARP to
10290 				 * allow it to progress only on 6 byte
10291 				 * addressable (ethernet like) media. Thus
10292 				 * the mac address copying can not overwrite
10293 				 * the sa_data area below.
10294 				 */
10295 				bcopy(macaddr, storage, addr_len);
10296 			}
10297 			/* Ditch the internal IOCTL. */
10298 			freemsg(mp);
10299 			ire_refrele(ire);
10300 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10301 			return;
10302 		}
10303 	}
10304 
10305 	/*
10306 	 * Delete the coresponding IRE_CACHE if any.
10307 	 * Reset the error if there was one (in case there was no entry
10308 	 * in arp.)
10309 	 */
10310 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10311 		ipif_t *ipintf = NULL;
10312 
10313 		if (ifx_arp_ioctl) {
10314 			/*
10315 			 * There's no need to lookup the ill, since
10316 			 * we've already done that when we started
10317 			 * processing the ioctl and sent the message
10318 			 * to ARP on that ill.  So use the ill that
10319 			 * is stored in q->q_ptr.
10320 			 */
10321 			ipintf = ill->ill_ipif;
10322 		}
10323 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10324 			/*
10325 			 * The address in "addr" may be an entry for a
10326 			 * router. If that's true, then any off-net
10327 			 * IRE_CACHE entries that go through the router
10328 			 * with address "addr" must be clobbered. Use
10329 			 * ire_walk to achieve this goal.
10330 			 */
10331 			if (ifx_arp_ioctl)
10332 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10333 				    ire_delete_cache_gw, (char *)&addr, ill);
10334 			else
10335 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10336 				    ALL_ZONES, ipst);
10337 			iocp->ioc_error = 0;
10338 		}
10339 	}
10340 errack:
10341 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10342 		err = iocp->ioc_error;
10343 		freemsg(mp);
10344 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10345 		return;
10346 	}
10347 
10348 	/*
10349 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10350 	 * the area_t into the struct {x}arpreq.
10351 	 */
10352 	if (x_arp_ioctl) {
10353 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10354 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10355 		    sizeof (xar->xarp_ha.sdl_data)) {
10356 			freemsg(mp);
10357 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10358 			    NULL);
10359 			return;
10360 		}
10361 	}
10362 	*flagsp = ATF_INUSE;
10363 	if (area->area_flags & ACE_F_PERMANENT)
10364 		*flagsp |= ATF_PERM;
10365 	if (area->area_flags & ACE_F_PUBLISH)
10366 		*flagsp |= ATF_PUBL;
10367 	if (area->area_flags & ACE_F_AUTHORITY)
10368 		*flagsp |= ATF_AUTHORITY;
10369 	if (area->area_hw_addr_length != 0) {
10370 		*flagsp |= ATF_COM;
10371 		/*
10372 		 * For SIOCGARP, MAC address length validation has
10373 		 * already been done before the ioctl was issued to ARP
10374 		 * to allow it to progress only on 6 byte addressable
10375 		 * (ethernet like) media. Thus the mac address copying
10376 		 * can not overwrite the sa_data area below.
10377 		 */
10378 		bcopy((char *)area + area->area_hw_addr_offset,
10379 		    storage, area->area_hw_addr_length);
10380 	}
10381 
10382 	/* Ditch the internal IOCTL. */
10383 	freemsg(mp);
10384 	/* Complete the original. */
10385 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10386 }
10387 
10388 /*
10389  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10390  * interface) create the next available logical interface for this
10391  * physical interface.
10392  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10393  * ipif with the specified name.
10394  *
10395  * If the address family is not AF_UNSPEC then set the address as well.
10396  *
10397  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10398  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10399  *
10400  * Executed as a writer on the ill or ill group.
10401  * So no lock is needed to traverse the ipif chain, or examine the
10402  * phyint flags.
10403  */
10404 /* ARGSUSED */
10405 int
10406 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10407     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10408 {
10409 	mblk_t	*mp1;
10410 	struct lifreq *lifr;
10411 	boolean_t	isv6;
10412 	boolean_t	exists;
10413 	char 	*name;
10414 	char	*endp;
10415 	char	*cp;
10416 	int	namelen;
10417 	ipif_t	*ipif;
10418 	long	id;
10419 	ipsq_t	*ipsq;
10420 	ill_t	*ill;
10421 	sin_t	*sin;
10422 	int	err = 0;
10423 	boolean_t found_sep = B_FALSE;
10424 	conn_t	*connp;
10425 	zoneid_t zoneid;
10426 	int	orig_ifindex = 0;
10427 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10428 
10429 	ASSERT(q->q_next == NULL);
10430 	ip1dbg(("ip_sioctl_addif\n"));
10431 	/* Existence of mp1 has been checked in ip_wput_nondata */
10432 	mp1 = mp->b_cont->b_cont;
10433 	/*
10434 	 * Null terminate the string to protect against buffer
10435 	 * overrun. String was generated by user code and may not
10436 	 * be trusted.
10437 	 */
10438 	lifr = (struct lifreq *)mp1->b_rptr;
10439 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10440 	name = lifr->lifr_name;
10441 	ASSERT(CONN_Q(q));
10442 	connp = Q_TO_CONN(q);
10443 	isv6 = connp->conn_af_isv6;
10444 	zoneid = connp->conn_zoneid;
10445 	namelen = mi_strlen(name);
10446 	if (namelen == 0)
10447 		return (EINVAL);
10448 
10449 	exists = B_FALSE;
10450 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10451 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10452 		/*
10453 		 * Allow creating lo0 using SIOCLIFADDIF.
10454 		 * can't be any other writer thread. So can pass null below
10455 		 * for the last 4 args to ipif_lookup_name.
10456 		 */
10457 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10458 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10459 		/* Prevent any further action */
10460 		if (ipif == NULL) {
10461 			return (ENOBUFS);
10462 		} else if (!exists) {
10463 			/* We created the ipif now and as writer */
10464 			ipif_refrele(ipif);
10465 			return (0);
10466 		} else {
10467 			ill = ipif->ipif_ill;
10468 			ill_refhold(ill);
10469 			ipif_refrele(ipif);
10470 		}
10471 	} else {
10472 		/* Look for a colon in the name. */
10473 		endp = &name[namelen];
10474 		for (cp = endp; --cp > name; ) {
10475 			if (*cp == IPIF_SEPARATOR_CHAR) {
10476 				found_sep = B_TRUE;
10477 				/*
10478 				 * Reject any non-decimal aliases for plumbing
10479 				 * of logical interfaces. Aliases with leading
10480 				 * zeroes are also rejected as they introduce
10481 				 * ambiguity in the naming of the interfaces.
10482 				 * Comparing with "0" takes care of all such
10483 				 * cases.
10484 				 */
10485 				if ((strncmp("0", cp+1, 1)) == 0)
10486 					return (EINVAL);
10487 
10488 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10489 				    id <= 0 || *endp != '\0') {
10490 					return (EINVAL);
10491 				}
10492 				*cp = '\0';
10493 				break;
10494 			}
10495 		}
10496 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10497 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10498 		if (found_sep)
10499 			*cp = IPIF_SEPARATOR_CHAR;
10500 		if (ill == NULL)
10501 			return (err);
10502 	}
10503 
10504 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10505 	    B_TRUE);
10506 
10507 	/*
10508 	 * Release the refhold due to the lookup, now that we are excl
10509 	 * or we are just returning
10510 	 */
10511 	ill_refrele(ill);
10512 
10513 	if (ipsq == NULL)
10514 		return (EINPROGRESS);
10515 
10516 	/*
10517 	 * If the interface is failed, inactive or offlined, look for a working
10518 	 * interface in the ill group and create the ipif there. If we can't
10519 	 * find a good interface, create the ipif anyway so that in.mpathd can
10520 	 * move it to the first repaired interface.
10521 	 */
10522 	if ((ill->ill_phyint->phyint_flags &
10523 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10524 	    ill->ill_phyint->phyint_groupname_len != 0) {
10525 		phyint_t *phyi;
10526 		char *groupname = ill->ill_phyint->phyint_groupname;
10527 
10528 		/*
10529 		 * We're looking for a working interface, but it doesn't matter
10530 		 * if it's up or down; so instead of following the group lists,
10531 		 * we look at each physical interface and compare the groupname.
10532 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10533 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10534 		 * Otherwise we create the ipif on the failed interface.
10535 		 */
10536 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10537 		phyi = avl_first(&ipst->ips_phyint_g_list->
10538 		    phyint_list_avl_by_index);
10539 		for (; phyi != NULL;
10540 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10541 		    phyint_list_avl_by_index,
10542 		    phyi, AVL_AFTER)) {
10543 			if (phyi->phyint_groupname_len == 0)
10544 				continue;
10545 			ASSERT(phyi->phyint_groupname != NULL);
10546 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10547 			    !(phyi->phyint_flags &
10548 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10549 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10550 			    (phyi->phyint_illv4 != NULL))) {
10551 				break;
10552 			}
10553 		}
10554 		rw_exit(&ipst->ips_ill_g_lock);
10555 
10556 		if (phyi != NULL) {
10557 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10558 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10559 			    phyi->phyint_illv4);
10560 		}
10561 	}
10562 
10563 	/*
10564 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10565 	 * before or after us.
10566 	 */
10567 	ASSERT(IAM_WRITER_ILL(ill));
10568 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10569 
10570 	if (found_sep && orig_ifindex == 0) {
10571 		/* Now see if there is an IPIF with this unit number. */
10572 		for (ipif = ill->ill_ipif; ipif != NULL;
10573 		    ipif = ipif->ipif_next) {
10574 			if (ipif->ipif_id == id) {
10575 				err = EEXIST;
10576 				goto done;
10577 			}
10578 		}
10579 	}
10580 
10581 	/*
10582 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10583 	 * of lo0. We never come here when we plumb lo0:0. It
10584 	 * happens in ipif_lookup_on_name.
10585 	 * The specified unit number is ignored when we create the ipif on a
10586 	 * different interface. However, we save it in ipif_orig_ipifid below so
10587 	 * that the ipif fails back to the right position.
10588 	 */
10589 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10590 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10591 		err = ENOBUFS;
10592 		goto done;
10593 	}
10594 
10595 	/* Return created name with ioctl */
10596 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10597 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10598 	ip1dbg(("created %s\n", lifr->lifr_name));
10599 
10600 	/* Set address */
10601 	sin = (sin_t *)&lifr->lifr_addr;
10602 	if (sin->sin_family != AF_UNSPEC) {
10603 		err = ip_sioctl_addr(ipif, sin, q, mp,
10604 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10605 	}
10606 
10607 	/* Set ifindex and unit number for failback */
10608 	if (err == 0 && orig_ifindex != 0) {
10609 		ipif->ipif_orig_ifindex = orig_ifindex;
10610 		if (found_sep) {
10611 			ipif->ipif_orig_ipifid = id;
10612 		}
10613 	}
10614 
10615 done:
10616 	ipsq_exit(ipsq);
10617 	return (err);
10618 }
10619 
10620 /*
10621  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10622  * interface) delete it based on the IP address (on this physical interface).
10623  * Otherwise delete it based on the ipif_id.
10624  * Also, special handling to allow a removeif of lo0.
10625  */
10626 /* ARGSUSED */
10627 int
10628 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10629     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10630 {
10631 	conn_t		*connp;
10632 	ill_t		*ill = ipif->ipif_ill;
10633 	boolean_t	 success;
10634 	ip_stack_t	*ipst;
10635 
10636 	ipst = CONNQ_TO_IPST(q);
10637 
10638 	ASSERT(q->q_next == NULL);
10639 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10640 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10641 	ASSERT(IAM_WRITER_IPIF(ipif));
10642 
10643 	connp = Q_TO_CONN(q);
10644 	/*
10645 	 * Special case for unplumbing lo0 (the loopback physical interface).
10646 	 * If unplumbing lo0, the incoming address structure has been
10647 	 * initialized to all zeros. When unplumbing lo0, all its logical
10648 	 * interfaces must be removed too.
10649 	 *
10650 	 * Note that this interface may be called to remove a specific
10651 	 * loopback logical interface (eg, lo0:1). But in that case
10652 	 * ipif->ipif_id != 0 so that the code path for that case is the
10653 	 * same as any other interface (meaning it skips the code directly
10654 	 * below).
10655 	 */
10656 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10657 		if (sin->sin_family == AF_UNSPEC &&
10658 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10659 			/*
10660 			 * Mark it condemned. No new ref. will be made to ill.
10661 			 */
10662 			mutex_enter(&ill->ill_lock);
10663 			ill->ill_state_flags |= ILL_CONDEMNED;
10664 			for (ipif = ill->ill_ipif; ipif != NULL;
10665 			    ipif = ipif->ipif_next) {
10666 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10667 			}
10668 			mutex_exit(&ill->ill_lock);
10669 
10670 			ipif = ill->ill_ipif;
10671 			/* unplumb the loopback interface */
10672 			ill_delete(ill);
10673 			mutex_enter(&connp->conn_lock);
10674 			mutex_enter(&ill->ill_lock);
10675 			ASSERT(ill->ill_group == NULL);
10676 
10677 			/* Are any references to this ill active */
10678 			if (ill_is_freeable(ill)) {
10679 				mutex_exit(&ill->ill_lock);
10680 				mutex_exit(&connp->conn_lock);
10681 				ill_delete_tail(ill);
10682 				mi_free(ill);
10683 				return (0);
10684 			}
10685 			success = ipsq_pending_mp_add(connp, ipif,
10686 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10687 			mutex_exit(&connp->conn_lock);
10688 			mutex_exit(&ill->ill_lock);
10689 			if (success)
10690 				return (EINPROGRESS);
10691 			else
10692 				return (EINTR);
10693 		}
10694 	}
10695 
10696 	/*
10697 	 * We are exclusive on the ipsq, so an ill move will be serialized
10698 	 * before or after us.
10699 	 */
10700 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10701 
10702 	if (ipif->ipif_id == 0) {
10703 
10704 		ipsq_t *ipsq;
10705 
10706 		/* Find based on address */
10707 		if (ipif->ipif_isv6) {
10708 			sin6_t *sin6;
10709 
10710 			if (sin->sin_family != AF_INET6)
10711 				return (EAFNOSUPPORT);
10712 
10713 			sin6 = (sin6_t *)sin;
10714 			/* We are a writer, so we should be able to lookup */
10715 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10716 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10717 			if (ipif == NULL) {
10718 				/*
10719 				 * Maybe the address in on another interface in
10720 				 * the same IPMP group? We check this below.
10721 				 */
10722 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10723 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10724 				    ipst);
10725 			}
10726 		} else {
10727 			ipaddr_t addr;
10728 
10729 			if (sin->sin_family != AF_INET)
10730 				return (EAFNOSUPPORT);
10731 
10732 			addr = sin->sin_addr.s_addr;
10733 			/* We are a writer, so we should be able to lookup */
10734 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10735 			    NULL, NULL, NULL, ipst);
10736 			if (ipif == NULL) {
10737 				/*
10738 				 * Maybe the address in on another interface in
10739 				 * the same IPMP group? We check this below.
10740 				 */
10741 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10742 				    NULL, NULL, NULL, NULL, ipst);
10743 			}
10744 		}
10745 		if (ipif == NULL) {
10746 			return (EADDRNOTAVAIL);
10747 		}
10748 
10749 		/*
10750 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10751 		 * lifr_name of the physical interface but with an ip address
10752 		 * lifr_addr of a logical interface plumbed over it.
10753 		 * So update ipsq_current_ipif once ipif points to the
10754 		 * correct interface after doing ipif_lookup_addr().
10755 		 */
10756 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10757 		ASSERT(ipsq != NULL);
10758 
10759 		mutex_enter(&ipsq->ipsq_lock);
10760 		ipsq->ipsq_current_ipif = ipif;
10761 		mutex_exit(&ipsq->ipsq_lock);
10762 
10763 		/*
10764 		 * When the address to be removed is hosted on a different
10765 		 * interface, we check if the interface is in the same IPMP
10766 		 * group as the specified one; if so we proceed with the
10767 		 * removal.
10768 		 * ill->ill_group is NULL when the ill is down, so we have to
10769 		 * compare the group names instead.
10770 		 */
10771 		if (ipif->ipif_ill != ill &&
10772 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10773 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10774 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10775 		    ill->ill_phyint->phyint_groupname) != 0)) {
10776 			ipif_refrele(ipif);
10777 			return (EADDRNOTAVAIL);
10778 		}
10779 
10780 		/* This is a writer */
10781 		ipif_refrele(ipif);
10782 	}
10783 
10784 	/*
10785 	 * Can not delete instance zero since it is tied to the ill.
10786 	 */
10787 	if (ipif->ipif_id == 0)
10788 		return (EBUSY);
10789 
10790 	mutex_enter(&ill->ill_lock);
10791 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10792 	mutex_exit(&ill->ill_lock);
10793 
10794 	ipif_free(ipif);
10795 
10796 	mutex_enter(&connp->conn_lock);
10797 	mutex_enter(&ill->ill_lock);
10798 
10799 
10800 	/* Are any references to this ipif active */
10801 	if (ipif_is_freeable(ipif)) {
10802 		mutex_exit(&ill->ill_lock);
10803 		mutex_exit(&connp->conn_lock);
10804 		ipif_non_duplicate(ipif);
10805 		ipif_down_tail(ipif);
10806 		ipif_free_tail(ipif); /* frees ipif */
10807 		return (0);
10808 	}
10809 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10810 	    IPIF_FREE);
10811 	mutex_exit(&ill->ill_lock);
10812 	mutex_exit(&connp->conn_lock);
10813 	if (success)
10814 		return (EINPROGRESS);
10815 	else
10816 		return (EINTR);
10817 }
10818 
10819 /*
10820  * Restart the removeif ioctl. The refcnt has gone down to 0.
10821  * The ipif is already condemned. So can't find it thru lookups.
10822  */
10823 /* ARGSUSED */
10824 int
10825 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10826     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10827 {
10828 	ill_t *ill = ipif->ipif_ill;
10829 
10830 	ASSERT(IAM_WRITER_IPIF(ipif));
10831 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10832 
10833 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10834 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10835 
10836 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10837 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
10838 		ill_delete_tail(ill);
10839 		mi_free(ill);
10840 		return (0);
10841 	}
10842 
10843 	ipif_non_duplicate(ipif);
10844 	ipif_down_tail(ipif);
10845 	ipif_free_tail(ipif);
10846 
10847 	ILL_UNMARK_CHANGING(ill);
10848 	return (0);
10849 }
10850 
10851 /*
10852  * Set the local interface address.
10853  * Allow an address of all zero when the interface is down.
10854  */
10855 /* ARGSUSED */
10856 int
10857 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10858     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10859 {
10860 	int err = 0;
10861 	in6_addr_t v6addr;
10862 	boolean_t need_up = B_FALSE;
10863 
10864 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10865 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10866 
10867 	ASSERT(IAM_WRITER_IPIF(ipif));
10868 
10869 	if (ipif->ipif_isv6) {
10870 		sin6_t *sin6;
10871 		ill_t *ill;
10872 		phyint_t *phyi;
10873 
10874 		if (sin->sin_family != AF_INET6)
10875 			return (EAFNOSUPPORT);
10876 
10877 		sin6 = (sin6_t *)sin;
10878 		v6addr = sin6->sin6_addr;
10879 		ill = ipif->ipif_ill;
10880 		phyi = ill->ill_phyint;
10881 
10882 		/*
10883 		 * Enforce that true multicast interfaces have a link-local
10884 		 * address for logical unit 0.
10885 		 */
10886 		if (ipif->ipif_id == 0 &&
10887 		    (ill->ill_flags & ILLF_MULTICAST) &&
10888 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
10889 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
10890 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
10891 			return (EADDRNOTAVAIL);
10892 		}
10893 
10894 		/*
10895 		 * up interfaces shouldn't have the unspecified address
10896 		 * unless they also have the IPIF_NOLOCAL flags set and
10897 		 * have a subnet assigned.
10898 		 */
10899 		if ((ipif->ipif_flags & IPIF_UP) &&
10900 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
10901 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
10902 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
10903 			return (EADDRNOTAVAIL);
10904 		}
10905 
10906 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10907 			return (EADDRNOTAVAIL);
10908 	} else {
10909 		ipaddr_t addr;
10910 
10911 		if (sin->sin_family != AF_INET)
10912 			return (EAFNOSUPPORT);
10913 
10914 		addr = sin->sin_addr.s_addr;
10915 
10916 		/* Allow 0 as the local address. */
10917 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10918 			return (EADDRNOTAVAIL);
10919 
10920 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10921 	}
10922 
10923 	/*
10924 	 * Even if there is no change we redo things just to rerun
10925 	 * ipif_set_default.
10926 	 */
10927 	if (ipif->ipif_flags & IPIF_UP) {
10928 		/*
10929 		 * Setting a new local address, make sure
10930 		 * we have net and subnet bcast ire's for
10931 		 * the old address if we need them.
10932 		 */
10933 		if (!ipif->ipif_isv6)
10934 			ipif_check_bcast_ires(ipif);
10935 		/*
10936 		 * If the interface is already marked up,
10937 		 * we call ipif_down which will take care
10938 		 * of ditching any IREs that have been set
10939 		 * up based on the old interface address.
10940 		 */
10941 		err = ipif_logical_down(ipif, q, mp);
10942 		if (err == EINPROGRESS)
10943 			return (err);
10944 		ipif_down_tail(ipif);
10945 		need_up = 1;
10946 	}
10947 
10948 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
10949 	return (err);
10950 }
10951 
10952 int
10953 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10954     boolean_t need_up)
10955 {
10956 	in6_addr_t v6addr;
10957 	in6_addr_t ov6addr;
10958 	ipaddr_t addr;
10959 	sin6_t	*sin6;
10960 	int	sinlen;
10961 	int	err = 0;
10962 	ill_t	*ill = ipif->ipif_ill;
10963 	boolean_t need_dl_down;
10964 	boolean_t need_arp_down;
10965 	struct iocblk *iocp;
10966 
10967 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
10968 
10969 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
10970 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10971 	ASSERT(IAM_WRITER_IPIF(ipif));
10972 
10973 	/* Must cancel any pending timer before taking the ill_lock */
10974 	if (ipif->ipif_recovery_id != 0)
10975 		(void) untimeout(ipif->ipif_recovery_id);
10976 	ipif->ipif_recovery_id = 0;
10977 
10978 	if (ipif->ipif_isv6) {
10979 		sin6 = (sin6_t *)sin;
10980 		v6addr = sin6->sin6_addr;
10981 		sinlen = sizeof (struct sockaddr_in6);
10982 	} else {
10983 		addr = sin->sin_addr.s_addr;
10984 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10985 		sinlen = sizeof (struct sockaddr_in);
10986 	}
10987 	mutex_enter(&ill->ill_lock);
10988 	ov6addr = ipif->ipif_v6lcl_addr;
10989 	ipif->ipif_v6lcl_addr = v6addr;
10990 	sctp_update_ipif_addr(ipif, ov6addr);
10991 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
10992 		ipif->ipif_v6src_addr = ipv6_all_zeros;
10993 	} else {
10994 		ipif->ipif_v6src_addr = v6addr;
10995 	}
10996 	ipif->ipif_addr_ready = 0;
10997 
10998 	/*
10999 	 * If the interface was previously marked as a duplicate, then since
11000 	 * we've now got a "new" address, it should no longer be considered a
11001 	 * duplicate -- even if the "new" address is the same as the old one.
11002 	 * Note that if all ipifs are down, we may have a pending ARP down
11003 	 * event to handle.  This is because we want to recover from duplicates
11004 	 * and thus delay tearing down ARP until the duplicates have been
11005 	 * removed or disabled.
11006 	 */
11007 	need_dl_down = need_arp_down = B_FALSE;
11008 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11009 		need_arp_down = !need_up;
11010 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11011 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11012 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11013 			need_dl_down = B_TRUE;
11014 		}
11015 	}
11016 
11017 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11018 	    !ill->ill_is_6to4tun) {
11019 		queue_t *wqp = ill->ill_wq;
11020 
11021 		/*
11022 		 * The local address of this interface is a 6to4 address,
11023 		 * check if this interface is in fact a 6to4 tunnel or just
11024 		 * an interface configured with a 6to4 address.  We are only
11025 		 * interested in the former.
11026 		 */
11027 		if (wqp != NULL) {
11028 			while ((wqp->q_next != NULL) &&
11029 			    (wqp->q_next->q_qinfo != NULL) &&
11030 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11031 
11032 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11033 				    == TUN6TO4_MODID) {
11034 					/* set for use in IP */
11035 					ill->ill_is_6to4tun = 1;
11036 					break;
11037 				}
11038 				wqp = wqp->q_next;
11039 			}
11040 		}
11041 	}
11042 
11043 	ipif_set_default(ipif);
11044 
11045 	/*
11046 	 * When publishing an interface address change event, we only notify
11047 	 * the event listeners of the new address.  It is assumed that if they
11048 	 * actively care about the addresses assigned that they will have
11049 	 * already discovered the previous address assigned (if there was one.)
11050 	 *
11051 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11052 	 */
11053 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11054 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
11055 		    NE_ADDRESS_CHANGE, sin, sinlen);
11056 	}
11057 
11058 	mutex_exit(&ill->ill_lock);
11059 
11060 	if (need_up) {
11061 		/*
11062 		 * Now bring the interface back up.  If this
11063 		 * is the only IPIF for the ILL, ipif_up
11064 		 * will have to re-bind to the device, so
11065 		 * we may get back EINPROGRESS, in which
11066 		 * case, this IOCTL will get completed in
11067 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11068 		 */
11069 		err = ipif_up(ipif, q, mp);
11070 	}
11071 
11072 	if (need_dl_down)
11073 		ill_dl_down(ill);
11074 	if (need_arp_down)
11075 		ipif_arp_down(ipif);
11076 
11077 	return (err);
11078 }
11079 
11080 
11081 /*
11082  * Restart entry point to restart the address set operation after the
11083  * refcounts have dropped to zero.
11084  */
11085 /* ARGSUSED */
11086 int
11087 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11088     ip_ioctl_cmd_t *ipip, void *ifreq)
11089 {
11090 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11091 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11092 	ASSERT(IAM_WRITER_IPIF(ipif));
11093 	ipif_down_tail(ipif);
11094 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11095 }
11096 
11097 /* ARGSUSED */
11098 int
11099 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11100     ip_ioctl_cmd_t *ipip, void *if_req)
11101 {
11102 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11103 	struct lifreq *lifr = (struct lifreq *)if_req;
11104 
11105 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11106 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11107 	/*
11108 	 * The net mask and address can't change since we have a
11109 	 * reference to the ipif. So no lock is necessary.
11110 	 */
11111 	if (ipif->ipif_isv6) {
11112 		*sin6 = sin6_null;
11113 		sin6->sin6_family = AF_INET6;
11114 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11115 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11116 		lifr->lifr_addrlen =
11117 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11118 	} else {
11119 		*sin = sin_null;
11120 		sin->sin_family = AF_INET;
11121 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11122 		if (ipip->ipi_cmd_type == LIF_CMD) {
11123 			lifr->lifr_addrlen =
11124 			    ip_mask_to_plen(ipif->ipif_net_mask);
11125 		}
11126 	}
11127 	return (0);
11128 }
11129 
11130 /*
11131  * Set the destination address for a pt-pt interface.
11132  */
11133 /* ARGSUSED */
11134 int
11135 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11136     ip_ioctl_cmd_t *ipip, void *if_req)
11137 {
11138 	int err = 0;
11139 	in6_addr_t v6addr;
11140 	boolean_t need_up = B_FALSE;
11141 
11142 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11143 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11144 	ASSERT(IAM_WRITER_IPIF(ipif));
11145 
11146 	if (ipif->ipif_isv6) {
11147 		sin6_t *sin6;
11148 
11149 		if (sin->sin_family != AF_INET6)
11150 			return (EAFNOSUPPORT);
11151 
11152 		sin6 = (sin6_t *)sin;
11153 		v6addr = sin6->sin6_addr;
11154 
11155 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11156 			return (EADDRNOTAVAIL);
11157 	} else {
11158 		ipaddr_t addr;
11159 
11160 		if (sin->sin_family != AF_INET)
11161 			return (EAFNOSUPPORT);
11162 
11163 		addr = sin->sin_addr.s_addr;
11164 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11165 			return (EADDRNOTAVAIL);
11166 
11167 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11168 	}
11169 
11170 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11171 		return (0);	/* No change */
11172 
11173 	if (ipif->ipif_flags & IPIF_UP) {
11174 		/*
11175 		 * If the interface is already marked up,
11176 		 * we call ipif_down which will take care
11177 		 * of ditching any IREs that have been set
11178 		 * up based on the old pp dst address.
11179 		 */
11180 		err = ipif_logical_down(ipif, q, mp);
11181 		if (err == EINPROGRESS)
11182 			return (err);
11183 		ipif_down_tail(ipif);
11184 		need_up = B_TRUE;
11185 	}
11186 	/*
11187 	 * could return EINPROGRESS. If so ioctl will complete in
11188 	 * ip_rput_dlpi_writer
11189 	 */
11190 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11191 	return (err);
11192 }
11193 
11194 static int
11195 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11196     boolean_t need_up)
11197 {
11198 	in6_addr_t v6addr;
11199 	ill_t	*ill = ipif->ipif_ill;
11200 	int	err = 0;
11201 	boolean_t need_dl_down;
11202 	boolean_t need_arp_down;
11203 
11204 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11205 	    ipif->ipif_id, (void *)ipif));
11206 
11207 	/* Must cancel any pending timer before taking the ill_lock */
11208 	if (ipif->ipif_recovery_id != 0)
11209 		(void) untimeout(ipif->ipif_recovery_id);
11210 	ipif->ipif_recovery_id = 0;
11211 
11212 	if (ipif->ipif_isv6) {
11213 		sin6_t *sin6;
11214 
11215 		sin6 = (sin6_t *)sin;
11216 		v6addr = sin6->sin6_addr;
11217 	} else {
11218 		ipaddr_t addr;
11219 
11220 		addr = sin->sin_addr.s_addr;
11221 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11222 	}
11223 	mutex_enter(&ill->ill_lock);
11224 	/* Set point to point destination address. */
11225 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11226 		/*
11227 		 * Allow this as a means of creating logical
11228 		 * pt-pt interfaces on top of e.g. an Ethernet.
11229 		 * XXX Undocumented HACK for testing.
11230 		 * pt-pt interfaces are created with NUD disabled.
11231 		 */
11232 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11233 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11234 		if (ipif->ipif_isv6)
11235 			ill->ill_flags |= ILLF_NONUD;
11236 	}
11237 
11238 	/*
11239 	 * If the interface was previously marked as a duplicate, then since
11240 	 * we've now got a "new" address, it should no longer be considered a
11241 	 * duplicate -- even if the "new" address is the same as the old one.
11242 	 * Note that if all ipifs are down, we may have a pending ARP down
11243 	 * event to handle.
11244 	 */
11245 	need_dl_down = need_arp_down = B_FALSE;
11246 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11247 		need_arp_down = !need_up;
11248 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11249 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11250 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11251 			need_dl_down = B_TRUE;
11252 		}
11253 	}
11254 
11255 	/* Set the new address. */
11256 	ipif->ipif_v6pp_dst_addr = v6addr;
11257 	/* Make sure subnet tracks pp_dst */
11258 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11259 	mutex_exit(&ill->ill_lock);
11260 
11261 	if (need_up) {
11262 		/*
11263 		 * Now bring the interface back up.  If this
11264 		 * is the only IPIF for the ILL, ipif_up
11265 		 * will have to re-bind to the device, so
11266 		 * we may get back EINPROGRESS, in which
11267 		 * case, this IOCTL will get completed in
11268 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11269 		 */
11270 		err = ipif_up(ipif, q, mp);
11271 	}
11272 
11273 	if (need_dl_down)
11274 		ill_dl_down(ill);
11275 
11276 	if (need_arp_down)
11277 		ipif_arp_down(ipif);
11278 	return (err);
11279 }
11280 
11281 /*
11282  * Restart entry point to restart the dstaddress set operation after the
11283  * refcounts have dropped to zero.
11284  */
11285 /* ARGSUSED */
11286 int
11287 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11288     ip_ioctl_cmd_t *ipip, void *ifreq)
11289 {
11290 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11291 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11292 	ipif_down_tail(ipif);
11293 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11294 }
11295 
11296 /* ARGSUSED */
11297 int
11298 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11299     ip_ioctl_cmd_t *ipip, void *if_req)
11300 {
11301 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11302 
11303 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11304 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11305 	/*
11306 	 * Get point to point destination address. The addresses can't
11307 	 * change since we hold a reference to the ipif.
11308 	 */
11309 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11310 		return (EADDRNOTAVAIL);
11311 
11312 	if (ipif->ipif_isv6) {
11313 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11314 		*sin6 = sin6_null;
11315 		sin6->sin6_family = AF_INET6;
11316 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11317 	} else {
11318 		*sin = sin_null;
11319 		sin->sin_family = AF_INET;
11320 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11321 	}
11322 	return (0);
11323 }
11324 
11325 /*
11326  * part of ipmp, make this func return the active/inactive state and
11327  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11328  */
11329 /*
11330  * This function either sets or clears the IFF_INACTIVE flag.
11331  *
11332  * As long as there are some addresses or multicast memberships on the
11333  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11334  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11335  * will be used for outbound packets.
11336  *
11337  * Caller needs to verify the validity of setting IFF_INACTIVE.
11338  */
11339 static void
11340 phyint_inactive(phyint_t *phyi)
11341 {
11342 	ill_t *ill_v4;
11343 	ill_t *ill_v6;
11344 	ipif_t *ipif;
11345 	ilm_t *ilm;
11346 
11347 	ill_v4 = phyi->phyint_illv4;
11348 	ill_v6 = phyi->phyint_illv6;
11349 
11350 	/*
11351 	 * No need for a lock while traversing the list since iam
11352 	 * a writer
11353 	 */
11354 	if (ill_v4 != NULL) {
11355 		ASSERT(IAM_WRITER_ILL(ill_v4));
11356 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11357 		    ipif = ipif->ipif_next) {
11358 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11359 				mutex_enter(&phyi->phyint_lock);
11360 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11361 				mutex_exit(&phyi->phyint_lock);
11362 				return;
11363 			}
11364 		}
11365 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11366 		    ilm = ilm->ilm_next) {
11367 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11368 				mutex_enter(&phyi->phyint_lock);
11369 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11370 				mutex_exit(&phyi->phyint_lock);
11371 				return;
11372 			}
11373 		}
11374 	}
11375 	if (ill_v6 != NULL) {
11376 		ill_v6 = phyi->phyint_illv6;
11377 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11378 		    ipif = ipif->ipif_next) {
11379 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11380 				mutex_enter(&phyi->phyint_lock);
11381 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11382 				mutex_exit(&phyi->phyint_lock);
11383 				return;
11384 			}
11385 		}
11386 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11387 		    ilm = ilm->ilm_next) {
11388 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11389 				mutex_enter(&phyi->phyint_lock);
11390 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11391 				mutex_exit(&phyi->phyint_lock);
11392 				return;
11393 			}
11394 		}
11395 	}
11396 	mutex_enter(&phyi->phyint_lock);
11397 	phyi->phyint_flags |= PHYI_INACTIVE;
11398 	mutex_exit(&phyi->phyint_lock);
11399 }
11400 
11401 /*
11402  * This function is called only when the phyint flags change. Currently
11403  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11404  * that we can select a good ill.
11405  */
11406 static void
11407 ip_redo_nomination(phyint_t *phyi)
11408 {
11409 	ill_t *ill_v4;
11410 
11411 	ill_v4 = phyi->phyint_illv4;
11412 
11413 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11414 		ASSERT(IAM_WRITER_ILL(ill_v4));
11415 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11416 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11417 	}
11418 }
11419 
11420 /*
11421  * Heuristic to check if ill is INACTIVE.
11422  * Checks if ill has an ipif with an usable ip address.
11423  *
11424  * Return values:
11425  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11426  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11427  */
11428 static boolean_t
11429 ill_is_inactive(ill_t *ill)
11430 {
11431 	ipif_t *ipif;
11432 
11433 	/* Check whether it is in an IPMP group */
11434 	if (ill->ill_phyint->phyint_groupname == NULL)
11435 		return (B_FALSE);
11436 
11437 	if (ill->ill_ipif_up_count == 0)
11438 		return (B_TRUE);
11439 
11440 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11441 		uint64_t flags = ipif->ipif_flags;
11442 
11443 		/*
11444 		 * This ipif is usable if it is IPIF_UP and not a
11445 		 * dedicated test address.  A dedicated test address
11446 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11447 		 * (note in particular that V6 test addresses are
11448 		 * link-local data addresses and thus are marked
11449 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11450 		 */
11451 		if ((flags & IPIF_UP) &&
11452 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11453 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11454 			return (B_FALSE);
11455 	}
11456 	return (B_TRUE);
11457 }
11458 
11459 /*
11460  * Set interface flags.
11461  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11462  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11463  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11464  *
11465  * NOTE : We really don't enforce that ipif_id zero should be used
11466  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11467  *	  is because applications generally does SICGLIFFLAGS and
11468  *	  ORs in the new flags (that affects the logical) and does a
11469  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11470  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11471  *	  flags that will be turned on is correct with respect to
11472  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11473  */
11474 /* ARGSUSED */
11475 int
11476 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11477     ip_ioctl_cmd_t *ipip, void *if_req)
11478 {
11479 	uint64_t turn_on;
11480 	uint64_t turn_off;
11481 	int	err;
11482 	phyint_t *phyi;
11483 	ill_t *ill;
11484 	uint64_t intf_flags;
11485 	boolean_t phyint_flags_modified = B_FALSE;
11486 	uint64_t flags;
11487 	struct ifreq *ifr;
11488 	struct lifreq *lifr;
11489 	boolean_t set_linklocal = B_FALSE;
11490 	boolean_t zero_source = B_FALSE;
11491 	ip_stack_t *ipst;
11492 
11493 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11494 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11495 
11496 	ASSERT(IAM_WRITER_IPIF(ipif));
11497 
11498 	ill = ipif->ipif_ill;
11499 	phyi = ill->ill_phyint;
11500 	ipst = ill->ill_ipst;
11501 
11502 	if (ipip->ipi_cmd_type == IF_CMD) {
11503 		ifr = (struct ifreq *)if_req;
11504 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11505 	} else {
11506 		lifr = (struct lifreq *)if_req;
11507 		flags = lifr->lifr_flags;
11508 	}
11509 
11510 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11511 
11512 	/*
11513 	 * Have the flags been set correctly until now?
11514 	 */
11515 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11516 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11517 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11518 	/*
11519 	 * Compare the new flags to the old, and partition
11520 	 * into those coming on and those going off.
11521 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11522 	 */
11523 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11524 		flags |= intf_flags & ~0xFFFF;
11525 
11526 	/*
11527 	 * First check which bits will change and then which will
11528 	 * go on and off
11529 	 */
11530 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11531 	if (!turn_on)
11532 		return (0);	/* No change */
11533 
11534 	turn_off = intf_flags & turn_on;
11535 	turn_on ^= turn_off;
11536 	err = 0;
11537 
11538 	/*
11539 	 * Don't allow any bits belonging to the logical interface
11540 	 * to be set or cleared on the replacement ipif that was
11541 	 * created temporarily during a MOVE.
11542 	 */
11543 	if (ipif->ipif_replace_zero &&
11544 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11545 		return (EINVAL);
11546 	}
11547 
11548 	/*
11549 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11550 	 * IPv6 interfaces.
11551 	 */
11552 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11553 		return (EINVAL);
11554 
11555 	/*
11556 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11557 	 */
11558 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11559 		return (EINVAL);
11560 
11561 	/*
11562 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11563 	 * interfaces.  It makes no sense in that context.
11564 	 */
11565 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11566 		return (EINVAL);
11567 
11568 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11569 		zero_source = B_TRUE;
11570 
11571 	/*
11572 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11573 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11574 	 * If the link local address isn't set, and can be set, it will get
11575 	 * set later on in this function.
11576 	 */
11577 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11578 	    (flags & IFF_UP) && !zero_source &&
11579 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11580 		if (ipif_cant_setlinklocal(ipif))
11581 			return (EINVAL);
11582 		set_linklocal = B_TRUE;
11583 	}
11584 
11585 	/*
11586 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11587 	 * same time. No need to grab ill_g_usesrc_lock here, see
11588 	 * synchronization notes in ip.c
11589 	 */
11590 	if (turn_on & PHYI_STANDBY &&
11591 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11592 		return (EINVAL);
11593 	}
11594 
11595 	/*
11596 	 * If we modify physical interface flags, we'll potentially need to
11597 	 * send up two routing socket messages for the changes (one for the
11598 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11599 	 */
11600 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11601 		phyint_flags_modified = B_TRUE;
11602 
11603 	/*
11604 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11605 	 * we need to flush the IRE_CACHES belonging to this ill.
11606 	 * We handle this case here without doing the DOWN/UP dance
11607 	 * like it is done for other flags. If some other flags are
11608 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11609 	 * below will handle it by bringing it down and then
11610 	 * bringing it UP.
11611 	 */
11612 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11613 		ill_t *ill_v4, *ill_v6;
11614 
11615 		ill_v4 = phyi->phyint_illv4;
11616 		ill_v6 = phyi->phyint_illv6;
11617 
11618 		/*
11619 		 * First set the INACTIVE flag if needed. Then delete the ires.
11620 		 * ire_add will atomically prevent creating new IRE_CACHEs
11621 		 * unless hidden flag is set.
11622 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11623 		 */
11624 		if ((turn_on & PHYI_FAILED) &&
11625 		    ((intf_flags & PHYI_STANDBY) ||
11626 		    !ipst->ips_ipmp_enable_failback)) {
11627 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11628 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11629 		}
11630 		if ((turn_off & PHYI_FAILED) &&
11631 		    ((intf_flags & PHYI_STANDBY) ||
11632 		    (!ipst->ips_ipmp_enable_failback &&
11633 		    ill_is_inactive(ill)))) {
11634 			phyint_inactive(phyi);
11635 		}
11636 
11637 		if (turn_on & PHYI_STANDBY) {
11638 			/*
11639 			 * We implicitly set INACTIVE only when STANDBY is set.
11640 			 * INACTIVE is also set on non-STANDBY phyint when user
11641 			 * disables FAILBACK using configuration file.
11642 			 * Do not allow STANDBY to be set on such INACTIVE
11643 			 * phyint
11644 			 */
11645 			if (phyi->phyint_flags & PHYI_INACTIVE)
11646 				return (EINVAL);
11647 			if (!(phyi->phyint_flags & PHYI_FAILED))
11648 				phyint_inactive(phyi);
11649 		}
11650 		if (turn_off & PHYI_STANDBY) {
11651 			if (ipst->ips_ipmp_enable_failback) {
11652 				/*
11653 				 * Reset PHYI_INACTIVE.
11654 				 */
11655 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11656 			} else if (ill_is_inactive(ill) &&
11657 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11658 				/*
11659 				 * Need to set INACTIVE, when user sets
11660 				 * STANDBY on a non-STANDBY phyint and
11661 				 * later resets STANDBY
11662 				 */
11663 				phyint_inactive(phyi);
11664 			}
11665 		}
11666 		/*
11667 		 * We should always send up a message so that the
11668 		 * daemons come to know of it. Note that the zeroth
11669 		 * interface can be down and the check below for IPIF_UP
11670 		 * will not make sense as we are actually setting
11671 		 * a phyint flag here. We assume that the ipif used
11672 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11673 		 * send up any message for non-zero ipifs).
11674 		 */
11675 		phyint_flags_modified = B_TRUE;
11676 
11677 		if (ill_v4 != NULL) {
11678 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11679 			    IRE_CACHE, ill_stq_cache_delete,
11680 			    (char *)ill_v4, ill_v4);
11681 			illgrp_reset_schednext(ill_v4);
11682 		}
11683 		if (ill_v6 != NULL) {
11684 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11685 			    IRE_CACHE, ill_stq_cache_delete,
11686 			    (char *)ill_v6, ill_v6);
11687 			illgrp_reset_schednext(ill_v6);
11688 		}
11689 	}
11690 
11691 	/*
11692 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11693 	 * status of the interface and, if the interface is part of an IPMP
11694 	 * group, all other interfaces that are part of the same IPMP
11695 	 * group.
11696 	 */
11697 	if ((turn_on | turn_off) & ILLF_ROUTER)
11698 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11699 
11700 	/*
11701 	 * If the interface is not UP and we are not going to
11702 	 * bring it UP, record the flags and return. When the
11703 	 * interface comes UP later, the right actions will be
11704 	 * taken.
11705 	 */
11706 	if (!(ipif->ipif_flags & IPIF_UP) &&
11707 	    !(turn_on & IPIF_UP)) {
11708 		/* Record new flags in their respective places. */
11709 		mutex_enter(&ill->ill_lock);
11710 		mutex_enter(&ill->ill_phyint->phyint_lock);
11711 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11712 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11713 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11714 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11715 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11716 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11717 		mutex_exit(&ill->ill_lock);
11718 		mutex_exit(&ill->ill_phyint->phyint_lock);
11719 
11720 		/*
11721 		 * We do the broadcast and nomination here rather
11722 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11723 		 * the case of FAILBACK from INACTIVE standby to the
11724 		 * interface that has been repaired, PHYI_FAILED has not
11725 		 * been cleared yet. If there are only two interfaces in
11726 		 * that group, all we have is a FAILED and INACTIVE
11727 		 * interface. If we do the nomination soon after a failback,
11728 		 * the broadcast nomination code would select the
11729 		 * INACTIVE interface for receiving broadcasts as FAILED is
11730 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11731 		 * receive broadcast packets, we need to redo nomination
11732 		 * when the FAILED is cleared here. Thus, in general we
11733 		 * always do the nomination here for FAILED, STANDBY
11734 		 * and OFFLINE.
11735 		 */
11736 		if (((turn_on | turn_off) &
11737 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11738 			ip_redo_nomination(phyi);
11739 		}
11740 		if (phyint_flags_modified) {
11741 			if (phyi->phyint_illv4 != NULL) {
11742 				ip_rts_ifmsg(phyi->phyint_illv4->
11743 				    ill_ipif);
11744 			}
11745 			if (phyi->phyint_illv6 != NULL) {
11746 				ip_rts_ifmsg(phyi->phyint_illv6->
11747 				    ill_ipif);
11748 			}
11749 		}
11750 		return (0);
11751 	} else if (set_linklocal || zero_source) {
11752 		mutex_enter(&ill->ill_lock);
11753 		if (set_linklocal)
11754 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11755 		if (zero_source)
11756 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11757 		mutex_exit(&ill->ill_lock);
11758 	}
11759 
11760 	/*
11761 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11762 	 * or point-to-point interfaces with an unspecified destination. We do
11763 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11764 	 * have a subnet assigned, which is how in.ndpd currently manages its
11765 	 * onlink prefix list when no addresses are configured with those
11766 	 * prefixes.
11767 	 */
11768 	if (ipif->ipif_isv6 &&
11769 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11770 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11771 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11772 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11773 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11774 		return (EINVAL);
11775 	}
11776 
11777 	/*
11778 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11779 	 * from being brought up.
11780 	 */
11781 	if (!ipif->ipif_isv6 &&
11782 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11783 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11784 		return (EINVAL);
11785 	}
11786 
11787 	/*
11788 	 * The only flag changes that we currently take specific action on
11789 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11790 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11791 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11792 	 * the flags and bringing it back up again.
11793 	 */
11794 	if ((turn_on|turn_off) &
11795 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11796 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11797 		/*
11798 		 * Taking this ipif down, make sure we have
11799 		 * valid net and subnet bcast ire's for other
11800 		 * logical interfaces, if we need them.
11801 		 */
11802 		if (!ipif->ipif_isv6)
11803 			ipif_check_bcast_ires(ipif);
11804 
11805 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11806 		    !(turn_off & IPIF_UP)) {
11807 			if (ipif->ipif_flags & IPIF_UP)
11808 				ill->ill_logical_down = 1;
11809 			turn_on &= ~IPIF_UP;
11810 		}
11811 		err = ipif_down(ipif, q, mp);
11812 		ip1dbg(("ipif_down returns %d err ", err));
11813 		if (err == EINPROGRESS)
11814 			return (err);
11815 		ipif_down_tail(ipif);
11816 	}
11817 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11818 }
11819 
11820 static int
11821 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
11822 {
11823 	ill_t	*ill;
11824 	phyint_t *phyi;
11825 	uint64_t turn_on;
11826 	uint64_t turn_off;
11827 	uint64_t intf_flags;
11828 	boolean_t phyint_flags_modified = B_FALSE;
11829 	int	err = 0;
11830 	boolean_t set_linklocal = B_FALSE;
11831 	boolean_t zero_source = B_FALSE;
11832 
11833 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11834 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
11835 
11836 	ASSERT(IAM_WRITER_IPIF(ipif));
11837 
11838 	ill = ipif->ipif_ill;
11839 	phyi = ill->ill_phyint;
11840 
11841 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11842 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
11843 
11844 	turn_off = intf_flags & turn_on;
11845 	turn_on ^= turn_off;
11846 
11847 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
11848 		phyint_flags_modified = B_TRUE;
11849 
11850 	/*
11851 	 * Now we change the flags. Track current value of
11852 	 * other flags in their respective places.
11853 	 */
11854 	mutex_enter(&ill->ill_lock);
11855 	mutex_enter(&phyi->phyint_lock);
11856 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11857 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11858 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11859 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11860 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11861 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11862 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11863 		set_linklocal = B_TRUE;
11864 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11865 	}
11866 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11867 		zero_source = B_TRUE;
11868 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11869 	}
11870 	mutex_exit(&ill->ill_lock);
11871 	mutex_exit(&phyi->phyint_lock);
11872 
11873 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
11874 		ip_redo_nomination(phyi);
11875 
11876 	if (set_linklocal)
11877 		(void) ipif_setlinklocal(ipif);
11878 
11879 	if (zero_source)
11880 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11881 	else
11882 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11883 
11884 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
11885 		/*
11886 		 * XXX ipif_up really does not know whether a phyint flags
11887 		 * was modified or not. So, it sends up information on
11888 		 * only one routing sockets message. As we don't bring up
11889 		 * the interface and also set STANDBY/FAILED simultaneously
11890 		 * it should be okay.
11891 		 */
11892 		err = ipif_up(ipif, q, mp);
11893 	} else {
11894 		/*
11895 		 * Make sure routing socket sees all changes to the flags.
11896 		 * ipif_up_done* handles this when we use ipif_up.
11897 		 */
11898 		if (phyint_flags_modified) {
11899 			if (phyi->phyint_illv4 != NULL) {
11900 				ip_rts_ifmsg(phyi->phyint_illv4->
11901 				    ill_ipif);
11902 			}
11903 			if (phyi->phyint_illv6 != NULL) {
11904 				ip_rts_ifmsg(phyi->phyint_illv6->
11905 				    ill_ipif);
11906 			}
11907 		} else {
11908 			ip_rts_ifmsg(ipif);
11909 		}
11910 		/*
11911 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
11912 		 * this in need_up case.
11913 		 */
11914 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11915 	}
11916 	return (err);
11917 }
11918 
11919 /*
11920  * Restart the flags operation now that the refcounts have dropped to zero.
11921  */
11922 /* ARGSUSED */
11923 int
11924 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11925     ip_ioctl_cmd_t *ipip, void *if_req)
11926 {
11927 	uint64_t flags;
11928 	struct ifreq *ifr = if_req;
11929 	struct lifreq *lifr = if_req;
11930 
11931 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11932 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11933 
11934 	ipif_down_tail(ipif);
11935 	if (ipip->ipi_cmd_type == IF_CMD) {
11936 		/* cast to uint16_t prevents unwanted sign extension */
11937 		flags = (uint16_t)ifr->ifr_flags;
11938 	} else {
11939 		flags = lifr->lifr_flags;
11940 	}
11941 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11942 }
11943 
11944 /*
11945  * Can operate on either a module or a driver queue.
11946  */
11947 /* ARGSUSED */
11948 int
11949 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11950     ip_ioctl_cmd_t *ipip, void *if_req)
11951 {
11952 	/*
11953 	 * Has the flags been set correctly till now ?
11954 	 */
11955 	ill_t *ill = ipif->ipif_ill;
11956 	phyint_t *phyi = ill->ill_phyint;
11957 
11958 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11959 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11960 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11961 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11962 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11963 
11964 	/*
11965 	 * Need a lock since some flags can be set even when there are
11966 	 * references to the ipif.
11967 	 */
11968 	mutex_enter(&ill->ill_lock);
11969 	if (ipip->ipi_cmd_type == IF_CMD) {
11970 		struct ifreq *ifr = (struct ifreq *)if_req;
11971 
11972 		/* Get interface flags (low 16 only). */
11973 		ifr->ifr_flags = ((ipif->ipif_flags |
11974 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11975 	} else {
11976 		struct lifreq *lifr = (struct lifreq *)if_req;
11977 
11978 		/* Get interface flags. */
11979 		lifr->lifr_flags = ipif->ipif_flags |
11980 		    ill->ill_flags | phyi->phyint_flags;
11981 	}
11982 	mutex_exit(&ill->ill_lock);
11983 	return (0);
11984 }
11985 
11986 /* ARGSUSED */
11987 int
11988 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11989     ip_ioctl_cmd_t *ipip, void *if_req)
11990 {
11991 	int mtu;
11992 	int ip_min_mtu;
11993 	struct ifreq	*ifr;
11994 	struct lifreq *lifr;
11995 	ire_t	*ire;
11996 	ip_stack_t *ipst;
11997 
11998 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11999 	    ipif->ipif_id, (void *)ipif));
12000 	if (ipip->ipi_cmd_type == IF_CMD) {
12001 		ifr = (struct ifreq *)if_req;
12002 		mtu = ifr->ifr_metric;
12003 	} else {
12004 		lifr = (struct lifreq *)if_req;
12005 		mtu = lifr->lifr_mtu;
12006 	}
12007 
12008 	if (ipif->ipif_isv6)
12009 		ip_min_mtu = IPV6_MIN_MTU;
12010 	else
12011 		ip_min_mtu = IP_MIN_MTU;
12012 
12013 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12014 		return (EINVAL);
12015 
12016 	/*
12017 	 * Change the MTU size in all relevant ire's.
12018 	 * Mtu change Vs. new ire creation - protocol below.
12019 	 * First change ipif_mtu and the ire_max_frag of the
12020 	 * interface ire. Then do an ire walk and change the
12021 	 * ire_max_frag of all affected ires. During ire_add
12022 	 * under the bucket lock, set the ire_max_frag of the
12023 	 * new ire being created from the ipif/ire from which
12024 	 * it is being derived. If an mtu change happens after
12025 	 * the ire is added, the new ire will be cleaned up.
12026 	 * Conversely if the mtu change happens before the ire
12027 	 * is added, ire_add will see the new value of the mtu.
12028 	 */
12029 	ipif->ipif_mtu = mtu;
12030 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12031 
12032 	if (ipif->ipif_isv6)
12033 		ire = ipif_to_ire_v6(ipif);
12034 	else
12035 		ire = ipif_to_ire(ipif);
12036 	if (ire != NULL) {
12037 		ire->ire_max_frag = ipif->ipif_mtu;
12038 		ire_refrele(ire);
12039 	}
12040 	ipst = ipif->ipif_ill->ill_ipst;
12041 	if (ipif->ipif_flags & IPIF_UP) {
12042 		if (ipif->ipif_isv6)
12043 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12044 			    ipst);
12045 		else
12046 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12047 			    ipst);
12048 	}
12049 	/* Update the MTU in SCTP's list */
12050 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12051 	return (0);
12052 }
12053 
12054 /* Get interface MTU. */
12055 /* ARGSUSED */
12056 int
12057 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12058 	ip_ioctl_cmd_t *ipip, void *if_req)
12059 {
12060 	struct ifreq	*ifr;
12061 	struct lifreq	*lifr;
12062 
12063 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12064 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12065 	if (ipip->ipi_cmd_type == IF_CMD) {
12066 		ifr = (struct ifreq *)if_req;
12067 		ifr->ifr_metric = ipif->ipif_mtu;
12068 	} else {
12069 		lifr = (struct lifreq *)if_req;
12070 		lifr->lifr_mtu = ipif->ipif_mtu;
12071 	}
12072 	return (0);
12073 }
12074 
12075 /* Set interface broadcast address. */
12076 /* ARGSUSED2 */
12077 int
12078 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12079 	ip_ioctl_cmd_t *ipip, void *if_req)
12080 {
12081 	ipaddr_t addr;
12082 	ire_t	*ire;
12083 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12084 
12085 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12086 	    ipif->ipif_id));
12087 
12088 	ASSERT(IAM_WRITER_IPIF(ipif));
12089 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12090 		return (EADDRNOTAVAIL);
12091 
12092 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12093 
12094 	if (sin->sin_family != AF_INET)
12095 		return (EAFNOSUPPORT);
12096 
12097 	addr = sin->sin_addr.s_addr;
12098 	if (ipif->ipif_flags & IPIF_UP) {
12099 		/*
12100 		 * If we are already up, make sure the new
12101 		 * broadcast address makes sense.  If it does,
12102 		 * there should be an IRE for it already.
12103 		 * Don't match on ipif, only on the ill
12104 		 * since we are sharing these now. Don't use
12105 		 * MATCH_IRE_ILL_GROUP as we are looking for
12106 		 * the broadcast ire on this ill and each ill
12107 		 * in the group has its own broadcast ire.
12108 		 */
12109 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12110 		    ipif, ALL_ZONES, NULL,
12111 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12112 		if (ire == NULL) {
12113 			return (EINVAL);
12114 		} else {
12115 			ire_refrele(ire);
12116 		}
12117 	}
12118 	/*
12119 	 * Changing the broadcast addr for this ipif.
12120 	 * Make sure we have valid net and subnet bcast
12121 	 * ire's for other logical interfaces, if needed.
12122 	 */
12123 	if (addr != ipif->ipif_brd_addr)
12124 		ipif_check_bcast_ires(ipif);
12125 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12126 	return (0);
12127 }
12128 
12129 /* Get interface broadcast address. */
12130 /* ARGSUSED */
12131 int
12132 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12133     ip_ioctl_cmd_t *ipip, void *if_req)
12134 {
12135 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12136 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12137 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12138 		return (EADDRNOTAVAIL);
12139 
12140 	/* IPIF_BROADCAST not possible with IPv6 */
12141 	ASSERT(!ipif->ipif_isv6);
12142 	*sin = sin_null;
12143 	sin->sin_family = AF_INET;
12144 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12145 	return (0);
12146 }
12147 
12148 /*
12149  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12150  */
12151 /* ARGSUSED */
12152 int
12153 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12154     ip_ioctl_cmd_t *ipip, void *if_req)
12155 {
12156 	int err = 0;
12157 	in6_addr_t v6mask;
12158 
12159 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12160 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12161 
12162 	ASSERT(IAM_WRITER_IPIF(ipif));
12163 
12164 	if (ipif->ipif_isv6) {
12165 		sin6_t *sin6;
12166 
12167 		if (sin->sin_family != AF_INET6)
12168 			return (EAFNOSUPPORT);
12169 
12170 		sin6 = (sin6_t *)sin;
12171 		v6mask = sin6->sin6_addr;
12172 	} else {
12173 		ipaddr_t mask;
12174 
12175 		if (sin->sin_family != AF_INET)
12176 			return (EAFNOSUPPORT);
12177 
12178 		mask = sin->sin_addr.s_addr;
12179 		V4MASK_TO_V6(mask, v6mask);
12180 	}
12181 
12182 	/*
12183 	 * No big deal if the interface isn't already up, or the mask
12184 	 * isn't really changing, or this is pt-pt.
12185 	 */
12186 	if (!(ipif->ipif_flags & IPIF_UP) ||
12187 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12188 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12189 		ipif->ipif_v6net_mask = v6mask;
12190 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12191 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12192 			    ipif->ipif_v6net_mask,
12193 			    ipif->ipif_v6subnet);
12194 		}
12195 		return (0);
12196 	}
12197 	/*
12198 	 * Make sure we have valid net and subnet broadcast ire's
12199 	 * for the old netmask, if needed by other logical interfaces.
12200 	 */
12201 	if (!ipif->ipif_isv6)
12202 		ipif_check_bcast_ires(ipif);
12203 
12204 	err = ipif_logical_down(ipif, q, mp);
12205 	if (err == EINPROGRESS)
12206 		return (err);
12207 	ipif_down_tail(ipif);
12208 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12209 	return (err);
12210 }
12211 
12212 static int
12213 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12214 {
12215 	in6_addr_t v6mask;
12216 	int err = 0;
12217 
12218 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12219 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12220 
12221 	if (ipif->ipif_isv6) {
12222 		sin6_t *sin6;
12223 
12224 		sin6 = (sin6_t *)sin;
12225 		v6mask = sin6->sin6_addr;
12226 	} else {
12227 		ipaddr_t mask;
12228 
12229 		mask = sin->sin_addr.s_addr;
12230 		V4MASK_TO_V6(mask, v6mask);
12231 	}
12232 
12233 	ipif->ipif_v6net_mask = v6mask;
12234 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12235 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12236 		    ipif->ipif_v6subnet);
12237 	}
12238 	err = ipif_up(ipif, q, mp);
12239 
12240 	if (err == 0 || err == EINPROGRESS) {
12241 		/*
12242 		 * The interface must be DL_BOUND if this packet has to
12243 		 * go out on the wire. Since we only go through a logical
12244 		 * down and are bound with the driver during an internal
12245 		 * down/up that is satisfied.
12246 		 */
12247 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12248 			/* Potentially broadcast an address mask reply. */
12249 			ipif_mask_reply(ipif);
12250 		}
12251 	}
12252 	return (err);
12253 }
12254 
12255 /* ARGSUSED */
12256 int
12257 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12258     ip_ioctl_cmd_t *ipip, void *if_req)
12259 {
12260 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12261 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12262 	ipif_down_tail(ipif);
12263 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12264 }
12265 
12266 /* Get interface net mask. */
12267 /* ARGSUSED */
12268 int
12269 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12270     ip_ioctl_cmd_t *ipip, void *if_req)
12271 {
12272 	struct lifreq *lifr = (struct lifreq *)if_req;
12273 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12274 
12275 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12276 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12277 
12278 	/*
12279 	 * net mask can't change since we have a reference to the ipif.
12280 	 */
12281 	if (ipif->ipif_isv6) {
12282 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12283 		*sin6 = sin6_null;
12284 		sin6->sin6_family = AF_INET6;
12285 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12286 		lifr->lifr_addrlen =
12287 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12288 	} else {
12289 		*sin = sin_null;
12290 		sin->sin_family = AF_INET;
12291 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12292 		if (ipip->ipi_cmd_type == LIF_CMD) {
12293 			lifr->lifr_addrlen =
12294 			    ip_mask_to_plen(ipif->ipif_net_mask);
12295 		}
12296 	}
12297 	return (0);
12298 }
12299 
12300 /* ARGSUSED */
12301 int
12302 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12303     ip_ioctl_cmd_t *ipip, void *if_req)
12304 {
12305 
12306 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12307 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12308 	/*
12309 	 * Set interface metric.  We don't use this for
12310 	 * anything but we keep track of it in case it is
12311 	 * important to routing applications or such.
12312 	 */
12313 	if (ipip->ipi_cmd_type == IF_CMD) {
12314 		struct ifreq    *ifr;
12315 
12316 		ifr = (struct ifreq *)if_req;
12317 		ipif->ipif_metric = ifr->ifr_metric;
12318 	} else {
12319 		struct lifreq   *lifr;
12320 
12321 		lifr = (struct lifreq *)if_req;
12322 		ipif->ipif_metric = lifr->lifr_metric;
12323 	}
12324 	return (0);
12325 }
12326 
12327 /* ARGSUSED */
12328 int
12329 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12330     ip_ioctl_cmd_t *ipip, void *if_req)
12331 {
12332 	/* Get interface metric. */
12333 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12334 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12335 	if (ipip->ipi_cmd_type == IF_CMD) {
12336 		struct ifreq    *ifr;
12337 
12338 		ifr = (struct ifreq *)if_req;
12339 		ifr->ifr_metric = ipif->ipif_metric;
12340 	} else {
12341 		struct lifreq   *lifr;
12342 
12343 		lifr = (struct lifreq *)if_req;
12344 		lifr->lifr_metric = ipif->ipif_metric;
12345 	}
12346 
12347 	return (0);
12348 }
12349 
12350 /* ARGSUSED */
12351 int
12352 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12353     ip_ioctl_cmd_t *ipip, void *if_req)
12354 {
12355 
12356 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12357 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12358 	/*
12359 	 * Set the muxid returned from I_PLINK.
12360 	 */
12361 	if (ipip->ipi_cmd_type == IF_CMD) {
12362 		struct ifreq *ifr = (struct ifreq *)if_req;
12363 
12364 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12365 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12366 	} else {
12367 		struct lifreq *lifr = (struct lifreq *)if_req;
12368 
12369 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12370 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12371 	}
12372 	return (0);
12373 }
12374 
12375 /* ARGSUSED */
12376 int
12377 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12378     ip_ioctl_cmd_t *ipip, void *if_req)
12379 {
12380 
12381 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12382 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12383 	/*
12384 	 * Get the muxid saved in ill for I_PUNLINK.
12385 	 */
12386 	if (ipip->ipi_cmd_type == IF_CMD) {
12387 		struct ifreq *ifr = (struct ifreq *)if_req;
12388 
12389 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12390 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12391 	} else {
12392 		struct lifreq *lifr = (struct lifreq *)if_req;
12393 
12394 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12395 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12396 	}
12397 	return (0);
12398 }
12399 
12400 /*
12401  * Set the subnet prefix. Does not modify the broadcast address.
12402  */
12403 /* ARGSUSED */
12404 int
12405 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12406     ip_ioctl_cmd_t *ipip, void *if_req)
12407 {
12408 	int err = 0;
12409 	in6_addr_t v6addr;
12410 	in6_addr_t v6mask;
12411 	boolean_t need_up = B_FALSE;
12412 	int addrlen;
12413 
12414 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12415 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12416 
12417 	ASSERT(IAM_WRITER_IPIF(ipif));
12418 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12419 
12420 	if (ipif->ipif_isv6) {
12421 		sin6_t *sin6;
12422 
12423 		if (sin->sin_family != AF_INET6)
12424 			return (EAFNOSUPPORT);
12425 
12426 		sin6 = (sin6_t *)sin;
12427 		v6addr = sin6->sin6_addr;
12428 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12429 			return (EADDRNOTAVAIL);
12430 	} else {
12431 		ipaddr_t addr;
12432 
12433 		if (sin->sin_family != AF_INET)
12434 			return (EAFNOSUPPORT);
12435 
12436 		addr = sin->sin_addr.s_addr;
12437 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12438 			return (EADDRNOTAVAIL);
12439 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12440 		/* Add 96 bits */
12441 		addrlen += IPV6_ABITS - IP_ABITS;
12442 	}
12443 
12444 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12445 		return (EINVAL);
12446 
12447 	/* Check if bits in the address is set past the mask */
12448 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12449 		return (EINVAL);
12450 
12451 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12452 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12453 		return (0);	/* No change */
12454 
12455 	if (ipif->ipif_flags & IPIF_UP) {
12456 		/*
12457 		 * If the interface is already marked up,
12458 		 * we call ipif_down which will take care
12459 		 * of ditching any IREs that have been set
12460 		 * up based on the old interface address.
12461 		 */
12462 		err = ipif_logical_down(ipif, q, mp);
12463 		if (err == EINPROGRESS)
12464 			return (err);
12465 		ipif_down_tail(ipif);
12466 		need_up = B_TRUE;
12467 	}
12468 
12469 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12470 	return (err);
12471 }
12472 
12473 static int
12474 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12475     queue_t *q, mblk_t *mp, boolean_t need_up)
12476 {
12477 	ill_t	*ill = ipif->ipif_ill;
12478 	int	err = 0;
12479 
12480 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12481 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12482 
12483 	/* Set the new address. */
12484 	mutex_enter(&ill->ill_lock);
12485 	ipif->ipif_v6net_mask = v6mask;
12486 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12487 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12488 		    ipif->ipif_v6subnet);
12489 	}
12490 	mutex_exit(&ill->ill_lock);
12491 
12492 	if (need_up) {
12493 		/*
12494 		 * Now bring the interface back up.  If this
12495 		 * is the only IPIF for the ILL, ipif_up
12496 		 * will have to re-bind to the device, so
12497 		 * we may get back EINPROGRESS, in which
12498 		 * case, this IOCTL will get completed in
12499 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12500 		 */
12501 		err = ipif_up(ipif, q, mp);
12502 		if (err == EINPROGRESS)
12503 			return (err);
12504 	}
12505 	return (err);
12506 }
12507 
12508 /* ARGSUSED */
12509 int
12510 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12511     ip_ioctl_cmd_t *ipip, void *if_req)
12512 {
12513 	int	addrlen;
12514 	in6_addr_t v6addr;
12515 	in6_addr_t v6mask;
12516 	struct lifreq *lifr = (struct lifreq *)if_req;
12517 
12518 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12519 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12520 	ipif_down_tail(ipif);
12521 
12522 	addrlen = lifr->lifr_addrlen;
12523 	if (ipif->ipif_isv6) {
12524 		sin6_t *sin6;
12525 
12526 		sin6 = (sin6_t *)sin;
12527 		v6addr = sin6->sin6_addr;
12528 	} else {
12529 		ipaddr_t addr;
12530 
12531 		addr = sin->sin_addr.s_addr;
12532 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12533 		addrlen += IPV6_ABITS - IP_ABITS;
12534 	}
12535 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12536 
12537 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12538 }
12539 
12540 /* ARGSUSED */
12541 int
12542 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12543     ip_ioctl_cmd_t *ipip, void *if_req)
12544 {
12545 	struct lifreq *lifr = (struct lifreq *)if_req;
12546 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12547 
12548 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12549 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12550 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12551 
12552 	if (ipif->ipif_isv6) {
12553 		*sin6 = sin6_null;
12554 		sin6->sin6_family = AF_INET6;
12555 		sin6->sin6_addr = ipif->ipif_v6subnet;
12556 		lifr->lifr_addrlen =
12557 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12558 	} else {
12559 		*sin = sin_null;
12560 		sin->sin_family = AF_INET;
12561 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12562 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12563 	}
12564 	return (0);
12565 }
12566 
12567 /*
12568  * Set the IPv6 address token.
12569  */
12570 /* ARGSUSED */
12571 int
12572 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12573     ip_ioctl_cmd_t *ipi, void *if_req)
12574 {
12575 	ill_t *ill = ipif->ipif_ill;
12576 	int err;
12577 	in6_addr_t v6addr;
12578 	in6_addr_t v6mask;
12579 	boolean_t need_up = B_FALSE;
12580 	int i;
12581 	sin6_t *sin6 = (sin6_t *)sin;
12582 	struct lifreq *lifr = (struct lifreq *)if_req;
12583 	int addrlen;
12584 
12585 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12586 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12587 	ASSERT(IAM_WRITER_IPIF(ipif));
12588 
12589 	addrlen = lifr->lifr_addrlen;
12590 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12591 	if (ipif->ipif_id != 0)
12592 		return (EINVAL);
12593 
12594 	if (!ipif->ipif_isv6)
12595 		return (EINVAL);
12596 
12597 	if (addrlen > IPV6_ABITS)
12598 		return (EINVAL);
12599 
12600 	v6addr = sin6->sin6_addr;
12601 
12602 	/*
12603 	 * The length of the token is the length from the end.  To get
12604 	 * the proper mask for this, compute the mask of the bits not
12605 	 * in the token; ie. the prefix, and then xor to get the mask.
12606 	 */
12607 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12608 		return (EINVAL);
12609 	for (i = 0; i < 4; i++) {
12610 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12611 	}
12612 
12613 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12614 	    ill->ill_token_length == addrlen)
12615 		return (0);	/* No change */
12616 
12617 	if (ipif->ipif_flags & IPIF_UP) {
12618 		err = ipif_logical_down(ipif, q, mp);
12619 		if (err == EINPROGRESS)
12620 			return (err);
12621 		ipif_down_tail(ipif);
12622 		need_up = B_TRUE;
12623 	}
12624 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12625 	return (err);
12626 }
12627 
12628 static int
12629 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12630     mblk_t *mp, boolean_t need_up)
12631 {
12632 	in6_addr_t v6addr;
12633 	in6_addr_t v6mask;
12634 	ill_t	*ill = ipif->ipif_ill;
12635 	int	i;
12636 	int	err = 0;
12637 
12638 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12639 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12640 	v6addr = sin6->sin6_addr;
12641 	/*
12642 	 * The length of the token is the length from the end.  To get
12643 	 * the proper mask for this, compute the mask of the bits not
12644 	 * in the token; ie. the prefix, and then xor to get the mask.
12645 	 */
12646 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12647 	for (i = 0; i < 4; i++)
12648 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12649 
12650 	mutex_enter(&ill->ill_lock);
12651 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12652 	ill->ill_token_length = addrlen;
12653 	mutex_exit(&ill->ill_lock);
12654 
12655 	if (need_up) {
12656 		/*
12657 		 * Now bring the interface back up.  If this
12658 		 * is the only IPIF for the ILL, ipif_up
12659 		 * will have to re-bind to the device, so
12660 		 * we may get back EINPROGRESS, in which
12661 		 * case, this IOCTL will get completed in
12662 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12663 		 */
12664 		err = ipif_up(ipif, q, mp);
12665 		if (err == EINPROGRESS)
12666 			return (err);
12667 	}
12668 	return (err);
12669 }
12670 
12671 /* ARGSUSED */
12672 int
12673 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12674     ip_ioctl_cmd_t *ipi, void *if_req)
12675 {
12676 	ill_t *ill;
12677 	sin6_t *sin6 = (sin6_t *)sin;
12678 	struct lifreq *lifr = (struct lifreq *)if_req;
12679 
12680 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12681 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12682 	if (ipif->ipif_id != 0)
12683 		return (EINVAL);
12684 
12685 	ill = ipif->ipif_ill;
12686 	if (!ill->ill_isv6)
12687 		return (ENXIO);
12688 
12689 	*sin6 = sin6_null;
12690 	sin6->sin6_family = AF_INET6;
12691 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12692 	sin6->sin6_addr = ill->ill_token;
12693 	lifr->lifr_addrlen = ill->ill_token_length;
12694 	return (0);
12695 }
12696 
12697 /*
12698  * Set (hardware) link specific information that might override
12699  * what was acquired through the DL_INFO_ACK.
12700  * The logic is as follows.
12701  *
12702  * become exclusive
12703  * set CHANGING flag
12704  * change mtu on affected IREs
12705  * clear CHANGING flag
12706  *
12707  * An ire add that occurs before the CHANGING flag is set will have its mtu
12708  * changed by the ip_sioctl_lnkinfo.
12709  *
12710  * During the time the CHANGING flag is set, no new ires will be added to the
12711  * bucket, and ire add will fail (due the CHANGING flag).
12712  *
12713  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12714  * before it is added to the bucket.
12715  *
12716  * Obviously only 1 thread can set the CHANGING flag and we need to become
12717  * exclusive to set the flag.
12718  */
12719 /* ARGSUSED */
12720 int
12721 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12722     ip_ioctl_cmd_t *ipi, void *if_req)
12723 {
12724 	ill_t		*ill = ipif->ipif_ill;
12725 	ipif_t		*nipif;
12726 	int		ip_min_mtu;
12727 	boolean_t	mtu_walk = B_FALSE;
12728 	struct lifreq	*lifr = (struct lifreq *)if_req;
12729 	lif_ifinfo_req_t *lir;
12730 	ire_t		*ire;
12731 
12732 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12733 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12734 	lir = &lifr->lifr_ifinfo;
12735 	ASSERT(IAM_WRITER_IPIF(ipif));
12736 
12737 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12738 	if (ipif->ipif_id != 0)
12739 		return (EINVAL);
12740 
12741 	/* Set interface MTU. */
12742 	if (ipif->ipif_isv6)
12743 		ip_min_mtu = IPV6_MIN_MTU;
12744 	else
12745 		ip_min_mtu = IP_MIN_MTU;
12746 
12747 	/*
12748 	 * Verify values before we set anything. Allow zero to
12749 	 * mean unspecified.
12750 	 */
12751 	if (lir->lir_maxmtu != 0 &&
12752 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12753 	    lir->lir_maxmtu < ip_min_mtu))
12754 		return (EINVAL);
12755 	if (lir->lir_reachtime != 0 &&
12756 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12757 		return (EINVAL);
12758 	if (lir->lir_reachretrans != 0 &&
12759 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12760 		return (EINVAL);
12761 
12762 	mutex_enter(&ill->ill_lock);
12763 	ill->ill_state_flags |= ILL_CHANGING;
12764 	for (nipif = ill->ill_ipif; nipif != NULL;
12765 	    nipif = nipif->ipif_next) {
12766 		nipif->ipif_state_flags |= IPIF_CHANGING;
12767 	}
12768 
12769 	mutex_exit(&ill->ill_lock);
12770 
12771 	if (lir->lir_maxmtu != 0) {
12772 		ill->ill_max_mtu = lir->lir_maxmtu;
12773 		ill->ill_mtu_userspecified = 1;
12774 		mtu_walk = B_TRUE;
12775 	}
12776 
12777 	if (lir->lir_reachtime != 0)
12778 		ill->ill_reachable_time = lir->lir_reachtime;
12779 
12780 	if (lir->lir_reachretrans != 0)
12781 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12782 
12783 	ill->ill_max_hops = lir->lir_maxhops;
12784 
12785 	ill->ill_max_buf = ND_MAX_Q;
12786 
12787 	if (mtu_walk) {
12788 		/*
12789 		 * Set the MTU on all ipifs associated with this ill except
12790 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12791 		 */
12792 		for (nipif = ill->ill_ipif; nipif != NULL;
12793 		    nipif = nipif->ipif_next) {
12794 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12795 				continue;
12796 
12797 			nipif->ipif_mtu = ill->ill_max_mtu;
12798 
12799 			if (!(nipif->ipif_flags & IPIF_UP))
12800 				continue;
12801 
12802 			if (nipif->ipif_isv6)
12803 				ire = ipif_to_ire_v6(nipif);
12804 			else
12805 				ire = ipif_to_ire(nipif);
12806 			if (ire != NULL) {
12807 				ire->ire_max_frag = ipif->ipif_mtu;
12808 				ire_refrele(ire);
12809 			}
12810 
12811 			ire_walk_ill(MATCH_IRE_ILL, 0, ipif_mtu_change,
12812 			    nipif, ill);
12813 		}
12814 	}
12815 
12816 	mutex_enter(&ill->ill_lock);
12817 	for (nipif = ill->ill_ipif; nipif != NULL;
12818 	    nipif = nipif->ipif_next) {
12819 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12820 	}
12821 	ILL_UNMARK_CHANGING(ill);
12822 	mutex_exit(&ill->ill_lock);
12823 
12824 	return (0);
12825 }
12826 
12827 /* ARGSUSED */
12828 int
12829 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12830     ip_ioctl_cmd_t *ipi, void *if_req)
12831 {
12832 	struct lif_ifinfo_req *lir;
12833 	ill_t *ill = ipif->ipif_ill;
12834 
12835 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12836 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12837 	if (ipif->ipif_id != 0)
12838 		return (EINVAL);
12839 
12840 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12841 	lir->lir_maxhops = ill->ill_max_hops;
12842 	lir->lir_reachtime = ill->ill_reachable_time;
12843 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12844 	lir->lir_maxmtu = ill->ill_max_mtu;
12845 
12846 	return (0);
12847 }
12848 
12849 /*
12850  * Return best guess as to the subnet mask for the specified address.
12851  * Based on the subnet masks for all the configured interfaces.
12852  *
12853  * We end up returning a zero mask in the case of default, multicast or
12854  * experimental.
12855  */
12856 static ipaddr_t
12857 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
12858 {
12859 	ipaddr_t net_mask;
12860 	ill_t	*ill;
12861 	ipif_t	*ipif;
12862 	ill_walk_context_t ctx;
12863 	ipif_t	*fallback_ipif = NULL;
12864 
12865 	net_mask = ip_net_mask(addr);
12866 	if (net_mask == 0) {
12867 		*ipifp = NULL;
12868 		return (0);
12869 	}
12870 
12871 	/* Let's check to see if this is maybe a local subnet route. */
12872 	/* this function only applies to IPv4 interfaces */
12873 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
12874 	ill = ILL_START_WALK_V4(&ctx, ipst);
12875 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12876 		mutex_enter(&ill->ill_lock);
12877 		for (ipif = ill->ill_ipif; ipif != NULL;
12878 		    ipif = ipif->ipif_next) {
12879 			if (!IPIF_CAN_LOOKUP(ipif))
12880 				continue;
12881 			if (!(ipif->ipif_flags & IPIF_UP))
12882 				continue;
12883 			if ((ipif->ipif_subnet & net_mask) ==
12884 			    (addr & net_mask)) {
12885 				/*
12886 				 * Don't trust pt-pt interfaces if there are
12887 				 * other interfaces.
12888 				 */
12889 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12890 					if (fallback_ipif == NULL) {
12891 						ipif_refhold_locked(ipif);
12892 						fallback_ipif = ipif;
12893 					}
12894 					continue;
12895 				}
12896 
12897 				/*
12898 				 * Fine. Just assume the same net mask as the
12899 				 * directly attached subnet interface is using.
12900 				 */
12901 				ipif_refhold_locked(ipif);
12902 				mutex_exit(&ill->ill_lock);
12903 				rw_exit(&ipst->ips_ill_g_lock);
12904 				if (fallback_ipif != NULL)
12905 					ipif_refrele(fallback_ipif);
12906 				*ipifp = ipif;
12907 				return (ipif->ipif_net_mask);
12908 			}
12909 		}
12910 		mutex_exit(&ill->ill_lock);
12911 	}
12912 	rw_exit(&ipst->ips_ill_g_lock);
12913 
12914 	*ipifp = fallback_ipif;
12915 	return ((fallback_ipif != NULL) ?
12916 	    fallback_ipif->ipif_net_mask : net_mask);
12917 }
12918 
12919 /*
12920  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12921  */
12922 static void
12923 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12924 {
12925 	IOCP	iocp;
12926 	ipft_t	*ipft;
12927 	ipllc_t	*ipllc;
12928 	mblk_t	*mp1;
12929 	cred_t	*cr;
12930 	int	error = 0;
12931 	conn_t	*connp;
12932 
12933 	ip1dbg(("ip_wput_ioctl"));
12934 	iocp = (IOCP)mp->b_rptr;
12935 	mp1 = mp->b_cont;
12936 	if (mp1 == NULL) {
12937 		iocp->ioc_error = EINVAL;
12938 		mp->b_datap->db_type = M_IOCNAK;
12939 		iocp->ioc_count = 0;
12940 		qreply(q, mp);
12941 		return;
12942 	}
12943 
12944 	/*
12945 	 * These IOCTLs provide various control capabilities to
12946 	 * upstream agents such as ULPs and processes.	There
12947 	 * are currently two such IOCTLs implemented.  They
12948 	 * are used by TCP to provide update information for
12949 	 * existing IREs and to forcibly delete an IRE for a
12950 	 * host that is not responding, thereby forcing an
12951 	 * attempt at a new route.
12952 	 */
12953 	iocp->ioc_error = EINVAL;
12954 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12955 		goto done;
12956 
12957 	ipllc = (ipllc_t *)mp1->b_rptr;
12958 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12959 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12960 			break;
12961 	}
12962 	/*
12963 	 * prefer credential from mblk over ioctl;
12964 	 * see ip_sioctl_copyin_setup
12965 	 */
12966 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
12967 
12968 	/*
12969 	 * Refhold the conn in case the request gets queued up in some lookup
12970 	 */
12971 	ASSERT(CONN_Q(q));
12972 	connp = Q_TO_CONN(q);
12973 	CONN_INC_REF(connp);
12974 	if (ipft->ipft_pfi &&
12975 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12976 	    pullupmsg(mp1, ipft->ipft_min_size))) {
12977 		error = (*ipft->ipft_pfi)(q,
12978 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12979 	}
12980 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12981 		/*
12982 		 * CONN_OPER_PENDING_DONE happens in the function called
12983 		 * through ipft_pfi above.
12984 		 */
12985 		return;
12986 	}
12987 
12988 	CONN_OPER_PENDING_DONE(connp);
12989 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12990 		freemsg(mp);
12991 		return;
12992 	}
12993 	iocp->ioc_error = error;
12994 
12995 done:
12996 	mp->b_datap->db_type = M_IOCACK;
12997 	if (iocp->ioc_error)
12998 		iocp->ioc_count = 0;
12999 	qreply(q, mp);
13000 }
13001 
13002 /*
13003  * Lookup an ipif using the sequence id (ipif_seqid)
13004  */
13005 ipif_t *
13006 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13007 {
13008 	ipif_t *ipif;
13009 
13010 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13011 
13012 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13013 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13014 			return (ipif);
13015 	}
13016 	return (NULL);
13017 }
13018 
13019 /*
13020  * Assign a unique id for the ipif. This is used later when we send
13021  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13022  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13023  * IRE is added, we verify that ipif has not disappeared.
13024  */
13025 
13026 static void
13027 ipif_assign_seqid(ipif_t *ipif)
13028 {
13029 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13030 
13031 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13032 }
13033 
13034 /*
13035  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13036  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13037  * be inserted into the first space available in the list. The value of
13038  * ipif_id will then be set to the appropriate value for its position.
13039  */
13040 static int
13041 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13042 {
13043 	ill_t *ill;
13044 	ipif_t *tipif;
13045 	ipif_t **tipifp;
13046 	int id;
13047 	ip_stack_t	*ipst;
13048 
13049 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13050 	    IAM_WRITER_IPIF(ipif));
13051 
13052 	ill = ipif->ipif_ill;
13053 	ASSERT(ill != NULL);
13054 	ipst = ill->ill_ipst;
13055 
13056 	/*
13057 	 * In the case of lo0:0 we already hold the ill_g_lock.
13058 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13059 	 * ipif_insert. Another such caller is ipif_move.
13060 	 */
13061 	if (acquire_g_lock)
13062 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13063 	if (acquire_ill_lock)
13064 		mutex_enter(&ill->ill_lock);
13065 	id = ipif->ipif_id;
13066 	tipifp = &(ill->ill_ipif);
13067 	if (id == -1) {	/* need to find a real id */
13068 		id = 0;
13069 		while ((tipif = *tipifp) != NULL) {
13070 			ASSERT(tipif->ipif_id >= id);
13071 			if (tipif->ipif_id != id)
13072 				break; /* non-consecutive id */
13073 			id++;
13074 			tipifp = &(tipif->ipif_next);
13075 		}
13076 		/* limit number of logical interfaces */
13077 		if (id >= ipst->ips_ip_addrs_per_if) {
13078 			if (acquire_ill_lock)
13079 				mutex_exit(&ill->ill_lock);
13080 			if (acquire_g_lock)
13081 				rw_exit(&ipst->ips_ill_g_lock);
13082 			return (-1);
13083 		}
13084 		ipif->ipif_id = id; /* assign new id */
13085 	} else if (id < ipst->ips_ip_addrs_per_if) {
13086 		/* we have a real id; insert ipif in the right place */
13087 		while ((tipif = *tipifp) != NULL) {
13088 			ASSERT(tipif->ipif_id != id);
13089 			if (tipif->ipif_id > id)
13090 				break; /* found correct location */
13091 			tipifp = &(tipif->ipif_next);
13092 		}
13093 	} else {
13094 		if (acquire_ill_lock)
13095 			mutex_exit(&ill->ill_lock);
13096 		if (acquire_g_lock)
13097 			rw_exit(&ipst->ips_ill_g_lock);
13098 		return (-1);
13099 	}
13100 
13101 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13102 
13103 	ipif->ipif_next = tipif;
13104 	*tipifp = ipif;
13105 	if (acquire_ill_lock)
13106 		mutex_exit(&ill->ill_lock);
13107 	if (acquire_g_lock)
13108 		rw_exit(&ipst->ips_ill_g_lock);
13109 	return (0);
13110 }
13111 
13112 static void
13113 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13114 {
13115 	ipif_t	**ipifp;
13116 	ill_t	*ill = ipif->ipif_ill;
13117 
13118 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13119 	if (acquire_ill_lock)
13120 		mutex_enter(&ill->ill_lock);
13121 	else
13122 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13123 
13124 	ipifp = &ill->ill_ipif;
13125 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13126 		if (*ipifp == ipif) {
13127 			*ipifp = ipif->ipif_next;
13128 			break;
13129 		}
13130 	}
13131 
13132 	if (acquire_ill_lock)
13133 		mutex_exit(&ill->ill_lock);
13134 }
13135 
13136 /*
13137  * Allocate and initialize a new interface control structure.  (Always
13138  * called as writer.)
13139  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13140  * is not part of the global linked list of ills. ipif_seqid is unique
13141  * in the system and to preserve the uniqueness, it is assigned only
13142  * when ill becomes part of the global list. At that point ill will
13143  * have a name. If it doesn't get assigned here, it will get assigned
13144  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13145  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13146  * the interface flags or any other information from the DL_INFO_ACK for
13147  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13148  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13149  * second DL_INFO_ACK comes in from the driver.
13150  */
13151 static ipif_t *
13152 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13153 {
13154 	ipif_t	*ipif;
13155 	phyint_t *phyi;
13156 
13157 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13158 	    ill->ill_name, id, (void *)ill));
13159 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13160 
13161 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13162 		return (NULL);
13163 	*ipif = ipif_zero;	/* start clean */
13164 
13165 	ipif->ipif_ill = ill;
13166 	ipif->ipif_id = id;	/* could be -1 */
13167 	/*
13168 	 * Inherit the zoneid from the ill; for the shared stack instance
13169 	 * this is always the global zone
13170 	 */
13171 	ipif->ipif_zoneid = ill->ill_zoneid;
13172 
13173 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13174 
13175 	ipif->ipif_refcnt = 0;
13176 	ipif->ipif_saved_ire_cnt = 0;
13177 
13178 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13179 		mi_free(ipif);
13180 		return (NULL);
13181 	}
13182 	/* -1 id should have been replaced by real id */
13183 	id = ipif->ipif_id;
13184 	ASSERT(id >= 0);
13185 
13186 	if (ill->ill_name[0] != '\0')
13187 		ipif_assign_seqid(ipif);
13188 
13189 	/*
13190 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13191 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13192 	 * ioctl sets ipif_orig_ipifid to zero.
13193 	 */
13194 	ipif->ipif_orig_ipifid = id;
13195 
13196 	/*
13197 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13198 	 * The ipif is still not up and can't be looked up until the
13199 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13200 	 */
13201 	mutex_enter(&ill->ill_lock);
13202 	mutex_enter(&ill->ill_phyint->phyint_lock);
13203 	/*
13204 	 * Set the running flag when logical interface zero is created.
13205 	 * For subsequent logical interfaces, a DLPI link down
13206 	 * notification message may have cleared the running flag to
13207 	 * indicate the link is down, so we shouldn't just blindly set it.
13208 	 */
13209 	if (id == 0)
13210 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13211 	ipif->ipif_ire_type = ire_type;
13212 	phyi = ill->ill_phyint;
13213 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13214 
13215 	if (ipif->ipif_isv6) {
13216 		ill->ill_flags |= ILLF_IPV6;
13217 	} else {
13218 		ipaddr_t inaddr_any = INADDR_ANY;
13219 
13220 		ill->ill_flags |= ILLF_IPV4;
13221 
13222 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13223 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13224 		    &ipif->ipif_v6lcl_addr);
13225 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13226 		    &ipif->ipif_v6src_addr);
13227 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13228 		    &ipif->ipif_v6subnet);
13229 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13230 		    &ipif->ipif_v6net_mask);
13231 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13232 		    &ipif->ipif_v6brd_addr);
13233 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13234 		    &ipif->ipif_v6pp_dst_addr);
13235 	}
13236 
13237 	/*
13238 	 * Don't set the interface flags etc. now, will do it in
13239 	 * ip_ll_subnet_defaults.
13240 	 */
13241 	if (!initialize) {
13242 		mutex_exit(&ill->ill_lock);
13243 		mutex_exit(&ill->ill_phyint->phyint_lock);
13244 		return (ipif);
13245 	}
13246 	ipif->ipif_mtu = ill->ill_max_mtu;
13247 
13248 	if (ill->ill_bcast_addr_length != 0) {
13249 		/*
13250 		 * Later detect lack of DLPI driver multicast
13251 		 * capability by catching DL_ENABMULTI errors in
13252 		 * ip_rput_dlpi.
13253 		 */
13254 		ill->ill_flags |= ILLF_MULTICAST;
13255 		if (!ipif->ipif_isv6)
13256 			ipif->ipif_flags |= IPIF_BROADCAST;
13257 	} else {
13258 		if (ill->ill_net_type != IRE_LOOPBACK) {
13259 			if (ipif->ipif_isv6)
13260 				/*
13261 				 * Note: xresolv interfaces will eventually need
13262 				 * NOARP set here as well, but that will require
13263 				 * those external resolvers to have some
13264 				 * knowledge of that flag and act appropriately.
13265 				 * Not to be changed at present.
13266 				 */
13267 				ill->ill_flags |= ILLF_NONUD;
13268 			else
13269 				ill->ill_flags |= ILLF_NOARP;
13270 		}
13271 		if (ill->ill_phys_addr_length == 0) {
13272 			if (ill->ill_media &&
13273 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13274 				ipif->ipif_flags |= IPIF_NOXMIT;
13275 				phyi->phyint_flags |= PHYI_VIRTUAL;
13276 			} else {
13277 				/* pt-pt supports multicast. */
13278 				ill->ill_flags |= ILLF_MULTICAST;
13279 				if (ill->ill_net_type == IRE_LOOPBACK) {
13280 					phyi->phyint_flags |=
13281 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13282 				} else {
13283 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13284 				}
13285 			}
13286 		}
13287 	}
13288 	mutex_exit(&ill->ill_lock);
13289 	mutex_exit(&ill->ill_phyint->phyint_lock);
13290 	return (ipif);
13291 }
13292 
13293 /*
13294  * If appropriate, send a message up to the resolver delete the entry
13295  * for the address of this interface which is going out of business.
13296  * (Always called as writer).
13297  *
13298  * NOTE : We need to check for NULL mps as some of the fields are
13299  *	  initialized only for some interface types. See ipif_resolver_up()
13300  *	  for details.
13301  */
13302 void
13303 ipif_arp_down(ipif_t *ipif)
13304 {
13305 	mblk_t	*mp;
13306 	ill_t	*ill = ipif->ipif_ill;
13307 
13308 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13309 	ASSERT(IAM_WRITER_IPIF(ipif));
13310 
13311 	/* Delete the mapping for the local address */
13312 	mp = ipif->ipif_arp_del_mp;
13313 	if (mp != NULL) {
13314 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13315 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13316 		putnext(ill->ill_rq, mp);
13317 		ipif->ipif_arp_del_mp = NULL;
13318 	}
13319 
13320 	/*
13321 	 * If this is the last ipif that is going down and there are no
13322 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13323 	 * clean up ARP completely.
13324 	 */
13325 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13326 
13327 		/* Send up AR_INTERFACE_DOWN message */
13328 		mp = ill->ill_arp_down_mp;
13329 		if (mp != NULL) {
13330 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13331 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13332 			    ipif->ipif_id));
13333 			putnext(ill->ill_rq, mp);
13334 			ill->ill_arp_down_mp = NULL;
13335 		}
13336 
13337 		/* Tell ARP to delete the multicast mappings */
13338 		mp = ill->ill_arp_del_mapping_mp;
13339 		if (mp != NULL) {
13340 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13341 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13342 			    ipif->ipif_id));
13343 			putnext(ill->ill_rq, mp);
13344 			ill->ill_arp_del_mapping_mp = NULL;
13345 		}
13346 	}
13347 }
13348 
13349 /*
13350  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13351  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13352  * that it wants the add_mp allocated in this function to be returned
13353  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13354  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13355  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13356  * as it does a ipif_arp_down after calling this function - which will
13357  * remove what we add here.
13358  *
13359  * Returns -1 on failures and 0 on success.
13360  */
13361 int
13362 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13363 {
13364 	mblk_t	*del_mp = NULL;
13365 	mblk_t *add_mp = NULL;
13366 	mblk_t *mp;
13367 	ill_t	*ill = ipif->ipif_ill;
13368 	phyint_t *phyi = ill->ill_phyint;
13369 	ipaddr_t addr, mask, extract_mask = 0;
13370 	arma_t	*arma;
13371 	uint8_t *maddr, *bphys_addr;
13372 	uint32_t hw_start;
13373 	dl_unitdata_req_t *dlur;
13374 
13375 	ASSERT(IAM_WRITER_IPIF(ipif));
13376 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13377 		return (0);
13378 
13379 	/*
13380 	 * Delete the existing mapping from ARP. Normally ipif_down
13381 	 * -> ipif_arp_down should send this up to ARP. The only
13382 	 * reason we would find this when we are switching from
13383 	 * Multicast to Broadcast where we did not do a down.
13384 	 */
13385 	mp = ill->ill_arp_del_mapping_mp;
13386 	if (mp != NULL) {
13387 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13388 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13389 		putnext(ill->ill_rq, mp);
13390 		ill->ill_arp_del_mapping_mp = NULL;
13391 	}
13392 
13393 	if (arp_add_mapping_mp != NULL)
13394 		*arp_add_mapping_mp = NULL;
13395 
13396 	/*
13397 	 * Check that the address is not to long for the constant
13398 	 * length reserved in the template arma_t.
13399 	 */
13400 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13401 		return (-1);
13402 
13403 	/* Add mapping mblk */
13404 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13405 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13406 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13407 	    (caddr_t)&addr);
13408 	if (add_mp == NULL)
13409 		return (-1);
13410 	arma = (arma_t *)add_mp->b_rptr;
13411 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13412 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13413 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13414 
13415 	/*
13416 	 * Determine the broadcast address.
13417 	 */
13418 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13419 	if (ill->ill_sap_length < 0)
13420 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13421 	else
13422 		bphys_addr = (uchar_t *)dlur +
13423 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13424 	/*
13425 	 * Check PHYI_MULTI_BCAST and length of physical
13426 	 * address to determine if we use the mapping or the
13427 	 * broadcast address.
13428 	 */
13429 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13430 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13431 		    bphys_addr, maddr, &hw_start, &extract_mask))
13432 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13433 
13434 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13435 	    (ill->ill_flags & ILLF_MULTICAST)) {
13436 		/* Make sure this will not match the "exact" entry. */
13437 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13438 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13439 		    (caddr_t)&addr);
13440 		if (del_mp == NULL) {
13441 			freemsg(add_mp);
13442 			return (-1);
13443 		}
13444 		bcopy(&extract_mask, (char *)arma +
13445 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13446 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13447 			/* Use link-layer broadcast address for MULTI_BCAST */
13448 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13449 			ip2dbg(("ipif_arp_setup_multicast: adding"
13450 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13451 		} else {
13452 			arma->arma_hw_mapping_start = hw_start;
13453 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13454 			    " ARP setup for %s\n", ill->ill_name));
13455 		}
13456 	} else {
13457 		freemsg(add_mp);
13458 		ASSERT(del_mp == NULL);
13459 		/* It is neither MULTICAST nor MULTI_BCAST */
13460 		return (0);
13461 	}
13462 	ASSERT(add_mp != NULL && del_mp != NULL);
13463 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13464 	ill->ill_arp_del_mapping_mp = del_mp;
13465 	if (arp_add_mapping_mp != NULL) {
13466 		/* The caller just wants the mblks allocated */
13467 		*arp_add_mapping_mp = add_mp;
13468 	} else {
13469 		/* The caller wants us to send it to arp */
13470 		putnext(ill->ill_rq, add_mp);
13471 	}
13472 	return (0);
13473 }
13474 
13475 /*
13476  * Get the resolver set up for a new interface address.
13477  * (Always called as writer.)
13478  * Called both for IPv4 and IPv6 interfaces,
13479  * though it only sets up the resolver for v6
13480  * if it's an xresolv interface (one using an external resolver).
13481  * Honors ILLF_NOARP.
13482  * The enumerated value res_act is used to tune the behavior.
13483  * If set to Res_act_initial, then we set up all the resolver
13484  * structures for a new interface.  If set to Res_act_move, then
13485  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13486  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13487  * asynchronous hardware address change notification.  If set to
13488  * Res_act_defend, then we tell ARP that it needs to send a single
13489  * gratuitous message in defense of the address.
13490  * Returns error on failure.
13491  */
13492 int
13493 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13494 {
13495 	caddr_t	addr;
13496 	mblk_t	*arp_up_mp = NULL;
13497 	mblk_t	*arp_down_mp = NULL;
13498 	mblk_t	*arp_add_mp = NULL;
13499 	mblk_t	*arp_del_mp = NULL;
13500 	mblk_t	*arp_add_mapping_mp = NULL;
13501 	mblk_t	*arp_del_mapping_mp = NULL;
13502 	ill_t	*ill = ipif->ipif_ill;
13503 	uchar_t	*area_p = NULL;
13504 	uchar_t	*ared_p = NULL;
13505 	int	err = ENOMEM;
13506 	boolean_t was_dup;
13507 
13508 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13509 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13510 	ASSERT(IAM_WRITER_IPIF(ipif));
13511 
13512 	was_dup = B_FALSE;
13513 	if (res_act == Res_act_initial) {
13514 		ipif->ipif_addr_ready = 0;
13515 		/*
13516 		 * We're bringing an interface up here.  There's no way that we
13517 		 * should need to shut down ARP now.
13518 		 */
13519 		mutex_enter(&ill->ill_lock);
13520 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13521 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13522 			ill->ill_ipif_dup_count--;
13523 			was_dup = B_TRUE;
13524 		}
13525 		mutex_exit(&ill->ill_lock);
13526 	}
13527 	if (ipif->ipif_recovery_id != 0)
13528 		(void) untimeout(ipif->ipif_recovery_id);
13529 	ipif->ipif_recovery_id = 0;
13530 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13531 		ipif->ipif_addr_ready = 1;
13532 		return (0);
13533 	}
13534 	/* NDP will set the ipif_addr_ready flag when it's ready */
13535 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13536 		return (0);
13537 
13538 	if (ill->ill_isv6) {
13539 		/*
13540 		 * External resolver for IPv6
13541 		 */
13542 		ASSERT(res_act == Res_act_initial);
13543 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13544 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13545 			area_p = (uchar_t *)&ip6_area_template;
13546 			ared_p = (uchar_t *)&ip6_ared_template;
13547 		}
13548 	} else {
13549 		/*
13550 		 * IPv4 arp case. If the ARP stream has already started
13551 		 * closing, fail this request for ARP bringup. Else
13552 		 * record the fact that an ARP bringup is pending.
13553 		 */
13554 		mutex_enter(&ill->ill_lock);
13555 		if (ill->ill_arp_closing) {
13556 			mutex_exit(&ill->ill_lock);
13557 			err = EINVAL;
13558 			goto failed;
13559 		} else {
13560 			if (ill->ill_ipif_up_count == 0 &&
13561 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13562 				ill->ill_arp_bringup_pending = 1;
13563 			mutex_exit(&ill->ill_lock);
13564 		}
13565 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13566 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13567 			area_p = (uchar_t *)&ip_area_template;
13568 			ared_p = (uchar_t *)&ip_ared_template;
13569 		}
13570 	}
13571 
13572 	/*
13573 	 * Add an entry for the local address in ARP only if it
13574 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13575 	 */
13576 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13577 		area_t *area;
13578 
13579 		/* Now ask ARP to publish our address. */
13580 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13581 		if (arp_add_mp == NULL)
13582 			goto failed;
13583 		area = (area_t *)arp_add_mp->b_rptr;
13584 		if (res_act != Res_act_initial) {
13585 			/*
13586 			 * Copy the new hardware address and length into
13587 			 * arp_add_mp to be sent to ARP.
13588 			 */
13589 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13590 			bcopy(ill->ill_phys_addr,
13591 			    ((char *)area + area->area_hw_addr_offset),
13592 			    area->area_hw_addr_length);
13593 		}
13594 
13595 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13596 		    ACE_F_MYADDR;
13597 
13598 		if (res_act == Res_act_defend) {
13599 			area->area_flags |= ACE_F_DEFEND;
13600 			/*
13601 			 * If we're just defending our address now, then
13602 			 * there's no need to set up ARP multicast mappings.
13603 			 * The publish command is enough.
13604 			 */
13605 			goto done;
13606 		}
13607 
13608 		if (res_act != Res_act_initial)
13609 			goto arp_setup_multicast;
13610 
13611 		/*
13612 		 * Allocate an ARP deletion message so we know we can tell ARP
13613 		 * when the interface goes down.
13614 		 */
13615 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13616 		if (arp_del_mp == NULL)
13617 			goto failed;
13618 
13619 	} else {
13620 		if (res_act != Res_act_initial)
13621 			goto done;
13622 	}
13623 	/*
13624 	 * Need to bring up ARP or setup multicast mapping only
13625 	 * when the first interface is coming UP.
13626 	 */
13627 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13628 	    was_dup) {
13629 		goto done;
13630 	}
13631 
13632 	/*
13633 	 * Allocate an ARP down message (to be saved) and an ARP up
13634 	 * message.
13635 	 */
13636 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13637 	if (arp_down_mp == NULL)
13638 		goto failed;
13639 
13640 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13641 	if (arp_up_mp == NULL)
13642 		goto failed;
13643 
13644 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13645 		goto done;
13646 
13647 arp_setup_multicast:
13648 	/*
13649 	 * Setup the multicast mappings. This function initializes
13650 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13651 	 * IPv6.
13652 	 */
13653 	if (!ill->ill_isv6) {
13654 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13655 		if (err != 0)
13656 			goto failed;
13657 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13658 		ASSERT(arp_add_mapping_mp != NULL);
13659 	}
13660 
13661 done:
13662 	if (arp_del_mp != NULL) {
13663 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13664 		ipif->ipif_arp_del_mp = arp_del_mp;
13665 	}
13666 	if (arp_down_mp != NULL) {
13667 		ASSERT(ill->ill_arp_down_mp == NULL);
13668 		ill->ill_arp_down_mp = arp_down_mp;
13669 	}
13670 	if (arp_del_mapping_mp != NULL) {
13671 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13672 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13673 	}
13674 	if (arp_up_mp != NULL) {
13675 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13676 		    ill->ill_name, ipif->ipif_id));
13677 		putnext(ill->ill_rq, arp_up_mp);
13678 	}
13679 	if (arp_add_mp != NULL) {
13680 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13681 		    ill->ill_name, ipif->ipif_id));
13682 		/*
13683 		 * If it's an extended ARP implementation, then we'll wait to
13684 		 * hear that DAD has finished before using the interface.
13685 		 */
13686 		if (!ill->ill_arp_extend)
13687 			ipif->ipif_addr_ready = 1;
13688 		putnext(ill->ill_rq, arp_add_mp);
13689 	} else {
13690 		ipif->ipif_addr_ready = 1;
13691 	}
13692 	if (arp_add_mapping_mp != NULL) {
13693 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13694 		    ill->ill_name, ipif->ipif_id));
13695 		putnext(ill->ill_rq, arp_add_mapping_mp);
13696 	}
13697 	if (res_act != Res_act_initial)
13698 		return (0);
13699 
13700 	if (ill->ill_flags & ILLF_NOARP)
13701 		err = ill_arp_off(ill);
13702 	else
13703 		err = ill_arp_on(ill);
13704 	if (err != 0) {
13705 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13706 		freemsg(ipif->ipif_arp_del_mp);
13707 		freemsg(ill->ill_arp_down_mp);
13708 		freemsg(ill->ill_arp_del_mapping_mp);
13709 		ipif->ipif_arp_del_mp = NULL;
13710 		ill->ill_arp_down_mp = NULL;
13711 		ill->ill_arp_del_mapping_mp = NULL;
13712 		return (err);
13713 	}
13714 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13715 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13716 
13717 failed:
13718 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13719 	freemsg(arp_add_mp);
13720 	freemsg(arp_del_mp);
13721 	freemsg(arp_add_mapping_mp);
13722 	freemsg(arp_up_mp);
13723 	freemsg(arp_down_mp);
13724 	ill->ill_arp_bringup_pending = 0;
13725 	return (err);
13726 }
13727 
13728 /*
13729  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13730  * just gone back up.
13731  */
13732 static void
13733 ipif_arp_start_dad(ipif_t *ipif)
13734 {
13735 	ill_t *ill = ipif->ipif_ill;
13736 	mblk_t *arp_add_mp;
13737 	area_t *area;
13738 
13739 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13740 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13741 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13742 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13743 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13744 		/*
13745 		 * If we can't contact ARP for some reason, that's not really a
13746 		 * problem.  Just send out the routing socket notification that
13747 		 * DAD completion would have done, and continue.
13748 		 */
13749 		ipif_mask_reply(ipif);
13750 		ipif_up_notify(ipif);
13751 		ipif->ipif_addr_ready = 1;
13752 		return;
13753 	}
13754 
13755 	/* Setting the 'unverified' flag restarts DAD */
13756 	area = (area_t *)arp_add_mp->b_rptr;
13757 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13758 	    ACE_F_UNVERIFIED;
13759 	putnext(ill->ill_rq, arp_add_mp);
13760 }
13761 
13762 static void
13763 ipif_ndp_start_dad(ipif_t *ipif)
13764 {
13765 	nce_t *nce;
13766 
13767 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13768 	if (nce == NULL)
13769 		return;
13770 
13771 	if (!ndp_restart_dad(nce)) {
13772 		/*
13773 		 * If we can't restart DAD for some reason, that's not really a
13774 		 * problem.  Just send out the routing socket notification that
13775 		 * DAD completion would have done, and continue.
13776 		 */
13777 		ipif_up_notify(ipif);
13778 		ipif->ipif_addr_ready = 1;
13779 	}
13780 	NCE_REFRELE(nce);
13781 }
13782 
13783 /*
13784  * Restart duplicate address detection on all interfaces on the given ill.
13785  *
13786  * This is called when an interface transitions from down to up
13787  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13788  *
13789  * Note that since the underlying physical link has transitioned, we must cause
13790  * at least one routing socket message to be sent here, either via DAD
13791  * completion or just by default on the first ipif.  (If we don't do this, then
13792  * in.mpathd will see long delays when doing link-based failure recovery.)
13793  */
13794 void
13795 ill_restart_dad(ill_t *ill, boolean_t went_up)
13796 {
13797 	ipif_t *ipif;
13798 
13799 	if (ill == NULL)
13800 		return;
13801 
13802 	/*
13803 	 * If layer two doesn't support duplicate address detection, then just
13804 	 * send the routing socket message now and be done with it.
13805 	 */
13806 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13807 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13808 		ip_rts_ifmsg(ill->ill_ipif);
13809 		return;
13810 	}
13811 
13812 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13813 		if (went_up) {
13814 			if (ipif->ipif_flags & IPIF_UP) {
13815 				if (ill->ill_isv6)
13816 					ipif_ndp_start_dad(ipif);
13817 				else
13818 					ipif_arp_start_dad(ipif);
13819 			} else if (ill->ill_isv6 &&
13820 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
13821 				/*
13822 				 * For IPv4, the ARP module itself will
13823 				 * automatically start the DAD process when it
13824 				 * sees DL_NOTE_LINK_UP.  We respond to the
13825 				 * AR_CN_READY at the completion of that task.
13826 				 * For IPv6, we must kick off the bring-up
13827 				 * process now.
13828 				 */
13829 				ndp_do_recovery(ipif);
13830 			} else {
13831 				/*
13832 				 * Unfortunately, the first ipif is "special"
13833 				 * and represents the underlying ill in the
13834 				 * routing socket messages.  Thus, when this
13835 				 * one ipif is down, we must still notify so
13836 				 * that the user knows the IFF_RUNNING status
13837 				 * change.  (If the first ipif is up, then
13838 				 * we'll handle eventual routing socket
13839 				 * notification via DAD completion.)
13840 				 */
13841 				if (ipif == ill->ill_ipif)
13842 					ip_rts_ifmsg(ill->ill_ipif);
13843 			}
13844 		} else {
13845 			/*
13846 			 * After link down, we'll need to send a new routing
13847 			 * message when the link comes back, so clear
13848 			 * ipif_addr_ready.
13849 			 */
13850 			ipif->ipif_addr_ready = 0;
13851 		}
13852 	}
13853 
13854 	/*
13855 	 * If we've torn down links, then notify the user right away.
13856 	 */
13857 	if (!went_up)
13858 		ip_rts_ifmsg(ill->ill_ipif);
13859 }
13860 
13861 /*
13862  * Wakeup all threads waiting to enter the ipsq, and sleeping
13863  * on any of the ills in this ipsq. The ill_lock of the ill
13864  * must be held so that waiters don't miss wakeups
13865  */
13866 static void
13867 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
13868 {
13869 	phyint_t *phyint;
13870 
13871 	phyint = ipsq->ipsq_phyint_list;
13872 	while (phyint != NULL) {
13873 		if (phyint->phyint_illv4) {
13874 			if (!caller_holds_lock)
13875 				mutex_enter(&phyint->phyint_illv4->ill_lock);
13876 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13877 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
13878 			if (!caller_holds_lock)
13879 				mutex_exit(&phyint->phyint_illv4->ill_lock);
13880 		}
13881 		if (phyint->phyint_illv6) {
13882 			if (!caller_holds_lock)
13883 				mutex_enter(&phyint->phyint_illv6->ill_lock);
13884 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13885 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
13886 			if (!caller_holds_lock)
13887 				mutex_exit(&phyint->phyint_illv6->ill_lock);
13888 		}
13889 		phyint = phyint->phyint_ipsq_next;
13890 	}
13891 }
13892 
13893 static ipsq_t *
13894 ipsq_create(char *groupname, ip_stack_t *ipst)
13895 {
13896 	ipsq_t	*ipsq;
13897 
13898 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
13899 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
13900 	if (ipsq == NULL) {
13901 		return (NULL);
13902 	}
13903 
13904 	if (groupname != NULL)
13905 		(void) strcpy(ipsq->ipsq_name, groupname);
13906 	else
13907 		ipsq->ipsq_name[0] = '\0';
13908 
13909 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
13910 	ipsq->ipsq_flags |= IPSQ_GROUP;
13911 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
13912 	ipst->ips_ipsq_g_head = ipsq;
13913 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
13914 	return (ipsq);
13915 }
13916 
13917 /*
13918  * Return an ipsq correspoding to the groupname. If 'create' is true
13919  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
13920  * uniquely with an IPMP group. However during IPMP groupname operations,
13921  * multiple IPMP groups may be associated with a single ipsq. But no
13922  * IPMP group can be associated with more than 1 ipsq at any time.
13923  * For example
13924  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
13925  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
13926  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
13927  *
13928  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
13929  * status shown below during the execution of the above command.
13930  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
13931  *
13932  * After the completion of the above groupname command we return to the stable
13933  * state shown below.
13934  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
13935  *	hme4			mpk17-85	ipsq2	mpk17-85	1
13936  *
13937  * Because of the above, we don't search based on the ipsq_name since that
13938  * would miss the correct ipsq during certain windows as shown above.
13939  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
13940  * natural state.
13941  */
13942 static ipsq_t *
13943 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
13944     ip_stack_t *ipst)
13945 {
13946 	ipsq_t	*ipsq;
13947 	int	group_len;
13948 	phyint_t *phyint;
13949 
13950 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
13951 
13952 	group_len = strlen(groupname);
13953 	ASSERT(group_len != 0);
13954 	group_len++;
13955 
13956 	for (ipsq = ipst->ips_ipsq_g_head;
13957 	    ipsq != NULL;
13958 	    ipsq = ipsq->ipsq_next) {
13959 		/*
13960 		 * When an ipsq is being split, and ill_split_ipsq
13961 		 * calls this function, we exclude it from being considered.
13962 		 */
13963 		if (ipsq == exclude_ipsq)
13964 			continue;
13965 
13966 		/*
13967 		 * Compare against the ipsq_name. The groupname change happens
13968 		 * in 2 phases. The 1st phase merges the from group into
13969 		 * the to group's ipsq, by calling ill_merge_groups and restarts
13970 		 * the ioctl. The 2nd phase then locates the ipsq again thru
13971 		 * ipsq_name. At this point the phyint_groupname has not been
13972 		 * updated.
13973 		 */
13974 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
13975 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
13976 			/*
13977 			 * Verify that an ipmp groupname is exactly
13978 			 * part of 1 ipsq and is not found in any other
13979 			 * ipsq.
13980 			 */
13981 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
13982 			    NULL);
13983 			return (ipsq);
13984 		}
13985 
13986 		/*
13987 		 * Comparison against ipsq_name alone is not sufficient.
13988 		 * In the case when groups are currently being
13989 		 * merged, the ipsq could hold other IPMP groups temporarily.
13990 		 * so we walk the phyint list and compare against the
13991 		 * phyint_groupname as well.
13992 		 */
13993 		phyint = ipsq->ipsq_phyint_list;
13994 		while (phyint != NULL) {
13995 			if ((group_len == phyint->phyint_groupname_len) &&
13996 			    (bcmp(phyint->phyint_groupname, groupname,
13997 			    group_len) == 0)) {
13998 				/*
13999 				 * Verify that an ipmp groupname is exactly
14000 				 * part of 1 ipsq and is not found in any other
14001 				 * ipsq.
14002 				 */
14003 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14004 				    ipst) == NULL);
14005 				return (ipsq);
14006 			}
14007 			phyint = phyint->phyint_ipsq_next;
14008 		}
14009 	}
14010 	if (create)
14011 		ipsq = ipsq_create(groupname, ipst);
14012 	return (ipsq);
14013 }
14014 
14015 static void
14016 ipsq_delete(ipsq_t *ipsq)
14017 {
14018 	ipsq_t *nipsq;
14019 	ipsq_t *pipsq = NULL;
14020 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14021 
14022 	/*
14023 	 * We don't hold the ipsq lock, but we are sure no new
14024 	 * messages can land up, since the ipsq_refs is zero.
14025 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14026 	 * is associated with this ipsq. (Lookups are based on ill_name
14027 	 * or phyint_groupname)
14028 	 */
14029 	ASSERT(ipsq->ipsq_refs == 0);
14030 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14031 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14032 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14033 		/*
14034 		 * This is not the ipsq of an IPMP group.
14035 		 */
14036 		ipsq->ipsq_ipst = NULL;
14037 		kmem_free(ipsq, sizeof (ipsq_t));
14038 		return;
14039 	}
14040 
14041 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14042 
14043 	/*
14044 	 * Locate the ipsq  before we can remove it from
14045 	 * the singly linked list of ipsq's.
14046 	 */
14047 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14048 	    nipsq = nipsq->ipsq_next) {
14049 		if (nipsq == ipsq) {
14050 			break;
14051 		}
14052 		pipsq = nipsq;
14053 	}
14054 
14055 	ASSERT(nipsq == ipsq);
14056 
14057 	/* unlink ipsq from the list */
14058 	if (pipsq != NULL)
14059 		pipsq->ipsq_next = ipsq->ipsq_next;
14060 	else
14061 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14062 	ipsq->ipsq_ipst = NULL;
14063 	kmem_free(ipsq, sizeof (ipsq_t));
14064 	rw_exit(&ipst->ips_ill_g_lock);
14065 }
14066 
14067 static void
14068 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14069     queue_t *q)
14070 {
14071 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14072 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14073 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14074 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14075 	ASSERT(current_mp != NULL);
14076 
14077 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14078 	    NEW_OP, NULL);
14079 
14080 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14081 	    new_ipsq->ipsq_xopq_mphead != NULL);
14082 
14083 	/*
14084 	 * move from old ipsq to the new ipsq.
14085 	 */
14086 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14087 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14088 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14089 
14090 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14091 }
14092 
14093 void
14094 ill_group_cleanup(ill_t *ill)
14095 {
14096 	ill_t *ill_v4;
14097 	ill_t *ill_v6;
14098 	ipif_t *ipif;
14099 
14100 	ill_v4 = ill->ill_phyint->phyint_illv4;
14101 	ill_v6 = ill->ill_phyint->phyint_illv6;
14102 
14103 	if (ill_v4 != NULL) {
14104 		mutex_enter(&ill_v4->ill_lock);
14105 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14106 		    ipif = ipif->ipif_next) {
14107 			IPIF_UNMARK_MOVING(ipif);
14108 		}
14109 		ill_v4->ill_up_ipifs = B_FALSE;
14110 		mutex_exit(&ill_v4->ill_lock);
14111 	}
14112 
14113 	if (ill_v6 != NULL) {
14114 		mutex_enter(&ill_v6->ill_lock);
14115 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14116 		    ipif = ipif->ipif_next) {
14117 			IPIF_UNMARK_MOVING(ipif);
14118 		}
14119 		ill_v6->ill_up_ipifs = B_FALSE;
14120 		mutex_exit(&ill_v6->ill_lock);
14121 	}
14122 }
14123 /*
14124  * This function is called when an ill has had a change in its group status
14125  * to bring up all the ipifs that were up before the change.
14126  */
14127 int
14128 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14129 {
14130 	ipif_t *ipif;
14131 	ill_t *ill_v4;
14132 	ill_t *ill_v6;
14133 	ill_t *from_ill;
14134 	int err = 0;
14135 
14136 	ASSERT(IAM_WRITER_ILL(ill));
14137 
14138 	/*
14139 	 * Except for ipif_state_flags and ill_state_flags the other
14140 	 * fields of the ipif/ill that are modified below are protected
14141 	 * implicitly since we are a writer. We would have tried to down
14142 	 * even an ipif that was already down, in ill_down_ipifs. So we
14143 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14144 	 */
14145 	ill_v4 = ill->ill_phyint->phyint_illv4;
14146 	ill_v6 = ill->ill_phyint->phyint_illv6;
14147 	if (ill_v4 != NULL) {
14148 		ill_v4->ill_up_ipifs = B_TRUE;
14149 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14150 		    ipif = ipif->ipif_next) {
14151 			mutex_enter(&ill_v4->ill_lock);
14152 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14153 			IPIF_UNMARK_MOVING(ipif);
14154 			mutex_exit(&ill_v4->ill_lock);
14155 			if (ipif->ipif_was_up) {
14156 				if (!(ipif->ipif_flags & IPIF_UP))
14157 					err = ipif_up(ipif, q, mp);
14158 				ipif->ipif_was_up = B_FALSE;
14159 				if (err != 0) {
14160 					/*
14161 					 * Can there be any other error ?
14162 					 */
14163 					ASSERT(err == EINPROGRESS);
14164 					return (err);
14165 				}
14166 			}
14167 		}
14168 		mutex_enter(&ill_v4->ill_lock);
14169 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14170 		mutex_exit(&ill_v4->ill_lock);
14171 		ill_v4->ill_up_ipifs = B_FALSE;
14172 		if (ill_v4->ill_move_in_progress) {
14173 			ASSERT(ill_v4->ill_move_peer != NULL);
14174 			ill_v4->ill_move_in_progress = B_FALSE;
14175 			from_ill = ill_v4->ill_move_peer;
14176 			from_ill->ill_move_in_progress = B_FALSE;
14177 			from_ill->ill_move_peer = NULL;
14178 			mutex_enter(&from_ill->ill_lock);
14179 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14180 			mutex_exit(&from_ill->ill_lock);
14181 			if (ill_v6 == NULL) {
14182 				if (from_ill->ill_phyint->phyint_flags &
14183 				    PHYI_STANDBY) {
14184 					phyint_inactive(from_ill->ill_phyint);
14185 				}
14186 				if (ill_v4->ill_phyint->phyint_flags &
14187 				    PHYI_STANDBY) {
14188 					phyint_inactive(ill_v4->ill_phyint);
14189 				}
14190 			}
14191 			ill_v4->ill_move_peer = NULL;
14192 		}
14193 	}
14194 
14195 	if (ill_v6 != NULL) {
14196 		ill_v6->ill_up_ipifs = B_TRUE;
14197 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14198 		    ipif = ipif->ipif_next) {
14199 			mutex_enter(&ill_v6->ill_lock);
14200 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14201 			IPIF_UNMARK_MOVING(ipif);
14202 			mutex_exit(&ill_v6->ill_lock);
14203 			if (ipif->ipif_was_up) {
14204 				if (!(ipif->ipif_flags & IPIF_UP))
14205 					err = ipif_up(ipif, q, mp);
14206 				ipif->ipif_was_up = B_FALSE;
14207 				if (err != 0) {
14208 					/*
14209 					 * Can there be any other error ?
14210 					 */
14211 					ASSERT(err == EINPROGRESS);
14212 					return (err);
14213 				}
14214 			}
14215 		}
14216 		mutex_enter(&ill_v6->ill_lock);
14217 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14218 		mutex_exit(&ill_v6->ill_lock);
14219 		ill_v6->ill_up_ipifs = B_FALSE;
14220 		if (ill_v6->ill_move_in_progress) {
14221 			ASSERT(ill_v6->ill_move_peer != NULL);
14222 			ill_v6->ill_move_in_progress = B_FALSE;
14223 			from_ill = ill_v6->ill_move_peer;
14224 			from_ill->ill_move_in_progress = B_FALSE;
14225 			from_ill->ill_move_peer = NULL;
14226 			mutex_enter(&from_ill->ill_lock);
14227 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14228 			mutex_exit(&from_ill->ill_lock);
14229 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14230 				phyint_inactive(from_ill->ill_phyint);
14231 			}
14232 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14233 				phyint_inactive(ill_v6->ill_phyint);
14234 			}
14235 			ill_v6->ill_move_peer = NULL;
14236 		}
14237 	}
14238 	return (0);
14239 }
14240 
14241 /*
14242  * bring down all the approriate ipifs.
14243  */
14244 /* ARGSUSED */
14245 static void
14246 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14247 {
14248 	ipif_t *ipif;
14249 
14250 	ASSERT(IAM_WRITER_ILL(ill));
14251 
14252 	/*
14253 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14254 	 * are modified below are protected implicitly since we are a writer
14255 	 */
14256 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14257 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14258 			continue;
14259 		/*
14260 		 * Don't bring down the LINK LOCAL addresses as they are tied
14261 		 * to physical interface and they don't move. Treat them as
14262 		 * IPIF_NOFAILOVER.
14263 		 */
14264 		if (chk_nofailover && ill->ill_isv6 &&
14265 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))
14266 			continue;
14267 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14268 			/*
14269 			 * We go through the ipif_down logic even if the ipif
14270 			 * is already down, since routes can be added based
14271 			 * on down ipifs. Going through ipif_down once again
14272 			 * will delete any IREs created based on these routes.
14273 			 */
14274 			if (ipif->ipif_flags & IPIF_UP)
14275 				ipif->ipif_was_up = B_TRUE;
14276 			/*
14277 			 * If called with chk_nofailover true ipif is moving.
14278 			 */
14279 			mutex_enter(&ill->ill_lock);
14280 			if (chk_nofailover) {
14281 				ipif->ipif_state_flags |=
14282 				    IPIF_MOVING | IPIF_CHANGING;
14283 			} else {
14284 				ipif->ipif_state_flags |= IPIF_CHANGING;
14285 			}
14286 			mutex_exit(&ill->ill_lock);
14287 			/*
14288 			 * Need to re-create net/subnet bcast ires if
14289 			 * they are dependent on ipif.
14290 			 */
14291 			if (!ipif->ipif_isv6)
14292 				ipif_check_bcast_ires(ipif);
14293 			(void) ipif_logical_down(ipif, NULL, NULL);
14294 			ipif_non_duplicate(ipif);
14295 			ipif_down_tail(ipif);
14296 		}
14297 	}
14298 }
14299 
14300 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14301 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14302 	(ipsq)->ipsq_refs++;				\
14303 }
14304 
14305 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14306 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14307 	(ipsq)->ipsq_refs--;				\
14308 	if ((ipsq)->ipsq_refs == 0)				\
14309 		(ipsq)->ipsq_name[0] = '\0'; 		\
14310 }
14311 
14312 /*
14313  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14314  * new_ipsq.
14315  */
14316 static void
14317 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14318 {
14319 	phyint_t *phyint;
14320 	phyint_t *next_phyint;
14321 
14322 	/*
14323 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14324 	 * writer and the ill_lock of the ill in question. Also the dest
14325 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14326 	 */
14327 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14328 
14329 	phyint = cur_ipsq->ipsq_phyint_list;
14330 	cur_ipsq->ipsq_phyint_list = NULL;
14331 	while (phyint != NULL) {
14332 		next_phyint = phyint->phyint_ipsq_next;
14333 		IPSQ_DEC_REF(cur_ipsq, ipst);
14334 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14335 		new_ipsq->ipsq_phyint_list = phyint;
14336 		IPSQ_INC_REF(new_ipsq, ipst);
14337 		phyint->phyint_ipsq = new_ipsq;
14338 		phyint = next_phyint;
14339 	}
14340 }
14341 
14342 #define	SPLIT_SUCCESS		0
14343 #define	SPLIT_NOT_NEEDED	1
14344 #define	SPLIT_FAILED		2
14345 
14346 int
14347 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14348     ip_stack_t *ipst)
14349 {
14350 	ipsq_t *newipsq = NULL;
14351 
14352 	/*
14353 	 * Assertions denote pre-requisites for changing the ipsq of
14354 	 * a phyint
14355 	 */
14356 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14357 	/*
14358 	 * <ill-phyint> assocs can't change while ill_g_lock
14359 	 * is held as writer. See ill_phyint_reinit()
14360 	 */
14361 	ASSERT(phyint->phyint_illv4 == NULL ||
14362 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14363 	ASSERT(phyint->phyint_illv6 == NULL ||
14364 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14365 
14366 	if ((phyint->phyint_groupname_len !=
14367 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14368 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14369 	    phyint->phyint_groupname_len) != 0)) {
14370 		/*
14371 		 * Once we fail in creating a new ipsq due to memory shortage,
14372 		 * don't attempt to create new ipsq again, based on another
14373 		 * phyint, since we want all phyints belonging to an IPMP group
14374 		 * to be in the same ipsq even in the event of mem alloc fails.
14375 		 */
14376 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14377 		    cur_ipsq, ipst);
14378 		if (newipsq == NULL) {
14379 			/* Memory allocation failure */
14380 			return (SPLIT_FAILED);
14381 		} else {
14382 			/* ipsq_refs protected by ill_g_lock (writer) */
14383 			IPSQ_DEC_REF(cur_ipsq, ipst);
14384 			phyint->phyint_ipsq = newipsq;
14385 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14386 			newipsq->ipsq_phyint_list = phyint;
14387 			IPSQ_INC_REF(newipsq, ipst);
14388 			return (SPLIT_SUCCESS);
14389 		}
14390 	}
14391 	return (SPLIT_NOT_NEEDED);
14392 }
14393 
14394 /*
14395  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14396  * to do this split
14397  */
14398 static int
14399 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14400 {
14401 	ipsq_t *newipsq;
14402 
14403 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14404 	/*
14405 	 * <ill-phyint> assocs can't change while ill_g_lock
14406 	 * is held as writer. See ill_phyint_reinit()
14407 	 */
14408 
14409 	ASSERT(phyint->phyint_illv4 == NULL ||
14410 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14411 	ASSERT(phyint->phyint_illv6 == NULL ||
14412 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14413 
14414 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14415 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14416 		/*
14417 		 * ipsq_init failed due to no memory
14418 		 * caller will use the same ipsq
14419 		 */
14420 		return (SPLIT_FAILED);
14421 	}
14422 
14423 	/* ipsq_ref is protected by ill_g_lock (writer) */
14424 	IPSQ_DEC_REF(cur_ipsq, ipst);
14425 
14426 	/*
14427 	 * This is a new ipsq that is unknown to the world.
14428 	 * So we don't need to hold ipsq_lock,
14429 	 */
14430 	newipsq = phyint->phyint_ipsq;
14431 	newipsq->ipsq_writer = NULL;
14432 	newipsq->ipsq_reentry_cnt--;
14433 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14434 #ifdef DEBUG
14435 	newipsq->ipsq_depth = 0;
14436 #endif
14437 
14438 	return (SPLIT_SUCCESS);
14439 }
14440 
14441 /*
14442  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14443  * ipsq's representing their individual groups or themselves. Return
14444  * whether split needs to be retried again later.
14445  */
14446 static boolean_t
14447 ill_split_ipsq(ipsq_t *cur_ipsq)
14448 {
14449 	phyint_t *phyint;
14450 	phyint_t *next_phyint;
14451 	int	error;
14452 	boolean_t need_retry = B_FALSE;
14453 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14454 
14455 	phyint = cur_ipsq->ipsq_phyint_list;
14456 	cur_ipsq->ipsq_phyint_list = NULL;
14457 	while (phyint != NULL) {
14458 		next_phyint = phyint->phyint_ipsq_next;
14459 		/*
14460 		 * 'created' will tell us whether the callee actually
14461 		 * created an ipsq. Lack of memory may force the callee
14462 		 * to return without creating an ipsq.
14463 		 */
14464 		if (phyint->phyint_groupname == NULL) {
14465 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14466 		} else {
14467 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14468 			    need_retry, ipst);
14469 		}
14470 
14471 		switch (error) {
14472 		case SPLIT_FAILED:
14473 			need_retry = B_TRUE;
14474 			/* FALLTHRU */
14475 		case SPLIT_NOT_NEEDED:
14476 			/*
14477 			 * Keep it on the list.
14478 			 */
14479 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14480 			cur_ipsq->ipsq_phyint_list = phyint;
14481 			break;
14482 		case SPLIT_SUCCESS:
14483 			break;
14484 		default:
14485 			ASSERT(0);
14486 		}
14487 
14488 		phyint = next_phyint;
14489 	}
14490 	return (need_retry);
14491 }
14492 
14493 /*
14494  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14495  * and return the ills in the list. This list will be
14496  * needed to unlock all the ills later on by the caller.
14497  * The <ill-ipsq> associations could change between the
14498  * lock and unlock. Hence the unlock can't traverse the
14499  * ipsq to get the list of ills.
14500  */
14501 static int
14502 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14503 {
14504 	int	cnt = 0;
14505 	phyint_t	*phyint;
14506 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14507 
14508 	/*
14509 	 * The caller holds ill_g_lock to ensure that the ill memberships
14510 	 * of the ipsq don't change
14511 	 */
14512 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14513 
14514 	phyint = ipsq->ipsq_phyint_list;
14515 	while (phyint != NULL) {
14516 		if (phyint->phyint_illv4 != NULL) {
14517 			ASSERT(cnt < list_max);
14518 			list[cnt++] = phyint->phyint_illv4;
14519 		}
14520 		if (phyint->phyint_illv6 != NULL) {
14521 			ASSERT(cnt < list_max);
14522 			list[cnt++] = phyint->phyint_illv6;
14523 		}
14524 		phyint = phyint->phyint_ipsq_next;
14525 	}
14526 	ill_lock_ills(list, cnt);
14527 	return (cnt);
14528 }
14529 
14530 void
14531 ill_lock_ills(ill_t **list, int cnt)
14532 {
14533 	int	i;
14534 
14535 	if (cnt > 1) {
14536 		boolean_t try_again;
14537 		do {
14538 			try_again = B_FALSE;
14539 			for (i = 0; i < cnt - 1; i++) {
14540 				if (list[i] < list[i + 1]) {
14541 					ill_t	*tmp;
14542 
14543 					/* swap the elements */
14544 					tmp = list[i];
14545 					list[i] = list[i + 1];
14546 					list[i + 1] = tmp;
14547 					try_again = B_TRUE;
14548 				}
14549 			}
14550 		} while (try_again);
14551 	}
14552 
14553 	for (i = 0; i < cnt; i++) {
14554 		if (i == 0) {
14555 			if (list[i] != NULL)
14556 				mutex_enter(&list[i]->ill_lock);
14557 			else
14558 				return;
14559 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14560 			mutex_enter(&list[i]->ill_lock);
14561 		}
14562 	}
14563 }
14564 
14565 void
14566 ill_unlock_ills(ill_t **list, int cnt)
14567 {
14568 	int	i;
14569 
14570 	for (i = 0; i < cnt; i++) {
14571 		if ((i == 0) && (list[i] != NULL)) {
14572 			mutex_exit(&list[i]->ill_lock);
14573 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14574 			mutex_exit(&list[i]->ill_lock);
14575 		}
14576 	}
14577 }
14578 
14579 /*
14580  * Merge all the ills from 1 ipsq group into another ipsq group.
14581  * The source ipsq group is specified by the ipsq associated with
14582  * 'from_ill'. The destination ipsq group is specified by the ipsq
14583  * associated with 'to_ill' or 'groupname' respectively.
14584  * Note that ipsq itself does not have a reference count mechanism
14585  * and functions don't look up an ipsq and pass it around. Instead
14586  * functions pass around an ill or groupname, and the ipsq is looked
14587  * up from the ill or groupname and the required operation performed
14588  * atomically with the lookup on the ipsq.
14589  */
14590 static int
14591 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14592     queue_t *q)
14593 {
14594 	ipsq_t *old_ipsq;
14595 	ipsq_t *new_ipsq;
14596 	ill_t	**ill_list;
14597 	int	cnt;
14598 	size_t	ill_list_size;
14599 	boolean_t became_writer_on_new_sq = B_FALSE;
14600 	ip_stack_t	*ipst = from_ill->ill_ipst;
14601 
14602 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14603 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14604 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14605 
14606 	/*
14607 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14608 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14609 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14610 	 */
14611 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14612 
14613 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14614 	if (groupname != NULL)
14615 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14616 	else {
14617 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14618 	}
14619 
14620 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14621 
14622 	/*
14623 	 * both groups are on the same ipsq.
14624 	 */
14625 	if (old_ipsq == new_ipsq) {
14626 		rw_exit(&ipst->ips_ill_g_lock);
14627 		return (0);
14628 	}
14629 
14630 	cnt = old_ipsq->ipsq_refs << 1;
14631 	ill_list_size = cnt * sizeof (ill_t *);
14632 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14633 	if (ill_list == NULL) {
14634 		rw_exit(&ipst->ips_ill_g_lock);
14635 		return (ENOMEM);
14636 	}
14637 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14638 
14639 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14640 	mutex_enter(&new_ipsq->ipsq_lock);
14641 	if ((new_ipsq->ipsq_writer == NULL &&
14642 	    new_ipsq->ipsq_current_ipif == NULL) ||
14643 	    (new_ipsq->ipsq_writer == curthread)) {
14644 		new_ipsq->ipsq_writer = curthread;
14645 		new_ipsq->ipsq_reentry_cnt++;
14646 		became_writer_on_new_sq = B_TRUE;
14647 	}
14648 
14649 	/*
14650 	 * We are holding ill_g_lock as writer and all the ill locks of
14651 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14652 	 * message can land up on the old ipsq even though we don't hold the
14653 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14654 	 */
14655 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14656 
14657 	/*
14658 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14659 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14660 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14661 	 */
14662 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14663 
14664 	/*
14665 	 * Mark the new ipsq as needing a split since it is currently
14666 	 * being shared by more than 1 IPMP group. The split will
14667 	 * occur at the end of ipsq_exit
14668 	 */
14669 	new_ipsq->ipsq_split = B_TRUE;
14670 
14671 	/* Now release all the locks */
14672 	mutex_exit(&new_ipsq->ipsq_lock);
14673 	ill_unlock_ills(ill_list, cnt);
14674 	rw_exit(&ipst->ips_ill_g_lock);
14675 
14676 	kmem_free(ill_list, ill_list_size);
14677 
14678 	/*
14679 	 * If we succeeded in becoming writer on the new ipsq, then
14680 	 * drain the new ipsq and start processing  all enqueued messages
14681 	 * including the current ioctl we are processing which is either
14682 	 * a set groupname or failover/failback.
14683 	 */
14684 	if (became_writer_on_new_sq)
14685 		ipsq_exit(new_ipsq);
14686 
14687 	/*
14688 	 * syncq has been changed and all the messages have been moved.
14689 	 */
14690 	mutex_enter(&old_ipsq->ipsq_lock);
14691 	old_ipsq->ipsq_current_ipif = NULL;
14692 	old_ipsq->ipsq_current_ioctl = 0;
14693 	old_ipsq->ipsq_current_done = B_TRUE;
14694 	mutex_exit(&old_ipsq->ipsq_lock);
14695 	return (EINPROGRESS);
14696 }
14697 
14698 /*
14699  * Delete and add the loopback copy and non-loopback copy of
14700  * the BROADCAST ire corresponding to ill and addr. Used to
14701  * group broadcast ires together when ill becomes part of
14702  * a group.
14703  *
14704  * This function is also called when ill is leaving the group
14705  * so that the ires belonging to the group gets re-grouped.
14706  */
14707 static void
14708 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14709 {
14710 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14711 	ire_t **ire_ptpn = &ire_head;
14712 	ip_stack_t	*ipst = ill->ill_ipst;
14713 
14714 	/*
14715 	 * The loopback and non-loopback IREs are inserted in the order in which
14716 	 * they're found, on the basis that they are correctly ordered (loopback
14717 	 * first).
14718 	 */
14719 	for (;;) {
14720 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14721 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14722 		if (ire == NULL)
14723 			break;
14724 
14725 		/*
14726 		 * we are passing in KM_SLEEP because it is not easy to
14727 		 * go back to a sane state in case of memory failure.
14728 		 */
14729 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14730 		ASSERT(nire != NULL);
14731 		bzero(nire, sizeof (ire_t));
14732 		/*
14733 		 * Don't use ire_max_frag directly since we don't
14734 		 * hold on to 'ire' until we add the new ire 'nire' and
14735 		 * we don't want the new ire to have a dangling reference
14736 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14737 		 * be in sync with the ipif_mtu of the associate ipif.
14738 		 * For eg. this happens as a result of SIOCSLIFNAME,
14739 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14740 		 * the driver. A change in ire_max_frag triggered as
14741 		 * as a result of path mtu discovery, or due to an
14742 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14743 		 * route change -mtu command does not apply to broadcast ires.
14744 		 *
14745 		 * XXX We need a recovery strategy here if ire_init fails
14746 		 */
14747 		if (ire_init(nire,
14748 		    (uchar_t *)&ire->ire_addr,
14749 		    (uchar_t *)&ire->ire_mask,
14750 		    (uchar_t *)&ire->ire_src_addr,
14751 		    (uchar_t *)&ire->ire_gateway_addr,
14752 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14753 		    &ire->ire_ipif->ipif_mtu,
14754 		    ire->ire_nce,
14755 		    ire->ire_rfq,
14756 		    ire->ire_stq,
14757 		    ire->ire_type,
14758 		    ire->ire_ipif,
14759 		    ire->ire_cmask,
14760 		    ire->ire_phandle,
14761 		    ire->ire_ihandle,
14762 		    ire->ire_flags,
14763 		    &ire->ire_uinfo,
14764 		    NULL,
14765 		    NULL,
14766 		    ipst) == NULL) {
14767 			cmn_err(CE_PANIC, "ire_init() failed");
14768 		}
14769 		ire_delete(ire);
14770 		ire_refrele(ire);
14771 
14772 		/*
14773 		 * The newly created IREs are inserted at the tail of the list
14774 		 * starting with ire_head. As we've just allocated them no one
14775 		 * knows about them so it's safe.
14776 		 */
14777 		*ire_ptpn = nire;
14778 		ire_ptpn = &nire->ire_next;
14779 	}
14780 
14781 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14782 		int error;
14783 		ire_t *oire;
14784 		/* unlink the IRE from our list before calling ire_add() */
14785 		nire_next = nire->ire_next;
14786 		nire->ire_next = NULL;
14787 
14788 		/* ire_add adds the ire at the right place in the list */
14789 		oire = nire;
14790 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14791 		ASSERT(error == 0);
14792 		ASSERT(oire == nire);
14793 		ire_refrele(nire);	/* Held in ire_add */
14794 	}
14795 }
14796 
14797 /*
14798  * This function is usually called when an ill is inserted in
14799  * a group and all the ipifs are already UP. As all the ipifs
14800  * are already UP, the broadcast ires have already been created
14801  * and been inserted. But, ire_add_v4 would not have grouped properly.
14802  * We need to re-group for the benefit of ip_wput_ire which
14803  * expects BROADCAST ires to be grouped properly to avoid sending
14804  * more than one copy of the broadcast packet per group.
14805  *
14806  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14807  *	  because when ipif_up_done ends up calling this, ires have
14808  *        already been added before illgrp_insert i.e before ill_group
14809  *	  has been initialized.
14810  */
14811 static void
14812 ill_group_bcast_for_xmit(ill_t *ill)
14813 {
14814 	ill_group_t *illgrp;
14815 	ipif_t *ipif;
14816 	ipaddr_t addr;
14817 	ipaddr_t net_mask;
14818 	ipaddr_t subnet_netmask;
14819 
14820 	illgrp = ill->ill_group;
14821 
14822 	/*
14823 	 * This function is called even when an ill is deleted from
14824 	 * the group. Hence, illgrp could be null.
14825 	 */
14826 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
14827 		return;
14828 
14829 	/*
14830 	 * Delete all the BROADCAST ires matching this ill and add
14831 	 * them back. This time, ire_add_v4 should take care of
14832 	 * grouping them with others because ill is part of the
14833 	 * group.
14834 	 */
14835 	ill_bcast_delete_and_add(ill, 0);
14836 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
14837 
14838 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14839 
14840 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14841 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14842 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14843 		} else {
14844 			net_mask = htonl(IN_CLASSA_NET);
14845 		}
14846 		addr = net_mask & ipif->ipif_subnet;
14847 		ill_bcast_delete_and_add(ill, addr);
14848 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
14849 
14850 		subnet_netmask = ipif->ipif_net_mask;
14851 		addr = ipif->ipif_subnet;
14852 		ill_bcast_delete_and_add(ill, addr);
14853 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
14854 	}
14855 }
14856 
14857 /*
14858  * This function is called from illgrp_delete when ill is being deleted
14859  * from the group.
14860  *
14861  * As ill is not there in the group anymore, any address belonging
14862  * to this ill should be cleared of IRE_MARK_NORECV.
14863  */
14864 static void
14865 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
14866 {
14867 	ire_t *ire;
14868 	irb_t *irb;
14869 	ip_stack_t	*ipst = ill->ill_ipst;
14870 
14871 	ASSERT(ill->ill_group == NULL);
14872 
14873 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14874 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14875 
14876 	if (ire != NULL) {
14877 		/*
14878 		 * IPMP and plumbing operations are serialized on the ipsq, so
14879 		 * no one will insert or delete a broadcast ire under our feet.
14880 		 */
14881 		irb = ire->ire_bucket;
14882 		rw_enter(&irb->irb_lock, RW_READER);
14883 		ire_refrele(ire);
14884 
14885 		for (; ire != NULL; ire = ire->ire_next) {
14886 			if (ire->ire_addr != addr)
14887 				break;
14888 			if (ire_to_ill(ire) != ill)
14889 				continue;
14890 
14891 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
14892 			ire->ire_marks &= ~IRE_MARK_NORECV;
14893 		}
14894 		rw_exit(&irb->irb_lock);
14895 	}
14896 }
14897 
14898 ire_t *
14899 irep_insert(ill_group_t *illgrp, ipaddr_t addr, ire_t *ire, ire_t ***pirep)
14900 {
14901 	boolean_t first = B_TRUE;
14902 	ire_t *clear_ire = NULL;
14903 	ire_t *start_ire = NULL;
14904 	uint64_t match_flags;
14905 	uint64_t phyi_flags;
14906 	boolean_t fallback = B_FALSE;
14907 
14908 	/*
14909 	 * irb_lock must be held by the caller.
14910 	 * Get to the first ire matching the address and the
14911 	 * group. If the address does not match we are done
14912 	 * as we could not find the IRE. If the address matches
14913 	 * we should get to the first one matching the group.
14914 	 */
14915 	while (ire != NULL) {
14916 		if (ire->ire_addr != addr ||
14917 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14918 			break;
14919 		}
14920 		ire = ire->ire_next;
14921 	}
14922 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
14923 	start_ire = ire;
14924 redo:
14925 	while (ire != NULL && ire->ire_addr == addr &&
14926 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14927 		/*
14928 		 * The first ire for any address within a group
14929 		 * should always be the one with IRE_MARK_NORECV cleared
14930 		 * so that ip_wput_ire can avoid searching for one.
14931 		 * Note down the insertion point which will be used
14932 		 * later.
14933 		 */
14934 		if (first && (*pirep == NULL))
14935 			*pirep = ire->ire_ptpn;
14936 		/*
14937 		 * PHYI_FAILED is set when the interface fails.
14938 		 * This interface might have become good, but the
14939 		 * daemon has not yet detected. We should still
14940 		 * not receive on this. PHYI_OFFLINE should never
14941 		 * be picked as this has been offlined and soon
14942 		 * be removed.
14943 		 */
14944 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
14945 		if (phyi_flags & PHYI_OFFLINE) {
14946 			ire->ire_marks |= IRE_MARK_NORECV;
14947 			ire = ire->ire_next;
14948 			continue;
14949 		}
14950 		if (phyi_flags & match_flags) {
14951 			ire->ire_marks |= IRE_MARK_NORECV;
14952 			ire = ire->ire_next;
14953 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
14954 			    PHYI_INACTIVE) {
14955 				fallback = B_TRUE;
14956 			}
14957 			continue;
14958 		}
14959 		if (first) {
14960 			/*
14961 			 * We will move this to the front of the list later
14962 			 * on.
14963 			 */
14964 			clear_ire = ire;
14965 			ire->ire_marks &= ~IRE_MARK_NORECV;
14966 		} else {
14967 			ire->ire_marks |= IRE_MARK_NORECV;
14968 		}
14969 		first = B_FALSE;
14970 		ire = ire->ire_next;
14971 	}
14972 	/*
14973 	 * If we never nominated anybody, try nominating at least
14974 	 * an INACTIVE, if we found one. Do it only once though.
14975 	 */
14976 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
14977 	    fallback) {
14978 		match_flags = PHYI_FAILED;
14979 		ire = start_ire;
14980 		*pirep = NULL;
14981 		goto redo;
14982 	}
14983 	return (clear_ire);
14984 }
14985 
14986 /*
14987  * This function must be called only after the broadcast ires
14988  * have been grouped together. For a given address addr, nominate
14989  * only one of the ires whose interface is not FAILED or OFFLINE.
14990  *
14991  * This is also called when an ipif goes down, so that we can nominate
14992  * a different ire with the same address for receiving.
14993  */
14994 static void
14995 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
14996 {
14997 	irb_t *irb;
14998 	ire_t *ire;
14999 	ire_t *ire1;
15000 	ire_t *save_ire;
15001 	ire_t **irep = NULL;
15002 	ire_t *clear_ire = NULL;
15003 	ire_t	*new_lb_ire;
15004 	ire_t	*new_nlb_ire;
15005 	boolean_t new_lb_ire_used = B_FALSE;
15006 	boolean_t new_nlb_ire_used = B_FALSE;
15007 	boolean_t refrele_lb_ire = B_FALSE;
15008 	boolean_t refrele_nlb_ire = B_FALSE;
15009 	uint_t	max_frag;
15010 
15011 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15012 	    NULL, MATCH_IRE_TYPE, ipst);
15013 	/*
15014 	 * We may not be able to find some ires if a previous
15015 	 * ire_create failed. This happens when an ipif goes
15016 	 * down and we are unable to create BROADCAST ires due
15017 	 * to memory failure. Thus, we have to check for NULL
15018 	 * below. This should handle the case for LOOPBACK,
15019 	 * POINTOPOINT and interfaces with some POINTOPOINT
15020 	 * logicals for which there are no BROADCAST ires.
15021 	 */
15022 	if (ire == NULL)
15023 		return;
15024 	/*
15025 	 * Currently IRE_BROADCASTS are deleted when an ipif
15026 	 * goes down which runs exclusively. Thus, setting
15027 	 * IRE_MARK_RCVD should not race with ire_delete marking
15028 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15029 	 * be consistent with other parts of the code that walks
15030 	 * a given bucket.
15031 	 */
15032 	save_ire = ire;
15033 	irb = ire->ire_bucket;
15034 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15035 	if (new_lb_ire == NULL) {
15036 		ire_refrele(ire);
15037 		return;
15038 	}
15039 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15040 	if (new_nlb_ire == NULL) {
15041 		ire_refrele(ire);
15042 		kmem_cache_free(ire_cache, new_lb_ire);
15043 		return;
15044 	}
15045 	IRB_REFHOLD(irb);
15046 	rw_enter(&irb->irb_lock, RW_WRITER);
15047 	clear_ire = irep_insert(illgrp, addr, ire, &irep);
15048 
15049 	/*
15050 	 * irep non-NULL indicates that we entered the while loop
15051 	 * above. If clear_ire is at the insertion point, we don't
15052 	 * have to do anything. clear_ire will be NULL if all the
15053 	 * interfaces are failed.
15054 	 *
15055 	 * We cannot unlink and reinsert the ire at the right place
15056 	 * in the list since there can be other walkers of this bucket.
15057 	 * Instead we delete and recreate the ire
15058 	 */
15059 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15060 		ire_t *clear_ire_stq = NULL;
15061 		ire_t *clr_ire = NULL;
15062 		ire_t *ire_next = NULL;
15063 
15064 		if (clear_ire->ire_stq == NULL)
15065 			ire_next = clear_ire->ire_next;
15066 
15067 		rw_exit(&irb->irb_lock);
15068 
15069 		bzero(new_lb_ire, sizeof (ire_t));
15070 		/* XXX We need a recovery strategy here. */
15071 		if (ire_init(new_lb_ire,
15072 		    (uchar_t *)&clear_ire->ire_addr,
15073 		    (uchar_t *)&clear_ire->ire_mask,
15074 		    (uchar_t *)&clear_ire->ire_src_addr,
15075 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15076 		    &clear_ire->ire_max_frag,
15077 		    NULL, /* let ire_nce_init derive the resolver info */
15078 		    clear_ire->ire_rfq,
15079 		    clear_ire->ire_stq,
15080 		    clear_ire->ire_type,
15081 		    clear_ire->ire_ipif,
15082 		    clear_ire->ire_cmask,
15083 		    clear_ire->ire_phandle,
15084 		    clear_ire->ire_ihandle,
15085 		    clear_ire->ire_flags,
15086 		    &clear_ire->ire_uinfo,
15087 		    NULL,
15088 		    NULL,
15089 		    ipst) == NULL)
15090 			cmn_err(CE_PANIC, "ire_init() failed");
15091 
15092 		refrele_lb_ire = B_TRUE;
15093 
15094 		if (ire_next != NULL &&
15095 		    ire_next->ire_stq != NULL &&
15096 		    ire_next->ire_addr == clear_ire->ire_addr &&
15097 		    ire_next->ire_ipif->ipif_ill ==
15098 		    clear_ire->ire_ipif->ipif_ill) {
15099 			clear_ire_stq = ire_next;
15100 
15101 			bzero(new_nlb_ire, sizeof (ire_t));
15102 			/* XXX We need a recovery strategy here. */
15103 			if (ire_init(new_nlb_ire,
15104 			    (uchar_t *)&clear_ire_stq->ire_addr,
15105 			    (uchar_t *)&clear_ire_stq->ire_mask,
15106 			    (uchar_t *)&clear_ire_stq->ire_src_addr,
15107 			    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15108 			    &clear_ire_stq->ire_max_frag,
15109 			    NULL,
15110 			    clear_ire_stq->ire_rfq,
15111 			    clear_ire_stq->ire_stq,
15112 			    clear_ire_stq->ire_type,
15113 			    clear_ire_stq->ire_ipif,
15114 			    clear_ire_stq->ire_cmask,
15115 			    clear_ire_stq->ire_phandle,
15116 			    clear_ire_stq->ire_ihandle,
15117 			    clear_ire_stq->ire_flags,
15118 			    &clear_ire_stq->ire_uinfo,
15119 			    NULL,
15120 			    NULL,
15121 			    ipst) == NULL)
15122 				cmn_err(CE_PANIC, "ire_init() failed");
15123 
15124 				refrele_nlb_ire = B_TRUE;
15125 			}
15126 
15127 		rw_enter(&irb->irb_lock, RW_WRITER);
15128 		/*
15129 		 * irb_lock was dropped across call to ire_init() due to
15130 		 * lock ordering issue with ipst->ips_ndp{4,6}->ndp_g_lock
15131 		 * mutex lock. Therefore irep could have changed. call
15132 		 * irep_insert() to get the new insertion point (irep) and
15133 		 * recheck all known conditions.
15134 		 */
15135 		irep = NULL;
15136 		clr_ire = irep_insert(illgrp, addr, save_ire, &irep);
15137 		if ((irep != NULL) && (*irep != clear_ire) &&
15138 		    (clr_ire == clear_ire)) {
15139 			if ((clear_ire_stq != NULL) &&
15140 			    (clr_ire->ire_next != clear_ire_stq))
15141 				clear_ire_stq = NULL;
15142 			/*
15143 			 * Delete the ire. We can't call ire_delete() since
15144 			 * we are holding the bucket lock. We can't release the
15145 			 * bucket lock since we can't allow irep to change.
15146 			 * So just mark it CONDEMNED.
15147 			 * The IRB_REFRELE will delete the ire from the list
15148 			 * and do the refrele.
15149 			 */
15150 			clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15151 			irb->irb_marks |= IRB_MARK_CONDEMNED;
15152 
15153 			if (clear_ire_stq != NULL &&
15154 			    clear_ire_stq->ire_nce != NULL) {
15155 				nce_fastpath_list_delete(
15156 				    clear_ire_stq->ire_nce);
15157 				clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15158 			}
15159 
15160 			/*
15161 			 * Also take care of otherfields like ib/ob pkt count
15162 			 * etc. Need to dup them.
15163 			 * ditto in ill_bcast_delete_and_add
15164 			 */
15165 
15166 			/* Set the max_frag before adding the ire */
15167 			max_frag = *new_lb_ire->ire_max_fragp;
15168 			new_lb_ire->ire_max_fragp = NULL;
15169 			new_lb_ire->ire_max_frag = max_frag;
15170 
15171 			/* Add the new ire's. Insert at *irep */
15172 			new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15173 			ire1 = *irep;
15174 			if (ire1 != NULL)
15175 				ire1->ire_ptpn = &new_lb_ire->ire_next;
15176 			new_lb_ire->ire_next = ire1;
15177 			/* Link the new one in. */
15178 			new_lb_ire->ire_ptpn = irep;
15179 			membar_producer();
15180 			*irep = new_lb_ire;
15181 			new_lb_ire_used = B_TRUE;
15182 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15183 			    ire_stats_inserted);
15184 			new_lb_ire->ire_bucket->irb_ire_cnt++;
15185 			DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *),
15186 			    new_lb_ire->ire_ipif,
15187 			    (char *), "ire", (void *), new_lb_ire);
15188 			new_lb_ire->ire_ipif->ipif_ire_cnt++;
15189 
15190 			if (clear_ire_stq != NULL) {
15191 				ill_t	*ire_ill;
15192 				/* Set the max_frag before adding the ire */
15193 				max_frag = *new_nlb_ire->ire_max_fragp;
15194 				new_nlb_ire->ire_max_fragp = NULL;
15195 				new_nlb_ire->ire_max_frag = max_frag;
15196 
15197 				new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15198 				irep = &new_lb_ire->ire_next;
15199 				/* Add the new ire. Insert at *irep */
15200 				ire1 = *irep;
15201 				if (ire1 != NULL)
15202 					ire1->ire_ptpn = &new_nlb_ire->ire_next;
15203 				new_nlb_ire->ire_next = ire1;
15204 				/* Link the new one in. */
15205 				new_nlb_ire->ire_ptpn = irep;
15206 				membar_producer();
15207 				*irep = new_nlb_ire;
15208 				new_nlb_ire_used = B_TRUE;
15209 				BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15210 				    ire_stats_inserted);
15211 				new_nlb_ire->ire_bucket->irb_ire_cnt++;
15212 				DTRACE_PROBE3(ipif__incr__cnt,
15213 				    (ipif_t *), new_nlb_ire->ire_ipif,
15214 				    (char *), "ire", (void *), new_nlb_ire);
15215 				new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15216 				DTRACE_PROBE3(ill__incr__cnt,
15217 				    (ill_t *), new_nlb_ire->ire_stq->q_ptr,
15218 				    (char *), "ire", (void *), new_nlb_ire);
15219 				ire_ill = (ill_t *)new_nlb_ire->ire_stq->q_ptr;
15220 				ire_ill->ill_ire_cnt++;
15221 			}
15222 		}
15223 	}
15224 	ire_refrele(save_ire);
15225 	rw_exit(&irb->irb_lock);
15226 	/*
15227 	 * Since we dropped the irb_lock across call to ire_init()
15228 	 * and rechecking known conditions, it is possible that
15229 	 * the checks might fail, therefore undo the work done by
15230 	 * ire_init() by calling ire_refrele() on the newly created ire.
15231 	 */
15232 	if (!new_lb_ire_used) {
15233 		if (refrele_lb_ire) {
15234 			ire_refrele(new_lb_ire);
15235 		} else {
15236 			kmem_cache_free(ire_cache, new_lb_ire);
15237 		}
15238 	}
15239 	if (!new_nlb_ire_used) {
15240 		if (refrele_nlb_ire) {
15241 			ire_refrele(new_nlb_ire);
15242 		} else {
15243 			kmem_cache_free(ire_cache, new_nlb_ire);
15244 		}
15245 	}
15246 	IRB_REFRELE(irb);
15247 }
15248 
15249 /*
15250  * Whenever an ipif goes down we have to renominate a different
15251  * broadcast ire to receive. Whenever an ipif comes up, we need
15252  * to make sure that we have only one nominated to receive.
15253  */
15254 static void
15255 ipif_renominate_bcast(ipif_t *ipif)
15256 {
15257 	ill_t *ill = ipif->ipif_ill;
15258 	ipaddr_t subnet_addr;
15259 	ipaddr_t net_addr;
15260 	ipaddr_t net_mask = 0;
15261 	ipaddr_t subnet_netmask;
15262 	ipaddr_t addr;
15263 	ill_group_t *illgrp;
15264 	ip_stack_t	*ipst = ill->ill_ipst;
15265 
15266 	illgrp = ill->ill_group;
15267 	/*
15268 	 * If this is the last ipif going down, it might take
15269 	 * the ill out of the group. In that case ipif_down ->
15270 	 * illgrp_delete takes care of doing the nomination.
15271 	 * ipif_down does not call for this case.
15272 	 */
15273 	ASSERT(illgrp != NULL);
15274 
15275 	/* There could not have been any ires associated with this */
15276 	if (ipif->ipif_subnet == 0)
15277 		return;
15278 
15279 	ill_mark_bcast(illgrp, 0, ipst);
15280 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15281 
15282 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15283 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15284 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15285 	} else {
15286 		net_mask = htonl(IN_CLASSA_NET);
15287 	}
15288 	addr = net_mask & ipif->ipif_subnet;
15289 	ill_mark_bcast(illgrp, addr, ipst);
15290 
15291 	net_addr = ~net_mask | addr;
15292 	ill_mark_bcast(illgrp, net_addr, ipst);
15293 
15294 	subnet_netmask = ipif->ipif_net_mask;
15295 	addr = ipif->ipif_subnet;
15296 	ill_mark_bcast(illgrp, addr, ipst);
15297 
15298 	subnet_addr = ~subnet_netmask | addr;
15299 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15300 }
15301 
15302 /*
15303  * Whenever we form or delete ill groups, we need to nominate one set of
15304  * BROADCAST ires for receiving in the group.
15305  *
15306  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15307  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15308  *    for ill_ipif_up_count to be non-zero. This is the only case where
15309  *    ill_ipif_up_count is zero and we would still find the ires.
15310  *
15311  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15312  *    ipif is UP and we just have to do the nomination.
15313  *
15314  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15315  *    from the group. So, we have to do the nomination.
15316  *
15317  * Because of (3), there could be just one ill in the group. But we have
15318  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15319  * Thus, this function does not optimize when there is only one ill as
15320  * it is not correct for (3).
15321  */
15322 static void
15323 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15324 {
15325 	ill_t *ill;
15326 	ipif_t *ipif;
15327 	ipaddr_t subnet_addr;
15328 	ipaddr_t prev_subnet_addr = 0;
15329 	ipaddr_t net_addr;
15330 	ipaddr_t prev_net_addr = 0;
15331 	ipaddr_t net_mask = 0;
15332 	ipaddr_t subnet_netmask;
15333 	ipaddr_t addr;
15334 	ip_stack_t	*ipst;
15335 
15336 	/*
15337 	 * When the last memeber is leaving, there is nothing to
15338 	 * nominate.
15339 	 */
15340 	if (illgrp->illgrp_ill_count == 0) {
15341 		ASSERT(illgrp->illgrp_ill == NULL);
15342 		return;
15343 	}
15344 
15345 	ill = illgrp->illgrp_ill;
15346 	ASSERT(!ill->ill_isv6);
15347 	ipst = ill->ill_ipst;
15348 	/*
15349 	 * We assume that ires with same address and belonging to the
15350 	 * same group, has been grouped together. Nominating a *single*
15351 	 * ill in the group for sending and receiving broadcast is done
15352 	 * by making sure that the first BROADCAST ire (which will be
15353 	 * the one returned by ire_ctable_lookup for ip_rput and the
15354 	 * one that will be used in ip_wput_ire) will be the one that
15355 	 * will not have IRE_MARK_NORECV set.
15356 	 *
15357 	 * 1) ip_rput checks and discards packets received on ires marked
15358 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15359 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15360 	 *    first ire in the group for every broadcast address in the group.
15361 	 *    ip_rput will accept packets only on the first ire i.e only
15362 	 *    one copy of the ill.
15363 	 *
15364 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15365 	 *    packet for the whole group. It needs to send out on the ill
15366 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15367 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15368 	 *    the copy echoed back on other port where the ire is not marked
15369 	 *    with IRE_MARK_NORECV.
15370 	 *
15371 	 * Note that we just need to have the first IRE either loopback or
15372 	 * non-loopback (either of them may not exist if ire_create failed
15373 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15374 	 * always hit the first one and hence will always accept one copy.
15375 	 *
15376 	 * We have a broadcast ire per ill for all the unique prefixes
15377 	 * hosted on that ill. As we don't have a way of knowing the
15378 	 * unique prefixes on a given ill and hence in the whole group,
15379 	 * we just call ill_mark_bcast on all the prefixes that exist
15380 	 * in the group. For the common case of one prefix, the code
15381 	 * below optimizes by remebering the last address used for
15382 	 * markng. In the case of multiple prefixes, this will still
15383 	 * optimize depending the order of prefixes.
15384 	 *
15385 	 * The only unique address across the whole group is 0.0.0.0 and
15386 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15387 	 * the first ire in the bucket for receiving and disables the
15388 	 * others.
15389 	 */
15390 	ill_mark_bcast(illgrp, 0, ipst);
15391 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15392 	for (; ill != NULL; ill = ill->ill_group_next) {
15393 
15394 		for (ipif = ill->ill_ipif; ipif != NULL;
15395 		    ipif = ipif->ipif_next) {
15396 
15397 			if (!(ipif->ipif_flags & IPIF_UP) ||
15398 			    ipif->ipif_subnet == 0) {
15399 				continue;
15400 			}
15401 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15402 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15403 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15404 			} else {
15405 				net_mask = htonl(IN_CLASSA_NET);
15406 			}
15407 			addr = net_mask & ipif->ipif_subnet;
15408 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15409 				ill_mark_bcast(illgrp, addr, ipst);
15410 				net_addr = ~net_mask | addr;
15411 				ill_mark_bcast(illgrp, net_addr, ipst);
15412 			}
15413 			prev_net_addr = addr;
15414 
15415 			subnet_netmask = ipif->ipif_net_mask;
15416 			addr = ipif->ipif_subnet;
15417 			if (prev_subnet_addr == 0 ||
15418 			    prev_subnet_addr != addr) {
15419 				ill_mark_bcast(illgrp, addr, ipst);
15420 				subnet_addr = ~subnet_netmask | addr;
15421 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15422 			}
15423 			prev_subnet_addr = addr;
15424 		}
15425 	}
15426 }
15427 
15428 /*
15429  * This function is called while forming ill groups.
15430  *
15431  * Currently, we handle only allmulti groups. We want to join
15432  * allmulti on only one of the ills in the groups. In future,
15433  * when we have link aggregation, we may have to join normal
15434  * multicast groups on multiple ills as switch does inbound load
15435  * balancing. Following are the functions that calls this
15436  * function :
15437  *
15438  * 1) ill_recover_multicast : Interface is coming back UP.
15439  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15440  *    will call ill_recover_multicast to recover all the multicast
15441  *    groups. We need to make sure that only one member is joined
15442  *    in the ill group.
15443  *
15444  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15445  *    Somebody is joining allmulti. We need to make sure that only one
15446  *    member is joined in the group.
15447  *
15448  * 3) illgrp_insert : If allmulti has already joined, we need to make
15449  *    sure that only one member is joined in the group.
15450  *
15451  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15452  *    allmulti who we have nominated. We need to pick someother ill.
15453  *
15454  * 5) illgrp_delete : The ill we nominated is leaving the group,
15455  *    we need to pick a new ill to join the group.
15456  *
15457  * For (1), (2), (5) - we just have to check whether there is
15458  * a good ill joined in the group. If we could not find any ills
15459  * joined the group, we should join.
15460  *
15461  * For (4), the one that was nominated to receive, left the group.
15462  * There could be nobody joined in the group when this function is
15463  * called.
15464  *
15465  * For (3) - we need to explicitly check whether there are multiple
15466  * ills joined in the group.
15467  *
15468  * For simplicity, we don't differentiate any of the above cases. We
15469  * just leave the group if it is joined on any of them and join on
15470  * the first good ill.
15471  */
15472 int
15473 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15474 {
15475 	ilm_t *ilm;
15476 	ill_t *ill;
15477 	ill_t *fallback_inactive_ill = NULL;
15478 	ill_t *fallback_failed_ill = NULL;
15479 	int ret = 0;
15480 
15481 	/*
15482 	 * Leave the allmulti on all the ills and start fresh.
15483 	 */
15484 	for (ill = illgrp->illgrp_ill; ill != NULL;
15485 	    ill = ill->ill_group_next) {
15486 		if (ill->ill_join_allmulti)
15487 			ill_leave_allmulti(ill);
15488 	}
15489 
15490 	/*
15491 	 * Choose a good ill. Fallback to inactive or failed if
15492 	 * none available. We need to fallback to FAILED in the
15493 	 * case where we have 2 interfaces in a group - where
15494 	 * one of them is failed and another is a good one and
15495 	 * the good one (not marked inactive) is leaving the group.
15496 	 */
15497 	for (ill = illgrp->illgrp_ill; ill != NULL; ill = ill->ill_group_next) {
15498 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15499 			continue;
15500 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15501 			fallback_failed_ill = ill;
15502 			continue;
15503 		}
15504 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15505 			fallback_inactive_ill = ill;
15506 			continue;
15507 		}
15508 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15509 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15510 				ret = ill_join_allmulti(ill);
15511 				/*
15512 				 * ill_join_allmulti() can fail because of
15513 				 * memory failures so make sure we join at
15514 				 * least on one ill.
15515 				 */
15516 				if (ill->ill_join_allmulti)
15517 					return (0);
15518 			}
15519 		}
15520 	}
15521 	if (ret != 0) {
15522 		/*
15523 		 * If we tried nominating above and failed to do so,
15524 		 * return error. We might have tried multiple times.
15525 		 * But, return the latest error.
15526 		 */
15527 		return (ret);
15528 	}
15529 	if ((ill = fallback_inactive_ill) != NULL) {
15530 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15531 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr))
15532 				return (ill_join_allmulti(ill));
15533 		}
15534 	} else if ((ill = fallback_failed_ill) != NULL) {
15535 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15536 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr))
15537 				return (ill_join_allmulti(ill));
15538 		}
15539 	}
15540 	return (0);
15541 }
15542 
15543 /*
15544  * This function is called from illgrp_delete after it is
15545  * deleted from the group to reschedule responsibilities
15546  * to a different ill.
15547  */
15548 static void
15549 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15550 {
15551 	ilm_t	*ilm;
15552 	ipif_t	*ipif;
15553 	ipaddr_t subnet_addr;
15554 	ipaddr_t net_addr;
15555 	ipaddr_t net_mask = 0;
15556 	ipaddr_t subnet_netmask;
15557 	ipaddr_t addr;
15558 	ip_stack_t *ipst = ill->ill_ipst;
15559 
15560 	ASSERT(ill->ill_group == NULL);
15561 	/*
15562 	 * Broadcast Responsibility:
15563 	 *
15564 	 * 1. If this ill has been nominated for receiving broadcast
15565 	 * packets, we need to find a new one. Before we find a new
15566 	 * one, we need to re-group the ires that are part of this new
15567 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15568 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15569 	 * thing for us.
15570 	 *
15571 	 * 2. If this ill was not nominated for receiving broadcast
15572 	 * packets, we need to clear the IRE_MARK_NORECV flag
15573 	 * so that we continue to send up broadcast packets.
15574 	 */
15575 	if (!ill->ill_isv6) {
15576 		/*
15577 		 * Case 1 above : No optimization here. Just redo the
15578 		 * nomination.
15579 		 */
15580 		ill_group_bcast_for_xmit(ill);
15581 		ill_nominate_bcast_rcv(illgrp);
15582 
15583 		/*
15584 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15585 		 */
15586 		ill_clear_bcast_mark(ill, 0);
15587 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15588 
15589 		for (ipif = ill->ill_ipif; ipif != NULL;
15590 		    ipif = ipif->ipif_next) {
15591 
15592 			if (!(ipif->ipif_flags & IPIF_UP) ||
15593 			    ipif->ipif_subnet == 0) {
15594 				continue;
15595 			}
15596 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15597 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15598 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15599 			} else {
15600 				net_mask = htonl(IN_CLASSA_NET);
15601 			}
15602 			addr = net_mask & ipif->ipif_subnet;
15603 			ill_clear_bcast_mark(ill, addr);
15604 
15605 			net_addr = ~net_mask | addr;
15606 			ill_clear_bcast_mark(ill, net_addr);
15607 
15608 			subnet_netmask = ipif->ipif_net_mask;
15609 			addr = ipif->ipif_subnet;
15610 			ill_clear_bcast_mark(ill, addr);
15611 
15612 			subnet_addr = ~subnet_netmask | addr;
15613 			ill_clear_bcast_mark(ill, subnet_addr);
15614 		}
15615 	}
15616 
15617 	/*
15618 	 * Multicast Responsibility.
15619 	 *
15620 	 * If we have joined allmulti on this one, find a new member
15621 	 * in the group to join allmulti. As this ill is already part
15622 	 * of allmulti, we don't have to join on this one.
15623 	 *
15624 	 * If we have not joined allmulti on this one, there is no
15625 	 * responsibility to handoff. But we need to take new
15626 	 * responsibility i.e, join allmulti on this one if we need
15627 	 * to.
15628 	 */
15629 	if (ill->ill_join_allmulti) {
15630 		(void) ill_nominate_mcast_rcv(illgrp);
15631 	} else {
15632 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15633 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15634 				(void) ill_join_allmulti(ill);
15635 				break;
15636 			}
15637 		}
15638 	}
15639 
15640 	/*
15641 	 * We intentionally do the flushing of IRE_CACHES only matching
15642 	 * on the ill and not on groups. Note that we are already deleted
15643 	 * from the group.
15644 	 *
15645 	 * This will make sure that all IRE_CACHES whose stq is pointing
15646 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15647 	 * deleted and IRE_CACHES that are not pointing at this ill will
15648 	 * be left alone.
15649 	 */
15650 	ire_walk_ill(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_CACHE,
15651 	    illgrp_cache_delete, ill, ill);
15652 
15653 	/*
15654 	 * Some conn may have cached one of the IREs deleted above. By removing
15655 	 * the ire reference, we clean up the extra reference to the ill held in
15656 	 * ire->ire_stq.
15657 	 */
15658 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15659 
15660 	/*
15661 	 * Re-do source address selection for all the members in the
15662 	 * group, if they borrowed source address from one of the ipifs
15663 	 * in this ill.
15664 	 */
15665 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15666 		if (ill->ill_isv6) {
15667 			ipif_update_other_ipifs_v6(ipif, illgrp);
15668 		} else {
15669 			ipif_update_other_ipifs(ipif, illgrp);
15670 		}
15671 	}
15672 }
15673 
15674 /*
15675  * Delete the ill from the group. The caller makes sure that it is
15676  * in a group and it okay to delete from the group. So, we always
15677  * delete here.
15678  */
15679 static void
15680 illgrp_delete(ill_t *ill)
15681 {
15682 	ill_group_t *illgrp;
15683 	ill_group_t *tmpg;
15684 	ill_t *tmp_ill;
15685 	ip_stack_t	*ipst = ill->ill_ipst;
15686 
15687 	/*
15688 	 * Reset illgrp_ill_schednext if it was pointing at us.
15689 	 * We need to do this before we set ill_group to NULL.
15690 	 */
15691 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15692 	mutex_enter(&ill->ill_lock);
15693 
15694 	illgrp_reset_schednext(ill);
15695 
15696 	illgrp = ill->ill_group;
15697 
15698 	/* Delete the ill from illgrp. */
15699 	if (illgrp->illgrp_ill == ill) {
15700 		illgrp->illgrp_ill = ill->ill_group_next;
15701 	} else {
15702 		tmp_ill = illgrp->illgrp_ill;
15703 		while (tmp_ill->ill_group_next != ill) {
15704 			tmp_ill = tmp_ill->ill_group_next;
15705 			ASSERT(tmp_ill != NULL);
15706 		}
15707 		tmp_ill->ill_group_next = ill->ill_group_next;
15708 	}
15709 	ill->ill_group = NULL;
15710 	ill->ill_group_next = NULL;
15711 
15712 	illgrp->illgrp_ill_count--;
15713 	mutex_exit(&ill->ill_lock);
15714 	rw_exit(&ipst->ips_ill_g_lock);
15715 
15716 	/*
15717 	 * As this ill is leaving the group, we need to hand off
15718 	 * the responsibilities to the other ills in the group, if
15719 	 * this ill had some responsibilities.
15720 	 */
15721 
15722 	ill_handoff_responsibility(ill, illgrp);
15723 
15724 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15725 
15726 	if (illgrp->illgrp_ill_count == 0) {
15727 
15728 		ASSERT(illgrp->illgrp_ill == NULL);
15729 		if (ill->ill_isv6) {
15730 			if (illgrp == ipst->ips_illgrp_head_v6) {
15731 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15732 			} else {
15733 				tmpg = ipst->ips_illgrp_head_v6;
15734 				while (tmpg->illgrp_next != illgrp) {
15735 					tmpg = tmpg->illgrp_next;
15736 					ASSERT(tmpg != NULL);
15737 				}
15738 				tmpg->illgrp_next = illgrp->illgrp_next;
15739 			}
15740 		} else {
15741 			if (illgrp == ipst->ips_illgrp_head_v4) {
15742 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15743 			} else {
15744 				tmpg = ipst->ips_illgrp_head_v4;
15745 				while (tmpg->illgrp_next != illgrp) {
15746 					tmpg = tmpg->illgrp_next;
15747 					ASSERT(tmpg != NULL);
15748 				}
15749 				tmpg->illgrp_next = illgrp->illgrp_next;
15750 			}
15751 		}
15752 		mutex_destroy(&illgrp->illgrp_lock);
15753 		mi_free(illgrp);
15754 	}
15755 	rw_exit(&ipst->ips_ill_g_lock);
15756 
15757 	/*
15758 	 * Even though the ill is out of the group its not necessary
15759 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15760 	 * We will split the ipsq when phyint_groupname is set to NULL.
15761 	 */
15762 
15763 	/*
15764 	 * Send a routing sockets message if we are deleting from
15765 	 * groups with names.
15766 	 */
15767 	if (ill->ill_phyint->phyint_groupname_len != 0)
15768 		ip_rts_ifmsg(ill->ill_ipif);
15769 }
15770 
15771 /*
15772  * Re-do source address selection. This is normally called when
15773  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15774  * ipif comes up.
15775  */
15776 void
15777 ill_update_source_selection(ill_t *ill)
15778 {
15779 	ipif_t *ipif;
15780 
15781 	ASSERT(IAM_WRITER_ILL(ill));
15782 
15783 	if (ill->ill_group != NULL)
15784 		ill = ill->ill_group->illgrp_ill;
15785 
15786 	for (; ill != NULL; ill = ill->ill_group_next) {
15787 		for (ipif = ill->ill_ipif; ipif != NULL;
15788 		    ipif = ipif->ipif_next) {
15789 			if (ill->ill_isv6)
15790 				ipif_recreate_interface_routes_v6(NULL, ipif);
15791 			else
15792 				ipif_recreate_interface_routes(NULL, ipif);
15793 		}
15794 	}
15795 }
15796 
15797 /*
15798  * Insert ill in a group headed by illgrp_head. The caller can either
15799  * pass a groupname in which case we search for a group with the
15800  * same name to insert in or pass a group to insert in. This function
15801  * would only search groups with names.
15802  *
15803  * NOTE : The caller should make sure that there is at least one ipif
15804  *	  UP on this ill so that illgrp_scheduler can pick this ill
15805  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15806  *	  already sent a DL_UNBIND to the driver and we don't want to
15807  *	  send anymore packets. We don't assert for ipif_up_count
15808  *	  to be greater than zero, because ipif_up_done wants to call
15809  *	  this function before bumping up the ipif_up_count. See
15810  *	  ipif_up_done() for details.
15811  */
15812 int
15813 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15814     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15815 {
15816 	ill_group_t *illgrp;
15817 	ill_t *prev_ill;
15818 	phyint_t *phyi;
15819 	ip_stack_t	*ipst = ill->ill_ipst;
15820 
15821 	ASSERT(ill->ill_group == NULL);
15822 
15823 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15824 	mutex_enter(&ill->ill_lock);
15825 
15826 	if (groupname != NULL) {
15827 		/*
15828 		 * Look for a group with a matching groupname to insert.
15829 		 */
15830 		for (illgrp = *illgrp_head; illgrp != NULL;
15831 		    illgrp = illgrp->illgrp_next) {
15832 
15833 			ill_t *tmp_ill;
15834 
15835 			/*
15836 			 * If we have an ill_group_t in the list which has
15837 			 * no ill_t assigned then we must be in the process of
15838 			 * removing this group. We skip this as illgrp_delete()
15839 			 * will remove it from the list.
15840 			 */
15841 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15842 				ASSERT(illgrp->illgrp_ill_count == 0);
15843 				continue;
15844 			}
15845 
15846 			ASSERT(tmp_ill->ill_phyint != NULL);
15847 			phyi = tmp_ill->ill_phyint;
15848 			/*
15849 			 * Look at groups which has names only.
15850 			 */
15851 			if (phyi->phyint_groupname_len == 0)
15852 				continue;
15853 			/*
15854 			 * Names are stored in the phyint common to both
15855 			 * IPv4 and IPv6.
15856 			 */
15857 			if (mi_strcmp(phyi->phyint_groupname,
15858 			    groupname) == 0) {
15859 				break;
15860 			}
15861 		}
15862 	} else {
15863 		/*
15864 		 * If the caller passes in a NULL "grp_to_insert", we
15865 		 * allocate one below and insert this singleton.
15866 		 */
15867 		illgrp = grp_to_insert;
15868 	}
15869 
15870 	ill->ill_group_next = NULL;
15871 
15872 	if (illgrp == NULL) {
15873 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
15874 		if (illgrp == NULL) {
15875 			return (ENOMEM);
15876 		}
15877 		illgrp->illgrp_next = *illgrp_head;
15878 		*illgrp_head = illgrp;
15879 		illgrp->illgrp_ill = ill;
15880 		illgrp->illgrp_ill_count = 1;
15881 		ill->ill_group = illgrp;
15882 		/*
15883 		 * Used in illgrp_scheduler to protect multiple threads
15884 		 * from traversing the list.
15885 		 */
15886 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
15887 	} else {
15888 		ASSERT(ill->ill_net_type ==
15889 		    illgrp->illgrp_ill->ill_net_type);
15890 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
15891 
15892 		/* Insert ill at tail of this group */
15893 		prev_ill = illgrp->illgrp_ill;
15894 		while (prev_ill->ill_group_next != NULL)
15895 			prev_ill = prev_ill->ill_group_next;
15896 		prev_ill->ill_group_next = ill;
15897 		ill->ill_group = illgrp;
15898 		illgrp->illgrp_ill_count++;
15899 		/*
15900 		 * Inherit group properties. Currently only forwarding
15901 		 * is the property we try to keep the same with all the
15902 		 * ills. When there are more, we will abstract this into
15903 		 * a function.
15904 		 */
15905 		ill->ill_flags &= ~ILLF_ROUTER;
15906 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
15907 	}
15908 	mutex_exit(&ill->ill_lock);
15909 	rw_exit(&ipst->ips_ill_g_lock);
15910 
15911 	/*
15912 	 * 1) When ipif_up_done() calls this function, ipif_up_count
15913 	 *    may be zero as it has not yet been bumped. But the ires
15914 	 *    have already been added. So, we do the nomination here
15915 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
15916 	 *    for ill_ipif_up_count != 0. Thus we don't check for
15917 	 *    ill_ipif_up_count here while nominating broadcast ires for
15918 	 *    receive.
15919 	 *
15920 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
15921 	 *    to group them properly as ire_add() has already happened
15922 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
15923 	 *    case, we need to do it here anyway.
15924 	 */
15925 	if (!ill->ill_isv6) {
15926 		ill_group_bcast_for_xmit(ill);
15927 		ill_nominate_bcast_rcv(illgrp);
15928 	}
15929 
15930 	if (!ipif_is_coming_up) {
15931 		/*
15932 		 * When ipif_up_done() calls this function, the multicast
15933 		 * groups have not been joined yet. So, there is no point in
15934 		 * nomination. ill_join_allmulti() will handle groups when
15935 		 * ill_recover_multicast() is called from ipif_up_done() later.
15936 		 */
15937 		(void) ill_nominate_mcast_rcv(illgrp);
15938 		/*
15939 		 * ipif_up_done calls ill_update_source_selection
15940 		 * anyway. Moreover, we don't want to re-create
15941 		 * interface routes while ipif_up_done() still has reference
15942 		 * to them. Refer to ipif_up_done() for more details.
15943 		 */
15944 		ill_update_source_selection(ill);
15945 	}
15946 
15947 	/*
15948 	 * Send a routing sockets message if we are inserting into
15949 	 * groups with names.
15950 	 */
15951 	if (groupname != NULL)
15952 		ip_rts_ifmsg(ill->ill_ipif);
15953 	return (0);
15954 }
15955 
15956 /*
15957  * Return the first phyint matching the groupname. There could
15958  * be more than one when there are ill groups.
15959  *
15960  * If 'usable' is set, then we exclude ones that are marked with any of
15961  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
15962  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
15963  * emulation of ipmp.
15964  */
15965 phyint_t *
15966 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
15967 {
15968 	phyint_t *phyi;
15969 
15970 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
15971 	/*
15972 	 * Group names are stored in the phyint - a common structure
15973 	 * to both IPv4 and IPv6.
15974 	 */
15975 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
15976 	for (; phyi != NULL;
15977 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15978 	    phyi, AVL_AFTER)) {
15979 		if (phyi->phyint_groupname_len == 0)
15980 			continue;
15981 		/*
15982 		 * Skip the ones that should not be used since the callers
15983 		 * sometime use this for sending packets.
15984 		 */
15985 		if (usable && (phyi->phyint_flags &
15986 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
15987 			continue;
15988 
15989 		ASSERT(phyi->phyint_groupname != NULL);
15990 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
15991 			return (phyi);
15992 	}
15993 	return (NULL);
15994 }
15995 
15996 
15997 /*
15998  * Return the first usable phyint matching the group index. By 'usable'
15999  * we exclude ones that are marked ununsable with any of
16000  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16001  *
16002  * Used only for the ipmp/netinfo emulation of ipmp.
16003  */
16004 phyint_t *
16005 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16006 {
16007 	phyint_t *phyi;
16008 
16009 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16010 
16011 	if (!ipst->ips_ipmp_hook_emulation)
16012 		return (NULL);
16013 
16014 	/*
16015 	 * Group indicies are stored in the phyint - a common structure
16016 	 * to both IPv4 and IPv6.
16017 	 */
16018 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16019 	for (; phyi != NULL;
16020 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16021 	    phyi, AVL_AFTER)) {
16022 		/* Ignore the ones that do not have a group */
16023 		if (phyi->phyint_groupname_len == 0)
16024 			continue;
16025 
16026 		ASSERT(phyi->phyint_group_ifindex != 0);
16027 		/*
16028 		 * Skip the ones that should not be used since the callers
16029 		 * sometime use this for sending packets.
16030 		 */
16031 		if (phyi->phyint_flags &
16032 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16033 			continue;
16034 		if (phyi->phyint_group_ifindex == group_ifindex)
16035 			return (phyi);
16036 	}
16037 	return (NULL);
16038 }
16039 
16040 /*
16041  * MT notes on creation and deletion of IPMP groups
16042  *
16043  * Creation and deletion of IPMP groups introduce the need to merge or
16044  * split the associated serialization objects i.e the ipsq's. Normally all
16045  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16046  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16047  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16048  * is a need to change the <ill-ipsq> association and we have to operate on both
16049  * the source and destination IPMP groups. For eg. attempting to set the
16050  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16051  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16052  * source or destination IPMP group are mapped to a single ipsq for executing
16053  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16054  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16055  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16056  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16057  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16058  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16059  *
16060  * In the above example the ioctl handling code locates the current ipsq of hme0
16061  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16062  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16063  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16064  * the destination ipsq. If the destination ipsq is not busy, it also enters
16065  * the destination ipsq exclusively. Now the actual groupname setting operation
16066  * can proceed. If the destination ipsq is busy, the operation is enqueued
16067  * on the destination (merged) ipsq and will be handled in the unwind from
16068  * ipsq_exit.
16069  *
16070  * To prevent other threads accessing the ill while the group name change is
16071  * in progres, we bring down the ipifs which also removes the ill from the
16072  * group. The group is changed in phyint and when the first ipif on the ill
16073  * is brought up, the ill is inserted into the right IPMP group by
16074  * illgrp_insert.
16075  */
16076 /* ARGSUSED */
16077 int
16078 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16079     ip_ioctl_cmd_t *ipip, void *ifreq)
16080 {
16081 	int i;
16082 	char *tmp;
16083 	int namelen;
16084 	ill_t *ill = ipif->ipif_ill;
16085 	ill_t *ill_v4, *ill_v6;
16086 	int err = 0;
16087 	phyint_t *phyi;
16088 	phyint_t *phyi_tmp;
16089 	struct lifreq *lifr;
16090 	mblk_t	*mp1;
16091 	char *groupname;
16092 	ipsq_t *ipsq;
16093 	ip_stack_t	*ipst = ill->ill_ipst;
16094 
16095 	ASSERT(IAM_WRITER_IPIF(ipif));
16096 
16097 	/* Existance verified in ip_wput_nondata */
16098 	mp1 = mp->b_cont->b_cont;
16099 	lifr = (struct lifreq *)mp1->b_rptr;
16100 	groupname = lifr->lifr_groupname;
16101 
16102 	if (ipif->ipif_id != 0)
16103 		return (EINVAL);
16104 
16105 	phyi = ill->ill_phyint;
16106 	ASSERT(phyi != NULL);
16107 
16108 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16109 		return (EINVAL);
16110 
16111 	tmp = groupname;
16112 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16113 		;
16114 
16115 	if (i == LIFNAMSIZ) {
16116 		/* no null termination */
16117 		return (EINVAL);
16118 	}
16119 
16120 	/*
16121 	 * Calculate the namelen exclusive of the null
16122 	 * termination character.
16123 	 */
16124 	namelen = tmp - groupname;
16125 
16126 	ill_v4 = phyi->phyint_illv4;
16127 	ill_v6 = phyi->phyint_illv6;
16128 
16129 	/*
16130 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16131 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16132 	 * synchronization notes in ip.c
16133 	 */
16134 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16135 		return (EINVAL);
16136 	}
16137 
16138 	/*
16139 	 * mark the ill as changing.
16140 	 * this should queue all new requests on the syncq.
16141 	 */
16142 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16143 
16144 	if (ill_v4 != NULL)
16145 		ill_v4->ill_state_flags |= ILL_CHANGING;
16146 	if (ill_v6 != NULL)
16147 		ill_v6->ill_state_flags |= ILL_CHANGING;
16148 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16149 
16150 	if (namelen == 0) {
16151 		/*
16152 		 * Null string means remove this interface from the
16153 		 * existing group.
16154 		 */
16155 		if (phyi->phyint_groupname_len == 0) {
16156 			/*
16157 			 * Never was in a group.
16158 			 */
16159 			err = 0;
16160 			goto done;
16161 		}
16162 
16163 		/*
16164 		 * IPv4 or IPv6 may be temporarily out of the group when all
16165 		 * the ipifs are down. Thus, we need to check for ill_group to
16166 		 * be non-NULL.
16167 		 */
16168 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16169 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16170 			mutex_enter(&ill_v4->ill_lock);
16171 			if (!ill_is_quiescent(ill_v4)) {
16172 				/*
16173 				 * ipsq_pending_mp_add will not fail since
16174 				 * connp is NULL
16175 				 */
16176 				(void) ipsq_pending_mp_add(NULL,
16177 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16178 				mutex_exit(&ill_v4->ill_lock);
16179 				err = EINPROGRESS;
16180 				goto done;
16181 			}
16182 			mutex_exit(&ill_v4->ill_lock);
16183 		}
16184 
16185 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16186 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16187 			mutex_enter(&ill_v6->ill_lock);
16188 			if (!ill_is_quiescent(ill_v6)) {
16189 				(void) ipsq_pending_mp_add(NULL,
16190 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16191 				mutex_exit(&ill_v6->ill_lock);
16192 				err = EINPROGRESS;
16193 				goto done;
16194 			}
16195 			mutex_exit(&ill_v6->ill_lock);
16196 		}
16197 
16198 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16199 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16200 		mutex_enter(&phyi->phyint_lock);
16201 		ASSERT(phyi->phyint_groupname != NULL);
16202 		mi_free(phyi->phyint_groupname);
16203 		phyi->phyint_groupname = NULL;
16204 		phyi->phyint_groupname_len = 0;
16205 
16206 		/* Restore the ifindex used to be the per interface one */
16207 		phyi->phyint_group_ifindex = 0;
16208 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16209 		mutex_exit(&phyi->phyint_lock);
16210 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16211 		rw_exit(&ipst->ips_ill_g_lock);
16212 		err = ill_up_ipifs(ill, q, mp);
16213 
16214 		/*
16215 		 * set the split flag so that the ipsq can be split
16216 		 */
16217 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16218 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16219 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16220 
16221 	} else {
16222 		if (phyi->phyint_groupname_len != 0) {
16223 			ASSERT(phyi->phyint_groupname != NULL);
16224 			/* Are we inserting in the same group ? */
16225 			if (mi_strcmp(groupname,
16226 			    phyi->phyint_groupname) == 0) {
16227 				err = 0;
16228 				goto done;
16229 			}
16230 		}
16231 
16232 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16233 		/*
16234 		 * Merge ipsq for the group's.
16235 		 * This check is here as multiple groups/ills might be
16236 		 * sharing the same ipsq.
16237 		 * If we have to merege than the operation is restarted
16238 		 * on the new ipsq.
16239 		 */
16240 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16241 		if (phyi->phyint_ipsq != ipsq) {
16242 			rw_exit(&ipst->ips_ill_g_lock);
16243 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16244 			goto done;
16245 		}
16246 		/*
16247 		 * Running exclusive on new ipsq.
16248 		 */
16249 
16250 		ASSERT(ipsq != NULL);
16251 		ASSERT(ipsq->ipsq_writer == curthread);
16252 
16253 		/*
16254 		 * Check whether the ill_type and ill_net_type matches before
16255 		 * we allocate any memory so that the cleanup is easier.
16256 		 *
16257 		 * We can't group dissimilar ones as we can't load spread
16258 		 * packets across the group because of potential link-level
16259 		 * header differences.
16260 		 */
16261 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16262 		if (phyi_tmp != NULL) {
16263 			if ((ill_v4 != NULL &&
16264 			    phyi_tmp->phyint_illv4 != NULL) &&
16265 			    ((ill_v4->ill_net_type !=
16266 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16267 			    (ill_v4->ill_type !=
16268 			    phyi_tmp->phyint_illv4->ill_type))) {
16269 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16270 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16271 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16272 				rw_exit(&ipst->ips_ill_g_lock);
16273 				return (EINVAL);
16274 			}
16275 			if ((ill_v6 != NULL &&
16276 			    phyi_tmp->phyint_illv6 != NULL) &&
16277 			    ((ill_v6->ill_net_type !=
16278 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16279 			    (ill_v6->ill_type !=
16280 			    phyi_tmp->phyint_illv6->ill_type))) {
16281 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16282 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16283 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16284 				rw_exit(&ipst->ips_ill_g_lock);
16285 				return (EINVAL);
16286 			}
16287 		}
16288 
16289 		rw_exit(&ipst->ips_ill_g_lock);
16290 
16291 		/*
16292 		 * bring down all v4 ipifs.
16293 		 */
16294 		if (ill_v4 != NULL) {
16295 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16296 		}
16297 
16298 		/*
16299 		 * bring down all v6 ipifs.
16300 		 */
16301 		if (ill_v6 != NULL) {
16302 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16303 		}
16304 
16305 		/*
16306 		 * make sure all ipifs are down and there are no active
16307 		 * references. Call to ipsq_pending_mp_add will not fail
16308 		 * since connp is NULL.
16309 		 */
16310 		if (ill_v4 != NULL) {
16311 			mutex_enter(&ill_v4->ill_lock);
16312 			if (!ill_is_quiescent(ill_v4)) {
16313 				(void) ipsq_pending_mp_add(NULL,
16314 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16315 				mutex_exit(&ill_v4->ill_lock);
16316 				err = EINPROGRESS;
16317 				goto done;
16318 			}
16319 			mutex_exit(&ill_v4->ill_lock);
16320 		}
16321 
16322 		if (ill_v6 != NULL) {
16323 			mutex_enter(&ill_v6->ill_lock);
16324 			if (!ill_is_quiescent(ill_v6)) {
16325 				(void) ipsq_pending_mp_add(NULL,
16326 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16327 				mutex_exit(&ill_v6->ill_lock);
16328 				err = EINPROGRESS;
16329 				goto done;
16330 			}
16331 			mutex_exit(&ill_v6->ill_lock);
16332 		}
16333 
16334 		/*
16335 		 * allocate including space for null terminator
16336 		 * before we insert.
16337 		 */
16338 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16339 		if (tmp == NULL)
16340 			return (ENOMEM);
16341 
16342 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16343 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16344 		mutex_enter(&phyi->phyint_lock);
16345 		if (phyi->phyint_groupname_len != 0) {
16346 			ASSERT(phyi->phyint_groupname != NULL);
16347 			mi_free(phyi->phyint_groupname);
16348 		}
16349 
16350 		/*
16351 		 * setup the new group name.
16352 		 */
16353 		phyi->phyint_groupname = tmp;
16354 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16355 		phyi->phyint_groupname_len = namelen + 1;
16356 
16357 		if (ipst->ips_ipmp_hook_emulation) {
16358 			/*
16359 			 * If the group already exists we use the existing
16360 			 * group_ifindex, otherwise we pick a new index here.
16361 			 */
16362 			if (phyi_tmp != NULL) {
16363 				phyi->phyint_group_ifindex =
16364 				    phyi_tmp->phyint_group_ifindex;
16365 			} else {
16366 				/* XXX We need a recovery strategy here. */
16367 				if (!ip_assign_ifindex(
16368 				    &phyi->phyint_group_ifindex, ipst))
16369 					cmn_err(CE_PANIC,
16370 					    "ip_assign_ifindex() failed");
16371 			}
16372 		}
16373 		/*
16374 		 * Select whether the netinfo and hook use the per-interface
16375 		 * or per-group ifindex.
16376 		 */
16377 		if (ipst->ips_ipmp_hook_emulation)
16378 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16379 		else
16380 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16381 
16382 		if (ipst->ips_ipmp_hook_emulation &&
16383 		    phyi_tmp != NULL) {
16384 			/* First phyint in group - group PLUMB event */
16385 			ill_nic_event_plumb(ill, B_TRUE);
16386 		}
16387 		mutex_exit(&phyi->phyint_lock);
16388 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16389 		rw_exit(&ipst->ips_ill_g_lock);
16390 
16391 		err = ill_up_ipifs(ill, q, mp);
16392 	}
16393 
16394 done:
16395 	/*
16396 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16397 	 */
16398 	if (err != EINPROGRESS) {
16399 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16400 		if (ill_v4 != NULL)
16401 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16402 		if (ill_v6 != NULL)
16403 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16404 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16405 	}
16406 	return (err);
16407 }
16408 
16409 /* ARGSUSED */
16410 int
16411 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16412     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16413 {
16414 	ill_t *ill;
16415 	phyint_t *phyi;
16416 	struct lifreq *lifr;
16417 	mblk_t	*mp1;
16418 
16419 	/* Existence verified in ip_wput_nondata */
16420 	mp1 = mp->b_cont->b_cont;
16421 	lifr = (struct lifreq *)mp1->b_rptr;
16422 	ill = ipif->ipif_ill;
16423 	phyi = ill->ill_phyint;
16424 
16425 	lifr->lifr_groupname[0] = '\0';
16426 	/*
16427 	 * ill_group may be null if all the interfaces
16428 	 * are down. But still, the phyint should always
16429 	 * hold the name.
16430 	 */
16431 	if (phyi->phyint_groupname_len != 0) {
16432 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16433 		    phyi->phyint_groupname_len);
16434 	}
16435 
16436 	return (0);
16437 }
16438 
16439 
16440 typedef struct conn_move_s {
16441 	ill_t	*cm_from_ill;
16442 	ill_t	*cm_to_ill;
16443 	int	cm_ifindex;
16444 } conn_move_t;
16445 
16446 /*
16447  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16448  */
16449 static void
16450 conn_move(conn_t *connp, caddr_t arg)
16451 {
16452 	conn_move_t *connm;
16453 	int ifindex;
16454 	int i;
16455 	ill_t *from_ill;
16456 	ill_t *to_ill;
16457 	ilg_t *ilg;
16458 	ilm_t *ret_ilm;
16459 
16460 	connm = (conn_move_t *)arg;
16461 	ifindex = connm->cm_ifindex;
16462 	from_ill = connm->cm_from_ill;
16463 	to_ill = connm->cm_to_ill;
16464 
16465 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16466 
16467 	/* All multicast fields protected by conn_lock */
16468 	mutex_enter(&connp->conn_lock);
16469 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16470 	if ((connp->conn_outgoing_ill == from_ill) &&
16471 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16472 		connp->conn_outgoing_ill = to_ill;
16473 		connp->conn_incoming_ill = to_ill;
16474 	}
16475 
16476 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16477 
16478 	if ((connp->conn_multicast_ill == from_ill) &&
16479 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16480 		connp->conn_multicast_ill = connm->cm_to_ill;
16481 	}
16482 
16483 	/*
16484 	 * Change the ilg_ill to point to the new one. This assumes
16485 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16486 	 * has been told to receive packets on this interface.
16487 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16488 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16489 	 * some ilms may not have moved. We check to see whether
16490 	 * the ilms have moved to to_ill. We can't check on from_ill
16491 	 * as in the process of moving, we could have split an ilm
16492 	 * in to two - which has the same orig_ifindex and v6group.
16493 	 *
16494 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16495 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16496 	 */
16497 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16498 		ilg = &connp->conn_ilg[i];
16499 		if ((ilg->ilg_ill == from_ill) &&
16500 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16501 			/* ifindex != 0 indicates failback */
16502 			if (ifindex != 0) {
16503 				connp->conn_ilg[i].ilg_ill = to_ill;
16504 				continue;
16505 			}
16506 
16507 			mutex_enter(&to_ill->ill_lock);
16508 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16509 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16510 			    connp->conn_zoneid);
16511 			mutex_exit(&to_ill->ill_lock);
16512 
16513 			if (ret_ilm != NULL)
16514 				connp->conn_ilg[i].ilg_ill = to_ill;
16515 		}
16516 	}
16517 	mutex_exit(&connp->conn_lock);
16518 }
16519 
16520 static void
16521 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16522 {
16523 	conn_move_t connm;
16524 	ip_stack_t	*ipst = from_ill->ill_ipst;
16525 
16526 	connm.cm_from_ill = from_ill;
16527 	connm.cm_to_ill = to_ill;
16528 	connm.cm_ifindex = ifindex;
16529 
16530 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16531 }
16532 
16533 /*
16534  * ilm has been moved from from_ill to to_ill.
16535  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16536  * appropriately.
16537  *
16538  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16539  *	  the code there de-references ipif_ill to get the ill to
16540  *	  send multicast requests. It does not work as ipif is on its
16541  *	  move and already moved when this function is called.
16542  *	  Thus, we need to use from_ill and to_ill send down multicast
16543  *	  requests.
16544  */
16545 static void
16546 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16547 {
16548 	ipif_t *ipif;
16549 	ilm_t *ilm;
16550 
16551 	/*
16552 	 * See whether we need to send down DL_ENABMULTI_REQ on
16553 	 * to_ill as ilm has just been added.
16554 	 */
16555 	ASSERT(IAM_WRITER_ILL(to_ill));
16556 	ASSERT(IAM_WRITER_ILL(from_ill));
16557 
16558 	ILM_WALKER_HOLD(to_ill);
16559 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16560 
16561 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16562 			continue;
16563 		/*
16564 		 * no locks held, ill/ipif cannot dissappear as long
16565 		 * as we are writer.
16566 		 */
16567 		ipif = to_ill->ill_ipif;
16568 		/*
16569 		 * No need to hold any lock as we are the writer and this
16570 		 * can only be changed by a writer.
16571 		 */
16572 		ilm->ilm_is_new = B_FALSE;
16573 
16574 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16575 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16576 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16577 			    "resolver\n"));
16578 			continue;		/* Must be IRE_IF_NORESOLVER */
16579 		}
16580 
16581 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16582 			ip1dbg(("ilm_send_multicast_reqs: "
16583 			    "to_ill MULTI_BCAST\n"));
16584 			goto from;
16585 		}
16586 
16587 		if (to_ill->ill_isv6)
16588 			mld_joingroup(ilm);
16589 		else
16590 			igmp_joingroup(ilm);
16591 
16592 		if (to_ill->ill_ipif_up_count == 0) {
16593 			/*
16594 			 * Nobody there. All multicast addresses will be
16595 			 * re-joined when we get the DL_BIND_ACK bringing the
16596 			 * interface up.
16597 			 */
16598 			ilm->ilm_notify_driver = B_FALSE;
16599 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16600 			goto from;
16601 		}
16602 
16603 		/*
16604 		 * For allmulti address, we want to join on only one interface.
16605 		 * Checking for ilm_numentries_v6 is not correct as you may
16606 		 * find an ilm with zero address on to_ill, but we may not
16607 		 * have nominated to_ill for receiving. Thus, if we have
16608 		 * nominated from_ill (ill_join_allmulti is set), nominate
16609 		 * only if to_ill is not already nominated (to_ill normally
16610 		 * should not have been nominated if "from_ill" has already
16611 		 * been nominated. As we don't prevent failovers from happening
16612 		 * across groups, we don't assert).
16613 		 */
16614 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16615 			/*
16616 			 * There is no need to hold ill locks as we are
16617 			 * writer on both ills and when ill_join_allmulti()
16618 			 * is called the thread is always a writer.
16619 			 */
16620 			if (from_ill->ill_join_allmulti &&
16621 			    !to_ill->ill_join_allmulti) {
16622 				(void) ill_join_allmulti(to_ill);
16623 			}
16624 		} else if (ilm->ilm_notify_driver) {
16625 
16626 			/*
16627 			 * This is a newly moved ilm so we need to tell the
16628 			 * driver about the new group. There can be more than
16629 			 * one ilm's for the same group in the list each with a
16630 			 * different orig_ifindex. We have to inform the driver
16631 			 * once. In ilm_move_v[4,6] we only set the flag
16632 			 * ilm_notify_driver for the first ilm.
16633 			 */
16634 
16635 			(void) ip_ll_send_enabmulti_req(to_ill,
16636 			    &ilm->ilm_v6addr);
16637 		}
16638 
16639 		ilm->ilm_notify_driver = B_FALSE;
16640 
16641 		/*
16642 		 * See whether we need to send down DL_DISABMULTI_REQ on
16643 		 * from_ill as ilm has just been removed.
16644 		 */
16645 from:
16646 		ipif = from_ill->ill_ipif;
16647 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16648 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16649 			ip1dbg(("ilm_send_multicast_reqs: "
16650 			    "from_ill not resolver\n"));
16651 			continue;		/* Must be IRE_IF_NORESOLVER */
16652 		}
16653 
16654 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16655 			ip1dbg(("ilm_send_multicast_reqs: "
16656 			    "from_ill MULTI_BCAST\n"));
16657 			continue;
16658 		}
16659 
16660 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16661 			if (from_ill->ill_join_allmulti)
16662 				ill_leave_allmulti(from_ill);
16663 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16664 			(void) ip_ll_send_disabmulti_req(from_ill,
16665 			    &ilm->ilm_v6addr);
16666 		}
16667 	}
16668 	ILM_WALKER_RELE(to_ill);
16669 }
16670 
16671 /*
16672  * This function is called when all multicast memberships needs
16673  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16674  * called only once unlike the IPv4 counterpart where it is called after
16675  * every logical interface is moved. The reason is due to multicast
16676  * memberships are joined using an interface address in IPv4 while in
16677  * IPv6, interface index is used.
16678  */
16679 static void
16680 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16681 {
16682 	ilm_t	*ilm;
16683 	ilm_t	*ilm_next;
16684 	ilm_t	*new_ilm;
16685 	ilm_t	**ilmp;
16686 	int	count;
16687 	char buf[INET6_ADDRSTRLEN];
16688 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16689 	ip_stack_t	*ipst = from_ill->ill_ipst;
16690 
16691 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16692 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16693 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16694 
16695 	if (ifindex == 0) {
16696 		/*
16697 		 * Form the solicited node mcast address which is used later.
16698 		 */
16699 		ipif_t *ipif;
16700 
16701 		ipif = from_ill->ill_ipif;
16702 		ASSERT(ipif->ipif_id == 0);
16703 
16704 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16705 	}
16706 
16707 	ilmp = &from_ill->ill_ilm;
16708 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16709 		ilm_next = ilm->ilm_next;
16710 
16711 		if (ilm->ilm_flags & ILM_DELETED) {
16712 			ilmp = &ilm->ilm_next;
16713 			continue;
16714 		}
16715 
16716 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16717 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16718 		ASSERT(ilm->ilm_orig_ifindex != 0);
16719 		if (ilm->ilm_orig_ifindex == ifindex) {
16720 			/*
16721 			 * We are failing back multicast memberships.
16722 			 * If the same ilm exists in to_ill, it means somebody
16723 			 * has joined the same group there e.g. ff02::1
16724 			 * is joined within the kernel when the interfaces
16725 			 * came UP.
16726 			 */
16727 			ASSERT(ilm->ilm_ipif == NULL);
16728 			if (new_ilm != NULL) {
16729 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16730 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16731 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16732 					new_ilm->ilm_is_new = B_TRUE;
16733 				}
16734 			} else {
16735 				/*
16736 				 * check if we can just move the ilm
16737 				 */
16738 				if (from_ill->ill_ilm_walker_cnt != 0) {
16739 					/*
16740 					 * We have walkers we cannot move
16741 					 * the ilm, so allocate a new ilm,
16742 					 * this (old) ilm will be marked
16743 					 * ILM_DELETED at the end of the loop
16744 					 * and will be freed when the
16745 					 * last walker exits.
16746 					 */
16747 					new_ilm = (ilm_t *)mi_zalloc
16748 					    (sizeof (ilm_t));
16749 					if (new_ilm == NULL) {
16750 						ip0dbg(("ilm_move_v6: "
16751 						    "FAILBACK of IPv6"
16752 						    " multicast address %s : "
16753 						    "from %s to"
16754 						    " %s failed : ENOMEM \n",
16755 						    inet_ntop(AF_INET6,
16756 						    &ilm->ilm_v6addr, buf,
16757 						    sizeof (buf)),
16758 						    from_ill->ill_name,
16759 						    to_ill->ill_name));
16760 
16761 							ilmp = &ilm->ilm_next;
16762 							continue;
16763 					}
16764 					*new_ilm = *ilm;
16765 					/*
16766 					 * we don't want new_ilm linked to
16767 					 * ilm's filter list.
16768 					 */
16769 					new_ilm->ilm_filter = NULL;
16770 				} else {
16771 					/*
16772 					 * No walkers we can move the ilm.
16773 					 * lets take it out of the list.
16774 					 */
16775 					*ilmp = ilm->ilm_next;
16776 					ilm->ilm_next = NULL;
16777 					DTRACE_PROBE3(ill__decr__cnt,
16778 					    (ill_t *), from_ill,
16779 					    (char *), "ilm", (void *), ilm);
16780 					ASSERT(from_ill->ill_ilm_cnt > 0);
16781 					from_ill->ill_ilm_cnt--;
16782 
16783 					new_ilm = ilm;
16784 				}
16785 
16786 				/*
16787 				 * if this is the first ilm for the group
16788 				 * set ilm_notify_driver so that we notify the
16789 				 * driver in ilm_send_multicast_reqs.
16790 				 */
16791 				if (ilm_lookup_ill_v6(to_ill,
16792 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16793 					new_ilm->ilm_notify_driver = B_TRUE;
16794 
16795 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
16796 				    (char *), "ilm", (void *), new_ilm);
16797 				new_ilm->ilm_ill = to_ill;
16798 				to_ill->ill_ilm_cnt++;
16799 
16800 				/* Add to the to_ill's list */
16801 				new_ilm->ilm_next = to_ill->ill_ilm;
16802 				to_ill->ill_ilm = new_ilm;
16803 				/*
16804 				 * set the flag so that mld_joingroup is
16805 				 * called in ilm_send_multicast_reqs().
16806 				 */
16807 				new_ilm->ilm_is_new = B_TRUE;
16808 			}
16809 			goto bottom;
16810 		} else if (ifindex != 0) {
16811 			/*
16812 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16813 			 * has not matched above, look at the next ilm.
16814 			 */
16815 			ilmp = &ilm->ilm_next;
16816 			continue;
16817 		}
16818 		/*
16819 		 * If we are here, it means ifindex is 0. Failover
16820 		 * everything.
16821 		 *
16822 		 * We need to handle solicited node mcast address
16823 		 * and all_nodes mcast address differently as they
16824 		 * are joined witin the kenrel (ipif_multicast_up)
16825 		 * and potentially from the userland. We are called
16826 		 * after the ipifs of from_ill has been moved.
16827 		 * If we still find ilms on ill with solicited node
16828 		 * mcast address or all_nodes mcast address, it must
16829 		 * belong to the UP interface that has not moved e.g.
16830 		 * ipif_id 0 with the link local prefix does not move.
16831 		 * We join this on the new ill accounting for all the
16832 		 * userland memberships so that applications don't
16833 		 * see any failure.
16834 		 *
16835 		 * We need to make sure that we account only for the
16836 		 * solicited node and all node multicast addresses
16837 		 * that was brought UP on these. In the case of
16838 		 * a failover from A to B, we might have ilms belonging
16839 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16840 		 * for the membership from the userland. If we are failing
16841 		 * over from B to C now, we will find the ones belonging
16842 		 * to A on B. These don't account for the ill_ipif_up_count.
16843 		 * They just move from B to C. The check below on
16844 		 * ilm_orig_ifindex ensures that.
16845 		 */
16846 		if ((ilm->ilm_orig_ifindex ==
16847 		    from_ill->ill_phyint->phyint_ifindex) &&
16848 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16849 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16850 		    &ilm->ilm_v6addr))) {
16851 			ASSERT(ilm->ilm_refcnt > 0);
16852 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16853 			/*
16854 			 * For indentation reasons, we are not using a
16855 			 * "else" here.
16856 			 */
16857 			if (count == 0) {
16858 				ilmp = &ilm->ilm_next;
16859 				continue;
16860 			}
16861 			ilm->ilm_refcnt -= count;
16862 			if (new_ilm != NULL) {
16863 				/*
16864 				 * Can find one with the same
16865 				 * ilm_orig_ifindex, if we are failing
16866 				 * over to a STANDBY. This happens
16867 				 * when somebody wants to join a group
16868 				 * on a STANDBY interface and we
16869 				 * internally join on a different one.
16870 				 * If we had joined on from_ill then, a
16871 				 * failover now will find a new ilm
16872 				 * with this index.
16873 				 */
16874 				ip1dbg(("ilm_move_v6: FAILOVER, found"
16875 				    " new ilm on %s, group address %s\n",
16876 				    to_ill->ill_name,
16877 				    inet_ntop(AF_INET6,
16878 				    &ilm->ilm_v6addr, buf,
16879 				    sizeof (buf))));
16880 				new_ilm->ilm_refcnt += count;
16881 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16882 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16883 					new_ilm->ilm_is_new = B_TRUE;
16884 				}
16885 			} else {
16886 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
16887 				if (new_ilm == NULL) {
16888 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
16889 					    " multicast address %s : from %s to"
16890 					    " %s failed : ENOMEM \n",
16891 					    inet_ntop(AF_INET6,
16892 					    &ilm->ilm_v6addr, buf,
16893 					    sizeof (buf)), from_ill->ill_name,
16894 					    to_ill->ill_name));
16895 					ilmp = &ilm->ilm_next;
16896 					continue;
16897 				}
16898 				*new_ilm = *ilm;
16899 				new_ilm->ilm_filter = NULL;
16900 				new_ilm->ilm_refcnt = count;
16901 				new_ilm->ilm_timer = INFINITY;
16902 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
16903 				new_ilm->ilm_is_new = B_TRUE;
16904 				/*
16905 				 * If the to_ill has not joined this
16906 				 * group we need to tell the driver in
16907 				 * ill_send_multicast_reqs.
16908 				 */
16909 				if (ilm_lookup_ill_v6(to_ill,
16910 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16911 					new_ilm->ilm_notify_driver = B_TRUE;
16912 
16913 				new_ilm->ilm_ill = to_ill;
16914 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
16915 				    (char *), "ilm", (void *), new_ilm);
16916 				to_ill->ill_ilm_cnt++;
16917 
16918 				/* Add to the to_ill's list */
16919 				new_ilm->ilm_next = to_ill->ill_ilm;
16920 				to_ill->ill_ilm = new_ilm;
16921 				ASSERT(new_ilm->ilm_ipif == NULL);
16922 			}
16923 			if (ilm->ilm_refcnt == 0) {
16924 				goto bottom;
16925 			} else {
16926 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16927 				CLEAR_SLIST(new_ilm->ilm_filter);
16928 				ilmp = &ilm->ilm_next;
16929 			}
16930 			continue;
16931 		} else {
16932 			/*
16933 			 * ifindex = 0 means, move everything pointing at
16934 			 * from_ill. We are doing this becuase ill has
16935 			 * either FAILED or became INACTIVE.
16936 			 *
16937 			 * As we would like to move things later back to
16938 			 * from_ill, we want to retain the identity of this
16939 			 * ilm. Thus, we don't blindly increment the reference
16940 			 * count on the ilms matching the address alone. We
16941 			 * need to match on the ilm_orig_index also. new_ilm
16942 			 * was obtained by matching ilm_orig_index also.
16943 			 */
16944 			if (new_ilm != NULL) {
16945 				/*
16946 				 * This is possible only if a previous restore
16947 				 * was incomplete i.e restore to
16948 				 * ilm_orig_ifindex left some ilms because
16949 				 * of some failures. Thus when we are failing
16950 				 * again, we might find our old friends there.
16951 				 */
16952 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
16953 				    " on %s, group address %s\n",
16954 				    to_ill->ill_name,
16955 				    inet_ntop(AF_INET6,
16956 				    &ilm->ilm_v6addr, buf,
16957 				    sizeof (buf))));
16958 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16959 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16960 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16961 					new_ilm->ilm_is_new = B_TRUE;
16962 				}
16963 			} else {
16964 				if (from_ill->ill_ilm_walker_cnt != 0) {
16965 					new_ilm = (ilm_t *)
16966 					    mi_zalloc(sizeof (ilm_t));
16967 					if (new_ilm == NULL) {
16968 						ip0dbg(("ilm_move_v6: "
16969 						    "FAILOVER of IPv6"
16970 						    " multicast address %s : "
16971 						    "from %s to"
16972 						    " %s failed : ENOMEM \n",
16973 						    inet_ntop(AF_INET6,
16974 						    &ilm->ilm_v6addr, buf,
16975 						    sizeof (buf)),
16976 						    from_ill->ill_name,
16977 						    to_ill->ill_name));
16978 
16979 							ilmp = &ilm->ilm_next;
16980 							continue;
16981 					}
16982 					*new_ilm = *ilm;
16983 					new_ilm->ilm_filter = NULL;
16984 				} else {
16985 					*ilmp = ilm->ilm_next;
16986 					DTRACE_PROBE3(ill__decr__cnt,
16987 					    (ill_t *), from_ill,
16988 					    (char *), "ilm", (void *), ilm);
16989 					ASSERT(from_ill->ill_ilm_cnt > 0);
16990 					from_ill->ill_ilm_cnt--;
16991 
16992 					new_ilm = ilm;
16993 				}
16994 				/*
16995 				 * If the to_ill has not joined this
16996 				 * group we need to tell the driver in
16997 				 * ill_send_multicast_reqs.
16998 				 */
16999 				if (ilm_lookup_ill_v6(to_ill,
17000 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17001 					new_ilm->ilm_notify_driver = B_TRUE;
17002 
17003 				/* Add to the to_ill's list */
17004 				new_ilm->ilm_next = to_ill->ill_ilm;
17005 				to_ill->ill_ilm = new_ilm;
17006 				ASSERT(ilm->ilm_ipif == NULL);
17007 				new_ilm->ilm_ill = to_ill;
17008 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17009 				    (char *), "ilm", (void *), new_ilm);
17010 				to_ill->ill_ilm_cnt++;
17011 				new_ilm->ilm_is_new = B_TRUE;
17012 			}
17013 
17014 		}
17015 
17016 bottom:
17017 		/*
17018 		 * Revert multicast filter state to (EXCLUDE, NULL).
17019 		 * new_ilm->ilm_is_new should already be set if needed.
17020 		 */
17021 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17022 		CLEAR_SLIST(new_ilm->ilm_filter);
17023 		/*
17024 		 * We allocated/got a new ilm, free the old one.
17025 		 */
17026 		if (new_ilm != ilm) {
17027 			if (from_ill->ill_ilm_walker_cnt == 0) {
17028 				*ilmp = ilm->ilm_next;
17029 
17030 				ASSERT(ilm->ilm_ipif == NULL); /* ipv6 */
17031 				DTRACE_PROBE3(ill__decr__cnt, (ill_t *),
17032 				    from_ill, (char *), "ilm", (void *), ilm);
17033 				ASSERT(from_ill->ill_ilm_cnt > 0);
17034 				from_ill->ill_ilm_cnt--;
17035 
17036 				ilm_inactive(ilm); /* frees this ilm */
17037 
17038 			} else {
17039 				ilm->ilm_flags |= ILM_DELETED;
17040 				from_ill->ill_ilm_cleanup_reqd = 1;
17041 				ilmp = &ilm->ilm_next;
17042 			}
17043 		}
17044 	}
17045 }
17046 
17047 /*
17048  * Move all the multicast memberships to to_ill. Called when
17049  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17050  * different from IPv6 counterpart as multicast memberships are associated
17051  * with ills in IPv6. This function is called after every ipif is moved
17052  * unlike IPv6, where it is moved only once.
17053  */
17054 static void
17055 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17056 {
17057 	ilm_t	*ilm;
17058 	ilm_t	*ilm_next;
17059 	ilm_t	*new_ilm;
17060 	ilm_t	**ilmp;
17061 	ip_stack_t	*ipst = from_ill->ill_ipst;
17062 
17063 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17064 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17065 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17066 
17067 	ilmp = &from_ill->ill_ilm;
17068 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17069 		ilm_next = ilm->ilm_next;
17070 
17071 		if (ilm->ilm_flags & ILM_DELETED) {
17072 			ilmp = &ilm->ilm_next;
17073 			continue;
17074 		}
17075 
17076 		ASSERT(ilm->ilm_ipif != NULL);
17077 
17078 		if (ilm->ilm_ipif != ipif) {
17079 			ilmp = &ilm->ilm_next;
17080 			continue;
17081 		}
17082 
17083 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17084 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17085 			new_ilm = ilm_lookup_ipif(ipif,
17086 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17087 			if (new_ilm != NULL) {
17088 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17089 				/*
17090 				 * We still need to deal with the from_ill.
17091 				 */
17092 				new_ilm->ilm_is_new = B_TRUE;
17093 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17094 				CLEAR_SLIST(new_ilm->ilm_filter);
17095 				ASSERT(ilm->ilm_ipif == ipif);
17096 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17097 				if (from_ill->ill_ilm_walker_cnt == 0) {
17098 					DTRACE_PROBE3(ill__decr__cnt,
17099 					    (ill_t *), from_ill,
17100 					    (char *), "ilm", (void *), ilm);
17101 					ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17102 				}
17103 				goto delete_ilm;
17104 			}
17105 			/*
17106 			 * If we could not find one e.g. ipif is
17107 			 * still down on to_ill, we add this ilm
17108 			 * on ill_new to preserve the reference
17109 			 * count.
17110 			 */
17111 		}
17112 		/*
17113 		 * When ipifs move, ilms always move with it
17114 		 * to the NEW ill. Thus we should never be
17115 		 * able to find ilm till we really move it here.
17116 		 */
17117 		ASSERT(ilm_lookup_ipif(ipif,
17118 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17119 
17120 		if (from_ill->ill_ilm_walker_cnt != 0) {
17121 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17122 			if (new_ilm == NULL) {
17123 				char buf[INET6_ADDRSTRLEN];
17124 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17125 				    " multicast address %s : "
17126 				    "from %s to"
17127 				    " %s failed : ENOMEM \n",
17128 				    inet_ntop(AF_INET,
17129 				    &ilm->ilm_v6addr, buf,
17130 				    sizeof (buf)),
17131 				    from_ill->ill_name,
17132 				    to_ill->ill_name));
17133 
17134 				ilmp = &ilm->ilm_next;
17135 				continue;
17136 			}
17137 			*new_ilm = *ilm;
17138 			DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), ipif,
17139 			    (char *), "ilm", (void *), ilm);
17140 			new_ilm->ilm_ipif->ipif_ilm_cnt++;
17141 			/* We don't want new_ilm linked to ilm's filter list */
17142 			new_ilm->ilm_filter = NULL;
17143 		} else {
17144 			/* Remove from the list */
17145 			*ilmp = ilm->ilm_next;
17146 			new_ilm = ilm;
17147 		}
17148 
17149 		/*
17150 		 * If we have never joined this group on the to_ill
17151 		 * make sure we tell the driver.
17152 		 */
17153 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17154 		    ALL_ZONES) == NULL)
17155 			new_ilm->ilm_notify_driver = B_TRUE;
17156 
17157 		/* Add to the to_ill's list */
17158 		new_ilm->ilm_next = to_ill->ill_ilm;
17159 		to_ill->ill_ilm = new_ilm;
17160 		new_ilm->ilm_is_new = B_TRUE;
17161 
17162 		/*
17163 		 * Revert multicast filter state to (EXCLUDE, NULL)
17164 		 */
17165 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17166 		CLEAR_SLIST(new_ilm->ilm_filter);
17167 
17168 		/*
17169 		 * Delete only if we have allocated a new ilm.
17170 		 */
17171 		if (new_ilm != ilm) {
17172 delete_ilm:
17173 			if (from_ill->ill_ilm_walker_cnt == 0) {
17174 				/* Remove from the list */
17175 				*ilmp = ilm->ilm_next;
17176 				ilm->ilm_next = NULL;
17177 				DTRACE_PROBE3(ipif__decr__cnt,
17178 				    (ipif_t *), ilm->ilm_ipif,
17179 				    (char *), "ilm", (void *), ilm);
17180 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17181 				ilm->ilm_ipif->ipif_ilm_cnt--;
17182 				ilm_inactive(ilm);
17183 			} else {
17184 				ilm->ilm_flags |= ILM_DELETED;
17185 				from_ill->ill_ilm_cleanup_reqd = 1;
17186 				ilmp = &ilm->ilm_next;
17187 			}
17188 		}
17189 	}
17190 }
17191 
17192 static uint_t
17193 ipif_get_id(ill_t *ill, uint_t id)
17194 {
17195 	uint_t	unit;
17196 	ipif_t	*tipif;
17197 	boolean_t found = B_FALSE;
17198 	ip_stack_t	*ipst = ill->ill_ipst;
17199 
17200 	/*
17201 	 * During failback, we want to go back to the same id
17202 	 * instead of the smallest id so that the original
17203 	 * configuration is maintained. id is non-zero in that
17204 	 * case.
17205 	 */
17206 	if (id != 0) {
17207 		/*
17208 		 * While failing back, if we still have an ipif with
17209 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17210 		 * as soon as we return from this function. It was
17211 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17212 		 * we can choose the smallest id. Thus we return zero
17213 		 * in that case ignoring the hint.
17214 		 */
17215 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17216 			return (0);
17217 		for (tipif = ill->ill_ipif; tipif != NULL;
17218 		    tipif = tipif->ipif_next) {
17219 			if (tipif->ipif_id == id) {
17220 				found = B_TRUE;
17221 				break;
17222 			}
17223 		}
17224 		/*
17225 		 * If somebody already plumbed another logical
17226 		 * with the same id, we won't be able to find it.
17227 		 */
17228 		if (!found)
17229 			return (id);
17230 	}
17231 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17232 		found = B_FALSE;
17233 		for (tipif = ill->ill_ipif; tipif != NULL;
17234 		    tipif = tipif->ipif_next) {
17235 			if (tipif->ipif_id == unit) {
17236 				found = B_TRUE;
17237 				break;
17238 			}
17239 		}
17240 		if (!found)
17241 			break;
17242 	}
17243 	return (unit);
17244 }
17245 
17246 /* ARGSUSED */
17247 static int
17248 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17249     ipif_t **rep_ipif_ptr)
17250 {
17251 	ill_t	*from_ill;
17252 	ipif_t	*rep_ipif;
17253 	uint_t	unit;
17254 	int err = 0;
17255 	ipif_t	*to_ipif;
17256 	struct iocblk	*iocp;
17257 	boolean_t failback_cmd;
17258 	boolean_t remove_ipif;
17259 	int	rc;
17260 	ip_stack_t	*ipst;
17261 
17262 	ASSERT(IAM_WRITER_ILL(to_ill));
17263 	ASSERT(IAM_WRITER_IPIF(ipif));
17264 
17265 	iocp = (struct iocblk *)mp->b_rptr;
17266 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17267 	remove_ipif = B_FALSE;
17268 
17269 	from_ill = ipif->ipif_ill;
17270 	ipst = from_ill->ill_ipst;
17271 
17272 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17273 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17274 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17275 
17276 	/*
17277 	 * Don't move LINK LOCAL addresses as they are tied to
17278 	 * physical interface.
17279 	 */
17280 	if (from_ill->ill_isv6 &&
17281 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17282 		ipif->ipif_was_up = B_FALSE;
17283 		IPIF_UNMARK_MOVING(ipif);
17284 		return (0);
17285 	}
17286 
17287 	/*
17288 	 * We set the ipif_id to maximum so that the search for
17289 	 * ipif_id will pick the lowest number i.e 0 in the
17290 	 * following 2 cases :
17291 	 *
17292 	 * 1) We have a replacement ipif at the head of to_ill.
17293 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17294 	 *    on to_ill and hence the MOVE might fail. We want to
17295 	 *    remove it only if we could move the ipif. Thus, by
17296 	 *    setting it to the MAX value, we make the search in
17297 	 *    ipif_get_id return the zeroth id.
17298 	 *
17299 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17300 	 *    we might just have a zero address plumbed on the ipif
17301 	 *    with zero id in the case of IPv4. We remove that while
17302 	 *    doing the failback. We want to remove it only if we
17303 	 *    could move the ipif. Thus, by setting it to the MAX
17304 	 *    value, we make the search in ipif_get_id return the
17305 	 *    zeroth id.
17306 	 *
17307 	 * Both (1) and (2) are done only when when we are moving
17308 	 * an ipif (either due to failover/failback) which originally
17309 	 * belonged to this interface i.e the ipif_orig_ifindex is
17310 	 * the same as to_ill's ifindex. This is needed so that
17311 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17312 	 * from B -> A (B is being removed from the group) and
17313 	 * FAILBACK from A -> B restores the original configuration.
17314 	 * Without the check for orig_ifindex, the second FAILOVER
17315 	 * could make the ipif belonging to B replace the A's zeroth
17316 	 * ipif and the subsequent failback re-creating the replacement
17317 	 * ipif again.
17318 	 *
17319 	 * NOTE : We created the replacement ipif when we did a
17320 	 * FAILOVER (See below). We could check for FAILBACK and
17321 	 * then look for replacement ipif to be removed. But we don't
17322 	 * want to do that because we wan't to allow the possibility
17323 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17324 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17325 	 * from B -> A.
17326 	 */
17327 	to_ipif = to_ill->ill_ipif;
17328 	if ((to_ill->ill_phyint->phyint_ifindex ==
17329 	    ipif->ipif_orig_ifindex) &&
17330 	    to_ipif->ipif_replace_zero) {
17331 		ASSERT(to_ipif->ipif_id == 0);
17332 		remove_ipif = B_TRUE;
17333 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17334 	}
17335 	/*
17336 	 * Find the lowest logical unit number on the to_ill.
17337 	 * If we are failing back, try to get the original id
17338 	 * rather than the lowest one so that the original
17339 	 * configuration is maintained.
17340 	 *
17341 	 * XXX need a better scheme for this.
17342 	 */
17343 	if (failback_cmd) {
17344 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17345 	} else {
17346 		unit = ipif_get_id(to_ill, 0);
17347 	}
17348 
17349 	/* Reset back to zero in case we fail below */
17350 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17351 		to_ipif->ipif_id = 0;
17352 
17353 	if (unit == ipst->ips_ip_addrs_per_if) {
17354 		ipif->ipif_was_up = B_FALSE;
17355 		IPIF_UNMARK_MOVING(ipif);
17356 		return (EINVAL);
17357 	}
17358 
17359 	/*
17360 	 * ipif is ready to move from "from_ill" to "to_ill".
17361 	 *
17362 	 * 1) If we are moving ipif with id zero, create a
17363 	 *    replacement ipif for this ipif on from_ill. If this fails
17364 	 *    fail the MOVE operation.
17365 	 *
17366 	 * 2) Remove the replacement ipif on to_ill if any.
17367 	 *    We could remove the replacement ipif when we are moving
17368 	 *    the ipif with id zero. But what if somebody already
17369 	 *    unplumbed it ? Thus we always remove it if it is present.
17370 	 *    We want to do it only if we are sure we are going to
17371 	 *    move the ipif to to_ill which is why there are no
17372 	 *    returns due to error till ipif is linked to to_ill.
17373 	 *    Note that the first ipif that we failback will always
17374 	 *    be zero if it is present.
17375 	 */
17376 	if (ipif->ipif_id == 0) {
17377 		ipaddr_t inaddr_any = INADDR_ANY;
17378 
17379 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17380 		if (rep_ipif == NULL) {
17381 			ipif->ipif_was_up = B_FALSE;
17382 			IPIF_UNMARK_MOVING(ipif);
17383 			return (ENOMEM);
17384 		}
17385 		*rep_ipif = ipif_zero;
17386 		/*
17387 		 * Before we put the ipif on the list, store the addresses
17388 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17389 		 * assumes so. This logic is not any different from what
17390 		 * ipif_allocate does.
17391 		 */
17392 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17393 		    &rep_ipif->ipif_v6lcl_addr);
17394 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17395 		    &rep_ipif->ipif_v6src_addr);
17396 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17397 		    &rep_ipif->ipif_v6subnet);
17398 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17399 		    &rep_ipif->ipif_v6net_mask);
17400 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17401 		    &rep_ipif->ipif_v6brd_addr);
17402 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17403 		    &rep_ipif->ipif_v6pp_dst_addr);
17404 		/*
17405 		 * We mark IPIF_NOFAILOVER so that this can never
17406 		 * move.
17407 		 */
17408 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17409 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17410 		rep_ipif->ipif_replace_zero = B_TRUE;
17411 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17412 		    MUTEX_DEFAULT, NULL);
17413 		rep_ipif->ipif_id = 0;
17414 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17415 		rep_ipif->ipif_ill = from_ill;
17416 		rep_ipif->ipif_orig_ifindex =
17417 		    from_ill->ill_phyint->phyint_ifindex;
17418 		/* Insert at head */
17419 		rep_ipif->ipif_next = from_ill->ill_ipif;
17420 		from_ill->ill_ipif = rep_ipif;
17421 		/*
17422 		 * We don't really care to let apps know about
17423 		 * this interface.
17424 		 */
17425 	}
17426 
17427 	if (remove_ipif) {
17428 		/*
17429 		 * We set to a max value above for this case to get
17430 		 * id zero. ASSERT that we did get one.
17431 		 */
17432 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17433 		rep_ipif = to_ipif;
17434 		to_ill->ill_ipif = rep_ipif->ipif_next;
17435 		rep_ipif->ipif_next = NULL;
17436 		/*
17437 		 * If some apps scanned and find this interface,
17438 		 * it is time to let them know, so that they can
17439 		 * delete it.
17440 		 */
17441 
17442 		*rep_ipif_ptr = rep_ipif;
17443 	}
17444 
17445 	/* Get it out of the ILL interface list. */
17446 	ipif_remove(ipif, B_FALSE);
17447 
17448 	/* Assign the new ill */
17449 	ipif->ipif_ill = to_ill;
17450 	ipif->ipif_id = unit;
17451 	/* id has already been checked */
17452 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17453 	ASSERT(rc == 0);
17454 	/* Let SCTP update its list */
17455 	sctp_move_ipif(ipif, from_ill, to_ill);
17456 	/*
17457 	 * Handle the failover and failback of ipif_t between
17458 	 * ill_t that have differing maximum mtu values.
17459 	 */
17460 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17461 		if (ipif->ipif_saved_mtu == 0) {
17462 			/*
17463 			 * As this ipif_t is moving to an ill_t
17464 			 * that has a lower ill_max_mtu, its
17465 			 * ipif_mtu needs to be saved so it can
17466 			 * be restored during failback or during
17467 			 * failover to an ill_t which has a
17468 			 * higher ill_max_mtu.
17469 			 */
17470 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17471 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17472 		} else {
17473 			/*
17474 			 * The ipif_t is, once again, moving to
17475 			 * an ill_t that has a lower maximum mtu
17476 			 * value.
17477 			 */
17478 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17479 		}
17480 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17481 	    ipif->ipif_saved_mtu != 0) {
17482 		/*
17483 		 * The mtu of this ipif_t had to be reduced
17484 		 * during an earlier failover; this is an
17485 		 * opportunity for it to be increased (either as
17486 		 * part of another failover or a failback).
17487 		 */
17488 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17489 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17490 			ipif->ipif_saved_mtu = 0;
17491 		} else {
17492 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17493 		}
17494 	}
17495 
17496 	/*
17497 	 * We preserve all the other fields of the ipif including
17498 	 * ipif_saved_ire_mp. The routes that are saved here will
17499 	 * be recreated on the new interface and back on the old
17500 	 * interface when we move back.
17501 	 */
17502 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17503 
17504 	return (err);
17505 }
17506 
17507 static int
17508 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17509     int ifindex, ipif_t **rep_ipif_ptr)
17510 {
17511 	ipif_t *mipif;
17512 	ipif_t *ipif_next;
17513 	int err;
17514 
17515 	/*
17516 	 * We don't really try to MOVE back things if some of the
17517 	 * operations fail. The daemon will take care of moving again
17518 	 * later on.
17519 	 */
17520 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17521 		ipif_next = mipif->ipif_next;
17522 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17523 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17524 
17525 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17526 
17527 			/*
17528 			 * When the MOVE fails, it is the job of the
17529 			 * application to take care of this properly
17530 			 * i.e try again if it is ENOMEM.
17531 			 */
17532 			if (mipif->ipif_ill != from_ill) {
17533 				/*
17534 				 * ipif has moved.
17535 				 *
17536 				 * Move the multicast memberships associated
17537 				 * with this ipif to the new ill. For IPv6, we
17538 				 * do it once after all the ipifs are moved
17539 				 * (in ill_move) as they are not associated
17540 				 * with ipifs.
17541 				 *
17542 				 * We need to move the ilms as the ipif has
17543 				 * already been moved to a new ill even
17544 				 * in the case of errors. Neither
17545 				 * ilm_free(ipif) will find the ilm
17546 				 * when somebody unplumbs this ipif nor
17547 				 * ilm_delete(ilm) will be able to find the
17548 				 * ilm, if we don't move now.
17549 				 */
17550 				if (!from_ill->ill_isv6)
17551 					ilm_move_v4(from_ill, to_ill, mipif);
17552 			}
17553 
17554 			if (err != 0)
17555 				return (err);
17556 		}
17557 	}
17558 	return (0);
17559 }
17560 
17561 static int
17562 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17563 {
17564 	int ifindex;
17565 	int err;
17566 	struct iocblk	*iocp;
17567 	ipif_t	*ipif;
17568 	ipif_t *rep_ipif_ptr = NULL;
17569 	ipif_t	*from_ipif = NULL;
17570 	boolean_t check_rep_if = B_FALSE;
17571 	ip_stack_t	*ipst = from_ill->ill_ipst;
17572 
17573 	iocp = (struct iocblk *)mp->b_rptr;
17574 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17575 		/*
17576 		 * Move everything pointing at from_ill to to_ill.
17577 		 * We acheive this by passing in 0 as ifindex.
17578 		 */
17579 		ifindex = 0;
17580 	} else {
17581 		/*
17582 		 * Move everything pointing at from_ill whose original
17583 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17584 		 * We acheive this by passing in ifindex rather than 0.
17585 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17586 		 */
17587 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17588 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17589 	}
17590 
17591 	/*
17592 	 * Determine if there is at least one ipif that would move from
17593 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17594 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17595 	 * the move, in which case we need to quiesce the replacement ipif also.
17596 	 */
17597 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17598 	    from_ipif = from_ipif->ipif_next) {
17599 		if (((ifindex == 0) ||
17600 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17601 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17602 			check_rep_if = B_TRUE;
17603 			break;
17604 		}
17605 	}
17606 
17607 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17608 
17609 	GRAB_ILL_LOCKS(from_ill, to_ill);
17610 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17611 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17612 		    mp, ILL_MOVE_OK);
17613 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17614 		return (EINPROGRESS);
17615 	}
17616 
17617 	/* Check if the replacement ipif is quiescent to delete */
17618 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17619 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17620 		to_ill->ill_ipif->ipif_state_flags |=
17621 		    IPIF_MOVING | IPIF_CHANGING;
17622 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17623 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17624 			    mp, ILL_MOVE_OK);
17625 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17626 			return (EINPROGRESS);
17627 		}
17628 	}
17629 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17630 
17631 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17632 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17633 	GRAB_ILL_LOCKS(from_ill, to_ill);
17634 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17635 
17636 	/* ilm_move is done inside ipif_move for IPv4 */
17637 	if (err == 0 && from_ill->ill_isv6)
17638 		ilm_move_v6(from_ill, to_ill, ifindex);
17639 
17640 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17641 	rw_exit(&ipst->ips_ill_g_lock);
17642 
17643 	/*
17644 	 * send rts messages and multicast messages.
17645 	 */
17646 	if (rep_ipif_ptr != NULL) {
17647 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17648 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17649 			rep_ipif_ptr->ipif_recovery_id = 0;
17650 		}
17651 		ip_rts_ifmsg(rep_ipif_ptr);
17652 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17653 #ifdef DEBUG
17654 		ipif_trace_cleanup(rep_ipif_ptr);
17655 #endif
17656 		mi_free(rep_ipif_ptr);
17657 	}
17658 
17659 	conn_move_ill(from_ill, to_ill, ifindex);
17660 
17661 	return (err);
17662 }
17663 
17664 /*
17665  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17666  * Also checks for the validity of the arguments.
17667  * Note: We are already exclusive inside the from group.
17668  * It is upto the caller to release refcnt on the to_ill's.
17669  */
17670 static int
17671 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17672     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17673 {
17674 	int dst_index;
17675 	ipif_t *ipif_v4, *ipif_v6;
17676 	struct lifreq *lifr;
17677 	mblk_t *mp1;
17678 	boolean_t exists;
17679 	sin_t	*sin;
17680 	int	err = 0;
17681 	ip_stack_t	*ipst;
17682 
17683 	if (CONN_Q(q))
17684 		ipst = CONNQ_TO_IPST(q);
17685 	else
17686 		ipst = ILLQ_TO_IPST(q);
17687 
17688 	if ((mp1 = mp->b_cont) == NULL)
17689 		return (EPROTO);
17690 
17691 	if ((mp1 = mp1->b_cont) == NULL)
17692 		return (EPROTO);
17693 
17694 	lifr = (struct lifreq *)mp1->b_rptr;
17695 	sin = (sin_t *)&lifr->lifr_addr;
17696 
17697 	/*
17698 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17699 	 * specific operations.
17700 	 */
17701 	if (sin->sin_family != AF_UNSPEC)
17702 		return (EINVAL);
17703 
17704 	/*
17705 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17706 	 * NULLs for the last 4 args and we know the lookup won't fail
17707 	 * with EINPROGRESS.
17708 	 */
17709 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17710 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17711 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17712 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17713 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17714 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17715 
17716 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17717 		return (ENXIO);
17718 
17719 	if (ipif_v4 != NULL) {
17720 		ASSERT(ipif_v4->ipif_refcnt != 0);
17721 		if (ipif_v4->ipif_id != 0) {
17722 			err = EINVAL;
17723 			goto done;
17724 		}
17725 
17726 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17727 		*ill_from_v4 = ipif_v4->ipif_ill;
17728 	}
17729 
17730 	if (ipif_v6 != NULL) {
17731 		ASSERT(ipif_v6->ipif_refcnt != 0);
17732 		if (ipif_v6->ipif_id != 0) {
17733 			err = EINVAL;
17734 			goto done;
17735 		}
17736 
17737 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17738 		*ill_from_v6 = ipif_v6->ipif_ill;
17739 	}
17740 
17741 	err = 0;
17742 	dst_index = lifr->lifr_movetoindex;
17743 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17744 	    q, mp, ip_process_ioctl, &err, ipst);
17745 	if (err != 0) {
17746 		/*
17747 		 * A move may be in progress, EINPROGRESS looking up the "to"
17748 		 * ill means changes already done to the "from" ipsq need to
17749 		 * be undone to avoid potential deadlocks.
17750 		 *
17751 		 * ENXIO will usually be because there is only v6 on the ill,
17752 		 * that's not treated as an error unless an ENXIO is also
17753 		 * seen when looking up the v6 "to" ill.
17754 		 *
17755 		 * If EINPROGRESS, the mp has been enqueued and can not be
17756 		 * used to look up the v6 "to" ill, but a preemptive clean
17757 		 * up of changes to the v6 "from" ipsq is done.
17758 		 */
17759 		if (err == EINPROGRESS) {
17760 			if (*ill_from_v4 != NULL) {
17761 				ill_t   *from_ill;
17762 				ipsq_t  *from_ipsq;
17763 
17764 				from_ill = ipif_v4->ipif_ill;
17765 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17766 
17767 				mutex_enter(&from_ipsq->ipsq_lock);
17768 				from_ipsq->ipsq_current_ipif = NULL;
17769 				mutex_exit(&from_ipsq->ipsq_lock);
17770 			}
17771 			if (*ill_from_v6 != NULL) {
17772 				ill_t   *from_ill;
17773 				ipsq_t  *from_ipsq;
17774 
17775 				from_ill = ipif_v6->ipif_ill;
17776 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17777 
17778 				mutex_enter(&from_ipsq->ipsq_lock);
17779 				from_ipsq->ipsq_current_ipif = NULL;
17780 				mutex_exit(&from_ipsq->ipsq_lock);
17781 			}
17782 			goto done;
17783 		}
17784 		ASSERT(err == ENXIO);
17785 		err = 0;
17786 	}
17787 
17788 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17789 	    q, mp, ip_process_ioctl, &err, ipst);
17790 	if (err != 0) {
17791 		/*
17792 		 * A move may be in progress, EINPROGRESS looking up the "to"
17793 		 * ill means changes already done to the "from" ipsq need to
17794 		 * be undone to avoid potential deadlocks.
17795 		 */
17796 		if (err == EINPROGRESS) {
17797 			if (*ill_from_v6 != NULL) {
17798 				ill_t   *from_ill;
17799 				ipsq_t  *from_ipsq;
17800 
17801 				from_ill = ipif_v6->ipif_ill;
17802 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17803 
17804 				mutex_enter(&from_ipsq->ipsq_lock);
17805 				from_ipsq->ipsq_current_ipif = NULL;
17806 				mutex_exit(&from_ipsq->ipsq_lock);
17807 			}
17808 			goto done;
17809 		}
17810 		ASSERT(err == ENXIO);
17811 
17812 		/* Both v4 and v6 lookup failed */
17813 		if (*ill_to_v4 == NULL) {
17814 			err = ENXIO;
17815 			goto done;
17816 		}
17817 		err = 0;
17818 	}
17819 
17820 	/*
17821 	 * If we have something to MOVE i.e "from" not NULL,
17822 	 * "to" should be non-NULL.
17823 	 */
17824 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17825 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17826 		err = EINVAL;
17827 	}
17828 
17829 done:
17830 	if (ipif_v4 != NULL)
17831 		ipif_refrele(ipif_v4);
17832 	if (ipif_v6 != NULL)
17833 		ipif_refrele(ipif_v6);
17834 	return (err);
17835 }
17836 
17837 /*
17838  * FAILOVER and FAILBACK are modelled as MOVE operations.
17839  *
17840  * We don't check whether the MOVE is within the same group or
17841  * not, because this ioctl can be used as a generic mechanism
17842  * to failover from interface A to B, though things will function
17843  * only if they are really part of the same group. Moreover,
17844  * all ipifs may be down and hence temporarily out of the group.
17845  *
17846  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17847  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17848  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17849  * have been deleted and there are no active references. Once quiescent the
17850  * ipif's are moved and brought up on the new ill.
17851  *
17852  * Normally the source ill and destination ill belong to the same IPMP group
17853  * and hence the same ipsq_t. In the event they don't belong to the same
17854  * same group the two ipsq's are first merged into one ipsq - that of the
17855  * to_ill. The multicast memberships on the source and destination ill cannot
17856  * change during the move operation since multicast joins/leaves also have to
17857  * execute on the same ipsq and are hence serialized.
17858  */
17859 /* ARGSUSED */
17860 int
17861 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17862     ip_ioctl_cmd_t *ipip, void *ifreq)
17863 {
17864 	ill_t *ill_to_v4 = NULL;
17865 	ill_t *ill_to_v6 = NULL;
17866 	ill_t *ill_from_v4 = NULL;
17867 	ill_t *ill_from_v6 = NULL;
17868 	int err = 0;
17869 
17870 	/*
17871 	 * setup from and to ill's, we can get EINPROGRESS only for
17872 	 * to_ill's.
17873 	 */
17874 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17875 	    &ill_to_v4, &ill_to_v6);
17876 
17877 	if (err != 0) {
17878 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17879 		goto done;
17880 	}
17881 
17882 	/*
17883 	 * nothing to do.
17884 	 */
17885 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17886 		goto done;
17887 	}
17888 
17889 	/*
17890 	 * nothing to do.
17891 	 */
17892 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17893 		goto done;
17894 	}
17895 
17896 	/*
17897 	 * Mark the ill as changing.
17898 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17899 	 * in ill_up_ipifs in case of error they are cleared below.
17900 	 */
17901 
17902 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17903 	if (ill_from_v4 != NULL)
17904 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17905 	if (ill_from_v6 != NULL)
17906 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17907 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17908 
17909 	/*
17910 	 * Make sure that both src and dst are
17911 	 * in the same syncq group. If not make it happen.
17912 	 * We are not holding any locks because we are the writer
17913 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17914 	 * to protect to_ipsq against changing.
17915 	 */
17916 	if (ill_from_v4 != NULL) {
17917 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17918 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17919 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17920 			    NULL, mp, q);
17921 			goto err_ret;
17922 
17923 		}
17924 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
17925 	} else {
17926 
17927 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
17928 		    ill_to_v6->ill_phyint->phyint_ipsq) {
17929 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
17930 			    NULL, mp, q);
17931 			goto err_ret;
17932 
17933 		}
17934 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
17935 	}
17936 
17937 	/*
17938 	 * Now that the ipsq's have been merged and we are the writer
17939 	 * lets mark to_ill as changing as well.
17940 	 */
17941 
17942 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17943 	if (ill_to_v4 != NULL)
17944 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
17945 	if (ill_to_v6 != NULL)
17946 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
17947 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17948 
17949 	/*
17950 	 * Its ok for us to proceed with the move even if
17951 	 * ill_pending_mp is non null on one of the from ill's as the reply
17952 	 * should not be looking at the ipif, it should only care about the
17953 	 * ill itself.
17954 	 */
17955 
17956 	/*
17957 	 * lets move ipv4 first.
17958 	 */
17959 	if (ill_from_v4 != NULL) {
17960 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
17961 		ill_from_v4->ill_move_in_progress = B_TRUE;
17962 		ill_to_v4->ill_move_in_progress = B_TRUE;
17963 		ill_to_v4->ill_move_peer = ill_from_v4;
17964 		ill_from_v4->ill_move_peer = ill_to_v4;
17965 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
17966 	}
17967 
17968 	/*
17969 	 * Now lets move ipv6.
17970 	 */
17971 	if (err == 0 && ill_from_v6 != NULL) {
17972 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
17973 		ill_from_v6->ill_move_in_progress = B_TRUE;
17974 		ill_to_v6->ill_move_in_progress = B_TRUE;
17975 		ill_to_v6->ill_move_peer = ill_from_v6;
17976 		ill_from_v6->ill_move_peer = ill_to_v6;
17977 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
17978 	}
17979 
17980 err_ret:
17981 	/*
17982 	 * EINPROGRESS means we are waiting for the ipif's that need to be
17983 	 * moved to become quiescent.
17984 	 */
17985 	if (err == EINPROGRESS) {
17986 		goto done;
17987 	}
17988 
17989 	/*
17990 	 * if err is set ill_up_ipifs will not be called
17991 	 * lets clear the flags.
17992 	 */
17993 
17994 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17995 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17996 	/*
17997 	 * Some of the clearing may be redundant. But it is simple
17998 	 * not making any extra checks.
17999 	 */
18000 	if (ill_from_v6 != NULL) {
18001 		ill_from_v6->ill_move_in_progress = B_FALSE;
18002 		ill_from_v6->ill_move_peer = NULL;
18003 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18004 	}
18005 	if (ill_from_v4 != NULL) {
18006 		ill_from_v4->ill_move_in_progress = B_FALSE;
18007 		ill_from_v4->ill_move_peer = NULL;
18008 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18009 	}
18010 	if (ill_to_v6 != NULL) {
18011 		ill_to_v6->ill_move_in_progress = B_FALSE;
18012 		ill_to_v6->ill_move_peer = NULL;
18013 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18014 	}
18015 	if (ill_to_v4 != NULL) {
18016 		ill_to_v4->ill_move_in_progress = B_FALSE;
18017 		ill_to_v4->ill_move_peer = NULL;
18018 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18019 	}
18020 
18021 	/*
18022 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18023 	 * Do this always to maintain proper state i.e even in case of errors.
18024 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18025 	 * we need not call on both v4 and v6 interfaces.
18026 	 */
18027 	if (ill_from_v4 != NULL) {
18028 		if ((ill_from_v4->ill_phyint->phyint_flags &
18029 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18030 			phyint_inactive(ill_from_v4->ill_phyint);
18031 		}
18032 	} else if (ill_from_v6 != NULL) {
18033 		if ((ill_from_v6->ill_phyint->phyint_flags &
18034 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18035 			phyint_inactive(ill_from_v6->ill_phyint);
18036 		}
18037 	}
18038 
18039 	if (ill_to_v4 != NULL) {
18040 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18041 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18042 		}
18043 	} else if (ill_to_v6 != NULL) {
18044 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18045 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18046 		}
18047 	}
18048 
18049 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18050 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18051 
18052 no_err:
18053 	/*
18054 	 * lets bring the interfaces up on the to_ill.
18055 	 */
18056 	if (err == 0) {
18057 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18058 		    q, mp);
18059 	}
18060 
18061 	if (err == 0) {
18062 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18063 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18064 
18065 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18066 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18067 	}
18068 done:
18069 
18070 	if (ill_to_v4 != NULL) {
18071 		ill_refrele(ill_to_v4);
18072 	}
18073 	if (ill_to_v6 != NULL) {
18074 		ill_refrele(ill_to_v6);
18075 	}
18076 
18077 	return (err);
18078 }
18079 
18080 static void
18081 ill_dl_down(ill_t *ill)
18082 {
18083 	/*
18084 	 * The ill is down; unbind but stay attached since we're still
18085 	 * associated with a PPA. If we have negotiated DLPI capabilites
18086 	 * with the data link service provider (IDS_OK) then reset them.
18087 	 * The interval between unbinding and rebinding is potentially
18088 	 * unbounded hence we cannot assume things will be the same.
18089 	 * The DLPI capabilities will be probed again when the data link
18090 	 * is brought up.
18091 	 */
18092 	mblk_t	*mp = ill->ill_unbind_mp;
18093 
18094 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18095 
18096 	ill->ill_unbind_mp = NULL;
18097 	if (mp != NULL) {
18098 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18099 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18100 		    ill->ill_name));
18101 		mutex_enter(&ill->ill_lock);
18102 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18103 		mutex_exit(&ill->ill_lock);
18104 		/*
18105 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
18106 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
18107 		 * ill_capability_dld_disable disable rightaway. If this is not
18108 		 * an unplumb operation then the disable happens on receipt of
18109 		 * the capab ack via ip_rput_dlpi_writer ->
18110 		 * ill_capability_ack_thr. In both cases the order of
18111 		 * the operations seen by DLD is capability disable followed
18112 		 * by DL_UNBIND. Also the DLD capability disable needs a
18113 		 * cv_wait'able context.
18114 		 */
18115 		if (ill->ill_state_flags & ILL_CONDEMNED)
18116 			ill_capability_dld_disable(ill);
18117 		ill_capability_reset(ill, B_FALSE);
18118 		ill_dlpi_send(ill, mp);
18119 	}
18120 
18121 	/*
18122 	 * Toss all of our multicast memberships.  We could keep them, but
18123 	 * then we'd have to do bookkeeping of any joins and leaves performed
18124 	 * by the application while the the interface is down (we can't just
18125 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18126 	 * on a downed interface).
18127 	 */
18128 	ill_leave_multicast(ill);
18129 
18130 	mutex_enter(&ill->ill_lock);
18131 	ill->ill_dl_up = 0;
18132 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
18133 	mutex_exit(&ill->ill_lock);
18134 }
18135 
18136 static void
18137 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18138 {
18139 	union DL_primitives *dlp;
18140 	t_uscalar_t prim;
18141 
18142 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18143 
18144 	dlp = (union DL_primitives *)mp->b_rptr;
18145 	prim = dlp->dl_primitive;
18146 
18147 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18148 	    dl_primstr(prim), prim, ill->ill_name));
18149 
18150 	switch (prim) {
18151 	case DL_PHYS_ADDR_REQ:
18152 	{
18153 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18154 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18155 		break;
18156 	}
18157 	case DL_BIND_REQ:
18158 		mutex_enter(&ill->ill_lock);
18159 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18160 		mutex_exit(&ill->ill_lock);
18161 		break;
18162 	}
18163 
18164 	/*
18165 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18166 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18167 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18168 	 */
18169 	mutex_enter(&ill->ill_lock);
18170 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18171 	    (prim == DL_UNBIND_REQ)) {
18172 		ill->ill_dlpi_pending = prim;
18173 	}
18174 	mutex_exit(&ill->ill_lock);
18175 	putnext(ill->ill_wq, mp);
18176 }
18177 
18178 /*
18179  * Helper function for ill_dlpi_send().
18180  */
18181 /* ARGSUSED */
18182 static void
18183 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18184 {
18185 	ill_dlpi_send(q->q_ptr, mp);
18186 }
18187 
18188 /*
18189  * Send a DLPI control message to the driver but make sure there
18190  * is only one outstanding message. Uses ill_dlpi_pending to tell
18191  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18192  * when an ACK or a NAK is received to process the next queued message.
18193  */
18194 void
18195 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18196 {
18197 	mblk_t **mpp;
18198 
18199 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18200 
18201 	/*
18202 	 * To ensure that any DLPI requests for current exclusive operation
18203 	 * are always completely sent before any DLPI messages for other
18204 	 * operations, require writer access before enqueuing.
18205 	 */
18206 	if (!IAM_WRITER_ILL(ill)) {
18207 		ill_refhold(ill);
18208 		/* qwriter_ip() does the ill_refrele() */
18209 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18210 		    NEW_OP, B_TRUE);
18211 		return;
18212 	}
18213 
18214 	mutex_enter(&ill->ill_lock);
18215 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18216 		/* Must queue message. Tail insertion */
18217 		mpp = &ill->ill_dlpi_deferred;
18218 		while (*mpp != NULL)
18219 			mpp = &((*mpp)->b_next);
18220 
18221 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18222 		    ill->ill_name));
18223 
18224 		*mpp = mp;
18225 		mutex_exit(&ill->ill_lock);
18226 		return;
18227 	}
18228 	mutex_exit(&ill->ill_lock);
18229 	ill_dlpi_dispatch(ill, mp);
18230 }
18231 
18232 static void
18233 ill_capability_send(ill_t *ill, mblk_t *mp)
18234 {
18235 	ill->ill_capab_pending_cnt++;
18236 	ill_dlpi_send(ill, mp);
18237 }
18238 
18239 void
18240 ill_capability_done(ill_t *ill)
18241 {
18242 	ASSERT(ill->ill_capab_pending_cnt != 0);
18243 
18244 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
18245 
18246 	ill->ill_capab_pending_cnt--;
18247 	if (ill->ill_capab_pending_cnt == 0 &&
18248 	    ill->ill_dlpi_capab_state == IDCS_OK)
18249 		ill_capability_reset_alloc(ill);
18250 }
18251 
18252 /*
18253  * Send all deferred DLPI messages without waiting for their ACKs.
18254  */
18255 void
18256 ill_dlpi_send_deferred(ill_t *ill)
18257 {
18258 	mblk_t *mp, *nextmp;
18259 
18260 	/*
18261 	 * Clear ill_dlpi_pending so that the message is not queued in
18262 	 * ill_dlpi_send().
18263 	 */
18264 	mutex_enter(&ill->ill_lock);
18265 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18266 	mp = ill->ill_dlpi_deferred;
18267 	ill->ill_dlpi_deferred = NULL;
18268 	mutex_exit(&ill->ill_lock);
18269 
18270 	for (; mp != NULL; mp = nextmp) {
18271 		nextmp = mp->b_next;
18272 		mp->b_next = NULL;
18273 		ill_dlpi_send(ill, mp);
18274 	}
18275 }
18276 
18277 /*
18278  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18279  */
18280 boolean_t
18281 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18282 {
18283 	t_uscalar_t pending;
18284 
18285 	mutex_enter(&ill->ill_lock);
18286 	if (ill->ill_dlpi_pending == prim) {
18287 		mutex_exit(&ill->ill_lock);
18288 		return (B_TRUE);
18289 	}
18290 
18291 	/*
18292 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
18293 	 * without waiting, so don't print any warnings in that case.
18294 	 */
18295 	if (ill->ill_state_flags & ILL_CONDEMNED) {
18296 		mutex_exit(&ill->ill_lock);
18297 		return (B_FALSE);
18298 	}
18299 	pending = ill->ill_dlpi_pending;
18300 	mutex_exit(&ill->ill_lock);
18301 
18302 	if (pending == DL_PRIM_INVAL) {
18303 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18304 		    "received unsolicited ack for %s on %s\n",
18305 		    dl_primstr(prim), ill->ill_name);
18306 	} else {
18307 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18308 		    "received unexpected ack for %s on %s (expecting %s)\n",
18309 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
18310 	}
18311 	return (B_FALSE);
18312 }
18313 
18314 /*
18315  * Complete the current DLPI operation associated with `prim' on `ill' and
18316  * start the next queued DLPI operation (if any).  If there are no queued DLPI
18317  * operations and the ill's current exclusive IPSQ operation has finished
18318  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
18319  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
18320  * the comments above ipsq_current_finish() for details.
18321  */
18322 void
18323 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18324 {
18325 	mblk_t *mp;
18326 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
18327 
18328 	ASSERT(IAM_WRITER_IPSQ(ipsq));
18329 	mutex_enter(&ill->ill_lock);
18330 
18331 	ASSERT(prim != DL_PRIM_INVAL);
18332 	ASSERT(ill->ill_dlpi_pending == prim);
18333 
18334 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18335 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18336 
18337 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18338 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18339 
18340 		mutex_enter(&ipsq->ipsq_lock);
18341 		if (ipsq->ipsq_current_done)
18342 			ipsq->ipsq_current_ipif = NULL;
18343 		mutex_exit(&ipsq->ipsq_lock);
18344 
18345 		cv_signal(&ill->ill_cv);
18346 		mutex_exit(&ill->ill_lock);
18347 		return;
18348 	}
18349 
18350 	ill->ill_dlpi_deferred = mp->b_next;
18351 	mp->b_next = NULL;
18352 	mutex_exit(&ill->ill_lock);
18353 
18354 	ill_dlpi_dispatch(ill, mp);
18355 }
18356 
18357 void
18358 conn_delete_ire(conn_t *connp, caddr_t arg)
18359 {
18360 	ipif_t	*ipif = (ipif_t *)arg;
18361 	ire_t	*ire;
18362 
18363 	/*
18364 	 * Look at the cached ires on conns which has pointers to ipifs.
18365 	 * We just call ire_refrele which clears up the reference
18366 	 * to ire. Called when a conn closes. Also called from ipif_free
18367 	 * to cleanup indirect references to the stale ipif via the cached ire.
18368 	 */
18369 	mutex_enter(&connp->conn_lock);
18370 	ire = connp->conn_ire_cache;
18371 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18372 		connp->conn_ire_cache = NULL;
18373 		mutex_exit(&connp->conn_lock);
18374 		IRE_REFRELE_NOTR(ire);
18375 		return;
18376 	}
18377 	mutex_exit(&connp->conn_lock);
18378 
18379 }
18380 
18381 /*
18382  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18383  * of IREs. Those IREs may have been previously cached in the conn structure.
18384  * This ipcl_walk() walker function releases all references to such IREs based
18385  * on the condemned flag.
18386  */
18387 /* ARGSUSED */
18388 void
18389 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18390 {
18391 	ire_t	*ire;
18392 
18393 	mutex_enter(&connp->conn_lock);
18394 	ire = connp->conn_ire_cache;
18395 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18396 		connp->conn_ire_cache = NULL;
18397 		mutex_exit(&connp->conn_lock);
18398 		IRE_REFRELE_NOTR(ire);
18399 		return;
18400 	}
18401 	mutex_exit(&connp->conn_lock);
18402 }
18403 
18404 /*
18405  * Take down a specific interface, but don't lose any information about it.
18406  * Also delete interface from its interface group (ifgrp).
18407  * (Always called as writer.)
18408  * This function goes through the down sequence even if the interface is
18409  * already down. There are 2 reasons.
18410  * a. Currently we permit interface routes that depend on down interfaces
18411  *    to be added. This behaviour itself is questionable. However it appears
18412  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18413  *    time. We go thru the cleanup in order to remove these routes.
18414  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18415  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18416  *    down, but we need to cleanup i.e. do ill_dl_down and
18417  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18418  *
18419  * IP-MT notes:
18420  *
18421  * Model of reference to interfaces.
18422  *
18423  * The following members in ipif_t track references to the ipif.
18424  *	int     ipif_refcnt;    Active reference count
18425  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18426  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
18427  *
18428  * The following members in ill_t track references to the ill.
18429  *	int             ill_refcnt;     active refcnt
18430  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18431  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18432  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
18433  *
18434  * Reference to an ipif or ill can be obtained in any of the following ways.
18435  *
18436  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18437  * Pointers to ipif / ill from other data structures viz ire and conn.
18438  * Implicit reference to the ipif / ill by holding a reference to the ire.
18439  *
18440  * The ipif/ill lookup functions return a reference held ipif / ill.
18441  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18442  * This is a purely dynamic reference count associated with threads holding
18443  * references to the ipif / ill. Pointers from other structures do not
18444  * count towards this reference count.
18445  *
18446  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
18447  * associated with the ipif/ill. This is incremented whenever a new
18448  * ire is created referencing the ipif/ill. This is done atomically inside
18449  * ire_add_v[46] where the ire is actually added to the ire hash table.
18450  * The count is decremented in ire_inactive where the ire is destroyed.
18451  *
18452  * nce's reference ill's thru nce_ill and the count of nce's associated with
18453  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18454  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18455  * table. Similarly it is decremented in ndp_inactive() where the nce
18456  * is destroyed.
18457  *
18458  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
18459  * is incremented in ilm_add_v6() and decremented before the ilm is freed
18460  * in ilm_walker_cleanup() or ilm_delete().
18461  *
18462  * Flow of ioctls involving interface down/up
18463  *
18464  * The following is the sequence of an attempt to set some critical flags on an
18465  * up interface.
18466  * ip_sioctl_flags
18467  * ipif_down
18468  * wait for ipif to be quiescent
18469  * ipif_down_tail
18470  * ip_sioctl_flags_tail
18471  *
18472  * All set ioctls that involve down/up sequence would have a skeleton similar
18473  * to the above. All the *tail functions are called after the refcounts have
18474  * dropped to the appropriate values.
18475  *
18476  * The mechanism to quiesce an ipif is as follows.
18477  *
18478  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18479  * on the ipif. Callers either pass a flag requesting wait or the lookup
18480  *  functions will return NULL.
18481  *
18482  * Delete all ires referencing this ipif
18483  *
18484  * Any thread attempting to do an ipif_refhold on an ipif that has been
18485  * obtained thru a cached pointer will first make sure that
18486  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18487  * increment the refcount.
18488  *
18489  * The above guarantees that the ipif refcount will eventually come down to
18490  * zero and the ipif will quiesce, once all threads that currently hold a
18491  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18492  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18493  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
18494  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
18495  * in ip.h
18496  *
18497  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18498  *
18499  * Threads trying to lookup an ipif or ill can pass a flag requesting
18500  * wait and restart if the ipif / ill cannot be looked up currently.
18501  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18502  * failure if the ipif is currently undergoing an exclusive operation, and
18503  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18504  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18505  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18506  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18507  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18508  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18509  * until we release the ipsq_lock, even though the the ill/ipif state flags
18510  * can change after we drop the ill_lock.
18511  *
18512  * An attempt to send out a packet using an ipif that is currently
18513  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18514  * operation and restart it later when the exclusive condition on the ipif ends.
18515  * This is an example of not passing the wait flag to the lookup functions. For
18516  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18517  * out a multicast packet on that ipif will fail while the ipif is
18518  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18519  * currently IPIF_CHANGING will also fail.
18520  */
18521 int
18522 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18523 {
18524 	ill_t		*ill = ipif->ipif_ill;
18525 	phyint_t	*phyi;
18526 	conn_t		*connp;
18527 	boolean_t	success;
18528 	boolean_t	ipif_was_up = B_FALSE;
18529 	ip_stack_t	*ipst = ill->ill_ipst;
18530 
18531 	ASSERT(IAM_WRITER_IPIF(ipif));
18532 
18533 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18534 
18535 	if (ipif->ipif_flags & IPIF_UP) {
18536 		mutex_enter(&ill->ill_lock);
18537 		ipif->ipif_flags &= ~IPIF_UP;
18538 		ASSERT(ill->ill_ipif_up_count > 0);
18539 		--ill->ill_ipif_up_count;
18540 		mutex_exit(&ill->ill_lock);
18541 		ipif_was_up = B_TRUE;
18542 		/* Update status in SCTP's list */
18543 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18544 		ill_nic_event_dispatch(ipif->ipif_ill,
18545 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
18546 	}
18547 
18548 	/*
18549 	 * Blow away memberships we established in ipif_multicast_up().
18550 	 */
18551 	ipif_multicast_down(ipif);
18552 
18553 	/*
18554 	 * Remove from the mapping for __sin6_src_id. We insert only
18555 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18556 	 * stored as mapped addresses, we need to check for mapped
18557 	 * INADDR_ANY also.
18558 	 */
18559 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18560 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18561 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18562 		int err;
18563 
18564 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18565 		    ipif->ipif_zoneid, ipst);
18566 		if (err != 0) {
18567 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18568 		}
18569 	}
18570 
18571 	/*
18572 	 * Before we delete the ill from the group (if any), we need
18573 	 * to make sure that we delete all the routes dependent on
18574 	 * this and also any ipifs dependent on this ipif for
18575 	 * source address. We need to do before we delete from
18576 	 * the group because
18577 	 *
18578 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18579 	 *
18580 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18581 	 *    for re-doing source address selection. Note that
18582 	 *    ipif_select_source[_v6] called from
18583 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18584 	 *    because we have already marked down here i.e cleared
18585 	 *    IPIF_UP.
18586 	 */
18587 	if (ipif->ipif_isv6) {
18588 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18589 		    ipst);
18590 	} else {
18591 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18592 		    ipst);
18593 	}
18594 
18595 	/*
18596 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18597 	 * ires have been deleted above. Otherwise a thread could end up
18598 	 * caching an ire in a conn after we have finished the cleanup of the
18599 	 * conn. The caching is done after making sure that the ire is not yet
18600 	 * condemned. Also documented in the block comment above ip_output
18601 	 */
18602 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18603 	/* Also, delete the ires cached in SCTP */
18604 	sctp_ire_cache_flush(ipif);
18605 
18606 	/*
18607 	 * Update any other ipifs which have used "our" local address as
18608 	 * a source address. This entails removing and recreating IRE_INTERFACE
18609 	 * entries for such ipifs.
18610 	 */
18611 	if (ipif->ipif_isv6)
18612 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18613 	else
18614 		ipif_update_other_ipifs(ipif, ill->ill_group);
18615 
18616 	if (ipif_was_up) {
18617 		/*
18618 		 * Check whether it is last ipif to leave this group.
18619 		 * If this is the last ipif to leave, we should remove
18620 		 * this ill from the group as ipif_select_source will not
18621 		 * be able to find any useful ipifs if this ill is selected
18622 		 * for load balancing.
18623 		 *
18624 		 * For nameless groups, we should call ifgrp_delete if this
18625 		 * belongs to some group. As this ipif is going down, we may
18626 		 * need to reconstruct groups.
18627 		 */
18628 		phyi = ill->ill_phyint;
18629 		/*
18630 		 * If the phyint_groupname_len is 0, it may or may not
18631 		 * be in the nameless group. If the phyint_groupname_len is
18632 		 * not 0, then this ill should be part of some group.
18633 		 * As we always insert this ill in the group if
18634 		 * phyint_groupname_len is not zero when the first ipif
18635 		 * comes up (in ipif_up_done), it should be in a group
18636 		 * when the namelen is not 0.
18637 		 *
18638 		 * NOTE : When we delete the ill from the group,it will
18639 		 * blow away all the IRE_CACHES pointing either at this ipif or
18640 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18641 		 * should be pointing at this ill.
18642 		 */
18643 		ASSERT(phyi->phyint_groupname_len == 0 ||
18644 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18645 
18646 		if (phyi->phyint_groupname_len != 0) {
18647 			if (ill->ill_ipif_up_count == 0)
18648 				illgrp_delete(ill);
18649 		}
18650 
18651 		/*
18652 		 * If we have deleted some of the broadcast ires associated
18653 		 * with this ipif, we need to re-nominate somebody else if
18654 		 * the ires that we deleted were the nominated ones.
18655 		 */
18656 		if (ill->ill_group != NULL && !ill->ill_isv6)
18657 			ipif_renominate_bcast(ipif);
18658 	}
18659 
18660 	/*
18661 	 * neighbor-discovery or arp entries for this interface.
18662 	 */
18663 	ipif_ndp_down(ipif);
18664 
18665 	/*
18666 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18667 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18668 	 * and ill_delete -> ipif_free -> ipif_down
18669 	 */
18670 	if (mp == NULL) {
18671 		ASSERT(q == NULL);
18672 		return (0);
18673 	}
18674 
18675 	if (CONN_Q(q)) {
18676 		connp = Q_TO_CONN(q);
18677 		mutex_enter(&connp->conn_lock);
18678 	} else {
18679 		connp = NULL;
18680 	}
18681 	mutex_enter(&ill->ill_lock);
18682 	/*
18683 	 * Are there any ire's pointing to this ipif that are still active ?
18684 	 * If this is the last ipif going down, are there any ire's pointing
18685 	 * to this ill that are still active ?
18686 	 */
18687 	if (ipif_is_quiescent(ipif)) {
18688 		mutex_exit(&ill->ill_lock);
18689 		if (connp != NULL)
18690 			mutex_exit(&connp->conn_lock);
18691 		return (0);
18692 	}
18693 
18694 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18695 	    ill->ill_name, (void *)ill));
18696 	/*
18697 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18698 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18699 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18700 	 */
18701 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18702 	if (!success) {
18703 		/* The conn is closing. So just return */
18704 		ASSERT(connp != NULL);
18705 		mutex_exit(&ill->ill_lock);
18706 		mutex_exit(&connp->conn_lock);
18707 		return (EINTR);
18708 	}
18709 
18710 	mutex_exit(&ill->ill_lock);
18711 	if (connp != NULL)
18712 		mutex_exit(&connp->conn_lock);
18713 	return (EINPROGRESS);
18714 }
18715 
18716 void
18717 ipif_down_tail(ipif_t *ipif)
18718 {
18719 	ill_t	*ill = ipif->ipif_ill;
18720 
18721 	/*
18722 	 * Skip any loopback interface (null wq).
18723 	 * If this is the last logical interface on the ill
18724 	 * have ill_dl_down tell the driver we are gone (unbind)
18725 	 * Note that lun 0 can ipif_down even though
18726 	 * there are other logical units that are up.
18727 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18728 	 */
18729 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18730 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18731 	    ill->ill_dl_up) {
18732 		ill_dl_down(ill);
18733 	}
18734 	ill->ill_logical_down = 0;
18735 
18736 	/*
18737 	 * Have to be after removing the routes in ipif_down_delete_ire.
18738 	 */
18739 	if (ipif->ipif_isv6) {
18740 		if (ill->ill_flags & ILLF_XRESOLV)
18741 			ipif_arp_down(ipif);
18742 	} else {
18743 		ipif_arp_down(ipif);
18744 	}
18745 
18746 	ip_rts_ifmsg(ipif);
18747 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18748 }
18749 
18750 /*
18751  * Bring interface logically down without bringing the physical interface
18752  * down e.g. when the netmask is changed. This avoids long lasting link
18753  * negotiations between an ethernet interface and a certain switches.
18754  */
18755 static int
18756 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18757 {
18758 	/*
18759 	 * The ill_logical_down flag is a transient flag. It is set here
18760 	 * and is cleared once the down has completed in ipif_down_tail.
18761 	 * This flag does not indicate whether the ill stream is in the
18762 	 * DL_BOUND state with the driver. Instead this flag is used by
18763 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18764 	 * the driver. The state of the ill stream i.e. whether it is
18765 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18766 	 */
18767 	ipif->ipif_ill->ill_logical_down = 1;
18768 	return (ipif_down(ipif, q, mp));
18769 }
18770 
18771 /*
18772  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18773  * If the usesrc client ILL is already part of a usesrc group or not,
18774  * in either case a ire_stq with the matching usesrc client ILL will
18775  * locate the IRE's that need to be deleted. We want IREs to be created
18776  * with the new source address.
18777  */
18778 static void
18779 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18780 {
18781 	ill_t	*ucill = (ill_t *)ill_arg;
18782 
18783 	ASSERT(IAM_WRITER_ILL(ucill));
18784 
18785 	if (ire->ire_stq == NULL)
18786 		return;
18787 
18788 	if ((ire->ire_type == IRE_CACHE) &&
18789 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18790 		ire_delete(ire);
18791 }
18792 
18793 /*
18794  * ire_walk routine to delete every IRE dependent on the interface
18795  * address that is going down.	(Always called as writer.)
18796  * Works for both v4 and v6.
18797  * In addition for checking for ire_ipif matches it also checks for
18798  * IRE_CACHE entries which have the same source address as the
18799  * disappearing ipif since ipif_select_source might have picked
18800  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18801  * care of any IRE_INTERFACE with the disappearing source address.
18802  */
18803 static void
18804 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18805 {
18806 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18807 	ill_t *ire_ill;
18808 	ill_t *ipif_ill;
18809 
18810 	ASSERT(IAM_WRITER_IPIF(ipif));
18811 	if (ire->ire_ipif == NULL)
18812 		return;
18813 
18814 	/*
18815 	 * For IPv4, we derive source addresses for an IRE from ipif's
18816 	 * belonging to the same IPMP group as the IRE's outgoing
18817 	 * interface.  If an IRE's outgoing interface isn't in the
18818 	 * same IPMP group as a particular ipif, then that ipif
18819 	 * couldn't have been used as a source address for this IRE.
18820 	 *
18821 	 * For IPv6, source addresses are only restricted to the IPMP group
18822 	 * if the IRE is for a link-local address or a multicast address.
18823 	 * Otherwise, source addresses for an IRE can be chosen from
18824 	 * interfaces other than the the outgoing interface for that IRE.
18825 	 *
18826 	 * For source address selection details, see ipif_select_source()
18827 	 * and ipif_select_source_v6().
18828 	 */
18829 	if (ire->ire_ipversion == IPV4_VERSION ||
18830 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18831 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18832 		ire_ill = ire->ire_ipif->ipif_ill;
18833 		ipif_ill = ipif->ipif_ill;
18834 
18835 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18836 			return;
18837 		}
18838 	}
18839 
18840 	if (ire->ire_ipif != ipif) {
18841 		/*
18842 		 * Look for a matching source address.
18843 		 */
18844 		if (ire->ire_type != IRE_CACHE)
18845 			return;
18846 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18847 			return;
18848 
18849 		if (ire->ire_ipversion == IPV4_VERSION) {
18850 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18851 				return;
18852 		} else {
18853 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18854 			    &ipif->ipif_v6lcl_addr))
18855 				return;
18856 		}
18857 		ire_delete(ire);
18858 		return;
18859 	}
18860 	/*
18861 	 * ire_delete() will do an ire_flush_cache which will delete
18862 	 * all ire_ipif matches
18863 	 */
18864 	ire_delete(ire);
18865 }
18866 
18867 /*
18868  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18869  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18870  * 2) when an interface is brought up or down (on that ill).
18871  * This ensures that the IRE_CACHE entries don't retain stale source
18872  * address selection results.
18873  */
18874 void
18875 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18876 {
18877 	ill_t	*ill = (ill_t *)ill_arg;
18878 	ill_t	*ipif_ill;
18879 
18880 	ASSERT(IAM_WRITER_ILL(ill));
18881 	/*
18882 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18883 	 * Hence this should be IRE_CACHE.
18884 	 */
18885 	ASSERT(ire->ire_type == IRE_CACHE);
18886 
18887 	/*
18888 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18889 	 * We are only interested in IRE_CACHES that has borrowed
18890 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18891 	 * for which we need to look at ire_ipif->ipif_ill match
18892 	 * with ill.
18893 	 */
18894 	ASSERT(ire->ire_ipif != NULL);
18895 	ipif_ill = ire->ire_ipif->ipif_ill;
18896 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18897 	    ipif_ill->ill_group == ill->ill_group)) {
18898 		ire_delete(ire);
18899 	}
18900 }
18901 
18902 /*
18903  * Delete all the ire whose stq references ill_arg.
18904  */
18905 static void
18906 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18907 {
18908 	ill_t	*ill = (ill_t *)ill_arg;
18909 	ill_t	*ire_ill;
18910 
18911 	ASSERT(IAM_WRITER_ILL(ill));
18912 	/*
18913 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18914 	 * Hence this should be IRE_CACHE.
18915 	 */
18916 	ASSERT(ire->ire_type == IRE_CACHE);
18917 
18918 	/*
18919 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18920 	 * matches ill. We are only interested in IRE_CACHES that
18921 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
18922 	 * filtering here.
18923 	 */
18924 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
18925 
18926 	if (ire_ill == ill)
18927 		ire_delete(ire);
18928 }
18929 
18930 /*
18931  * This is called when an ill leaves the group. We want to delete
18932  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
18933  * pointing at ill.
18934  */
18935 static void
18936 illgrp_cache_delete(ire_t *ire, char *ill_arg)
18937 {
18938 	ill_t	*ill = (ill_t *)ill_arg;
18939 
18940 	ASSERT(IAM_WRITER_ILL(ill));
18941 	ASSERT(ill->ill_group == NULL);
18942 	/*
18943 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18944 	 * Hence this should be IRE_CACHE.
18945 	 */
18946 	ASSERT(ire->ire_type == IRE_CACHE);
18947 	/*
18948 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18949 	 * matches ill. We are interested in both.
18950 	 */
18951 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
18952 	    (ire->ire_ipif->ipif_ill == ill));
18953 
18954 	ire_delete(ire);
18955 }
18956 
18957 /*
18958  * Initiate deallocate of an IPIF. Always called as writer. Called by
18959  * ill_delete or ip_sioctl_removeif.
18960  */
18961 static void
18962 ipif_free(ipif_t *ipif)
18963 {
18964 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
18965 
18966 	ASSERT(IAM_WRITER_IPIF(ipif));
18967 
18968 	if (ipif->ipif_recovery_id != 0)
18969 		(void) untimeout(ipif->ipif_recovery_id);
18970 	ipif->ipif_recovery_id = 0;
18971 
18972 	/* Remove conn references */
18973 	reset_conn_ipif(ipif);
18974 
18975 	/*
18976 	 * Make sure we have valid net and subnet broadcast ire's for the
18977 	 * other ipif's which share them with this ipif.
18978 	 */
18979 	if (!ipif->ipif_isv6)
18980 		ipif_check_bcast_ires(ipif);
18981 
18982 	/*
18983 	 * Take down the interface. We can be called either from ill_delete
18984 	 * or from ip_sioctl_removeif.
18985 	 */
18986 	(void) ipif_down(ipif, NULL, NULL);
18987 
18988 	/*
18989 	 * Now that the interface is down, there's no chance it can still
18990 	 * become a duplicate.  Cancel any timer that may have been set while
18991 	 * tearing down.
18992 	 */
18993 	if (ipif->ipif_recovery_id != 0)
18994 		(void) untimeout(ipif->ipif_recovery_id);
18995 	ipif->ipif_recovery_id = 0;
18996 
18997 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
18998 	/* Remove pointers to this ill in the multicast routing tables */
18999 	reset_mrt_vif_ipif(ipif);
19000 	rw_exit(&ipst->ips_ill_g_lock);
19001 }
19002 
19003 /*
19004  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19005  * also ill_move().
19006  */
19007 static void
19008 ipif_free_tail(ipif_t *ipif)
19009 {
19010 	mblk_t	*mp;
19011 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19012 
19013 	/*
19014 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19015 	 */
19016 	mutex_enter(&ipif->ipif_saved_ire_lock);
19017 	mp = ipif->ipif_saved_ire_mp;
19018 	ipif->ipif_saved_ire_mp = NULL;
19019 	mutex_exit(&ipif->ipif_saved_ire_lock);
19020 	freemsg(mp);
19021 
19022 	/*
19023 	 * Need to hold both ill_g_lock and ill_lock while
19024 	 * inserting or removing an ipif from the linked list
19025 	 * of ipifs hanging off the ill.
19026 	 */
19027 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19028 
19029 	ASSERT(ilm_walk_ipif(ipif) == 0);
19030 
19031 #ifdef DEBUG
19032 	ipif_trace_cleanup(ipif);
19033 #endif
19034 
19035 	/* Ask SCTP to take it out of it list */
19036 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19037 
19038 	/* Get it out of the ILL interface list. */
19039 	ipif_remove(ipif, B_TRUE);
19040 	rw_exit(&ipst->ips_ill_g_lock);
19041 
19042 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19043 
19044 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19045 	ASSERT(ipif->ipif_recovery_id == 0);
19046 
19047 	/* Free the memory. */
19048 	mi_free(ipif);
19049 }
19050 
19051 /*
19052  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19053  * is zero.
19054  */
19055 void
19056 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19057 {
19058 	char	lbuf[LIFNAMSIZ];
19059 	char	*name;
19060 	size_t	name_len;
19061 
19062 	buf[0] = '\0';
19063 	name = ipif->ipif_ill->ill_name;
19064 	name_len = ipif->ipif_ill->ill_name_length;
19065 	if (ipif->ipif_id != 0) {
19066 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19067 		    ipif->ipif_id);
19068 		name = lbuf;
19069 		name_len = mi_strlen(name) + 1;
19070 	}
19071 	len -= 1;
19072 	buf[len] = '\0';
19073 	len = MIN(len, name_len);
19074 	bcopy(name, buf, len);
19075 }
19076 
19077 /*
19078  * Find an IPIF based on the name passed in.  Names can be of the
19079  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19080  * The <phys> string can have forms like <dev><#> (e.g., le0),
19081  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19082  * When there is no colon, the implied unit id is zero. <phys> must
19083  * correspond to the name of an ILL.  (May be called as writer.)
19084  */
19085 static ipif_t *
19086 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19087     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19088     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19089 {
19090 	char	*cp;
19091 	char	*endp;
19092 	long	id;
19093 	ill_t	*ill;
19094 	ipif_t	*ipif;
19095 	uint_t	ire_type;
19096 	boolean_t did_alloc = B_FALSE;
19097 	ipsq_t	*ipsq;
19098 
19099 	if (error != NULL)
19100 		*error = 0;
19101 
19102 	/*
19103 	 * If the caller wants to us to create the ipif, make sure we have a
19104 	 * valid zoneid
19105 	 */
19106 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19107 
19108 	if (namelen == 0) {
19109 		if (error != NULL)
19110 			*error = ENXIO;
19111 		return (NULL);
19112 	}
19113 
19114 	*exists = B_FALSE;
19115 	/* Look for a colon in the name. */
19116 	endp = &name[namelen];
19117 	for (cp = endp; --cp > name; ) {
19118 		if (*cp == IPIF_SEPARATOR_CHAR)
19119 			break;
19120 	}
19121 
19122 	if (*cp == IPIF_SEPARATOR_CHAR) {
19123 		/*
19124 		 * Reject any non-decimal aliases for logical
19125 		 * interfaces. Aliases with leading zeroes
19126 		 * are also rejected as they introduce ambiguity
19127 		 * in the naming of the interfaces.
19128 		 * In order to confirm with existing semantics,
19129 		 * and to not break any programs/script relying
19130 		 * on that behaviour, if<0>:0 is considered to be
19131 		 * a valid interface.
19132 		 *
19133 		 * If alias has two or more digits and the first
19134 		 * is zero, fail.
19135 		 */
19136 		if (&cp[2] < endp && cp[1] == '0') {
19137 			if (error != NULL)
19138 				*error = EINVAL;
19139 			return (NULL);
19140 		}
19141 	}
19142 
19143 	if (cp <= name) {
19144 		cp = endp;
19145 	} else {
19146 		*cp = '\0';
19147 	}
19148 
19149 	/*
19150 	 * Look up the ILL, based on the portion of the name
19151 	 * before the slash. ill_lookup_on_name returns a held ill.
19152 	 * Temporary to check whether ill exists already. If so
19153 	 * ill_lookup_on_name will clear it.
19154 	 */
19155 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19156 	    q, mp, func, error, &did_alloc, ipst);
19157 	if (cp != endp)
19158 		*cp = IPIF_SEPARATOR_CHAR;
19159 	if (ill == NULL)
19160 		return (NULL);
19161 
19162 	/* Establish the unit number in the name. */
19163 	id = 0;
19164 	if (cp < endp && *endp == '\0') {
19165 		/* If there was a colon, the unit number follows. */
19166 		cp++;
19167 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19168 			ill_refrele(ill);
19169 			if (error != NULL)
19170 				*error = ENXIO;
19171 			return (NULL);
19172 		}
19173 	}
19174 
19175 	GRAB_CONN_LOCK(q);
19176 	mutex_enter(&ill->ill_lock);
19177 	/* Now see if there is an IPIF with this unit number. */
19178 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19179 		if (ipif->ipif_id == id) {
19180 			if (zoneid != ALL_ZONES &&
19181 			    zoneid != ipif->ipif_zoneid &&
19182 			    ipif->ipif_zoneid != ALL_ZONES) {
19183 				mutex_exit(&ill->ill_lock);
19184 				RELEASE_CONN_LOCK(q);
19185 				ill_refrele(ill);
19186 				if (error != NULL)
19187 					*error = ENXIO;
19188 				return (NULL);
19189 			}
19190 			/*
19191 			 * The block comment at the start of ipif_down
19192 			 * explains the use of the macros used below
19193 			 */
19194 			if (IPIF_CAN_LOOKUP(ipif)) {
19195 				ipif_refhold_locked(ipif);
19196 				mutex_exit(&ill->ill_lock);
19197 				if (!did_alloc)
19198 					*exists = B_TRUE;
19199 				/*
19200 				 * Drop locks before calling ill_refrele
19201 				 * since it can potentially call into
19202 				 * ipif_ill_refrele_tail which can end up
19203 				 * in trying to acquire any lock.
19204 				 */
19205 				RELEASE_CONN_LOCK(q);
19206 				ill_refrele(ill);
19207 				return (ipif);
19208 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19209 				ipsq = ill->ill_phyint->phyint_ipsq;
19210 				mutex_enter(&ipsq->ipsq_lock);
19211 				mutex_exit(&ill->ill_lock);
19212 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19213 				mutex_exit(&ipsq->ipsq_lock);
19214 				RELEASE_CONN_LOCK(q);
19215 				ill_refrele(ill);
19216 				if (error != NULL)
19217 					*error = EINPROGRESS;
19218 				return (NULL);
19219 			}
19220 		}
19221 	}
19222 	RELEASE_CONN_LOCK(q);
19223 
19224 	if (!do_alloc) {
19225 		mutex_exit(&ill->ill_lock);
19226 		ill_refrele(ill);
19227 		if (error != NULL)
19228 			*error = ENXIO;
19229 		return (NULL);
19230 	}
19231 
19232 	/*
19233 	 * If none found, atomically allocate and return a new one.
19234 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19235 	 * to support "receive only" use of lo0:1 etc. as is still done
19236 	 * below as an initial guess.
19237 	 * However, this is now likely to be overriden later in ipif_up_done()
19238 	 * when we know for sure what address has been configured on the
19239 	 * interface, since we might have more than one loopback interface
19240 	 * with a loopback address, e.g. in the case of zones, and all the
19241 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19242 	 */
19243 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19244 		ire_type = IRE_LOOPBACK;
19245 	else
19246 		ire_type = IRE_LOCAL;
19247 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19248 	if (ipif != NULL)
19249 		ipif_refhold_locked(ipif);
19250 	else if (error != NULL)
19251 		*error = ENOMEM;
19252 	mutex_exit(&ill->ill_lock);
19253 	ill_refrele(ill);
19254 	return (ipif);
19255 }
19256 
19257 /*
19258  * This routine is called whenever a new address comes up on an ipif.  If
19259  * we are configured to respond to address mask requests, then we are supposed
19260  * to broadcast an address mask reply at this time.  This routine is also
19261  * called if we are already up, but a netmask change is made.  This is legal
19262  * but might not make the system manager very popular.	(May be called
19263  * as writer.)
19264  */
19265 void
19266 ipif_mask_reply(ipif_t *ipif)
19267 {
19268 	icmph_t	*icmph;
19269 	ipha_t	*ipha;
19270 	mblk_t	*mp;
19271 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19272 
19273 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19274 
19275 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19276 		return;
19277 
19278 	/* ICMP mask reply is IPv4 only */
19279 	ASSERT(!ipif->ipif_isv6);
19280 	/* ICMP mask reply is not for a loopback interface */
19281 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19282 
19283 	mp = allocb(REPLY_LEN, BPRI_HI);
19284 	if (mp == NULL)
19285 		return;
19286 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19287 
19288 	ipha = (ipha_t *)mp->b_rptr;
19289 	bzero(ipha, REPLY_LEN);
19290 	*ipha = icmp_ipha;
19291 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19292 	ipha->ipha_src = ipif->ipif_src_addr;
19293 	ipha->ipha_dst = ipif->ipif_brd_addr;
19294 	ipha->ipha_length = htons(REPLY_LEN);
19295 	ipha->ipha_ident = 0;
19296 
19297 	icmph = (icmph_t *)&ipha[1];
19298 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19299 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19300 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19301 
19302 	put(ipif->ipif_wq, mp);
19303 
19304 #undef	REPLY_LEN
19305 }
19306 
19307 /*
19308  * When the mtu in the ipif changes, we call this routine through ire_walk
19309  * to update all the relevant IREs.
19310  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19311  */
19312 static void
19313 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19314 {
19315 	ipif_t *ipif = (ipif_t *)ipif_arg;
19316 
19317 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19318 		return;
19319 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19320 }
19321 
19322 /*
19323  * When the mtu in the ill changes, we call this routine through ire_walk
19324  * to update all the relevant IREs.
19325  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19326  */
19327 void
19328 ill_mtu_change(ire_t *ire, char *ill_arg)
19329 {
19330 	ill_t	*ill = (ill_t *)ill_arg;
19331 
19332 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19333 		return;
19334 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19335 }
19336 
19337 /*
19338  * Join the ipif specific multicast groups.
19339  * Must be called after a mapping has been set up in the resolver.  (Always
19340  * called as writer.)
19341  */
19342 void
19343 ipif_multicast_up(ipif_t *ipif)
19344 {
19345 	int err, index;
19346 	ill_t *ill;
19347 
19348 	ASSERT(IAM_WRITER_IPIF(ipif));
19349 
19350 	ill = ipif->ipif_ill;
19351 	index = ill->ill_phyint->phyint_ifindex;
19352 
19353 	ip1dbg(("ipif_multicast_up\n"));
19354 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19355 		return;
19356 
19357 	if (ipif->ipif_isv6) {
19358 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19359 			return;
19360 
19361 		/* Join the all hosts multicast address */
19362 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19363 		/*
19364 		 * Passing B_TRUE means we have to join the multicast
19365 		 * membership on this interface even though this is
19366 		 * FAILED. If we join on a different one in the group,
19367 		 * we will not be able to delete the membership later
19368 		 * as we currently don't track where we join when we
19369 		 * join within the kernel unlike applications where
19370 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19371 		 * for more on this.
19372 		 */
19373 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19374 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19375 		if (err != 0) {
19376 			ip0dbg(("ipif_multicast_up: "
19377 			    "all_hosts_mcast failed %d\n",
19378 			    err));
19379 			return;
19380 		}
19381 		/*
19382 		 * Enable multicast for the solicited node multicast address
19383 		 */
19384 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19385 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19386 
19387 			ipv6_multi.s6_addr32[3] |=
19388 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19389 
19390 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19391 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19392 			    NULL);
19393 			if (err != 0) {
19394 				ip0dbg(("ipif_multicast_up: solicited MC"
19395 				    " failed %d\n", err));
19396 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19397 				    ill, ill->ill_phyint->phyint_ifindex,
19398 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19399 				return;
19400 			}
19401 		}
19402 	} else {
19403 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19404 			return;
19405 
19406 		/* Join the all hosts multicast address */
19407 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19408 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19409 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19410 		if (err) {
19411 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19412 			return;
19413 		}
19414 	}
19415 	ipif->ipif_multicast_up = 1;
19416 }
19417 
19418 /*
19419  * Blow away any multicast groups that we joined in ipif_multicast_up().
19420  * (Explicit memberships are blown away in ill_leave_multicast() when the
19421  * ill is brought down.)
19422  */
19423 static void
19424 ipif_multicast_down(ipif_t *ipif)
19425 {
19426 	int err;
19427 
19428 	ASSERT(IAM_WRITER_IPIF(ipif));
19429 
19430 	ip1dbg(("ipif_multicast_down\n"));
19431 	if (!ipif->ipif_multicast_up)
19432 		return;
19433 
19434 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19435 
19436 	if (!ipif->ipif_isv6) {
19437 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19438 		    B_TRUE);
19439 		if (err != 0)
19440 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19441 
19442 		ipif->ipif_multicast_up = 0;
19443 		return;
19444 	}
19445 
19446 	/*
19447 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19448 	 * we should look for ilms on this ill rather than the ones that have
19449 	 * been failed over here.  They are here temporarily. As
19450 	 * ipif_multicast_up has joined on this ill, we should delete only
19451 	 * from this ill.
19452 	 */
19453 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19454 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19455 	    B_TRUE, B_TRUE);
19456 	if (err != 0) {
19457 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19458 		    err));
19459 	}
19460 	/*
19461 	 * Disable multicast for the solicited node multicast address
19462 	 */
19463 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19464 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19465 
19466 		ipv6_multi.s6_addr32[3] |=
19467 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19468 
19469 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19470 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19471 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19472 
19473 		if (err != 0) {
19474 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19475 			    err));
19476 		}
19477 	}
19478 
19479 	ipif->ipif_multicast_up = 0;
19480 }
19481 
19482 /*
19483  * Used when an interface comes up to recreate any extra routes on this
19484  * interface.
19485  */
19486 static ire_t **
19487 ipif_recover_ire(ipif_t *ipif)
19488 {
19489 	mblk_t	*mp;
19490 	ire_t	**ipif_saved_irep;
19491 	ire_t	**irep;
19492 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19493 
19494 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19495 	    ipif->ipif_id));
19496 
19497 	mutex_enter(&ipif->ipif_saved_ire_lock);
19498 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19499 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19500 	if (ipif_saved_irep == NULL) {
19501 		mutex_exit(&ipif->ipif_saved_ire_lock);
19502 		return (NULL);
19503 	}
19504 
19505 	irep = ipif_saved_irep;
19506 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19507 		ire_t		*ire;
19508 		queue_t		*rfq;
19509 		queue_t		*stq;
19510 		ifrt_t		*ifrt;
19511 		uchar_t		*src_addr;
19512 		uchar_t		*gateway_addr;
19513 		ushort_t	type;
19514 
19515 		/*
19516 		 * When the ire was initially created and then added in
19517 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19518 		 * in the case of a traditional interface route, or as one of
19519 		 * the IRE_OFFSUBNET types (with the exception of
19520 		 * IRE_HOST types ire which is created by icmp_redirect() and
19521 		 * which we don't need to save or recover).  In the case where
19522 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19523 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19524 		 * to satisfy software like GateD and Sun Cluster which creates
19525 		 * routes using the the loopback interface's address as a
19526 		 * gateway.
19527 		 *
19528 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19529 		 * ire_create() will be called in the same way here as
19530 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19531 		 * the route looks like a traditional interface route (where
19532 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19533 		 * the saved ifrt->ifrt_type.  This means that in the case where
19534 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19535 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19536 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19537 		 */
19538 		ifrt = (ifrt_t *)mp->b_rptr;
19539 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19540 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19541 			rfq = NULL;
19542 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19543 			    ? ipif->ipif_rq : ipif->ipif_wq;
19544 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19545 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19546 			    : (uint8_t *)&ipif->ipif_src_addr;
19547 			gateway_addr = NULL;
19548 			type = ipif->ipif_net_type;
19549 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19550 			/* Recover multiroute broadcast IRE. */
19551 			rfq = ipif->ipif_rq;
19552 			stq = ipif->ipif_wq;
19553 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19554 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19555 			    : (uint8_t *)&ipif->ipif_src_addr;
19556 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19557 			type = ifrt->ifrt_type;
19558 		} else {
19559 			rfq = NULL;
19560 			stq = NULL;
19561 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19562 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19563 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19564 			type = ifrt->ifrt_type;
19565 		}
19566 
19567 		/*
19568 		 * Create a copy of the IRE with the saved address and netmask.
19569 		 */
19570 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19571 		    "0x%x/0x%x\n",
19572 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19573 		    ntohl(ifrt->ifrt_addr),
19574 		    ntohl(ifrt->ifrt_mask)));
19575 		ire = ire_create(
19576 		    (uint8_t *)&ifrt->ifrt_addr,
19577 		    (uint8_t *)&ifrt->ifrt_mask,
19578 		    src_addr,
19579 		    gateway_addr,
19580 		    &ifrt->ifrt_max_frag,
19581 		    NULL,
19582 		    rfq,
19583 		    stq,
19584 		    type,
19585 		    ipif,
19586 		    0,
19587 		    0,
19588 		    0,
19589 		    ifrt->ifrt_flags,
19590 		    &ifrt->ifrt_iulp_info,
19591 		    NULL,
19592 		    NULL,
19593 		    ipst);
19594 
19595 		if (ire == NULL) {
19596 			mutex_exit(&ipif->ipif_saved_ire_lock);
19597 			kmem_free(ipif_saved_irep,
19598 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19599 			return (NULL);
19600 		}
19601 
19602 		/*
19603 		 * Some software (for example, GateD and Sun Cluster) attempts
19604 		 * to create (what amount to) IRE_PREFIX routes with the
19605 		 * loopback address as the gateway.  This is primarily done to
19606 		 * set up prefixes with the RTF_REJECT flag set (for example,
19607 		 * when generating aggregate routes.)
19608 		 *
19609 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19610 		 * IRE_LOOPBACK, then we map the request into a
19611 		 * IRE_IF_NORESOLVER.
19612 		 */
19613 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19614 			ire->ire_type = IRE_IF_NORESOLVER;
19615 		/*
19616 		 * ire held by ire_add, will be refreled' towards the
19617 		 * the end of ipif_up_done
19618 		 */
19619 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19620 		*irep = ire;
19621 		irep++;
19622 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19623 	}
19624 	mutex_exit(&ipif->ipif_saved_ire_lock);
19625 	return (ipif_saved_irep);
19626 }
19627 
19628 /*
19629  * Used to set the netmask and broadcast address to default values when the
19630  * interface is brought up.  (Always called as writer.)
19631  */
19632 static void
19633 ipif_set_default(ipif_t *ipif)
19634 {
19635 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19636 
19637 	if (!ipif->ipif_isv6) {
19638 		/*
19639 		 * Interface holds an IPv4 address. Default
19640 		 * mask is the natural netmask.
19641 		 */
19642 		if (!ipif->ipif_net_mask) {
19643 			ipaddr_t	v4mask;
19644 
19645 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19646 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19647 		}
19648 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19649 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19650 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19651 		} else {
19652 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19653 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19654 		}
19655 		/*
19656 		 * NOTE: SunOS 4.X does this even if the broadcast address
19657 		 * has been already set thus we do the same here.
19658 		 */
19659 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19660 			ipaddr_t	v4addr;
19661 
19662 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19663 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19664 		}
19665 	} else {
19666 		/*
19667 		 * Interface holds an IPv6-only address.  Default
19668 		 * mask is all-ones.
19669 		 */
19670 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19671 			ipif->ipif_v6net_mask = ipv6_all_ones;
19672 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19673 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19674 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19675 		} else {
19676 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19677 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19678 		}
19679 	}
19680 }
19681 
19682 /*
19683  * Return 0 if this address can be used as local address without causing
19684  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19685  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19686  * Special checks are needed to allow the same IPv6 link-local address
19687  * on different ills.
19688  * TODO: allowing the same site-local address on different ill's.
19689  */
19690 int
19691 ip_addr_availability_check(ipif_t *new_ipif)
19692 {
19693 	in6_addr_t our_v6addr;
19694 	ill_t *ill;
19695 	ipif_t *ipif;
19696 	ill_walk_context_t ctx;
19697 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19698 
19699 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19700 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19701 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19702 
19703 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19704 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19705 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19706 		return (0);
19707 
19708 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19709 
19710 	if (new_ipif->ipif_isv6)
19711 		ill = ILL_START_WALK_V6(&ctx, ipst);
19712 	else
19713 		ill = ILL_START_WALK_V4(&ctx, ipst);
19714 
19715 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19716 		for (ipif = ill->ill_ipif; ipif != NULL;
19717 		    ipif = ipif->ipif_next) {
19718 			if ((ipif == new_ipif) ||
19719 			    !(ipif->ipif_flags & IPIF_UP) ||
19720 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19721 				continue;
19722 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19723 			    &our_v6addr)) {
19724 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19725 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19726 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19727 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19728 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19729 				    new_ipif->ipif_ill != ill)
19730 					continue;
19731 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19732 				    new_ipif->ipif_ill != ill)
19733 					continue;
19734 				else if (new_ipif->ipif_zoneid !=
19735 				    ipif->ipif_zoneid &&
19736 				    ipif->ipif_zoneid != ALL_ZONES &&
19737 				    IS_LOOPBACK(ill))
19738 					continue;
19739 				else if (new_ipif->ipif_ill == ill)
19740 					return (EADDRINUSE);
19741 				else
19742 					return (EADDRNOTAVAIL);
19743 			}
19744 		}
19745 	}
19746 
19747 	return (0);
19748 }
19749 
19750 /*
19751  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19752  * IREs for the ipif.
19753  * When the routine returns EINPROGRESS then mp has been consumed and
19754  * the ioctl will be acked from ip_rput_dlpi.
19755  */
19756 static int
19757 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19758 {
19759 	ill_t	*ill = ipif->ipif_ill;
19760 	boolean_t isv6 = ipif->ipif_isv6;
19761 	int	err = 0;
19762 	boolean_t success;
19763 
19764 	ASSERT(IAM_WRITER_IPIF(ipif));
19765 
19766 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19767 
19768 	/* Shouldn't get here if it is already up. */
19769 	if (ipif->ipif_flags & IPIF_UP)
19770 		return (EALREADY);
19771 
19772 	/* Skip arp/ndp for any loopback interface. */
19773 	if (ill->ill_wq != NULL) {
19774 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19775 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19776 
19777 		if (!ill->ill_dl_up) {
19778 			/*
19779 			 * ill_dl_up is not yet set. i.e. we are yet to
19780 			 * DL_BIND with the driver and this is the first
19781 			 * logical interface on the ill to become "up".
19782 			 * Tell the driver to get going (via DL_BIND_REQ).
19783 			 * Note that changing "significant" IFF_ flags
19784 			 * address/netmask etc cause a down/up dance, but
19785 			 * does not cause an unbind (DL_UNBIND) with the driver
19786 			 */
19787 			return (ill_dl_up(ill, ipif, mp, q));
19788 		}
19789 
19790 		/*
19791 		 * ipif_resolver_up may end up sending an
19792 		 * AR_INTERFACE_UP message to ARP, which would, in
19793 		 * turn send a DLPI message to the driver. ioctls are
19794 		 * serialized and so we cannot send more than one
19795 		 * interface up message at a time. If ipif_resolver_up
19796 		 * does send an interface up message to ARP, we get
19797 		 * EINPROGRESS and we will complete in ip_arp_done.
19798 		 */
19799 
19800 		ASSERT(connp != NULL || !CONN_Q(q));
19801 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19802 		if (connp != NULL)
19803 			mutex_enter(&connp->conn_lock);
19804 		mutex_enter(&ill->ill_lock);
19805 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19806 		mutex_exit(&ill->ill_lock);
19807 		if (connp != NULL)
19808 			mutex_exit(&connp->conn_lock);
19809 		if (!success)
19810 			return (EINTR);
19811 
19812 		/*
19813 		 * Crank up IPv6 neighbor discovery
19814 		 * Unlike ARP, this should complete when
19815 		 * ipif_ndp_up returns. However, for
19816 		 * ILLF_XRESOLV interfaces we also send a
19817 		 * AR_INTERFACE_UP to the external resolver.
19818 		 * That ioctl will complete in ip_rput.
19819 		 */
19820 		if (isv6) {
19821 			err = ipif_ndp_up(ipif);
19822 			if (err != 0) {
19823 				if (err != EINPROGRESS)
19824 					mp = ipsq_pending_mp_get(ipsq, &connp);
19825 				return (err);
19826 			}
19827 		}
19828 		/* Now, ARP */
19829 		err = ipif_resolver_up(ipif, Res_act_initial);
19830 		if (err == EINPROGRESS) {
19831 			/* We will complete it in ip_arp_done */
19832 			return (err);
19833 		}
19834 		mp = ipsq_pending_mp_get(ipsq, &connp);
19835 		ASSERT(mp != NULL);
19836 		if (err != 0)
19837 			return (err);
19838 	} else {
19839 		/*
19840 		 * Interfaces without underlying hardware don't do duplicate
19841 		 * address detection.
19842 		 */
19843 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19844 		ipif->ipif_addr_ready = 1;
19845 	}
19846 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19847 }
19848 
19849 /*
19850  * Perform a bind for the physical device.
19851  * When the routine returns EINPROGRESS then mp has been consumed and
19852  * the ioctl will be acked from ip_rput_dlpi.
19853  * Allocate an unbind message and save it until ipif_down.
19854  */
19855 static int
19856 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19857 {
19858 	areq_t	*areq;
19859 	mblk_t	*areq_mp = NULL;
19860 	mblk_t	*bind_mp = NULL;
19861 	mblk_t	*unbind_mp = NULL;
19862 	conn_t	*connp;
19863 	boolean_t success;
19864 	uint16_t sap_addr;
19865 
19866 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19867 	ASSERT(IAM_WRITER_ILL(ill));
19868 	ASSERT(mp != NULL);
19869 
19870 	/* Create a resolver cookie for ARP */
19871 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19872 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19873 		if (areq_mp == NULL)
19874 			return (ENOMEM);
19875 
19876 		freemsg(ill->ill_resolver_mp);
19877 		ill->ill_resolver_mp = areq_mp;
19878 		areq = (areq_t *)areq_mp->b_rptr;
19879 		sap_addr = ill->ill_sap;
19880 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19881 	}
19882 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19883 	    DL_BIND_REQ);
19884 	if (bind_mp == NULL)
19885 		goto bad;
19886 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19887 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19888 
19889 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19890 	if (unbind_mp == NULL)
19891 		goto bad;
19892 
19893 	/*
19894 	 * Record state needed to complete this operation when the
19895 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19896 	 */
19897 	ASSERT(WR(q)->q_next == NULL);
19898 	connp = Q_TO_CONN(q);
19899 
19900 	mutex_enter(&connp->conn_lock);
19901 	mutex_enter(&ipif->ipif_ill->ill_lock);
19902 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19903 	mutex_exit(&ipif->ipif_ill->ill_lock);
19904 	mutex_exit(&connp->conn_lock);
19905 	if (!success)
19906 		goto bad;
19907 
19908 	/*
19909 	 * Save the unbind message for ill_dl_down(); it will be consumed when
19910 	 * the interface goes down.
19911 	 */
19912 	ASSERT(ill->ill_unbind_mp == NULL);
19913 	ill->ill_unbind_mp = unbind_mp;
19914 
19915 	ill_dlpi_send(ill, bind_mp);
19916 	/* Send down link-layer capabilities probe if not already done. */
19917 	ill_capability_probe(ill);
19918 
19919 	/*
19920 	 * Sysid used to rely on the fact that netboots set domainname
19921 	 * and the like. Now that miniroot boots aren't strictly netboots
19922 	 * and miniroot network configuration is driven from userland
19923 	 * these things still need to be set. This situation can be detected
19924 	 * by comparing the interface being configured here to the one
19925 	 * dhcifname was set to reference by the boot loader. Once sysid is
19926 	 * converted to use dhcp_ipc_getinfo() this call can go away.
19927 	 */
19928 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
19929 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
19930 	    (strlen(srpc_domain) == 0)) {
19931 		if (dhcpinit() != 0)
19932 			cmn_err(CE_WARN, "no cached dhcp response");
19933 	}
19934 
19935 	/*
19936 	 * This operation will complete in ip_rput_dlpi with either
19937 	 * a DL_BIND_ACK or DL_ERROR_ACK.
19938 	 */
19939 	return (EINPROGRESS);
19940 bad:
19941 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
19942 	/*
19943 	 * We don't have to check for possible removal from illgrp
19944 	 * as we have not yet inserted in illgrp. For groups
19945 	 * without names, this ipif is still not UP and hence
19946 	 * this could not have possibly had any influence in forming
19947 	 * groups.
19948 	 */
19949 
19950 	freemsg(bind_mp);
19951 	freemsg(unbind_mp);
19952 	return (ENOMEM);
19953 }
19954 
19955 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
19956 
19957 /*
19958  * DLPI and ARP is up.
19959  * Create all the IREs associated with an interface bring up multicast.
19960  * Set the interface flag and finish other initialization
19961  * that potentially had to be differed to after DL_BIND_ACK.
19962  */
19963 int
19964 ipif_up_done(ipif_t *ipif)
19965 {
19966 	ire_t	*ire_array[20];
19967 	ire_t	**irep = ire_array;
19968 	ire_t	**irep1;
19969 	ipaddr_t net_mask = 0;
19970 	ipaddr_t subnet_mask, route_mask;
19971 	ill_t	*ill = ipif->ipif_ill;
19972 	queue_t	*stq;
19973 	ipif_t	 *src_ipif;
19974 	ipif_t   *tmp_ipif;
19975 	boolean_t	flush_ire_cache = B_TRUE;
19976 	int	err = 0;
19977 	phyint_t *phyi;
19978 	ire_t	**ipif_saved_irep = NULL;
19979 	int ipif_saved_ire_cnt;
19980 	int	cnt;
19981 	boolean_t	src_ipif_held = B_FALSE;
19982 	boolean_t	ire_added = B_FALSE;
19983 	boolean_t	loopback = B_FALSE;
19984 	ip_stack_t	*ipst = ill->ill_ipst;
19985 
19986 	ip1dbg(("ipif_up_done(%s:%u)\n",
19987 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
19988 	/* Check if this is a loopback interface */
19989 	if (ipif->ipif_ill->ill_wq == NULL)
19990 		loopback = B_TRUE;
19991 
19992 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19993 	/*
19994 	 * If all other interfaces for this ill are down or DEPRECATED,
19995 	 * or otherwise unsuitable for source address selection, remove
19996 	 * any IRE_CACHE entries for this ill to make sure source
19997 	 * address selection gets to take this new ipif into account.
19998 	 * No need to hold ill_lock while traversing the ipif list since
19999 	 * we are writer
20000 	 */
20001 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20002 	    tmp_ipif = tmp_ipif->ipif_next) {
20003 		if (((tmp_ipif->ipif_flags &
20004 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20005 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20006 		    (tmp_ipif == ipif))
20007 			continue;
20008 		/* first useable pre-existing interface */
20009 		flush_ire_cache = B_FALSE;
20010 		break;
20011 	}
20012 	if (flush_ire_cache)
20013 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20014 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20015 
20016 	/*
20017 	 * Figure out which way the send-to queue should go.  Only
20018 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20019 	 * should show up here.
20020 	 */
20021 	switch (ill->ill_net_type) {
20022 	case IRE_IF_RESOLVER:
20023 		stq = ill->ill_rq;
20024 		break;
20025 	case IRE_IF_NORESOLVER:
20026 	case IRE_LOOPBACK:
20027 		stq = ill->ill_wq;
20028 		break;
20029 	default:
20030 		return (EINVAL);
20031 	}
20032 
20033 	if (IS_LOOPBACK(ill)) {
20034 		/*
20035 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20036 		 * ipif_lookup_on_name(), but in the case of zones we can have
20037 		 * several loopback addresses on lo0. So all the interfaces with
20038 		 * loopback addresses need to be marked IRE_LOOPBACK.
20039 		 */
20040 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20041 		    htonl(INADDR_LOOPBACK))
20042 			ipif->ipif_ire_type = IRE_LOOPBACK;
20043 		else
20044 			ipif->ipif_ire_type = IRE_LOCAL;
20045 	}
20046 
20047 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20048 		/*
20049 		 * Can't use our source address. Select a different
20050 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20051 		 */
20052 		src_ipif = ipif_select_source(ipif->ipif_ill,
20053 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20054 		if (src_ipif == NULL)
20055 			src_ipif = ipif;	/* Last resort */
20056 		else
20057 			src_ipif_held = B_TRUE;
20058 	} else {
20059 		src_ipif = ipif;
20060 	}
20061 
20062 	/* Create all the IREs associated with this interface */
20063 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20064 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20065 
20066 		/*
20067 		 * If we're on a labeled system then make sure that zone-
20068 		 * private addresses have proper remote host database entries.
20069 		 */
20070 		if (is_system_labeled() &&
20071 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20072 		    !tsol_check_interface_address(ipif))
20073 			return (EINVAL);
20074 
20075 		/* Register the source address for __sin6_src_id */
20076 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20077 		    ipif->ipif_zoneid, ipst);
20078 		if (err != 0) {
20079 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20080 			return (err);
20081 		}
20082 
20083 		/* If the interface address is set, create the local IRE. */
20084 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20085 		    (void *)ipif,
20086 		    ipif->ipif_ire_type,
20087 		    ntohl(ipif->ipif_lcl_addr)));
20088 		*irep++ = ire_create(
20089 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20090 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20091 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20092 		    NULL,				/* no gateway */
20093 		    &ip_loopback_mtuplus,		/* max frag size */
20094 		    NULL,
20095 		    ipif->ipif_rq,			/* recv-from queue */
20096 		    NULL,				/* no send-to queue */
20097 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20098 		    ipif,
20099 		    0,
20100 		    0,
20101 		    0,
20102 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20103 		    RTF_PRIVATE : 0,
20104 		    &ire_uinfo_null,
20105 		    NULL,
20106 		    NULL,
20107 		    ipst);
20108 	} else {
20109 		ip1dbg((
20110 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20111 		    ipif->ipif_ire_type,
20112 		    ntohl(ipif->ipif_lcl_addr),
20113 		    (uint_t)ipif->ipif_flags));
20114 	}
20115 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20116 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20117 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20118 	} else {
20119 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20120 	}
20121 
20122 	subnet_mask = ipif->ipif_net_mask;
20123 
20124 	/*
20125 	 * If mask was not specified, use natural netmask of
20126 	 * interface address. Also, store this mask back into the
20127 	 * ipif struct.
20128 	 */
20129 	if (subnet_mask == 0) {
20130 		subnet_mask = net_mask;
20131 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20132 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20133 		    ipif->ipif_v6subnet);
20134 	}
20135 
20136 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20137 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20138 	    ipif->ipif_subnet != INADDR_ANY) {
20139 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20140 
20141 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20142 			route_mask = IP_HOST_MASK;
20143 		} else {
20144 			route_mask = subnet_mask;
20145 		}
20146 
20147 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20148 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20149 		    (void *)ipif, (void *)ill,
20150 		    ill->ill_net_type,
20151 		    ntohl(ipif->ipif_subnet)));
20152 		*irep++ = ire_create(
20153 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20154 		    (uchar_t *)&route_mask,		/* mask */
20155 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20156 		    NULL,				/* no gateway */
20157 		    &ipif->ipif_mtu,			/* max frag */
20158 		    NULL,
20159 		    NULL,				/* no recv queue */
20160 		    stq,				/* send-to queue */
20161 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20162 		    ipif,
20163 		    0,
20164 		    0,
20165 		    0,
20166 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20167 		    &ire_uinfo_null,
20168 		    NULL,
20169 		    NULL,
20170 		    ipst);
20171 	}
20172 
20173 	/*
20174 	 * Create any necessary broadcast IREs.
20175 	 */
20176 	if (ipif->ipif_flags & IPIF_BROADCAST)
20177 		irep = ipif_create_bcast_ires(ipif, irep);
20178 
20179 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20180 
20181 	/* If an earlier ire_create failed, get out now */
20182 	for (irep1 = irep; irep1 > ire_array; ) {
20183 		irep1--;
20184 		if (*irep1 == NULL) {
20185 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20186 			err = ENOMEM;
20187 			goto bad;
20188 		}
20189 	}
20190 
20191 	/*
20192 	 * Need to atomically check for ip_addr_availablity_check
20193 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20194 	 * from group also.The ill_g_lock is grabbed as reader
20195 	 * just to make sure no new ills or new ipifs are being added
20196 	 * to the system while we are checking the uniqueness of addresses.
20197 	 */
20198 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20199 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20200 	/* Mark it up, and increment counters. */
20201 	ipif->ipif_flags |= IPIF_UP;
20202 	ill->ill_ipif_up_count++;
20203 	err = ip_addr_availability_check(ipif);
20204 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20205 	rw_exit(&ipst->ips_ill_g_lock);
20206 
20207 	if (err != 0) {
20208 		/*
20209 		 * Our address may already be up on the same ill. In this case,
20210 		 * the ARP entry for our ipif replaced the one for the other
20211 		 * ipif. So we don't want to delete it (otherwise the other ipif
20212 		 * would be unable to send packets).
20213 		 * ip_addr_availability_check() identifies this case for us and
20214 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20215 		 * which is the expected error code.
20216 		 */
20217 		if (err == EADDRINUSE) {
20218 			freemsg(ipif->ipif_arp_del_mp);
20219 			ipif->ipif_arp_del_mp = NULL;
20220 			err = EADDRNOTAVAIL;
20221 		}
20222 		ill->ill_ipif_up_count--;
20223 		ipif->ipif_flags &= ~IPIF_UP;
20224 		goto bad;
20225 	}
20226 
20227 	/*
20228 	 * Add in all newly created IREs.  ire_create_bcast() has
20229 	 * already checked for duplicates of the IRE_BROADCAST type.
20230 	 * We want to add before we call ifgrp_insert which wants
20231 	 * to know whether IRE_IF_RESOLVER exists or not.
20232 	 *
20233 	 * NOTE : We refrele the ire though we may branch to "bad"
20234 	 *	  later on where we do ire_delete. This is okay
20235 	 *	  because nobody can delete it as we are running
20236 	 *	  exclusively.
20237 	 */
20238 	for (irep1 = irep; irep1 > ire_array; ) {
20239 		irep1--;
20240 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20241 		/*
20242 		 * refheld by ire_add. refele towards the end of the func
20243 		 */
20244 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20245 	}
20246 	ire_added = B_TRUE;
20247 	/*
20248 	 * Form groups if possible.
20249 	 *
20250 	 * If we are supposed to be in a ill_group with a name, insert it
20251 	 * now as we know that at least one ipif is UP. Otherwise form
20252 	 * nameless groups.
20253 	 *
20254 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20255 	 * this ipif into the appropriate interface group, or create a
20256 	 * new one. If this is already in a nameless group, we try to form
20257 	 * a bigger group looking at other ills potentially sharing this
20258 	 * ipif's prefix.
20259 	 */
20260 	phyi = ill->ill_phyint;
20261 	if (phyi->phyint_groupname_len != 0) {
20262 		ASSERT(phyi->phyint_groupname != NULL);
20263 		if (ill->ill_ipif_up_count == 1) {
20264 			ASSERT(ill->ill_group == NULL);
20265 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20266 			    phyi->phyint_groupname, NULL, B_TRUE);
20267 			if (err != 0) {
20268 				ip1dbg(("ipif_up_done: illgrp allocation "
20269 				    "failed, error %d\n", err));
20270 				goto bad;
20271 			}
20272 		}
20273 		ASSERT(ill->ill_group != NULL);
20274 	}
20275 
20276 	/*
20277 	 * When this is part of group, we need to make sure that
20278 	 * any broadcast ires created because of this ipif coming
20279 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20280 	 * so that we don't receive duplicate broadcast packets.
20281 	 */
20282 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20283 		ipif_renominate_bcast(ipif);
20284 
20285 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20286 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20287 	ipif_saved_irep = ipif_recover_ire(ipif);
20288 
20289 	if (!loopback) {
20290 		/*
20291 		 * If the broadcast address has been set, make sure it makes
20292 		 * sense based on the interface address.
20293 		 * Only match on ill since we are sharing broadcast addresses.
20294 		 */
20295 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20296 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20297 			ire_t	*ire;
20298 
20299 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20300 			    IRE_BROADCAST, ipif, ALL_ZONES,
20301 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20302 
20303 			if (ire == NULL) {
20304 				/*
20305 				 * If there isn't a matching broadcast IRE,
20306 				 * revert to the default for this netmask.
20307 				 */
20308 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20309 				mutex_enter(&ipif->ipif_ill->ill_lock);
20310 				ipif_set_default(ipif);
20311 				mutex_exit(&ipif->ipif_ill->ill_lock);
20312 			} else {
20313 				ire_refrele(ire);
20314 			}
20315 		}
20316 
20317 	}
20318 
20319 	if (ill->ill_need_recover_multicast) {
20320 		/*
20321 		 * Need to recover all multicast memberships in the driver.
20322 		 * This had to be deferred until we had attached.  The same
20323 		 * code exists in ipif_up_done_v6() to recover IPv6
20324 		 * memberships.
20325 		 *
20326 		 * Note that it would be preferable to unconditionally do the
20327 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
20328 		 * that since ill_join_allmulti() depends on ill_dl_up being
20329 		 * set, and it is not set until we receive a DL_BIND_ACK after
20330 		 * having called ill_dl_up().
20331 		 */
20332 		ill_recover_multicast(ill);
20333 	}
20334 	/* Join the allhosts multicast address */
20335 	ipif_multicast_up(ipif);
20336 
20337 	if (!loopback) {
20338 		/*
20339 		 * See whether anybody else would benefit from the
20340 		 * new ipif that we added. We call this always rather
20341 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20342 		 * ipif is for the benefit of illgrp_insert (done above)
20343 		 * which does not do source address selection as it does
20344 		 * not want to re-create interface routes that we are
20345 		 * having reference to it here.
20346 		 */
20347 		ill_update_source_selection(ill);
20348 	}
20349 
20350 	for (irep1 = irep; irep1 > ire_array; ) {
20351 		irep1--;
20352 		if (*irep1 != NULL) {
20353 			/* was held in ire_add */
20354 			ire_refrele(*irep1);
20355 		}
20356 	}
20357 
20358 	cnt = ipif_saved_ire_cnt;
20359 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20360 		if (*irep1 != NULL) {
20361 			/* was held in ire_add */
20362 			ire_refrele(*irep1);
20363 		}
20364 	}
20365 
20366 	if (!loopback && ipif->ipif_addr_ready) {
20367 		/* Broadcast an address mask reply. */
20368 		ipif_mask_reply(ipif);
20369 	}
20370 	if (ipif_saved_irep != NULL) {
20371 		kmem_free(ipif_saved_irep,
20372 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20373 	}
20374 	if (src_ipif_held)
20375 		ipif_refrele(src_ipif);
20376 
20377 	/*
20378 	 * This had to be deferred until we had bound.  Tell routing sockets and
20379 	 * others that this interface is up if it looks like the address has
20380 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20381 	 * duplicate address detection to do its thing.
20382 	 */
20383 	if (ipif->ipif_addr_ready)
20384 		ipif_up_notify(ipif);
20385 	return (0);
20386 
20387 bad:
20388 	ip1dbg(("ipif_up_done: FAILED \n"));
20389 	/*
20390 	 * We don't have to bother removing from ill groups because
20391 	 *
20392 	 * 1) For groups with names, we insert only when the first ipif
20393 	 *    comes up. In that case if it fails, it will not be in any
20394 	 *    group. So, we need not try to remove for that case.
20395 	 *
20396 	 * 2) For groups without names, either we tried to insert ipif_ill
20397 	 *    in a group as singleton or found some other group to become
20398 	 *    a bigger group. For the former, if it fails we don't have
20399 	 *    anything to do as ipif_ill is not in the group and for the
20400 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20401 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20402 	 */
20403 	while (irep > ire_array) {
20404 		irep--;
20405 		if (*irep != NULL) {
20406 			ire_delete(*irep);
20407 			if (ire_added)
20408 				ire_refrele(*irep);
20409 		}
20410 	}
20411 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20412 
20413 	if (ipif_saved_irep != NULL) {
20414 		kmem_free(ipif_saved_irep,
20415 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20416 	}
20417 	if (src_ipif_held)
20418 		ipif_refrele(src_ipif);
20419 
20420 	ipif_arp_down(ipif);
20421 	return (err);
20422 }
20423 
20424 /*
20425  * Turn off the ARP with the ILLF_NOARP flag.
20426  */
20427 static int
20428 ill_arp_off(ill_t *ill)
20429 {
20430 	mblk_t	*arp_off_mp = NULL;
20431 	mblk_t	*arp_on_mp = NULL;
20432 
20433 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20434 
20435 	ASSERT(IAM_WRITER_ILL(ill));
20436 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20437 
20438 	/*
20439 	 * If the on message is still around we've already done
20440 	 * an arp_off without doing an arp_on thus there is no
20441 	 * work needed.
20442 	 */
20443 	if (ill->ill_arp_on_mp != NULL)
20444 		return (0);
20445 
20446 	/*
20447 	 * Allocate an ARP on message (to be saved) and an ARP off message
20448 	 */
20449 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20450 	if (!arp_off_mp)
20451 		return (ENOMEM);
20452 
20453 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20454 	if (!arp_on_mp)
20455 		goto failed;
20456 
20457 	ASSERT(ill->ill_arp_on_mp == NULL);
20458 	ill->ill_arp_on_mp = arp_on_mp;
20459 
20460 	/* Send an AR_INTERFACE_OFF request */
20461 	putnext(ill->ill_rq, arp_off_mp);
20462 	return (0);
20463 failed:
20464 
20465 	if (arp_off_mp)
20466 		freemsg(arp_off_mp);
20467 	return (ENOMEM);
20468 }
20469 
20470 /*
20471  * Turn on ARP by turning off the ILLF_NOARP flag.
20472  */
20473 static int
20474 ill_arp_on(ill_t *ill)
20475 {
20476 	mblk_t	*mp;
20477 
20478 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20479 
20480 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20481 
20482 	ASSERT(IAM_WRITER_ILL(ill));
20483 	/*
20484 	 * Send an AR_INTERFACE_ON request if we have already done
20485 	 * an arp_off (which allocated the message).
20486 	 */
20487 	if (ill->ill_arp_on_mp != NULL) {
20488 		mp = ill->ill_arp_on_mp;
20489 		ill->ill_arp_on_mp = NULL;
20490 		putnext(ill->ill_rq, mp);
20491 	}
20492 	return (0);
20493 }
20494 
20495 /*
20496  * Called after either deleting ill from the group or when setting
20497  * FAILED or STANDBY on the interface.
20498  */
20499 static void
20500 illgrp_reset_schednext(ill_t *ill)
20501 {
20502 	ill_group_t *illgrp;
20503 	ill_t *save_ill;
20504 
20505 	ASSERT(IAM_WRITER_ILL(ill));
20506 	/*
20507 	 * When called from illgrp_delete, ill_group will be non-NULL.
20508 	 * But when called from ip_sioctl_flags, it could be NULL if
20509 	 * somebody is setting FAILED/INACTIVE on some interface which
20510 	 * is not part of a group.
20511 	 */
20512 	illgrp = ill->ill_group;
20513 	if (illgrp == NULL)
20514 		return;
20515 	if (illgrp->illgrp_ill_schednext != ill)
20516 		return;
20517 
20518 	illgrp->illgrp_ill_schednext = NULL;
20519 	save_ill = ill;
20520 	/*
20521 	 * Choose a good ill to be the next one for
20522 	 * outbound traffic. As the flags FAILED/STANDBY is
20523 	 * not yet marked when called from ip_sioctl_flags,
20524 	 * we check for ill separately.
20525 	 */
20526 	for (ill = illgrp->illgrp_ill; ill != NULL;
20527 	    ill = ill->ill_group_next) {
20528 		if ((ill != save_ill) &&
20529 		    !(ill->ill_phyint->phyint_flags &
20530 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20531 			illgrp->illgrp_ill_schednext = ill;
20532 			return;
20533 		}
20534 	}
20535 }
20536 
20537 /*
20538  * Given an ill, find the next ill in the group to be scheduled.
20539  * (This should be called by ip_newroute() before ire_create().)
20540  * The passed in ill may be pulled out of the group, after we have picked
20541  * up a different outgoing ill from the same group. However ire add will
20542  * atomically check this.
20543  */
20544 ill_t *
20545 illgrp_scheduler(ill_t *ill)
20546 {
20547 	ill_t *retill;
20548 	ill_group_t *illgrp;
20549 	int illcnt;
20550 	int i;
20551 	uint64_t flags;
20552 	ip_stack_t	*ipst = ill->ill_ipst;
20553 
20554 	/*
20555 	 * We don't use a lock to check for the ill_group. If this ill
20556 	 * is currently being inserted we may end up just returning this
20557 	 * ill itself. That is ok.
20558 	 */
20559 	if (ill->ill_group == NULL) {
20560 		ill_refhold(ill);
20561 		return (ill);
20562 	}
20563 
20564 	/*
20565 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20566 	 * a set of stable ills. No ill can be added or deleted or change
20567 	 * group while we hold the reader lock.
20568 	 */
20569 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20570 	if ((illgrp = ill->ill_group) == NULL) {
20571 		rw_exit(&ipst->ips_ill_g_lock);
20572 		ill_refhold(ill);
20573 		return (ill);
20574 	}
20575 
20576 	illcnt = illgrp->illgrp_ill_count;
20577 	mutex_enter(&illgrp->illgrp_lock);
20578 	retill = illgrp->illgrp_ill_schednext;
20579 
20580 	if (retill == NULL)
20581 		retill = illgrp->illgrp_ill;
20582 
20583 	/*
20584 	 * We do a circular search beginning at illgrp_ill_schednext
20585 	 * or illgrp_ill. We don't check the flags against the ill lock
20586 	 * since it can change anytime. The ire creation will be atomic
20587 	 * and will fail if the ill is FAILED or OFFLINE.
20588 	 */
20589 	for (i = 0; i < illcnt; i++) {
20590 		flags = retill->ill_phyint->phyint_flags;
20591 
20592 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20593 		    ILL_CAN_LOOKUP(retill)) {
20594 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20595 			ill_refhold(retill);
20596 			break;
20597 		}
20598 		retill = retill->ill_group_next;
20599 		if (retill == NULL)
20600 			retill = illgrp->illgrp_ill;
20601 	}
20602 	mutex_exit(&illgrp->illgrp_lock);
20603 	rw_exit(&ipst->ips_ill_g_lock);
20604 
20605 	return (i == illcnt ? NULL : retill);
20606 }
20607 
20608 /*
20609  * Checks for availbility of a usable source address (if there is one) when the
20610  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20611  * this selection is done regardless of the destination.
20612  */
20613 boolean_t
20614 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20615 {
20616 	uint_t	ifindex;
20617 	ipif_t	*ipif = NULL;
20618 	ill_t	*uill;
20619 	boolean_t isv6;
20620 	ip_stack_t	*ipst = ill->ill_ipst;
20621 
20622 	ASSERT(ill != NULL);
20623 
20624 	isv6 = ill->ill_isv6;
20625 	ifindex = ill->ill_usesrc_ifindex;
20626 	if (ifindex != 0) {
20627 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20628 		    NULL, ipst);
20629 		if (uill == NULL)
20630 			return (NULL);
20631 		mutex_enter(&uill->ill_lock);
20632 		for (ipif = uill->ill_ipif; ipif != NULL;
20633 		    ipif = ipif->ipif_next) {
20634 			if (!IPIF_CAN_LOOKUP(ipif))
20635 				continue;
20636 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20637 				continue;
20638 			if (!(ipif->ipif_flags & IPIF_UP))
20639 				continue;
20640 			if (ipif->ipif_zoneid != zoneid)
20641 				continue;
20642 			if ((isv6 &&
20643 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20644 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20645 				continue;
20646 			mutex_exit(&uill->ill_lock);
20647 			ill_refrele(uill);
20648 			return (B_TRUE);
20649 		}
20650 		mutex_exit(&uill->ill_lock);
20651 		ill_refrele(uill);
20652 	}
20653 	return (B_FALSE);
20654 }
20655 
20656 /*
20657  * Determine the best source address given a destination address and an ill.
20658  * Prefers non-deprecated over deprecated but will return a deprecated
20659  * address if there is no other choice. If there is a usable source address
20660  * on the interface pointed to by ill_usesrc_ifindex then that is given
20661  * first preference.
20662  *
20663  * Returns NULL if there is no suitable source address for the ill.
20664  * This only occurs when there is no valid source address for the ill.
20665  */
20666 ipif_t *
20667 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20668 {
20669 	ipif_t *ipif;
20670 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20671 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20672 	int index = 0;
20673 	boolean_t wrapped = B_FALSE;
20674 	boolean_t same_subnet_only = B_FALSE;
20675 	boolean_t ipif_same_found, ipif_other_found;
20676 	boolean_t specific_found;
20677 	ill_t	*till, *usill = NULL;
20678 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20679 	ip_stack_t	*ipst = ill->ill_ipst;
20680 
20681 	if (ill->ill_usesrc_ifindex != 0) {
20682 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20683 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20684 		if (usill != NULL)
20685 			ill = usill;	/* Select source from usesrc ILL */
20686 		else
20687 			return (NULL);
20688 	}
20689 
20690 	/*
20691 	 * If we're dealing with an unlabeled destination on a labeled system,
20692 	 * make sure that we ignore source addresses that are incompatible with
20693 	 * the destination's default label.  That destination's default label
20694 	 * must dominate the minimum label on the source address.
20695 	 */
20696 	dst_rhtp = NULL;
20697 	if (is_system_labeled()) {
20698 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20699 		if (dst_rhtp == NULL)
20700 			return (NULL);
20701 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20702 			TPC_RELE(dst_rhtp);
20703 			dst_rhtp = NULL;
20704 		}
20705 	}
20706 
20707 	/*
20708 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20709 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20710 	 * After selecting the right ipif, under ill_lock make sure ipif is
20711 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20712 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20713 	 * but not under a lock.
20714 	 */
20715 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20716 
20717 retry:
20718 	till = ill;
20719 	ipif_arr[0] = NULL;
20720 
20721 	if (till->ill_group != NULL)
20722 		till = till->ill_group->illgrp_ill;
20723 
20724 	/*
20725 	 * Choose one good source address from each ill across the group.
20726 	 * If possible choose a source address in the same subnet as
20727 	 * the destination address.
20728 	 *
20729 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20730 	 * This is okay because of the following.
20731 	 *
20732 	 *    If PHYI_FAILED is set and we still have non-deprecated
20733 	 *    addresses, it means the addresses have not yet been
20734 	 *    failed over to a different interface. We potentially
20735 	 *    select them to create IRE_CACHES, which will be later
20736 	 *    flushed when the addresses move over.
20737 	 *
20738 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20739 	 *    addresses, it means either the user has configured them
20740 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20741 	 *    been moved over. For the former, in.mpathd does a failover
20742 	 *    when the interface becomes INACTIVE and hence we should
20743 	 *    not find them. Once INACTIVE is set, we don't allow them
20744 	 *    to create logical interfaces anymore. For the latter, a
20745 	 *    flush will happen when INACTIVE is cleared which will
20746 	 *    flush the IRE_CACHES.
20747 	 *
20748 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20749 	 *    over soon. We potentially select them to create IRE_CACHEs,
20750 	 *    which will be later flushed when the addresses move over.
20751 	 *
20752 	 * NOTE : As ipif_select_source is called to borrow source address
20753 	 * for an ipif that is part of a group, source address selection
20754 	 * will be re-done whenever the group changes i.e either an
20755 	 * insertion/deletion in the group.
20756 	 *
20757 	 * Fill ipif_arr[] with source addresses, using these rules:
20758 	 *
20759 	 *	1. At most one source address from a given ill ends up
20760 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20761 	 *	   associated with a given ill ends up in ipif_arr[].
20762 	 *
20763 	 *	2. If there is at least one non-deprecated ipif in the
20764 	 *	   IPMP group with a source address on the same subnet as
20765 	 *	   our destination, then fill ipif_arr[] only with
20766 	 *	   source addresses on the same subnet as our destination.
20767 	 *	   Note that because of (1), only the first
20768 	 *	   non-deprecated ipif found with a source address
20769 	 *	   matching the destination ends up in ipif_arr[].
20770 	 *
20771 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20772 	 *	   addresses not in the same subnet as our destination.
20773 	 *	   Again, because of (1), only the first off-subnet source
20774 	 *	   address will be chosen.
20775 	 *
20776 	 *	4. If there are no non-deprecated ipifs, then just use
20777 	 *	   the source address associated with the last deprecated
20778 	 *	   one we find that happens to be on the same subnet,
20779 	 *	   otherwise the first one not in the same subnet.
20780 	 */
20781 	specific_found = B_FALSE;
20782 	for (; till != NULL; till = till->ill_group_next) {
20783 		ipif_same_found = B_FALSE;
20784 		ipif_other_found = B_FALSE;
20785 		for (ipif = till->ill_ipif; ipif != NULL;
20786 		    ipif = ipif->ipif_next) {
20787 			if (!IPIF_CAN_LOOKUP(ipif))
20788 				continue;
20789 			/* Always skip NOLOCAL and ANYCAST interfaces */
20790 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20791 				continue;
20792 			if (!(ipif->ipif_flags & IPIF_UP) ||
20793 			    !ipif->ipif_addr_ready)
20794 				continue;
20795 			if (ipif->ipif_zoneid != zoneid &&
20796 			    ipif->ipif_zoneid != ALL_ZONES)
20797 				continue;
20798 			/*
20799 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20800 			 * but are not valid as source addresses.
20801 			 */
20802 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20803 				continue;
20804 
20805 			/*
20806 			 * Check compatibility of local address for
20807 			 * destination's default label if we're on a labeled
20808 			 * system.  Incompatible addresses can't be used at
20809 			 * all.
20810 			 */
20811 			if (dst_rhtp != NULL) {
20812 				boolean_t incompat;
20813 
20814 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20815 				    IPV4_VERSION, B_FALSE);
20816 				if (src_rhtp == NULL)
20817 					continue;
20818 				incompat =
20819 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20820 				    src_rhtp->tpc_tp.tp_doi !=
20821 				    dst_rhtp->tpc_tp.tp_doi ||
20822 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20823 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20824 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20825 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20826 				TPC_RELE(src_rhtp);
20827 				if (incompat)
20828 					continue;
20829 			}
20830 
20831 			/*
20832 			 * We prefer not to use all all-zones addresses, if we
20833 			 * can avoid it, as they pose problems with unlabeled
20834 			 * destinations.
20835 			 */
20836 			if (ipif->ipif_zoneid != ALL_ZONES) {
20837 				if (!specific_found &&
20838 				    (!same_subnet_only ||
20839 				    (ipif->ipif_net_mask & dst) ==
20840 				    ipif->ipif_subnet)) {
20841 					index = 0;
20842 					specific_found = B_TRUE;
20843 					ipif_other_found = B_FALSE;
20844 				}
20845 			} else {
20846 				if (specific_found)
20847 					continue;
20848 			}
20849 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20850 				if (ipif_dep == NULL ||
20851 				    (ipif->ipif_net_mask & dst) ==
20852 				    ipif->ipif_subnet)
20853 					ipif_dep = ipif;
20854 				continue;
20855 			}
20856 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20857 				/* found a source address in the same subnet */
20858 				if (!same_subnet_only) {
20859 					same_subnet_only = B_TRUE;
20860 					index = 0;
20861 				}
20862 				ipif_same_found = B_TRUE;
20863 			} else {
20864 				if (same_subnet_only || ipif_other_found)
20865 					continue;
20866 				ipif_other_found = B_TRUE;
20867 			}
20868 			ipif_arr[index++] = ipif;
20869 			if (index == MAX_IPIF_SELECT_SOURCE) {
20870 				wrapped = B_TRUE;
20871 				index = 0;
20872 			}
20873 			if (ipif_same_found)
20874 				break;
20875 		}
20876 	}
20877 
20878 	if (ipif_arr[0] == NULL) {
20879 		ipif = ipif_dep;
20880 	} else {
20881 		if (wrapped)
20882 			index = MAX_IPIF_SELECT_SOURCE;
20883 		ipif = ipif_arr[ipif_rand(ipst) % index];
20884 		ASSERT(ipif != NULL);
20885 	}
20886 
20887 	if (ipif != NULL) {
20888 		mutex_enter(&ipif->ipif_ill->ill_lock);
20889 		if (!IPIF_CAN_LOOKUP(ipif)) {
20890 			mutex_exit(&ipif->ipif_ill->ill_lock);
20891 			goto retry;
20892 		}
20893 		ipif_refhold_locked(ipif);
20894 		mutex_exit(&ipif->ipif_ill->ill_lock);
20895 	}
20896 
20897 	rw_exit(&ipst->ips_ill_g_lock);
20898 	if (usill != NULL)
20899 		ill_refrele(usill);
20900 	if (dst_rhtp != NULL)
20901 		TPC_RELE(dst_rhtp);
20902 
20903 #ifdef DEBUG
20904 	if (ipif == NULL) {
20905 		char buf1[INET6_ADDRSTRLEN];
20906 
20907 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20908 		    ill->ill_name,
20909 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20910 	} else {
20911 		char buf1[INET6_ADDRSTRLEN];
20912 		char buf2[INET6_ADDRSTRLEN];
20913 
20914 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20915 		    ipif->ipif_ill->ill_name,
20916 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
20917 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
20918 		    buf2, sizeof (buf2))));
20919 	}
20920 #endif /* DEBUG */
20921 	return (ipif);
20922 }
20923 
20924 
20925 /*
20926  * If old_ipif is not NULL, see if ipif was derived from old
20927  * ipif and if so, recreate the interface route by re-doing
20928  * source address selection. This happens when ipif_down ->
20929  * ipif_update_other_ipifs calls us.
20930  *
20931  * If old_ipif is NULL, just redo the source address selection
20932  * if needed. This happens when illgrp_insert or ipif_up_done
20933  * calls us.
20934  */
20935 static void
20936 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
20937 {
20938 	ire_t *ire;
20939 	ire_t *ipif_ire;
20940 	queue_t *stq;
20941 	ipif_t *nipif;
20942 	ill_t *ill;
20943 	boolean_t need_rele = B_FALSE;
20944 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
20945 
20946 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
20947 	ASSERT(IAM_WRITER_IPIF(ipif));
20948 
20949 	ill = ipif->ipif_ill;
20950 	if (!(ipif->ipif_flags &
20951 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
20952 		/*
20953 		 * Can't possibly have borrowed the source
20954 		 * from old_ipif.
20955 		 */
20956 		return;
20957 	}
20958 
20959 	/*
20960 	 * Is there any work to be done? No work if the address
20961 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
20962 	 * ipif_select_source() does not borrow addresses from
20963 	 * NOLOCAL and ANYCAST interfaces).
20964 	 */
20965 	if ((old_ipif != NULL) &&
20966 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
20967 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
20968 	    (old_ipif->ipif_flags &
20969 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
20970 		return;
20971 	}
20972 
20973 	/*
20974 	 * Perform the same checks as when creating the
20975 	 * IRE_INTERFACE in ipif_up_done.
20976 	 */
20977 	if (!(ipif->ipif_flags & IPIF_UP))
20978 		return;
20979 
20980 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
20981 	    (ipif->ipif_subnet == INADDR_ANY))
20982 		return;
20983 
20984 	ipif_ire = ipif_to_ire(ipif);
20985 	if (ipif_ire == NULL)
20986 		return;
20987 
20988 	/*
20989 	 * We know that ipif uses some other source for its
20990 	 * IRE_INTERFACE. Is it using the source of this
20991 	 * old_ipif?
20992 	 */
20993 	if (old_ipif != NULL &&
20994 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
20995 		ire_refrele(ipif_ire);
20996 		return;
20997 	}
20998 	if (ip_debug > 2) {
20999 		/* ip1dbg */
21000 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21001 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21002 	}
21003 
21004 	stq = ipif_ire->ire_stq;
21005 
21006 	/*
21007 	 * Can't use our source address. Select a different
21008 	 * source address for the IRE_INTERFACE.
21009 	 */
21010 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21011 	if (nipif == NULL) {
21012 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21013 		nipif = ipif;
21014 	} else {
21015 		need_rele = B_TRUE;
21016 	}
21017 
21018 	ire = ire_create(
21019 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21020 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21021 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21022 	    NULL,				/* no gateway */
21023 	    &ipif->ipif_mtu,			/* max frag */
21024 	    NULL,				/* no src nce */
21025 	    NULL,				/* no recv from queue */
21026 	    stq,				/* send-to queue */
21027 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21028 	    ipif,
21029 	    0,
21030 	    0,
21031 	    0,
21032 	    0,
21033 	    &ire_uinfo_null,
21034 	    NULL,
21035 	    NULL,
21036 	    ipst);
21037 
21038 	if (ire != NULL) {
21039 		ire_t *ret_ire;
21040 		int error;
21041 
21042 		/*
21043 		 * We don't need ipif_ire anymore. We need to delete
21044 		 * before we add so that ire_add does not detect
21045 		 * duplicates.
21046 		 */
21047 		ire_delete(ipif_ire);
21048 		ret_ire = ire;
21049 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21050 		ASSERT(error == 0);
21051 		ASSERT(ire == ret_ire);
21052 		/* Held in ire_add */
21053 		ire_refrele(ret_ire);
21054 	}
21055 	/*
21056 	 * Either we are falling through from above or could not
21057 	 * allocate a replacement.
21058 	 */
21059 	ire_refrele(ipif_ire);
21060 	if (need_rele)
21061 		ipif_refrele(nipif);
21062 }
21063 
21064 /*
21065  * This old_ipif is going away.
21066  *
21067  * Determine if any other ipif's is using our address as
21068  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21069  * IPIF_DEPRECATED).
21070  * Find the IRE_INTERFACE for such ipifs and recreate them
21071  * to use an different source address following the rules in
21072  * ipif_up_done.
21073  *
21074  * This function takes an illgrp as an argument so that illgrp_delete
21075  * can call this to update source address even after deleting the
21076  * old_ipif->ipif_ill from the ill group.
21077  */
21078 static void
21079 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21080 {
21081 	ipif_t *ipif;
21082 	ill_t *ill;
21083 	char	buf[INET6_ADDRSTRLEN];
21084 
21085 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21086 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21087 
21088 	ill = old_ipif->ipif_ill;
21089 
21090 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21091 	    ill->ill_name,
21092 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21093 	    buf, sizeof (buf))));
21094 	/*
21095 	 * If this part of a group, look at all ills as ipif_select_source
21096 	 * borrows source address across all the ills in the group.
21097 	 */
21098 	if (illgrp != NULL)
21099 		ill = illgrp->illgrp_ill;
21100 
21101 	for (; ill != NULL; ill = ill->ill_group_next) {
21102 		for (ipif = ill->ill_ipif; ipif != NULL;
21103 		    ipif = ipif->ipif_next) {
21104 
21105 			if (ipif == old_ipif)
21106 				continue;
21107 
21108 			ipif_recreate_interface_routes(old_ipif, ipif);
21109 		}
21110 	}
21111 }
21112 
21113 /* ARGSUSED */
21114 int
21115 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21116 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21117 {
21118 	/*
21119 	 * ill_phyint_reinit merged the v4 and v6 into a single
21120 	 * ipsq. Could also have become part of a ipmp group in the
21121 	 * process, and we might not have been able to complete the
21122 	 * operation in ipif_set_values, if we could not become
21123 	 * exclusive.  If so restart it here.
21124 	 */
21125 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21126 }
21127 
21128 /*
21129  * Can operate on either a module or a driver queue.
21130  * Returns an error if not a module queue.
21131  */
21132 /* ARGSUSED */
21133 int
21134 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21135     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21136 {
21137 	queue_t		*q1 = q;
21138 	char 		*cp;
21139 	char		interf_name[LIFNAMSIZ];
21140 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21141 
21142 	if (q->q_next == NULL) {
21143 		ip1dbg((
21144 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21145 		return (EINVAL);
21146 	}
21147 
21148 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21149 		return (EALREADY);
21150 
21151 	do {
21152 		q1 = q1->q_next;
21153 	} while (q1->q_next);
21154 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21155 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21156 
21157 	/*
21158 	 * Here we are not going to delay the ioack until after
21159 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21160 	 * original ioctl message before sending the requests.
21161 	 */
21162 	return (ipif_set_values(q, mp, interf_name, &ppa));
21163 }
21164 
21165 /* ARGSUSED */
21166 int
21167 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21168     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21169 {
21170 	return (ENXIO);
21171 }
21172 
21173 /*
21174  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21175  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21176  * ire_check_and_create_bcast()).
21177  */
21178 static ire_t **
21179 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21180 {
21181 	ipaddr_t addr;
21182 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21183 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21184 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21185 
21186 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21187 
21188 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21189 
21190 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21191 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21192 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21193 
21194 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21195 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21196 
21197 	/*
21198 	 * For backward compatibility, we create net broadcast IREs based on
21199 	 * the old "IP address class system", since some old machines only
21200 	 * respond to these class derived net broadcast.  However, we must not
21201 	 * create these net broadcast IREs if the subnetmask is shorter than
21202 	 * the IP address class based derived netmask.  Otherwise, we may
21203 	 * create a net broadcast address which is the same as an IP address
21204 	 * on the subnet -- and then TCP will refuse to talk to that address.
21205 	 */
21206 	if (netmask < subnetmask) {
21207 		addr = netmask & ipif->ipif_subnet;
21208 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21209 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21210 		    flags);
21211 	}
21212 
21213 	/*
21214 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21215 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21216 	 * created.  Creating these broadcast IREs will only create confusion
21217 	 * as `addr' will be the same as the IP address.
21218 	 */
21219 	if (subnetmask != 0xFFFFFFFF) {
21220 		addr = ipif->ipif_subnet;
21221 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21222 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21223 		    irep, flags);
21224 	}
21225 
21226 	return (irep);
21227 }
21228 
21229 /*
21230  * Broadcast IRE info structure used in the functions below.  Since we
21231  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21232  */
21233 typedef struct bcast_ireinfo {
21234 	uchar_t		bi_type;	/* BCAST_* value from below */
21235 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21236 			bi_needrep:1,	/* do we need to replace it? */
21237 			bi_haverep:1,	/* have we replaced it? */
21238 			bi_pad:5;
21239 	ipaddr_t	bi_addr;	/* IRE address */
21240 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21241 } bcast_ireinfo_t;
21242 
21243 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21244 
21245 /*
21246  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21247  * return B_TRUE if it should immediately be used to recreate the IRE.
21248  */
21249 static boolean_t
21250 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21251 {
21252 	ipaddr_t addr;
21253 
21254 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21255 
21256 	switch (bireinfop->bi_type) {
21257 	case BCAST_NET:
21258 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21259 		if (addr != bireinfop->bi_addr)
21260 			return (B_FALSE);
21261 		break;
21262 	case BCAST_SUBNET:
21263 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21264 			return (B_FALSE);
21265 		break;
21266 	}
21267 
21268 	bireinfop->bi_needrep = 1;
21269 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21270 		if (bireinfop->bi_backup == NULL)
21271 			bireinfop->bi_backup = ipif;
21272 		return (B_FALSE);
21273 	}
21274 	return (B_TRUE);
21275 }
21276 
21277 /*
21278  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21279  * them ala ire_check_and_create_bcast().
21280  */
21281 static ire_t **
21282 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21283 {
21284 	ipaddr_t mask, addr;
21285 
21286 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21287 
21288 	addr = bireinfop->bi_addr;
21289 	irep = ire_create_bcast(ipif, addr, irep);
21290 
21291 	switch (bireinfop->bi_type) {
21292 	case BCAST_NET:
21293 		mask = ip_net_mask(ipif->ipif_subnet);
21294 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21295 		break;
21296 	case BCAST_SUBNET:
21297 		mask = ipif->ipif_net_mask;
21298 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21299 		break;
21300 	}
21301 
21302 	bireinfop->bi_haverep = 1;
21303 	return (irep);
21304 }
21305 
21306 /*
21307  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21308  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21309  * that are going away are still needed.  If so, have ipif_create_bcast()
21310  * recreate them (except for the deprecated case, as explained below).
21311  */
21312 static ire_t **
21313 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21314     ire_t **irep)
21315 {
21316 	int i;
21317 	ipif_t *ipif;
21318 
21319 	ASSERT(!ill->ill_isv6);
21320 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21321 		/*
21322 		 * Skip this ipif if it's (a) the one being taken down, (b)
21323 		 * not in the same zone, or (c) has no valid local address.
21324 		 */
21325 		if (ipif == test_ipif ||
21326 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21327 		    ipif->ipif_subnet == 0 ||
21328 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21329 		    (IPIF_UP|IPIF_BROADCAST))
21330 			continue;
21331 
21332 		/*
21333 		 * For each dying IRE that hasn't yet been replaced, see if
21334 		 * `ipif' needs it and whether the IRE should be recreated on
21335 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21336 		 * will return B_FALSE even if `ipif' needs the IRE on the
21337 		 * hopes that we'll later find a needy non-deprecated ipif.
21338 		 * However, the ipif is recorded in bi_backup for possible
21339 		 * subsequent use by ipif_check_bcast_ires().
21340 		 */
21341 		for (i = 0; i < BCAST_COUNT; i++) {
21342 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21343 				continue;
21344 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21345 				continue;
21346 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21347 		}
21348 
21349 		/*
21350 		 * If we've replaced all of the broadcast IREs that are going
21351 		 * to be taken down, we know we're done.
21352 		 */
21353 		for (i = 0; i < BCAST_COUNT; i++) {
21354 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21355 				break;
21356 		}
21357 		if (i == BCAST_COUNT)
21358 			break;
21359 	}
21360 	return (irep);
21361 }
21362 
21363 /*
21364  * Check if `test_ipif' (which is going away) is associated with any existing
21365  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21366  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21367  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21368  *
21369  * This is necessary because broadcast IREs are shared.  In particular, a
21370  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21371  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21372  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21373  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21374  * same zone, they will share the same set of broadcast IREs.
21375  *
21376  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21377  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21378  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21379  */
21380 static void
21381 ipif_check_bcast_ires(ipif_t *test_ipif)
21382 {
21383 	ill_t		*ill = test_ipif->ipif_ill;
21384 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21385 	ire_t		**irep1, **irep = &ire_array[0];
21386 	uint_t 		i, willdie;
21387 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21388 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21389 
21390 	ASSERT(!test_ipif->ipif_isv6);
21391 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21392 
21393 	/*
21394 	 * No broadcast IREs for the LOOPBACK interface
21395 	 * or others such as point to point and IPIF_NOXMIT.
21396 	 */
21397 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21398 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21399 		return;
21400 
21401 	bzero(bireinfo, sizeof (bireinfo));
21402 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21403 	bireinfo[0].bi_addr = 0;
21404 
21405 	bireinfo[1].bi_type = BCAST_ALLONES;
21406 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21407 
21408 	bireinfo[2].bi_type = BCAST_NET;
21409 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21410 
21411 	if (test_ipif->ipif_net_mask != 0)
21412 		mask = test_ipif->ipif_net_mask;
21413 	bireinfo[3].bi_type = BCAST_SUBNET;
21414 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21415 
21416 	/*
21417 	 * Figure out what (if any) broadcast IREs will die as a result of
21418 	 * `test_ipif' going away.  If none will die, we're done.
21419 	 */
21420 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21421 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21422 		    test_ipif, ALL_ZONES, NULL,
21423 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21424 		if (ire != NULL) {
21425 			willdie++;
21426 			bireinfo[i].bi_willdie = 1;
21427 			ire_refrele(ire);
21428 		}
21429 	}
21430 
21431 	if (willdie == 0)
21432 		return;
21433 
21434 	/*
21435 	 * Walk through all the ipifs that will be affected by the dying IREs,
21436 	 * and recreate the IREs as necessary.
21437 	 */
21438 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21439 
21440 	/*
21441 	 * Scan through the set of broadcast IREs and see if there are any
21442 	 * that we need to replace that have not yet been replaced.  If so,
21443 	 * replace them using the appropriate backup ipif.
21444 	 */
21445 	for (i = 0; i < BCAST_COUNT; i++) {
21446 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21447 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21448 			    &bireinfo[i], irep);
21449 	}
21450 
21451 	/*
21452 	 * If we can't create all of them, don't add any of them.  (Code in
21453 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21454 	 * non-loopback copy and loopback copy for a given address.)
21455 	 */
21456 	for (irep1 = irep; irep1 > ire_array; ) {
21457 		irep1--;
21458 		if (*irep1 == NULL) {
21459 			ip0dbg(("ipif_check_bcast_ires: can't create "
21460 			    "IRE_BROADCAST, memory allocation failure\n"));
21461 			while (irep > ire_array) {
21462 				irep--;
21463 				if (*irep != NULL)
21464 					ire_delete(*irep);
21465 			}
21466 			return;
21467 		}
21468 	}
21469 
21470 	for (irep1 = irep; irep1 > ire_array; ) {
21471 		irep1--;
21472 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21473 			ire_refrele(*irep1);		/* Held in ire_add */
21474 	}
21475 }
21476 
21477 /*
21478  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21479  * from lifr_flags and the name from lifr_name.
21480  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21481  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21482  * Returns EINPROGRESS when mp has been consumed by queueing it on
21483  * ill_pending_mp and the ioctl will complete in ip_rput.
21484  *
21485  * Can operate on either a module or a driver queue.
21486  * Returns an error if not a module queue.
21487  */
21488 /* ARGSUSED */
21489 int
21490 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21491     ip_ioctl_cmd_t *ipip, void *if_req)
21492 {
21493 	ill_t	*ill = q->q_ptr;
21494 	phyint_t *phyi;
21495 	ip_stack_t *ipst;
21496 	struct lifreq *lifr = if_req;
21497 
21498 	ASSERT(ipif != NULL);
21499 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21500 
21501 	if (q->q_next == NULL) {
21502 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21503 		return (EINVAL);
21504 	}
21505 
21506 	/*
21507 	 * If we are not writer on 'q' then this interface exists already
21508 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
21509 	 * so return EALREADY.
21510 	 */
21511 	if (ill != ipif->ipif_ill)
21512 		return (EALREADY);
21513 
21514 	if (ill->ill_name[0] != '\0')
21515 		return (EALREADY);
21516 
21517 	/*
21518 	 * Set all the flags. Allows all kinds of override. Provide some
21519 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21520 	 * unless there is either multicast/broadcast support in the driver
21521 	 * or it is a pt-pt link.
21522 	 */
21523 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21524 		/* Meaningless to IP thus don't allow them to be set. */
21525 		ip1dbg(("ip_setname: EINVAL 1\n"));
21526 		return (EINVAL);
21527 	}
21528 
21529 	/*
21530 	 * If there's another ill already with the requested name, ensure
21531 	 * that it's of the same type.	Otherwise, ill_phyint_reinit() will
21532 	 * fuse together two unrelated ills, which will cause chaos.
21533 	 */
21534 	ipst = ill->ill_ipst;
21535 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
21536 	    lifr->lifr_name, NULL);
21537 	if (phyi != NULL) {
21538 		ill_t *ill_mate = phyi->phyint_illv4;
21539 
21540 		if (ill_mate == NULL)
21541 			ill_mate = phyi->phyint_illv6;
21542 		ASSERT(ill_mate != NULL);
21543 
21544 		if (ill_mate->ill_media->ip_m_mac_type !=
21545 		    ill->ill_media->ip_m_mac_type) {
21546 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
21547 			    "use the same ill name on differing media\n"));
21548 			return (EINVAL);
21549 		}
21550 	}
21551 
21552 	/*
21553 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21554 	 * ill_bcast_addr_length info.
21555 	 */
21556 	if (!ill->ill_needs_attach &&
21557 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21558 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21559 	    ill->ill_bcast_addr_length == 0)) {
21560 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21561 		ip1dbg(("ip_setname: EINVAL 2\n"));
21562 		return (EINVAL);
21563 	}
21564 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21565 	    ((lifr->lifr_flags & IFF_IPV6) ||
21566 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21567 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21568 		ip1dbg(("ip_setname: EINVAL 3\n"));
21569 		return (EINVAL);
21570 	}
21571 	if (lifr->lifr_flags & IFF_UP) {
21572 		/* Can only be set with SIOCSLIFFLAGS */
21573 		ip1dbg(("ip_setname: EINVAL 4\n"));
21574 		return (EINVAL);
21575 	}
21576 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21577 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21578 		ip1dbg(("ip_setname: EINVAL 5\n"));
21579 		return (EINVAL);
21580 	}
21581 	/*
21582 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21583 	 */
21584 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21585 	    !(lifr->lifr_flags & IFF_IPV6) &&
21586 	    !(ipif->ipif_isv6)) {
21587 		ip1dbg(("ip_setname: EINVAL 6\n"));
21588 		return (EINVAL);
21589 	}
21590 
21591 	/*
21592 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21593 	 * we have all the flags here. So, we assign rather than we OR.
21594 	 * We can't OR the flags here because we don't want to set
21595 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21596 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21597 	 * on lifr_flags value here.
21598 	 */
21599 	/*
21600 	 * This ill has not been inserted into the global list.
21601 	 * So we are still single threaded and don't need any lock
21602 	 */
21603 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
21604 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21605 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21606 
21607 	/* We started off as V4. */
21608 	if (ill->ill_flags & ILLF_IPV6) {
21609 		ill->ill_phyint->phyint_illv6 = ill;
21610 		ill->ill_phyint->phyint_illv4 = NULL;
21611 	}
21612 
21613 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
21614 }
21615 
21616 /* ARGSUSED */
21617 int
21618 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21619     ip_ioctl_cmd_t *ipip, void *if_req)
21620 {
21621 	/*
21622 	 * ill_phyint_reinit merged the v4 and v6 into a single
21623 	 * ipsq. Could also have become part of a ipmp group in the
21624 	 * process, and we might not have been able to complete the
21625 	 * slifname in ipif_set_values, if we could not become
21626 	 * exclusive.  If so restart it here
21627 	 */
21628 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21629 }
21630 
21631 /*
21632  * Return a pointer to the ipif which matches the index, IP version type and
21633  * zoneid.
21634  */
21635 ipif_t *
21636 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21637     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21638 {
21639 	ill_t	*ill;
21640 	ipif_t	*ipif = NULL;
21641 
21642 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21643 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21644 
21645 	if (err != NULL)
21646 		*err = 0;
21647 
21648 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
21649 	if (ill != NULL) {
21650 		mutex_enter(&ill->ill_lock);
21651 		for (ipif = ill->ill_ipif; ipif != NULL;
21652 		    ipif = ipif->ipif_next) {
21653 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
21654 			    zoneid == ipif->ipif_zoneid ||
21655 			    ipif->ipif_zoneid == ALL_ZONES)) {
21656 				ipif_refhold_locked(ipif);
21657 				break;
21658 			}
21659 		}
21660 		mutex_exit(&ill->ill_lock);
21661 		ill_refrele(ill);
21662 		if (ipif == NULL && err != NULL)
21663 			*err = ENXIO;
21664 	}
21665 	return (ipif);
21666 }
21667 
21668 typedef struct conn_change_s {
21669 	uint_t cc_old_ifindex;
21670 	uint_t cc_new_ifindex;
21671 } conn_change_t;
21672 
21673 /*
21674  * ipcl_walk function for changing interface index.
21675  */
21676 static void
21677 conn_change_ifindex(conn_t *connp, caddr_t arg)
21678 {
21679 	conn_change_t *connc;
21680 	uint_t old_ifindex;
21681 	uint_t new_ifindex;
21682 	int i;
21683 	ilg_t *ilg;
21684 
21685 	connc = (conn_change_t *)arg;
21686 	old_ifindex = connc->cc_old_ifindex;
21687 	new_ifindex = connc->cc_new_ifindex;
21688 
21689 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21690 		connp->conn_orig_bound_ifindex = new_ifindex;
21691 
21692 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21693 		connp->conn_orig_multicast_ifindex = new_ifindex;
21694 
21695 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21696 		ilg = &connp->conn_ilg[i];
21697 		if (ilg->ilg_orig_ifindex == old_ifindex)
21698 			ilg->ilg_orig_ifindex = new_ifindex;
21699 	}
21700 }
21701 
21702 /*
21703  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21704  * to new_index if it matches the old_index.
21705  *
21706  * Failovers typically happen within a group of ills. But somebody
21707  * can remove an ill from the group after a failover happened. If
21708  * we are setting the ifindex after this, we potentially need to
21709  * look at all the ills rather than just the ones in the group.
21710  * We cut down the work by looking at matching ill_net_types
21711  * and ill_types as we could not possibly grouped them together.
21712  */
21713 static void
21714 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21715 {
21716 	ill_t *ill;
21717 	ipif_t *ipif;
21718 	uint_t old_ifindex;
21719 	uint_t new_ifindex;
21720 	ilm_t *ilm;
21721 	ill_walk_context_t ctx;
21722 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21723 
21724 	old_ifindex = connc->cc_old_ifindex;
21725 	new_ifindex = connc->cc_new_ifindex;
21726 
21727 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21728 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21729 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21730 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21731 		    (ill_orig->ill_type != ill->ill_type)) {
21732 			continue;
21733 		}
21734 		for (ipif = ill->ill_ipif; ipif != NULL;
21735 		    ipif = ipif->ipif_next) {
21736 			if (ipif->ipif_orig_ifindex == old_ifindex)
21737 				ipif->ipif_orig_ifindex = new_ifindex;
21738 		}
21739 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21740 			if (ilm->ilm_orig_ifindex == old_ifindex)
21741 				ilm->ilm_orig_ifindex = new_ifindex;
21742 		}
21743 	}
21744 	rw_exit(&ipst->ips_ill_g_lock);
21745 }
21746 
21747 /*
21748  * We first need to ensure that the new index is unique, and
21749  * then carry the change across both v4 and v6 ill representation
21750  * of the physical interface.
21751  */
21752 /* ARGSUSED */
21753 int
21754 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21755     ip_ioctl_cmd_t *ipip, void *ifreq)
21756 {
21757 	ill_t		*ill;
21758 	ill_t		*ill_other;
21759 	phyint_t	*phyi;
21760 	int		old_index;
21761 	conn_change_t	connc;
21762 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21763 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21764 	uint_t	index;
21765 	ill_t	*ill_v4;
21766 	ill_t	*ill_v6;
21767 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21768 
21769 	if (ipip->ipi_cmd_type == IF_CMD)
21770 		index = ifr->ifr_index;
21771 	else
21772 		index = lifr->lifr_index;
21773 
21774 	/*
21775 	 * Only allow on physical interface. Also, index zero is illegal.
21776 	 *
21777 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21778 	 *
21779 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21780 	 *    implies a possible failback might have to happen. As failback
21781 	 *    depends on the old index, we should fail setting the index.
21782 	 *
21783 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21784 	 *    any addresses or multicast memberships are failed over to
21785 	 *    a non-STANDBY interface. As failback depends on the old
21786 	 *    index, we should fail setting the index for this case also.
21787 	 *
21788 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21789 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21790 	 */
21791 	ill = ipif->ipif_ill;
21792 	phyi = ill->ill_phyint;
21793 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21794 	    ipif->ipif_id != 0 || index == 0) {
21795 		return (EINVAL);
21796 	}
21797 	old_index = phyi->phyint_ifindex;
21798 
21799 	/* If the index is not changing, no work to do */
21800 	if (old_index == index)
21801 		return (0);
21802 
21803 	/*
21804 	 * Use ill_lookup_on_ifindex to determine if the
21805 	 * new index is unused and if so allow the change.
21806 	 */
21807 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21808 	    ipst);
21809 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21810 	    ipst);
21811 	if (ill_v6 != NULL || ill_v4 != NULL) {
21812 		if (ill_v4 != NULL)
21813 			ill_refrele(ill_v4);
21814 		if (ill_v6 != NULL)
21815 			ill_refrele(ill_v6);
21816 		return (EBUSY);
21817 	}
21818 
21819 	/*
21820 	 * The new index is unused. Set it in the phyint.
21821 	 * Locate the other ill so that we can send a routing
21822 	 * sockets message.
21823 	 */
21824 	if (ill->ill_isv6) {
21825 		ill_other = phyi->phyint_illv4;
21826 	} else {
21827 		ill_other = phyi->phyint_illv6;
21828 	}
21829 
21830 	phyi->phyint_ifindex = index;
21831 
21832 	/* Update SCTP's ILL list */
21833 	sctp_ill_reindex(ill, old_index);
21834 
21835 	connc.cc_old_ifindex = old_index;
21836 	connc.cc_new_ifindex = index;
21837 	ip_change_ifindex(ill, &connc);
21838 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21839 
21840 	/* Send the routing sockets message */
21841 	ip_rts_ifmsg(ipif);
21842 	if (ill_other != NULL)
21843 		ip_rts_ifmsg(ill_other->ill_ipif);
21844 
21845 	return (0);
21846 }
21847 
21848 /* ARGSUSED */
21849 int
21850 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21851     ip_ioctl_cmd_t *ipip, void *ifreq)
21852 {
21853 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21854 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21855 
21856 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21857 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21858 	/* Get the interface index */
21859 	if (ipip->ipi_cmd_type == IF_CMD) {
21860 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21861 	} else {
21862 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21863 	}
21864 	return (0);
21865 }
21866 
21867 /* ARGSUSED */
21868 int
21869 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21870     ip_ioctl_cmd_t *ipip, void *ifreq)
21871 {
21872 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21873 
21874 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21875 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21876 	/* Get the interface zone */
21877 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21878 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21879 	return (0);
21880 }
21881 
21882 /*
21883  * Set the zoneid of an interface.
21884  */
21885 /* ARGSUSED */
21886 int
21887 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21888     ip_ioctl_cmd_t *ipip, void *ifreq)
21889 {
21890 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21891 	int err = 0;
21892 	boolean_t need_up = B_FALSE;
21893 	zone_t *zptr;
21894 	zone_status_t status;
21895 	zoneid_t zoneid;
21896 
21897 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21898 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21899 		if (!is_system_labeled())
21900 			return (ENOTSUP);
21901 		zoneid = GLOBAL_ZONEID;
21902 	}
21903 
21904 	/* cannot assign instance zero to a non-global zone */
21905 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
21906 		return (ENOTSUP);
21907 
21908 	/*
21909 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21910 	 * the event of a race with the zone shutdown processing, since IP
21911 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
21912 	 * interface will be cleaned up even if the zone is shut down
21913 	 * immediately after the status check. If the interface can't be brought
21914 	 * down right away, and the zone is shut down before the restart
21915 	 * function is called, we resolve the possible races by rechecking the
21916 	 * zone status in the restart function.
21917 	 */
21918 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
21919 		return (EINVAL);
21920 	status = zone_status_get(zptr);
21921 	zone_rele(zptr);
21922 
21923 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
21924 		return (EINVAL);
21925 
21926 	if (ipif->ipif_flags & IPIF_UP) {
21927 		/*
21928 		 * If the interface is already marked up,
21929 		 * we call ipif_down which will take care
21930 		 * of ditching any IREs that have been set
21931 		 * up based on the old interface address.
21932 		 */
21933 		err = ipif_logical_down(ipif, q, mp);
21934 		if (err == EINPROGRESS)
21935 			return (err);
21936 		ipif_down_tail(ipif);
21937 		need_up = B_TRUE;
21938 	}
21939 
21940 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
21941 	return (err);
21942 }
21943 
21944 static int
21945 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
21946     queue_t *q, mblk_t *mp, boolean_t need_up)
21947 {
21948 	int	err = 0;
21949 	ip_stack_t	*ipst;
21950 
21951 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
21952 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21953 
21954 	if (CONN_Q(q))
21955 		ipst = CONNQ_TO_IPST(q);
21956 	else
21957 		ipst = ILLQ_TO_IPST(q);
21958 
21959 	/*
21960 	 * For exclusive stacks we don't allow a different zoneid than
21961 	 * global.
21962 	 */
21963 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
21964 	    zoneid != GLOBAL_ZONEID)
21965 		return (EINVAL);
21966 
21967 	/* Set the new zone id. */
21968 	ipif->ipif_zoneid = zoneid;
21969 
21970 	/* Update sctp list */
21971 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
21972 
21973 	if (need_up) {
21974 		/*
21975 		 * Now bring the interface back up.  If this
21976 		 * is the only IPIF for the ILL, ipif_up
21977 		 * will have to re-bind to the device, so
21978 		 * we may get back EINPROGRESS, in which
21979 		 * case, this IOCTL will get completed in
21980 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
21981 		 */
21982 		err = ipif_up(ipif, q, mp);
21983 	}
21984 	return (err);
21985 }
21986 
21987 /* ARGSUSED */
21988 int
21989 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21990     ip_ioctl_cmd_t *ipip, void *if_req)
21991 {
21992 	struct lifreq *lifr = (struct lifreq *)if_req;
21993 	zoneid_t zoneid;
21994 	zone_t *zptr;
21995 	zone_status_t status;
21996 
21997 	ASSERT(ipif->ipif_id != 0);
21998 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21999 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22000 		zoneid = GLOBAL_ZONEID;
22001 
22002 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22003 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22004 
22005 	/*
22006 	 * We recheck the zone status to resolve the following race condition:
22007 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22008 	 * 2) hme0:1 is up and can't be brought down right away;
22009 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22010 	 * 3) zone "myzone" is halted; the zone status switches to
22011 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22012 	 * the interfaces to remove - hme0:1 is not returned because it's not
22013 	 * yet in "myzone", so it won't be removed;
22014 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22015 	 * status check here, we would have hme0:1 in "myzone" after it's been
22016 	 * destroyed.
22017 	 * Note that if the status check fails, we need to bring the interface
22018 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22019 	 * ipif_up_done[_v6]().
22020 	 */
22021 	status = ZONE_IS_UNINITIALIZED;
22022 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22023 		status = zone_status_get(zptr);
22024 		zone_rele(zptr);
22025 	}
22026 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22027 		if (ipif->ipif_isv6) {
22028 			(void) ipif_up_done_v6(ipif);
22029 		} else {
22030 			(void) ipif_up_done(ipif);
22031 		}
22032 		return (EINVAL);
22033 	}
22034 
22035 	ipif_down_tail(ipif);
22036 
22037 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22038 	    B_TRUE));
22039 }
22040 
22041 /* ARGSUSED */
22042 int
22043 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22044 	ip_ioctl_cmd_t *ipip, void *ifreq)
22045 {
22046 	struct lifreq	*lifr = ifreq;
22047 
22048 	ASSERT(q->q_next == NULL);
22049 	ASSERT(CONN_Q(q));
22050 
22051 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22052 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22053 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22054 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22055 
22056 	return (0);
22057 }
22058 
22059 /* Find the previous ILL in this usesrc group */
22060 static ill_t *
22061 ill_prev_usesrc(ill_t *uill)
22062 {
22063 	ill_t *ill;
22064 
22065 	for (ill = uill->ill_usesrc_grp_next;
22066 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22067 	    ill = ill->ill_usesrc_grp_next)
22068 		/* do nothing */;
22069 	return (ill);
22070 }
22071 
22072 /*
22073  * Release all members of the usesrc group. This routine is called
22074  * from ill_delete when the interface being unplumbed is the
22075  * group head.
22076  */
22077 static void
22078 ill_disband_usesrc_group(ill_t *uill)
22079 {
22080 	ill_t *next_ill, *tmp_ill;
22081 	ip_stack_t	*ipst = uill->ill_ipst;
22082 
22083 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22084 	next_ill = uill->ill_usesrc_grp_next;
22085 
22086 	do {
22087 		ASSERT(next_ill != NULL);
22088 		tmp_ill = next_ill->ill_usesrc_grp_next;
22089 		ASSERT(tmp_ill != NULL);
22090 		next_ill->ill_usesrc_grp_next = NULL;
22091 		next_ill->ill_usesrc_ifindex = 0;
22092 		next_ill = tmp_ill;
22093 	} while (next_ill->ill_usesrc_ifindex != 0);
22094 	uill->ill_usesrc_grp_next = NULL;
22095 }
22096 
22097 /*
22098  * Remove the client usesrc ILL from the list and relink to a new list
22099  */
22100 int
22101 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22102 {
22103 	ill_t *ill, *tmp_ill;
22104 	ip_stack_t	*ipst = ucill->ill_ipst;
22105 
22106 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22107 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22108 
22109 	/*
22110 	 * Check if the usesrc client ILL passed in is not already
22111 	 * in use as a usesrc ILL i.e one whose source address is
22112 	 * in use OR a usesrc ILL is not already in use as a usesrc
22113 	 * client ILL
22114 	 */
22115 	if ((ucill->ill_usesrc_ifindex == 0) ||
22116 	    (uill->ill_usesrc_ifindex != 0)) {
22117 		return (-1);
22118 	}
22119 
22120 	ill = ill_prev_usesrc(ucill);
22121 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22122 
22123 	/* Remove from the current list */
22124 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22125 		/* Only two elements in the list */
22126 		ASSERT(ill->ill_usesrc_ifindex == 0);
22127 		ill->ill_usesrc_grp_next = NULL;
22128 	} else {
22129 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22130 	}
22131 
22132 	if (ifindex == 0) {
22133 		ucill->ill_usesrc_ifindex = 0;
22134 		ucill->ill_usesrc_grp_next = NULL;
22135 		return (0);
22136 	}
22137 
22138 	ucill->ill_usesrc_ifindex = ifindex;
22139 	tmp_ill = uill->ill_usesrc_grp_next;
22140 	uill->ill_usesrc_grp_next = ucill;
22141 	ucill->ill_usesrc_grp_next =
22142 	    (tmp_ill != NULL) ? tmp_ill : uill;
22143 	return (0);
22144 }
22145 
22146 /*
22147  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22148  * ip.c for locking details.
22149  */
22150 /* ARGSUSED */
22151 int
22152 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22153     ip_ioctl_cmd_t *ipip, void *ifreq)
22154 {
22155 	struct lifreq *lifr = (struct lifreq *)ifreq;
22156 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22157 	    ill_flag_changed = B_FALSE;
22158 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22159 	int err = 0, ret;
22160 	uint_t ifindex;
22161 	phyint_t *us_phyint, *us_cli_phyint;
22162 	ipsq_t *ipsq = NULL;
22163 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22164 
22165 	ASSERT(IAM_WRITER_IPIF(ipif));
22166 	ASSERT(q->q_next == NULL);
22167 	ASSERT(CONN_Q(q));
22168 
22169 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22170 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22171 
22172 	ASSERT(us_cli_phyint != NULL);
22173 
22174 	/*
22175 	 * If the client ILL is being used for IPMP, abort.
22176 	 * Note, this can be done before ipsq_try_enter since we are already
22177 	 * exclusive on this ILL
22178 	 */
22179 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22180 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22181 		return (EINVAL);
22182 	}
22183 
22184 	ifindex = lifr->lifr_index;
22185 	if (ifindex == 0) {
22186 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22187 			/* non usesrc group interface, nothing to reset */
22188 			return (0);
22189 		}
22190 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22191 		/* valid reset request */
22192 		reset_flg = B_TRUE;
22193 	}
22194 
22195 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22196 	    ip_process_ioctl, &err, ipst);
22197 	if (usesrc_ill == NULL) {
22198 		return (err);
22199 	}
22200 
22201 	/*
22202 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22203 	 * group nor can either of the interfaces be used for standy. So
22204 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22205 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22206 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22207 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22208 	 * the usesrc_cli_ill
22209 	 */
22210 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22211 	    NEW_OP, B_TRUE);
22212 	if (ipsq == NULL) {
22213 		err = EINPROGRESS;
22214 		/* Operation enqueued on the ipsq of the usesrc ILL */
22215 		goto done;
22216 	}
22217 
22218 	/* Check if the usesrc_ill is used for IPMP */
22219 	us_phyint = usesrc_ill->ill_phyint;
22220 	if ((us_phyint->phyint_groupname != NULL) ||
22221 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22222 		err = EINVAL;
22223 		goto done;
22224 	}
22225 
22226 	/*
22227 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22228 	 * already a client then return EINVAL
22229 	 */
22230 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22231 		err = EINVAL;
22232 		goto done;
22233 	}
22234 
22235 	/*
22236 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22237 	 * be then this is a duplicate operation.
22238 	 */
22239 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22240 		err = 0;
22241 		goto done;
22242 	}
22243 
22244 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22245 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22246 	    usesrc_ill->ill_isv6));
22247 
22248 	/*
22249 	 * The next step ensures that no new ires will be created referencing
22250 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22251 	 * we go through an ire walk deleting all ire caches that reference
22252 	 * the client ill. New ires referencing the client ill that are added
22253 	 * to the ire table before the ILL_CHANGING flag is set, will be
22254 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22255 	 * the client ill while the ILL_CHANGING flag is set will be failed
22256 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22257 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22258 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22259 	 * belong to the same usesrc group.
22260 	 */
22261 	mutex_enter(&usesrc_cli_ill->ill_lock);
22262 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22263 	mutex_exit(&usesrc_cli_ill->ill_lock);
22264 	ill_flag_changed = B_TRUE;
22265 
22266 	if (ipif->ipif_isv6)
22267 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22268 		    ALL_ZONES, ipst);
22269 	else
22270 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22271 		    ALL_ZONES, ipst);
22272 
22273 	/*
22274 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22275 	 * and the ill_usesrc_ifindex fields
22276 	 */
22277 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22278 
22279 	if (reset_flg) {
22280 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22281 		if (ret != 0) {
22282 			err = EINVAL;
22283 		}
22284 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22285 		goto done;
22286 	}
22287 
22288 	/*
22289 	 * Four possibilities to consider:
22290 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22291 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22292 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22293 	 * 4. Both are part of their respective usesrc groups
22294 	 */
22295 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22296 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22297 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22298 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22299 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22300 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22301 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22302 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22303 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22304 		/* Insert at head of list */
22305 		usesrc_cli_ill->ill_usesrc_grp_next =
22306 		    usesrc_ill->ill_usesrc_grp_next;
22307 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22308 	} else {
22309 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22310 		    ifindex);
22311 		if (ret != 0)
22312 			err = EINVAL;
22313 	}
22314 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22315 
22316 done:
22317 	if (ill_flag_changed) {
22318 		mutex_enter(&usesrc_cli_ill->ill_lock);
22319 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22320 		mutex_exit(&usesrc_cli_ill->ill_lock);
22321 	}
22322 	if (ipsq != NULL)
22323 		ipsq_exit(ipsq);
22324 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22325 	ill_refrele(usesrc_ill);
22326 	return (err);
22327 }
22328 
22329 /*
22330  * comparison function used by avl.
22331  */
22332 static int
22333 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22334 {
22335 
22336 	uint_t index;
22337 
22338 	ASSERT(phyip != NULL && index_ptr != NULL);
22339 
22340 	index = *((uint_t *)index_ptr);
22341 	/*
22342 	 * let the phyint with the lowest index be on top.
22343 	 */
22344 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22345 		return (1);
22346 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22347 		return (-1);
22348 	return (0);
22349 }
22350 
22351 /*
22352  * comparison function used by avl.
22353  */
22354 static int
22355 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22356 {
22357 	ill_t *ill;
22358 	int res = 0;
22359 
22360 	ASSERT(phyip != NULL && name_ptr != NULL);
22361 
22362 	if (((phyint_t *)phyip)->phyint_illv4)
22363 		ill = ((phyint_t *)phyip)->phyint_illv4;
22364 	else
22365 		ill = ((phyint_t *)phyip)->phyint_illv6;
22366 	ASSERT(ill != NULL);
22367 
22368 	res = strcmp(ill->ill_name, (char *)name_ptr);
22369 	if (res > 0)
22370 		return (1);
22371 	else if (res < 0)
22372 		return (-1);
22373 	return (0);
22374 }
22375 /*
22376  * This function is called from ill_delete when the ill is being
22377  * unplumbed. We remove the reference from the phyint and we also
22378  * free the phyint when there are no more references to it.
22379  */
22380 static void
22381 ill_phyint_free(ill_t *ill)
22382 {
22383 	phyint_t *phyi;
22384 	phyint_t *next_phyint;
22385 	ipsq_t *cur_ipsq;
22386 	ip_stack_t	*ipst = ill->ill_ipst;
22387 
22388 	ASSERT(ill->ill_phyint != NULL);
22389 
22390 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22391 	phyi = ill->ill_phyint;
22392 	ill->ill_phyint = NULL;
22393 	/*
22394 	 * ill_init allocates a phyint always to store the copy
22395 	 * of flags relevant to phyint. At that point in time, we could
22396 	 * not assign the name and hence phyint_illv4/v6 could not be
22397 	 * initialized. Later in ipif_set_values, we assign the name to
22398 	 * the ill, at which point in time we assign phyint_illv4/v6.
22399 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22400 	 */
22401 	if (ill->ill_flags & ILLF_IPV6) {
22402 		phyi->phyint_illv6 = NULL;
22403 	} else {
22404 		phyi->phyint_illv4 = NULL;
22405 	}
22406 	/*
22407 	 * ipif_down removes it from the group when the last ipif goes
22408 	 * down.
22409 	 */
22410 	ASSERT(ill->ill_group == NULL);
22411 
22412 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22413 		return;
22414 
22415 	/*
22416 	 * Make sure this phyint was put in the list.
22417 	 */
22418 	if (phyi->phyint_ifindex > 0) {
22419 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22420 		    phyi);
22421 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22422 		    phyi);
22423 	}
22424 	/*
22425 	 * remove phyint from the ipsq list.
22426 	 */
22427 	cur_ipsq = phyi->phyint_ipsq;
22428 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22429 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22430 	} else {
22431 		next_phyint = cur_ipsq->ipsq_phyint_list;
22432 		while (next_phyint != NULL) {
22433 			if (next_phyint->phyint_ipsq_next == phyi) {
22434 				next_phyint->phyint_ipsq_next =
22435 				    phyi->phyint_ipsq_next;
22436 				break;
22437 			}
22438 			next_phyint = next_phyint->phyint_ipsq_next;
22439 		}
22440 		ASSERT(next_phyint != NULL);
22441 	}
22442 	IPSQ_DEC_REF(cur_ipsq, ipst);
22443 
22444 	if (phyi->phyint_groupname_len != 0) {
22445 		ASSERT(phyi->phyint_groupname != NULL);
22446 		mi_free(phyi->phyint_groupname);
22447 	}
22448 	mi_free(phyi);
22449 }
22450 
22451 /*
22452  * Attach the ill to the phyint structure which can be shared by both
22453  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22454  * function is called from ipif_set_values and ill_lookup_on_name (for
22455  * loopback) where we know the name of the ill. We lookup the ill and if
22456  * there is one present already with the name use that phyint. Otherwise
22457  * reuse the one allocated by ill_init.
22458  */
22459 static void
22460 ill_phyint_reinit(ill_t *ill)
22461 {
22462 	boolean_t isv6 = ill->ill_isv6;
22463 	phyint_t *phyi_old;
22464 	phyint_t *phyi;
22465 	avl_index_t where = 0;
22466 	ill_t	*ill_other = NULL;
22467 	ipsq_t	*ipsq;
22468 	ip_stack_t	*ipst = ill->ill_ipst;
22469 
22470 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22471 
22472 	phyi_old = ill->ill_phyint;
22473 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22474 	    phyi_old->phyint_illv6 == NULL));
22475 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22476 	    phyi_old->phyint_illv4 == NULL));
22477 	ASSERT(phyi_old->phyint_ifindex == 0);
22478 
22479 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22480 	    ill->ill_name, &where);
22481 
22482 	/*
22483 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22484 	 *    the global list of ills. So no other thread could have located
22485 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22486 	 * 2. Now locate the other protocol instance of this ill.
22487 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22488 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22489 	 *    of neither ill can change.
22490 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22491 	 *    other ill.
22492 	 * 5. Release all locks.
22493 	 */
22494 
22495 	/*
22496 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22497 	 * we are initializing IPv4.
22498 	 */
22499 	if (phyi != NULL) {
22500 		ill_other = (isv6) ? phyi->phyint_illv4 :
22501 		    phyi->phyint_illv6;
22502 		ASSERT(ill_other->ill_phyint != NULL);
22503 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22504 		    (!isv6 && ill_other->ill_isv6));
22505 		GRAB_ILL_LOCKS(ill, ill_other);
22506 		/*
22507 		 * We are potentially throwing away phyint_flags which
22508 		 * could be different from the one that we obtain from
22509 		 * ill_other->ill_phyint. But it is okay as we are assuming
22510 		 * that the state maintained within IP is correct.
22511 		 */
22512 		mutex_enter(&phyi->phyint_lock);
22513 		if (isv6) {
22514 			ASSERT(phyi->phyint_illv6 == NULL);
22515 			phyi->phyint_illv6 = ill;
22516 		} else {
22517 			ASSERT(phyi->phyint_illv4 == NULL);
22518 			phyi->phyint_illv4 = ill;
22519 		}
22520 		/*
22521 		 * This is a new ill, currently undergoing SLIFNAME
22522 		 * So we could not have joined an IPMP group until now.
22523 		 */
22524 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22525 		    phyi_old->phyint_groupname == NULL);
22526 
22527 		/*
22528 		 * This phyi_old is going away. Decref ipsq_refs and
22529 		 * assert it is zero. The ipsq itself will be freed in
22530 		 * ipsq_exit
22531 		 */
22532 		ipsq = phyi_old->phyint_ipsq;
22533 		IPSQ_DEC_REF(ipsq, ipst);
22534 		ASSERT(ipsq->ipsq_refs == 0);
22535 		/* Get the singleton phyint out of the ipsq list */
22536 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22537 		ipsq->ipsq_phyint_list = NULL;
22538 		phyi_old->phyint_illv4 = NULL;
22539 		phyi_old->phyint_illv6 = NULL;
22540 		mi_free(phyi_old);
22541 	} else {
22542 		mutex_enter(&ill->ill_lock);
22543 		/*
22544 		 * We don't need to acquire any lock, since
22545 		 * the ill is not yet visible globally  and we
22546 		 * have not yet released the ill_g_lock.
22547 		 */
22548 		phyi = phyi_old;
22549 		mutex_enter(&phyi->phyint_lock);
22550 		/* XXX We need a recovery strategy here. */
22551 		if (!phyint_assign_ifindex(phyi, ipst))
22552 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22553 
22554 		/* No IPMP group yet, thus the hook uses the ifindex */
22555 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22556 
22557 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22558 		    (void *)phyi, where);
22559 
22560 		(void) avl_find(&ipst->ips_phyint_g_list->
22561 		    phyint_list_avl_by_index,
22562 		    &phyi->phyint_ifindex, &where);
22563 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22564 		    (void *)phyi, where);
22565 	}
22566 
22567 	/*
22568 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22569 	 * pending mp is not affected because that is per ill basis.
22570 	 */
22571 	ill->ill_phyint = phyi;
22572 
22573 	/*
22574 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22575 	 * We do this here as when the first ipif was allocated,
22576 	 * ipif_allocate does not know the right interface index.
22577 	 */
22578 
22579 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22580 	/*
22581 	 * Now that the phyint's ifindex has been assigned, complete the
22582 	 * remaining
22583 	 */
22584 
22585 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22586 	if (ill->ill_isv6) {
22587 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22588 		    ill->ill_phyint->phyint_ifindex;
22589 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22590 	} else {
22591 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22592 	}
22593 
22594 	/*
22595 	 * Generate an event within the hooks framework to indicate that
22596 	 * a new interface has just been added to IP.  For this event to
22597 	 * be generated, the network interface must, at least, have an
22598 	 * ifindex assigned to it.
22599 	 *
22600 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22601 	 * that the ordering of delivered events to listeners matches the
22602 	 * order of them in the kernel.
22603 	 *
22604 	 * This function could be called from ill_lookup_on_name. In that case
22605 	 * the interface is loopback "lo", which will not generate a NIC event.
22606 	 */
22607 	if (ill->ill_name_length <= 2 ||
22608 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22609 		/*
22610 		 * Generate nic plumb event for ill_name even if
22611 		 * ipmp_hook_emulation is set. That avoids generating events
22612 		 * for the ill_names should ipmp_hook_emulation be turned on
22613 		 * later.
22614 		 */
22615 		ill_nic_event_plumb(ill, B_FALSE);
22616 	}
22617 	RELEASE_ILL_LOCKS(ill, ill_other);
22618 	mutex_exit(&phyi->phyint_lock);
22619 }
22620 
22621 /*
22622  * Allocate a NE_PLUMB nic info event and store in the ill.
22623  * If 'group' is set we do it for the group name, otherwise the ill name.
22624  * It will be sent when we leave the ipsq.
22625  */
22626 void
22627 ill_nic_event_plumb(ill_t *ill, boolean_t group)
22628 {
22629 	phyint_t	*phyi = ill->ill_phyint;
22630 	char		*name;
22631 	int		namelen;
22632 
22633 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22634 
22635 	if (group) {
22636 		ASSERT(phyi->phyint_groupname_len != 0);
22637 		namelen = phyi->phyint_groupname_len;
22638 		name = phyi->phyint_groupname;
22639 	} else {
22640 		namelen = ill->ill_name_length;
22641 		name = ill->ill_name;
22642 	}
22643 
22644 	ill_nic_event_dispatch(ill, 0, NE_PLUMB, name, namelen);
22645 }
22646 
22647 /*
22648  * Notify any downstream modules of the name of this interface.
22649  * An M_IOCTL is used even though we don't expect a successful reply.
22650  * Any reply message from the driver (presumably an M_IOCNAK) will
22651  * eventually get discarded somewhere upstream.  The message format is
22652  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22653  * to IP.
22654  */
22655 static void
22656 ip_ifname_notify(ill_t *ill, queue_t *q)
22657 {
22658 	mblk_t *mp1, *mp2;
22659 	struct iocblk *iocp;
22660 	struct lifreq *lifr;
22661 
22662 	mp1 = mkiocb(SIOCSLIFNAME);
22663 	if (mp1 == NULL)
22664 		return;
22665 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22666 	if (mp2 == NULL) {
22667 		freeb(mp1);
22668 		return;
22669 	}
22670 
22671 	mp1->b_cont = mp2;
22672 	iocp = (struct iocblk *)mp1->b_rptr;
22673 	iocp->ioc_count = sizeof (struct lifreq);
22674 
22675 	lifr = (struct lifreq *)mp2->b_rptr;
22676 	mp2->b_wptr += sizeof (struct lifreq);
22677 	bzero(lifr, sizeof (struct lifreq));
22678 
22679 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22680 	lifr->lifr_ppa = ill->ill_ppa;
22681 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22682 
22683 	putnext(q, mp1);
22684 }
22685 
22686 static int
22687 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22688 {
22689 	int err;
22690 	ip_stack_t	*ipst = ill->ill_ipst;
22691 
22692 	/* Set the obsolete NDD per-interface forwarding name. */
22693 	err = ill_set_ndd_name(ill);
22694 	if (err != 0) {
22695 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22696 		    err);
22697 	}
22698 
22699 	/* Tell downstream modules where they are. */
22700 	ip_ifname_notify(ill, q);
22701 
22702 	/*
22703 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22704 	 * Error cases are ENOMEM ...
22705 	 */
22706 	err = ill_dl_phys(ill, ipif, mp, q);
22707 
22708 	/*
22709 	 * If there is no IRE expiration timer running, get one started.
22710 	 * igmp and mld timers will be triggered by the first multicast
22711 	 */
22712 	if (ipst->ips_ip_ire_expire_id == 0) {
22713 		/*
22714 		 * acquire the lock and check again.
22715 		 */
22716 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22717 		if (ipst->ips_ip_ire_expire_id == 0) {
22718 			ipst->ips_ip_ire_expire_id = timeout(
22719 			    ip_trash_timer_expire, ipst,
22720 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22721 		}
22722 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22723 	}
22724 
22725 	if (ill->ill_isv6) {
22726 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22727 		if (ipst->ips_mld_slowtimeout_id == 0) {
22728 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22729 			    (void *)ipst,
22730 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22731 		}
22732 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22733 	} else {
22734 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22735 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22736 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22737 			    (void *)ipst,
22738 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22739 		}
22740 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22741 	}
22742 
22743 	return (err);
22744 }
22745 
22746 /*
22747  * Common routine for ppa and ifname setting. Should be called exclusive.
22748  *
22749  * Returns EINPROGRESS when mp has been consumed by queueing it on
22750  * ill_pending_mp and the ioctl will complete in ip_rput.
22751  *
22752  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22753  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22754  * For SLIFNAME, we pass these values back to the userland.
22755  */
22756 static int
22757 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22758 {
22759 	ill_t	*ill;
22760 	ipif_t	*ipif;
22761 	ipsq_t	*ipsq;
22762 	char	*ppa_ptr;
22763 	char	*old_ptr;
22764 	char	old_char;
22765 	int	error;
22766 	ip_stack_t	*ipst;
22767 
22768 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22769 	ASSERT(q->q_next != NULL);
22770 	ASSERT(interf_name != NULL);
22771 
22772 	ill = (ill_t *)q->q_ptr;
22773 	ipst = ill->ill_ipst;
22774 
22775 	ASSERT(ill->ill_ipst != NULL);
22776 	ASSERT(ill->ill_name[0] == '\0');
22777 	ASSERT(IAM_WRITER_ILL(ill));
22778 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22779 	ASSERT(ill->ill_ppa == UINT_MAX);
22780 
22781 	/* The ppa is sent down by ifconfig or is chosen */
22782 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22783 		return (EINVAL);
22784 	}
22785 
22786 	/*
22787 	 * make sure ppa passed in is same as ppa in the name.
22788 	 * This check is not made when ppa == UINT_MAX in that case ppa
22789 	 * in the name could be anything. System will choose a ppa and
22790 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22791 	 */
22792 	if (*new_ppa_ptr != UINT_MAX) {
22793 		/* stoi changes the pointer */
22794 		old_ptr = ppa_ptr;
22795 		/*
22796 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22797 		 * (they don't have an externally visible ppa).  We assign one
22798 		 * here so that we can manage the interface.  Note that in
22799 		 * the past this value was always 0 for DLPI 1 drivers.
22800 		 */
22801 		if (*new_ppa_ptr == 0)
22802 			*new_ppa_ptr = stoi(&old_ptr);
22803 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22804 			return (EINVAL);
22805 	}
22806 	/*
22807 	 * terminate string before ppa
22808 	 * save char at that location.
22809 	 */
22810 	old_char = ppa_ptr[0];
22811 	ppa_ptr[0] = '\0';
22812 
22813 	ill->ill_ppa = *new_ppa_ptr;
22814 	/*
22815 	 * Finish as much work now as possible before calling ill_glist_insert
22816 	 * which makes the ill globally visible and also merges it with the
22817 	 * other protocol instance of this phyint. The remaining work is
22818 	 * done after entering the ipsq which may happen sometime later.
22819 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22820 	 */
22821 	ipif = ill->ill_ipif;
22822 
22823 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22824 	ipif_assign_seqid(ipif);
22825 
22826 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22827 		ill->ill_flags |= ILLF_IPV4;
22828 
22829 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22830 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22831 
22832 	if (ill->ill_flags & ILLF_IPV6) {
22833 
22834 		ill->ill_isv6 = B_TRUE;
22835 		if (ill->ill_rq != NULL) {
22836 			ill->ill_rq->q_qinfo = &iprinitv6;
22837 			ill->ill_wq->q_qinfo = &ipwinitv6;
22838 		}
22839 
22840 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22841 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22842 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22843 		ipif->ipif_v6subnet = ipv6_all_zeros;
22844 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22845 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22846 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22847 		/*
22848 		 * point-to-point or Non-mulicast capable
22849 		 * interfaces won't do NUD unless explicitly
22850 		 * configured to do so.
22851 		 */
22852 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22853 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22854 			ill->ill_flags |= ILLF_NONUD;
22855 		}
22856 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22857 		if (ill->ill_flags & ILLF_NOARP) {
22858 			/*
22859 			 * Note: xresolv interfaces will eventually need
22860 			 * NOARP set here as well, but that will require
22861 			 * those external resolvers to have some
22862 			 * knowledge of that flag and act appropriately.
22863 			 * Not to be changed at present.
22864 			 */
22865 			ill->ill_flags &= ~ILLF_NOARP;
22866 		}
22867 		/*
22868 		 * Set the ILLF_ROUTER flag according to the global
22869 		 * IPv6 forwarding policy.
22870 		 */
22871 		if (ipst->ips_ipv6_forward != 0)
22872 			ill->ill_flags |= ILLF_ROUTER;
22873 	} else if (ill->ill_flags & ILLF_IPV4) {
22874 		ill->ill_isv6 = B_FALSE;
22875 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
22876 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
22877 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
22878 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
22879 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
22880 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
22881 		/*
22882 		 * Set the ILLF_ROUTER flag according to the global
22883 		 * IPv4 forwarding policy.
22884 		 */
22885 		if (ipst->ips_ip_g_forward != 0)
22886 			ill->ill_flags |= ILLF_ROUTER;
22887 	}
22888 
22889 	ASSERT(ill->ill_phyint != NULL);
22890 
22891 	/*
22892 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
22893 	 * be completed in ill_glist_insert -> ill_phyint_reinit
22894 	 */
22895 	if (!ill_allocate_mibs(ill))
22896 		return (ENOMEM);
22897 
22898 	/*
22899 	 * Pick a default sap until we get the DL_INFO_ACK back from
22900 	 * the driver.
22901 	 */
22902 	if (ill->ill_sap == 0) {
22903 		if (ill->ill_isv6)
22904 			ill->ill_sap = IP6_DL_SAP;
22905 		else
22906 			ill->ill_sap = IP_DL_SAP;
22907 	}
22908 
22909 	ill->ill_ifname_pending = 1;
22910 	ill->ill_ifname_pending_err = 0;
22911 
22912 	/*
22913 	 * When the first ipif comes up in ipif_up_done(), multicast groups
22914 	 * that were joined while this ill was not bound to the DLPI link need
22915 	 * to be recovered by ill_recover_multicast().
22916 	 */
22917 	ill->ill_need_recover_multicast = 1;
22918 
22919 	ill_refhold(ill);
22920 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
22921 	if ((error = ill_glist_insert(ill, interf_name,
22922 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
22923 		ill->ill_ppa = UINT_MAX;
22924 		ill->ill_name[0] = '\0';
22925 		/*
22926 		 * undo null termination done above.
22927 		 */
22928 		ppa_ptr[0] = old_char;
22929 		rw_exit(&ipst->ips_ill_g_lock);
22930 		ill_refrele(ill);
22931 		return (error);
22932 	}
22933 
22934 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
22935 
22936 	/*
22937 	 * When we return the buffer pointed to by interf_name should contain
22938 	 * the same name as in ill_name.
22939 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
22940 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
22941 	 * so copy full name and update the ppa ptr.
22942 	 * When ppa passed in != UINT_MAX all values are correct just undo
22943 	 * null termination, this saves a bcopy.
22944 	 */
22945 	if (*new_ppa_ptr == UINT_MAX) {
22946 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
22947 		*new_ppa_ptr = ill->ill_ppa;
22948 	} else {
22949 		/*
22950 		 * undo null termination done above.
22951 		 */
22952 		ppa_ptr[0] = old_char;
22953 	}
22954 
22955 	/* Let SCTP know about this ILL */
22956 	sctp_update_ill(ill, SCTP_ILL_INSERT);
22957 
22958 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
22959 	    B_TRUE);
22960 
22961 	rw_exit(&ipst->ips_ill_g_lock);
22962 	ill_refrele(ill);
22963 	if (ipsq == NULL)
22964 		return (EINPROGRESS);
22965 
22966 	/*
22967 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
22968 	 */
22969 	if (ipsq->ipsq_current_ipif == NULL)
22970 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
22971 	else
22972 		ASSERT(ipsq->ipsq_current_ipif == ipif);
22973 
22974 	error = ipif_set_values_tail(ill, ipif, mp, q);
22975 	ipsq_exit(ipsq);
22976 	if (error != 0 && error != EINPROGRESS) {
22977 		/*
22978 		 * restore previous values
22979 		 */
22980 		ill->ill_isv6 = B_FALSE;
22981 	}
22982 	return (error);
22983 }
22984 
22985 
22986 void
22987 ipif_init(ip_stack_t *ipst)
22988 {
22989 	hrtime_t hrt;
22990 	int i;
22991 
22992 	/*
22993 	 * Can't call drv_getparm here as it is too early in the boot.
22994 	 * As we use ipif_src_random just for picking a different
22995 	 * source address everytime, this need not be really random.
22996 	 */
22997 	hrt = gethrtime();
22998 	ipst->ips_ipif_src_random =
22999 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23000 
23001 	for (i = 0; i < MAX_G_HEADS; i++) {
23002 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23003 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23004 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23005 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23006 	}
23007 
23008 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23009 	    ill_phyint_compare_index,
23010 	    sizeof (phyint_t),
23011 	    offsetof(struct phyint, phyint_avl_by_index));
23012 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23013 	    ill_phyint_compare_name,
23014 	    sizeof (phyint_t),
23015 	    offsetof(struct phyint, phyint_avl_by_name));
23016 }
23017 
23018 /*
23019  * Lookup the ipif corresponding to the onlink destination address. For
23020  * point-to-point interfaces, it matches with remote endpoint destination
23021  * address. For point-to-multipoint interfaces it only tries to match the
23022  * destination with the interface's subnet address. The longest, most specific
23023  * match is found to take care of such rare network configurations like -
23024  * le0: 129.146.1.1/16
23025  * le1: 129.146.2.2/24
23026  * It is used only by SO_DONTROUTE at the moment.
23027  */
23028 ipif_t *
23029 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23030 {
23031 	ipif_t	*ipif, *best_ipif;
23032 	ill_t	*ill;
23033 	ill_walk_context_t ctx;
23034 
23035 	ASSERT(zoneid != ALL_ZONES);
23036 	best_ipif = NULL;
23037 
23038 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23039 	ill = ILL_START_WALK_V4(&ctx, ipst);
23040 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23041 		mutex_enter(&ill->ill_lock);
23042 		for (ipif = ill->ill_ipif; ipif != NULL;
23043 		    ipif = ipif->ipif_next) {
23044 			if (!IPIF_CAN_LOOKUP(ipif))
23045 				continue;
23046 			if (ipif->ipif_zoneid != zoneid &&
23047 			    ipif->ipif_zoneid != ALL_ZONES)
23048 				continue;
23049 			/*
23050 			 * Point-to-point case. Look for exact match with
23051 			 * destination address.
23052 			 */
23053 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23054 				if (ipif->ipif_pp_dst_addr == addr) {
23055 					ipif_refhold_locked(ipif);
23056 					mutex_exit(&ill->ill_lock);
23057 					rw_exit(&ipst->ips_ill_g_lock);
23058 					if (best_ipif != NULL)
23059 						ipif_refrele(best_ipif);
23060 					return (ipif);
23061 				}
23062 			} else if (ipif->ipif_subnet == (addr &
23063 			    ipif->ipif_net_mask)) {
23064 				/*
23065 				 * Point-to-multipoint case. Looping through to
23066 				 * find the most specific match. If there are
23067 				 * multiple best match ipif's then prefer ipif's
23068 				 * that are UP. If there is only one best match
23069 				 * ipif and it is DOWN we must still return it.
23070 				 */
23071 				if ((best_ipif == NULL) ||
23072 				    (ipif->ipif_net_mask >
23073 				    best_ipif->ipif_net_mask) ||
23074 				    ((ipif->ipif_net_mask ==
23075 				    best_ipif->ipif_net_mask) &&
23076 				    ((ipif->ipif_flags & IPIF_UP) &&
23077 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23078 					ipif_refhold_locked(ipif);
23079 					mutex_exit(&ill->ill_lock);
23080 					rw_exit(&ipst->ips_ill_g_lock);
23081 					if (best_ipif != NULL)
23082 						ipif_refrele(best_ipif);
23083 					best_ipif = ipif;
23084 					rw_enter(&ipst->ips_ill_g_lock,
23085 					    RW_READER);
23086 					mutex_enter(&ill->ill_lock);
23087 				}
23088 			}
23089 		}
23090 		mutex_exit(&ill->ill_lock);
23091 	}
23092 	rw_exit(&ipst->ips_ill_g_lock);
23093 	return (best_ipif);
23094 }
23095 
23096 /*
23097  * Save enough information so that we can recreate the IRE if
23098  * the interface goes down and then up.
23099  */
23100 static void
23101 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23102 {
23103 	mblk_t	*save_mp;
23104 
23105 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23106 	if (save_mp != NULL) {
23107 		ifrt_t	*ifrt;
23108 
23109 		save_mp->b_wptr += sizeof (ifrt_t);
23110 		ifrt = (ifrt_t *)save_mp->b_rptr;
23111 		bzero(ifrt, sizeof (ifrt_t));
23112 		ifrt->ifrt_type = ire->ire_type;
23113 		ifrt->ifrt_addr = ire->ire_addr;
23114 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23115 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23116 		ifrt->ifrt_mask = ire->ire_mask;
23117 		ifrt->ifrt_flags = ire->ire_flags;
23118 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23119 		mutex_enter(&ipif->ipif_saved_ire_lock);
23120 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23121 		ipif->ipif_saved_ire_mp = save_mp;
23122 		ipif->ipif_saved_ire_cnt++;
23123 		mutex_exit(&ipif->ipif_saved_ire_lock);
23124 	}
23125 }
23126 
23127 static void
23128 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23129 {
23130 	mblk_t	**mpp;
23131 	mblk_t	*mp;
23132 	ifrt_t	*ifrt;
23133 
23134 	/* Remove from ipif_saved_ire_mp list if it is there */
23135 	mutex_enter(&ipif->ipif_saved_ire_lock);
23136 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23137 	    mpp = &(*mpp)->b_cont) {
23138 		/*
23139 		 * On a given ipif, the triple of address, gateway and
23140 		 * mask is unique for each saved IRE (in the case of
23141 		 * ordinary interface routes, the gateway address is
23142 		 * all-zeroes).
23143 		 */
23144 		mp = *mpp;
23145 		ifrt = (ifrt_t *)mp->b_rptr;
23146 		if (ifrt->ifrt_addr == ire->ire_addr &&
23147 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23148 		    ifrt->ifrt_mask == ire->ire_mask) {
23149 			*mpp = mp->b_cont;
23150 			ipif->ipif_saved_ire_cnt--;
23151 			freeb(mp);
23152 			break;
23153 		}
23154 	}
23155 	mutex_exit(&ipif->ipif_saved_ire_lock);
23156 }
23157 
23158 /*
23159  * IP multirouting broadcast routes handling
23160  * Append CGTP broadcast IREs to regular ones created
23161  * at ifconfig time.
23162  */
23163 static void
23164 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23165 {
23166 	ire_t *ire_prim;
23167 
23168 	ASSERT(ire != NULL);
23169 	ASSERT(ire_dst != NULL);
23170 
23171 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23172 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23173 	if (ire_prim != NULL) {
23174 		/*
23175 		 * We are in the special case of broadcasts for
23176 		 * CGTP. We add an IRE_BROADCAST that holds
23177 		 * the RTF_MULTIRT flag, the destination
23178 		 * address of ire_dst and the low level
23179 		 * info of ire_prim. In other words, CGTP
23180 		 * broadcast is added to the redundant ipif.
23181 		 */
23182 		ipif_t *ipif_prim;
23183 		ire_t  *bcast_ire;
23184 
23185 		ipif_prim = ire_prim->ire_ipif;
23186 
23187 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23188 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23189 		    (void *)ire_dst, (void *)ire_prim,
23190 		    (void *)ipif_prim));
23191 
23192 		bcast_ire = ire_create(
23193 		    (uchar_t *)&ire->ire_addr,
23194 		    (uchar_t *)&ip_g_all_ones,
23195 		    (uchar_t *)&ire_dst->ire_src_addr,
23196 		    (uchar_t *)&ire->ire_gateway_addr,
23197 		    &ipif_prim->ipif_mtu,
23198 		    NULL,
23199 		    ipif_prim->ipif_rq,
23200 		    ipif_prim->ipif_wq,
23201 		    IRE_BROADCAST,
23202 		    ipif_prim,
23203 		    0,
23204 		    0,
23205 		    0,
23206 		    ire->ire_flags,
23207 		    &ire_uinfo_null,
23208 		    NULL,
23209 		    NULL,
23210 		    ipst);
23211 
23212 		if (bcast_ire != NULL) {
23213 
23214 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23215 			    B_FALSE) == 0) {
23216 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23217 				    "added bcast_ire %p\n",
23218 				    (void *)bcast_ire));
23219 
23220 				ipif_save_ire(bcast_ire->ire_ipif,
23221 				    bcast_ire);
23222 				ire_refrele(bcast_ire);
23223 			}
23224 		}
23225 		ire_refrele(ire_prim);
23226 	}
23227 }
23228 
23229 
23230 /*
23231  * IP multirouting broadcast routes handling
23232  * Remove the broadcast ire
23233  */
23234 static void
23235 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23236 {
23237 	ire_t *ire_dst;
23238 
23239 	ASSERT(ire != NULL);
23240 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23241 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23242 	if (ire_dst != NULL) {
23243 		ire_t *ire_prim;
23244 
23245 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23246 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23247 		if (ire_prim != NULL) {
23248 			ipif_t *ipif_prim;
23249 			ire_t  *bcast_ire;
23250 
23251 			ipif_prim = ire_prim->ire_ipif;
23252 
23253 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23254 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23255 			    (void *)ire_dst, (void *)ire_prim,
23256 			    (void *)ipif_prim));
23257 
23258 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23259 			    ire->ire_gateway_addr,
23260 			    IRE_BROADCAST,
23261 			    ipif_prim, ALL_ZONES,
23262 			    NULL,
23263 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23264 			    MATCH_IRE_MASK, ipst);
23265 
23266 			if (bcast_ire != NULL) {
23267 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23268 				    "looked up bcast_ire %p\n",
23269 				    (void *)bcast_ire));
23270 				ipif_remove_ire(bcast_ire->ire_ipif,
23271 				    bcast_ire);
23272 				ire_delete(bcast_ire);
23273 				ire_refrele(bcast_ire);
23274 			}
23275 			ire_refrele(ire_prim);
23276 		}
23277 		ire_refrele(ire_dst);
23278 	}
23279 }
23280 
23281 /*
23282  * IPsec hardware acceleration capabilities related functions.
23283  */
23284 
23285 /*
23286  * Free a per-ill IPsec capabilities structure.
23287  */
23288 static void
23289 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23290 {
23291 	if (capab->auth_hw_algs != NULL)
23292 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23293 	if (capab->encr_hw_algs != NULL)
23294 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23295 	if (capab->encr_algparm != NULL)
23296 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23297 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23298 }
23299 
23300 /*
23301  * Allocate a new per-ill IPsec capabilities structure. This structure
23302  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23303  * an array which specifies, for each algorithm, whether this algorithm
23304  * is supported by the ill or not.
23305  */
23306 static ill_ipsec_capab_t *
23307 ill_ipsec_capab_alloc(void)
23308 {
23309 	ill_ipsec_capab_t *capab;
23310 	uint_t nelems;
23311 
23312 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23313 	if (capab == NULL)
23314 		return (NULL);
23315 
23316 	/* we need one bit per algorithm */
23317 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23318 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23319 
23320 	/* allocate memory to store algorithm flags */
23321 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23322 	if (capab->encr_hw_algs == NULL)
23323 		goto nomem;
23324 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23325 	if (capab->auth_hw_algs == NULL)
23326 		goto nomem;
23327 	/*
23328 	 * Leave encr_algparm NULL for now since we won't need it half
23329 	 * the time
23330 	 */
23331 	return (capab);
23332 
23333 nomem:
23334 	ill_ipsec_capab_free(capab);
23335 	return (NULL);
23336 }
23337 
23338 /*
23339  * Resize capability array.  Since we're exclusive, this is OK.
23340  */
23341 static boolean_t
23342 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23343 {
23344 	ipsec_capab_algparm_t *nalp, *oalp;
23345 	uint32_t olen, nlen;
23346 
23347 	oalp = capab->encr_algparm;
23348 	olen = capab->encr_algparm_size;
23349 
23350 	if (oalp != NULL) {
23351 		if (algid < capab->encr_algparm_end)
23352 			return (B_TRUE);
23353 	}
23354 
23355 	nlen = (algid + 1) * sizeof (*nalp);
23356 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23357 	if (nalp == NULL)
23358 		return (B_FALSE);
23359 
23360 	if (oalp != NULL) {
23361 		bcopy(oalp, nalp, olen);
23362 		kmem_free(oalp, olen);
23363 	}
23364 	capab->encr_algparm = nalp;
23365 	capab->encr_algparm_size = nlen;
23366 	capab->encr_algparm_end = algid + 1;
23367 
23368 	return (B_TRUE);
23369 }
23370 
23371 /*
23372  * Compare the capabilities of the specified ill with the protocol
23373  * and algorithms specified by the SA passed as argument.
23374  * If they match, returns B_TRUE, B_FALSE if they do not match.
23375  *
23376  * The ill can be passed as a pointer to it, or by specifying its index
23377  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23378  *
23379  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23380  * packet is eligible for hardware acceleration, and by
23381  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23382  * to a particular ill.
23383  */
23384 boolean_t
23385 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23386     ipsa_t *sa, netstack_t *ns)
23387 {
23388 	boolean_t sa_isv6;
23389 	uint_t algid;
23390 	struct ill_ipsec_capab_s *cpp;
23391 	boolean_t need_refrele = B_FALSE;
23392 	ip_stack_t	*ipst = ns->netstack_ip;
23393 
23394 	if (ill == NULL) {
23395 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23396 		    NULL, NULL, NULL, ipst);
23397 		if (ill == NULL) {
23398 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23399 			return (B_FALSE);
23400 		}
23401 		need_refrele = B_TRUE;
23402 	}
23403 
23404 	/*
23405 	 * Use the address length specified by the SA to determine
23406 	 * if it corresponds to a IPv6 address, and fail the matching
23407 	 * if the isv6 flag passed as argument does not match.
23408 	 * Note: this check is used for SADB capability checking before
23409 	 * sending SA information to an ill.
23410 	 */
23411 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23412 	if (sa_isv6 != ill_isv6)
23413 		/* protocol mismatch */
23414 		goto done;
23415 
23416 	/*
23417 	 * Check if the ill supports the protocol, algorithm(s) and
23418 	 * key size(s) specified by the SA, and get the pointers to
23419 	 * the algorithms supported by the ill.
23420 	 */
23421 	switch (sa->ipsa_type) {
23422 
23423 	case SADB_SATYPE_ESP:
23424 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23425 			/* ill does not support ESP acceleration */
23426 			goto done;
23427 		cpp = ill->ill_ipsec_capab_esp;
23428 		algid = sa->ipsa_auth_alg;
23429 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23430 			goto done;
23431 		algid = sa->ipsa_encr_alg;
23432 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23433 			goto done;
23434 		if (algid < cpp->encr_algparm_end) {
23435 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23436 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23437 				goto done;
23438 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23439 				goto done;
23440 		}
23441 		break;
23442 
23443 	case SADB_SATYPE_AH:
23444 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23445 			/* ill does not support AH acceleration */
23446 			goto done;
23447 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23448 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23449 			goto done;
23450 		break;
23451 	}
23452 
23453 	if (need_refrele)
23454 		ill_refrele(ill);
23455 	return (B_TRUE);
23456 done:
23457 	if (need_refrele)
23458 		ill_refrele(ill);
23459 	return (B_FALSE);
23460 }
23461 
23462 /*
23463  * Add a new ill to the list of IPsec capable ills.
23464  * Called from ill_capability_ipsec_ack() when an ACK was received
23465  * indicating that IPsec hardware processing was enabled for an ill.
23466  *
23467  * ill must point to the ill for which acceleration was enabled.
23468  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23469  */
23470 static void
23471 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23472 {
23473 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23474 	uint_t sa_type;
23475 	uint_t ipproto;
23476 	ip_stack_t	*ipst = ill->ill_ipst;
23477 
23478 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23479 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23480 
23481 	switch (dl_cap) {
23482 	case DL_CAPAB_IPSEC_AH:
23483 		sa_type = SADB_SATYPE_AH;
23484 		ills = &ipst->ips_ipsec_capab_ills_ah;
23485 		ipproto = IPPROTO_AH;
23486 		break;
23487 	case DL_CAPAB_IPSEC_ESP:
23488 		sa_type = SADB_SATYPE_ESP;
23489 		ills = &ipst->ips_ipsec_capab_ills_esp;
23490 		ipproto = IPPROTO_ESP;
23491 		break;
23492 	}
23493 
23494 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23495 
23496 	/*
23497 	 * Add ill index to list of hardware accelerators. If
23498 	 * already in list, do nothing.
23499 	 */
23500 	for (cur_ill = *ills; cur_ill != NULL &&
23501 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23502 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23503 		;
23504 
23505 	if (cur_ill == NULL) {
23506 		/* if this is a new entry for this ill */
23507 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23508 		if (new_ill == NULL) {
23509 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23510 			return;
23511 		}
23512 
23513 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23514 		new_ill->ill_isv6 = ill->ill_isv6;
23515 		new_ill->next = *ills;
23516 		*ills = new_ill;
23517 	} else if (!sadb_resync) {
23518 		/* not resync'ing SADB and an entry exists for this ill */
23519 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23520 		return;
23521 	}
23522 
23523 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23524 
23525 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23526 		/*
23527 		 * IPsec module for protocol loaded, initiate dump
23528 		 * of the SADB to this ill.
23529 		 */
23530 		sadb_ill_download(ill, sa_type);
23531 }
23532 
23533 /*
23534  * Remove an ill from the list of IPsec capable ills.
23535  */
23536 static void
23537 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23538 {
23539 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23540 	ip_stack_t	*ipst = ill->ill_ipst;
23541 
23542 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23543 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23544 
23545 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23546 	    &ipst->ips_ipsec_capab_ills_esp;
23547 
23548 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23549 
23550 	prev_ill = NULL;
23551 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23552 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23553 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23554 		;
23555 	if (cur_ill == NULL) {
23556 		/* entry not found */
23557 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23558 		return;
23559 	}
23560 	if (prev_ill == NULL) {
23561 		/* entry at front of list */
23562 		*ills = NULL;
23563 	} else {
23564 		prev_ill->next = cur_ill->next;
23565 	}
23566 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23567 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23568 }
23569 
23570 /*
23571  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23572  * supporting the specified IPsec protocol acceleration.
23573  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23574  * We free the mblk and, if sa is non-null, release the held referece.
23575  */
23576 void
23577 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23578     netstack_t *ns)
23579 {
23580 	ipsec_capab_ill_t *ici, *cur_ici;
23581 	ill_t *ill;
23582 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23583 	ip_stack_t	*ipst = ns->netstack_ip;
23584 
23585 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23586 	    ipst->ips_ipsec_capab_ills_esp;
23587 
23588 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23589 
23590 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23591 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23592 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23593 
23594 		/*
23595 		 * Handle the case where the ill goes away while the SADB is
23596 		 * attempting to send messages.  If it's going away, it's
23597 		 * nuking its shadow SADB, so we don't care..
23598 		 */
23599 
23600 		if (ill == NULL)
23601 			continue;
23602 
23603 		if (sa != NULL) {
23604 			/*
23605 			 * Make sure capabilities match before
23606 			 * sending SA to ill.
23607 			 */
23608 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23609 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23610 				ill_refrele(ill);
23611 				continue;
23612 			}
23613 
23614 			mutex_enter(&sa->ipsa_lock);
23615 			sa->ipsa_flags |= IPSA_F_HW;
23616 			mutex_exit(&sa->ipsa_lock);
23617 		}
23618 
23619 		/*
23620 		 * Copy template message, and add it to the front
23621 		 * of the mblk ship list. We want to avoid holding
23622 		 * the ipsec_capab_ills_lock while sending the
23623 		 * message to the ills.
23624 		 *
23625 		 * The b_next and b_prev are temporarily used
23626 		 * to build a list of mblks to be sent down, and to
23627 		 * save the ill to which they must be sent.
23628 		 */
23629 		nmp = copymsg(mp);
23630 		if (nmp == NULL) {
23631 			ill_refrele(ill);
23632 			continue;
23633 		}
23634 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23635 		nmp->b_next = mp_ship_list;
23636 		mp_ship_list = nmp;
23637 		nmp->b_prev = (mblk_t *)ill;
23638 	}
23639 
23640 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23641 
23642 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23643 		/* restore the mblk to a sane state */
23644 		next_mp = nmp->b_next;
23645 		nmp->b_next = NULL;
23646 		ill = (ill_t *)nmp->b_prev;
23647 		nmp->b_prev = NULL;
23648 
23649 		ill_dlpi_send(ill, nmp);
23650 		ill_refrele(ill);
23651 	}
23652 
23653 	if (sa != NULL)
23654 		IPSA_REFRELE(sa);
23655 	freemsg(mp);
23656 }
23657 
23658 /*
23659  * Derive an interface id from the link layer address.
23660  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23661  */
23662 static boolean_t
23663 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23664 {
23665 	char		*addr;
23666 
23667 	if (phys_length != ETHERADDRL)
23668 		return (B_FALSE);
23669 
23670 	/* Form EUI-64 like address */
23671 	addr = (char *)&v6addr->s6_addr32[2];
23672 	bcopy((char *)phys_addr, addr, 3);
23673 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23674 	addr[3] = (char)0xff;
23675 	addr[4] = (char)0xfe;
23676 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23677 	return (B_TRUE);
23678 }
23679 
23680 /* ARGSUSED */
23681 static boolean_t
23682 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23683 {
23684 	return (B_FALSE);
23685 }
23686 
23687 /* ARGSUSED */
23688 static boolean_t
23689 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23690     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23691 {
23692 	/*
23693 	 * Multicast address mappings used over Ethernet/802.X.
23694 	 * This address is used as a base for mappings.
23695 	 */
23696 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23697 	    0x00, 0x00, 0x00};
23698 
23699 	/*
23700 	 * Extract low order 32 bits from IPv6 multicast address.
23701 	 * Or that into the link layer address, starting from the
23702 	 * second byte.
23703 	 */
23704 	*hw_start = 2;
23705 	v6_extract_mask->s6_addr32[0] = 0;
23706 	v6_extract_mask->s6_addr32[1] = 0;
23707 	v6_extract_mask->s6_addr32[2] = 0;
23708 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23709 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23710 	return (B_TRUE);
23711 }
23712 
23713 /*
23714  * Indicate by return value whether multicast is supported. If not,
23715  * this code should not touch/change any parameters.
23716  */
23717 /* ARGSUSED */
23718 static boolean_t
23719 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23720     uint32_t *hw_start, ipaddr_t *extract_mask)
23721 {
23722 	/*
23723 	 * Multicast address mappings used over Ethernet/802.X.
23724 	 * This address is used as a base for mappings.
23725 	 */
23726 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23727 	    0x00, 0x00, 0x00 };
23728 
23729 	if (phys_length != ETHERADDRL)
23730 		return (B_FALSE);
23731 
23732 	*extract_mask = htonl(0x007fffff);
23733 	*hw_start = 2;
23734 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23735 	return (B_TRUE);
23736 }
23737 
23738 /*
23739  * Derive IPoIB interface id from the link layer address.
23740  */
23741 static boolean_t
23742 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23743 {
23744 	char		*addr;
23745 
23746 	if (phys_length != 20)
23747 		return (B_FALSE);
23748 	addr = (char *)&v6addr->s6_addr32[2];
23749 	bcopy(phys_addr + 12, addr, 8);
23750 	/*
23751 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23752 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23753 	 * rules. In these cases, the IBA considers these GUIDs to be in
23754 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23755 	 * required; vendors are required not to assign global EUI-64's
23756 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23757 	 * of the interface identifier. Whether the GUID is in modified
23758 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23759 	 * bit set to 1.
23760 	 */
23761 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23762 	return (B_TRUE);
23763 }
23764 
23765 /*
23766  * Note on mapping from multicast IP addresses to IPoIB multicast link
23767  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23768  * The format of an IPoIB multicast address is:
23769  *
23770  *  4 byte QPN      Scope Sign.  Pkey
23771  * +--------------------------------------------+
23772  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23773  * +--------------------------------------------+
23774  *
23775  * The Scope and Pkey components are properties of the IBA port and
23776  * network interface. They can be ascertained from the broadcast address.
23777  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23778  */
23779 
23780 static boolean_t
23781 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23782     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23783 {
23784 	/*
23785 	 * Base IPoIB IPv6 multicast address used for mappings.
23786 	 * Does not contain the IBA scope/Pkey values.
23787 	 */
23788 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23789 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23790 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23791 
23792 	/*
23793 	 * Extract low order 80 bits from IPv6 multicast address.
23794 	 * Or that into the link layer address, starting from the
23795 	 * sixth byte.
23796 	 */
23797 	*hw_start = 6;
23798 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23799 
23800 	/*
23801 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23802 	 */
23803 	*(maddr + 5) = *(bphys_addr + 5);
23804 	*(maddr + 8) = *(bphys_addr + 8);
23805 	*(maddr + 9) = *(bphys_addr + 9);
23806 
23807 	v6_extract_mask->s6_addr32[0] = 0;
23808 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23809 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23810 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23811 	return (B_TRUE);
23812 }
23813 
23814 static boolean_t
23815 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23816     uint32_t *hw_start, ipaddr_t *extract_mask)
23817 {
23818 	/*
23819 	 * Base IPoIB IPv4 multicast address used for mappings.
23820 	 * Does not contain the IBA scope/Pkey values.
23821 	 */
23822 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23823 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23824 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23825 
23826 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23827 		return (B_FALSE);
23828 
23829 	/*
23830 	 * Extract low order 28 bits from IPv4 multicast address.
23831 	 * Or that into the link layer address, starting from the
23832 	 * sixteenth byte.
23833 	 */
23834 	*extract_mask = htonl(0x0fffffff);
23835 	*hw_start = 16;
23836 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23837 
23838 	/*
23839 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23840 	 */
23841 	*(maddr + 5) = *(bphys_addr + 5);
23842 	*(maddr + 8) = *(bphys_addr + 8);
23843 	*(maddr + 9) = *(bphys_addr + 9);
23844 	return (B_TRUE);
23845 }
23846 
23847 /*
23848  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23849  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23850  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23851  * the link-local address is preferred.
23852  */
23853 boolean_t
23854 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23855 {
23856 	ipif_t	*ipif;
23857 	ipif_t	*maybe_ipif = NULL;
23858 
23859 	mutex_enter(&ill->ill_lock);
23860 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23861 		mutex_exit(&ill->ill_lock);
23862 		if (ipifp != NULL)
23863 			*ipifp = NULL;
23864 		return (B_FALSE);
23865 	}
23866 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23867 		if (!IPIF_CAN_LOOKUP(ipif))
23868 			continue;
23869 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23870 		    ipif->ipif_zoneid != ALL_ZONES)
23871 			continue;
23872 		if ((ipif->ipif_flags & flags) != flags)
23873 			continue;
23874 
23875 		if (ipifp == NULL) {
23876 			mutex_exit(&ill->ill_lock);
23877 			ASSERT(maybe_ipif == NULL);
23878 			return (B_TRUE);
23879 		}
23880 		if (!ill->ill_isv6 ||
23881 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23882 			ipif_refhold_locked(ipif);
23883 			mutex_exit(&ill->ill_lock);
23884 			*ipifp = ipif;
23885 			return (B_TRUE);
23886 		}
23887 		if (maybe_ipif == NULL)
23888 			maybe_ipif = ipif;
23889 	}
23890 	if (ipifp != NULL) {
23891 		if (maybe_ipif != NULL)
23892 			ipif_refhold_locked(maybe_ipif);
23893 		*ipifp = maybe_ipif;
23894 	}
23895 	mutex_exit(&ill->ill_lock);
23896 	return (maybe_ipif != NULL);
23897 }
23898 
23899 /*
23900  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
23901  */
23902 boolean_t
23903 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23904 {
23905 	ill_t *illg;
23906 	ip_stack_t	*ipst = ill->ill_ipst;
23907 
23908 	/*
23909 	 * We look at the passed-in ill first without grabbing ill_g_lock.
23910 	 */
23911 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
23912 		return (B_TRUE);
23913 	}
23914 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23915 	if (ill->ill_group == NULL) {
23916 		/* ill not in a group */
23917 		rw_exit(&ipst->ips_ill_g_lock);
23918 		return (B_FALSE);
23919 	}
23920 
23921 	/*
23922 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
23923 	 * group. We need to look for an ipif in the zone on all the ills in the
23924 	 * group.
23925 	 */
23926 	illg = ill->ill_group->illgrp_ill;
23927 	do {
23928 		/*
23929 		 * We don't call ipif_lookup_zoneid() on ill as we already know
23930 		 * that it's not there.
23931 		 */
23932 		if (illg != ill &&
23933 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
23934 			break;
23935 		}
23936 	} while ((illg = illg->ill_group_next) != NULL);
23937 	rw_exit(&ipst->ips_ill_g_lock);
23938 	return (illg != NULL);
23939 }
23940 
23941 /*
23942  * Check if this ill is only being used to send ICMP probes for IPMP
23943  */
23944 boolean_t
23945 ill_is_probeonly(ill_t *ill)
23946 {
23947 	/*
23948 	 * Check if the interface is FAILED, or INACTIVE
23949 	 */
23950 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
23951 		return (B_TRUE);
23952 
23953 	return (B_FALSE);
23954 }
23955 
23956 /*
23957  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
23958  * If a pointer to an ipif_t is returned then the caller will need to do
23959  * an ill_refrele().
23960  *
23961  * If there is no real interface which matches the ifindex, then it looks
23962  * for a group that has a matching index. In the case of a group match the
23963  * lifidx must be zero. We don't need emulate the logical interfaces
23964  * since IP Filter's use of netinfo doesn't use that.
23965  */
23966 ipif_t *
23967 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
23968     ip_stack_t *ipst)
23969 {
23970 	ipif_t *ipif;
23971 	ill_t *ill;
23972 
23973 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
23974 	    ipst);
23975 
23976 	if (ill == NULL) {
23977 		/* Fallback to group names only if hook_emulation set */
23978 		if (!ipst->ips_ipmp_hook_emulation)
23979 			return (NULL);
23980 
23981 		if (lifidx != 0)
23982 			return (NULL);
23983 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
23984 		if (ill == NULL)
23985 			return (NULL);
23986 	}
23987 
23988 	mutex_enter(&ill->ill_lock);
23989 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23990 		mutex_exit(&ill->ill_lock);
23991 		ill_refrele(ill);
23992 		return (NULL);
23993 	}
23994 
23995 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23996 		if (!IPIF_CAN_LOOKUP(ipif))
23997 			continue;
23998 		if (lifidx == ipif->ipif_id) {
23999 			ipif_refhold_locked(ipif);
24000 			break;
24001 		}
24002 	}
24003 
24004 	mutex_exit(&ill->ill_lock);
24005 	ill_refrele(ill);
24006 	return (ipif);
24007 }
24008 
24009 /*
24010  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24011  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24012  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24013  * for details.
24014  */
24015 void
24016 ill_fastpath_flush(ill_t *ill)
24017 {
24018 	ip_stack_t *ipst = ill->ill_ipst;
24019 
24020 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24021 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24022 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24023 }
24024 
24025 /*
24026  * Set the physical address information for `ill' to the contents of the
24027  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24028  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24029  * EINPROGRESS will be returned.
24030  */
24031 int
24032 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24033 {
24034 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24035 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24036 
24037 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24038 
24039 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24040 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24041 		/* Changing DL_IPV6_TOKEN is not yet supported */
24042 		return (0);
24043 	}
24044 
24045 	/*
24046 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24047 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24048 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24049 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24050 	 */
24051 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24052 		freemsg(mp);
24053 		return (ENOMEM);
24054 	}
24055 
24056 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24057 
24058 	/*
24059 	 * If we can quiesce the ill, then set the address.  If not, then
24060 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24061 	 */
24062 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24063 	mutex_enter(&ill->ill_lock);
24064 	if (!ill_is_quiescent(ill)) {
24065 		/* call cannot fail since `conn_t *' argument is NULL */
24066 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24067 		    mp, ILL_DOWN);
24068 		mutex_exit(&ill->ill_lock);
24069 		return (EINPROGRESS);
24070 	}
24071 	mutex_exit(&ill->ill_lock);
24072 
24073 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24074 	return (0);
24075 }
24076 
24077 /*
24078  * Once the ill associated with `q' has quiesced, set its physical address
24079  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24080  * are passed (linked by b_cont), since we sometimes need to save two distinct
24081  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24082  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24083  * is quiesced, we know any stale IREs with the old address information have
24084  * already been removed, so we don't need to call ill_fastpath_flush().
24085  */
24086 /* ARGSUSED */
24087 static void
24088 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24089 {
24090 	ill_t		*ill = q->q_ptr;
24091 	mblk_t		*addrmp2 = unlinkb(addrmp);
24092 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24093 	uint_t		addrlen, addroff;
24094 
24095 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24096 
24097 	addroff	= dlindp->dl_addr_offset;
24098 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24099 
24100 	switch (dlindp->dl_data) {
24101 	case DL_IPV6_LINK_LAYER_ADDR:
24102 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24103 		freemsg(addrmp2);
24104 		break;
24105 
24106 	case DL_CURR_PHYS_ADDR:
24107 		freemsg(ill->ill_phys_addr_mp);
24108 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24109 		ill->ill_phys_addr_mp = addrmp;
24110 		ill->ill_phys_addr_length = addrlen;
24111 
24112 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24113 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24114 		else
24115 			freemsg(addrmp2);
24116 		break;
24117 	default:
24118 		ASSERT(0);
24119 	}
24120 
24121 	/*
24122 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24123 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24124 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24125 	 * brought up.
24126 	 */
24127 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24128 		ipsq_current_finish(ipsq);
24129 }
24130 
24131 /*
24132  * Helper routine for setting the ill_nd_lla fields.
24133  */
24134 void
24135 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24136 {
24137 	freemsg(ill->ill_nd_lla_mp);
24138 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24139 	ill->ill_nd_lla_mp = ndmp;
24140 	ill->ill_nd_lla_len = addrlen;
24141 }
24142 
24143 major_t IP_MAJ;
24144 #define	IP	"ip"
24145 
24146 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24147 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24148 
24149 /*
24150  * Issue REMOVEIF ioctls to have the loopback interfaces
24151  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24152  * the former going away when the user-level processes in the zone
24153  * are killed  * and the latter are cleaned up by the stream head
24154  * str_stack_shutdown callback that undoes all I_PLINKs.
24155  */
24156 void
24157 ip_loopback_cleanup(ip_stack_t *ipst)
24158 {
24159 	int error;
24160 	ldi_handle_t	lh = NULL;
24161 	ldi_ident_t	li = NULL;
24162 	int		rval;
24163 	cred_t		*cr;
24164 	struct strioctl iocb;
24165 	struct lifreq	lifreq;
24166 
24167 	IP_MAJ = ddi_name_to_major(IP);
24168 
24169 #ifdef NS_DEBUG
24170 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24171 	    ipst->ips_netstack->netstack_stackid);
24172 #endif
24173 
24174 	bzero(&lifreq, sizeof (lifreq));
24175 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24176 
24177 	error = ldi_ident_from_major(IP_MAJ, &li);
24178 	if (error) {
24179 #ifdef DEBUG
24180 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24181 		    error);
24182 #endif
24183 		return;
24184 	}
24185 
24186 	cr = zone_get_kcred(netstackid_to_zoneid(
24187 	    ipst->ips_netstack->netstack_stackid));
24188 	ASSERT(cr != NULL);
24189 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24190 	if (error) {
24191 #ifdef DEBUG
24192 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24193 		    error);
24194 #endif
24195 		goto out;
24196 	}
24197 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24198 	iocb.ic_timout = 15;
24199 	iocb.ic_len = sizeof (lifreq);
24200 	iocb.ic_dp = (char *)&lifreq;
24201 
24202 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24203 	/* LINTED - statement has no consequent */
24204 	if (error) {
24205 #ifdef NS_DEBUG
24206 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24207 		    "UDP6 error %d\n", error);
24208 #endif
24209 	}
24210 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24211 	lh = NULL;
24212 
24213 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24214 	if (error) {
24215 #ifdef NS_DEBUG
24216 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24217 		    error);
24218 #endif
24219 		goto out;
24220 	}
24221 
24222 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24223 	iocb.ic_timout = 15;
24224 	iocb.ic_len = sizeof (lifreq);
24225 	iocb.ic_dp = (char *)&lifreq;
24226 
24227 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24228 	/* LINTED - statement has no consequent */
24229 	if (error) {
24230 #ifdef NS_DEBUG
24231 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24232 		    "UDP error %d\n", error);
24233 #endif
24234 	}
24235 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24236 	lh = NULL;
24237 
24238 out:
24239 	/* Close layered handles */
24240 	if (lh)
24241 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24242 	if (li)
24243 		ldi_ident_release(li);
24244 
24245 	crfree(cr);
24246 }
24247 
24248 /*
24249  * This needs to be in-sync with nic_event_t definition
24250  */
24251 static const char *
24252 ill_hook_event2str(nic_event_t event)
24253 {
24254 	switch (event) {
24255 	case NE_PLUMB:
24256 		return ("PLUMB");
24257 	case NE_UNPLUMB:
24258 		return ("UNPLUMB");
24259 	case NE_UP:
24260 		return ("UP");
24261 	case NE_DOWN:
24262 		return ("DOWN");
24263 	case NE_ADDRESS_CHANGE:
24264 		return ("ADDRESS_CHANGE");
24265 	case NE_LIF_UP:
24266 		return ("LIF_UP");
24267 	case NE_LIF_DOWN:
24268 		return ("LIF_DOWN");
24269 	default:
24270 		return ("UNKNOWN");
24271 	}
24272 }
24273 
24274 void
24275 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
24276     nic_event_data_t data, size_t datalen)
24277 {
24278 	ip_stack_t		*ipst = ill->ill_ipst;
24279 	hook_nic_event_int_t	*info;
24280 	const char		*str = NULL;
24281 
24282 	/* create a new nic event info */
24283 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
24284 		goto fail;
24285 
24286 	if (event == NE_UNPLUMB)
24287 		info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
24288 	else
24289 		info->hnei_event.hne_nic = ill->ill_phyint->phyint_hook_ifindex;
24290 	info->hnei_event.hne_lif = lif;
24291 	info->hnei_event.hne_event = event;
24292 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
24293 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
24294 	info->hnei_event.hne_data = NULL;
24295 	info->hnei_event.hne_datalen = 0;
24296 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
24297 
24298 	if (data != NULL && datalen != 0) {
24299 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
24300 		if (info->hnei_event.hne_data == NULL)
24301 			goto fail;
24302 		bcopy(data, info->hnei_event.hne_data, datalen);
24303 		info->hnei_event.hne_datalen = datalen;
24304 	}
24305 
24306 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
24307 	    DDI_NOSLEEP) == DDI_SUCCESS)
24308 		return;
24309 
24310 fail:
24311 	if (info != NULL) {
24312 		if (info->hnei_event.hne_data != NULL) {
24313 			kmem_free(info->hnei_event.hne_data,
24314 			    info->hnei_event.hne_datalen);
24315 		}
24316 		kmem_free(info, sizeof (hook_nic_event_t));
24317 	}
24318 	str = ill_hook_event2str(event);
24319 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
24320 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
24321 }
24322 
24323 void
24324 ipif_up_notify(ipif_t *ipif)
24325 {
24326 	ip_rts_ifmsg(ipif);
24327 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
24328 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
24329 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
24330 	    NE_LIF_UP, NULL, 0);
24331 }
24332