xref: /titanic_44/usr/src/uts/common/inet/ip/ip_if.c (revision b6917abefc343244b784f0cc34bc65b01469c3bf)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the interface control functions for IP.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strlog.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/kstat.h>
44 #include <sys/debug.h>
45 #include <sys/zone.h>
46 #include <sys/sunldi.h>
47 #include <sys/file.h>
48 #include <sys/bitmap.h>
49 
50 #include <sys/kmem.h>
51 #include <sys/systm.h>
52 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <sys/isa_defs.h>
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <sys/sockio.h>
61 #include <netinet/in.h>
62 #include <netinet/ip6.h>
63 #include <netinet/icmp6.h>
64 #include <netinet/igmp_var.h>
65 #include <sys/strsun.h>
66 #include <sys/policy.h>
67 #include <sys/ethernet.h>
68 
69 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
70 #include <inet/mi.h>
71 #include <inet/nd.h>
72 #include <inet/arp.h>
73 #include <inet/mib2.h>
74 #include <inet/ip.h>
75 #include <inet/ip6.h>
76 #include <inet/ip6_asp.h>
77 #include <inet/tcp.h>
78 #include <inet/ip_multi.h>
79 #include <inet/ip_ire.h>
80 #include <inet/ip_ftable.h>
81 #include <inet/ip_rts.h>
82 #include <inet/ip_ndp.h>
83 #include <inet/ip_if.h>
84 #include <inet/ip_impl.h>
85 #include <inet/tun.h>
86 #include <inet/sctp_ip.h>
87 #include <inet/ip_netinfo.h>
88 #include <inet/mib2.h>
89 
90 #include <net/pfkeyv2.h>
91 #include <inet/ipsec_info.h>
92 #include <inet/sadb.h>
93 #include <inet/ipsec_impl.h>
94 #include <sys/iphada.h>
95 
96 
97 #include <netinet/igmp.h>
98 #include <inet/ip_listutils.h>
99 #include <inet/ipclassifier.h>
100 #include <sys/mac.h>
101 
102 #include <sys/systeminfo.h>
103 #include <sys/bootconf.h>
104 
105 #include <sys/tsol/tndb.h>
106 #include <sys/tsol/tnet.h>
107 
108 /* The character which tells where the ill_name ends */
109 #define	IPIF_SEPARATOR_CHAR	':'
110 
111 /* IP ioctl function table entry */
112 typedef struct ipft_s {
113 	int	ipft_cmd;
114 	pfi_t	ipft_pfi;
115 	int	ipft_min_size;
116 	int	ipft_flags;
117 } ipft_t;
118 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
119 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
120 
121 typedef struct ip_sock_ar_s {
122 	union {
123 		area_t	ip_sock_area;
124 		ared_t	ip_sock_ared;
125 		areq_t	ip_sock_areq;
126 	} ip_sock_ar_u;
127 	queue_t	*ip_sock_ar_q;
128 } ip_sock_ar_t;
129 
130 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
131 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
132 		    char *value, caddr_t cp, cred_t *ioc_cr);
133 
134 static boolean_t ill_is_quiescent(ill_t *);
135 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
136 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
137 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
140     mblk_t *mp, boolean_t need_up);
141 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
142     queue_t *q, mblk_t *mp, boolean_t need_up);
143 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
144     mblk_t *mp);
145 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
146     mblk_t *mp);
147 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
148     queue_t *q, mblk_t *mp, boolean_t need_up);
149 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
150     int ioccmd, struct linkblk *li, boolean_t doconsist);
151 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
152 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
153 static void	ipsq_flush(ill_t *ill);
154 
155 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
156     queue_t *q, mblk_t *mp, boolean_t need_up);
157 static void	ipsq_delete(ipsq_t *);
158 
159 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
160 		    boolean_t initialize);
161 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
162 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
163 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
164 		    boolean_t isv6);
165 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
166 static void	ipif_delete_cache_ire(ire_t *, char *);
167 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
168 static void	ipif_free(ipif_t *ipif);
169 static void	ipif_free_tail(ipif_t *ipif);
170 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
171 static void	ipif_multicast_down(ipif_t *ipif);
172 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
173 static void	ipif_set_default(ipif_t *ipif);
174 static int	ipif_set_values(queue_t *q, mblk_t *mp,
175     char *interf_name, uint_t *ppa);
176 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
177     queue_t *q);
178 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
179     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
180     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
181 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
182 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
183 
184 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
185 static int	ill_arp_off(ill_t *ill);
186 static int	ill_arp_on(ill_t *ill);
187 static void	ill_delete_interface_type(ill_if_t *);
188 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
189 static void	ill_dl_down(ill_t *ill);
190 static void	ill_down(ill_t *ill);
191 static void	ill_downi(ire_t *ire, char *ill_arg);
192 static void	ill_free_mib(ill_t *ill);
193 static void	ill_glist_delete(ill_t *);
194 static boolean_t ill_has_usable_ipif(ill_t *);
195 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
196 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
197 static void	ill_phyint_free(ill_t *ill);
198 static void	ill_phyint_reinit(ill_t *ill);
199 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
200 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
201 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
202 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
203 static void	ill_stq_cache_delete(ire_t *, char *);
204 
205 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
206 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
207 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     in6_addr_t *);
209 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
210     ipaddr_t *);
211 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
212 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
213     in6_addr_t *);
214 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
215     ipaddr_t *);
216 
217 static void	ipif_save_ire(ipif_t *, ire_t *);
218 static void	ipif_remove_ire(ipif_t *, ire_t *);
219 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
220 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
221 
222 /*
223  * Per-ill IPsec capabilities management.
224  */
225 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
226 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
227 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
228 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
229 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
230 static void ill_capability_proto(ill_t *, int, mblk_t *);
231 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
232     boolean_t);
233 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
234 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
235 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
236 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
237 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
238 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
239 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
240 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
241     dl_capability_sub_t *);
242 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
243 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
244 static void ill_capability_lso_reset(ill_t *, mblk_t **);
245 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
246 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
247 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
248 static void	ill_capability_dls_disable(ill_t *);
249 
250 static void	illgrp_cache_delete(ire_t *, char *);
251 static void	illgrp_delete(ill_t *ill);
252 static void	illgrp_reset_schednext(ill_t *ill);
253 
254 static ill_t	*ill_prev_usesrc(ill_t *);
255 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
256 static void	ill_disband_usesrc_group(ill_t *);
257 
258 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
259 
260 #ifdef DEBUG
261 static	void	ill_trace_cleanup(const ill_t *);
262 static	void	ipif_trace_cleanup(const ipif_t *);
263 #endif
264 
265 /*
266  * if we go over the memory footprint limit more than once in this msec
267  * interval, we'll start pruning aggressively.
268  */
269 int ip_min_frag_prune_time = 0;
270 
271 /*
272  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
273  * and the IPsec DOI
274  */
275 #define	MAX_IPSEC_ALGS	256
276 
277 #define	BITSPERBYTE	8
278 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
279 
280 #define	IPSEC_ALG_ENABLE(algs, algid) \
281 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
282 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
283 
284 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
285 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
286 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
287 
288 typedef uint8_t ipsec_capab_elem_t;
289 
290 /*
291  * Per-algorithm parameters.  Note that at present, only encryption
292  * algorithms have variable keysize (IKE does not provide a way to negotiate
293  * auth algorithm keysize).
294  *
295  * All sizes here are in bits.
296  */
297 typedef struct
298 {
299 	uint16_t	minkeylen;
300 	uint16_t	maxkeylen;
301 } ipsec_capab_algparm_t;
302 
303 /*
304  * Per-ill capabilities.
305  */
306 struct ill_ipsec_capab_s {
307 	ipsec_capab_elem_t *encr_hw_algs;
308 	ipsec_capab_elem_t *auth_hw_algs;
309 	uint32_t algs_size;	/* size of _hw_algs in bytes */
310 	/* algorithm key lengths */
311 	ipsec_capab_algparm_t *encr_algparm;
312 	uint32_t encr_algparm_size;
313 	uint32_t encr_algparm_end;
314 };
315 
316 /*
317  * The field values are larger than strictly necessary for simple
318  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
319  */
320 static area_t	ip_area_template = {
321 	AR_ENTRY_ADD,			/* area_cmd */
322 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
323 					/* area_name_offset */
324 	/* area_name_length temporarily holds this structure length */
325 	sizeof (area_t),			/* area_name_length */
326 	IP_ARP_PROTO_TYPE,		/* area_proto */
327 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
328 	IP_ADDR_LEN,			/* area_proto_addr_length */
329 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
330 					/* area_proto_mask_offset */
331 	0,				/* area_flags */
332 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
333 					/* area_hw_addr_offset */
334 	/* Zero length hw_addr_length means 'use your idea of the address' */
335 	0				/* area_hw_addr_length */
336 };
337 
338 /*
339  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
340  * support
341  */
342 static area_t	ip6_area_template = {
343 	AR_ENTRY_ADD,			/* area_cmd */
344 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
345 					/* area_name_offset */
346 	/* area_name_length temporarily holds this structure length */
347 	sizeof (area_t),			/* area_name_length */
348 	IP_ARP_PROTO_TYPE,		/* area_proto */
349 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
350 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
351 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
352 					/* area_proto_mask_offset */
353 	0,				/* area_flags */
354 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
355 					/* area_hw_addr_offset */
356 	/* Zero length hw_addr_length means 'use your idea of the address' */
357 	0				/* area_hw_addr_length */
358 };
359 
360 static ared_t	ip_ared_template = {
361 	AR_ENTRY_DELETE,
362 	sizeof (ared_t) + IP_ADDR_LEN,
363 	sizeof (ared_t),
364 	IP_ARP_PROTO_TYPE,
365 	sizeof (ared_t),
366 	IP_ADDR_LEN
367 };
368 
369 static ared_t	ip6_ared_template = {
370 	AR_ENTRY_DELETE,
371 	sizeof (ared_t) + IPV6_ADDR_LEN,
372 	sizeof (ared_t),
373 	IP_ARP_PROTO_TYPE,
374 	sizeof (ared_t),
375 	IPV6_ADDR_LEN
376 };
377 
378 /*
379  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
380  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
381  * areq is used).
382  */
383 static areq_t	ip_areq_template = {
384 	AR_ENTRY_QUERY,			/* cmd */
385 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
386 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
387 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
388 	sizeof (areq_t),			/* target addr offset */
389 	IP_ADDR_LEN,			/* target addr_length */
390 	0,				/* flags */
391 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
392 	IP_ADDR_LEN,			/* sender addr length */
393 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
394 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
395 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
396 	/* anything else filled in by the code */
397 };
398 
399 static arc_t	ip_aru_template = {
400 	AR_INTERFACE_UP,
401 	sizeof (arc_t),		/* Name offset */
402 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
403 };
404 
405 static arc_t	ip_ard_template = {
406 	AR_INTERFACE_DOWN,
407 	sizeof (arc_t),		/* Name offset */
408 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
409 };
410 
411 static arc_t	ip_aron_template = {
412 	AR_INTERFACE_ON,
413 	sizeof (arc_t),		/* Name offset */
414 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
415 };
416 
417 static arc_t	ip_aroff_template = {
418 	AR_INTERFACE_OFF,
419 	sizeof (arc_t),		/* Name offset */
420 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
421 };
422 
423 static arma_t	ip_arma_multi_template = {
424 	AR_MAPPING_ADD,
425 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
426 				/* Name offset */
427 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
428 	IP_ARP_PROTO_TYPE,
429 	sizeof (arma_t),			/* proto_addr_offset */
430 	IP_ADDR_LEN,				/* proto_addr_length */
431 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
432 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
433 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
434 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
435 	IP_MAX_HW_LEN,				/* hw_addr_length */
436 	0,					/* hw_mapping_start */
437 };
438 
439 static ipft_t	ip_ioctl_ftbl[] = {
440 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
441 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
442 		IPFT_F_NO_REPLY },
443 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
444 		IPFT_F_NO_REPLY },
445 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
446 	{ 0 }
447 };
448 
449 /* Simple ICMP IP Header Template */
450 static ipha_t icmp_ipha = {
451 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
452 };
453 
454 /* Flag descriptors for ip_ipif_report */
455 static nv_t	ipif_nv_tbl[] = {
456 	{ IPIF_UP,		"UP" },
457 	{ IPIF_BROADCAST,	"BROADCAST" },
458 	{ ILLF_DEBUG,		"DEBUG" },
459 	{ PHYI_LOOPBACK,	"LOOPBACK" },
460 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
461 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
462 	{ PHYI_RUNNING,		"RUNNING" },
463 	{ ILLF_NOARP,		"NOARP" },
464 	{ PHYI_PROMISC,		"PROMISC" },
465 	{ PHYI_ALLMULTI,	"ALLMULTI" },
466 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
467 	{ ILLF_MULTICAST,	"MULTICAST" },
468 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
469 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
470 	{ IPIF_DHCPRUNNING,	"DHCP" },
471 	{ IPIF_PRIVATE,		"PRIVATE" },
472 	{ IPIF_NOXMIT,		"NOXMIT" },
473 	{ IPIF_NOLOCAL,		"NOLOCAL" },
474 	{ IPIF_DEPRECATED,	"DEPRECATED" },
475 	{ IPIF_PREFERRED,	"PREFERRED" },
476 	{ IPIF_TEMPORARY,	"TEMPORARY" },
477 	{ IPIF_ADDRCONF,	"ADDRCONF" },
478 	{ PHYI_VIRTUAL,		"VIRTUAL" },
479 	{ ILLF_ROUTER,		"ROUTER" },
480 	{ ILLF_NONUD,		"NONUD" },
481 	{ IPIF_ANYCAST,		"ANYCAST" },
482 	{ ILLF_NORTEXCH,	"NORTEXCH" },
483 	{ ILLF_IPV4,		"IPV4" },
484 	{ ILLF_IPV6,		"IPV6" },
485 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
486 	{ PHYI_FAILED,		"FAILED" },
487 	{ PHYI_STANDBY,		"STANDBY" },
488 	{ PHYI_INACTIVE,	"INACTIVE" },
489 	{ PHYI_OFFLINE,		"OFFLINE" },
490 };
491 
492 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
493 
494 static ip_m_t	ip_m_tbl[] = {
495 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
496 	    ip_ether_v6intfid },
497 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_nodef_v6intfid },
499 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_nodef_v6intfid },
501 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
502 	    ip_nodef_v6intfid },
503 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
504 	    ip_ether_v6intfid },
505 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
506 	    ip_ib_v6intfid },
507 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
508 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
509 	    ip_nodef_v6intfid }
510 };
511 
512 static ill_t	ill_null;		/* Empty ILL for init. */
513 char	ipif_loopback_name[] = "lo0";
514 static char *ipv4_forward_suffix = ":ip_forwarding";
515 static char *ipv6_forward_suffix = ":ip6_forwarding";
516 static	sin6_t	sin6_null;	/* Zero address for quick clears */
517 static	sin_t	sin_null;	/* Zero address for quick clears */
518 
519 /* When set search for unused ipif_seqid */
520 static ipif_t	ipif_zero;
521 
522 /*
523  * ppa arena is created after these many
524  * interfaces have been plumbed.
525  */
526 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
527 
528 /*
529  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
530  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
531  * set through platform specific code (Niagara/Ontario).
532  */
533 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
534 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
535 
536 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
537 
538 static uint_t
539 ipif_rand(ip_stack_t *ipst)
540 {
541 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
542 	    12345;
543 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
544 }
545 
546 /*
547  * Allocate per-interface mibs.
548  * Returns true if ok. False otherwise.
549  *  ipsq  may not yet be allocated (loopback case ).
550  */
551 static boolean_t
552 ill_allocate_mibs(ill_t *ill)
553 {
554 	/* Already allocated? */
555 	if (ill->ill_ip_mib != NULL) {
556 		if (ill->ill_isv6)
557 			ASSERT(ill->ill_icmp6_mib != NULL);
558 		return (B_TRUE);
559 	}
560 
561 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
562 	    KM_NOSLEEP);
563 	if (ill->ill_ip_mib == NULL) {
564 		return (B_FALSE);
565 	}
566 
567 	/* Setup static information */
568 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
569 	    sizeof (mib2_ipIfStatsEntry_t));
570 	if (ill->ill_isv6) {
571 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
572 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
573 		    sizeof (mib2_ipv6AddrEntry_t));
574 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
575 		    sizeof (mib2_ipv6RouteEntry_t));
576 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
577 		    sizeof (mib2_ipv6NetToMediaEntry_t));
578 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
579 		    sizeof (ipv6_member_t));
580 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
581 		    sizeof (ipv6_grpsrc_t));
582 	} else {
583 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
584 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
585 		    sizeof (mib2_ipAddrEntry_t));
586 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
587 		    sizeof (mib2_ipRouteEntry_t));
588 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
589 		    sizeof (mib2_ipNetToMediaEntry_t));
590 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
591 		    sizeof (ip_member_t));
592 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
593 		    sizeof (ip_grpsrc_t));
594 
595 		/*
596 		 * For a v4 ill, we are done at this point, because per ill
597 		 * icmp mibs are only used for v6.
598 		 */
599 		return (B_TRUE);
600 	}
601 
602 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
603 	    KM_NOSLEEP);
604 	if (ill->ill_icmp6_mib == NULL) {
605 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
606 		ill->ill_ip_mib = NULL;
607 		return (B_FALSE);
608 	}
609 	/* static icmp info */
610 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
611 	    sizeof (mib2_ipv6IfIcmpEntry_t);
612 	/*
613 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
614 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
615 	 * -> ill_phyint_reinit
616 	 */
617 	return (B_TRUE);
618 }
619 
620 /*
621  * Common code for preparation of ARP commands.  Two points to remember:
622  * 	1) The ill_name is tacked on at the end of the allocated space so
623  *	   the templates name_offset field must contain the total space
624  *	   to allocate less the name length.
625  *
626  *	2) The templates name_length field should contain the *template*
627  *	   length.  We use it as a parameter to bcopy() and then write
628  *	   the real ill_name_length into the name_length field of the copy.
629  * (Always called as writer.)
630  */
631 mblk_t *
632 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
633 {
634 	arc_t	*arc = (arc_t *)template;
635 	char	*cp;
636 	int	len;
637 	mblk_t	*mp;
638 	uint_t	name_length = ill->ill_name_length;
639 	uint_t	template_len = arc->arc_name_length;
640 
641 	len = arc->arc_name_offset + name_length;
642 	mp = allocb(len, BPRI_HI);
643 	if (mp == NULL)
644 		return (NULL);
645 	cp = (char *)mp->b_rptr;
646 	mp->b_wptr = (uchar_t *)&cp[len];
647 	if (template_len)
648 		bcopy(template, cp, template_len);
649 	if (len > template_len)
650 		bzero(&cp[template_len], len - template_len);
651 	mp->b_datap->db_type = M_PROTO;
652 
653 	arc = (arc_t *)cp;
654 	arc->arc_name_length = name_length;
655 	cp = (char *)arc + arc->arc_name_offset;
656 	bcopy(ill->ill_name, cp, name_length);
657 
658 	if (addr) {
659 		area_t	*area = (area_t *)mp->b_rptr;
660 
661 		cp = (char *)area + area->area_proto_addr_offset;
662 		bcopy(addr, cp, area->area_proto_addr_length);
663 		if (area->area_cmd == AR_ENTRY_ADD) {
664 			cp = (char *)area;
665 			len = area->area_proto_addr_length;
666 			if (area->area_proto_mask_offset)
667 				cp += area->area_proto_mask_offset;
668 			else
669 				cp += area->area_proto_addr_offset + len;
670 			while (len-- > 0)
671 				*cp++ = (char)~0;
672 		}
673 	}
674 	return (mp);
675 }
676 
677 mblk_t *
678 ipif_area_alloc(ipif_t *ipif)
679 {
680 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
681 	    (char *)&ipif->ipif_lcl_addr));
682 }
683 
684 mblk_t *
685 ipif_ared_alloc(ipif_t *ipif)
686 {
687 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
688 	    (char *)&ipif->ipif_lcl_addr));
689 }
690 
691 mblk_t *
692 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
693 {
694 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
695 	    (char *)&addr));
696 }
697 
698 /*
699  * Completely vaporize a lower level tap and all associated interfaces.
700  * ill_delete is called only out of ip_close when the device control
701  * stream is being closed.
702  */
703 void
704 ill_delete(ill_t *ill)
705 {
706 	ipif_t	*ipif;
707 	ill_t	*prev_ill;
708 	ip_stack_t	*ipst = ill->ill_ipst;
709 
710 	/*
711 	 * ill_delete may be forcibly entering the ipsq. The previous
712 	 * ioctl may not have completed and may need to be aborted.
713 	 * ipsq_flush takes care of it. If we don't need to enter the
714 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
715 	 * ill_delete_tail is sufficient.
716 	 */
717 	ipsq_flush(ill);
718 
719 	/*
720 	 * Nuke all interfaces.  ipif_free will take down the interface,
721 	 * remove it from the list, and free the data structure.
722 	 * Walk down the ipif list and remove the logical interfaces
723 	 * first before removing the main ipif. We can't unplumb
724 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
725 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
726 	 * POINTOPOINT.
727 	 *
728 	 * If ill_ipif was not properly initialized (i.e low on memory),
729 	 * then no interfaces to clean up. In this case just clean up the
730 	 * ill.
731 	 */
732 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
733 		ipif_free(ipif);
734 
735 	/*
736 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
737 	 * So nobody can be using this mp now. Free the mp allocated for
738 	 * honoring ILLF_NOARP
739 	 */
740 	freemsg(ill->ill_arp_on_mp);
741 	ill->ill_arp_on_mp = NULL;
742 
743 	/* Clean up msgs on pending upcalls for mrouted */
744 	reset_mrt_ill(ill);
745 
746 	/*
747 	 * ipif_free -> reset_conn_ipif will remove all multicast
748 	 * references for IPv4. For IPv6, we need to do it here as
749 	 * it points only at ills.
750 	 */
751 	reset_conn_ill(ill);
752 
753 	/*
754 	 * ill_down will arrange to blow off any IRE's dependent on this
755 	 * ILL, and shut down fragmentation reassembly.
756 	 */
757 	ill_down(ill);
758 
759 	/* Let SCTP know, so that it can remove this from its list. */
760 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
761 
762 	/*
763 	 * If an address on this ILL is being used as a source address then
764 	 * clear out the pointers in other ILLs that point to this ILL.
765 	 */
766 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
767 	if (ill->ill_usesrc_grp_next != NULL) {
768 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
769 			ill_disband_usesrc_group(ill);
770 		} else {	/* consumer of the usesrc ILL */
771 			prev_ill = ill_prev_usesrc(ill);
772 			prev_ill->ill_usesrc_grp_next =
773 			    ill->ill_usesrc_grp_next;
774 		}
775 	}
776 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
777 }
778 
779 static void
780 ipif_non_duplicate(ipif_t *ipif)
781 {
782 	ill_t *ill = ipif->ipif_ill;
783 	mutex_enter(&ill->ill_lock);
784 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
785 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
786 		ASSERT(ill->ill_ipif_dup_count > 0);
787 		ill->ill_ipif_dup_count--;
788 	}
789 	mutex_exit(&ill->ill_lock);
790 }
791 
792 /*
793  * ill_delete_tail is called from ip_modclose after all references
794  * to the closing ill are gone. The wait is done in ip_modclose
795  */
796 void
797 ill_delete_tail(ill_t *ill)
798 {
799 	mblk_t	**mpp;
800 	ipif_t	*ipif;
801 	ip_stack_t	*ipst = ill->ill_ipst;
802 
803 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
804 		ipif_non_duplicate(ipif);
805 		ipif_down_tail(ipif);
806 	}
807 
808 	ASSERT(ill->ill_ipif_dup_count == 0 &&
809 	    ill->ill_arp_down_mp == NULL &&
810 	    ill->ill_arp_del_mapping_mp == NULL);
811 
812 	/*
813 	 * If polling capability is enabled (which signifies direct
814 	 * upcall into IP and driver has ill saved as a handle),
815 	 * we need to make sure that unbind has completed before we
816 	 * let the ill disappear and driver no longer has any reference
817 	 * to this ill.
818 	 */
819 	mutex_enter(&ill->ill_lock);
820 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
821 		cv_wait(&ill->ill_cv, &ill->ill_lock);
822 	mutex_exit(&ill->ill_lock);
823 
824 	/*
825 	 * Clean up polling and soft ring capabilities
826 	 */
827 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
828 		ill_capability_dls_disable(ill);
829 
830 	if (ill->ill_net_type != IRE_LOOPBACK)
831 		qprocsoff(ill->ill_rq);
832 
833 	/*
834 	 * We do an ipsq_flush once again now. New messages could have
835 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
836 	 * could also have landed up if an ioctl thread had looked up
837 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
838 	 * enqueued the ioctl when we did the ipsq_flush last time.
839 	 */
840 	ipsq_flush(ill);
841 
842 	/*
843 	 * Free capabilities.
844 	 */
845 	if (ill->ill_ipsec_capab_ah != NULL) {
846 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
847 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
848 		ill->ill_ipsec_capab_ah = NULL;
849 	}
850 
851 	if (ill->ill_ipsec_capab_esp != NULL) {
852 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
853 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
854 		ill->ill_ipsec_capab_esp = NULL;
855 	}
856 
857 	if (ill->ill_mdt_capab != NULL) {
858 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
859 		ill->ill_mdt_capab = NULL;
860 	}
861 
862 	if (ill->ill_hcksum_capab != NULL) {
863 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
864 		ill->ill_hcksum_capab = NULL;
865 	}
866 
867 	if (ill->ill_zerocopy_capab != NULL) {
868 		kmem_free(ill->ill_zerocopy_capab,
869 		    sizeof (ill_zerocopy_capab_t));
870 		ill->ill_zerocopy_capab = NULL;
871 	}
872 
873 	if (ill->ill_lso_capab != NULL) {
874 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
875 		ill->ill_lso_capab = NULL;
876 	}
877 
878 	if (ill->ill_dls_capab != NULL) {
879 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
880 		ill->ill_dls_capab->ill_unbind_conn = NULL;
881 		kmem_free(ill->ill_dls_capab,
882 		    sizeof (ill_dls_capab_t) +
883 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
884 		ill->ill_dls_capab = NULL;
885 	}
886 
887 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
888 
889 	while (ill->ill_ipif != NULL)
890 		ipif_free_tail(ill->ill_ipif);
891 
892 	/*
893 	 * We have removed all references to ilm from conn and the ones joined
894 	 * within the kernel.
895 	 *
896 	 * We don't walk conns, mrts and ires because
897 	 *
898 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
899 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
900 	 *    ill references.
901 	 */
902 	ASSERT(ilm_walk_ill(ill) == 0);
903 	/*
904 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
905 	 * could free the phyint. No more reference to the phyint after this
906 	 * point.
907 	 */
908 	(void) ill_glist_delete(ill);
909 
910 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
911 	if (ill->ill_ndd_name != NULL)
912 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
913 	rw_exit(&ipst->ips_ip_g_nd_lock);
914 
915 	if (ill->ill_frag_ptr != NULL) {
916 		uint_t count;
917 
918 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
919 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
920 		}
921 		mi_free(ill->ill_frag_ptr);
922 		ill->ill_frag_ptr = NULL;
923 		ill->ill_frag_hash_tbl = NULL;
924 	}
925 
926 	freemsg(ill->ill_nd_lla_mp);
927 	/* Free all retained control messages. */
928 	mpp = &ill->ill_first_mp_to_free;
929 	do {
930 		while (mpp[0]) {
931 			mblk_t  *mp;
932 			mblk_t  *mp1;
933 
934 			mp = mpp[0];
935 			mpp[0] = mp->b_next;
936 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
937 				mp1->b_next = NULL;
938 				mp1->b_prev = NULL;
939 			}
940 			freemsg(mp);
941 		}
942 	} while (mpp++ != &ill->ill_last_mp_to_free);
943 
944 	ill_free_mib(ill);
945 
946 #ifdef DEBUG
947 	ill_trace_cleanup(ill);
948 #endif
949 
950 	/* Drop refcnt here */
951 	netstack_rele(ill->ill_ipst->ips_netstack);
952 	ill->ill_ipst = NULL;
953 }
954 
955 static void
956 ill_free_mib(ill_t *ill)
957 {
958 	ip_stack_t *ipst = ill->ill_ipst;
959 
960 	/*
961 	 * MIB statistics must not be lost, so when an interface
962 	 * goes away the counter values will be added to the global
963 	 * MIBs.
964 	 */
965 	if (ill->ill_ip_mib != NULL) {
966 		if (ill->ill_isv6) {
967 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
968 			    ill->ill_ip_mib);
969 		} else {
970 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
971 			    ill->ill_ip_mib);
972 		}
973 
974 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
975 		ill->ill_ip_mib = NULL;
976 	}
977 	if (ill->ill_icmp6_mib != NULL) {
978 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
979 		    ill->ill_icmp6_mib);
980 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
981 		ill->ill_icmp6_mib = NULL;
982 	}
983 }
984 
985 /*
986  * Concatenate together a physical address and a sap.
987  *
988  * Sap_lengths are interpreted as follows:
989  *   sap_length == 0	==>	no sap
990  *   sap_length > 0	==>	sap is at the head of the dlpi address
991  *   sap_length < 0	==>	sap is at the tail of the dlpi address
992  */
993 static void
994 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
995     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
996 {
997 	uint16_t sap_addr = (uint16_t)sap_src;
998 
999 	if (sap_length == 0) {
1000 		if (phys_src == NULL)
1001 			bzero(dst, phys_length);
1002 		else
1003 			bcopy(phys_src, dst, phys_length);
1004 	} else if (sap_length < 0) {
1005 		if (phys_src == NULL)
1006 			bzero(dst, phys_length);
1007 		else
1008 			bcopy(phys_src, dst, phys_length);
1009 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1010 	} else {
1011 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1012 		if (phys_src == NULL)
1013 			bzero((char *)dst + sap_length, phys_length);
1014 		else
1015 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1016 	}
1017 }
1018 
1019 /*
1020  * Generate a dl_unitdata_req mblk for the device and address given.
1021  * addr_length is the length of the physical portion of the address.
1022  * If addr is NULL include an all zero address of the specified length.
1023  * TRUE? In any case, addr_length is taken to be the entire length of the
1024  * dlpi address, including the absolute value of sap_length.
1025  */
1026 mblk_t *
1027 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1028 		t_scalar_t sap_length)
1029 {
1030 	dl_unitdata_req_t *dlur;
1031 	mblk_t	*mp;
1032 	t_scalar_t	abs_sap_length;		/* absolute value */
1033 
1034 	abs_sap_length = ABS(sap_length);
1035 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1036 	    DL_UNITDATA_REQ);
1037 	if (mp == NULL)
1038 		return (NULL);
1039 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1040 	/* HACK: accomodate incompatible DLPI drivers */
1041 	if (addr_length == 8)
1042 		addr_length = 6;
1043 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1044 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1045 	dlur->dl_priority.dl_min = 0;
1046 	dlur->dl_priority.dl_max = 0;
1047 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1048 	    (uchar_t *)&dlur[1]);
1049 	return (mp);
1050 }
1051 
1052 /*
1053  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1054  * Return an error if we already have 1 or more ioctls in progress.
1055  * This is used only for non-exclusive ioctls. Currently this is used
1056  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1057  * and thus need to use ipsq_pending_mp_add.
1058  */
1059 boolean_t
1060 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1061 {
1062 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1063 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1064 	/*
1065 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1066 	 */
1067 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1068 	    (add_mp->b_datap->db_type == M_IOCTL));
1069 
1070 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1071 	/*
1072 	 * Return error if the conn has started closing. The conn
1073 	 * could have finished cleaning up the pending mp list,
1074 	 * If so we should not add another mp to the list negating
1075 	 * the cleanup.
1076 	 */
1077 	if (connp->conn_state_flags & CONN_CLOSING)
1078 		return (B_FALSE);
1079 	/*
1080 	 * Add the pending mp to the head of the list, chained by b_next.
1081 	 * Note down the conn on which the ioctl request came, in b_prev.
1082 	 * This will be used to later get the conn, when we get a response
1083 	 * on the ill queue, from some other module (typically arp)
1084 	 */
1085 	add_mp->b_next = (void *)ill->ill_pending_mp;
1086 	add_mp->b_queue = CONNP_TO_WQ(connp);
1087 	ill->ill_pending_mp = add_mp;
1088 	if (connp != NULL)
1089 		connp->conn_oper_pending_ill = ill;
1090 	return (B_TRUE);
1091 }
1092 
1093 /*
1094  * Retrieve the ill_pending_mp and return it. We have to walk the list
1095  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1096  */
1097 mblk_t *
1098 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1099 {
1100 	mblk_t	*prev = NULL;
1101 	mblk_t	*curr = NULL;
1102 	uint_t	id;
1103 	conn_t	*connp;
1104 
1105 	/*
1106 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1107 	 * up the pending mp, but it does not know the ioc_id and
1108 	 * passes in a zero for it.
1109 	 */
1110 	mutex_enter(&ill->ill_lock);
1111 	if (ioc_id != 0)
1112 		*connpp = NULL;
1113 
1114 	/* Search the list for the appropriate ioctl based on ioc_id */
1115 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1116 	    prev = curr, curr = curr->b_next) {
1117 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1118 		connp = Q_TO_CONN(curr->b_queue);
1119 		/* Match based on the ioc_id or based on the conn */
1120 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1121 			break;
1122 	}
1123 
1124 	if (curr != NULL) {
1125 		/* Unlink the mblk from the pending mp list */
1126 		if (prev != NULL) {
1127 			prev->b_next = curr->b_next;
1128 		} else {
1129 			ASSERT(ill->ill_pending_mp == curr);
1130 			ill->ill_pending_mp = curr->b_next;
1131 		}
1132 
1133 		/*
1134 		 * conn refcnt must have been bumped up at the start of
1135 		 * the ioctl. So we can safely access the conn.
1136 		 */
1137 		ASSERT(CONN_Q(curr->b_queue));
1138 		*connpp = Q_TO_CONN(curr->b_queue);
1139 		curr->b_next = NULL;
1140 		curr->b_queue = NULL;
1141 	}
1142 
1143 	mutex_exit(&ill->ill_lock);
1144 
1145 	return (curr);
1146 }
1147 
1148 /*
1149  * Add the pending mp to the list. There can be only 1 pending mp
1150  * in the list. Any exclusive ioctl that needs to wait for a response
1151  * from another module or driver needs to use this function to set
1152  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1153  * the other module/driver. This is also used while waiting for the
1154  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1155  */
1156 boolean_t
1157 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1158     int waitfor)
1159 {
1160 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1161 
1162 	ASSERT(IAM_WRITER_IPIF(ipif));
1163 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1164 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1165 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1166 	/*
1167 	 * The caller may be using a different ipif than the one passed into
1168 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1169 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1170 	 * that `ipsq_current_ipif == ipif'.
1171 	 */
1172 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1173 
1174 	/*
1175 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1176 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1177 	 */
1178 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1179 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1180 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1181 
1182 	if (connp != NULL) {
1183 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1184 		/*
1185 		 * Return error if the conn has started closing. The conn
1186 		 * could have finished cleaning up the pending mp list,
1187 		 * If so we should not add another mp to the list negating
1188 		 * the cleanup.
1189 		 */
1190 		if (connp->conn_state_flags & CONN_CLOSING)
1191 			return (B_FALSE);
1192 	}
1193 	mutex_enter(&ipsq->ipsq_lock);
1194 	ipsq->ipsq_pending_ipif = ipif;
1195 	/*
1196 	 * Note down the queue in b_queue. This will be returned by
1197 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1198 	 * the processing
1199 	 */
1200 	add_mp->b_next = NULL;
1201 	add_mp->b_queue = q;
1202 	ipsq->ipsq_pending_mp = add_mp;
1203 	ipsq->ipsq_waitfor = waitfor;
1204 
1205 	if (connp != NULL)
1206 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1207 	mutex_exit(&ipsq->ipsq_lock);
1208 	return (B_TRUE);
1209 }
1210 
1211 /*
1212  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1213  * queued in the list.
1214  */
1215 mblk_t *
1216 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1217 {
1218 	mblk_t	*curr = NULL;
1219 
1220 	mutex_enter(&ipsq->ipsq_lock);
1221 	*connpp = NULL;
1222 	if (ipsq->ipsq_pending_mp == NULL) {
1223 		mutex_exit(&ipsq->ipsq_lock);
1224 		return (NULL);
1225 	}
1226 
1227 	/* There can be only 1 such excl message */
1228 	curr = ipsq->ipsq_pending_mp;
1229 	ASSERT(curr != NULL && curr->b_next == NULL);
1230 	ipsq->ipsq_pending_ipif = NULL;
1231 	ipsq->ipsq_pending_mp = NULL;
1232 	ipsq->ipsq_waitfor = 0;
1233 	mutex_exit(&ipsq->ipsq_lock);
1234 
1235 	if (CONN_Q(curr->b_queue)) {
1236 		/*
1237 		 * This mp did a refhold on the conn, at the start of the ioctl.
1238 		 * So we can safely return a pointer to the conn to the caller.
1239 		 */
1240 		*connpp = Q_TO_CONN(curr->b_queue);
1241 	} else {
1242 		*connpp = NULL;
1243 	}
1244 	curr->b_next = NULL;
1245 	curr->b_prev = NULL;
1246 	return (curr);
1247 }
1248 
1249 /*
1250  * Cleanup the ioctl mp queued in ipsq_pending_mp
1251  * - Called in the ill_delete path
1252  * - Called in the M_ERROR or M_HANGUP path on the ill.
1253  * - Called in the conn close path.
1254  */
1255 boolean_t
1256 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1257 {
1258 	mblk_t	*mp;
1259 	ipsq_t	*ipsq;
1260 	queue_t	*q;
1261 	ipif_t	*ipif;
1262 
1263 	ASSERT(IAM_WRITER_ILL(ill));
1264 	ipsq = ill->ill_phyint->phyint_ipsq;
1265 	mutex_enter(&ipsq->ipsq_lock);
1266 	/*
1267 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1268 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1269 	 * even if it is meant for another ill, since we have to enqueue
1270 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1271 	 * If connp is non-null we are called from the conn close path.
1272 	 */
1273 	mp = ipsq->ipsq_pending_mp;
1274 	if (mp == NULL || (connp != NULL &&
1275 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1276 		mutex_exit(&ipsq->ipsq_lock);
1277 		return (B_FALSE);
1278 	}
1279 	/* Now remove from the ipsq_pending_mp */
1280 	ipsq->ipsq_pending_mp = NULL;
1281 	q = mp->b_queue;
1282 	mp->b_next = NULL;
1283 	mp->b_prev = NULL;
1284 	mp->b_queue = NULL;
1285 
1286 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1287 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1288 	if (ill->ill_move_in_progress) {
1289 		ILL_CLEAR_MOVE(ill);
1290 	} else if (ill->ill_up_ipifs) {
1291 		ill_group_cleanup(ill);
1292 	}
1293 
1294 	ipif = ipsq->ipsq_pending_ipif;
1295 	ipsq->ipsq_pending_ipif = NULL;
1296 	ipsq->ipsq_waitfor = 0;
1297 	ipsq->ipsq_current_ipif = NULL;
1298 	ipsq->ipsq_current_ioctl = 0;
1299 	ipsq->ipsq_current_done = B_TRUE;
1300 	mutex_exit(&ipsq->ipsq_lock);
1301 
1302 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1303 		if (connp == NULL) {
1304 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1305 		} else {
1306 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1307 			mutex_enter(&ipif->ipif_ill->ill_lock);
1308 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1309 			mutex_exit(&ipif->ipif_ill->ill_lock);
1310 		}
1311 	} else {
1312 		/*
1313 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1314 		 * be just inet_freemsg. we have to restart it
1315 		 * otherwise the thread will be stuck.
1316 		 */
1317 		inet_freemsg(mp);
1318 	}
1319 	return (B_TRUE);
1320 }
1321 
1322 /*
1323  * The ill is closing. Cleanup all the pending mps. Called exclusively
1324  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1325  * knows this ill, and hence nobody can add an mp to this list
1326  */
1327 static void
1328 ill_pending_mp_cleanup(ill_t *ill)
1329 {
1330 	mblk_t	*mp;
1331 	queue_t	*q;
1332 
1333 	ASSERT(IAM_WRITER_ILL(ill));
1334 
1335 	mutex_enter(&ill->ill_lock);
1336 	/*
1337 	 * Every mp on the pending mp list originating from an ioctl
1338 	 * added 1 to the conn refcnt, at the start of the ioctl.
1339 	 * So bump it down now.  See comments in ip_wput_nondata()
1340 	 */
1341 	while (ill->ill_pending_mp != NULL) {
1342 		mp = ill->ill_pending_mp;
1343 		ill->ill_pending_mp = mp->b_next;
1344 		mutex_exit(&ill->ill_lock);
1345 
1346 		q = mp->b_queue;
1347 		ASSERT(CONN_Q(q));
1348 		mp->b_next = NULL;
1349 		mp->b_prev = NULL;
1350 		mp->b_queue = NULL;
1351 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1352 		mutex_enter(&ill->ill_lock);
1353 	}
1354 	ill->ill_pending_ipif = NULL;
1355 
1356 	mutex_exit(&ill->ill_lock);
1357 }
1358 
1359 /*
1360  * Called in the conn close path and ill delete path
1361  */
1362 static void
1363 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1364 {
1365 	ipsq_t	*ipsq;
1366 	mblk_t	*prev;
1367 	mblk_t	*curr;
1368 	mblk_t	*next;
1369 	queue_t	*q;
1370 	mblk_t	*tmp_list = NULL;
1371 
1372 	ASSERT(IAM_WRITER_ILL(ill));
1373 	if (connp != NULL)
1374 		q = CONNP_TO_WQ(connp);
1375 	else
1376 		q = ill->ill_wq;
1377 
1378 	ipsq = ill->ill_phyint->phyint_ipsq;
1379 	/*
1380 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1381 	 * In the case of ioctl from a conn, there can be only 1 mp
1382 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1383 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1384 	 * ioctls meant for this ill form conn's are not flushed. They will
1385 	 * be processed during ipsq_exit and will not find the ill and will
1386 	 * return error.
1387 	 */
1388 	mutex_enter(&ipsq->ipsq_lock);
1389 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1390 	    curr = next) {
1391 		next = curr->b_next;
1392 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1393 			/* Unlink the mblk from the pending mp list */
1394 			if (prev != NULL) {
1395 				prev->b_next = curr->b_next;
1396 			} else {
1397 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1398 				ipsq->ipsq_xopq_mphead = curr->b_next;
1399 			}
1400 			if (ipsq->ipsq_xopq_mptail == curr)
1401 				ipsq->ipsq_xopq_mptail = prev;
1402 			/*
1403 			 * Create a temporary list and release the ipsq lock
1404 			 * New elements are added to the head of the tmp_list
1405 			 */
1406 			curr->b_next = tmp_list;
1407 			tmp_list = curr;
1408 		} else {
1409 			prev = curr;
1410 		}
1411 	}
1412 	mutex_exit(&ipsq->ipsq_lock);
1413 
1414 	while (tmp_list != NULL) {
1415 		curr = tmp_list;
1416 		tmp_list = curr->b_next;
1417 		curr->b_next = NULL;
1418 		curr->b_prev = NULL;
1419 		curr->b_queue = NULL;
1420 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1421 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1422 			    CONN_CLOSE : NO_COPYOUT, NULL);
1423 		} else {
1424 			/*
1425 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1426 			 * this can't be just inet_freemsg. we have to
1427 			 * restart it otherwise the thread will be stuck.
1428 			 */
1429 			inet_freemsg(curr);
1430 		}
1431 	}
1432 }
1433 
1434 /*
1435  * This conn has started closing. Cleanup any pending ioctl from this conn.
1436  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1437  */
1438 void
1439 conn_ioctl_cleanup(conn_t *connp)
1440 {
1441 	mblk_t *curr;
1442 	ipsq_t	*ipsq;
1443 	ill_t	*ill;
1444 	boolean_t refheld;
1445 
1446 	/*
1447 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1448 	 * ioctl has not yet started, the mp is pending in the list headed by
1449 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1450 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1451 	 * is currently executing now the mp is not queued anywhere but
1452 	 * conn_oper_pending_ill is null. The conn close will wait
1453 	 * till the conn_ref drops to zero.
1454 	 */
1455 	mutex_enter(&connp->conn_lock);
1456 	ill = connp->conn_oper_pending_ill;
1457 	if (ill == NULL) {
1458 		mutex_exit(&connp->conn_lock);
1459 		return;
1460 	}
1461 
1462 	curr = ill_pending_mp_get(ill, &connp, 0);
1463 	if (curr != NULL) {
1464 		mutex_exit(&connp->conn_lock);
1465 		CONN_DEC_REF(connp);
1466 		inet_freemsg(curr);
1467 		return;
1468 	}
1469 	/*
1470 	 * We may not be able to refhold the ill if the ill/ipif
1471 	 * is changing. But we need to make sure that the ill will
1472 	 * not vanish. So we just bump up the ill_waiter count.
1473 	 */
1474 	refheld = ill_waiter_inc(ill);
1475 	mutex_exit(&connp->conn_lock);
1476 	if (refheld) {
1477 		if (ipsq_enter(ill, B_TRUE)) {
1478 			ill_waiter_dcr(ill);
1479 			/*
1480 			 * Check whether this ioctl has started and is
1481 			 * pending now in ipsq_pending_mp. If it is not
1482 			 * found there then check whether this ioctl has
1483 			 * not even started and is in the ipsq_xopq list.
1484 			 */
1485 			if (!ipsq_pending_mp_cleanup(ill, connp))
1486 				ipsq_xopq_mp_cleanup(ill, connp);
1487 			ipsq = ill->ill_phyint->phyint_ipsq;
1488 			ipsq_exit(ipsq);
1489 			return;
1490 		}
1491 	}
1492 
1493 	/*
1494 	 * The ill is also closing and we could not bump up the
1495 	 * ill_waiter_count or we could not enter the ipsq. Leave
1496 	 * the cleanup to ill_delete
1497 	 */
1498 	mutex_enter(&connp->conn_lock);
1499 	while (connp->conn_oper_pending_ill != NULL)
1500 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1501 	mutex_exit(&connp->conn_lock);
1502 	if (refheld)
1503 		ill_waiter_dcr(ill);
1504 }
1505 
1506 /*
1507  * ipcl_walk function for cleaning up conn_*_ill fields.
1508  */
1509 static void
1510 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1511 {
1512 	ill_t	*ill = (ill_t *)arg;
1513 	ire_t	*ire;
1514 
1515 	mutex_enter(&connp->conn_lock);
1516 	if (connp->conn_multicast_ill == ill) {
1517 		/* Revert to late binding */
1518 		connp->conn_multicast_ill = NULL;
1519 		connp->conn_orig_multicast_ifindex = 0;
1520 	}
1521 	if (connp->conn_incoming_ill == ill)
1522 		connp->conn_incoming_ill = NULL;
1523 	if (connp->conn_outgoing_ill == ill)
1524 		connp->conn_outgoing_ill = NULL;
1525 	if (connp->conn_outgoing_pill == ill)
1526 		connp->conn_outgoing_pill = NULL;
1527 	if (connp->conn_nofailover_ill == ill)
1528 		connp->conn_nofailover_ill = NULL;
1529 	if (connp->conn_dhcpinit_ill == ill) {
1530 		connp->conn_dhcpinit_ill = NULL;
1531 		ASSERT(ill->ill_dhcpinit != 0);
1532 		atomic_dec_32(&ill->ill_dhcpinit);
1533 	}
1534 	if (connp->conn_ire_cache != NULL) {
1535 		ire = connp->conn_ire_cache;
1536 		/*
1537 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1538 		 * interface X and ipif coming from interface Y, if interface
1539 		 * X and Y are part of the same IPMPgroup. Thus whenever
1540 		 * interface X goes down, remove all references to it by
1541 		 * checking both on ire_ipif and ire_stq.
1542 		 */
1543 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1544 		    (ire->ire_type == IRE_CACHE &&
1545 		    ire->ire_stq == ill->ill_wq)) {
1546 			connp->conn_ire_cache = NULL;
1547 			mutex_exit(&connp->conn_lock);
1548 			ire_refrele_notr(ire);
1549 			return;
1550 		}
1551 	}
1552 	mutex_exit(&connp->conn_lock);
1553 }
1554 
1555 /* ARGSUSED */
1556 void
1557 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1558 {
1559 	ill_t	*ill = q->q_ptr;
1560 	ipif_t	*ipif;
1561 
1562 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1563 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1564 		ipif_non_duplicate(ipif);
1565 		ipif_down_tail(ipif);
1566 	}
1567 	freemsg(mp);
1568 	ipsq_current_finish(ipsq);
1569 }
1570 
1571 /*
1572  * ill_down_start is called when we want to down this ill and bring it up again
1573  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1574  * all interfaces, but don't tear down any plumbing.
1575  */
1576 boolean_t
1577 ill_down_start(queue_t *q, mblk_t *mp)
1578 {
1579 	ill_t	*ill = q->q_ptr;
1580 	ipif_t	*ipif;
1581 
1582 	ASSERT(IAM_WRITER_ILL(ill));
1583 
1584 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1585 		(void) ipif_down(ipif, NULL, NULL);
1586 
1587 	ill_down(ill);
1588 
1589 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1590 
1591 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1592 
1593 	/*
1594 	 * Atomically test and add the pending mp if references are active.
1595 	 */
1596 	mutex_enter(&ill->ill_lock);
1597 	if (!ill_is_quiescent(ill)) {
1598 		/* call cannot fail since `conn_t *' argument is NULL */
1599 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1600 		    mp, ILL_DOWN);
1601 		mutex_exit(&ill->ill_lock);
1602 		return (B_FALSE);
1603 	}
1604 	mutex_exit(&ill->ill_lock);
1605 	return (B_TRUE);
1606 }
1607 
1608 static void
1609 ill_down(ill_t *ill)
1610 {
1611 	ip_stack_t	*ipst = ill->ill_ipst;
1612 
1613 	/* Blow off any IREs dependent on this ILL. */
1614 	ire_walk(ill_downi, (char *)ill, ipst);
1615 
1616 	/* Remove any conn_*_ill depending on this ill */
1617 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1618 
1619 	if (ill->ill_group != NULL) {
1620 		illgrp_delete(ill);
1621 	}
1622 }
1623 
1624 /*
1625  * ire_walk routine used to delete every IRE that depends on queues
1626  * associated with 'ill'.  (Always called as writer.)
1627  */
1628 static void
1629 ill_downi(ire_t *ire, char *ill_arg)
1630 {
1631 	ill_t	*ill = (ill_t *)ill_arg;
1632 
1633 	/*
1634 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1635 	 * interface X and ipif coming from interface Y, if interface
1636 	 * X and Y are part of the same IPMP group. Thus whenever interface
1637 	 * X goes down, remove all references to it by checking both
1638 	 * on ire_ipif and ire_stq.
1639 	 */
1640 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1641 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1642 		ire_delete(ire);
1643 	}
1644 }
1645 
1646 /*
1647  * Remove ire/nce from the fastpath list.
1648  */
1649 void
1650 ill_fastpath_nack(ill_t *ill)
1651 {
1652 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1653 }
1654 
1655 /* Consume an M_IOCACK of the fastpath probe. */
1656 void
1657 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1658 {
1659 	mblk_t	*mp1 = mp;
1660 
1661 	/*
1662 	 * If this was the first attempt turn on the fastpath probing.
1663 	 */
1664 	mutex_enter(&ill->ill_lock);
1665 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1666 		ill->ill_dlpi_fastpath_state = IDS_OK;
1667 	mutex_exit(&ill->ill_lock);
1668 
1669 	/* Free the M_IOCACK mblk, hold on to the data */
1670 	mp = mp->b_cont;
1671 	freeb(mp1);
1672 	if (mp == NULL)
1673 		return;
1674 	if (mp->b_cont != NULL) {
1675 		/*
1676 		 * Update all IRE's or NCE's that are waiting for
1677 		 * fastpath update.
1678 		 */
1679 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1680 		mp1 = mp->b_cont;
1681 		freeb(mp);
1682 		mp = mp1;
1683 	} else {
1684 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1685 	}
1686 
1687 	freeb(mp);
1688 }
1689 
1690 /*
1691  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1692  * The data portion of the request is a dl_unitdata_req_t template for
1693  * what we would send downstream in the absence of a fastpath confirmation.
1694  */
1695 int
1696 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1697 {
1698 	struct iocblk	*ioc;
1699 	mblk_t	*mp;
1700 
1701 	if (dlur_mp == NULL)
1702 		return (EINVAL);
1703 
1704 	mutex_enter(&ill->ill_lock);
1705 	switch (ill->ill_dlpi_fastpath_state) {
1706 	case IDS_FAILED:
1707 		/*
1708 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1709 		 * support it.
1710 		 */
1711 		mutex_exit(&ill->ill_lock);
1712 		return (ENOTSUP);
1713 	case IDS_UNKNOWN:
1714 		/* This is the first probe */
1715 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1716 		break;
1717 	default:
1718 		break;
1719 	}
1720 	mutex_exit(&ill->ill_lock);
1721 
1722 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1723 		return (EAGAIN);
1724 
1725 	mp->b_cont = copyb(dlur_mp);
1726 	if (mp->b_cont == NULL) {
1727 		freeb(mp);
1728 		return (EAGAIN);
1729 	}
1730 
1731 	ioc = (struct iocblk *)mp->b_rptr;
1732 	ioc->ioc_count = msgdsize(mp->b_cont);
1733 
1734 	putnext(ill->ill_wq, mp);
1735 	return (0);
1736 }
1737 
1738 void
1739 ill_capability_probe(ill_t *ill)
1740 {
1741 	/*
1742 	 * Do so only if capabilities are still unknown.
1743 	 */
1744 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
1745 		return;
1746 
1747 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1748 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1749 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1750 }
1751 
1752 void
1753 ill_capability_reset(ill_t *ill)
1754 {
1755 	mblk_t *sc_mp = NULL;
1756 	mblk_t *tmp;
1757 
1758 	/*
1759 	 * Note here that we reset the state to UNKNOWN, and later send
1760 	 * down the DL_CAPABILITY_REQ without first setting the state to
1761 	 * INPROGRESS.  We do this in order to distinguish the
1762 	 * DL_CAPABILITY_ACK response which may come back in response to
1763 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1764 	 * also handle the case where the driver doesn't send us back
1765 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1766 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1767 	 * features are turned off until the state reaches IDS_OK.
1768 	 */
1769 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1770 	ill->ill_capab_reneg = B_FALSE;
1771 
1772 	/*
1773 	 * Disable sub-capabilities and request a list of sub-capability
1774 	 * messages which will be sent down to the driver.  Each handler
1775 	 * allocates the corresponding dl_capability_sub_t inside an
1776 	 * mblk, and links it to the existing sc_mp mblk, or return it
1777 	 * as sc_mp if it's the first sub-capability (the passed in
1778 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1779 	 * sc_mp will be pulled-up, before passing it downstream.
1780 	 */
1781 	ill_capability_mdt_reset(ill, &sc_mp);
1782 	ill_capability_hcksum_reset(ill, &sc_mp);
1783 	ill_capability_zerocopy_reset(ill, &sc_mp);
1784 	ill_capability_ipsec_reset(ill, &sc_mp);
1785 	ill_capability_dls_reset(ill, &sc_mp);
1786 	ill_capability_lso_reset(ill, &sc_mp);
1787 
1788 	/* Nothing to send down in order to disable the capabilities? */
1789 	if (sc_mp == NULL)
1790 		return;
1791 
1792 	tmp = msgpullup(sc_mp, -1);
1793 	freemsg(sc_mp);
1794 	if ((sc_mp = tmp) == NULL) {
1795 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1796 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1797 		return;
1798 	}
1799 
1800 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1801 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1802 }
1803 
1804 /*
1805  * Request or set new-style hardware capabilities supported by DLS provider.
1806  */
1807 static void
1808 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1809 {
1810 	mblk_t *mp;
1811 	dl_capability_req_t *capb;
1812 	size_t size = 0;
1813 	uint8_t *ptr;
1814 
1815 	if (reqp != NULL)
1816 		size = MBLKL(reqp);
1817 
1818 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1819 	if (mp == NULL) {
1820 		freemsg(reqp);
1821 		return;
1822 	}
1823 	ptr = mp->b_rptr;
1824 
1825 	capb = (dl_capability_req_t *)ptr;
1826 	ptr += sizeof (dl_capability_req_t);
1827 
1828 	if (reqp != NULL) {
1829 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1830 		capb->dl_sub_length = size;
1831 		bcopy(reqp->b_rptr, ptr, size);
1832 		ptr += size;
1833 		mp->b_cont = reqp->b_cont;
1834 		freeb(reqp);
1835 	}
1836 	ASSERT(ptr == mp->b_wptr);
1837 
1838 	ill_dlpi_send(ill, 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 
1944 			if (*ill_mdt_capab == NULL) {
1945 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1946 				    "could not enable MDT version %d "
1947 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1948 				    ill->ill_name);
1949 				return;
1950 			}
1951 		}
1952 
1953 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1954 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1955 		    "header spaces, %d max pld bufs, %d span limit)\n",
1956 		    ill->ill_name, MDT_VERSION_2,
1957 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1958 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1959 
1960 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1961 		(*ill_mdt_capab)->ill_mdt_on = 1;
1962 		/*
1963 		 * Round the following values to the nearest 32-bit; ULP
1964 		 * may further adjust them to accomodate for additional
1965 		 * protocol headers.  We pass these values to ULP during
1966 		 * bind time.
1967 		 */
1968 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1969 		    roundup(mdt_ic->mdt_hdr_head, 4);
1970 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1971 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1972 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1973 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1974 
1975 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1976 	} else {
1977 		uint_t size;
1978 		uchar_t *rptr;
1979 
1980 		size = sizeof (dl_capability_req_t) +
1981 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1982 
1983 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1984 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1985 			    "could not enable MDT for %s (ENOMEM)\n",
1986 			    ill->ill_name);
1987 			return;
1988 		}
1989 
1990 		rptr = nmp->b_rptr;
1991 		/* initialize dl_capability_req_t */
1992 		oc = (dl_capability_req_t *)nmp->b_rptr;
1993 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1994 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1995 		    sizeof (dl_capab_mdt_t);
1996 		nmp->b_rptr += sizeof (dl_capability_req_t);
1997 
1998 		/* initialize dl_capability_sub_t */
1999 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2000 		nmp->b_rptr += sizeof (*isub);
2001 
2002 		/* initialize dl_capab_mdt_t */
2003 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2004 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2005 
2006 		nmp->b_rptr = rptr;
2007 
2008 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2009 		    "to enable MDT version %d\n", ill->ill_name,
2010 		    MDT_VERSION_2));
2011 
2012 		/* set ENABLE flag */
2013 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2014 
2015 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2016 		ill_dlpi_send(ill, nmp);
2017 	}
2018 }
2019 
2020 static void
2021 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2022 {
2023 	mblk_t *mp;
2024 	dl_capab_mdt_t *mdt_subcap;
2025 	dl_capability_sub_t *dl_subcap;
2026 	int size;
2027 
2028 	if (!ILL_MDT_CAPABLE(ill))
2029 		return;
2030 
2031 	ASSERT(ill->ill_mdt_capab != NULL);
2032 	/*
2033 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2034 	 * structure since it's possible that another thread is still
2035 	 * referring to it.  The structure only gets deallocated when
2036 	 * we destroy the ill.
2037 	 */
2038 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2039 
2040 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2041 
2042 	mp = allocb(size, BPRI_HI);
2043 	if (mp == NULL) {
2044 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2045 		    "request to disable MDT\n"));
2046 		return;
2047 	}
2048 
2049 	mp->b_wptr = mp->b_rptr + size;
2050 
2051 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2052 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2053 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2054 
2055 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2056 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2057 	mdt_subcap->mdt_flags = 0;
2058 	mdt_subcap->mdt_hdr_head = 0;
2059 	mdt_subcap->mdt_hdr_tail = 0;
2060 
2061 	if (*sc_mp != NULL)
2062 		linkb(*sc_mp, mp);
2063 	else
2064 		*sc_mp = mp;
2065 }
2066 
2067 /*
2068  * Send a DL_NOTIFY_REQ to the specified ill to enable
2069  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2070  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2071  * acceleration.
2072  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2073  */
2074 static boolean_t
2075 ill_enable_promisc_notify(ill_t *ill)
2076 {
2077 	mblk_t *mp;
2078 	dl_notify_req_t *req;
2079 
2080 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2081 
2082 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2083 	if (mp == NULL)
2084 		return (B_FALSE);
2085 
2086 	req = (dl_notify_req_t *)mp->b_rptr;
2087 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2088 	    DL_NOTE_PROMISC_OFF_PHYS;
2089 
2090 	ill_dlpi_send(ill, mp);
2091 
2092 	return (B_TRUE);
2093 }
2094 
2095 /*
2096  * Allocate an IPsec capability request which will be filled by our
2097  * caller to turn on support for one or more algorithms.
2098  */
2099 static mblk_t *
2100 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2101 {
2102 	mblk_t *nmp;
2103 	dl_capability_req_t	*ocap;
2104 	dl_capab_ipsec_t	*ocip;
2105 	dl_capab_ipsec_t	*icip;
2106 	uint8_t			*ptr;
2107 	icip = (dl_capab_ipsec_t *)(isub + 1);
2108 
2109 	/*
2110 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2111 	 * PROMISC_ON/OFF notification from the provider. We need to
2112 	 * do this before enabling the algorithms to avoid leakage of
2113 	 * cleartext packets.
2114 	 */
2115 
2116 	if (!ill_enable_promisc_notify(ill))
2117 		return (NULL);
2118 
2119 	/*
2120 	 * Allocate new mblk which will contain a new capability
2121 	 * request to enable the capabilities.
2122 	 */
2123 
2124 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2125 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2126 	if (nmp == NULL)
2127 		return (NULL);
2128 
2129 	ptr = nmp->b_rptr;
2130 
2131 	/* initialize dl_capability_req_t */
2132 	ocap = (dl_capability_req_t *)ptr;
2133 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2134 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2135 	ptr += sizeof (dl_capability_req_t);
2136 
2137 	/* initialize dl_capability_sub_t */
2138 	bcopy(isub, ptr, sizeof (*isub));
2139 	ptr += sizeof (*isub);
2140 
2141 	/* initialize dl_capab_ipsec_t */
2142 	ocip = (dl_capab_ipsec_t *)ptr;
2143 	bcopy(icip, ocip, sizeof (*icip));
2144 
2145 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2146 	return (nmp);
2147 }
2148 
2149 /*
2150  * Process an IPsec capability negotiation ack received from a DLS Provider.
2151  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2152  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2153  */
2154 static void
2155 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2156 {
2157 	dl_capab_ipsec_t	*icip;
2158 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2159 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2160 	uint_t cipher, nciphers;
2161 	mblk_t *nmp;
2162 	uint_t alg_len;
2163 	boolean_t need_sadb_dump;
2164 	uint_t sub_dl_cap = isub->dl_cap;
2165 	ill_ipsec_capab_t **ill_capab;
2166 	uint64_t ill_capab_flag;
2167 	uint8_t *capend, *ciphend;
2168 	boolean_t sadb_resync;
2169 
2170 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2171 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2172 
2173 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2174 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2175 		ill_capab_flag = ILL_CAPAB_AH;
2176 	} else {
2177 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2178 		ill_capab_flag = ILL_CAPAB_ESP;
2179 	}
2180 
2181 	/*
2182 	 * If the ill capability structure exists, then this incoming
2183 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2184 	 * If this is so, then we'd need to resynchronize the SADB
2185 	 * after re-enabling the offloaded ciphers.
2186 	 */
2187 	sadb_resync = (*ill_capab != NULL);
2188 
2189 	/*
2190 	 * Note: range checks here are not absolutely sufficient to
2191 	 * make us robust against malformed messages sent by drivers;
2192 	 * this is in keeping with the rest of IP's dlpi handling.
2193 	 * (Remember, it's coming from something else in the kernel
2194 	 * address space)
2195 	 */
2196 
2197 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2198 	if (capend > mp->b_wptr) {
2199 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2200 		    "malformed sub-capability too long for mblk");
2201 		return;
2202 	}
2203 
2204 	/*
2205 	 * There are two types of acks we process here:
2206 	 * 1. acks in reply to a (first form) generic capability req
2207 	 *    (no ENABLE flag set)
2208 	 * 2. acks in reply to a ENABLE capability req.
2209 	 *    (ENABLE flag set)
2210 	 *
2211 	 * We process the subcapability passed as argument as follows:
2212 	 * 1 do initializations
2213 	 *   1.1 initialize nmp = NULL
2214 	 *   1.2 set need_sadb_dump to B_FALSE
2215 	 * 2 for each cipher in subcapability:
2216 	 *   2.1 if ENABLE flag is set:
2217 	 *	2.1.1 update per-ill ipsec capabilities info
2218 	 *	2.1.2 set need_sadb_dump to B_TRUE
2219 	 *   2.2 if ENABLE flag is not set:
2220 	 *	2.2.1 if nmp is NULL:
2221 	 *		2.2.1.1 allocate and initialize nmp
2222 	 *		2.2.1.2 init current pos in nmp
2223 	 *	2.2.2 copy current cipher to current pos in nmp
2224 	 *	2.2.3 set ENABLE flag in nmp
2225 	 *	2.2.4 update current pos
2226 	 * 3 if nmp is not equal to NULL, send enable request
2227 	 *   3.1 send capability request
2228 	 * 4 if need_sadb_dump is B_TRUE
2229 	 *   4.1 enable promiscuous on/off notifications
2230 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2231 	 *	AH or ESP SA's to interface.
2232 	 */
2233 
2234 	nmp = NULL;
2235 	oalg = NULL;
2236 	need_sadb_dump = B_FALSE;
2237 	icip = (dl_capab_ipsec_t *)(isub + 1);
2238 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2239 
2240 	nciphers = icip->cip_nciphers;
2241 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2242 
2243 	if (ciphend > capend) {
2244 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2245 		    "too many ciphers for sub-capability len");
2246 		return;
2247 	}
2248 
2249 	for (cipher = 0; cipher < nciphers; cipher++) {
2250 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2251 
2252 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2253 			/*
2254 			 * TBD: when we provide a way to disable capabilities
2255 			 * from above, need to manage the request-pending state
2256 			 * and fail if we were not expecting this ACK.
2257 			 */
2258 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2259 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2260 
2261 			/*
2262 			 * Update IPsec capabilities for this ill
2263 			 */
2264 
2265 			if (*ill_capab == NULL) {
2266 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2267 				    ("ill_capability_ipsec_ack: "
2268 				    "allocating ipsec_capab for ill\n"));
2269 				*ill_capab = ill_ipsec_capab_alloc();
2270 
2271 				if (*ill_capab == NULL) {
2272 					cmn_err(CE_WARN,
2273 					    "ill_capability_ipsec_ack: "
2274 					    "could not enable IPsec Hardware "
2275 					    "acceleration for %s (ENOMEM)\n",
2276 					    ill->ill_name);
2277 					return;
2278 				}
2279 			}
2280 
2281 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2282 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2283 
2284 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2285 				cmn_err(CE_WARN,
2286 				    "ill_capability_ipsec_ack: "
2287 				    "malformed IPsec algorithm id %d",
2288 				    ialg->alg_prim);
2289 				continue;
2290 			}
2291 
2292 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2293 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2294 				    ialg->alg_prim);
2295 			} else {
2296 				ipsec_capab_algparm_t *alp;
2297 
2298 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2299 				    ialg->alg_prim);
2300 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2301 				    ialg->alg_prim)) {
2302 					cmn_err(CE_WARN,
2303 					    "ill_capability_ipsec_ack: "
2304 					    "no space for IPsec alg id %d",
2305 					    ialg->alg_prim);
2306 					continue;
2307 				}
2308 				alp = &((*ill_capab)->encr_algparm[
2309 				    ialg->alg_prim]);
2310 				alp->minkeylen = ialg->alg_minbits;
2311 				alp->maxkeylen = ialg->alg_maxbits;
2312 			}
2313 			ill->ill_capabilities |= ill_capab_flag;
2314 			/*
2315 			 * indicate that a capability was enabled, which
2316 			 * will be used below to kick off a SADB dump
2317 			 * to the ill.
2318 			 */
2319 			need_sadb_dump = B_TRUE;
2320 		} else {
2321 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2322 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2323 			    ialg->alg_prim));
2324 
2325 			if (nmp == NULL) {
2326 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2327 				if (nmp == NULL) {
2328 					/*
2329 					 * Sending the PROMISC_ON/OFF
2330 					 * notification request failed.
2331 					 * We cannot enable the algorithms
2332 					 * since the Provider will not
2333 					 * notify IP of promiscous mode
2334 					 * changes, which could lead
2335 					 * to leakage of packets.
2336 					 */
2337 					cmn_err(CE_WARN,
2338 					    "ill_capability_ipsec_ack: "
2339 					    "could not enable IPsec Hardware "
2340 					    "acceleration for %s (ENOMEM)\n",
2341 					    ill->ill_name);
2342 					return;
2343 				}
2344 				/* ptr to current output alg specifier */
2345 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2346 			}
2347 
2348 			/*
2349 			 * Copy current alg specifier, set ENABLE
2350 			 * flag, and advance to next output alg.
2351 			 * For now we enable all IPsec capabilities.
2352 			 */
2353 			ASSERT(oalg != NULL);
2354 			bcopy(ialg, oalg, alg_len);
2355 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2356 			nmp->b_wptr += alg_len;
2357 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2358 		}
2359 
2360 		/* move to next input algorithm specifier */
2361 		ialg = (dl_capab_ipsec_alg_t *)
2362 		    ((char *)ialg + alg_len);
2363 	}
2364 
2365 	if (nmp != NULL)
2366 		/*
2367 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2368 		 * IPsec hardware acceleration.
2369 		 */
2370 		ill_dlpi_send(ill, nmp);
2371 
2372 	if (need_sadb_dump)
2373 		/*
2374 		 * An acknowledgement corresponding to a request to
2375 		 * enable acceleration was received, notify SADB.
2376 		 */
2377 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2378 }
2379 
2380 /*
2381  * Given an mblk with enough space in it, create sub-capability entries for
2382  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2383  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2384  * in preparation for the reset the DL_CAPABILITY_REQ message.
2385  */
2386 static void
2387 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2388     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2389 {
2390 	dl_capab_ipsec_t *oipsec;
2391 	dl_capab_ipsec_alg_t *oalg;
2392 	dl_capability_sub_t *dl_subcap;
2393 	int i, k;
2394 
2395 	ASSERT(nciphers > 0);
2396 	ASSERT(ill_cap != NULL);
2397 	ASSERT(mp != NULL);
2398 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2399 
2400 	/* dl_capability_sub_t for "stype" */
2401 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2402 	dl_subcap->dl_cap = stype;
2403 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2404 	mp->b_wptr += sizeof (dl_capability_sub_t);
2405 
2406 	/* dl_capab_ipsec_t for "stype" */
2407 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2408 	oipsec->cip_version = 1;
2409 	oipsec->cip_nciphers = nciphers;
2410 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2411 
2412 	/* create entries for "stype" AUTH ciphers */
2413 	for (i = 0; i < ill_cap->algs_size; i++) {
2414 		for (k = 0; k < BITSPERBYTE; k++) {
2415 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2416 				continue;
2417 
2418 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2419 			bzero((void *)oalg, sizeof (*oalg));
2420 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2421 			oalg->alg_prim = k + (BITSPERBYTE * i);
2422 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2423 		}
2424 	}
2425 	/* create entries for "stype" ENCR ciphers */
2426 	for (i = 0; i < ill_cap->algs_size; i++) {
2427 		for (k = 0; k < BITSPERBYTE; k++) {
2428 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2429 				continue;
2430 
2431 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2432 			bzero((void *)oalg, sizeof (*oalg));
2433 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2434 			oalg->alg_prim = k + (BITSPERBYTE * i);
2435 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2436 		}
2437 	}
2438 }
2439 
2440 /*
2441  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2442  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2443  * POPC instruction, but our macro is more flexible for an arbitrary length
2444  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2445  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2446  * stays that way, we can reduce the number of iterations required.
2447  */
2448 #define	COUNT_1S(val, sum) {					\
2449 	uint8_t x = val & 0xff;					\
2450 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2451 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2452 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2453 }
2454 
2455 /* ARGSUSED */
2456 static void
2457 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2458 {
2459 	mblk_t *mp;
2460 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2461 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2462 	uint64_t ill_capabilities = ill->ill_capabilities;
2463 	int ah_cnt = 0, esp_cnt = 0;
2464 	int ah_len = 0, esp_len = 0;
2465 	int i, size = 0;
2466 
2467 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2468 		return;
2469 
2470 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2471 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2472 
2473 	/* Find out the number of ciphers for AH */
2474 	if (cap_ah != NULL) {
2475 		for (i = 0; i < cap_ah->algs_size; i++) {
2476 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2477 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2478 		}
2479 		if (ah_cnt > 0) {
2480 			size += sizeof (dl_capability_sub_t) +
2481 			    sizeof (dl_capab_ipsec_t);
2482 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2483 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2484 			size += ah_len;
2485 		}
2486 	}
2487 
2488 	/* Find out the number of ciphers for ESP */
2489 	if (cap_esp != NULL) {
2490 		for (i = 0; i < cap_esp->algs_size; i++) {
2491 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2492 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2493 		}
2494 		if (esp_cnt > 0) {
2495 			size += sizeof (dl_capability_sub_t) +
2496 			    sizeof (dl_capab_ipsec_t);
2497 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2498 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2499 			size += esp_len;
2500 		}
2501 	}
2502 
2503 	if (size == 0) {
2504 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2505 		    "there's nothing to reset\n"));
2506 		return;
2507 	}
2508 
2509 	mp = allocb(size, BPRI_HI);
2510 	if (mp == NULL) {
2511 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2512 		    "request to disable IPSEC Hardware Acceleration\n"));
2513 		return;
2514 	}
2515 
2516 	/*
2517 	 * Clear the capability flags for IPsec HA but retain the ill
2518 	 * capability structures since it's possible that another thread
2519 	 * is still referring to them.  The structures only get deallocated
2520 	 * when we destroy the ill.
2521 	 *
2522 	 * Various places check the flags to see if the ill is capable of
2523 	 * hardware acceleration, and by clearing them we ensure that new
2524 	 * outbound IPsec packets are sent down encrypted.
2525 	 */
2526 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2527 
2528 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2529 	if (ah_cnt > 0) {
2530 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2531 		    cap_ah, mp);
2532 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2533 	}
2534 
2535 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2536 	if (esp_cnt > 0) {
2537 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2538 		    cap_esp, mp);
2539 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2540 	}
2541 
2542 	/*
2543 	 * At this point we've composed a bunch of sub-capabilities to be
2544 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2545 	 * by the caller.  Upon receiving this reset message, the driver
2546 	 * must stop inbound decryption (by destroying all inbound SAs)
2547 	 * and let the corresponding packets come in encrypted.
2548 	 */
2549 
2550 	if (*sc_mp != NULL)
2551 		linkb(*sc_mp, mp);
2552 	else
2553 		*sc_mp = mp;
2554 }
2555 
2556 static void
2557 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2558     boolean_t encapsulated)
2559 {
2560 	boolean_t legacy = B_FALSE;
2561 
2562 	/*
2563 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2564 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2565 	 * instructed the driver to disable its advertised capabilities,
2566 	 * so there's no point in accepting any response at this moment.
2567 	 */
2568 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2569 		return;
2570 
2571 	/*
2572 	 * Note that only the following two sub-capabilities may be
2573 	 * considered as "legacy", since their original definitions
2574 	 * do not incorporate the dl_mid_t module ID token, and hence
2575 	 * may require the use of the wrapper sub-capability.
2576 	 */
2577 	switch (subp->dl_cap) {
2578 	case DL_CAPAB_IPSEC_AH:
2579 	case DL_CAPAB_IPSEC_ESP:
2580 		legacy = B_TRUE;
2581 		break;
2582 	}
2583 
2584 	/*
2585 	 * For legacy sub-capabilities which don't incorporate a queue_t
2586 	 * pointer in their structures, discard them if we detect that
2587 	 * there are intermediate modules in between IP and the driver.
2588 	 */
2589 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2590 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2591 		    "%d discarded; %d module(s) present below IP\n",
2592 		    subp->dl_cap, ill->ill_lmod_cnt));
2593 		return;
2594 	}
2595 
2596 	switch (subp->dl_cap) {
2597 	case DL_CAPAB_IPSEC_AH:
2598 	case DL_CAPAB_IPSEC_ESP:
2599 		ill_capability_ipsec_ack(ill, mp, subp);
2600 		break;
2601 	case DL_CAPAB_MDT:
2602 		ill_capability_mdt_ack(ill, mp, subp);
2603 		break;
2604 	case DL_CAPAB_HCKSUM:
2605 		ill_capability_hcksum_ack(ill, mp, subp);
2606 		break;
2607 	case DL_CAPAB_ZEROCOPY:
2608 		ill_capability_zerocopy_ack(ill, mp, subp);
2609 		break;
2610 	case DL_CAPAB_POLL:
2611 		if (!SOFT_RINGS_ENABLED())
2612 			ill_capability_dls_ack(ill, mp, subp);
2613 		break;
2614 	case DL_CAPAB_SOFT_RING:
2615 		if (SOFT_RINGS_ENABLED())
2616 			ill_capability_dls_ack(ill, mp, subp);
2617 		break;
2618 	case DL_CAPAB_LSO:
2619 		ill_capability_lso_ack(ill, mp, subp);
2620 		break;
2621 	default:
2622 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2623 		    subp->dl_cap));
2624 	}
2625 }
2626 
2627 /*
2628  * As part of negotiating polling capability, the driver tells us
2629  * the default (or normal) blanking interval and packet threshold
2630  * (the receive timer fires if blanking interval is reached or
2631  * the packet threshold is reached).
2632  *
2633  * As part of manipulating the polling interval, we always use our
2634  * estimated interval (avg service time * number of packets queued
2635  * on the squeue) but we try to blank for a minimum of
2636  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2637  * packet threshold during this time. When we are not in polling mode
2638  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2639  * rr_min_blank_ratio but up the packet cnt by a ratio of
2640  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2641  * possible although for a shorter interval.
2642  */
2643 #define	RR_MAX_BLANK_RATIO	20
2644 #define	RR_MIN_BLANK_RATIO	10
2645 #define	RR_MAX_PKT_CNT_RATIO	3
2646 #define	RR_MIN_PKT_CNT_RATIO	3
2647 
2648 /*
2649  * These can be tuned via /etc/system.
2650  */
2651 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2652 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2653 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2654 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2655 
2656 static mac_resource_handle_t
2657 ill_ring_add(void *arg, mac_resource_t *mrp)
2658 {
2659 	ill_t			*ill = (ill_t *)arg;
2660 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2661 	ill_rx_ring_t		*rx_ring;
2662 	int			ip_rx_index;
2663 
2664 	ASSERT(mrp != NULL);
2665 	if (mrp->mr_type != MAC_RX_FIFO) {
2666 		return (NULL);
2667 	}
2668 	ASSERT(ill != NULL);
2669 	ASSERT(ill->ill_dls_capab != NULL);
2670 
2671 	mutex_enter(&ill->ill_lock);
2672 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2673 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2674 		ASSERT(rx_ring != NULL);
2675 
2676 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2677 			time_t normal_blank_time =
2678 			    mrfp->mrf_normal_blank_time;
2679 			uint_t normal_pkt_cnt =
2680 			    mrfp->mrf_normal_pkt_count;
2681 
2682 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2683 
2684 	rx_ring->rr_blank = mrfp->mrf_blank;
2685 	rx_ring->rr_handle = mrfp->mrf_arg;
2686 	rx_ring->rr_ill = ill;
2687 	rx_ring->rr_normal_blank_time = normal_blank_time;
2688 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2689 
2690 			rx_ring->rr_max_blank_time =
2691 			    normal_blank_time * rr_max_blank_ratio;
2692 			rx_ring->rr_min_blank_time =
2693 			    normal_blank_time * rr_min_blank_ratio;
2694 			rx_ring->rr_max_pkt_cnt =
2695 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2696 			rx_ring->rr_min_pkt_cnt =
2697 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2698 
2699 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2700 			mutex_exit(&ill->ill_lock);
2701 
2702 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2703 			    (int), ip_rx_index);
2704 			return ((mac_resource_handle_t)rx_ring);
2705 		}
2706 	}
2707 
2708 	/*
2709 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2710 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2711 	 * should be made configurable. Meanwhile it cause no panic because
2712 	 * driver will pass ip_input a NULL handle which will make
2713 	 * IP allocate the default squeue and Polling mode will not
2714 	 * be used for this ring.
2715 	 */
2716 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2717 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2718 
2719 	mutex_exit(&ill->ill_lock);
2720 	return (NULL);
2721 }
2722 
2723 static boolean_t
2724 ill_capability_dls_init(ill_t *ill)
2725 {
2726 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2727 	conn_t 			*connp;
2728 	size_t			sz;
2729 	ip_stack_t *ipst = ill->ill_ipst;
2730 
2731 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2732 		if (ill_dls == NULL) {
2733 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2734 			    "soft_ring enabled for ill=%s (%p) but data "
2735 			    "structs uninitialized\n", ill->ill_name,
2736 			    (void *)ill);
2737 		}
2738 		return (B_TRUE);
2739 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2740 		if (ill_dls == NULL) {
2741 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2742 			    "polling enabled for ill=%s (%p) but data "
2743 			    "structs uninitialized\n", ill->ill_name,
2744 			    (void *)ill);
2745 		}
2746 		return (B_TRUE);
2747 	}
2748 
2749 	if (ill_dls != NULL) {
2750 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2751 		/* Soft_Ring or polling is being re-enabled */
2752 
2753 		connp = ill_dls->ill_unbind_conn;
2754 		ASSERT(rx_ring != NULL);
2755 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2756 		bzero((void *)rx_ring,
2757 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2758 		ill_dls->ill_ring_tbl = rx_ring;
2759 		ill_dls->ill_unbind_conn = connp;
2760 		return (B_TRUE);
2761 	}
2762 
2763 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2764 	    ipst->ips_netstack)) == NULL)
2765 		return (B_FALSE);
2766 
2767 	sz = sizeof (ill_dls_capab_t);
2768 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2769 
2770 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2771 	if (ill_dls == NULL) {
2772 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2773 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2774 		    (void *)ill);
2775 		CONN_DEC_REF(connp);
2776 		return (B_FALSE);
2777 	}
2778 
2779 	/* Allocate space to hold ring table */
2780 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2781 	ill->ill_dls_capab = ill_dls;
2782 	ill_dls->ill_unbind_conn = connp;
2783 	return (B_TRUE);
2784 }
2785 
2786 /*
2787  * ill_capability_dls_disable: disable soft_ring and/or polling
2788  * capability. Since any of the rings might already be in use, need
2789  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2790  * direct calls if necessary.
2791  */
2792 static void
2793 ill_capability_dls_disable(ill_t *ill)
2794 {
2795 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2796 
2797 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2798 		ip_squeue_clean_all(ill);
2799 		ill_dls->ill_tx = NULL;
2800 		ill_dls->ill_tx_handle = NULL;
2801 		ill_dls->ill_dls_change_status = NULL;
2802 		ill_dls->ill_dls_bind = NULL;
2803 		ill_dls->ill_dls_unbind = NULL;
2804 	}
2805 
2806 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2807 }
2808 
2809 static void
2810 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2811     dl_capability_sub_t *isub)
2812 {
2813 	uint_t			size;
2814 	uchar_t			*rptr;
2815 	dl_capab_dls_t	dls, *odls;
2816 	ill_dls_capab_t	*ill_dls;
2817 	mblk_t			*nmp = NULL;
2818 	dl_capability_req_t	*ocap;
2819 	uint_t			sub_dl_cap = isub->dl_cap;
2820 
2821 	if (!ill_capability_dls_init(ill))
2822 		return;
2823 	ill_dls = ill->ill_dls_capab;
2824 
2825 	/* Copy locally to get the members aligned */
2826 	bcopy((void *)idls, (void *)&dls,
2827 	    sizeof (dl_capab_dls_t));
2828 
2829 	/* Get the tx function and handle from dld */
2830 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2831 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2832 
2833 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2834 		ill_dls->ill_dls_change_status =
2835 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2836 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2837 		ill_dls->ill_dls_unbind =
2838 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2839 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2840 	}
2841 
2842 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2843 	    isub->dl_length;
2844 
2845 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2846 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2847 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2848 		    ill->ill_name, (void *)ill);
2849 		return;
2850 	}
2851 
2852 	/* initialize dl_capability_req_t */
2853 	rptr = nmp->b_rptr;
2854 	ocap = (dl_capability_req_t *)rptr;
2855 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2856 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2857 	rptr += sizeof (dl_capability_req_t);
2858 
2859 	/* initialize dl_capability_sub_t */
2860 	bcopy(isub, rptr, sizeof (*isub));
2861 	rptr += sizeof (*isub);
2862 
2863 	odls = (dl_capab_dls_t *)rptr;
2864 	rptr += sizeof (dl_capab_dls_t);
2865 
2866 	/* initialize dl_capab_dls_t to be sent down */
2867 	dls.dls_rx_handle = (uintptr_t)ill;
2868 	dls.dls_rx = (uintptr_t)ip_input;
2869 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2870 
2871 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2872 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2873 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2874 		dls.dls_flags = SOFT_RING_ENABLE;
2875 	} else {
2876 		dls.dls_flags = POLL_ENABLE;
2877 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2878 		    "to enable polling\n", ill->ill_name));
2879 	}
2880 	bcopy((void *)&dls, (void *)odls,
2881 	    sizeof (dl_capab_dls_t));
2882 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2883 	/*
2884 	 * nmp points to a DL_CAPABILITY_REQ message to
2885 	 * enable either soft_ring or polling
2886 	 */
2887 	ill_dlpi_send(ill, nmp);
2888 }
2889 
2890 static void
2891 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2892 {
2893 	mblk_t *mp;
2894 	dl_capab_dls_t *idls;
2895 	dl_capability_sub_t *dl_subcap;
2896 	int size;
2897 
2898 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2899 		return;
2900 
2901 	ASSERT(ill->ill_dls_capab != NULL);
2902 
2903 	size = sizeof (*dl_subcap) + sizeof (*idls);
2904 
2905 	mp = allocb(size, BPRI_HI);
2906 	if (mp == NULL) {
2907 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2908 		    "request to disable soft_ring\n"));
2909 		return;
2910 	}
2911 
2912 	mp->b_wptr = mp->b_rptr + size;
2913 
2914 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2915 	dl_subcap->dl_length = sizeof (*idls);
2916 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2917 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2918 	else
2919 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2920 
2921 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2922 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2923 		idls->dls_flags = SOFT_RING_DISABLE;
2924 	else
2925 		idls->dls_flags = POLL_DISABLE;
2926 
2927 	if (*sc_mp != NULL)
2928 		linkb(*sc_mp, mp);
2929 	else
2930 		*sc_mp = mp;
2931 }
2932 
2933 /*
2934  * Process a soft_ring/poll capability negotiation ack received
2935  * from a DLS Provider.isub must point to the sub-capability
2936  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2937  */
2938 static void
2939 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2940 {
2941 	dl_capab_dls_t		*idls;
2942 	uint_t			sub_dl_cap = isub->dl_cap;
2943 	uint8_t			*capend;
2944 
2945 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2946 	    sub_dl_cap == DL_CAPAB_POLL);
2947 
2948 	if (ill->ill_isv6)
2949 		return;
2950 
2951 	/*
2952 	 * Note: range checks here are not absolutely sufficient to
2953 	 * make us robust against malformed messages sent by drivers;
2954 	 * this is in keeping with the rest of IP's dlpi handling.
2955 	 * (Remember, it's coming from something else in the kernel
2956 	 * address space)
2957 	 */
2958 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2959 	if (capend > mp->b_wptr) {
2960 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2961 		    "malformed sub-capability too long for mblk");
2962 		return;
2963 	}
2964 
2965 	/*
2966 	 * There are two types of acks we process here:
2967 	 * 1. acks in reply to a (first form) generic capability req
2968 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2969 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2970 	 *    capability req.
2971 	 */
2972 	idls = (dl_capab_dls_t *)(isub + 1);
2973 
2974 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2975 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2976 		    "capability isn't as expected; pass-thru "
2977 		    "module(s) detected, discarding capability\n"));
2978 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2979 			/*
2980 			 * This is a capability renegotitation case.
2981 			 * The interface better be unusable at this
2982 			 * point other wise bad things will happen
2983 			 * if we disable direct calls on a running
2984 			 * and up interface.
2985 			 */
2986 			ill_capability_dls_disable(ill);
2987 		}
2988 		return;
2989 	}
2990 
2991 	switch (idls->dls_flags) {
2992 	default:
2993 		/* Disable if unknown flag */
2994 	case SOFT_RING_DISABLE:
2995 	case POLL_DISABLE:
2996 		ill_capability_dls_disable(ill);
2997 		break;
2998 	case SOFT_RING_CAPABLE:
2999 	case POLL_CAPABLE:
3000 		/*
3001 		 * If the capability was already enabled, its safe
3002 		 * to disable it first to get rid of stale information
3003 		 * and then start enabling it again.
3004 		 */
3005 		ill_capability_dls_disable(ill);
3006 		ill_capability_dls_capable(ill, idls, isub);
3007 		break;
3008 	case SOFT_RING_ENABLE:
3009 	case POLL_ENABLE:
3010 		mutex_enter(&ill->ill_lock);
3011 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3012 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3013 			ASSERT(ill->ill_dls_capab != NULL);
3014 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3015 		}
3016 		if (sub_dl_cap == DL_CAPAB_POLL &&
3017 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3018 			ASSERT(ill->ill_dls_capab != NULL);
3019 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3020 			ip1dbg(("ill_capability_dls_ack: interface %s "
3021 			    "has enabled polling\n", ill->ill_name));
3022 		}
3023 		mutex_exit(&ill->ill_lock);
3024 		break;
3025 	}
3026 }
3027 
3028 /*
3029  * Process a hardware checksum offload capability negotiation ack received
3030  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3031  * of a DL_CAPABILITY_ACK message.
3032  */
3033 static void
3034 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3035 {
3036 	dl_capability_req_t	*ocap;
3037 	dl_capab_hcksum_t	*ihck, *ohck;
3038 	ill_hcksum_capab_t	**ill_hcksum;
3039 	mblk_t			*nmp = NULL;
3040 	uint_t			sub_dl_cap = isub->dl_cap;
3041 	uint8_t			*capend;
3042 
3043 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3044 
3045 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3046 
3047 	/*
3048 	 * Note: range checks here are not absolutely sufficient to
3049 	 * make us robust against malformed messages sent by drivers;
3050 	 * this is in keeping with the rest of IP's dlpi handling.
3051 	 * (Remember, it's coming from something else in the kernel
3052 	 * address space)
3053 	 */
3054 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3055 	if (capend > mp->b_wptr) {
3056 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3057 		    "malformed sub-capability too long for mblk");
3058 		return;
3059 	}
3060 
3061 	/*
3062 	 * There are two types of acks we process here:
3063 	 * 1. acks in reply to a (first form) generic capability req
3064 	 *    (no ENABLE flag set)
3065 	 * 2. acks in reply to a ENABLE capability req.
3066 	 *    (ENABLE flag set)
3067 	 */
3068 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3069 
3070 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3071 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3072 		    "unsupported hardware checksum "
3073 		    "sub-capability (version %d, expected %d)",
3074 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3075 		return;
3076 	}
3077 
3078 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3079 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3080 		    "checksum capability isn't as expected; pass-thru "
3081 		    "module(s) detected, discarding capability\n"));
3082 		return;
3083 	}
3084 
3085 #define	CURR_HCKSUM_CAPAB				\
3086 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3087 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3088 
3089 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3090 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3091 		/* do ENABLE processing */
3092 		if (*ill_hcksum == NULL) {
3093 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3094 			    KM_NOSLEEP);
3095 
3096 			if (*ill_hcksum == NULL) {
3097 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3098 				    "could not enable hcksum version %d "
3099 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3100 				    ill->ill_name);
3101 				return;
3102 			}
3103 		}
3104 
3105 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3106 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3107 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3108 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3109 		    "has enabled hardware checksumming\n ",
3110 		    ill->ill_name));
3111 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3112 		/*
3113 		 * Enabling hardware checksum offload
3114 		 * Currently IP supports {TCP,UDP}/IPv4
3115 		 * partial and full cksum offload and
3116 		 * IPv4 header checksum offload.
3117 		 * Allocate new mblk which will
3118 		 * contain a new capability request
3119 		 * to enable hardware checksum offload.
3120 		 */
3121 		uint_t	size;
3122 		uchar_t	*rptr;
3123 
3124 		size = sizeof (dl_capability_req_t) +
3125 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3126 
3127 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3128 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3129 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3130 			    ill->ill_name);
3131 			return;
3132 		}
3133 
3134 		rptr = nmp->b_rptr;
3135 		/* initialize dl_capability_req_t */
3136 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3137 		ocap->dl_sub_offset =
3138 		    sizeof (dl_capability_req_t);
3139 		ocap->dl_sub_length =
3140 		    sizeof (dl_capability_sub_t) +
3141 		    isub->dl_length;
3142 		nmp->b_rptr += sizeof (dl_capability_req_t);
3143 
3144 		/* initialize dl_capability_sub_t */
3145 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3146 		nmp->b_rptr += sizeof (*isub);
3147 
3148 		/* initialize dl_capab_hcksum_t */
3149 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3150 		bcopy(ihck, ohck, sizeof (*ihck));
3151 
3152 		nmp->b_rptr = rptr;
3153 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3154 
3155 		/* Set ENABLE flag */
3156 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3157 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3158 
3159 		/*
3160 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3161 		 * hardware checksum acceleration.
3162 		 */
3163 		ill_dlpi_send(ill, nmp);
3164 	} else {
3165 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3166 		    "advertised %x hardware checksum capability flags\n",
3167 		    ill->ill_name, ihck->hcksum_txflags));
3168 	}
3169 }
3170 
3171 static void
3172 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3173 {
3174 	mblk_t *mp;
3175 	dl_capab_hcksum_t *hck_subcap;
3176 	dl_capability_sub_t *dl_subcap;
3177 	int size;
3178 
3179 	if (!ILL_HCKSUM_CAPABLE(ill))
3180 		return;
3181 
3182 	ASSERT(ill->ill_hcksum_capab != NULL);
3183 	/*
3184 	 * Clear the capability flag for hardware checksum offload but
3185 	 * retain the ill_hcksum_capab structure since it's possible that
3186 	 * another thread is still referring to it.  The structure only
3187 	 * gets deallocated when we destroy the ill.
3188 	 */
3189 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3190 
3191 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3192 
3193 	mp = allocb(size, BPRI_HI);
3194 	if (mp == NULL) {
3195 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3196 		    "request to disable hardware checksum offload\n"));
3197 		return;
3198 	}
3199 
3200 	mp->b_wptr = mp->b_rptr + size;
3201 
3202 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3203 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3204 	dl_subcap->dl_length = sizeof (*hck_subcap);
3205 
3206 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3207 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3208 	hck_subcap->hcksum_txflags = 0;
3209 
3210 	if (*sc_mp != NULL)
3211 		linkb(*sc_mp, mp);
3212 	else
3213 		*sc_mp = mp;
3214 }
3215 
3216 static void
3217 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3218 {
3219 	mblk_t *nmp = NULL;
3220 	dl_capability_req_t *oc;
3221 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3222 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3223 	uint_t sub_dl_cap = isub->dl_cap;
3224 	uint8_t *capend;
3225 
3226 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3227 
3228 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3229 
3230 	/*
3231 	 * Note: range checks here are not absolutely sufficient to
3232 	 * make us robust against malformed messages sent by drivers;
3233 	 * this is in keeping with the rest of IP's dlpi handling.
3234 	 * (Remember, it's coming from something else in the kernel
3235 	 * address space)
3236 	 */
3237 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3238 	if (capend > mp->b_wptr) {
3239 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3240 		    "malformed sub-capability too long for mblk");
3241 		return;
3242 	}
3243 
3244 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3245 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3246 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3247 		    "unsupported ZEROCOPY sub-capability (version %d, "
3248 		    "expected %d)", zc_ic->zerocopy_version,
3249 		    ZEROCOPY_VERSION_1);
3250 		return;
3251 	}
3252 
3253 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3254 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3255 		    "capability isn't as expected; pass-thru module(s) "
3256 		    "detected, discarding capability\n"));
3257 		return;
3258 	}
3259 
3260 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3261 		if (*ill_zerocopy_capab == NULL) {
3262 			*ill_zerocopy_capab =
3263 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3264 			    KM_NOSLEEP);
3265 
3266 			if (*ill_zerocopy_capab == NULL) {
3267 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3268 				    "could not enable Zero-copy version %d "
3269 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3270 				    ill->ill_name);
3271 				return;
3272 			}
3273 		}
3274 
3275 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3276 		    "supports Zero-copy version %d\n", ill->ill_name,
3277 		    ZEROCOPY_VERSION_1));
3278 
3279 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3280 		    zc_ic->zerocopy_version;
3281 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3282 		    zc_ic->zerocopy_flags;
3283 
3284 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3285 	} else {
3286 		uint_t size;
3287 		uchar_t *rptr;
3288 
3289 		size = sizeof (dl_capability_req_t) +
3290 		    sizeof (dl_capability_sub_t) +
3291 		    sizeof (dl_capab_zerocopy_t);
3292 
3293 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3294 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3295 			    "could not enable zerocopy for %s (ENOMEM)\n",
3296 			    ill->ill_name);
3297 			return;
3298 		}
3299 
3300 		rptr = nmp->b_rptr;
3301 		/* initialize dl_capability_req_t */
3302 		oc = (dl_capability_req_t *)rptr;
3303 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3304 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3305 		    sizeof (dl_capab_zerocopy_t);
3306 		rptr += sizeof (dl_capability_req_t);
3307 
3308 		/* initialize dl_capability_sub_t */
3309 		bcopy(isub, rptr, sizeof (*isub));
3310 		rptr += sizeof (*isub);
3311 
3312 		/* initialize dl_capab_zerocopy_t */
3313 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3314 		*zc_oc = *zc_ic;
3315 
3316 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3317 		    "to enable zero-copy version %d\n", ill->ill_name,
3318 		    ZEROCOPY_VERSION_1));
3319 
3320 		/* set VMSAFE_MEM flag */
3321 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3322 
3323 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3324 		ill_dlpi_send(ill, nmp);
3325 	}
3326 }
3327 
3328 static void
3329 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3330 {
3331 	mblk_t *mp;
3332 	dl_capab_zerocopy_t *zerocopy_subcap;
3333 	dl_capability_sub_t *dl_subcap;
3334 	int size;
3335 
3336 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3337 		return;
3338 
3339 	ASSERT(ill->ill_zerocopy_capab != NULL);
3340 	/*
3341 	 * Clear the capability flag for Zero-copy but retain the
3342 	 * ill_zerocopy_capab structure since it's possible that another
3343 	 * thread is still referring to it.  The structure only gets
3344 	 * deallocated when we destroy the ill.
3345 	 */
3346 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3347 
3348 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3349 
3350 	mp = allocb(size, BPRI_HI);
3351 	if (mp == NULL) {
3352 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3353 		    "request to disable Zero-copy\n"));
3354 		return;
3355 	}
3356 
3357 	mp->b_wptr = mp->b_rptr + size;
3358 
3359 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3360 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3361 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3362 
3363 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3364 	zerocopy_subcap->zerocopy_version =
3365 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3366 	zerocopy_subcap->zerocopy_flags = 0;
3367 
3368 	if (*sc_mp != NULL)
3369 		linkb(*sc_mp, mp);
3370 	else
3371 		*sc_mp = mp;
3372 }
3373 
3374 /*
3375  * Process Large Segment Offload capability negotiation ack received from a
3376  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3377  * DL_CAPABILITY_ACK message.
3378  */
3379 static void
3380 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3381 {
3382 	mblk_t *nmp = NULL;
3383 	dl_capability_req_t *oc;
3384 	dl_capab_lso_t *lso_ic, *lso_oc;
3385 	ill_lso_capab_t **ill_lso_capab;
3386 	uint_t sub_dl_cap = isub->dl_cap;
3387 	uint8_t *capend;
3388 
3389 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3390 
3391 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3392 
3393 	/*
3394 	 * Note: range checks here are not absolutely sufficient to
3395 	 * make us robust against malformed messages sent by drivers;
3396 	 * this is in keeping with the rest of IP's dlpi handling.
3397 	 * (Remember, it's coming from something else in the kernel
3398 	 * address space)
3399 	 */
3400 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3401 	if (capend > mp->b_wptr) {
3402 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3403 		    "malformed sub-capability too long for mblk");
3404 		return;
3405 	}
3406 
3407 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3408 
3409 	if (lso_ic->lso_version != LSO_VERSION_1) {
3410 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3411 		    "unsupported LSO sub-capability (version %d, expected %d)",
3412 		    lso_ic->lso_version, LSO_VERSION_1);
3413 		return;
3414 	}
3415 
3416 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3417 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3418 		    "capability isn't as expected; pass-thru module(s) "
3419 		    "detected, discarding capability\n"));
3420 		return;
3421 	}
3422 
3423 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3424 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3425 		if (*ill_lso_capab == NULL) {
3426 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3427 			    KM_NOSLEEP);
3428 
3429 			if (*ill_lso_capab == NULL) {
3430 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3431 				    "could not enable LSO version %d "
3432 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3433 				    ill->ill_name);
3434 				return;
3435 			}
3436 		}
3437 
3438 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3439 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3440 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3441 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3442 
3443 		ip1dbg(("ill_capability_lso_ack: interface %s "
3444 		    "has enabled LSO\n ", ill->ill_name));
3445 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3446 		uint_t size;
3447 		uchar_t *rptr;
3448 
3449 		size = sizeof (dl_capability_req_t) +
3450 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3451 
3452 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3453 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3454 			    "could not enable LSO for %s (ENOMEM)\n",
3455 			    ill->ill_name);
3456 			return;
3457 		}
3458 
3459 		rptr = nmp->b_rptr;
3460 		/* initialize dl_capability_req_t */
3461 		oc = (dl_capability_req_t *)nmp->b_rptr;
3462 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3463 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3464 		    sizeof (dl_capab_lso_t);
3465 		nmp->b_rptr += sizeof (dl_capability_req_t);
3466 
3467 		/* initialize dl_capability_sub_t */
3468 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3469 		nmp->b_rptr += sizeof (*isub);
3470 
3471 		/* initialize dl_capab_lso_t */
3472 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3473 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3474 
3475 		nmp->b_rptr = rptr;
3476 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3477 
3478 		/* set ENABLE flag */
3479 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3480 
3481 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3482 		ill_dlpi_send(ill, nmp);
3483 	} else {
3484 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3485 		    "advertised %x LSO capability flags\n",
3486 		    ill->ill_name, lso_ic->lso_flags));
3487 	}
3488 }
3489 
3490 static void
3491 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3492 {
3493 	mblk_t *mp;
3494 	dl_capab_lso_t *lso_subcap;
3495 	dl_capability_sub_t *dl_subcap;
3496 	int size;
3497 
3498 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3499 		return;
3500 
3501 	ASSERT(ill->ill_lso_capab != NULL);
3502 	/*
3503 	 * Clear the capability flag for LSO but retain the
3504 	 * ill_lso_capab structure since it's possible that another
3505 	 * thread is still referring to it.  The structure only gets
3506 	 * deallocated when we destroy the ill.
3507 	 */
3508 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3509 
3510 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3511 
3512 	mp = allocb(size, BPRI_HI);
3513 	if (mp == NULL) {
3514 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3515 		    "request to disable LSO\n"));
3516 		return;
3517 	}
3518 
3519 	mp->b_wptr = mp->b_rptr + size;
3520 
3521 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3522 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3523 	dl_subcap->dl_length = sizeof (*lso_subcap);
3524 
3525 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3526 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3527 	lso_subcap->lso_flags = 0;
3528 
3529 	if (*sc_mp != NULL)
3530 		linkb(*sc_mp, mp);
3531 	else
3532 		*sc_mp = mp;
3533 }
3534 
3535 /*
3536  * Consume a new-style hardware capabilities negotiation ack.
3537  * Called from ip_rput_dlpi_writer().
3538  */
3539 void
3540 ill_capability_ack(ill_t *ill, mblk_t *mp)
3541 {
3542 	dl_capability_ack_t *capp;
3543 	dl_capability_sub_t *subp, *endp;
3544 
3545 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3546 		ill->ill_dlpi_capab_state = IDS_OK;
3547 
3548 	capp = (dl_capability_ack_t *)mp->b_rptr;
3549 
3550 	if (capp->dl_sub_length == 0)
3551 		/* no new-style capabilities */
3552 		return;
3553 
3554 	/* make sure the driver supplied correct dl_sub_length */
3555 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3556 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3557 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3558 		return;
3559 	}
3560 
3561 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3562 	/*
3563 	 * There are sub-capabilities. Process the ones we know about.
3564 	 * Loop until we don't have room for another sub-cap header..
3565 	 */
3566 	for (subp = SC(capp, capp->dl_sub_offset),
3567 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3568 	    subp <= endp;
3569 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3570 
3571 		switch (subp->dl_cap) {
3572 		case DL_CAPAB_ID_WRAPPER:
3573 			ill_capability_id_ack(ill, mp, subp);
3574 			break;
3575 		default:
3576 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3577 			break;
3578 		}
3579 	}
3580 #undef SC
3581 }
3582 
3583 /*
3584  * This routine is called to scan the fragmentation reassembly table for
3585  * the specified ILL for any packets that are starting to smell.
3586  * dead_interval is the maximum time in seconds that will be tolerated.  It
3587  * will either be the value specified in ip_g_frag_timeout, or zero if the
3588  * ILL is shutting down and it is time to blow everything off.
3589  *
3590  * It returns the number of seconds (as a time_t) that the next frag timer
3591  * should be scheduled for, 0 meaning that the timer doesn't need to be
3592  * re-started.  Note that the method of calculating next_timeout isn't
3593  * entirely accurate since time will flow between the time we grab
3594  * current_time and the time we schedule the next timeout.  This isn't a
3595  * big problem since this is the timer for sending an ICMP reassembly time
3596  * exceeded messages, and it doesn't have to be exactly accurate.
3597  *
3598  * This function is
3599  * sometimes called as writer, although this is not required.
3600  */
3601 time_t
3602 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3603 {
3604 	ipfb_t	*ipfb;
3605 	ipfb_t	*endp;
3606 	ipf_t	*ipf;
3607 	ipf_t	*ipfnext;
3608 	mblk_t	*mp;
3609 	time_t	current_time = gethrestime_sec();
3610 	time_t	next_timeout = 0;
3611 	uint32_t	hdr_length;
3612 	mblk_t	*send_icmp_head;
3613 	mblk_t	*send_icmp_head_v6;
3614 	zoneid_t zoneid;
3615 	ip_stack_t *ipst = ill->ill_ipst;
3616 
3617 	ipfb = ill->ill_frag_hash_tbl;
3618 	if (ipfb == NULL)
3619 		return (B_FALSE);
3620 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3621 	/* Walk the frag hash table. */
3622 	for (; ipfb < endp; ipfb++) {
3623 		send_icmp_head = NULL;
3624 		send_icmp_head_v6 = NULL;
3625 		mutex_enter(&ipfb->ipfb_lock);
3626 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3627 			time_t frag_time = current_time - ipf->ipf_timestamp;
3628 			time_t frag_timeout;
3629 
3630 			if (frag_time < dead_interval) {
3631 				/*
3632 				 * There are some outstanding fragments
3633 				 * that will timeout later.  Make note of
3634 				 * the time so that we can reschedule the
3635 				 * next timeout appropriately.
3636 				 */
3637 				frag_timeout = dead_interval - frag_time;
3638 				if (next_timeout == 0 ||
3639 				    frag_timeout < next_timeout) {
3640 					next_timeout = frag_timeout;
3641 				}
3642 				break;
3643 			}
3644 			/* Time's up.  Get it out of here. */
3645 			hdr_length = ipf->ipf_nf_hdr_len;
3646 			ipfnext = ipf->ipf_hash_next;
3647 			if (ipfnext)
3648 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3649 			*ipf->ipf_ptphn = ipfnext;
3650 			mp = ipf->ipf_mp->b_cont;
3651 			for (; mp; mp = mp->b_cont) {
3652 				/* Extra points for neatness. */
3653 				IP_REASS_SET_START(mp, 0);
3654 				IP_REASS_SET_END(mp, 0);
3655 			}
3656 			mp = ipf->ipf_mp->b_cont;
3657 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
3658 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3659 			ipfb->ipfb_count -= ipf->ipf_count;
3660 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3661 			ipfb->ipfb_frag_pkts--;
3662 			/*
3663 			 * We do not send any icmp message from here because
3664 			 * we currently are holding the ipfb_lock for this
3665 			 * hash chain. If we try and send any icmp messages
3666 			 * from here we may end up via a put back into ip
3667 			 * trying to get the same lock, causing a recursive
3668 			 * mutex panic. Instead we build a list and send all
3669 			 * the icmp messages after we have dropped the lock.
3670 			 */
3671 			if (ill->ill_isv6) {
3672 				if (hdr_length != 0) {
3673 					mp->b_next = send_icmp_head_v6;
3674 					send_icmp_head_v6 = mp;
3675 				} else {
3676 					freemsg(mp);
3677 				}
3678 			} else {
3679 				if (hdr_length != 0) {
3680 					mp->b_next = send_icmp_head;
3681 					send_icmp_head = mp;
3682 				} else {
3683 					freemsg(mp);
3684 				}
3685 			}
3686 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3687 			freeb(ipf->ipf_mp);
3688 		}
3689 		mutex_exit(&ipfb->ipfb_lock);
3690 		/*
3691 		 * Now need to send any icmp messages that we delayed from
3692 		 * above.
3693 		 */
3694 		while (send_icmp_head_v6 != NULL) {
3695 			ip6_t *ip6h;
3696 
3697 			mp = send_icmp_head_v6;
3698 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3699 			mp->b_next = NULL;
3700 			if (mp->b_datap->db_type == M_CTL)
3701 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3702 			else
3703 				ip6h = (ip6_t *)mp->b_rptr;
3704 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3705 			    ill, ipst);
3706 			if (zoneid == ALL_ZONES) {
3707 				freemsg(mp);
3708 			} else {
3709 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3710 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3711 				    B_FALSE, zoneid, ipst);
3712 			}
3713 		}
3714 		while (send_icmp_head != NULL) {
3715 			ipaddr_t dst;
3716 
3717 			mp = send_icmp_head;
3718 			send_icmp_head = send_icmp_head->b_next;
3719 			mp->b_next = NULL;
3720 
3721 			if (mp->b_datap->db_type == M_CTL)
3722 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3723 			else
3724 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3725 
3726 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3727 			if (zoneid == ALL_ZONES) {
3728 				freemsg(mp);
3729 			} else {
3730 				icmp_time_exceeded(ill->ill_wq, mp,
3731 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3732 				    ipst);
3733 			}
3734 		}
3735 	}
3736 	/*
3737 	 * A non-dying ILL will use the return value to decide whether to
3738 	 * restart the frag timer, and for how long.
3739 	 */
3740 	return (next_timeout);
3741 }
3742 
3743 /*
3744  * This routine is called when the approximate count of mblk memory used
3745  * for the specified ILL has exceeded max_count.
3746  */
3747 void
3748 ill_frag_prune(ill_t *ill, uint_t max_count)
3749 {
3750 	ipfb_t	*ipfb;
3751 	ipf_t	*ipf;
3752 	size_t	count;
3753 
3754 	/*
3755 	 * If we are here within ip_min_frag_prune_time msecs remove
3756 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3757 	 * ill_frag_free_num_pkts.
3758 	 */
3759 	mutex_enter(&ill->ill_lock);
3760 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3761 	    (ip_min_frag_prune_time != 0 ?
3762 	    ip_min_frag_prune_time : msec_per_tick)) {
3763 
3764 		ill->ill_frag_free_num_pkts++;
3765 
3766 	} else {
3767 		ill->ill_frag_free_num_pkts = 0;
3768 	}
3769 	ill->ill_last_frag_clean_time = lbolt;
3770 	mutex_exit(&ill->ill_lock);
3771 
3772 	/*
3773 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3774 	 */
3775 	if (ill->ill_frag_free_num_pkts != 0) {
3776 		int ix;
3777 
3778 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3779 			ipfb = &ill->ill_frag_hash_tbl[ix];
3780 			mutex_enter(&ipfb->ipfb_lock);
3781 			if (ipfb->ipfb_ipf != NULL) {
3782 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3783 				    ill->ill_frag_free_num_pkts);
3784 			}
3785 			mutex_exit(&ipfb->ipfb_lock);
3786 		}
3787 	}
3788 	/*
3789 	 * While the reassembly list for this ILL is too big, prune a fragment
3790 	 * queue by age, oldest first.
3791 	 */
3792 	while (ill->ill_frag_count > max_count) {
3793 		int	ix;
3794 		ipfb_t	*oipfb = NULL;
3795 		uint_t	oldest = UINT_MAX;
3796 
3797 		count = 0;
3798 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3799 			ipfb = &ill->ill_frag_hash_tbl[ix];
3800 			mutex_enter(&ipfb->ipfb_lock);
3801 			ipf = ipfb->ipfb_ipf;
3802 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3803 				oldest = ipf->ipf_gen;
3804 				oipfb = ipfb;
3805 			}
3806 			count += ipfb->ipfb_count;
3807 			mutex_exit(&ipfb->ipfb_lock);
3808 		}
3809 		if (oipfb == NULL)
3810 			break;
3811 
3812 		if (count <= max_count)
3813 			return;	/* Somebody beat us to it, nothing to do */
3814 		mutex_enter(&oipfb->ipfb_lock);
3815 		ipf = oipfb->ipfb_ipf;
3816 		if (ipf != NULL) {
3817 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3818 		}
3819 		mutex_exit(&oipfb->ipfb_lock);
3820 	}
3821 }
3822 
3823 /*
3824  * free 'free_cnt' fragmented packets starting at ipf.
3825  */
3826 void
3827 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3828 {
3829 	size_t	count;
3830 	mblk_t	*mp;
3831 	mblk_t	*tmp;
3832 	ipf_t **ipfp = ipf->ipf_ptphn;
3833 
3834 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3835 	ASSERT(ipfp != NULL);
3836 	ASSERT(ipf != NULL);
3837 
3838 	while (ipf != NULL && free_cnt-- > 0) {
3839 		count = ipf->ipf_count;
3840 		mp = ipf->ipf_mp;
3841 		ipf = ipf->ipf_hash_next;
3842 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3843 			IP_REASS_SET_START(tmp, 0);
3844 			IP_REASS_SET_END(tmp, 0);
3845 		}
3846 		atomic_add_32(&ill->ill_frag_count, -count);
3847 		ASSERT(ipfb->ipfb_count >= count);
3848 		ipfb->ipfb_count -= count;
3849 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3850 		ipfb->ipfb_frag_pkts--;
3851 		freemsg(mp);
3852 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3853 	}
3854 
3855 	if (ipf)
3856 		ipf->ipf_ptphn = ipfp;
3857 	ipfp[0] = ipf;
3858 }
3859 
3860 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3861 	"obsolete and may be removed in a future release of Solaris.  Use " \
3862 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3863 
3864 /*
3865  * For obsolete per-interface forwarding configuration;
3866  * called in response to ND_GET.
3867  */
3868 /* ARGSUSED */
3869 static int
3870 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3871 {
3872 	ill_t *ill = (ill_t *)cp;
3873 
3874 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3875 
3876 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3877 	return (0);
3878 }
3879 
3880 /*
3881  * For obsolete per-interface forwarding configuration;
3882  * called in response to ND_SET.
3883  */
3884 /* ARGSUSED */
3885 static int
3886 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3887     cred_t *ioc_cr)
3888 {
3889 	long value;
3890 	int retval;
3891 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3892 
3893 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3894 
3895 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3896 	    value < 0 || value > 1) {
3897 		return (EINVAL);
3898 	}
3899 
3900 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3901 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3902 	rw_exit(&ipst->ips_ill_g_lock);
3903 	return (retval);
3904 }
3905 
3906 /*
3907  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3908  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3909  * up RTS_IFINFO routing socket messages for each interface whose flags we
3910  * change.
3911  */
3912 int
3913 ill_forward_set(ill_t *ill, boolean_t enable)
3914 {
3915 	ill_group_t *illgrp;
3916 	ip_stack_t	*ipst = ill->ill_ipst;
3917 
3918 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3919 
3920 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3921 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3922 		return (0);
3923 
3924 	if (IS_LOOPBACK(ill))
3925 		return (EINVAL);
3926 
3927 	/*
3928 	 * If the ill is in an IPMP group, set the forwarding policy on all
3929 	 * members of the group to the same value.
3930 	 */
3931 	illgrp = ill->ill_group;
3932 	if (illgrp != NULL) {
3933 		ill_t *tmp_ill;
3934 
3935 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3936 		    tmp_ill = tmp_ill->ill_group_next) {
3937 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3938 			    (enable ? "Enabling" : "Disabling"),
3939 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3940 			    tmp_ill->ill_name));
3941 			mutex_enter(&tmp_ill->ill_lock);
3942 			if (enable)
3943 				tmp_ill->ill_flags |= ILLF_ROUTER;
3944 			else
3945 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3946 			mutex_exit(&tmp_ill->ill_lock);
3947 			if (tmp_ill->ill_isv6)
3948 				ill_set_nce_router_flags(tmp_ill, enable);
3949 			/* Notify routing socket listeners of this change. */
3950 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3951 		}
3952 	} else {
3953 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3954 		    (enable ? "Enabling" : "Disabling"),
3955 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3956 		mutex_enter(&ill->ill_lock);
3957 		if (enable)
3958 			ill->ill_flags |= ILLF_ROUTER;
3959 		else
3960 			ill->ill_flags &= ~ILLF_ROUTER;
3961 		mutex_exit(&ill->ill_lock);
3962 		if (ill->ill_isv6)
3963 			ill_set_nce_router_flags(ill, enable);
3964 		/* Notify routing socket listeners of this change. */
3965 		ip_rts_ifmsg(ill->ill_ipif);
3966 	}
3967 
3968 	return (0);
3969 }
3970 
3971 /*
3972  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3973  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3974  * set or clear.
3975  */
3976 static void
3977 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3978 {
3979 	ipif_t *ipif;
3980 	nce_t *nce;
3981 
3982 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3983 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3984 		if (nce != NULL) {
3985 			mutex_enter(&nce->nce_lock);
3986 			if (enable)
3987 				nce->nce_flags |= NCE_F_ISROUTER;
3988 			else
3989 				nce->nce_flags &= ~NCE_F_ISROUTER;
3990 			mutex_exit(&nce->nce_lock);
3991 			NCE_REFRELE(nce);
3992 		}
3993 	}
3994 }
3995 
3996 /*
3997  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3998  * for this ill.  Make sure the v6/v4 question has been answered about this
3999  * ill.  The creation of this ndd variable is only for backwards compatibility.
4000  * The preferred way to control per-interface IP forwarding is through the
4001  * ILLF_ROUTER interface flag.
4002  */
4003 static int
4004 ill_set_ndd_name(ill_t *ill)
4005 {
4006 	char *suffix;
4007 	ip_stack_t	*ipst = ill->ill_ipst;
4008 
4009 	ASSERT(IAM_WRITER_ILL(ill));
4010 
4011 	if (ill->ill_isv6)
4012 		suffix = ipv6_forward_suffix;
4013 	else
4014 		suffix = ipv4_forward_suffix;
4015 
4016 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4017 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4018 	/*
4019 	 * Copies over the '\0'.
4020 	 * Note that strlen(suffix) is always bounded.
4021 	 */
4022 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4023 	    strlen(suffix) + 1);
4024 
4025 	/*
4026 	 * Use of the nd table requires holding the reader lock.
4027 	 * Modifying the nd table thru nd_load/nd_unload requires
4028 	 * the writer lock.
4029 	 */
4030 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4031 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4032 	    nd_ill_forward_set, (caddr_t)ill)) {
4033 		/*
4034 		 * If the nd_load failed, it only meant that it could not
4035 		 * allocate a new bunch of room for further NDD expansion.
4036 		 * Because of that, the ill_ndd_name will be set to 0, and
4037 		 * this interface is at the mercy of the global ip_forwarding
4038 		 * variable.
4039 		 */
4040 		rw_exit(&ipst->ips_ip_g_nd_lock);
4041 		ill->ill_ndd_name = NULL;
4042 		return (ENOMEM);
4043 	}
4044 	rw_exit(&ipst->ips_ip_g_nd_lock);
4045 	return (0);
4046 }
4047 
4048 /*
4049  * Intializes the context structure and returns the first ill in the list
4050  * cuurently start_list and end_list can have values:
4051  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4052  * IP_V4_G_HEAD		Traverse IPV4 list only.
4053  * IP_V6_G_HEAD		Traverse IPV6 list only.
4054  */
4055 
4056 /*
4057  * We don't check for CONDEMNED ills here. Caller must do that if
4058  * necessary under the ill lock.
4059  */
4060 ill_t *
4061 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4062     ip_stack_t *ipst)
4063 {
4064 	ill_if_t *ifp;
4065 	ill_t *ill;
4066 	avl_tree_t *avl_tree;
4067 
4068 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4069 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4070 
4071 	/*
4072 	 * setup the lists to search
4073 	 */
4074 	if (end_list != MAX_G_HEADS) {
4075 		ctx->ctx_current_list = start_list;
4076 		ctx->ctx_last_list = end_list;
4077 	} else {
4078 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4079 		ctx->ctx_current_list = 0;
4080 	}
4081 
4082 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4083 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4084 		if (ifp != (ill_if_t *)
4085 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4086 			avl_tree = &ifp->illif_avl_by_ppa;
4087 			ill = avl_first(avl_tree);
4088 			/*
4089 			 * ill is guaranteed to be non NULL or ifp should have
4090 			 * not existed.
4091 			 */
4092 			ASSERT(ill != NULL);
4093 			return (ill);
4094 		}
4095 		ctx->ctx_current_list++;
4096 	}
4097 
4098 	return (NULL);
4099 }
4100 
4101 /*
4102  * returns the next ill in the list. ill_first() must have been called
4103  * before calling ill_next() or bad things will happen.
4104  */
4105 
4106 /*
4107  * We don't check for CONDEMNED ills here. Caller must do that if
4108  * necessary under the ill lock.
4109  */
4110 ill_t *
4111 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4112 {
4113 	ill_if_t *ifp;
4114 	ill_t *ill;
4115 	ip_stack_t	*ipst = lastill->ill_ipst;
4116 
4117 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4118 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4119 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4120 	    AVL_AFTER)) != NULL) {
4121 		return (ill);
4122 	}
4123 
4124 	/* goto next ill_ifp in the list. */
4125 	ifp = lastill->ill_ifptr->illif_next;
4126 
4127 	/* make sure not at end of circular list */
4128 	while (ifp ==
4129 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4130 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4131 			return (NULL);
4132 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4133 	}
4134 
4135 	return (avl_first(&ifp->illif_avl_by_ppa));
4136 }
4137 
4138 /*
4139  * Check interface name for correct format which is name+ppa.
4140  * name can contain characters and digits, the right most digits
4141  * make up the ppa number. use of octal is not allowed, name must contain
4142  * a ppa, return pointer to the start of ppa.
4143  * In case of error return NULL.
4144  */
4145 static char *
4146 ill_get_ppa_ptr(char *name)
4147 {
4148 	int namelen = mi_strlen(name);
4149 
4150 	int len = namelen;
4151 
4152 	name += len;
4153 	while (len > 0) {
4154 		name--;
4155 		if (*name < '0' || *name > '9')
4156 			break;
4157 		len--;
4158 	}
4159 
4160 	/* empty string, all digits, or no trailing digits */
4161 	if (len == 0 || len == (int)namelen)
4162 		return (NULL);
4163 
4164 	name++;
4165 	/* check for attempted use of octal */
4166 	if (*name == '0' && len != (int)namelen - 1)
4167 		return (NULL);
4168 	return (name);
4169 }
4170 
4171 /*
4172  * use avl tree to locate the ill.
4173  */
4174 static ill_t *
4175 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4176     ipsq_func_t func, int *error, ip_stack_t *ipst)
4177 {
4178 	char *ppa_ptr = NULL;
4179 	int len;
4180 	uint_t ppa;
4181 	ill_t *ill = NULL;
4182 	ill_if_t *ifp;
4183 	int list;
4184 	ipsq_t *ipsq;
4185 
4186 	if (error != NULL)
4187 		*error = 0;
4188 
4189 	/*
4190 	 * get ppa ptr
4191 	 */
4192 	if (isv6)
4193 		list = IP_V6_G_HEAD;
4194 	else
4195 		list = IP_V4_G_HEAD;
4196 
4197 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4198 		if (error != NULL)
4199 			*error = ENXIO;
4200 		return (NULL);
4201 	}
4202 
4203 	len = ppa_ptr - name + 1;
4204 
4205 	ppa = stoi(&ppa_ptr);
4206 
4207 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4208 
4209 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4210 		/*
4211 		 * match is done on len - 1 as the name is not null
4212 		 * terminated it contains ppa in addition to the interface
4213 		 * name.
4214 		 */
4215 		if ((ifp->illif_name_len == len) &&
4216 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4217 			break;
4218 		} else {
4219 			ifp = ifp->illif_next;
4220 		}
4221 	}
4222 
4223 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4224 		/*
4225 		 * Even the interface type does not exist.
4226 		 */
4227 		if (error != NULL)
4228 			*error = ENXIO;
4229 		return (NULL);
4230 	}
4231 
4232 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4233 	if (ill != NULL) {
4234 		/*
4235 		 * The block comment at the start of ipif_down
4236 		 * explains the use of the macros used below
4237 		 */
4238 		GRAB_CONN_LOCK(q);
4239 		mutex_enter(&ill->ill_lock);
4240 		if (ILL_CAN_LOOKUP(ill)) {
4241 			ill_refhold_locked(ill);
4242 			mutex_exit(&ill->ill_lock);
4243 			RELEASE_CONN_LOCK(q);
4244 			return (ill);
4245 		} else if (ILL_CAN_WAIT(ill, q)) {
4246 			ipsq = ill->ill_phyint->phyint_ipsq;
4247 			mutex_enter(&ipsq->ipsq_lock);
4248 			mutex_exit(&ill->ill_lock);
4249 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4250 			mutex_exit(&ipsq->ipsq_lock);
4251 			RELEASE_CONN_LOCK(q);
4252 			if (error != NULL)
4253 				*error = EINPROGRESS;
4254 			return (NULL);
4255 		}
4256 		mutex_exit(&ill->ill_lock);
4257 		RELEASE_CONN_LOCK(q);
4258 	}
4259 	if (error != NULL)
4260 		*error = ENXIO;
4261 	return (NULL);
4262 }
4263 
4264 /*
4265  * comparison function for use with avl.
4266  */
4267 static int
4268 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4269 {
4270 	uint_t ppa;
4271 	uint_t ill_ppa;
4272 
4273 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4274 
4275 	ppa = *((uint_t *)ppa_ptr);
4276 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4277 	/*
4278 	 * We want the ill with the lowest ppa to be on the
4279 	 * top.
4280 	 */
4281 	if (ill_ppa < ppa)
4282 		return (1);
4283 	if (ill_ppa > ppa)
4284 		return (-1);
4285 	return (0);
4286 }
4287 
4288 /*
4289  * remove an interface type from the global list.
4290  */
4291 static void
4292 ill_delete_interface_type(ill_if_t *interface)
4293 {
4294 	ASSERT(interface != NULL);
4295 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4296 
4297 	avl_destroy(&interface->illif_avl_by_ppa);
4298 	if (interface->illif_ppa_arena != NULL)
4299 		vmem_destroy(interface->illif_ppa_arena);
4300 
4301 	remque(interface);
4302 
4303 	mi_free(interface);
4304 }
4305 
4306 /*
4307  * remove ill from the global list.
4308  */
4309 static void
4310 ill_glist_delete(ill_t *ill)
4311 {
4312 	hook_nic_event_t *info;
4313 	ip_stack_t	*ipst;
4314 
4315 	if (ill == NULL)
4316 		return;
4317 	ipst = ill->ill_ipst;
4318 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4319 
4320 	/*
4321 	 * If the ill was never inserted into the AVL tree
4322 	 * we skip the if branch.
4323 	 */
4324 	if (ill->ill_ifptr != NULL) {
4325 		/*
4326 		 * remove from AVL tree and free ppa number
4327 		 */
4328 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4329 
4330 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4331 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4332 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4333 		}
4334 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4335 			ill_delete_interface_type(ill->ill_ifptr);
4336 		}
4337 
4338 		/*
4339 		 * Indicate ill is no longer in the list.
4340 		 */
4341 		ill->ill_ifptr = NULL;
4342 		ill->ill_name_length = 0;
4343 		ill->ill_name[0] = '\0';
4344 		ill->ill_ppa = UINT_MAX;
4345 	}
4346 
4347 	/*
4348 	 * Run the unplumb hook after the NIC has disappeared from being
4349 	 * visible so that attempts to revalidate its existance will fail.
4350 	 *
4351 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4352 	 * that the ordering of delivered events to listeners matches the
4353 	 * order of them in the kernel.
4354 	 */
4355 	info = ill->ill_nic_event_info;
4356 	if (info != NULL && info->hne_event == NE_DOWN) {
4357 		mutex_enter(&ill->ill_lock);
4358 		ill_nic_info_dispatch(ill);
4359 		mutex_exit(&ill->ill_lock);
4360 	}
4361 
4362 	/* Generate NE_UNPLUMB event for ill_name. */
4363 	(void) ill_hook_event_create(ill, 0, NE_UNPLUMB, ill->ill_name,
4364 	    ill->ill_name_length);
4365 
4366 	ill_phyint_free(ill);
4367 	rw_exit(&ipst->ips_ill_g_lock);
4368 }
4369 
4370 /*
4371  * allocate a ppa, if the number of plumbed interfaces of this type are
4372  * less than ill_no_arena do a linear search to find a unused ppa.
4373  * When the number goes beyond ill_no_arena switch to using an arena.
4374  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4375  * is the return value for an error condition, so allocation starts at one
4376  * and is decremented by one.
4377  */
4378 static int
4379 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4380 {
4381 	ill_t *tmp_ill;
4382 	uint_t start, end;
4383 	int ppa;
4384 
4385 	if (ifp->illif_ppa_arena == NULL &&
4386 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4387 		/*
4388 		 * Create an arena.
4389 		 */
4390 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4391 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4392 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4393 			/* allocate what has already been assigned */
4394 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4395 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4396 		    tmp_ill, AVL_AFTER)) {
4397 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4398 			    1,		/* size */
4399 			    1,		/* align/quantum */
4400 			    0,		/* phase */
4401 			    0,		/* nocross */
4402 			    /* minaddr */
4403 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4404 			    /* maxaddr */
4405 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4406 			    VM_NOSLEEP|VM_FIRSTFIT);
4407 			if (ppa == 0) {
4408 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4409 				    " failed while switching"));
4410 				vmem_destroy(ifp->illif_ppa_arena);
4411 				ifp->illif_ppa_arena = NULL;
4412 				break;
4413 			}
4414 		}
4415 	}
4416 
4417 	if (ifp->illif_ppa_arena != NULL) {
4418 		if (ill->ill_ppa == UINT_MAX) {
4419 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4420 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4421 			if (ppa == 0)
4422 				return (EAGAIN);
4423 			ill->ill_ppa = --ppa;
4424 		} else {
4425 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4426 			    1, 		/* size */
4427 			    1, 		/* align/quantum */
4428 			    0, 		/* phase */
4429 			    0, 		/* nocross */
4430 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4431 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4432 			    VM_NOSLEEP|VM_FIRSTFIT);
4433 			/*
4434 			 * Most likely the allocation failed because
4435 			 * the requested ppa was in use.
4436 			 */
4437 			if (ppa == 0)
4438 				return (EEXIST);
4439 		}
4440 		return (0);
4441 	}
4442 
4443 	/*
4444 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4445 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4446 	 */
4447 	if (ill->ill_ppa == UINT_MAX) {
4448 		end = UINT_MAX - 1;
4449 		start = 0;
4450 	} else {
4451 		end = start = ill->ill_ppa;
4452 	}
4453 
4454 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4455 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4456 		if (start++ >= end) {
4457 			if (ill->ill_ppa == UINT_MAX)
4458 				return (EAGAIN);
4459 			else
4460 				return (EEXIST);
4461 		}
4462 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4463 	}
4464 	ill->ill_ppa = start;
4465 	return (0);
4466 }
4467 
4468 /*
4469  * Insert ill into the list of configured ill's. Once this function completes,
4470  * the ill is globally visible and is available through lookups. More precisely
4471  * this happens after the caller drops the ill_g_lock.
4472  */
4473 static int
4474 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4475 {
4476 	ill_if_t *ill_interface;
4477 	avl_index_t where = 0;
4478 	int error;
4479 	int name_length;
4480 	int index;
4481 	boolean_t check_length = B_FALSE;
4482 	ip_stack_t	*ipst = ill->ill_ipst;
4483 
4484 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4485 
4486 	name_length = mi_strlen(name) + 1;
4487 
4488 	if (isv6)
4489 		index = IP_V6_G_HEAD;
4490 	else
4491 		index = IP_V4_G_HEAD;
4492 
4493 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4494 	/*
4495 	 * Search for interface type based on name
4496 	 */
4497 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4498 		if ((ill_interface->illif_name_len == name_length) &&
4499 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4500 			break;
4501 		}
4502 		ill_interface = ill_interface->illif_next;
4503 	}
4504 
4505 	/*
4506 	 * Interface type not found, create one.
4507 	 */
4508 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4509 
4510 		ill_g_head_t ghead;
4511 
4512 		/*
4513 		 * allocate ill_if_t structure
4514 		 */
4515 
4516 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4517 		if (ill_interface == NULL) {
4518 			return (ENOMEM);
4519 		}
4520 
4521 
4522 
4523 		(void) strcpy(ill_interface->illif_name, name);
4524 		ill_interface->illif_name_len = name_length;
4525 
4526 		avl_create(&ill_interface->illif_avl_by_ppa,
4527 		    ill_compare_ppa, sizeof (ill_t),
4528 		    offsetof(struct ill_s, ill_avl_byppa));
4529 
4530 		/*
4531 		 * link the structure in the back to maintain order
4532 		 * of configuration for ifconfig output.
4533 		 */
4534 		ghead = ipst->ips_ill_g_heads[index];
4535 		insque(ill_interface, ghead.ill_g_list_tail);
4536 
4537 	}
4538 
4539 	if (ill->ill_ppa == UINT_MAX)
4540 		check_length = B_TRUE;
4541 
4542 	error = ill_alloc_ppa(ill_interface, ill);
4543 	if (error != 0) {
4544 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4545 			ill_delete_interface_type(ill->ill_ifptr);
4546 		return (error);
4547 	}
4548 
4549 	/*
4550 	 * When the ppa is choosen by the system, check that there is
4551 	 * enough space to insert ppa. if a specific ppa was passed in this
4552 	 * check is not required as the interface name passed in will have
4553 	 * the right ppa in it.
4554 	 */
4555 	if (check_length) {
4556 		/*
4557 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4558 		 */
4559 		char buf[sizeof (uint_t) * 3];
4560 
4561 		/*
4562 		 * convert ppa to string to calculate the amount of space
4563 		 * required for it in the name.
4564 		 */
4565 		numtos(ill->ill_ppa, buf);
4566 
4567 		/* Do we have enough space to insert ppa ? */
4568 
4569 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4570 			/* Free ppa and interface type struct */
4571 			if (ill_interface->illif_ppa_arena != NULL) {
4572 				vmem_free(ill_interface->illif_ppa_arena,
4573 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4574 			}
4575 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4576 				ill_delete_interface_type(ill->ill_ifptr);
4577 
4578 			return (EINVAL);
4579 		}
4580 	}
4581 
4582 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4583 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4584 
4585 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4586 	    &where);
4587 	ill->ill_ifptr = ill_interface;
4588 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4589 
4590 	ill_phyint_reinit(ill);
4591 	return (0);
4592 }
4593 
4594 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4595 static boolean_t
4596 ipsq_init(ill_t *ill)
4597 {
4598 	ipsq_t  *ipsq;
4599 
4600 	/* Init the ipsq and impicitly enter as writer */
4601 	ill->ill_phyint->phyint_ipsq =
4602 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4603 	if (ill->ill_phyint->phyint_ipsq == NULL)
4604 		return (B_FALSE);
4605 	ipsq = ill->ill_phyint->phyint_ipsq;
4606 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4607 	ill->ill_phyint->phyint_ipsq_next = NULL;
4608 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4609 	ipsq->ipsq_refs = 1;
4610 	ipsq->ipsq_writer = curthread;
4611 	ipsq->ipsq_reentry_cnt = 1;
4612 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4613 #ifdef DEBUG
4614 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack,
4615 	    IPSQ_STACK_DEPTH);
4616 #endif
4617 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4618 	return (B_TRUE);
4619 }
4620 
4621 /*
4622  * ill_init is called by ip_open when a device control stream is opened.
4623  * It does a few initializations, and shoots a DL_INFO_REQ message down
4624  * to the driver.  The response is later picked up in ip_rput_dlpi and
4625  * used to set up default mechanisms for talking to the driver.  (Always
4626  * called as writer.)
4627  *
4628  * If this function returns error, ip_open will call ip_close which in
4629  * turn will call ill_delete to clean up any memory allocated here that
4630  * is not yet freed.
4631  */
4632 int
4633 ill_init(queue_t *q, ill_t *ill)
4634 {
4635 	int	count;
4636 	dl_info_req_t	*dlir;
4637 	mblk_t	*info_mp;
4638 	uchar_t *frag_ptr;
4639 
4640 	/*
4641 	 * The ill is initialized to zero by mi_alloc*(). In addition
4642 	 * some fields already contain valid values, initialized in
4643 	 * ip_open(), before we reach here.
4644 	 */
4645 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4646 
4647 	ill->ill_rq = q;
4648 	ill->ill_wq = WR(q);
4649 
4650 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4651 	    BPRI_HI);
4652 	if (info_mp == NULL)
4653 		return (ENOMEM);
4654 
4655 	/*
4656 	 * Allocate sufficient space to contain our fragment hash table and
4657 	 * the device name.
4658 	 */
4659 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4660 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4661 	if (frag_ptr == NULL) {
4662 		freemsg(info_mp);
4663 		return (ENOMEM);
4664 	}
4665 	ill->ill_frag_ptr = frag_ptr;
4666 	ill->ill_frag_free_num_pkts = 0;
4667 	ill->ill_last_frag_clean_time = 0;
4668 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4669 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4670 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4671 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4672 		    NULL, MUTEX_DEFAULT, NULL);
4673 	}
4674 
4675 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4676 	if (ill->ill_phyint == NULL) {
4677 		freemsg(info_mp);
4678 		mi_free(frag_ptr);
4679 		return (ENOMEM);
4680 	}
4681 
4682 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4683 	/*
4684 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4685 	 * at this point because of the following reason. If we can't
4686 	 * enter the ipsq at some point and cv_wait, the writer that
4687 	 * wakes us up tries to locate us using the list of all phyints
4688 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4689 	 * If we don't set it now, we risk a missed wakeup.
4690 	 */
4691 	ill->ill_phyint->phyint_illv4 = ill;
4692 	ill->ill_ppa = UINT_MAX;
4693 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4694 
4695 	if (!ipsq_init(ill)) {
4696 		freemsg(info_mp);
4697 		mi_free(frag_ptr);
4698 		mi_free(ill->ill_phyint);
4699 		return (ENOMEM);
4700 	}
4701 
4702 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4703 
4704 	/* Frag queue limit stuff */
4705 	ill->ill_frag_count = 0;
4706 	ill->ill_ipf_gen = 0;
4707 
4708 	ill->ill_global_timer = INFINITY;
4709 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4710 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4711 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4712 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4713 
4714 	/*
4715 	 * Initialize IPv6 configuration variables.  The IP module is always
4716 	 * opened as an IPv4 module.  Instead tracking down the cases where
4717 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4718 	 * here for convenience, this has no effect until the ill is set to do
4719 	 * IPv6.
4720 	 */
4721 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4722 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4723 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4724 	ill->ill_max_buf = ND_MAX_Q;
4725 	ill->ill_refcnt = 0;
4726 
4727 	/* Send down the Info Request to the driver. */
4728 	info_mp->b_datap->db_type = M_PCPROTO;
4729 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4730 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4731 	dlir->dl_primitive = DL_INFO_REQ;
4732 
4733 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4734 
4735 	qprocson(q);
4736 	ill_dlpi_send(ill, info_mp);
4737 
4738 	return (0);
4739 }
4740 
4741 /*
4742  * ill_dls_info
4743  * creates datalink socket info from the device.
4744  */
4745 int
4746 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4747 {
4748 	size_t	len;
4749 	ill_t	*ill = ipif->ipif_ill;
4750 
4751 	sdl->sdl_family = AF_LINK;
4752 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4753 	sdl->sdl_type = ill->ill_type;
4754 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4755 	len = strlen(sdl->sdl_data);
4756 	ASSERT(len < 256);
4757 	sdl->sdl_nlen = (uchar_t)len;
4758 	sdl->sdl_alen = ill->ill_phys_addr_length;
4759 	sdl->sdl_slen = 0;
4760 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4761 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4762 
4763 	return (sizeof (struct sockaddr_dl));
4764 }
4765 
4766 /*
4767  * ill_xarp_info
4768  * creates xarp info from the device.
4769  */
4770 static int
4771 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4772 {
4773 	sdl->sdl_family = AF_LINK;
4774 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4775 	sdl->sdl_type = ill->ill_type;
4776 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4777 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4778 	sdl->sdl_alen = ill->ill_phys_addr_length;
4779 	sdl->sdl_slen = 0;
4780 	return (sdl->sdl_nlen);
4781 }
4782 
4783 static int
4784 loopback_kstat_update(kstat_t *ksp, int rw)
4785 {
4786 	kstat_named_t *kn;
4787 	netstackid_t	stackid;
4788 	netstack_t	*ns;
4789 	ip_stack_t	*ipst;
4790 
4791 	if (ksp == NULL || ksp->ks_data == NULL)
4792 		return (EIO);
4793 
4794 	if (rw == KSTAT_WRITE)
4795 		return (EACCES);
4796 
4797 	kn = KSTAT_NAMED_PTR(ksp);
4798 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4799 
4800 	ns = netstack_find_by_stackid(stackid);
4801 	if (ns == NULL)
4802 		return (-1);
4803 
4804 	ipst = ns->netstack_ip;
4805 	if (ipst == NULL) {
4806 		netstack_rele(ns);
4807 		return (-1);
4808 	}
4809 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4810 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4811 	netstack_rele(ns);
4812 	return (0);
4813 }
4814 
4815 /*
4816  * Has ifindex been plumbed already.
4817  * Compares both phyint_ifindex and phyint_group_ifindex.
4818  */
4819 static boolean_t
4820 phyint_exists(uint_t index, ip_stack_t *ipst)
4821 {
4822 	phyint_t *phyi;
4823 
4824 	ASSERT(index != 0);
4825 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4826 	/*
4827 	 * Indexes are stored in the phyint - a common structure
4828 	 * to both IPv4 and IPv6.
4829 	 */
4830 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4831 	for (; phyi != NULL;
4832 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4833 	    phyi, AVL_AFTER)) {
4834 		if (phyi->phyint_ifindex == index ||
4835 		    phyi->phyint_group_ifindex == index)
4836 			return (B_TRUE);
4837 	}
4838 	return (B_FALSE);
4839 }
4840 
4841 /* Pick a unique ifindex */
4842 boolean_t
4843 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4844 {
4845 	uint_t starting_index;
4846 
4847 	if (!ipst->ips_ill_index_wrap) {
4848 		*indexp = ipst->ips_ill_index++;
4849 		if (ipst->ips_ill_index == 0) {
4850 			/* Reached the uint_t limit Next time wrap  */
4851 			ipst->ips_ill_index_wrap = B_TRUE;
4852 		}
4853 		return (B_TRUE);
4854 	}
4855 
4856 	/*
4857 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4858 	 * at this point and don't want to call any function that attempts
4859 	 * to get the lock again.
4860 	 */
4861 	starting_index = ipst->ips_ill_index++;
4862 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4863 		if (ipst->ips_ill_index != 0 &&
4864 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4865 			/* found unused index - use it */
4866 			*indexp = ipst->ips_ill_index;
4867 			return (B_TRUE);
4868 		}
4869 	}
4870 
4871 	/*
4872 	 * all interface indicies are inuse.
4873 	 */
4874 	return (B_FALSE);
4875 }
4876 
4877 /*
4878  * Assign a unique interface index for the phyint.
4879  */
4880 static boolean_t
4881 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4882 {
4883 	ASSERT(phyi->phyint_ifindex == 0);
4884 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4885 }
4886 
4887 /*
4888  * Return a pointer to the ill which matches the supplied name.  Note that
4889  * the ill name length includes the null termination character.  (May be
4890  * called as writer.)
4891  * If do_alloc and the interface is "lo0" it will be automatically created.
4892  * Cannot bump up reference on condemned ills. So dup detect can't be done
4893  * using this func.
4894  */
4895 ill_t *
4896 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4897     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4898     ip_stack_t *ipst)
4899 {
4900 	ill_t	*ill;
4901 	ipif_t	*ipif;
4902 	kstat_named_t	*kn;
4903 	boolean_t isloopback;
4904 	ipsq_t *old_ipsq;
4905 	in6_addr_t ov6addr;
4906 
4907 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4908 
4909 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4910 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4911 	rw_exit(&ipst->ips_ill_g_lock);
4912 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4913 		return (ill);
4914 
4915 	/*
4916 	 * Couldn't find it.  Does this happen to be a lookup for the
4917 	 * loopback device and are we allowed to allocate it?
4918 	 */
4919 	if (!isloopback || !do_alloc)
4920 		return (NULL);
4921 
4922 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4923 
4924 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4925 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4926 		rw_exit(&ipst->ips_ill_g_lock);
4927 		return (ill);
4928 	}
4929 
4930 	/* Create the loopback device on demand */
4931 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4932 	    sizeof (ipif_loopback_name), BPRI_MED));
4933 	if (ill == NULL)
4934 		goto done;
4935 
4936 	*ill = ill_null;
4937 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4938 	ill->ill_ipst = ipst;
4939 	netstack_hold(ipst->ips_netstack);
4940 	/*
4941 	 * For exclusive stacks we set the zoneid to zero
4942 	 * to make IP operate as if in the global zone.
4943 	 */
4944 	ill->ill_zoneid = GLOBAL_ZONEID;
4945 
4946 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4947 	if (ill->ill_phyint == NULL)
4948 		goto done;
4949 
4950 	if (isv6)
4951 		ill->ill_phyint->phyint_illv6 = ill;
4952 	else
4953 		ill->ill_phyint->phyint_illv4 = ill;
4954 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4955 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4956 	/* Add room for tcp+ip headers */
4957 	if (isv6) {
4958 		ill->ill_isv6 = B_TRUE;
4959 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4960 	} else {
4961 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4962 	}
4963 	if (!ill_allocate_mibs(ill))
4964 		goto done;
4965 	ill->ill_max_mtu = ill->ill_max_frag;
4966 	/*
4967 	 * ipif_loopback_name can't be pointed at directly because its used
4968 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4969 	 * from the glist, ill_glist_delete() sets the first character of
4970 	 * ill_name to '\0'.
4971 	 */
4972 	ill->ill_name = (char *)ill + sizeof (*ill);
4973 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4974 	ill->ill_name_length = sizeof (ipif_loopback_name);
4975 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4976 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4977 
4978 	ill->ill_global_timer = INFINITY;
4979 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4980 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4981 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4982 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4983 
4984 	/* No resolver here. */
4985 	ill->ill_net_type = IRE_LOOPBACK;
4986 
4987 	/* Initialize the ipsq */
4988 	if (!ipsq_init(ill))
4989 		goto done;
4990 
4991 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
4992 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
4993 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
4994 #ifdef DEBUG
4995 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
4996 #endif
4997 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
4998 	if (ipif == NULL)
4999 		goto done;
5000 
5001 	ill->ill_flags = ILLF_MULTICAST;
5002 
5003 	ov6addr = ipif->ipif_v6lcl_addr;
5004 	/* Set up default loopback address and mask. */
5005 	if (!isv6) {
5006 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5007 
5008 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5009 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5010 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5011 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5012 		    ipif->ipif_v6subnet);
5013 		ill->ill_flags |= ILLF_IPV4;
5014 	} else {
5015 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5016 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5017 		ipif->ipif_v6net_mask = ipv6_all_ones;
5018 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5019 		    ipif->ipif_v6subnet);
5020 		ill->ill_flags |= ILLF_IPV6;
5021 	}
5022 
5023 	/*
5024 	 * Chain us in at the end of the ill list. hold the ill
5025 	 * before we make it globally visible. 1 for the lookup.
5026 	 */
5027 	ill->ill_refcnt = 0;
5028 	ill_refhold(ill);
5029 
5030 	ill->ill_frag_count = 0;
5031 	ill->ill_frag_free_num_pkts = 0;
5032 	ill->ill_last_frag_clean_time = 0;
5033 
5034 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5035 
5036 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5037 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5038 
5039 	/* Let SCTP know so that it can add this to its list */
5040 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5041 
5042 	/*
5043 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5044 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5045 	 * requires to be after ill_glist_insert() since we need the
5046 	 * ill_index set. Pass on ipv6_loopback as the old address.
5047 	 */
5048 	sctp_update_ipif_addr(ipif, ov6addr);
5049 
5050 	/*
5051 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5052 	 */
5053 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5054 		/* Loopback ills aren't in any IPMP group */
5055 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5056 		ipsq_delete(old_ipsq);
5057 	}
5058 
5059 	/*
5060 	 * Delay this till the ipif is allocated as ipif_allocate
5061 	 * de-references ill_phyint for getting the ifindex. We
5062 	 * can't do this before ipif_allocate because ill_phyint_reinit
5063 	 * -> phyint_assign_ifindex expects ipif to be present.
5064 	 */
5065 	mutex_enter(&ill->ill_phyint->phyint_lock);
5066 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5067 	mutex_exit(&ill->ill_phyint->phyint_lock);
5068 
5069 	if (ipst->ips_loopback_ksp == NULL) {
5070 		/* Export loopback interface statistics */
5071 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5072 		    ipif_loopback_name, "net",
5073 		    KSTAT_TYPE_NAMED, 2, 0,
5074 		    ipst->ips_netstack->netstack_stackid);
5075 		if (ipst->ips_loopback_ksp != NULL) {
5076 			ipst->ips_loopback_ksp->ks_update =
5077 			    loopback_kstat_update;
5078 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5079 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5080 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5081 			ipst->ips_loopback_ksp->ks_private =
5082 			    (void *)(uintptr_t)ipst->ips_netstack->
5083 			    netstack_stackid;
5084 			kstat_install(ipst->ips_loopback_ksp);
5085 		}
5086 	}
5087 
5088 	if (error != NULL)
5089 		*error = 0;
5090 	*did_alloc = B_TRUE;
5091 	rw_exit(&ipst->ips_ill_g_lock);
5092 	return (ill);
5093 done:
5094 	if (ill != NULL) {
5095 		if (ill->ill_phyint != NULL) {
5096 			ipsq_t	*ipsq;
5097 
5098 			ipsq = ill->ill_phyint->phyint_ipsq;
5099 			if (ipsq != NULL) {
5100 				ipsq->ipsq_ipst = NULL;
5101 				kmem_free(ipsq, sizeof (ipsq_t));
5102 			}
5103 			mi_free(ill->ill_phyint);
5104 		}
5105 		ill_free_mib(ill);
5106 		if (ill->ill_ipst != NULL)
5107 			netstack_rele(ill->ill_ipst->ips_netstack);
5108 		mi_free(ill);
5109 	}
5110 	rw_exit(&ipst->ips_ill_g_lock);
5111 	if (error != NULL)
5112 		*error = ENOMEM;
5113 	return (NULL);
5114 }
5115 
5116 /*
5117  * For IPP calls - use the ip_stack_t for global stack.
5118  */
5119 ill_t *
5120 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5121     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5122 {
5123 	ip_stack_t	*ipst;
5124 	ill_t		*ill;
5125 
5126 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5127 	if (ipst == NULL) {
5128 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5129 		return (NULL);
5130 	}
5131 
5132 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5133 	netstack_rele(ipst->ips_netstack);
5134 	return (ill);
5135 }
5136 
5137 /*
5138  * Return a pointer to the ill which matches the index and IP version type.
5139  */
5140 ill_t *
5141 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5142     ipsq_func_t func, int *err, ip_stack_t *ipst)
5143 {
5144 	ill_t	*ill;
5145 	ipsq_t  *ipsq;
5146 	phyint_t *phyi;
5147 
5148 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5149 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5150 
5151 	if (err != NULL)
5152 		*err = 0;
5153 
5154 	/*
5155 	 * Indexes are stored in the phyint - a common structure
5156 	 * to both IPv4 and IPv6.
5157 	 */
5158 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5159 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5160 	    (void *) &index, NULL);
5161 	if (phyi != NULL) {
5162 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5163 		if (ill != NULL) {
5164 			/*
5165 			 * The block comment at the start of ipif_down
5166 			 * explains the use of the macros used below
5167 			 */
5168 			GRAB_CONN_LOCK(q);
5169 			mutex_enter(&ill->ill_lock);
5170 			if (ILL_CAN_LOOKUP(ill)) {
5171 				ill_refhold_locked(ill);
5172 				mutex_exit(&ill->ill_lock);
5173 				RELEASE_CONN_LOCK(q);
5174 				rw_exit(&ipst->ips_ill_g_lock);
5175 				return (ill);
5176 			} else if (ILL_CAN_WAIT(ill, q)) {
5177 				ipsq = ill->ill_phyint->phyint_ipsq;
5178 				mutex_enter(&ipsq->ipsq_lock);
5179 				rw_exit(&ipst->ips_ill_g_lock);
5180 				mutex_exit(&ill->ill_lock);
5181 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5182 				mutex_exit(&ipsq->ipsq_lock);
5183 				RELEASE_CONN_LOCK(q);
5184 				if (err != NULL)
5185 					*err = EINPROGRESS;
5186 				return (NULL);
5187 			}
5188 			RELEASE_CONN_LOCK(q);
5189 			mutex_exit(&ill->ill_lock);
5190 		}
5191 	}
5192 	rw_exit(&ipst->ips_ill_g_lock);
5193 	if (err != NULL)
5194 		*err = ENXIO;
5195 	return (NULL);
5196 }
5197 
5198 /*
5199  * Return the ifindex next in sequence after the passed in ifindex.
5200  * If there is no next ifindex for the given protocol, return 0.
5201  */
5202 uint_t
5203 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5204 {
5205 	phyint_t *phyi;
5206 	phyint_t *phyi_initial;
5207 	uint_t   ifindex;
5208 
5209 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5210 
5211 	if (index == 0) {
5212 		phyi = avl_first(
5213 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5214 	} else {
5215 		phyi = phyi_initial = avl_find(
5216 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5217 		    (void *) &index, NULL);
5218 	}
5219 
5220 	for (; phyi != NULL;
5221 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5222 	    phyi, AVL_AFTER)) {
5223 		/*
5224 		 * If we're not returning the first interface in the tree
5225 		 * and we still haven't moved past the phyint_t that
5226 		 * corresponds to index, avl_walk needs to be called again
5227 		 */
5228 		if (!((index != 0) && (phyi == phyi_initial))) {
5229 			if (isv6) {
5230 				if ((phyi->phyint_illv6) &&
5231 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5232 				    (phyi->phyint_illv6->ill_isv6 == 1))
5233 					break;
5234 			} else {
5235 				if ((phyi->phyint_illv4) &&
5236 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5237 				    (phyi->phyint_illv4->ill_isv6 == 0))
5238 					break;
5239 			}
5240 		}
5241 	}
5242 
5243 	rw_exit(&ipst->ips_ill_g_lock);
5244 
5245 	if (phyi != NULL)
5246 		ifindex = phyi->phyint_ifindex;
5247 	else
5248 		ifindex = 0;
5249 
5250 	return (ifindex);
5251 }
5252 
5253 /*
5254  * Return the ifindex for the named interface.
5255  * If there is no next ifindex for the interface, return 0.
5256  */
5257 uint_t
5258 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5259 {
5260 	phyint_t	*phyi;
5261 	avl_index_t	where = 0;
5262 	uint_t		ifindex;
5263 
5264 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5265 
5266 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5267 	    name, &where)) == NULL) {
5268 		rw_exit(&ipst->ips_ill_g_lock);
5269 		return (0);
5270 	}
5271 
5272 	ifindex = phyi->phyint_ifindex;
5273 
5274 	rw_exit(&ipst->ips_ill_g_lock);
5275 
5276 	return (ifindex);
5277 }
5278 
5279 /*
5280  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5281  * that gives a running thread a reference to the ill. This reference must be
5282  * released by the thread when it is done accessing the ill and related
5283  * objects. ill_refcnt can not be used to account for static references
5284  * such as other structures pointing to an ill. Callers must generally
5285  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5286  * or be sure that the ill is not being deleted or changing state before
5287  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5288  * ill won't change any of its critical state such as address, netmask etc.
5289  */
5290 void
5291 ill_refhold(ill_t *ill)
5292 {
5293 	mutex_enter(&ill->ill_lock);
5294 	ill->ill_refcnt++;
5295 	ILL_TRACE_REF(ill);
5296 	mutex_exit(&ill->ill_lock);
5297 }
5298 
5299 void
5300 ill_refhold_locked(ill_t *ill)
5301 {
5302 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5303 	ill->ill_refcnt++;
5304 	ILL_TRACE_REF(ill);
5305 }
5306 
5307 int
5308 ill_check_and_refhold(ill_t *ill)
5309 {
5310 	mutex_enter(&ill->ill_lock);
5311 	if (ILL_CAN_LOOKUP(ill)) {
5312 		ill_refhold_locked(ill);
5313 		mutex_exit(&ill->ill_lock);
5314 		return (0);
5315 	}
5316 	mutex_exit(&ill->ill_lock);
5317 	return (ILL_LOOKUP_FAILED);
5318 }
5319 
5320 /*
5321  * Must not be called while holding any locks. Otherwise if this is
5322  * the last reference to be released, there is a chance of recursive mutex
5323  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5324  * to restart an ioctl.
5325  */
5326 void
5327 ill_refrele(ill_t *ill)
5328 {
5329 	mutex_enter(&ill->ill_lock);
5330 	ASSERT(ill->ill_refcnt != 0);
5331 	ill->ill_refcnt--;
5332 	ILL_UNTRACE_REF(ill);
5333 	if (ill->ill_refcnt != 0) {
5334 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5335 		mutex_exit(&ill->ill_lock);
5336 		return;
5337 	}
5338 
5339 	/* Drops the ill_lock */
5340 	ipif_ill_refrele_tail(ill);
5341 }
5342 
5343 /*
5344  * Obtain a weak reference count on the ill. This reference ensures the
5345  * ill won't be freed, but the ill may change any of its critical state
5346  * such as netmask, address etc. Returns an error if the ill has started
5347  * closing.
5348  */
5349 boolean_t
5350 ill_waiter_inc(ill_t *ill)
5351 {
5352 	mutex_enter(&ill->ill_lock);
5353 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5354 		mutex_exit(&ill->ill_lock);
5355 		return (B_FALSE);
5356 	}
5357 	ill->ill_waiters++;
5358 	mutex_exit(&ill->ill_lock);
5359 	return (B_TRUE);
5360 }
5361 
5362 void
5363 ill_waiter_dcr(ill_t *ill)
5364 {
5365 	mutex_enter(&ill->ill_lock);
5366 	ill->ill_waiters--;
5367 	if (ill->ill_waiters == 0)
5368 		cv_broadcast(&ill->ill_cv);
5369 	mutex_exit(&ill->ill_lock);
5370 }
5371 
5372 /*
5373  * Named Dispatch routine to produce a formatted report on all ILLs.
5374  * This report is accessed by using the ndd utility to "get" ND variable
5375  * "ip_ill_status".
5376  */
5377 /* ARGSUSED */
5378 int
5379 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5380 {
5381 	ill_t		*ill;
5382 	ill_walk_context_t ctx;
5383 	ip_stack_t	*ipst;
5384 
5385 	ipst = CONNQ_TO_IPST(q);
5386 
5387 	(void) mi_mpprintf(mp,
5388 	    "ILL      " MI_COL_HDRPAD_STR
5389 	/*   01234567[89ABCDEF] */
5390 	    "rq       " MI_COL_HDRPAD_STR
5391 	/*   01234567[89ABCDEF] */
5392 	    "wq       " MI_COL_HDRPAD_STR
5393 	/*   01234567[89ABCDEF] */
5394 	    "upcnt mxfrg err name");
5395 	/*   12345 12345 123 xxxxxxxx  */
5396 
5397 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5398 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5399 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5400 		(void) mi_mpprintf(mp,
5401 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5402 		    "%05u %05u %03d %s",
5403 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5404 		    ill->ill_ipif_up_count,
5405 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5406 	}
5407 	rw_exit(&ipst->ips_ill_g_lock);
5408 
5409 	return (0);
5410 }
5411 
5412 /*
5413  * Named Dispatch routine to produce a formatted report on all IPIFs.
5414  * This report is accessed by using the ndd utility to "get" ND variable
5415  * "ip_ipif_status".
5416  */
5417 /* ARGSUSED */
5418 int
5419 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5420 {
5421 	char	buf1[INET6_ADDRSTRLEN];
5422 	char	buf2[INET6_ADDRSTRLEN];
5423 	char	buf3[INET6_ADDRSTRLEN];
5424 	char	buf4[INET6_ADDRSTRLEN];
5425 	char	buf5[INET6_ADDRSTRLEN];
5426 	char	buf6[INET6_ADDRSTRLEN];
5427 	char	buf[LIFNAMSIZ];
5428 	ill_t	*ill;
5429 	ipif_t	*ipif;
5430 	nv_t	*nvp;
5431 	uint64_t flags;
5432 	zoneid_t zoneid;
5433 	ill_walk_context_t ctx;
5434 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5435 
5436 	(void) mi_mpprintf(mp,
5437 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5438 	    "\tlocal address\n"
5439 	    "\tsrc address\n"
5440 	    "\tsubnet\n"
5441 	    "\tmask\n"
5442 	    "\tbroadcast\n"
5443 	    "\tp-p-dst");
5444 
5445 	ASSERT(q->q_next == NULL);
5446 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5447 
5448 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5449 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5450 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5451 		for (ipif = ill->ill_ipif; ipif != NULL;
5452 		    ipif = ipif->ipif_next) {
5453 			if (zoneid != GLOBAL_ZONEID &&
5454 			    zoneid != ipif->ipif_zoneid &&
5455 			    ipif->ipif_zoneid != ALL_ZONES)
5456 				continue;
5457 
5458 			ipif_get_name(ipif, buf, sizeof (buf));
5459 			(void) mi_mpprintf(mp,
5460 			    MI_COL_PTRFMT_STR
5461 			    "%04u %05u %u/%u/%u %s %d",
5462 			    (void *)ipif,
5463 			    ipif->ipif_metric, ipif->ipif_mtu,
5464 			    ipif->ipif_ib_pkt_count,
5465 			    ipif->ipif_ob_pkt_count,
5466 			    ipif->ipif_fo_pkt_count,
5467 			    buf,
5468 			    ipif->ipif_zoneid);
5469 
5470 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5471 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5472 
5473 		/* Tack on text strings for any flags. */
5474 		nvp = ipif_nv_tbl;
5475 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5476 			if (nvp->nv_value & flags)
5477 				(void) mi_mpprintf_nr(mp, " %s",
5478 				    nvp->nv_name);
5479 		}
5480 		(void) mi_mpprintf(mp,
5481 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5482 		    inet_ntop(AF_INET6,
5483 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5484 		    inet_ntop(AF_INET6,
5485 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5486 		    inet_ntop(AF_INET6,
5487 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5488 		    inet_ntop(AF_INET6,
5489 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5490 		    inet_ntop(AF_INET6,
5491 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5492 		    inet_ntop(AF_INET6,
5493 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5494 		}
5495 	}
5496 	rw_exit(&ipst->ips_ill_g_lock);
5497 	return (0);
5498 }
5499 
5500 /*
5501  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5502  * driver.  We construct best guess defaults for lower level information that
5503  * we need.  If an interface is brought up without injection of any overriding
5504  * information from outside, we have to be ready to go with these defaults.
5505  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5506  * we primarely want the dl_provider_style.
5507  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5508  * at which point we assume the other part of the information is valid.
5509  */
5510 void
5511 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5512 {
5513 	uchar_t		*brdcst_addr;
5514 	uint_t		brdcst_addr_length, phys_addr_length;
5515 	t_scalar_t	sap_length;
5516 	dl_info_ack_t	*dlia;
5517 	ip_m_t		*ipm;
5518 	dl_qos_cl_sel1_t *sel1;
5519 
5520 	ASSERT(IAM_WRITER_ILL(ill));
5521 
5522 	/*
5523 	 * Till the ill is fully up ILL_CHANGING will be set and
5524 	 * the ill is not globally visible. So no need for a lock.
5525 	 */
5526 	dlia = (dl_info_ack_t *)mp->b_rptr;
5527 	ill->ill_mactype = dlia->dl_mac_type;
5528 
5529 	ipm = ip_m_lookup(dlia->dl_mac_type);
5530 	if (ipm == NULL) {
5531 		ipm = ip_m_lookup(DL_OTHER);
5532 		ASSERT(ipm != NULL);
5533 	}
5534 	ill->ill_media = ipm;
5535 
5536 	/*
5537 	 * When the new DLPI stuff is ready we'll pull lengths
5538 	 * from dlia.
5539 	 */
5540 	if (dlia->dl_version == DL_VERSION_2) {
5541 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5542 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5543 		    brdcst_addr_length);
5544 		if (brdcst_addr == NULL) {
5545 			brdcst_addr_length = 0;
5546 		}
5547 		sap_length = dlia->dl_sap_length;
5548 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5549 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5550 		    brdcst_addr_length, sap_length, phys_addr_length));
5551 	} else {
5552 		brdcst_addr_length = 6;
5553 		brdcst_addr = ip_six_byte_all_ones;
5554 		sap_length = -2;
5555 		phys_addr_length = brdcst_addr_length;
5556 	}
5557 
5558 	ill->ill_bcast_addr_length = brdcst_addr_length;
5559 	ill->ill_phys_addr_length = phys_addr_length;
5560 	ill->ill_sap_length = sap_length;
5561 	ill->ill_max_frag = dlia->dl_max_sdu;
5562 	ill->ill_max_mtu = ill->ill_max_frag;
5563 
5564 	ill->ill_type = ipm->ip_m_type;
5565 
5566 	if (!ill->ill_dlpi_style_set) {
5567 		if (dlia->dl_provider_style == DL_STYLE2)
5568 			ill->ill_needs_attach = 1;
5569 
5570 		/*
5571 		 * Allocate the first ipif on this ill. We don't delay it
5572 		 * further as ioctl handling assumes atleast one ipif to
5573 		 * be present.
5574 		 *
5575 		 * At this point we don't know whether the ill is v4 or v6.
5576 		 * We will know this whan the SIOCSLIFNAME happens and
5577 		 * the correct value for ill_isv6 will be assigned in
5578 		 * ipif_set_values(). We need to hold the ill lock and
5579 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5580 		 * the wakeup.
5581 		 */
5582 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5583 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5584 		mutex_enter(&ill->ill_lock);
5585 		ASSERT(ill->ill_dlpi_style_set == 0);
5586 		ill->ill_dlpi_style_set = 1;
5587 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5588 		cv_broadcast(&ill->ill_cv);
5589 		mutex_exit(&ill->ill_lock);
5590 		freemsg(mp);
5591 		return;
5592 	}
5593 	ASSERT(ill->ill_ipif != NULL);
5594 	/*
5595 	 * We know whether it is IPv4 or IPv6 now, as this is the
5596 	 * second DL_INFO_ACK we are recieving in response to the
5597 	 * DL_INFO_REQ sent in ipif_set_values.
5598 	 */
5599 	if (ill->ill_isv6)
5600 		ill->ill_sap = IP6_DL_SAP;
5601 	else
5602 		ill->ill_sap = IP_DL_SAP;
5603 	/*
5604 	 * Set ipif_mtu which is used to set the IRE's
5605 	 * ire_max_frag value. The driver could have sent
5606 	 * a different mtu from what it sent last time. No
5607 	 * need to call ipif_mtu_change because IREs have
5608 	 * not yet been created.
5609 	 */
5610 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5611 	/*
5612 	 * Clear all the flags that were set based on ill_bcast_addr_length
5613 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5614 	 * changed now and we need to re-evaluate.
5615 	 */
5616 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5617 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5618 
5619 	/*
5620 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5621 	 * changed now.
5622 	 */
5623 	if (ill->ill_bcast_addr_length == 0) {
5624 		if (ill->ill_resolver_mp != NULL)
5625 			freemsg(ill->ill_resolver_mp);
5626 		if (ill->ill_bcast_mp != NULL)
5627 			freemsg(ill->ill_bcast_mp);
5628 		if (ill->ill_flags & ILLF_XRESOLV)
5629 			ill->ill_net_type = IRE_IF_RESOLVER;
5630 		else
5631 			ill->ill_net_type = IRE_IF_NORESOLVER;
5632 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5633 		    ill->ill_phys_addr_length,
5634 		    ill->ill_sap,
5635 		    ill->ill_sap_length);
5636 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5637 
5638 		if (ill->ill_isv6)
5639 			/*
5640 			 * Note: xresolv interfaces will eventually need NOARP
5641 			 * set here as well, but that will require those
5642 			 * external resolvers to have some knowledge of
5643 			 * that flag and act appropriately. Not to be changed
5644 			 * at present.
5645 			 */
5646 			ill->ill_flags |= ILLF_NONUD;
5647 		else
5648 			ill->ill_flags |= ILLF_NOARP;
5649 
5650 		if (ill->ill_phys_addr_length == 0) {
5651 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5652 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5653 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5654 			} else {
5655 				/* pt-pt supports multicast. */
5656 				ill->ill_flags |= ILLF_MULTICAST;
5657 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5658 			}
5659 		}
5660 	} else {
5661 		ill->ill_net_type = IRE_IF_RESOLVER;
5662 		if (ill->ill_bcast_mp != NULL)
5663 			freemsg(ill->ill_bcast_mp);
5664 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5665 		    ill->ill_bcast_addr_length, ill->ill_sap,
5666 		    ill->ill_sap_length);
5667 		/*
5668 		 * Later detect lack of DLPI driver multicast
5669 		 * capability by catching DL_ENABMULTI errors in
5670 		 * ip_rput_dlpi.
5671 		 */
5672 		ill->ill_flags |= ILLF_MULTICAST;
5673 		if (!ill->ill_isv6)
5674 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5675 	}
5676 	/* By default an interface does not support any CoS marking */
5677 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5678 
5679 	/*
5680 	 * If we get QoS information in DL_INFO_ACK, the device supports
5681 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5682 	 */
5683 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5684 	    dlia->dl_qos_length);
5685 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5686 		ill->ill_flags |= ILLF_COS_ENABLED;
5687 	}
5688 
5689 	/* Clear any previous error indication. */
5690 	ill->ill_error = 0;
5691 	freemsg(mp);
5692 }
5693 
5694 /*
5695  * Perform various checks to verify that an address would make sense as a
5696  * local, remote, or subnet interface address.
5697  */
5698 static boolean_t
5699 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5700 {
5701 	ipaddr_t	net_mask;
5702 
5703 	/*
5704 	 * Don't allow all zeroes, or all ones, but allow
5705 	 * all ones netmask.
5706 	 */
5707 	if ((net_mask = ip_net_mask(addr)) == 0)
5708 		return (B_FALSE);
5709 	/* A given netmask overrides the "guess" netmask */
5710 	if (subnet_mask != 0)
5711 		net_mask = subnet_mask;
5712 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5713 	    (addr == (addr | ~net_mask)))) {
5714 		return (B_FALSE);
5715 	}
5716 
5717 	/*
5718 	 * Even if the netmask is all ones, we do not allow address to be
5719 	 * 255.255.255.255
5720 	 */
5721 	if (addr == INADDR_BROADCAST)
5722 		return (B_FALSE);
5723 
5724 	if (CLASSD(addr))
5725 		return (B_FALSE);
5726 
5727 	return (B_TRUE);
5728 }
5729 
5730 #define	V6_IPIF_LINKLOCAL(p)	\
5731 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5732 
5733 /*
5734  * Compare two given ipifs and check if the second one is better than
5735  * the first one using the order of preference (not taking deprecated
5736  * into acount) specified in ipif_lookup_multicast().
5737  */
5738 static boolean_t
5739 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5740 {
5741 	/* Check the least preferred first. */
5742 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5743 		/* If both ipifs are the same, use the first one. */
5744 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5745 			return (B_FALSE);
5746 		else
5747 			return (B_TRUE);
5748 	}
5749 
5750 	/* For IPv6, check for link local address. */
5751 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5752 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5753 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5754 			/* The second one is equal or less preferred. */
5755 			return (B_FALSE);
5756 		} else {
5757 			return (B_TRUE);
5758 		}
5759 	}
5760 
5761 	/* Then check for point to point interface. */
5762 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5763 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5764 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5765 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5766 			return (B_FALSE);
5767 		} else {
5768 			return (B_TRUE);
5769 		}
5770 	}
5771 
5772 	/* old_ipif is a normal interface, so no need to use the new one. */
5773 	return (B_FALSE);
5774 }
5775 
5776 /*
5777  * Find any non-virtual, not condemned, and up multicast capable interface
5778  * given an IP instance and zoneid.  Order of preference is:
5779  *
5780  * 1. normal
5781  * 1.1 normal, but deprecated
5782  * 2. point to point
5783  * 2.1 point to point, but deprecated
5784  * 3. link local
5785  * 3.1 link local, but deprecated
5786  * 4. loopback.
5787  */
5788 ipif_t *
5789 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5790 {
5791 	ill_t			*ill;
5792 	ill_walk_context_t	ctx;
5793 	ipif_t			*ipif;
5794 	ipif_t			*saved_ipif = NULL;
5795 	ipif_t			*dep_ipif = NULL;
5796 
5797 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5798 	if (isv6)
5799 		ill = ILL_START_WALK_V6(&ctx, ipst);
5800 	else
5801 		ill = ILL_START_WALK_V4(&ctx, ipst);
5802 
5803 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5804 		mutex_enter(&ill->ill_lock);
5805 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5806 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5807 			mutex_exit(&ill->ill_lock);
5808 			continue;
5809 		}
5810 		for (ipif = ill->ill_ipif; ipif != NULL;
5811 		    ipif = ipif->ipif_next) {
5812 			if (zoneid != ipif->ipif_zoneid &&
5813 			    zoneid != ALL_ZONES &&
5814 			    ipif->ipif_zoneid != ALL_ZONES) {
5815 				continue;
5816 			}
5817 			if (!(ipif->ipif_flags & IPIF_UP) ||
5818 			    !IPIF_CAN_LOOKUP(ipif)) {
5819 				continue;
5820 			}
5821 
5822 			/*
5823 			 * Found one candidate.  If it is deprecated,
5824 			 * remember it in dep_ipif.  If it is not deprecated,
5825 			 * remember it in saved_ipif.
5826 			 */
5827 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5828 				if (dep_ipif == NULL) {
5829 					dep_ipif = ipif;
5830 				} else if (ipif_comp_multi(dep_ipif, ipif,
5831 				    isv6)) {
5832 					/*
5833 					 * If the previous dep_ipif does not
5834 					 * belong to the same ill, we've done
5835 					 * a ipif_refhold() on it.  So we need
5836 					 * to release it.
5837 					 */
5838 					if (dep_ipif->ipif_ill != ill)
5839 						ipif_refrele(dep_ipif);
5840 					dep_ipif = ipif;
5841 				}
5842 				continue;
5843 			}
5844 			if (saved_ipif == NULL) {
5845 				saved_ipif = ipif;
5846 			} else {
5847 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5848 					if (saved_ipif->ipif_ill != ill)
5849 						ipif_refrele(saved_ipif);
5850 					saved_ipif = ipif;
5851 				}
5852 			}
5853 		}
5854 		/*
5855 		 * Before going to the next ill, do a ipif_refhold() on the
5856 		 * saved ones.
5857 		 */
5858 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5859 			ipif_refhold_locked(saved_ipif);
5860 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5861 			ipif_refhold_locked(dep_ipif);
5862 		mutex_exit(&ill->ill_lock);
5863 	}
5864 	rw_exit(&ipst->ips_ill_g_lock);
5865 
5866 	/*
5867 	 * If we have only the saved_ipif, return it.  But if we have both
5868 	 * saved_ipif and dep_ipif, check to see which one is better.
5869 	 */
5870 	if (saved_ipif != NULL) {
5871 		if (dep_ipif != NULL) {
5872 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5873 				ipif_refrele(saved_ipif);
5874 				return (dep_ipif);
5875 			} else {
5876 				ipif_refrele(dep_ipif);
5877 				return (saved_ipif);
5878 			}
5879 		}
5880 		return (saved_ipif);
5881 	} else {
5882 		return (dep_ipif);
5883 	}
5884 }
5885 
5886 /*
5887  * This function is called when an application does not specify an interface
5888  * to be used for multicast traffic (joining a group/sending data).  It
5889  * calls ire_lookup_multi() to look for an interface route for the
5890  * specified multicast group.  Doing this allows the administrator to add
5891  * prefix routes for multicast to indicate which interface to be used for
5892  * multicast traffic in the above scenario.  The route could be for all
5893  * multicast (224.0/4), for a single multicast group (a /32 route) or
5894  * anything in between.  If there is no such multicast route, we just find
5895  * any multicast capable interface and return it.  The returned ipif
5896  * is refhold'ed.
5897  */
5898 ipif_t *
5899 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5900 {
5901 	ire_t			*ire;
5902 	ipif_t			*ipif;
5903 
5904 	ire = ire_lookup_multi(group, zoneid, ipst);
5905 	if (ire != NULL) {
5906 		ipif = ire->ire_ipif;
5907 		ipif_refhold(ipif);
5908 		ire_refrele(ire);
5909 		return (ipif);
5910 	}
5911 
5912 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5913 }
5914 
5915 /*
5916  * Look for an ipif with the specified interface address and destination.
5917  * The destination address is used only for matching point-to-point interfaces.
5918  */
5919 ipif_t *
5920 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5921     ipsq_func_t func, int *error, ip_stack_t *ipst)
5922 {
5923 	ipif_t	*ipif;
5924 	ill_t	*ill;
5925 	ill_walk_context_t ctx;
5926 	ipsq_t	*ipsq;
5927 
5928 	if (error != NULL)
5929 		*error = 0;
5930 
5931 	/*
5932 	 * First match all the point-to-point interfaces
5933 	 * before looking at non-point-to-point interfaces.
5934 	 * This is done to avoid returning non-point-to-point
5935 	 * ipif instead of unnumbered point-to-point ipif.
5936 	 */
5937 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5938 	ill = ILL_START_WALK_V4(&ctx, ipst);
5939 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5940 		GRAB_CONN_LOCK(q);
5941 		mutex_enter(&ill->ill_lock);
5942 		for (ipif = ill->ill_ipif; ipif != NULL;
5943 		    ipif = ipif->ipif_next) {
5944 			/* Allow the ipif to be down */
5945 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5946 			    (ipif->ipif_lcl_addr == if_addr) &&
5947 			    (ipif->ipif_pp_dst_addr == dst)) {
5948 				/*
5949 				 * The block comment at the start of ipif_down
5950 				 * explains the use of the macros used below
5951 				 */
5952 				if (IPIF_CAN_LOOKUP(ipif)) {
5953 					ipif_refhold_locked(ipif);
5954 					mutex_exit(&ill->ill_lock);
5955 					RELEASE_CONN_LOCK(q);
5956 					rw_exit(&ipst->ips_ill_g_lock);
5957 					return (ipif);
5958 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5959 					ipsq = ill->ill_phyint->phyint_ipsq;
5960 					mutex_enter(&ipsq->ipsq_lock);
5961 					mutex_exit(&ill->ill_lock);
5962 					rw_exit(&ipst->ips_ill_g_lock);
5963 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5964 					    ill);
5965 					mutex_exit(&ipsq->ipsq_lock);
5966 					RELEASE_CONN_LOCK(q);
5967 					if (error != NULL)
5968 						*error = EINPROGRESS;
5969 					return (NULL);
5970 				}
5971 			}
5972 		}
5973 		mutex_exit(&ill->ill_lock);
5974 		RELEASE_CONN_LOCK(q);
5975 	}
5976 	rw_exit(&ipst->ips_ill_g_lock);
5977 
5978 	/* lookup the ipif based on interface address */
5979 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5980 	    ipst);
5981 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5982 	return (ipif);
5983 }
5984 
5985 /*
5986  * Look for an ipif with the specified address. For point-point links
5987  * we look for matches on either the destination address and the local
5988  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5989  * is set.
5990  * Matches on a specific ill if match_ill is set.
5991  */
5992 ipif_t *
5993 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5994     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5995 {
5996 	ipif_t  *ipif;
5997 	ill_t   *ill;
5998 	boolean_t ptp = B_FALSE;
5999 	ipsq_t	*ipsq;
6000 	ill_walk_context_t	ctx;
6001 
6002 	if (error != NULL)
6003 		*error = 0;
6004 
6005 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6006 	/*
6007 	 * Repeat twice, first based on local addresses and
6008 	 * next time for pointopoint.
6009 	 */
6010 repeat:
6011 	ill = ILL_START_WALK_V4(&ctx, ipst);
6012 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6013 		if (match_ill != NULL && ill != match_ill) {
6014 			continue;
6015 		}
6016 		GRAB_CONN_LOCK(q);
6017 		mutex_enter(&ill->ill_lock);
6018 		for (ipif = ill->ill_ipif; ipif != NULL;
6019 		    ipif = ipif->ipif_next) {
6020 			if (zoneid != ALL_ZONES &&
6021 			    zoneid != ipif->ipif_zoneid &&
6022 			    ipif->ipif_zoneid != ALL_ZONES)
6023 				continue;
6024 			/* Allow the ipif to be down */
6025 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6026 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6027 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6028 			    (ipif->ipif_pp_dst_addr == addr))) {
6029 				/*
6030 				 * The block comment at the start of ipif_down
6031 				 * explains the use of the macros used below
6032 				 */
6033 				if (IPIF_CAN_LOOKUP(ipif)) {
6034 					ipif_refhold_locked(ipif);
6035 					mutex_exit(&ill->ill_lock);
6036 					RELEASE_CONN_LOCK(q);
6037 					rw_exit(&ipst->ips_ill_g_lock);
6038 					return (ipif);
6039 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6040 					ipsq = ill->ill_phyint->phyint_ipsq;
6041 					mutex_enter(&ipsq->ipsq_lock);
6042 					mutex_exit(&ill->ill_lock);
6043 					rw_exit(&ipst->ips_ill_g_lock);
6044 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6045 					    ill);
6046 					mutex_exit(&ipsq->ipsq_lock);
6047 					RELEASE_CONN_LOCK(q);
6048 					if (error != NULL)
6049 						*error = EINPROGRESS;
6050 					return (NULL);
6051 				}
6052 			}
6053 		}
6054 		mutex_exit(&ill->ill_lock);
6055 		RELEASE_CONN_LOCK(q);
6056 	}
6057 
6058 	/* If we already did the ptp case, then we are done */
6059 	if (ptp) {
6060 		rw_exit(&ipst->ips_ill_g_lock);
6061 		if (error != NULL)
6062 			*error = ENXIO;
6063 		return (NULL);
6064 	}
6065 	ptp = B_TRUE;
6066 	goto repeat;
6067 }
6068 
6069 /*
6070  * Look for an ipif with the specified address. For point-point links
6071  * we look for matches on either the destination address and the local
6072  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6073  * is set.
6074  * Matches on a specific ill if match_ill is set.
6075  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6076  */
6077 zoneid_t
6078 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6079 {
6080 	zoneid_t zoneid;
6081 	ipif_t  *ipif;
6082 	ill_t   *ill;
6083 	boolean_t ptp = B_FALSE;
6084 	ill_walk_context_t	ctx;
6085 
6086 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6087 	/*
6088 	 * Repeat twice, first based on local addresses and
6089 	 * next time for pointopoint.
6090 	 */
6091 repeat:
6092 	ill = ILL_START_WALK_V4(&ctx, ipst);
6093 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6094 		if (match_ill != NULL && ill != match_ill) {
6095 			continue;
6096 		}
6097 		mutex_enter(&ill->ill_lock);
6098 		for (ipif = ill->ill_ipif; ipif != NULL;
6099 		    ipif = ipif->ipif_next) {
6100 			/* Allow the ipif to be down */
6101 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6102 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6103 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6104 			    (ipif->ipif_pp_dst_addr == addr)) &&
6105 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6106 				zoneid = ipif->ipif_zoneid;
6107 				mutex_exit(&ill->ill_lock);
6108 				rw_exit(&ipst->ips_ill_g_lock);
6109 				/*
6110 				 * If ipif_zoneid was ALL_ZONES then we have
6111 				 * a trusted extensions shared IP address.
6112 				 * In that case GLOBAL_ZONEID works to send.
6113 				 */
6114 				if (zoneid == ALL_ZONES)
6115 					zoneid = GLOBAL_ZONEID;
6116 				return (zoneid);
6117 			}
6118 		}
6119 		mutex_exit(&ill->ill_lock);
6120 	}
6121 
6122 	/* If we already did the ptp case, then we are done */
6123 	if (ptp) {
6124 		rw_exit(&ipst->ips_ill_g_lock);
6125 		return (ALL_ZONES);
6126 	}
6127 	ptp = B_TRUE;
6128 	goto repeat;
6129 }
6130 
6131 /*
6132  * Look for an ipif that matches the specified remote address i.e. the
6133  * ipif that would receive the specified packet.
6134  * First look for directly connected interfaces and then do a recursive
6135  * IRE lookup and pick the first ipif corresponding to the source address in the
6136  * ire.
6137  * Returns: held ipif
6138  */
6139 ipif_t *
6140 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6141 {
6142 	ipif_t	*ipif;
6143 	ire_t	*ire;
6144 	ip_stack_t	*ipst = ill->ill_ipst;
6145 
6146 	ASSERT(!ill->ill_isv6);
6147 
6148 	/*
6149 	 * Someone could be changing this ipif currently or change it
6150 	 * after we return this. Thus  a few packets could use the old
6151 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6152 	 * will atomically be updated or cleaned up with the new value
6153 	 * Thus we don't need a lock to check the flags or other attrs below.
6154 	 */
6155 	mutex_enter(&ill->ill_lock);
6156 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6157 		if (!IPIF_CAN_LOOKUP(ipif))
6158 			continue;
6159 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6160 		    ipif->ipif_zoneid != ALL_ZONES)
6161 			continue;
6162 		/* Allow the ipif to be down */
6163 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6164 			if ((ipif->ipif_pp_dst_addr == addr) ||
6165 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6166 			    ipif->ipif_lcl_addr == addr)) {
6167 				ipif_refhold_locked(ipif);
6168 				mutex_exit(&ill->ill_lock);
6169 				return (ipif);
6170 			}
6171 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6172 			ipif_refhold_locked(ipif);
6173 			mutex_exit(&ill->ill_lock);
6174 			return (ipif);
6175 		}
6176 	}
6177 	mutex_exit(&ill->ill_lock);
6178 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6179 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6180 	if (ire != NULL) {
6181 		/*
6182 		 * The callers of this function wants to know the
6183 		 * interface on which they have to send the replies
6184 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6185 		 * derived from different ills, we really don't care
6186 		 * what we return here.
6187 		 */
6188 		ipif = ire->ire_ipif;
6189 		if (ipif != NULL) {
6190 			ipif_refhold(ipif);
6191 			ire_refrele(ire);
6192 			return (ipif);
6193 		}
6194 		ire_refrele(ire);
6195 	}
6196 	/* Pick the first interface */
6197 	ipif = ipif_get_next_ipif(NULL, ill);
6198 	return (ipif);
6199 }
6200 
6201 /*
6202  * This func does not prevent refcnt from increasing. But if
6203  * the caller has taken steps to that effect, then this func
6204  * can be used to determine whether the ill has become quiescent
6205  */
6206 static boolean_t
6207 ill_is_quiescent(ill_t *ill)
6208 {
6209 	ipif_t	*ipif;
6210 
6211 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6212 
6213 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6214 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6215 			return (B_FALSE);
6216 		}
6217 	}
6218 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6219 		return (B_FALSE);
6220 	}
6221 	return (B_TRUE);
6222 }
6223 
6224 boolean_t
6225 ill_is_freeable(ill_t *ill)
6226 {
6227 	ipif_t	*ipif;
6228 
6229 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6230 
6231 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6232 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6233 			return (B_FALSE);
6234 		}
6235 	}
6236 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6237 		return (B_FALSE);
6238 	}
6239 	return (B_TRUE);
6240 }
6241 
6242 /*
6243  * This func does not prevent refcnt from increasing. But if
6244  * the caller has taken steps to that effect, then this func
6245  * can be used to determine whether the ipif has become quiescent
6246  */
6247 static boolean_t
6248 ipif_is_quiescent(ipif_t *ipif)
6249 {
6250 	ill_t *ill;
6251 
6252 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6253 
6254 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6255 		return (B_FALSE);
6256 	}
6257 
6258 	ill = ipif->ipif_ill;
6259 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6260 	    ill->ill_logical_down) {
6261 		return (B_TRUE);
6262 	}
6263 
6264 	/* This is the last ipif going down or being deleted on this ill */
6265 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6266 		return (B_FALSE);
6267 	}
6268 
6269 	return (B_TRUE);
6270 }
6271 
6272 /*
6273  * return true if the ipif can be destroyed: the ipif has to be quiescent
6274  * with zero references from ire/nce/ilm to it.
6275  */
6276 static boolean_t
6277 ipif_is_freeable(ipif_t *ipif)
6278 {
6279 
6280 	ill_t *ill;
6281 
6282 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6283 
6284 	if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6285 		return (B_FALSE);
6286 	}
6287 
6288 	ill = ipif->ipif_ill;
6289 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6290 	    ill->ill_logical_down) {
6291 		return (B_TRUE);
6292 	}
6293 
6294 	/* This is the last ipif going down or being deleted on this ill */
6295 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6296 		return (B_FALSE);
6297 	}
6298 
6299 	return (B_TRUE);
6300 }
6301 
6302 /*
6303  * This func does not prevent refcnt from increasing. But if
6304  * the caller has taken steps to that effect, then this func
6305  * can be used to determine whether the ipifs marked with IPIF_MOVING
6306  * have become quiescent and can be moved in a failover/failback.
6307  */
6308 static ipif_t *
6309 ill_quiescent_to_move(ill_t *ill)
6310 {
6311 	ipif_t  *ipif;
6312 
6313 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6314 
6315 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6316 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6317 			if (ipif->ipif_refcnt != 0 ||
6318 			    !IPIF_DOWN_OK(ipif)) {
6319 				return (ipif);
6320 			}
6321 		}
6322 	}
6323 	return (NULL);
6324 }
6325 
6326 /*
6327  * The ipif/ill/ire has been refreled. Do the tail processing.
6328  * Determine if the ipif or ill in question has become quiescent and if so
6329  * wakeup close and/or restart any queued pending ioctl that is waiting
6330  * for the ipif_down (or ill_down)
6331  */
6332 void
6333 ipif_ill_refrele_tail(ill_t *ill)
6334 {
6335 	mblk_t	*mp;
6336 	conn_t	*connp;
6337 	ipsq_t	*ipsq;
6338 	ipif_t	*ipif;
6339 	dl_notify_ind_t *dlindp;
6340 
6341 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6342 
6343 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6344 	    ill_is_freeable(ill)) {
6345 		/* ill_close may be waiting */
6346 		cv_broadcast(&ill->ill_cv);
6347 	}
6348 
6349 	/* ipsq can't change because ill_lock  is held */
6350 	ipsq = ill->ill_phyint->phyint_ipsq;
6351 	if (ipsq->ipsq_waitfor == 0) {
6352 		/* Not waiting for anything, just return. */
6353 		mutex_exit(&ill->ill_lock);
6354 		return;
6355 	}
6356 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6357 	    ipsq->ipsq_pending_ipif != NULL);
6358 	/*
6359 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6360 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6361 	 * be zero for restarting an ioctl that ends up downing the ill.
6362 	 */
6363 	ipif = ipsq->ipsq_pending_ipif;
6364 	if (ipif->ipif_ill != ill) {
6365 		/* The ioctl is pending on some other ill. */
6366 		mutex_exit(&ill->ill_lock);
6367 		return;
6368 	}
6369 
6370 	switch (ipsq->ipsq_waitfor) {
6371 	case IPIF_DOWN:
6372 		if (!ipif_is_quiescent(ipif)) {
6373 			mutex_exit(&ill->ill_lock);
6374 			return;
6375 		}
6376 		break;
6377 	case IPIF_FREE:
6378 		if (!ipif_is_freeable(ipif)) {
6379 			mutex_exit(&ill->ill_lock);
6380 			return;
6381 		}
6382 		break;
6383 
6384 	case ILL_DOWN:
6385 		if (!ill_is_quiescent(ill)) {
6386 			mutex_exit(&ill->ill_lock);
6387 			return;
6388 		}
6389 		break;
6390 	case ILL_FREE:
6391 		/*
6392 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6393 		 * waits synchronously in ip_close, and no message is queued in
6394 		 * ipsq_pending_mp at all in this case
6395 		 */
6396 		if (!ill_is_freeable(ill)) {
6397 			mutex_exit(&ill->ill_lock);
6398 			return;
6399 		}
6400 		break;
6401 
6402 	case ILL_MOVE_OK:
6403 		if (ill_quiescent_to_move(ill) != NULL) {
6404 			mutex_exit(&ill->ill_lock);
6405 			return;
6406 		}
6407 		break;
6408 	default:
6409 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6410 		    (void *)ipsq, ipsq->ipsq_waitfor);
6411 	}
6412 
6413 	/*
6414 	 * Incr refcnt for the qwriter_ip call below which
6415 	 * does a refrele
6416 	 */
6417 	ill_refhold_locked(ill);
6418 	mp = ipsq_pending_mp_get(ipsq, &connp);
6419 	mutex_exit(&ill->ill_lock);
6420 
6421 	ASSERT(mp != NULL);
6422 	/*
6423 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6424 	 * we can only get here when the current operation decides it
6425 	 * it needs to quiesce via ipsq_pending_mp_add().
6426 	 */
6427 	switch (mp->b_datap->db_type) {
6428 	case M_PCPROTO:
6429 	case M_PROTO:
6430 		/*
6431 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6432 		 */
6433 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6434 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6435 
6436 		switch (dlindp->dl_notification) {
6437 		case DL_NOTE_PHYS_ADDR:
6438 			qwriter_ip(ill, ill->ill_rq, mp,
6439 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6440 			return;
6441 		default:
6442 			ASSERT(0);
6443 		}
6444 		break;
6445 
6446 	case M_ERROR:
6447 	case M_HANGUP:
6448 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6449 		    B_TRUE);
6450 		return;
6451 
6452 	case M_IOCTL:
6453 	case M_IOCDATA:
6454 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6455 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6456 		return;
6457 
6458 	default:
6459 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6460 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6461 	}
6462 }
6463 
6464 #ifdef DEBUG
6465 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6466 static void
6467 th_trace_rrecord(th_trace_t *th_trace)
6468 {
6469 	tr_buf_t *tr_buf;
6470 	uint_t lastref;
6471 
6472 	lastref = th_trace->th_trace_lastref;
6473 	lastref++;
6474 	if (lastref == TR_BUF_MAX)
6475 		lastref = 0;
6476 	th_trace->th_trace_lastref = lastref;
6477 	tr_buf = &th_trace->th_trbuf[lastref];
6478 	tr_buf->tr_time = lbolt;
6479 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6480 }
6481 
6482 static void
6483 th_trace_free(void *value)
6484 {
6485 	th_trace_t *th_trace = value;
6486 
6487 	ASSERT(th_trace->th_refcnt == 0);
6488 	kmem_free(th_trace, sizeof (*th_trace));
6489 }
6490 
6491 /*
6492  * Find or create the per-thread hash table used to track object references.
6493  * The ipst argument is NULL if we shouldn't allocate.
6494  *
6495  * Accesses per-thread data, so there's no need to lock here.
6496  */
6497 static mod_hash_t *
6498 th_trace_gethash(ip_stack_t *ipst)
6499 {
6500 	th_hash_t *thh;
6501 
6502 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6503 		mod_hash_t *mh;
6504 		char name[256];
6505 		size_t objsize, rshift;
6506 		int retv;
6507 
6508 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6509 			return (NULL);
6510 		(void) snprintf(name, sizeof (name), "th_trace_%p", curthread);
6511 
6512 		/*
6513 		 * We use mod_hash_create_extended here rather than the more
6514 		 * obvious mod_hash_create_ptrhash because the latter has a
6515 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6516 		 * block.
6517 		 */
6518 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6519 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6520 		rshift = highbit(objsize);
6521 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6522 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6523 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6524 		if (mh == NULL) {
6525 			kmem_free(thh, sizeof (*thh));
6526 			return (NULL);
6527 		}
6528 		thh->thh_hash = mh;
6529 		thh->thh_ipst = ipst;
6530 		/*
6531 		 * We trace ills, ipifs, ires, and nces.  All of these are
6532 		 * per-IP-stack, so the lock on the thread list is as well.
6533 		 */
6534 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6535 		list_insert_tail(&ip_thread_list, thh);
6536 		rw_exit(&ip_thread_rwlock);
6537 		retv = tsd_set(ip_thread_data, thh);
6538 		ASSERT(retv == 0);
6539 	}
6540 	return (thh != NULL ? thh->thh_hash : NULL);
6541 }
6542 
6543 boolean_t
6544 th_trace_ref(const void *obj, ip_stack_t *ipst)
6545 {
6546 	th_trace_t *th_trace;
6547 	mod_hash_t *mh;
6548 	mod_hash_val_t val;
6549 
6550 	if ((mh = th_trace_gethash(ipst)) == NULL)
6551 		return (B_FALSE);
6552 
6553 	/*
6554 	 * Attempt to locate the trace buffer for this obj and thread.
6555 	 * If it does not exist, then allocate a new trace buffer and
6556 	 * insert into the hash.
6557 	 */
6558 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6559 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6560 		if (th_trace == NULL)
6561 			return (B_FALSE);
6562 
6563 		th_trace->th_id = curthread;
6564 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6565 		    (mod_hash_val_t)th_trace) != 0) {
6566 			kmem_free(th_trace, sizeof (th_trace_t));
6567 			return (B_FALSE);
6568 		}
6569 	} else {
6570 		th_trace = (th_trace_t *)val;
6571 	}
6572 
6573 	ASSERT(th_trace->th_refcnt >= 0 &&
6574 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6575 
6576 	th_trace->th_refcnt++;
6577 	th_trace_rrecord(th_trace);
6578 	return (B_TRUE);
6579 }
6580 
6581 /*
6582  * For the purpose of tracing a reference release, we assume that global
6583  * tracing is always on and that the same thread initiated the reference hold
6584  * is releasing.
6585  */
6586 void
6587 th_trace_unref(const void *obj)
6588 {
6589 	int retv;
6590 	mod_hash_t *mh;
6591 	th_trace_t *th_trace;
6592 	mod_hash_val_t val;
6593 
6594 	mh = th_trace_gethash(NULL);
6595 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6596 	ASSERT(retv == 0);
6597 	th_trace = (th_trace_t *)val;
6598 
6599 	ASSERT(th_trace->th_refcnt > 0);
6600 	th_trace->th_refcnt--;
6601 	th_trace_rrecord(th_trace);
6602 }
6603 
6604 /*
6605  * If tracing has been disabled, then we assume that the reference counts are
6606  * now useless, and we clear them out before destroying the entries.
6607  */
6608 void
6609 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6610 {
6611 	th_hash_t	*thh;
6612 	mod_hash_t	*mh;
6613 	mod_hash_val_t	val;
6614 	th_trace_t	*th_trace;
6615 	int		retv;
6616 
6617 	rw_enter(&ip_thread_rwlock, RW_READER);
6618 	for (thh = list_head(&ip_thread_list); thh != NULL;
6619 	    thh = list_next(&ip_thread_list, thh)) {
6620 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6621 		    &val) == 0) {
6622 			th_trace = (th_trace_t *)val;
6623 			if (trace_disable)
6624 				th_trace->th_refcnt = 0;
6625 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6626 			ASSERT(retv == 0);
6627 		}
6628 	}
6629 	rw_exit(&ip_thread_rwlock);
6630 }
6631 
6632 void
6633 ipif_trace_ref(ipif_t *ipif)
6634 {
6635 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6636 
6637 	if (ipif->ipif_trace_disable)
6638 		return;
6639 
6640 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6641 		ipif->ipif_trace_disable = B_TRUE;
6642 		ipif_trace_cleanup(ipif);
6643 	}
6644 }
6645 
6646 void
6647 ipif_untrace_ref(ipif_t *ipif)
6648 {
6649 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6650 
6651 	if (!ipif->ipif_trace_disable)
6652 		th_trace_unref(ipif);
6653 }
6654 
6655 void
6656 ill_trace_ref(ill_t *ill)
6657 {
6658 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6659 
6660 	if (ill->ill_trace_disable)
6661 		return;
6662 
6663 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6664 		ill->ill_trace_disable = B_TRUE;
6665 		ill_trace_cleanup(ill);
6666 	}
6667 }
6668 
6669 void
6670 ill_untrace_ref(ill_t *ill)
6671 {
6672 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6673 
6674 	if (!ill->ill_trace_disable)
6675 		th_trace_unref(ill);
6676 }
6677 
6678 /*
6679  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6680  * failure, ipif_trace_disable is set.
6681  */
6682 static void
6683 ipif_trace_cleanup(const ipif_t *ipif)
6684 {
6685 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6686 }
6687 
6688 /*
6689  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6690  * failure, ill_trace_disable is set.
6691  */
6692 static void
6693 ill_trace_cleanup(const ill_t *ill)
6694 {
6695 	th_trace_cleanup(ill, ill->ill_trace_disable);
6696 }
6697 #endif /* DEBUG */
6698 
6699 void
6700 ipif_refhold_locked(ipif_t *ipif)
6701 {
6702 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6703 	ipif->ipif_refcnt++;
6704 	IPIF_TRACE_REF(ipif);
6705 }
6706 
6707 void
6708 ipif_refhold(ipif_t *ipif)
6709 {
6710 	ill_t	*ill;
6711 
6712 	ill = ipif->ipif_ill;
6713 	mutex_enter(&ill->ill_lock);
6714 	ipif->ipif_refcnt++;
6715 	IPIF_TRACE_REF(ipif);
6716 	mutex_exit(&ill->ill_lock);
6717 }
6718 
6719 /*
6720  * Must not be called while holding any locks. Otherwise if this is
6721  * the last reference to be released there is a chance of recursive mutex
6722  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6723  * to restart an ioctl.
6724  */
6725 void
6726 ipif_refrele(ipif_t *ipif)
6727 {
6728 	ill_t	*ill;
6729 
6730 	ill = ipif->ipif_ill;
6731 
6732 	mutex_enter(&ill->ill_lock);
6733 	ASSERT(ipif->ipif_refcnt != 0);
6734 	ipif->ipif_refcnt--;
6735 	IPIF_UNTRACE_REF(ipif);
6736 	if (ipif->ipif_refcnt != 0) {
6737 		mutex_exit(&ill->ill_lock);
6738 		return;
6739 	}
6740 
6741 	/* Drops the ill_lock */
6742 	ipif_ill_refrele_tail(ill);
6743 }
6744 
6745 ipif_t *
6746 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6747 {
6748 	ipif_t	*ipif;
6749 
6750 	mutex_enter(&ill->ill_lock);
6751 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6752 	    ipif != NULL; ipif = ipif->ipif_next) {
6753 		if (!IPIF_CAN_LOOKUP(ipif))
6754 			continue;
6755 		ipif_refhold_locked(ipif);
6756 		mutex_exit(&ill->ill_lock);
6757 		return (ipif);
6758 	}
6759 	mutex_exit(&ill->ill_lock);
6760 	return (NULL);
6761 }
6762 
6763 /*
6764  * TODO: make this table extendible at run time
6765  * Return a pointer to the mac type info for 'mac_type'
6766  */
6767 static ip_m_t *
6768 ip_m_lookup(t_uscalar_t mac_type)
6769 {
6770 	ip_m_t	*ipm;
6771 
6772 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6773 		if (ipm->ip_m_mac_type == mac_type)
6774 			return (ipm);
6775 	return (NULL);
6776 }
6777 
6778 /*
6779  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6780  * ipif_arg is passed in to associate it with the correct interface.
6781  * We may need to restart this operation if the ipif cannot be looked up
6782  * due to an exclusive operation that is currently in progress. The restart
6783  * entry point is specified by 'func'
6784  */
6785 int
6786 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6787     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6788     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6789     struct rtsa_s *sp, ip_stack_t *ipst)
6790 {
6791 	ire_t	*ire;
6792 	ire_t	*gw_ire = NULL;
6793 	ipif_t	*ipif = NULL;
6794 	boolean_t ipif_refheld = B_FALSE;
6795 	uint_t	type;
6796 	int	match_flags = MATCH_IRE_TYPE;
6797 	int	error;
6798 	tsol_gc_t *gc = NULL;
6799 	tsol_gcgrp_t *gcgrp = NULL;
6800 	boolean_t gcgrp_xtraref = B_FALSE;
6801 
6802 	ip1dbg(("ip_rt_add:"));
6803 
6804 	if (ire_arg != NULL)
6805 		*ire_arg = NULL;
6806 
6807 	/*
6808 	 * If this is the case of RTF_HOST being set, then we set the netmask
6809 	 * to all ones (regardless if one was supplied).
6810 	 */
6811 	if (flags & RTF_HOST)
6812 		mask = IP_HOST_MASK;
6813 
6814 	/*
6815 	 * Prevent routes with a zero gateway from being created (since
6816 	 * interfaces can currently be plumbed and brought up no assigned
6817 	 * address).
6818 	 */
6819 	if (gw_addr == 0)
6820 		return (ENETUNREACH);
6821 	/*
6822 	 * Get the ipif, if any, corresponding to the gw_addr
6823 	 */
6824 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6825 	    ipst);
6826 	if (ipif != NULL) {
6827 		if (IS_VNI(ipif->ipif_ill)) {
6828 			ipif_refrele(ipif);
6829 			return (EINVAL);
6830 		}
6831 		ipif_refheld = B_TRUE;
6832 	} else if (error == EINPROGRESS) {
6833 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6834 		return (EINPROGRESS);
6835 	} else {
6836 		error = 0;
6837 	}
6838 
6839 	if (ipif != NULL) {
6840 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6841 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6842 	} else {
6843 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6844 	}
6845 
6846 	/*
6847 	 * GateD will attempt to create routes with a loopback interface
6848 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6849 	 * these routes to be added, but create them as interface routes
6850 	 * since the gateway is an interface address.
6851 	 */
6852 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6853 		flags &= ~RTF_GATEWAY;
6854 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6855 		    mask == IP_HOST_MASK) {
6856 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6857 			    ALL_ZONES, NULL, match_flags, ipst);
6858 			if (ire != NULL) {
6859 				ire_refrele(ire);
6860 				if (ipif_refheld)
6861 					ipif_refrele(ipif);
6862 				return (EEXIST);
6863 			}
6864 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6865 			    "for 0x%x\n", (void *)ipif,
6866 			    ipif->ipif_ire_type,
6867 			    ntohl(ipif->ipif_lcl_addr)));
6868 			ire = ire_create(
6869 			    (uchar_t *)&dst_addr,	/* dest address */
6870 			    (uchar_t *)&mask,		/* mask */
6871 			    (uchar_t *)&ipif->ipif_src_addr,
6872 			    NULL,			/* no gateway */
6873 			    &ipif->ipif_mtu,
6874 			    NULL,
6875 			    ipif->ipif_rq,		/* recv-from queue */
6876 			    NULL,			/* no send-to queue */
6877 			    ipif->ipif_ire_type,	/* LOOPBACK */
6878 			    ipif,
6879 			    0,
6880 			    0,
6881 			    0,
6882 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6883 			    RTF_PRIVATE : 0,
6884 			    &ire_uinfo_null,
6885 			    NULL,
6886 			    NULL,
6887 			    ipst);
6888 
6889 			if (ire == NULL) {
6890 				if (ipif_refheld)
6891 					ipif_refrele(ipif);
6892 				return (ENOMEM);
6893 			}
6894 			error = ire_add(&ire, q, mp, func, B_FALSE);
6895 			if (error == 0)
6896 				goto save_ire;
6897 			if (ipif_refheld)
6898 				ipif_refrele(ipif);
6899 			return (error);
6900 
6901 		}
6902 	}
6903 
6904 	/*
6905 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6906 	 * and the gateway address provided is one of the system's interface
6907 	 * addresses.  By using the routing socket interface and supplying an
6908 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6909 	 * specifying an interface route to be created is available which uses
6910 	 * the interface index that specifies the outgoing interface rather than
6911 	 * the address of an outgoing interface (which may not be able to
6912 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6913 	 * flag, routes can be specified which not only specify the next-hop to
6914 	 * be used when routing to a certain prefix, but also which outgoing
6915 	 * interface should be used.
6916 	 *
6917 	 * Previously, interfaces would have unique addresses assigned to them
6918 	 * and so the address assigned to a particular interface could be used
6919 	 * to identify a particular interface.  One exception to this was the
6920 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6921 	 *
6922 	 * With the advent of IPv6 and its link-local addresses, this
6923 	 * restriction was relaxed and interfaces could share addresses between
6924 	 * themselves.  In fact, typically all of the link-local interfaces on
6925 	 * an IPv6 node or router will have the same link-local address.  In
6926 	 * order to differentiate between these interfaces, the use of an
6927 	 * interface index is necessary and this index can be carried inside a
6928 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6929 	 * of using the interface index, however, is that all of the ipif's that
6930 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6931 	 * cannot be used to differentiate between ipif's (or logical
6932 	 * interfaces) that belong to the same ill (physical interface).
6933 	 *
6934 	 * For example, in the following case involving IPv4 interfaces and
6935 	 * logical interfaces
6936 	 *
6937 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6938 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6939 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6940 	 *
6941 	 * the ipif's corresponding to each of these interface routes can be
6942 	 * uniquely identified by the "gateway" (actually interface address).
6943 	 *
6944 	 * In this case involving multiple IPv6 default routes to a particular
6945 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6946 	 * default route is of interest:
6947 	 *
6948 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6949 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6950 	 */
6951 
6952 	/* RTF_GATEWAY not set */
6953 	if (!(flags & RTF_GATEWAY)) {
6954 		queue_t	*stq;
6955 
6956 		if (sp != NULL) {
6957 			ip2dbg(("ip_rt_add: gateway security attributes "
6958 			    "cannot be set with interface route\n"));
6959 			if (ipif_refheld)
6960 				ipif_refrele(ipif);
6961 			return (EINVAL);
6962 		}
6963 
6964 		/*
6965 		 * As the interface index specified with the RTA_IFP sockaddr is
6966 		 * the same for all ipif's off of an ill, the matching logic
6967 		 * below uses MATCH_IRE_ILL if such an index was specified.
6968 		 * This means that routes sharing the same prefix when added
6969 		 * using a RTA_IFP sockaddr must have distinct interface
6970 		 * indices (namely, they must be on distinct ill's).
6971 		 *
6972 		 * On the other hand, since the gateway address will usually be
6973 		 * different for each ipif on the system, the matching logic
6974 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6975 		 * route.  This means that interface routes for the same prefix
6976 		 * can be created if they belong to distinct ipif's and if a
6977 		 * RTA_IFP sockaddr is not present.
6978 		 */
6979 		if (ipif_arg != NULL) {
6980 			if (ipif_refheld)  {
6981 				ipif_refrele(ipif);
6982 				ipif_refheld = B_FALSE;
6983 			}
6984 			ipif = ipif_arg;
6985 			match_flags |= MATCH_IRE_ILL;
6986 		} else {
6987 			/*
6988 			 * Check the ipif corresponding to the gw_addr
6989 			 */
6990 			if (ipif == NULL)
6991 				return (ENETUNREACH);
6992 			match_flags |= MATCH_IRE_IPIF;
6993 		}
6994 		ASSERT(ipif != NULL);
6995 
6996 		/*
6997 		 * We check for an existing entry at this point.
6998 		 *
6999 		 * Since a netmask isn't passed in via the ioctl interface
7000 		 * (SIOCADDRT), we don't check for a matching netmask in that
7001 		 * case.
7002 		 */
7003 		if (!ioctl_msg)
7004 			match_flags |= MATCH_IRE_MASK;
7005 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
7006 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7007 		if (ire != NULL) {
7008 			ire_refrele(ire);
7009 			if (ipif_refheld)
7010 				ipif_refrele(ipif);
7011 			return (EEXIST);
7012 		}
7013 
7014 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7015 		    ? ipif->ipif_rq : ipif->ipif_wq;
7016 
7017 		/*
7018 		 * Create a copy of the IRE_LOOPBACK,
7019 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7020 		 * the modified address and netmask.
7021 		 */
7022 		ire = ire_create(
7023 		    (uchar_t *)&dst_addr,
7024 		    (uint8_t *)&mask,
7025 		    (uint8_t *)&ipif->ipif_src_addr,
7026 		    NULL,
7027 		    &ipif->ipif_mtu,
7028 		    NULL,
7029 		    NULL,
7030 		    stq,
7031 		    ipif->ipif_net_type,
7032 		    ipif,
7033 		    0,
7034 		    0,
7035 		    0,
7036 		    flags,
7037 		    &ire_uinfo_null,
7038 		    NULL,
7039 		    NULL,
7040 		    ipst);
7041 		if (ire == NULL) {
7042 			if (ipif_refheld)
7043 				ipif_refrele(ipif);
7044 			return (ENOMEM);
7045 		}
7046 
7047 		/*
7048 		 * Some software (for example, GateD and Sun Cluster) attempts
7049 		 * to create (what amount to) IRE_PREFIX routes with the
7050 		 * loopback address as the gateway.  This is primarily done to
7051 		 * set up prefixes with the RTF_REJECT flag set (for example,
7052 		 * when generating aggregate routes.)
7053 		 *
7054 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7055 		 * IRE_LOOPBACK, then we map the request into a
7056 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
7057 		 * these interface routes, by definition, can only be that.
7058 		 *
7059 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7060 		 * routine, but rather using ire_create() directly.
7061 		 *
7062 		 */
7063 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
7064 			ire->ire_type = IRE_IF_NORESOLVER;
7065 			ire->ire_flags |= RTF_BLACKHOLE;
7066 		}
7067 
7068 		error = ire_add(&ire, q, mp, func, B_FALSE);
7069 		if (error == 0)
7070 			goto save_ire;
7071 
7072 		/*
7073 		 * In the result of failure, ire_add() will have already
7074 		 * deleted the ire in question, so there is no need to
7075 		 * do that here.
7076 		 */
7077 		if (ipif_refheld)
7078 			ipif_refrele(ipif);
7079 		return (error);
7080 	}
7081 	if (ipif_refheld) {
7082 		ipif_refrele(ipif);
7083 		ipif_refheld = B_FALSE;
7084 	}
7085 
7086 	/*
7087 	 * Get an interface IRE for the specified gateway.
7088 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7089 	 * gateway, it is currently unreachable and we fail the request
7090 	 * accordingly.
7091 	 */
7092 	ipif = ipif_arg;
7093 	if (ipif_arg != NULL)
7094 		match_flags |= MATCH_IRE_ILL;
7095 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7096 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7097 	if (gw_ire == NULL)
7098 		return (ENETUNREACH);
7099 
7100 	/*
7101 	 * We create one of three types of IREs as a result of this request
7102 	 * based on the netmask.  A netmask of all ones (which is automatically
7103 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7104 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7105 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7106 	 * destination prefix.
7107 	 */
7108 	if (mask == IP_HOST_MASK)
7109 		type = IRE_HOST;
7110 	else if (mask == 0)
7111 		type = IRE_DEFAULT;
7112 	else
7113 		type = IRE_PREFIX;
7114 
7115 	/* check for a duplicate entry */
7116 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7117 	    NULL, ALL_ZONES, 0, NULL,
7118 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7119 	if (ire != NULL) {
7120 		ire_refrele(gw_ire);
7121 		ire_refrele(ire);
7122 		return (EEXIST);
7123 	}
7124 
7125 	/* Security attribute exists */
7126 	if (sp != NULL) {
7127 		tsol_gcgrp_addr_t ga;
7128 
7129 		/* find or create the gateway credentials group */
7130 		ga.ga_af = AF_INET;
7131 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7132 
7133 		/* we hold reference to it upon success */
7134 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7135 		if (gcgrp == NULL) {
7136 			ire_refrele(gw_ire);
7137 			return (ENOMEM);
7138 		}
7139 
7140 		/*
7141 		 * Create and add the security attribute to the group; a
7142 		 * reference to the group is made upon allocating a new
7143 		 * entry successfully.  If it finds an already-existing
7144 		 * entry for the security attribute in the group, it simply
7145 		 * returns it and no new reference is made to the group.
7146 		 */
7147 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7148 		if (gc == NULL) {
7149 			/* release reference held by gcgrp_lookup */
7150 			GCGRP_REFRELE(gcgrp);
7151 			ire_refrele(gw_ire);
7152 			return (ENOMEM);
7153 		}
7154 	}
7155 
7156 	/* Create the IRE. */
7157 	ire = ire_create(
7158 	    (uchar_t *)&dst_addr,		/* dest address */
7159 	    (uchar_t *)&mask,			/* mask */
7160 	    /* src address assigned by the caller? */
7161 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7162 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7163 	    (uchar_t *)&gw_addr,		/* gateway address */
7164 	    &gw_ire->ire_max_frag,
7165 	    NULL,				/* no src nce */
7166 	    NULL,				/* no recv-from queue */
7167 	    NULL,				/* no send-to queue */
7168 	    (ushort_t)type,			/* IRE type */
7169 	    ipif_arg,
7170 	    0,
7171 	    0,
7172 	    0,
7173 	    flags,
7174 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7175 	    gc,					/* security attribute */
7176 	    NULL,
7177 	    ipst);
7178 
7179 	/*
7180 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7181 	 * reference to the 'gcgrp'. We can now release the extra reference
7182 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7183 	 */
7184 	if (gcgrp_xtraref)
7185 		GCGRP_REFRELE(gcgrp);
7186 	if (ire == NULL) {
7187 		if (gc != NULL)
7188 			GC_REFRELE(gc);
7189 		ire_refrele(gw_ire);
7190 		return (ENOMEM);
7191 	}
7192 
7193 	/*
7194 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7195 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7196 	 */
7197 
7198 	/* Add the new IRE. */
7199 	error = ire_add(&ire, q, mp, func, B_FALSE);
7200 	if (error != 0) {
7201 		/*
7202 		 * In the result of failure, ire_add() will have already
7203 		 * deleted the ire in question, so there is no need to
7204 		 * do that here.
7205 		 */
7206 		ire_refrele(gw_ire);
7207 		return (error);
7208 	}
7209 
7210 	if (flags & RTF_MULTIRT) {
7211 		/*
7212 		 * Invoke the CGTP (multirouting) filtering module
7213 		 * to add the dst address in the filtering database.
7214 		 * Replicated inbound packets coming from that address
7215 		 * will be filtered to discard the duplicates.
7216 		 * It is not necessary to call the CGTP filter hook
7217 		 * when the dst address is a broadcast or multicast,
7218 		 * because an IP source address cannot be a broadcast
7219 		 * or a multicast.
7220 		 */
7221 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7222 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7223 		if (ire_dst != NULL) {
7224 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7225 			ire_refrele(ire_dst);
7226 			goto save_ire;
7227 		}
7228 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7229 		    !CLASSD(ire->ire_addr)) {
7230 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7231 			    ipst->ips_netstack->netstack_stackid,
7232 			    ire->ire_addr,
7233 			    ire->ire_gateway_addr,
7234 			    ire->ire_src_addr,
7235 			    gw_ire->ire_src_addr);
7236 			if (res != 0) {
7237 				ire_refrele(gw_ire);
7238 				ire_delete(ire);
7239 				return (res);
7240 			}
7241 		}
7242 	}
7243 
7244 	/*
7245 	 * Now that the prefix IRE entry has been created, delete any
7246 	 * existing gateway IRE cache entries as well as any IRE caches
7247 	 * using the gateway, and force them to be created through
7248 	 * ip_newroute.
7249 	 */
7250 	if (gc != NULL) {
7251 		ASSERT(gcgrp != NULL);
7252 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7253 	}
7254 
7255 save_ire:
7256 	if (gw_ire != NULL) {
7257 		ire_refrele(gw_ire);
7258 	}
7259 	if (ipif != NULL) {
7260 		/*
7261 		 * Save enough information so that we can recreate the IRE if
7262 		 * the interface goes down and then up.  The metrics associated
7263 		 * with the route will be saved as well when rts_setmetrics() is
7264 		 * called after the IRE has been created.  In the case where
7265 		 * memory cannot be allocated, none of this information will be
7266 		 * saved.
7267 		 */
7268 		ipif_save_ire(ipif, ire);
7269 	}
7270 	if (ioctl_msg)
7271 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7272 	if (ire_arg != NULL) {
7273 		/*
7274 		 * Store the ire that was successfully added into where ire_arg
7275 		 * points to so that callers don't have to look it up
7276 		 * themselves (but they are responsible for ire_refrele()ing
7277 		 * the ire when they are finished with it).
7278 		 */
7279 		*ire_arg = ire;
7280 	} else {
7281 		ire_refrele(ire);		/* Held in ire_add */
7282 	}
7283 	if (ipif_refheld)
7284 		ipif_refrele(ipif);
7285 	return (0);
7286 }
7287 
7288 /*
7289  * ip_rt_delete is called to delete an IPv4 route.
7290  * ipif_arg is passed in to associate it with the correct interface.
7291  * We may need to restart this operation if the ipif cannot be looked up
7292  * due to an exclusive operation that is currently in progress. The restart
7293  * entry point is specified by 'func'
7294  */
7295 /* ARGSUSED4 */
7296 int
7297 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7298     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7299     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7300 {
7301 	ire_t	*ire = NULL;
7302 	ipif_t	*ipif;
7303 	boolean_t ipif_refheld = B_FALSE;
7304 	uint_t	type;
7305 	uint_t	match_flags = MATCH_IRE_TYPE;
7306 	int	err = 0;
7307 
7308 	ip1dbg(("ip_rt_delete:"));
7309 	/*
7310 	 * If this is the case of RTF_HOST being set, then we set the netmask
7311 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7312 	 */
7313 	if (flags & RTF_HOST) {
7314 		mask = IP_HOST_MASK;
7315 		match_flags |= MATCH_IRE_MASK;
7316 	} else if (rtm_addrs & RTA_NETMASK) {
7317 		match_flags |= MATCH_IRE_MASK;
7318 	}
7319 
7320 	/*
7321 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7322 	 * we check if the gateway address is one of our interfaces first,
7323 	 * and fall back on RTF_GATEWAY routes.
7324 	 *
7325 	 * This makes it possible to delete an original
7326 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7327 	 *
7328 	 * As the interface index specified with the RTA_IFP sockaddr is the
7329 	 * same for all ipif's off of an ill, the matching logic below uses
7330 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7331 	 * sharing the same prefix and interface index as the the route
7332 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7333 	 * is specified in the request.
7334 	 *
7335 	 * On the other hand, since the gateway address will usually be
7336 	 * different for each ipif on the system, the matching logic
7337 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7338 	 * route.  This means that interface routes for the same prefix can be
7339 	 * uniquely identified if they belong to distinct ipif's and if a
7340 	 * RTA_IFP sockaddr is not present.
7341 	 *
7342 	 * For more detail on specifying routes by gateway address and by
7343 	 * interface index, see the comments in ip_rt_add().
7344 	 */
7345 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7346 	    ipst);
7347 	if (ipif != NULL)
7348 		ipif_refheld = B_TRUE;
7349 	else if (err == EINPROGRESS)
7350 		return (err);
7351 	else
7352 		err = 0;
7353 	if (ipif != NULL) {
7354 		if (ipif_arg != NULL) {
7355 			if (ipif_refheld) {
7356 				ipif_refrele(ipif);
7357 				ipif_refheld = B_FALSE;
7358 			}
7359 			ipif = ipif_arg;
7360 			match_flags |= MATCH_IRE_ILL;
7361 		} else {
7362 			match_flags |= MATCH_IRE_IPIF;
7363 		}
7364 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7365 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7366 			    ALL_ZONES, NULL, match_flags, ipst);
7367 		}
7368 		if (ire == NULL) {
7369 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7370 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7371 			    match_flags, ipst);
7372 		}
7373 	}
7374 
7375 	if (ire == NULL) {
7376 		/*
7377 		 * At this point, the gateway address is not one of our own
7378 		 * addresses or a matching interface route was not found.  We
7379 		 * set the IRE type to lookup based on whether
7380 		 * this is a host route, a default route or just a prefix.
7381 		 *
7382 		 * If an ipif_arg was passed in, then the lookup is based on an
7383 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7384 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7385 		 * set as the route being looked up is not a traditional
7386 		 * interface route.
7387 		 */
7388 		match_flags &= ~MATCH_IRE_IPIF;
7389 		match_flags |= MATCH_IRE_GW;
7390 		if (ipif_arg != NULL)
7391 			match_flags |= MATCH_IRE_ILL;
7392 		if (mask == IP_HOST_MASK)
7393 			type = IRE_HOST;
7394 		else if (mask == 0)
7395 			type = IRE_DEFAULT;
7396 		else
7397 			type = IRE_PREFIX;
7398 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7399 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7400 	}
7401 
7402 	if (ipif_refheld)
7403 		ipif_refrele(ipif);
7404 
7405 	/* ipif is not refheld anymore */
7406 	if (ire == NULL)
7407 		return (ESRCH);
7408 
7409 	if (ire->ire_flags & RTF_MULTIRT) {
7410 		/*
7411 		 * Invoke the CGTP (multirouting) filtering module
7412 		 * to remove the dst address from the filtering database.
7413 		 * Packets coming from that address will no longer be
7414 		 * filtered to remove duplicates.
7415 		 */
7416 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7417 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7418 			    ipst->ips_netstack->netstack_stackid,
7419 			    ire->ire_addr, ire->ire_gateway_addr);
7420 		}
7421 		ip_cgtp_bcast_delete(ire, ipst);
7422 	}
7423 
7424 	ipif = ire->ire_ipif;
7425 	if (ipif != NULL)
7426 		ipif_remove_ire(ipif, ire);
7427 	if (ioctl_msg)
7428 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7429 	ire_delete(ire);
7430 	ire_refrele(ire);
7431 	return (err);
7432 }
7433 
7434 /*
7435  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7436  */
7437 /* ARGSUSED */
7438 int
7439 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7440     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7441 {
7442 	ipaddr_t dst_addr;
7443 	ipaddr_t gw_addr;
7444 	ipaddr_t mask;
7445 	int error = 0;
7446 	mblk_t *mp1;
7447 	struct rtentry *rt;
7448 	ipif_t *ipif = NULL;
7449 	ip_stack_t	*ipst;
7450 
7451 	ASSERT(q->q_next == NULL);
7452 	ipst = CONNQ_TO_IPST(q);
7453 
7454 	ip1dbg(("ip_siocaddrt:"));
7455 	/* Existence of mp1 verified in ip_wput_nondata */
7456 	mp1 = mp->b_cont->b_cont;
7457 	rt = (struct rtentry *)mp1->b_rptr;
7458 
7459 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7460 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7461 
7462 	/*
7463 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7464 	 * to a particular host address.  In this case, we set the netmask to
7465 	 * all ones for the particular destination address.  Otherwise,
7466 	 * determine the netmask to be used based on dst_addr and the interfaces
7467 	 * in use.
7468 	 */
7469 	if (rt->rt_flags & RTF_HOST) {
7470 		mask = IP_HOST_MASK;
7471 	} else {
7472 		/*
7473 		 * Note that ip_subnet_mask returns a zero mask in the case of
7474 		 * default (an all-zeroes address).
7475 		 */
7476 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7477 	}
7478 
7479 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7480 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7481 	if (ipif != NULL)
7482 		ipif_refrele(ipif);
7483 	return (error);
7484 }
7485 
7486 /*
7487  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7488  */
7489 /* ARGSUSED */
7490 int
7491 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7492     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7493 {
7494 	ipaddr_t dst_addr;
7495 	ipaddr_t gw_addr;
7496 	ipaddr_t mask;
7497 	int error;
7498 	mblk_t *mp1;
7499 	struct rtentry *rt;
7500 	ipif_t *ipif = NULL;
7501 	ip_stack_t	*ipst;
7502 
7503 	ASSERT(q->q_next == NULL);
7504 	ipst = CONNQ_TO_IPST(q);
7505 
7506 	ip1dbg(("ip_siocdelrt:"));
7507 	/* Existence of mp1 verified in ip_wput_nondata */
7508 	mp1 = mp->b_cont->b_cont;
7509 	rt = (struct rtentry *)mp1->b_rptr;
7510 
7511 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7512 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7513 
7514 	/*
7515 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7516 	 * to a particular host address.  In this case, we set the netmask to
7517 	 * all ones for the particular destination address.  Otherwise,
7518 	 * determine the netmask to be used based on dst_addr and the interfaces
7519 	 * in use.
7520 	 */
7521 	if (rt->rt_flags & RTF_HOST) {
7522 		mask = IP_HOST_MASK;
7523 	} else {
7524 		/*
7525 		 * Note that ip_subnet_mask returns a zero mask in the case of
7526 		 * default (an all-zeroes address).
7527 		 */
7528 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7529 	}
7530 
7531 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7532 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7533 	    mp, ip_process_ioctl, ipst);
7534 	if (ipif != NULL)
7535 		ipif_refrele(ipif);
7536 	return (error);
7537 }
7538 
7539 /*
7540  * Enqueue the mp onto the ipsq, chained by b_next.
7541  * b_prev stores the function to be executed later, and b_queue the queue
7542  * where this mp originated.
7543  */
7544 void
7545 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7546     ill_t *pending_ill)
7547 {
7548 	conn_t	*connp = NULL;
7549 
7550 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7551 	ASSERT(func != NULL);
7552 
7553 	mp->b_queue = q;
7554 	mp->b_prev = (void *)func;
7555 	mp->b_next = NULL;
7556 
7557 	switch (type) {
7558 	case CUR_OP:
7559 		if (ipsq->ipsq_mptail != NULL) {
7560 			ASSERT(ipsq->ipsq_mphead != NULL);
7561 			ipsq->ipsq_mptail->b_next = mp;
7562 		} else {
7563 			ASSERT(ipsq->ipsq_mphead == NULL);
7564 			ipsq->ipsq_mphead = mp;
7565 		}
7566 		ipsq->ipsq_mptail = mp;
7567 		break;
7568 
7569 	case NEW_OP:
7570 		if (ipsq->ipsq_xopq_mptail != NULL) {
7571 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7572 			ipsq->ipsq_xopq_mptail->b_next = mp;
7573 		} else {
7574 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7575 			ipsq->ipsq_xopq_mphead = mp;
7576 		}
7577 		ipsq->ipsq_xopq_mptail = mp;
7578 		break;
7579 	default:
7580 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7581 	}
7582 
7583 	if (CONN_Q(q) && pending_ill != NULL) {
7584 		connp = Q_TO_CONN(q);
7585 
7586 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7587 		connp->conn_oper_pending_ill = pending_ill;
7588 	}
7589 }
7590 
7591 /*
7592  * Return the mp at the head of the ipsq. After emptying the ipsq
7593  * look at the next ioctl, if this ioctl is complete. Otherwise
7594  * return, we will resume when we complete the current ioctl.
7595  * The current ioctl will wait till it gets a response from the
7596  * driver below.
7597  */
7598 static mblk_t *
7599 ipsq_dq(ipsq_t *ipsq)
7600 {
7601 	mblk_t	*mp;
7602 
7603 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7604 
7605 	mp = ipsq->ipsq_mphead;
7606 	if (mp != NULL) {
7607 		ipsq->ipsq_mphead = mp->b_next;
7608 		if (ipsq->ipsq_mphead == NULL)
7609 			ipsq->ipsq_mptail = NULL;
7610 		mp->b_next = NULL;
7611 		return (mp);
7612 	}
7613 	if (ipsq->ipsq_current_ipif != NULL)
7614 		return (NULL);
7615 	mp = ipsq->ipsq_xopq_mphead;
7616 	if (mp != NULL) {
7617 		ipsq->ipsq_xopq_mphead = mp->b_next;
7618 		if (ipsq->ipsq_xopq_mphead == NULL)
7619 			ipsq->ipsq_xopq_mptail = NULL;
7620 		mp->b_next = NULL;
7621 		return (mp);
7622 	}
7623 	return (NULL);
7624 }
7625 
7626 /*
7627  * Enter the ipsq corresponding to ill, by waiting synchronously till
7628  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7629  * will have to drain completely before ipsq_enter returns success.
7630  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7631  * and the ipsq_exit logic will start the next enqueued ioctl after
7632  * completion of the current ioctl. If 'force' is used, we don't wait
7633  * for the enqueued ioctls. This is needed when a conn_close wants to
7634  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7635  * of an ill can also use this option. But we dont' use it currently.
7636  */
7637 #define	ENTER_SQ_WAIT_TICKS 100
7638 boolean_t
7639 ipsq_enter(ill_t *ill, boolean_t force)
7640 {
7641 	ipsq_t	*ipsq;
7642 	boolean_t waited_enough = B_FALSE;
7643 
7644 	/*
7645 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7646 	 * Since the <ill-ipsq> assocs could change while we wait for the
7647 	 * writer, it is easier to wait on a fixed global rather than try to
7648 	 * cv_wait on a changing ipsq.
7649 	 */
7650 	mutex_enter(&ill->ill_lock);
7651 	for (;;) {
7652 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7653 			mutex_exit(&ill->ill_lock);
7654 			return (B_FALSE);
7655 		}
7656 
7657 		ipsq = ill->ill_phyint->phyint_ipsq;
7658 		mutex_enter(&ipsq->ipsq_lock);
7659 		if (ipsq->ipsq_writer == NULL &&
7660 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7661 			break;
7662 		} else if (ipsq->ipsq_writer != NULL) {
7663 			mutex_exit(&ipsq->ipsq_lock);
7664 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7665 		} else {
7666 			mutex_exit(&ipsq->ipsq_lock);
7667 			if (force) {
7668 				(void) cv_timedwait(&ill->ill_cv,
7669 				    &ill->ill_lock,
7670 				    lbolt + ENTER_SQ_WAIT_TICKS);
7671 				waited_enough = B_TRUE;
7672 				continue;
7673 			} else {
7674 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7675 			}
7676 		}
7677 	}
7678 
7679 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7680 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7681 	ipsq->ipsq_writer = curthread;
7682 	ipsq->ipsq_reentry_cnt++;
7683 #ifdef DEBUG
7684 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IPSQ_STACK_DEPTH);
7685 #endif
7686 	mutex_exit(&ipsq->ipsq_lock);
7687 	mutex_exit(&ill->ill_lock);
7688 	return (B_TRUE);
7689 }
7690 
7691 /*
7692  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7693  * certain critical operations like plumbing (i.e. most set ioctls),
7694  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7695  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7696  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7697  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7698  * threads executing in the ipsq. Responses from the driver pertain to the
7699  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7700  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7701  *
7702  * If a thread does not want to reenter the ipsq when it is already writer,
7703  * it must make sure that the specified reentry point to be called later
7704  * when the ipsq is empty, nor any code path starting from the specified reentry
7705  * point must never ever try to enter the ipsq again. Otherwise it can lead
7706  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7707  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7708  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7709  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7710  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7711  * ioctl if the current ioctl has completed. If the current ioctl is still
7712  * in progress it simply returns. The current ioctl could be waiting for
7713  * a response from another module (arp_ or the driver or could be waiting for
7714  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7715  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7716  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7717  * ipsq_current_ipif is clear which happens only on ioctl completion.
7718  */
7719 
7720 /*
7721  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7722  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7723  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7724  * completion.
7725  */
7726 ipsq_t *
7727 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7728     ipsq_func_t func, int type, boolean_t reentry_ok)
7729 {
7730 	ipsq_t	*ipsq;
7731 
7732 	/* Only 1 of ipif or ill can be specified */
7733 	ASSERT((ipif != NULL) ^ (ill != NULL));
7734 	if (ipif != NULL)
7735 		ill = ipif->ipif_ill;
7736 
7737 	/*
7738 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7739 	 * ipsq of an ill can't change when ill_lock is held.
7740 	 */
7741 	GRAB_CONN_LOCK(q);
7742 	mutex_enter(&ill->ill_lock);
7743 	ipsq = ill->ill_phyint->phyint_ipsq;
7744 	mutex_enter(&ipsq->ipsq_lock);
7745 
7746 	/*
7747 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7748 	 *    (Note: If the caller does not specify reentry_ok then neither
7749 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7750 	 *    again. Otherwise it can lead to an infinite loop
7751 	 * 2. Enter the ipsq if there is no current writer and this attempted
7752 	 *    entry is part of the current ioctl or operation
7753 	 * 3. Enter the ipsq if there is no current writer and this is a new
7754 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7755 	 *    empty and there is no ioctl (or operation) currently in progress
7756 	 */
7757 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7758 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7759 	    ipsq->ipsq_current_ipif == NULL))) ||
7760 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7761 		/* Success. */
7762 		ipsq->ipsq_reentry_cnt++;
7763 		ipsq->ipsq_writer = curthread;
7764 		mutex_exit(&ipsq->ipsq_lock);
7765 		mutex_exit(&ill->ill_lock);
7766 		RELEASE_CONN_LOCK(q);
7767 #ifdef DEBUG
7768 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack,
7769 		    IPSQ_STACK_DEPTH);
7770 #endif
7771 		return (ipsq);
7772 	}
7773 
7774 	ipsq_enq(ipsq, q, mp, func, type, ill);
7775 
7776 	mutex_exit(&ipsq->ipsq_lock);
7777 	mutex_exit(&ill->ill_lock);
7778 	RELEASE_CONN_LOCK(q);
7779 	return (NULL);
7780 }
7781 
7782 /*
7783  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7784  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7785  * cannot be entered, the mp is queued for completion.
7786  */
7787 void
7788 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7789     boolean_t reentry_ok)
7790 {
7791 	ipsq_t	*ipsq;
7792 
7793 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7794 
7795 	/*
7796 	 * Drop the caller's refhold on the ill.  This is safe since we either
7797 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7798 	 * IPSQ, in which case we return without accessing ill anymore.  This
7799 	 * is needed because func needs to see the correct refcount.
7800 	 * e.g. removeif can work only then.
7801 	 */
7802 	ill_refrele(ill);
7803 	if (ipsq != NULL) {
7804 		(*func)(ipsq, q, mp, NULL);
7805 		ipsq_exit(ipsq);
7806 	}
7807 }
7808 
7809 /*
7810  * If there are more than ILL_GRP_CNT ills in a group,
7811  * we use kmem alloc'd buffers, else use the stack
7812  */
7813 #define	ILL_GRP_CNT	14
7814 /*
7815  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7816  * Called by a thread that is currently exclusive on this ipsq.
7817  */
7818 void
7819 ipsq_exit(ipsq_t *ipsq)
7820 {
7821 	queue_t	*q;
7822 	mblk_t	*mp;
7823 	ipsq_func_t	func;
7824 	int	next;
7825 	ill_t	**ill_list = NULL;
7826 	size_t	ill_list_size = 0;
7827 	int	cnt = 0;
7828 	boolean_t need_ipsq_free = B_FALSE;
7829 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7830 
7831 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7832 	mutex_enter(&ipsq->ipsq_lock);
7833 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7834 	if (ipsq->ipsq_reentry_cnt != 1) {
7835 		ipsq->ipsq_reentry_cnt--;
7836 		mutex_exit(&ipsq->ipsq_lock);
7837 		return;
7838 	}
7839 
7840 	mp = ipsq_dq(ipsq);
7841 	while (mp != NULL) {
7842 again:
7843 		mutex_exit(&ipsq->ipsq_lock);
7844 		func = (ipsq_func_t)mp->b_prev;
7845 		q = (queue_t *)mp->b_queue;
7846 		mp->b_prev = NULL;
7847 		mp->b_queue = NULL;
7848 
7849 		/*
7850 		 * If 'q' is an conn queue, it is valid, since we did a
7851 		 * a refhold on the connp, at the start of the ioctl.
7852 		 * If 'q' is an ill queue, it is valid, since close of an
7853 		 * ill will clean up the 'ipsq'.
7854 		 */
7855 		(*func)(ipsq, q, mp, NULL);
7856 
7857 		mutex_enter(&ipsq->ipsq_lock);
7858 		mp = ipsq_dq(ipsq);
7859 	}
7860 
7861 	mutex_exit(&ipsq->ipsq_lock);
7862 
7863 	/*
7864 	 * Need to grab the locks in the right order. Need to
7865 	 * atomically check (under ipsq_lock) that there are no
7866 	 * messages before relinquishing the ipsq. Also need to
7867 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7868 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7869 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7870 	 * to grab ill_g_lock as writer.
7871 	 */
7872 	rw_enter(&ipst->ips_ill_g_lock,
7873 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7874 
7875 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7876 	if (ipsq->ipsq_refs != 0) {
7877 		/* At most 2 ills v4/v6 per phyint */
7878 		cnt = ipsq->ipsq_refs << 1;
7879 		ill_list_size = cnt * sizeof (ill_t *);
7880 		/*
7881 		 * If memory allocation fails, we will do the split
7882 		 * the next time ipsq_exit is called for whatever reason.
7883 		 * As long as the ipsq_split flag is set the need to
7884 		 * split is remembered.
7885 		 */
7886 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7887 		if (ill_list != NULL)
7888 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7889 	}
7890 	mutex_enter(&ipsq->ipsq_lock);
7891 	mp = ipsq_dq(ipsq);
7892 	if (mp != NULL) {
7893 		/* oops, some message has landed up, we can't get out */
7894 		if (ill_list != NULL)
7895 			ill_unlock_ills(ill_list, cnt);
7896 		rw_exit(&ipst->ips_ill_g_lock);
7897 		if (ill_list != NULL)
7898 			kmem_free(ill_list, ill_list_size);
7899 		ill_list = NULL;
7900 		ill_list_size = 0;
7901 		cnt = 0;
7902 		goto again;
7903 	}
7904 
7905 	/*
7906 	 * Split only if no ioctl is pending and if memory alloc succeeded
7907 	 * above.
7908 	 */
7909 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7910 	    ill_list != NULL) {
7911 		/*
7912 		 * No new ill can join this ipsq since we are holding the
7913 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7914 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7915 		 * If so we will retry on the next ipsq_exit.
7916 		 */
7917 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7918 	}
7919 
7920 	/*
7921 	 * We are holding the ipsq lock, hence no new messages can
7922 	 * land up on the ipsq, and there are no messages currently.
7923 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7924 	 * atomically while holding ill locks.
7925 	 */
7926 	ipsq->ipsq_writer = NULL;
7927 	ipsq->ipsq_reentry_cnt--;
7928 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7929 #ifdef DEBUG
7930 	ipsq->ipsq_depth = 0;
7931 #endif
7932 	mutex_exit(&ipsq->ipsq_lock);
7933 	/*
7934 	 * For IPMP this should wake up all ills in this ipsq.
7935 	 * We need to hold the ill_lock while waking up waiters to
7936 	 * avoid missed wakeups. But there is no need to acquire all
7937 	 * the ill locks and then wakeup. If we have not acquired all
7938 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7939 	 * wakes up ills one at a time after getting the right ill_lock
7940 	 */
7941 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7942 	if (ill_list != NULL)
7943 		ill_unlock_ills(ill_list, cnt);
7944 	if (ipsq->ipsq_refs == 0)
7945 		need_ipsq_free = B_TRUE;
7946 	rw_exit(&ipst->ips_ill_g_lock);
7947 	if (ill_list != 0)
7948 		kmem_free(ill_list, ill_list_size);
7949 
7950 	if (need_ipsq_free) {
7951 		/*
7952 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7953 		 * looked up. ipsq can be looked up only thru ill or phyint
7954 		 * and there are no ills/phyint on this ipsq.
7955 		 */
7956 		ipsq_delete(ipsq);
7957 	}
7958 
7959 	/*
7960 	 * Now that we're outside the IPSQ, start any IGMP/MLD timers.  We
7961 	 * can't start these inside the IPSQ since e.g. igmp_start_timers() ->
7962 	 * untimeout() (inside the IPSQ, waiting for an executing timeout to
7963 	 * finish) could deadlock with igmp_timeout_handler() -> ipsq_enter()
7964 	 * (executing the timeout, waiting to get inside the IPSQ).
7965 	 *
7966 	 * However, there is one exception to the above: if this thread *is*
7967 	 * the IGMP/MLD timeout handler thread, then we must not start its
7968 	 * timer until the current handler is done.
7969 	 */
7970 	mutex_enter(&ipst->ips_igmp_timer_lock);
7971 	if (curthread != ipst->ips_igmp_timer_thread) {
7972 		next = ipst->ips_igmp_deferred_next;
7973 		ipst->ips_igmp_deferred_next = INFINITY;
7974 		mutex_exit(&ipst->ips_igmp_timer_lock);
7975 
7976 		if (next != INFINITY)
7977 			igmp_start_timers(next, ipst);
7978 	} else {
7979 		mutex_exit(&ipst->ips_igmp_timer_lock);
7980 	}
7981 
7982 	mutex_enter(&ipst->ips_mld_timer_lock);
7983 	if (curthread != ipst->ips_mld_timer_thread) {
7984 		next = ipst->ips_mld_deferred_next;
7985 		ipst->ips_mld_deferred_next = INFINITY;
7986 		mutex_exit(&ipst->ips_mld_timer_lock);
7987 
7988 		if (next != INFINITY)
7989 			mld_start_timers(next, ipst);
7990 	} else {
7991 		mutex_exit(&ipst->ips_mld_timer_lock);
7992 	}
7993 }
7994 
7995 /*
7996  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7997  * and `ioccmd'.
7998  */
7999 void
8000 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8001 {
8002 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8003 
8004 	mutex_enter(&ipsq->ipsq_lock);
8005 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8006 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8007 	ipsq->ipsq_current_done = B_FALSE;
8008 	ipsq->ipsq_current_ipif = ipif;
8009 	ipsq->ipsq_current_ioctl = ioccmd;
8010 	mutex_exit(&ipsq->ipsq_lock);
8011 }
8012 
8013 /*
8014  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
8015  * the next exclusive operation to begin once we ipsq_exit().  However, if
8016  * pending DLPI operations remain, then we will wait for the queue to drain
8017  * before allowing the next exclusive operation to begin.  This ensures that
8018  * DLPI operations from one exclusive operation are never improperly processed
8019  * as part of a subsequent exclusive operation.
8020  */
8021 void
8022 ipsq_current_finish(ipsq_t *ipsq)
8023 {
8024 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8025 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
8026 
8027 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8028 
8029 	/*
8030 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8031 	 * (but in that case, IPIF_CHANGING will already be clear and no
8032 	 * pending DLPI messages can remain).
8033 	 */
8034 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8035 		ill_t *ill = ipif->ipif_ill;
8036 
8037 		mutex_enter(&ill->ill_lock);
8038 		dlpi_pending = ill->ill_dlpi_pending;
8039 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8040 		/* Send any queued event */
8041 		ill_nic_info_dispatch(ill);
8042 		mutex_exit(&ill->ill_lock);
8043 	}
8044 
8045 	mutex_enter(&ipsq->ipsq_lock);
8046 	ipsq->ipsq_current_ioctl = 0;
8047 	ipsq->ipsq_current_done = B_TRUE;
8048 	if (dlpi_pending == DL_PRIM_INVAL)
8049 		ipsq->ipsq_current_ipif = NULL;
8050 	mutex_exit(&ipsq->ipsq_lock);
8051 }
8052 
8053 /*
8054  * The ill is closing. Flush all messages on the ipsq that originated
8055  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8056  * for this ill since ipsq_enter could not have entered until then.
8057  * New messages can't be queued since the CONDEMNED flag is set.
8058  */
8059 static void
8060 ipsq_flush(ill_t *ill)
8061 {
8062 	queue_t	*q;
8063 	mblk_t	*prev;
8064 	mblk_t	*mp;
8065 	mblk_t	*mp_next;
8066 	ipsq_t	*ipsq;
8067 
8068 	ASSERT(IAM_WRITER_ILL(ill));
8069 	ipsq = ill->ill_phyint->phyint_ipsq;
8070 	/*
8071 	 * Flush any messages sent up by the driver.
8072 	 */
8073 	mutex_enter(&ipsq->ipsq_lock);
8074 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8075 		mp_next = mp->b_next;
8076 		q = mp->b_queue;
8077 		if (q == ill->ill_rq || q == ill->ill_wq) {
8078 			/* Remove the mp from the ipsq */
8079 			if (prev == NULL)
8080 				ipsq->ipsq_mphead = mp->b_next;
8081 			else
8082 				prev->b_next = mp->b_next;
8083 			if (ipsq->ipsq_mptail == mp) {
8084 				ASSERT(mp_next == NULL);
8085 				ipsq->ipsq_mptail = prev;
8086 			}
8087 			inet_freemsg(mp);
8088 		} else {
8089 			prev = mp;
8090 		}
8091 	}
8092 	mutex_exit(&ipsq->ipsq_lock);
8093 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8094 	ipsq_xopq_mp_cleanup(ill, NULL);
8095 	ill_pending_mp_cleanup(ill);
8096 }
8097 
8098 /* ARGSUSED */
8099 int
8100 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8101     ip_ioctl_cmd_t *ipip, void *ifreq)
8102 {
8103 	ill_t	*ill;
8104 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8105 	boolean_t isv6;
8106 	conn_t	*connp;
8107 	ip_stack_t	*ipst;
8108 
8109 	connp = Q_TO_CONN(q);
8110 	ipst = connp->conn_netstack->netstack_ip;
8111 	isv6 = connp->conn_af_isv6;
8112 	/*
8113 	 * Set original index.
8114 	 * Failover and failback move logical interfaces
8115 	 * from one physical interface to another.  The
8116 	 * original index indicates the parent of a logical
8117 	 * interface, in other words, the physical interface
8118 	 * the logical interface will be moved back to on
8119 	 * failback.
8120 	 */
8121 
8122 	/*
8123 	 * Don't allow the original index to be changed
8124 	 * for non-failover addresses, autoconfigured
8125 	 * addresses, or IPv6 link local addresses.
8126 	 */
8127 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8128 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8129 		return (EINVAL);
8130 	}
8131 	/*
8132 	 * The new original index must be in use by some
8133 	 * physical interface.
8134 	 */
8135 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8136 	    NULL, NULL, ipst);
8137 	if (ill == NULL)
8138 		return (ENXIO);
8139 	ill_refrele(ill);
8140 
8141 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8142 	/*
8143 	 * When this ipif gets failed back, don't
8144 	 * preserve the original id, as it is no
8145 	 * longer applicable.
8146 	 */
8147 	ipif->ipif_orig_ipifid = 0;
8148 	/*
8149 	 * For IPv4, change the original index of any
8150 	 * multicast addresses associated with the
8151 	 * ipif to the new value.
8152 	 */
8153 	if (!isv6) {
8154 		ilm_t *ilm;
8155 
8156 		mutex_enter(&ipif->ipif_ill->ill_lock);
8157 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8158 		    ilm = ilm->ilm_next) {
8159 			if (ilm->ilm_ipif == ipif) {
8160 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8161 			}
8162 		}
8163 		mutex_exit(&ipif->ipif_ill->ill_lock);
8164 	}
8165 	return (0);
8166 }
8167 
8168 /* ARGSUSED */
8169 int
8170 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8171     ip_ioctl_cmd_t *ipip, void *ifreq)
8172 {
8173 	struct lifreq *lifr = (struct lifreq *)ifreq;
8174 
8175 	/*
8176 	 * Get the original interface index i.e the one
8177 	 * before FAILOVER if it ever happened.
8178 	 */
8179 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8180 	return (0);
8181 }
8182 
8183 /*
8184  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8185  * refhold and return the associated ipif
8186  */
8187 /* ARGSUSED */
8188 int
8189 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8190     cmd_info_t *ci, ipsq_func_t func)
8191 {
8192 	boolean_t exists;
8193 	struct iftun_req *ta;
8194 	ipif_t	*ipif;
8195 	ill_t	*ill;
8196 	boolean_t isv6;
8197 	mblk_t	*mp1;
8198 	int	error;
8199 	conn_t	*connp;
8200 	ip_stack_t	*ipst;
8201 
8202 	/* Existence verified in ip_wput_nondata */
8203 	mp1 = mp->b_cont->b_cont;
8204 	ta = (struct iftun_req *)mp1->b_rptr;
8205 	/*
8206 	 * Null terminate the string to protect against buffer
8207 	 * overrun. String was generated by user code and may not
8208 	 * be trusted.
8209 	 */
8210 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8211 
8212 	connp = Q_TO_CONN(q);
8213 	isv6 = connp->conn_af_isv6;
8214 	ipst = connp->conn_netstack->netstack_ip;
8215 
8216 	/* Disallows implicit create */
8217 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8218 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8219 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8220 	if (ipif == NULL)
8221 		return (error);
8222 
8223 	if (ipif->ipif_id != 0) {
8224 		/*
8225 		 * We really don't want to set/get tunnel parameters
8226 		 * on virtual tunnel interfaces.  Only allow the
8227 		 * base tunnel to do these.
8228 		 */
8229 		ipif_refrele(ipif);
8230 		return (EINVAL);
8231 	}
8232 
8233 	/*
8234 	 * Send down to tunnel mod for ioctl processing.
8235 	 * Will finish ioctl in ip_rput_other().
8236 	 */
8237 	ill = ipif->ipif_ill;
8238 	if (ill->ill_net_type == IRE_LOOPBACK) {
8239 		ipif_refrele(ipif);
8240 		return (EOPNOTSUPP);
8241 	}
8242 
8243 	if (ill->ill_wq == NULL) {
8244 		ipif_refrele(ipif);
8245 		return (ENXIO);
8246 	}
8247 	/*
8248 	 * Mark the ioctl as coming from an IPv6 interface for
8249 	 * tun's convenience.
8250 	 */
8251 	if (ill->ill_isv6)
8252 		ta->ifta_flags |= 0x80000000;
8253 	ci->ci_ipif = ipif;
8254 	return (0);
8255 }
8256 
8257 /*
8258  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8259  * and return the associated ipif.
8260  * Return value:
8261  *	Non zero: An error has occurred. ci may not be filled out.
8262  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8263  *	a held ipif in ci.ci_ipif.
8264  */
8265 int
8266 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8267     cmd_info_t *ci, ipsq_func_t func)
8268 {
8269 	sin_t		*sin;
8270 	sin6_t		*sin6;
8271 	char		*name;
8272 	struct ifreq    *ifr;
8273 	struct lifreq    *lifr;
8274 	ipif_t		*ipif = NULL;
8275 	ill_t		*ill;
8276 	conn_t		*connp;
8277 	boolean_t	isv6;
8278 	boolean_t	exists;
8279 	int		err;
8280 	mblk_t		*mp1;
8281 	zoneid_t	zoneid;
8282 	ip_stack_t	*ipst;
8283 
8284 	if (q->q_next != NULL) {
8285 		ill = (ill_t *)q->q_ptr;
8286 		isv6 = ill->ill_isv6;
8287 		connp = NULL;
8288 		zoneid = ALL_ZONES;
8289 		ipst = ill->ill_ipst;
8290 	} else {
8291 		ill = NULL;
8292 		connp = Q_TO_CONN(q);
8293 		isv6 = connp->conn_af_isv6;
8294 		zoneid = connp->conn_zoneid;
8295 		if (zoneid == GLOBAL_ZONEID) {
8296 			/* global zone can access ipifs in all zones */
8297 			zoneid = ALL_ZONES;
8298 		}
8299 		ipst = connp->conn_netstack->netstack_ip;
8300 	}
8301 
8302 	/* Has been checked in ip_wput_nondata */
8303 	mp1 = mp->b_cont->b_cont;
8304 
8305 	if (ipip->ipi_cmd_type == IF_CMD) {
8306 		/* This a old style SIOC[GS]IF* command */
8307 		ifr = (struct ifreq *)mp1->b_rptr;
8308 		/*
8309 		 * Null terminate the string to protect against buffer
8310 		 * overrun. String was generated by user code and may not
8311 		 * be trusted.
8312 		 */
8313 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8314 		sin = (sin_t *)&ifr->ifr_addr;
8315 		name = ifr->ifr_name;
8316 		ci->ci_sin = sin;
8317 		ci->ci_sin6 = NULL;
8318 		ci->ci_lifr = (struct lifreq *)ifr;
8319 	} else {
8320 		/* This a new style SIOC[GS]LIF* command */
8321 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8322 		lifr = (struct lifreq *)mp1->b_rptr;
8323 		/*
8324 		 * Null terminate the string to protect against buffer
8325 		 * overrun. String was generated by user code and may not
8326 		 * be trusted.
8327 		 */
8328 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8329 		name = lifr->lifr_name;
8330 		sin = (sin_t *)&lifr->lifr_addr;
8331 		sin6 = (sin6_t *)&lifr->lifr_addr;
8332 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8333 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8334 			    LIFNAMSIZ);
8335 		}
8336 		ci->ci_sin = sin;
8337 		ci->ci_sin6 = sin6;
8338 		ci->ci_lifr = lifr;
8339 	}
8340 
8341 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8342 		/*
8343 		 * The ioctl will be failed if the ioctl comes down
8344 		 * an conn stream
8345 		 */
8346 		if (ill == NULL) {
8347 			/*
8348 			 * Not an ill queue, return EINVAL same as the
8349 			 * old error code.
8350 			 */
8351 			return (ENXIO);
8352 		}
8353 		ipif = ill->ill_ipif;
8354 		ipif_refhold(ipif);
8355 	} else {
8356 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8357 		    &exists, isv6, zoneid,
8358 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8359 		    ipst);
8360 		if (ipif == NULL) {
8361 			if (err == EINPROGRESS)
8362 				return (err);
8363 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8364 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8365 				/*
8366 				 * Need to try both v4 and v6 since this
8367 				 * ioctl can come down either v4 or v6
8368 				 * socket. The lifreq.lifr_family passed
8369 				 * down by this ioctl is AF_UNSPEC.
8370 				 */
8371 				ipif = ipif_lookup_on_name(name,
8372 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8373 				    zoneid, (connp == NULL) ? q :
8374 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8375 				if (err == EINPROGRESS)
8376 					return (err);
8377 			}
8378 			err = 0;	/* Ensure we don't use it below */
8379 		}
8380 	}
8381 
8382 	/*
8383 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8384 	 */
8385 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8386 		ipif_refrele(ipif);
8387 		return (ENXIO);
8388 	}
8389 
8390 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8391 	    name[0] == '\0') {
8392 		/*
8393 		 * Handle a or a SIOC?IF* with a null name
8394 		 * during plumb (on the ill queue before the I_PLINK).
8395 		 */
8396 		ipif = ill->ill_ipif;
8397 		ipif_refhold(ipif);
8398 	}
8399 
8400 	if (ipif == NULL)
8401 		return (ENXIO);
8402 
8403 	/*
8404 	 * Allow only GET operations if this ipif has been created
8405 	 * temporarily due to a MOVE operation.
8406 	 */
8407 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8408 		ipif_refrele(ipif);
8409 		return (EINVAL);
8410 	}
8411 
8412 	ci->ci_ipif = ipif;
8413 	return (0);
8414 }
8415 
8416 /*
8417  * Return the total number of ipifs.
8418  */
8419 static uint_t
8420 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8421 {
8422 	uint_t numifs = 0;
8423 	ill_t	*ill;
8424 	ill_walk_context_t	ctx;
8425 	ipif_t	*ipif;
8426 
8427 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8428 	ill = ILL_START_WALK_V4(&ctx, ipst);
8429 
8430 	while (ill != NULL) {
8431 		for (ipif = ill->ill_ipif; ipif != NULL;
8432 		    ipif = ipif->ipif_next) {
8433 			if (ipif->ipif_zoneid == zoneid ||
8434 			    ipif->ipif_zoneid == ALL_ZONES)
8435 				numifs++;
8436 		}
8437 		ill = ill_next(&ctx, ill);
8438 	}
8439 	rw_exit(&ipst->ips_ill_g_lock);
8440 	return (numifs);
8441 }
8442 
8443 /*
8444  * Return the total number of ipifs.
8445  */
8446 static uint_t
8447 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8448 {
8449 	uint_t numifs = 0;
8450 	ill_t	*ill;
8451 	ipif_t	*ipif;
8452 	ill_walk_context_t	ctx;
8453 
8454 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8455 
8456 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8457 	if (family == AF_INET)
8458 		ill = ILL_START_WALK_V4(&ctx, ipst);
8459 	else if (family == AF_INET6)
8460 		ill = ILL_START_WALK_V6(&ctx, ipst);
8461 	else
8462 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8463 
8464 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8465 		for (ipif = ill->ill_ipif; ipif != NULL;
8466 		    ipif = ipif->ipif_next) {
8467 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8468 			    !(lifn_flags & LIFC_NOXMIT))
8469 				continue;
8470 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8471 			    !(lifn_flags & LIFC_TEMPORARY))
8472 				continue;
8473 			if (((ipif->ipif_flags &
8474 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8475 			    IPIF_DEPRECATED)) ||
8476 			    IS_LOOPBACK(ill) ||
8477 			    !(ipif->ipif_flags & IPIF_UP)) &&
8478 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8479 				continue;
8480 
8481 			if (zoneid != ipif->ipif_zoneid &&
8482 			    ipif->ipif_zoneid != ALL_ZONES &&
8483 			    (zoneid != GLOBAL_ZONEID ||
8484 			    !(lifn_flags & LIFC_ALLZONES)))
8485 				continue;
8486 
8487 			numifs++;
8488 		}
8489 	}
8490 	rw_exit(&ipst->ips_ill_g_lock);
8491 	return (numifs);
8492 }
8493 
8494 uint_t
8495 ip_get_lifsrcofnum(ill_t *ill)
8496 {
8497 	uint_t numifs = 0;
8498 	ill_t	*ill_head = ill;
8499 	ip_stack_t	*ipst = ill->ill_ipst;
8500 
8501 	/*
8502 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8503 	 * other thread may be trying to relink the ILLs in this usesrc group
8504 	 * and adjusting the ill_usesrc_grp_next pointers
8505 	 */
8506 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8507 	if ((ill->ill_usesrc_ifindex == 0) &&
8508 	    (ill->ill_usesrc_grp_next != NULL)) {
8509 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8510 		    ill = ill->ill_usesrc_grp_next)
8511 			numifs++;
8512 	}
8513 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8514 
8515 	return (numifs);
8516 }
8517 
8518 /* Null values are passed in for ipif, sin, and ifreq */
8519 /* ARGSUSED */
8520 int
8521 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8522     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8523 {
8524 	int *nump;
8525 	conn_t *connp = Q_TO_CONN(q);
8526 
8527 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8528 
8529 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8530 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8531 
8532 	*nump = ip_get_numifs(connp->conn_zoneid,
8533 	    connp->conn_netstack->netstack_ip);
8534 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8535 	return (0);
8536 }
8537 
8538 /* Null values are passed in for ipif, sin, and ifreq */
8539 /* ARGSUSED */
8540 int
8541 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8542     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8543 {
8544 	struct lifnum *lifn;
8545 	mblk_t	*mp1;
8546 	conn_t *connp = Q_TO_CONN(q);
8547 
8548 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8549 
8550 	/* Existence checked in ip_wput_nondata */
8551 	mp1 = mp->b_cont->b_cont;
8552 
8553 	lifn = (struct lifnum *)mp1->b_rptr;
8554 	switch (lifn->lifn_family) {
8555 	case AF_UNSPEC:
8556 	case AF_INET:
8557 	case AF_INET6:
8558 		break;
8559 	default:
8560 		return (EAFNOSUPPORT);
8561 	}
8562 
8563 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8564 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8565 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8566 	return (0);
8567 }
8568 
8569 /* ARGSUSED */
8570 int
8571 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8572     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8573 {
8574 	STRUCT_HANDLE(ifconf, ifc);
8575 	mblk_t *mp1;
8576 	struct iocblk *iocp;
8577 	struct ifreq *ifr;
8578 	ill_walk_context_t	ctx;
8579 	ill_t	*ill;
8580 	ipif_t	*ipif;
8581 	struct sockaddr_in *sin;
8582 	int32_t	ifclen;
8583 	zoneid_t zoneid;
8584 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8585 
8586 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8587 
8588 	ip1dbg(("ip_sioctl_get_ifconf"));
8589 	/* Existence verified in ip_wput_nondata */
8590 	mp1 = mp->b_cont->b_cont;
8591 	iocp = (struct iocblk *)mp->b_rptr;
8592 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8593 
8594 	/*
8595 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8596 	 * the user buffer address and length into which the list of struct
8597 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8598 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8599 	 * the SIOCGIFCONF operation was redefined to simply provide
8600 	 * a large output buffer into which we are supposed to jam the ifreq
8601 	 * array.  The same ioctl command code was used, despite the fact that
8602 	 * both the applications and the kernel code had to change, thus making
8603 	 * it impossible to support both interfaces.
8604 	 *
8605 	 * For reasons not good enough to try to explain, the following
8606 	 * algorithm is used for deciding what to do with one of these:
8607 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8608 	 * form with the output buffer coming down as the continuation message.
8609 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8610 	 * and we have to copy in the ifconf structure to find out how big the
8611 	 * output buffer is and where to copy out to.  Sure no problem...
8612 	 *
8613 	 */
8614 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8615 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8616 		int numifs = 0;
8617 		size_t ifc_bufsize;
8618 
8619 		/*
8620 		 * Must be (better be!) continuation of a TRANSPARENT
8621 		 * IOCTL.  We just copied in the ifconf structure.
8622 		 */
8623 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8624 		    (struct ifconf *)mp1->b_rptr);
8625 
8626 		/*
8627 		 * Allocate a buffer to hold requested information.
8628 		 *
8629 		 * If ifc_len is larger than what is needed, we only
8630 		 * allocate what we will use.
8631 		 *
8632 		 * If ifc_len is smaller than what is needed, return
8633 		 * EINVAL.
8634 		 *
8635 		 * XXX: the ill_t structure can hava 2 counters, for
8636 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8637 		 * number of interfaces for a device, so we don't need
8638 		 * to count them here...
8639 		 */
8640 		numifs = ip_get_numifs(zoneid, ipst);
8641 
8642 		ifclen = STRUCT_FGET(ifc, ifc_len);
8643 		ifc_bufsize = numifs * sizeof (struct ifreq);
8644 		if (ifc_bufsize > ifclen) {
8645 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8646 				/* old behaviour */
8647 				return (EINVAL);
8648 			} else {
8649 				ifc_bufsize = ifclen;
8650 			}
8651 		}
8652 
8653 		mp1 = mi_copyout_alloc(q, mp,
8654 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8655 		if (mp1 == NULL)
8656 			return (ENOMEM);
8657 
8658 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8659 	}
8660 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8661 	/*
8662 	 * the SIOCGIFCONF ioctl only knows about
8663 	 * IPv4 addresses, so don't try to tell
8664 	 * it about interfaces with IPv6-only
8665 	 * addresses. (Last parm 'isv6' is B_FALSE)
8666 	 */
8667 
8668 	ifr = (struct ifreq *)mp1->b_rptr;
8669 
8670 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8671 	ill = ILL_START_WALK_V4(&ctx, ipst);
8672 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8673 		for (ipif = ill->ill_ipif; ipif != NULL;
8674 		    ipif = ipif->ipif_next) {
8675 			if (zoneid != ipif->ipif_zoneid &&
8676 			    ipif->ipif_zoneid != ALL_ZONES)
8677 				continue;
8678 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8679 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8680 					/* old behaviour */
8681 					rw_exit(&ipst->ips_ill_g_lock);
8682 					return (EINVAL);
8683 				} else {
8684 					goto if_copydone;
8685 				}
8686 			}
8687 			ipif_get_name(ipif, ifr->ifr_name,
8688 			    sizeof (ifr->ifr_name));
8689 			sin = (sin_t *)&ifr->ifr_addr;
8690 			*sin = sin_null;
8691 			sin->sin_family = AF_INET;
8692 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8693 			ifr++;
8694 		}
8695 	}
8696 if_copydone:
8697 	rw_exit(&ipst->ips_ill_g_lock);
8698 	mp1->b_wptr = (uchar_t *)ifr;
8699 
8700 	if (STRUCT_BUF(ifc) != NULL) {
8701 		STRUCT_FSET(ifc, ifc_len,
8702 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8703 	}
8704 	return (0);
8705 }
8706 
8707 /*
8708  * Get the interfaces using the address hosted on the interface passed in,
8709  * as a source adddress
8710  */
8711 /* ARGSUSED */
8712 int
8713 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8714     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8715 {
8716 	mblk_t *mp1;
8717 	ill_t	*ill, *ill_head;
8718 	ipif_t	*ipif, *orig_ipif;
8719 	int	numlifs = 0;
8720 	size_t	lifs_bufsize, lifsmaxlen;
8721 	struct	lifreq *lifr;
8722 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8723 	uint_t	ifindex;
8724 	zoneid_t zoneid;
8725 	int err = 0;
8726 	boolean_t isv6 = B_FALSE;
8727 	struct	sockaddr_in	*sin;
8728 	struct	sockaddr_in6	*sin6;
8729 	STRUCT_HANDLE(lifsrcof, lifs);
8730 	ip_stack_t		*ipst;
8731 
8732 	ipst = CONNQ_TO_IPST(q);
8733 
8734 	ASSERT(q->q_next == NULL);
8735 
8736 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8737 
8738 	/* Existence verified in ip_wput_nondata */
8739 	mp1 = mp->b_cont->b_cont;
8740 
8741 	/*
8742 	 * Must be (better be!) continuation of a TRANSPARENT
8743 	 * IOCTL.  We just copied in the lifsrcof structure.
8744 	 */
8745 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8746 	    (struct lifsrcof *)mp1->b_rptr);
8747 
8748 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8749 		return (EINVAL);
8750 
8751 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8752 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8753 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8754 	    ip_process_ioctl, &err, ipst);
8755 	if (ipif == NULL) {
8756 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8757 		    ifindex));
8758 		return (err);
8759 	}
8760 
8761 	/* Allocate a buffer to hold requested information */
8762 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8763 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8764 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8765 	/* The actual size needed is always returned in lifs_len */
8766 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8767 
8768 	/* If the amount we need is more than what is passed in, abort */
8769 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8770 		ipif_refrele(ipif);
8771 		return (0);
8772 	}
8773 
8774 	mp1 = mi_copyout_alloc(q, mp,
8775 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8776 	if (mp1 == NULL) {
8777 		ipif_refrele(ipif);
8778 		return (ENOMEM);
8779 	}
8780 
8781 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8782 	bzero(mp1->b_rptr, lifs_bufsize);
8783 
8784 	lifr = (struct lifreq *)mp1->b_rptr;
8785 
8786 	ill = ill_head = ipif->ipif_ill;
8787 	orig_ipif = ipif;
8788 
8789 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8790 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8791 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8792 
8793 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8794 	for (; (ill != NULL) && (ill != ill_head);
8795 	    ill = ill->ill_usesrc_grp_next) {
8796 
8797 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8798 			break;
8799 
8800 		ipif = ill->ill_ipif;
8801 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8802 		if (ipif->ipif_isv6) {
8803 			sin6 = (sin6_t *)&lifr->lifr_addr;
8804 			*sin6 = sin6_null;
8805 			sin6->sin6_family = AF_INET6;
8806 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8807 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8808 			    &ipif->ipif_v6net_mask);
8809 		} else {
8810 			sin = (sin_t *)&lifr->lifr_addr;
8811 			*sin = sin_null;
8812 			sin->sin_family = AF_INET;
8813 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8814 			lifr->lifr_addrlen = ip_mask_to_plen(
8815 			    ipif->ipif_net_mask);
8816 		}
8817 		lifr++;
8818 	}
8819 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8820 	rw_exit(&ipst->ips_ill_g_lock);
8821 	ipif_refrele(orig_ipif);
8822 	mp1->b_wptr = (uchar_t *)lifr;
8823 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8824 
8825 	return (0);
8826 }
8827 
8828 /* ARGSUSED */
8829 int
8830 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8831     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8832 {
8833 	mblk_t *mp1;
8834 	int	list;
8835 	ill_t	*ill;
8836 	ipif_t	*ipif;
8837 	int	flags;
8838 	int	numlifs = 0;
8839 	size_t	lifc_bufsize;
8840 	struct	lifreq *lifr;
8841 	sa_family_t	family;
8842 	struct	sockaddr_in	*sin;
8843 	struct	sockaddr_in6	*sin6;
8844 	ill_walk_context_t	ctx;
8845 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8846 	int32_t	lifclen;
8847 	zoneid_t zoneid;
8848 	STRUCT_HANDLE(lifconf, lifc);
8849 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8850 
8851 	ip1dbg(("ip_sioctl_get_lifconf"));
8852 
8853 	ASSERT(q->q_next == NULL);
8854 
8855 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8856 
8857 	/* Existence verified in ip_wput_nondata */
8858 	mp1 = mp->b_cont->b_cont;
8859 
8860 	/*
8861 	 * An extended version of SIOCGIFCONF that takes an
8862 	 * additional address family and flags field.
8863 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8864 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8865 	 * interfaces are omitted.
8866 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8867 	 * unless LIFC_TEMPORARY is specified.
8868 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8869 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8870 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8871 	 * has priority over LIFC_NOXMIT.
8872 	 */
8873 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8874 
8875 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8876 		return (EINVAL);
8877 
8878 	/*
8879 	 * Must be (better be!) continuation of a TRANSPARENT
8880 	 * IOCTL.  We just copied in the lifconf structure.
8881 	 */
8882 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8883 
8884 	family = STRUCT_FGET(lifc, lifc_family);
8885 	flags = STRUCT_FGET(lifc, lifc_flags);
8886 
8887 	switch (family) {
8888 	case AF_UNSPEC:
8889 		/*
8890 		 * walk all ILL's.
8891 		 */
8892 		list = MAX_G_HEADS;
8893 		break;
8894 	case AF_INET:
8895 		/*
8896 		 * walk only IPV4 ILL's.
8897 		 */
8898 		list = IP_V4_G_HEAD;
8899 		break;
8900 	case AF_INET6:
8901 		/*
8902 		 * walk only IPV6 ILL's.
8903 		 */
8904 		list = IP_V6_G_HEAD;
8905 		break;
8906 	default:
8907 		return (EAFNOSUPPORT);
8908 	}
8909 
8910 	/*
8911 	 * Allocate a buffer to hold requested information.
8912 	 *
8913 	 * If lifc_len is larger than what is needed, we only
8914 	 * allocate what we will use.
8915 	 *
8916 	 * If lifc_len is smaller than what is needed, return
8917 	 * EINVAL.
8918 	 */
8919 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8920 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8921 	lifclen = STRUCT_FGET(lifc, lifc_len);
8922 	if (lifc_bufsize > lifclen) {
8923 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8924 			return (EINVAL);
8925 		else
8926 			lifc_bufsize = lifclen;
8927 	}
8928 
8929 	mp1 = mi_copyout_alloc(q, mp,
8930 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8931 	if (mp1 == NULL)
8932 		return (ENOMEM);
8933 
8934 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8935 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8936 
8937 	lifr = (struct lifreq *)mp1->b_rptr;
8938 
8939 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8940 	ill = ill_first(list, list, &ctx, ipst);
8941 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8942 		for (ipif = ill->ill_ipif; ipif != NULL;
8943 		    ipif = ipif->ipif_next) {
8944 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8945 			    !(flags & LIFC_NOXMIT))
8946 				continue;
8947 
8948 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8949 			    !(flags & LIFC_TEMPORARY))
8950 				continue;
8951 
8952 			if (((ipif->ipif_flags &
8953 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8954 			    IPIF_DEPRECATED)) ||
8955 			    IS_LOOPBACK(ill) ||
8956 			    !(ipif->ipif_flags & IPIF_UP)) &&
8957 			    (flags & LIFC_EXTERNAL_SOURCE))
8958 				continue;
8959 
8960 			if (zoneid != ipif->ipif_zoneid &&
8961 			    ipif->ipif_zoneid != ALL_ZONES &&
8962 			    (zoneid != GLOBAL_ZONEID ||
8963 			    !(flags & LIFC_ALLZONES)))
8964 				continue;
8965 
8966 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8967 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8968 					rw_exit(&ipst->ips_ill_g_lock);
8969 					return (EINVAL);
8970 				} else {
8971 					goto lif_copydone;
8972 				}
8973 			}
8974 
8975 			ipif_get_name(ipif, lifr->lifr_name,
8976 			    sizeof (lifr->lifr_name));
8977 			if (ipif->ipif_isv6) {
8978 				sin6 = (sin6_t *)&lifr->lifr_addr;
8979 				*sin6 = sin6_null;
8980 				sin6->sin6_family = AF_INET6;
8981 				sin6->sin6_addr =
8982 				    ipif->ipif_v6lcl_addr;
8983 				lifr->lifr_addrlen =
8984 				    ip_mask_to_plen_v6(
8985 				    &ipif->ipif_v6net_mask);
8986 			} else {
8987 				sin = (sin_t *)&lifr->lifr_addr;
8988 				*sin = sin_null;
8989 				sin->sin_family = AF_INET;
8990 				sin->sin_addr.s_addr =
8991 				    ipif->ipif_lcl_addr;
8992 				lifr->lifr_addrlen =
8993 				    ip_mask_to_plen(
8994 				    ipif->ipif_net_mask);
8995 			}
8996 			lifr++;
8997 		}
8998 	}
8999 lif_copydone:
9000 	rw_exit(&ipst->ips_ill_g_lock);
9001 
9002 	mp1->b_wptr = (uchar_t *)lifr;
9003 	if (STRUCT_BUF(lifc) != NULL) {
9004 		STRUCT_FSET(lifc, lifc_len,
9005 		    (int)((uchar_t *)lifr - mp1->b_rptr));
9006 	}
9007 	return (0);
9008 }
9009 
9010 /* ARGSUSED */
9011 int
9012 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9013     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9014 {
9015 	ip_stack_t	*ipst;
9016 
9017 	if (q->q_next == NULL)
9018 		ipst = CONNQ_TO_IPST(q);
9019 	else
9020 		ipst = ILLQ_TO_IPST(q);
9021 
9022 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9023 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9024 	return (0);
9025 }
9026 
9027 static void
9028 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9029 {
9030 	ip6_asp_t *table;
9031 	size_t table_size;
9032 	mblk_t *data_mp;
9033 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9034 	ip_stack_t	*ipst;
9035 
9036 	if (q->q_next == NULL)
9037 		ipst = CONNQ_TO_IPST(q);
9038 	else
9039 		ipst = ILLQ_TO_IPST(q);
9040 
9041 	/* These two ioctls are I_STR only */
9042 	if (iocp->ioc_count == TRANSPARENT) {
9043 		miocnak(q, mp, 0, EINVAL);
9044 		return;
9045 	}
9046 
9047 	data_mp = mp->b_cont;
9048 	if (data_mp == NULL) {
9049 		/* The user passed us a NULL argument */
9050 		table = NULL;
9051 		table_size = iocp->ioc_count;
9052 	} else {
9053 		/*
9054 		 * The user provided a table.  The stream head
9055 		 * may have copied in the user data in chunks,
9056 		 * so make sure everything is pulled up
9057 		 * properly.
9058 		 */
9059 		if (MBLKL(data_mp) < iocp->ioc_count) {
9060 			mblk_t *new_data_mp;
9061 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9062 			    NULL) {
9063 				miocnak(q, mp, 0, ENOMEM);
9064 				return;
9065 			}
9066 			freemsg(data_mp);
9067 			data_mp = new_data_mp;
9068 			mp->b_cont = data_mp;
9069 		}
9070 		table = (ip6_asp_t *)data_mp->b_rptr;
9071 		table_size = iocp->ioc_count;
9072 	}
9073 
9074 	switch (iocp->ioc_cmd) {
9075 	case SIOCGIP6ADDRPOLICY:
9076 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9077 		if (iocp->ioc_rval == -1)
9078 			iocp->ioc_error = EINVAL;
9079 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9080 		else if (table != NULL &&
9081 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9082 			ip6_asp_t *src = table;
9083 			ip6_asp32_t *dst = (void *)table;
9084 			int count = table_size / sizeof (ip6_asp_t);
9085 			int i;
9086 
9087 			/*
9088 			 * We need to do an in-place shrink of the array
9089 			 * to match the alignment attributes of the
9090 			 * 32-bit ABI looking at it.
9091 			 */
9092 			/* LINTED: logical expression always true: op "||" */
9093 			ASSERT(sizeof (*src) > sizeof (*dst));
9094 			for (i = 1; i < count; i++)
9095 				bcopy(src + i, dst + i, sizeof (*dst));
9096 		}
9097 #endif
9098 		break;
9099 
9100 	case SIOCSIP6ADDRPOLICY:
9101 		ASSERT(mp->b_prev == NULL);
9102 		mp->b_prev = (void *)q;
9103 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9104 		/*
9105 		 * We pass in the datamodel here so that the ip6_asp_replace()
9106 		 * routine can handle converting from 32-bit to native formats
9107 		 * where necessary.
9108 		 *
9109 		 * A better way to handle this might be to convert the inbound
9110 		 * data structure here, and hang it off a new 'mp'; thus the
9111 		 * ip6_asp_replace() logic would always be dealing with native
9112 		 * format data structures..
9113 		 *
9114 		 * (An even simpler way to handle these ioctls is to just
9115 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9116 		 * and just recompile everything that depends on it.)
9117 		 */
9118 #endif
9119 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9120 		    iocp->ioc_flag & IOC_MODELS);
9121 		return;
9122 	}
9123 
9124 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9125 	qreply(q, mp);
9126 }
9127 
9128 static void
9129 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9130 {
9131 	mblk_t 		*data_mp;
9132 	struct dstinforeq	*dir;
9133 	uint8_t		*end, *cur;
9134 	in6_addr_t	*daddr, *saddr;
9135 	ipaddr_t	v4daddr;
9136 	ire_t		*ire;
9137 	char		*slabel, *dlabel;
9138 	boolean_t	isipv4;
9139 	int		match_ire;
9140 	ill_t		*dst_ill;
9141 	ipif_t		*src_ipif, *ire_ipif;
9142 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9143 	zoneid_t	zoneid;
9144 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9145 
9146 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9147 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9148 
9149 	/*
9150 	 * This ioctl is I_STR only, and must have a
9151 	 * data mblk following the M_IOCTL mblk.
9152 	 */
9153 	data_mp = mp->b_cont;
9154 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9155 		miocnak(q, mp, 0, EINVAL);
9156 		return;
9157 	}
9158 
9159 	if (MBLKL(data_mp) < iocp->ioc_count) {
9160 		mblk_t *new_data_mp;
9161 
9162 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9163 			miocnak(q, mp, 0, ENOMEM);
9164 			return;
9165 		}
9166 		freemsg(data_mp);
9167 		data_mp = new_data_mp;
9168 		mp->b_cont = data_mp;
9169 	}
9170 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9171 
9172 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9173 	    end - cur >= sizeof (struct dstinforeq);
9174 	    cur += sizeof (struct dstinforeq)) {
9175 		dir = (struct dstinforeq *)cur;
9176 		daddr = &dir->dir_daddr;
9177 		saddr = &dir->dir_saddr;
9178 
9179 		/*
9180 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9181 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9182 		 * and ipif_select_source[_v6]() do not.
9183 		 */
9184 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9185 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9186 
9187 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9188 		if (isipv4) {
9189 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9190 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9191 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9192 		} else {
9193 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9194 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9195 		}
9196 		if (ire == NULL) {
9197 			dir->dir_dreachable = 0;
9198 
9199 			/* move on to next dst addr */
9200 			continue;
9201 		}
9202 		dir->dir_dreachable = 1;
9203 
9204 		ire_ipif = ire->ire_ipif;
9205 		if (ire_ipif == NULL)
9206 			goto next_dst;
9207 
9208 		/*
9209 		 * We expect to get back an interface ire or a
9210 		 * gateway ire cache entry.  For both types, the
9211 		 * output interface is ire_ipif->ipif_ill.
9212 		 */
9213 		dst_ill = ire_ipif->ipif_ill;
9214 		dir->dir_dmactype = dst_ill->ill_mactype;
9215 
9216 		if (isipv4) {
9217 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9218 		} else {
9219 			src_ipif = ipif_select_source_v6(dst_ill,
9220 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9221 			    zoneid);
9222 		}
9223 		if (src_ipif == NULL)
9224 			goto next_dst;
9225 
9226 		*saddr = src_ipif->ipif_v6lcl_addr;
9227 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9228 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9229 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9230 		dir->dir_sdeprecated =
9231 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9232 		ipif_refrele(src_ipif);
9233 next_dst:
9234 		ire_refrele(ire);
9235 	}
9236 	miocack(q, mp, iocp->ioc_count, 0);
9237 }
9238 
9239 /*
9240  * Check if this is an address assigned to this machine.
9241  * Skips interfaces that are down by using ire checks.
9242  * Translates mapped addresses to v4 addresses and then
9243  * treats them as such, returning true if the v4 address
9244  * associated with this mapped address is configured.
9245  * Note: Applications will have to be careful what they do
9246  * with the response; use of mapped addresses limits
9247  * what can be done with the socket, especially with
9248  * respect to socket options and ioctls - neither IPv4
9249  * options nor IPv6 sticky options/ancillary data options
9250  * may be used.
9251  */
9252 /* ARGSUSED */
9253 int
9254 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9255     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9256 {
9257 	struct sioc_addrreq *sia;
9258 	sin_t *sin;
9259 	ire_t *ire;
9260 	mblk_t *mp1;
9261 	zoneid_t zoneid;
9262 	ip_stack_t	*ipst;
9263 
9264 	ip1dbg(("ip_sioctl_tmyaddr"));
9265 
9266 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9267 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9268 	ipst = CONNQ_TO_IPST(q);
9269 
9270 	/* Existence verified in ip_wput_nondata */
9271 	mp1 = mp->b_cont->b_cont;
9272 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9273 	sin = (sin_t *)&sia->sa_addr;
9274 	switch (sin->sin_family) {
9275 	case AF_INET6: {
9276 		sin6_t *sin6 = (sin6_t *)sin;
9277 
9278 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9279 			ipaddr_t v4_addr;
9280 
9281 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9282 			    v4_addr);
9283 			ire = ire_ctable_lookup(v4_addr, 0,
9284 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9285 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9286 		} else {
9287 			in6_addr_t v6addr;
9288 
9289 			v6addr = sin6->sin6_addr;
9290 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9291 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9292 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9293 		}
9294 		break;
9295 	}
9296 	case AF_INET: {
9297 		ipaddr_t v4addr;
9298 
9299 		v4addr = sin->sin_addr.s_addr;
9300 		ire = ire_ctable_lookup(v4addr, 0,
9301 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9302 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9303 		break;
9304 	}
9305 	default:
9306 		return (EAFNOSUPPORT);
9307 	}
9308 	if (ire != NULL) {
9309 		sia->sa_res = 1;
9310 		ire_refrele(ire);
9311 	} else {
9312 		sia->sa_res = 0;
9313 	}
9314 	return (0);
9315 }
9316 
9317 /*
9318  * Check if this is an address assigned on-link i.e. neighbor,
9319  * and makes sure it's reachable from the current zone.
9320  * Returns true for my addresses as well.
9321  * Translates mapped addresses to v4 addresses and then
9322  * treats them as such, returning true if the v4 address
9323  * associated with this mapped address is configured.
9324  * Note: Applications will have to be careful what they do
9325  * with the response; use of mapped addresses limits
9326  * what can be done with the socket, especially with
9327  * respect to socket options and ioctls - neither IPv4
9328  * options nor IPv6 sticky options/ancillary data options
9329  * may be used.
9330  */
9331 /* ARGSUSED */
9332 int
9333 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9334     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9335 {
9336 	struct sioc_addrreq *sia;
9337 	sin_t *sin;
9338 	mblk_t	*mp1;
9339 	ire_t *ire = NULL;
9340 	zoneid_t zoneid;
9341 	ip_stack_t	*ipst;
9342 
9343 	ip1dbg(("ip_sioctl_tonlink"));
9344 
9345 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9346 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9347 	ipst = CONNQ_TO_IPST(q);
9348 
9349 	/* Existence verified in ip_wput_nondata */
9350 	mp1 = mp->b_cont->b_cont;
9351 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9352 	sin = (sin_t *)&sia->sa_addr;
9353 
9354 	/*
9355 	 * Match addresses with a zero gateway field to avoid
9356 	 * routes going through a router.
9357 	 * Exclude broadcast and multicast addresses.
9358 	 */
9359 	switch (sin->sin_family) {
9360 	case AF_INET6: {
9361 		sin6_t *sin6 = (sin6_t *)sin;
9362 
9363 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9364 			ipaddr_t v4_addr;
9365 
9366 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9367 			    v4_addr);
9368 			if (!CLASSD(v4_addr)) {
9369 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9370 				    NULL, NULL, zoneid, NULL,
9371 				    MATCH_IRE_GW, ipst);
9372 			}
9373 		} else {
9374 			in6_addr_t v6addr;
9375 			in6_addr_t v6gw;
9376 
9377 			v6addr = sin6->sin6_addr;
9378 			v6gw = ipv6_all_zeros;
9379 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9380 				ire = ire_route_lookup_v6(&v6addr, 0,
9381 				    &v6gw, 0, NULL, NULL, zoneid,
9382 				    NULL, MATCH_IRE_GW, ipst);
9383 			}
9384 		}
9385 		break;
9386 	}
9387 	case AF_INET: {
9388 		ipaddr_t v4addr;
9389 
9390 		v4addr = sin->sin_addr.s_addr;
9391 		if (!CLASSD(v4addr)) {
9392 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9393 			    NULL, NULL, zoneid, NULL,
9394 			    MATCH_IRE_GW, ipst);
9395 		}
9396 		break;
9397 	}
9398 	default:
9399 		return (EAFNOSUPPORT);
9400 	}
9401 	sia->sa_res = 0;
9402 	if (ire != NULL) {
9403 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9404 		    IRE_LOCAL|IRE_LOOPBACK)) {
9405 			sia->sa_res = 1;
9406 		}
9407 		ire_refrele(ire);
9408 	}
9409 	return (0);
9410 }
9411 
9412 /*
9413  * TBD: implement when kernel maintaines a list of site prefixes.
9414  */
9415 /* ARGSUSED */
9416 int
9417 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9418     ip_ioctl_cmd_t *ipip, void *ifreq)
9419 {
9420 	return (ENXIO);
9421 }
9422 
9423 /* ARGSUSED */
9424 int
9425 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9426     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9427 {
9428 	ill_t  		*ill;
9429 	mblk_t		*mp1;
9430 	conn_t		*connp;
9431 	boolean_t	success;
9432 
9433 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9434 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9435 	/* ioctl comes down on an conn */
9436 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9437 	connp = Q_TO_CONN(q);
9438 
9439 	mp->b_datap->db_type = M_IOCTL;
9440 
9441 	/*
9442 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9443 	 * The original mp contains contaminated b_next values due to 'mi',
9444 	 * which is needed to do the mi_copy_done. Unfortunately if we
9445 	 * send down the original mblk itself and if we are popped due to an
9446 	 * an unplumb before the response comes back from tunnel,
9447 	 * the streamhead (which does a freemsg) will see this contaminated
9448 	 * message and the assertion in freemsg about non-null b_next/b_prev
9449 	 * will panic a DEBUG kernel.
9450 	 */
9451 	mp1 = copymsg(mp);
9452 	if (mp1 == NULL)
9453 		return (ENOMEM);
9454 
9455 	ill = ipif->ipif_ill;
9456 	mutex_enter(&connp->conn_lock);
9457 	mutex_enter(&ill->ill_lock);
9458 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9459 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9460 		    mp, 0);
9461 	} else {
9462 		success = ill_pending_mp_add(ill, connp, mp);
9463 	}
9464 	mutex_exit(&ill->ill_lock);
9465 	mutex_exit(&connp->conn_lock);
9466 
9467 	if (success) {
9468 		ip1dbg(("sending down tunparam request "));
9469 		putnext(ill->ill_wq, mp1);
9470 		return (EINPROGRESS);
9471 	} else {
9472 		/* The conn has started closing */
9473 		freemsg(mp1);
9474 		return (EINTR);
9475 	}
9476 }
9477 
9478 /*
9479  * ARP IOCTLs.
9480  * How does IP get in the business of fronting ARP configuration/queries?
9481  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9482  * are by tradition passed in through a datagram socket.  That lands in IP.
9483  * As it happens, this is just as well since the interface is quite crude in
9484  * that it passes in no information about protocol or hardware types, or
9485  * interface association.  After making the protocol assumption, IP is in
9486  * the position to look up the name of the ILL, which ARP will need, and
9487  * format a request that can be handled by ARP.  The request is passed up
9488  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9489  * back a response.  ARP supports its own set of more general IOCTLs, in
9490  * case anyone is interested.
9491  */
9492 /* ARGSUSED */
9493 int
9494 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9495     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9496 {
9497 	mblk_t *mp1;
9498 	mblk_t *mp2;
9499 	mblk_t *pending_mp;
9500 	ipaddr_t ipaddr;
9501 	area_t *area;
9502 	struct iocblk *iocp;
9503 	conn_t *connp;
9504 	struct arpreq *ar;
9505 	struct xarpreq *xar;
9506 	int flags, alength;
9507 	char *lladdr;
9508 	ip_stack_t	*ipst;
9509 	ill_t *ill = ipif->ipif_ill;
9510 	boolean_t if_arp_ioctl = B_FALSE;
9511 
9512 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9513 	connp = Q_TO_CONN(q);
9514 	ipst = connp->conn_netstack->netstack_ip;
9515 
9516 	if (ipip->ipi_cmd_type == XARP_CMD) {
9517 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9518 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9519 		ar = NULL;
9520 
9521 		flags = xar->xarp_flags;
9522 		lladdr = LLADDR(&xar->xarp_ha);
9523 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9524 		/*
9525 		 * Validate against user's link layer address length
9526 		 * input and name and addr length limits.
9527 		 */
9528 		alength = ill->ill_phys_addr_length;
9529 		if (ipip->ipi_cmd == SIOCSXARP) {
9530 			if (alength != xar->xarp_ha.sdl_alen ||
9531 			    (alength + xar->xarp_ha.sdl_nlen >
9532 			    sizeof (xar->xarp_ha.sdl_data)))
9533 				return (EINVAL);
9534 		}
9535 	} else {
9536 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9537 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9538 		xar = NULL;
9539 
9540 		flags = ar->arp_flags;
9541 		lladdr = ar->arp_ha.sa_data;
9542 		/*
9543 		 * Theoretically, the sa_family could tell us what link
9544 		 * layer type this operation is trying to deal with. By
9545 		 * common usage AF_UNSPEC means ethernet. We'll assume
9546 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9547 		 * for now. Our new SIOC*XARP ioctls can be used more
9548 		 * generally.
9549 		 *
9550 		 * If the underlying media happens to have a non 6 byte
9551 		 * address, arp module will fail set/get, but the del
9552 		 * operation will succeed.
9553 		 */
9554 		alength = 6;
9555 		if ((ipip->ipi_cmd != SIOCDARP) &&
9556 		    (alength != ill->ill_phys_addr_length)) {
9557 			return (EINVAL);
9558 		}
9559 	}
9560 
9561 	/*
9562 	 * We are going to pass up to ARP a packet chain that looks
9563 	 * like:
9564 	 *
9565 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9566 	 *
9567 	 * Get a copy of the original IOCTL mblk to head the chain,
9568 	 * to be sent up (in mp1). Also get another copy to store
9569 	 * in the ill_pending_mp list, for matching the response
9570 	 * when it comes back from ARP.
9571 	 */
9572 	mp1 = copyb(mp);
9573 	pending_mp = copymsg(mp);
9574 	if (mp1 == NULL || pending_mp == NULL) {
9575 		if (mp1 != NULL)
9576 			freeb(mp1);
9577 		if (pending_mp != NULL)
9578 			inet_freemsg(pending_mp);
9579 		return (ENOMEM);
9580 	}
9581 
9582 	ipaddr = sin->sin_addr.s_addr;
9583 
9584 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9585 	    (caddr_t)&ipaddr);
9586 	if (mp2 == NULL) {
9587 		freeb(mp1);
9588 		inet_freemsg(pending_mp);
9589 		return (ENOMEM);
9590 	}
9591 	/* Put together the chain. */
9592 	mp1->b_cont = mp2;
9593 	mp1->b_datap->db_type = M_IOCTL;
9594 	mp2->b_cont = mp;
9595 	mp2->b_datap->db_type = M_DATA;
9596 
9597 	iocp = (struct iocblk *)mp1->b_rptr;
9598 
9599 	/*
9600 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9601 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9602 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9603 	 * ioc_count field; set ioc_count to be correct.
9604 	 */
9605 	iocp->ioc_count = MBLKL(mp1->b_cont);
9606 
9607 	/*
9608 	 * Set the proper command in the ARP message.
9609 	 * Convert the SIOC{G|S|D}ARP calls into our
9610 	 * AR_ENTRY_xxx calls.
9611 	 */
9612 	area = (area_t *)mp2->b_rptr;
9613 	switch (iocp->ioc_cmd) {
9614 	case SIOCDARP:
9615 	case SIOCDXARP:
9616 		/*
9617 		 * We defer deleting the corresponding IRE until
9618 		 * we return from arp.
9619 		 */
9620 		area->area_cmd = AR_ENTRY_DELETE;
9621 		area->area_proto_mask_offset = 0;
9622 		break;
9623 	case SIOCGARP:
9624 	case SIOCGXARP:
9625 		area->area_cmd = AR_ENTRY_SQUERY;
9626 		area->area_proto_mask_offset = 0;
9627 		break;
9628 	case SIOCSARP:
9629 	case SIOCSXARP:
9630 		/*
9631 		 * Delete the corresponding ire to make sure IP will
9632 		 * pick up any change from arp.
9633 		 */
9634 		if (!if_arp_ioctl) {
9635 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9636 		} else {
9637 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9638 			if (ipif != NULL) {
9639 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9640 				    ipst);
9641 				ipif_refrele(ipif);
9642 			}
9643 		}
9644 		break;
9645 	}
9646 	iocp->ioc_cmd = area->area_cmd;
9647 
9648 	/*
9649 	 * Fill in the rest of the ARP operation fields.
9650 	 */
9651 	area->area_hw_addr_length = alength;
9652 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9653 
9654 	/* Translate the flags. */
9655 	if (flags & ATF_PERM)
9656 		area->area_flags |= ACE_F_PERMANENT;
9657 	if (flags & ATF_PUBL)
9658 		area->area_flags |= ACE_F_PUBLISH;
9659 	if (flags & ATF_AUTHORITY)
9660 		area->area_flags |= ACE_F_AUTHORITY;
9661 
9662 	/*
9663 	 * Before sending 'mp' to ARP, we have to clear the b_next
9664 	 * and b_prev. Otherwise if STREAMS encounters such a message
9665 	 * in freemsg(), (because ARP can close any time) it can cause
9666 	 * a panic. But mi code needs the b_next and b_prev values of
9667 	 * mp->b_cont, to complete the ioctl. So we store it here
9668 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9669 	 * when the response comes down from ARP.
9670 	 */
9671 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9672 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9673 	mp->b_cont->b_next = NULL;
9674 	mp->b_cont->b_prev = NULL;
9675 
9676 	mutex_enter(&connp->conn_lock);
9677 	mutex_enter(&ill->ill_lock);
9678 	/* conn has not yet started closing, hence this can't fail */
9679 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9680 	mutex_exit(&ill->ill_lock);
9681 	mutex_exit(&connp->conn_lock);
9682 
9683 	/*
9684 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9685 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9686 	 */
9687 	putnext(ill->ill_rq, mp1);
9688 	return (EINPROGRESS);
9689 }
9690 
9691 /*
9692  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9693  * the associated sin and refhold and return the associated ipif via `ci'.
9694  */
9695 int
9696 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9697     cmd_info_t *ci, ipsq_func_t func)
9698 {
9699 	mblk_t	*mp1;
9700 	int	err;
9701 	sin_t	*sin;
9702 	conn_t	*connp;
9703 	ipif_t	*ipif;
9704 	ire_t	*ire = NULL;
9705 	ill_t	*ill = NULL;
9706 	boolean_t exists;
9707 	ip_stack_t *ipst;
9708 	struct arpreq *ar;
9709 	struct xarpreq *xar;
9710 	struct sockaddr_dl *sdl;
9711 
9712 	/* ioctl comes down on a conn */
9713 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9714 	connp = Q_TO_CONN(q);
9715 	if (connp->conn_af_isv6)
9716 		return (ENXIO);
9717 
9718 	ipst = connp->conn_netstack->netstack_ip;
9719 
9720 	/* Verified in ip_wput_nondata */
9721 	mp1 = mp->b_cont->b_cont;
9722 
9723 	if (ipip->ipi_cmd_type == XARP_CMD) {
9724 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9725 		xar = (struct xarpreq *)mp1->b_rptr;
9726 		sin = (sin_t *)&xar->xarp_pa;
9727 		sdl = &xar->xarp_ha;
9728 
9729 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9730 			return (ENXIO);
9731 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9732 			return (EINVAL);
9733 	} else {
9734 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9735 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9736 		ar = (struct arpreq *)mp1->b_rptr;
9737 		sin = (sin_t *)&ar->arp_pa;
9738 	}
9739 
9740 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9741 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9742 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9743 		    mp, func, &err, ipst);
9744 		if (ipif == NULL)
9745 			return (err);
9746 		if (ipif->ipif_id != 0 ||
9747 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9748 			ipif_refrele(ipif);
9749 			return (ENXIO);
9750 		}
9751 	} else {
9752 		/*
9753 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9754 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9755 		 * case, a simple forwarding table lookup will return the
9756 		 * IRE_IF_RESOLVER for the first interface in the group, which
9757 		 * might not be the interface on which the requested IP
9758 		 * address was resolved due to the ill selection algorithm
9759 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9760 		 * lookup first: if the IRE cache entry for the IP address is
9761 		 * still there, it will contain the ill pointer for the right
9762 		 * interface, so we use that. If the cache entry has been
9763 		 * flushed, we fall back to the forwarding table lookup. This
9764 		 * should be rare enough since IRE cache entries have a longer
9765 		 * life expectancy than ARP cache entries.
9766 		 */
9767 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9768 		    ipst);
9769 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9770 		    ((ill = ire_to_ill(ire)) == NULL) ||
9771 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9772 			if (ire != NULL)
9773 				ire_refrele(ire);
9774 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9775 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9776 			    NULL, MATCH_IRE_TYPE, ipst);
9777 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9778 
9779 				if (ire != NULL)
9780 					ire_refrele(ire);
9781 				return (ENXIO);
9782 			}
9783 		}
9784 		ASSERT(ire != NULL && ill != NULL);
9785 		ipif = ill->ill_ipif;
9786 		ipif_refhold(ipif);
9787 		ire_refrele(ire);
9788 	}
9789 	ci->ci_sin = sin;
9790 	ci->ci_ipif = ipif;
9791 	return (0);
9792 }
9793 
9794 /*
9795  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9796  * atomically set/clear the muxids. Also complete the ioctl by acking or
9797  * naking it.  Note that the code is structured such that the link type,
9798  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9799  * its clones use the persistent link, while pppd(1M) and perhaps many
9800  * other daemons may use non-persistent link.  When combined with some
9801  * ill_t states, linking and unlinking lower streams may be used as
9802  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9803  */
9804 /* ARGSUSED */
9805 void
9806 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9807 {
9808 	mblk_t		*mp1, *mp2;
9809 	struct linkblk	*li;
9810 	struct ipmx_s	*ipmxp;
9811 	ill_t		*ill;
9812 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9813 	int		err = 0;
9814 	boolean_t	entered_ipsq = B_FALSE;
9815 	boolean_t	islink;
9816 	ip_stack_t	*ipst;
9817 
9818 	if (CONN_Q(q))
9819 		ipst = CONNQ_TO_IPST(q);
9820 	else
9821 		ipst = ILLQ_TO_IPST(q);
9822 
9823 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9824 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9825 
9826 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9827 
9828 	mp1 = mp->b_cont;	/* This is the linkblk info */
9829 	li = (struct linkblk *)mp1->b_rptr;
9830 
9831 	/*
9832 	 * ARP has added this special mblk, and the utility is asking us
9833 	 * to perform consistency checks, and also atomically set the
9834 	 * muxid. Ifconfig is an example.  It achieves this by using
9835 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9836 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9837 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9838 	 * and other comments in this routine for more details.
9839 	 */
9840 	mp2 = mp1->b_cont;	/* This is added by ARP */
9841 
9842 	/*
9843 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9844 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9845 	 * get the special mblk above.  For backward compatibility, we
9846 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9847 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9848 	 * not atomic, and can leave the streams unplumbable if the utility
9849 	 * is interrupted before it does the SIOCSLIFMUXID.
9850 	 */
9851 	if (mp2 == NULL) {
9852 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9853 		if (err == EINPROGRESS)
9854 			return;
9855 		goto done;
9856 	}
9857 
9858 	/*
9859 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9860 	 * ARP has appended this last mblk to tell us whether the lower stream
9861 	 * is an arp-dev stream or an IP module stream.
9862 	 */
9863 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9864 	if (ipmxp->ipmx_arpdev_stream) {
9865 		/*
9866 		 * The lower stream is the arp-dev stream.
9867 		 */
9868 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9869 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9870 		if (ill == NULL) {
9871 			if (err == EINPROGRESS)
9872 				return;
9873 			err = EINVAL;
9874 			goto done;
9875 		}
9876 
9877 		if (ipsq == NULL) {
9878 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9879 			    NEW_OP, B_TRUE);
9880 			if (ipsq == NULL) {
9881 				ill_refrele(ill);
9882 				return;
9883 			}
9884 			entered_ipsq = B_TRUE;
9885 		}
9886 		ASSERT(IAM_WRITER_ILL(ill));
9887 		ill_refrele(ill);
9888 
9889 		/*
9890 		 * To ensure consistency between IP and ARP, the following
9891 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9892 		 * This is because the muxid's are stored in the IP stream on
9893 		 * the ill.
9894 		 *
9895 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9896 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9897 		 * not yet plinked, and it also checks that the corresponding
9898 		 * IP stream is already plinked.
9899 		 *
9900 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9901 		 * punlinking the IP stream. IP does not allow punlink of the
9902 		 * IP stream unless the arp stream has been punlinked.
9903 		 */
9904 		if ((islink &&
9905 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9906 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9907 			err = EINVAL;
9908 			goto done;
9909 		}
9910 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9911 	} else {
9912 		/*
9913 		 * The lower stream is probably an IP module stream.  Do
9914 		 * consistency checking.
9915 		 */
9916 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9917 		if (err == EINPROGRESS)
9918 			return;
9919 	}
9920 done:
9921 	if (err == 0)
9922 		miocack(q, mp, 0, 0);
9923 	else
9924 		miocnak(q, mp, 0, err);
9925 
9926 	/* Conn was refheld in ip_sioctl_copyin_setup */
9927 	if (CONN_Q(q))
9928 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9929 	if (entered_ipsq)
9930 		ipsq_exit(ipsq);
9931 }
9932 
9933 /*
9934  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9935  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9936  * module stream).  If `doconsist' is set, then do the extended consistency
9937  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9938  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9939  * an error code on failure.
9940  */
9941 static int
9942 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9943     struct linkblk *li, boolean_t doconsist)
9944 {
9945 	ill_t  		*ill;
9946 	queue_t		*ipwq, *dwq;
9947 	const char	*name;
9948 	struct qinit	*qinfo;
9949 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9950 	boolean_t	entered_ipsq = B_FALSE;
9951 
9952 	/*
9953 	 * Walk the lower stream to verify it's the IP module stream.
9954 	 * The IP module is identified by its name, wput function,
9955 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9956 	 * (li->l_qbot) will not vanish until this ioctl completes.
9957 	 */
9958 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9959 		qinfo = ipwq->q_qinfo;
9960 		name = qinfo->qi_minfo->mi_idname;
9961 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9962 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9963 			break;
9964 		}
9965 	}
9966 
9967 	/*
9968 	 * If this isn't an IP module stream, bail.
9969 	 */
9970 	if (ipwq == NULL)
9971 		return (0);
9972 
9973 	ill = ipwq->q_ptr;
9974 	ASSERT(ill != NULL);
9975 
9976 	if (ipsq == NULL) {
9977 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9978 		    NEW_OP, B_TRUE);
9979 		if (ipsq == NULL)
9980 			return (EINPROGRESS);
9981 		entered_ipsq = B_TRUE;
9982 	}
9983 	ASSERT(IAM_WRITER_ILL(ill));
9984 
9985 	if (doconsist) {
9986 		/*
9987 		 * Consistency checking requires that I_{P}LINK occurs
9988 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9989 		 * occurs prior to clearing ill_arp_muxid.
9990 		 */
9991 		if ((islink && ill->ill_ip_muxid != 0) ||
9992 		    (!islink && ill->ill_arp_muxid != 0)) {
9993 			if (entered_ipsq)
9994 				ipsq_exit(ipsq);
9995 			return (EINVAL);
9996 		}
9997 	}
9998 
9999 	/*
10000 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10001 	 * the read queue of the module immediately below IP in the ill.
10002 	 * These are used during the capability negotiation below.
10003 	 */
10004 	ill->ill_lmod_rq = NULL;
10005 	ill->ill_lmod_cnt = 0;
10006 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10007 		ill->ill_lmod_rq = RD(dwq);
10008 		for (; dwq != NULL; dwq = dwq->q_next)
10009 			ill->ill_lmod_cnt++;
10010 	}
10011 
10012 	if (doconsist)
10013 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10014 
10015 	/*
10016 	 * If there's at least one up ipif on this ill, then we're bound to
10017 	 * the underlying driver via DLPI.  In that case, renegotiate
10018 	 * capabilities to account for any possible change in modules
10019 	 * interposed between IP and the driver.
10020 	 */
10021 	if (ill->ill_ipif_up_count > 0) {
10022 		if (islink)
10023 			ill_capability_probe(ill);
10024 		else
10025 			ill_capability_reset(ill);
10026 	}
10027 
10028 	if (entered_ipsq)
10029 		ipsq_exit(ipsq);
10030 
10031 	return (0);
10032 }
10033 
10034 /*
10035  * Search the ioctl command in the ioctl tables and return a pointer
10036  * to the ioctl command information. The ioctl command tables are
10037  * static and fully populated at compile time.
10038  */
10039 ip_ioctl_cmd_t *
10040 ip_sioctl_lookup(int ioc_cmd)
10041 {
10042 	int index;
10043 	ip_ioctl_cmd_t *ipip;
10044 	ip_ioctl_cmd_t *ipip_end;
10045 
10046 	if (ioc_cmd == IPI_DONTCARE)
10047 		return (NULL);
10048 
10049 	/*
10050 	 * Do a 2 step search. First search the indexed table
10051 	 * based on the least significant byte of the ioctl cmd.
10052 	 * If we don't find a match, then search the misc table
10053 	 * serially.
10054 	 */
10055 	index = ioc_cmd & 0xFF;
10056 	if (index < ip_ndx_ioctl_count) {
10057 		ipip = &ip_ndx_ioctl_table[index];
10058 		if (ipip->ipi_cmd == ioc_cmd) {
10059 			/* Found a match in the ndx table */
10060 			return (ipip);
10061 		}
10062 	}
10063 
10064 	/* Search the misc table */
10065 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10066 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10067 		if (ipip->ipi_cmd == ioc_cmd)
10068 			/* Found a match in the misc table */
10069 			return (ipip);
10070 	}
10071 
10072 	return (NULL);
10073 }
10074 
10075 /*
10076  * Wrapper function for resuming deferred ioctl processing
10077  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10078  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10079  */
10080 /* ARGSUSED */
10081 void
10082 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10083     void *dummy_arg)
10084 {
10085 	ip_sioctl_copyin_setup(q, mp);
10086 }
10087 
10088 /*
10089  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10090  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10091  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10092  * We establish here the size of the block to be copied in.  mi_copyin
10093  * arranges for this to happen, an processing continues in ip_wput with
10094  * an M_IOCDATA message.
10095  */
10096 void
10097 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10098 {
10099 	int	copyin_size;
10100 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10101 	ip_ioctl_cmd_t *ipip;
10102 	cred_t *cr;
10103 	ip_stack_t	*ipst;
10104 
10105 	if (CONN_Q(q))
10106 		ipst = CONNQ_TO_IPST(q);
10107 	else
10108 		ipst = ILLQ_TO_IPST(q);
10109 
10110 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10111 	if (ipip == NULL) {
10112 		/*
10113 		 * The ioctl is not one we understand or own.
10114 		 * Pass it along to be processed down stream,
10115 		 * if this is a module instance of IP, else nak
10116 		 * the ioctl.
10117 		 */
10118 		if (q->q_next == NULL) {
10119 			goto nak;
10120 		} else {
10121 			putnext(q, mp);
10122 			return;
10123 		}
10124 	}
10125 
10126 	/*
10127 	 * If this is deferred, then we will do all the checks when we
10128 	 * come back.
10129 	 */
10130 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10131 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10132 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10133 		return;
10134 	}
10135 
10136 	/*
10137 	 * Only allow a very small subset of IP ioctls on this stream if
10138 	 * IP is a module and not a driver. Allowing ioctls to be processed
10139 	 * in this case may cause assert failures or data corruption.
10140 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10141 	 * ioctls allowed on an IP module stream, after which this stream
10142 	 * normally becomes a multiplexor (at which time the stream head
10143 	 * will fail all ioctls).
10144 	 */
10145 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10146 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10147 			/*
10148 			 * Pass common Streams ioctls which the IP
10149 			 * module does not own or consume along to
10150 			 * be processed down stream.
10151 			 */
10152 			putnext(q, mp);
10153 			return;
10154 		} else {
10155 			goto nak;
10156 		}
10157 	}
10158 
10159 	/* Make sure we have ioctl data to process. */
10160 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10161 		goto nak;
10162 
10163 	/*
10164 	 * Prefer dblk credential over ioctl credential; some synthesized
10165 	 * ioctls have kcred set because there's no way to crhold()
10166 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10167 	 * the framework; the caller of ioctl needs to hold the reference
10168 	 * for the duration of the call).
10169 	 */
10170 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10171 
10172 	/* Make sure normal users don't send down privileged ioctls */
10173 	if ((ipip->ipi_flags & IPI_PRIV) &&
10174 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10175 		/* We checked the privilege earlier but log it here */
10176 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10177 		return;
10178 	}
10179 
10180 	/*
10181 	 * The ioctl command tables can only encode fixed length
10182 	 * ioctl data. If the length is variable, the table will
10183 	 * encode the length as zero. Such special cases are handled
10184 	 * below in the switch.
10185 	 */
10186 	if (ipip->ipi_copyin_size != 0) {
10187 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10188 		return;
10189 	}
10190 
10191 	switch (iocp->ioc_cmd) {
10192 	case O_SIOCGIFCONF:
10193 	case SIOCGIFCONF:
10194 		/*
10195 		 * This IOCTL is hilarious.  See comments in
10196 		 * ip_sioctl_get_ifconf for the story.
10197 		 */
10198 		if (iocp->ioc_count == TRANSPARENT)
10199 			copyin_size = SIZEOF_STRUCT(ifconf,
10200 			    iocp->ioc_flag);
10201 		else
10202 			copyin_size = iocp->ioc_count;
10203 		mi_copyin(q, mp, NULL, copyin_size);
10204 		return;
10205 
10206 	case O_SIOCGLIFCONF:
10207 	case SIOCGLIFCONF:
10208 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10209 		mi_copyin(q, mp, NULL, copyin_size);
10210 		return;
10211 
10212 	case SIOCGLIFSRCOF:
10213 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10214 		mi_copyin(q, mp, NULL, copyin_size);
10215 		return;
10216 	case SIOCGIP6ADDRPOLICY:
10217 		ip_sioctl_ip6addrpolicy(q, mp);
10218 		ip6_asp_table_refrele(ipst);
10219 		return;
10220 
10221 	case SIOCSIP6ADDRPOLICY:
10222 		ip_sioctl_ip6addrpolicy(q, mp);
10223 		return;
10224 
10225 	case SIOCGDSTINFO:
10226 		ip_sioctl_dstinfo(q, mp);
10227 		ip6_asp_table_refrele(ipst);
10228 		return;
10229 
10230 	case I_PLINK:
10231 	case I_PUNLINK:
10232 	case I_LINK:
10233 	case I_UNLINK:
10234 		/*
10235 		 * We treat non-persistent link similarly as the persistent
10236 		 * link case, in terms of plumbing/unplumbing, as well as
10237 		 * dynamic re-plumbing events indicator.  See comments
10238 		 * in ip_sioctl_plink() for more.
10239 		 *
10240 		 * Request can be enqueued in the 'ipsq' while waiting
10241 		 * to become exclusive. So bump up the conn ref.
10242 		 */
10243 		if (CONN_Q(q))
10244 			CONN_INC_REF(Q_TO_CONN(q));
10245 		ip_sioctl_plink(NULL, q, mp, NULL);
10246 		return;
10247 
10248 	case ND_GET:
10249 	case ND_SET:
10250 		/*
10251 		 * Use of the nd table requires holding the reader lock.
10252 		 * Modifying the nd table thru nd_load/nd_unload requires
10253 		 * the writer lock.
10254 		 */
10255 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10256 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10257 			rw_exit(&ipst->ips_ip_g_nd_lock);
10258 
10259 			if (iocp->ioc_error)
10260 				iocp->ioc_count = 0;
10261 			mp->b_datap->db_type = M_IOCACK;
10262 			qreply(q, mp);
10263 			return;
10264 		}
10265 		rw_exit(&ipst->ips_ip_g_nd_lock);
10266 		/*
10267 		 * We don't understand this subioctl of ND_GET / ND_SET.
10268 		 * Maybe intended for some driver / module below us
10269 		 */
10270 		if (q->q_next) {
10271 			putnext(q, mp);
10272 		} else {
10273 			iocp->ioc_error = ENOENT;
10274 			mp->b_datap->db_type = M_IOCNAK;
10275 			iocp->ioc_count = 0;
10276 			qreply(q, mp);
10277 		}
10278 		return;
10279 
10280 	case IP_IOCTL:
10281 		ip_wput_ioctl(q, mp);
10282 		return;
10283 	default:
10284 		cmn_err(CE_PANIC, "should not happen ");
10285 	}
10286 nak:
10287 	if (mp->b_cont != NULL) {
10288 		freemsg(mp->b_cont);
10289 		mp->b_cont = NULL;
10290 	}
10291 	iocp->ioc_error = EINVAL;
10292 	mp->b_datap->db_type = M_IOCNAK;
10293 	iocp->ioc_count = 0;
10294 	qreply(q, mp);
10295 }
10296 
10297 /* ip_wput hands off ARP IOCTL responses to us */
10298 void
10299 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10300 {
10301 	struct arpreq *ar;
10302 	struct xarpreq *xar;
10303 	area_t	*area;
10304 	mblk_t	*area_mp;
10305 	struct iocblk *iocp;
10306 	mblk_t	*orig_ioc_mp, *tmp;
10307 	struct iocblk	*orig_iocp;
10308 	ill_t *ill;
10309 	conn_t *connp = NULL;
10310 	uint_t ioc_id;
10311 	mblk_t *pending_mp;
10312 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10313 	int *flagsp;
10314 	char *storage = NULL;
10315 	sin_t *sin;
10316 	ipaddr_t addr;
10317 	int err;
10318 	ip_stack_t *ipst;
10319 
10320 	ill = q->q_ptr;
10321 	ASSERT(ill != NULL);
10322 	ipst = ill->ill_ipst;
10323 
10324 	/*
10325 	 * We should get back from ARP a packet chain that looks like:
10326 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10327 	 */
10328 	if (!(area_mp = mp->b_cont) ||
10329 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10330 	    !(orig_ioc_mp = area_mp->b_cont) ||
10331 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10332 		freemsg(mp);
10333 		return;
10334 	}
10335 
10336 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10337 
10338 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10339 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10340 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10341 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10342 		x_arp_ioctl = B_TRUE;
10343 		xar = (struct xarpreq *)tmp->b_rptr;
10344 		sin = (sin_t *)&xar->xarp_pa;
10345 		flagsp = &xar->xarp_flags;
10346 		storage = xar->xarp_ha.sdl_data;
10347 		if (xar->xarp_ha.sdl_nlen != 0)
10348 			ifx_arp_ioctl = B_TRUE;
10349 	} else {
10350 		ar = (struct arpreq *)tmp->b_rptr;
10351 		sin = (sin_t *)&ar->arp_pa;
10352 		flagsp = &ar->arp_flags;
10353 		storage = ar->arp_ha.sa_data;
10354 	}
10355 
10356 	iocp = (struct iocblk *)mp->b_rptr;
10357 
10358 	/*
10359 	 * Pick out the originating queue based on the ioc_id.
10360 	 */
10361 	ioc_id = iocp->ioc_id;
10362 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10363 	if (pending_mp == NULL) {
10364 		ASSERT(connp == NULL);
10365 		inet_freemsg(mp);
10366 		return;
10367 	}
10368 	ASSERT(connp != NULL);
10369 	q = CONNP_TO_WQ(connp);
10370 
10371 	/* Uncouple the internally generated IOCTL from the original one */
10372 	area = (area_t *)area_mp->b_rptr;
10373 	area_mp->b_cont = NULL;
10374 
10375 	/*
10376 	 * Restore the b_next and b_prev used by mi code. This is needed
10377 	 * to complete the ioctl using mi* functions. We stored them in
10378 	 * the pending mp prior to sending the request to ARP.
10379 	 */
10380 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10381 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10382 	inet_freemsg(pending_mp);
10383 
10384 	/*
10385 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10386 	 * Catch the case where there is an IRE_CACHE by no entry in the
10387 	 * arp table.
10388 	 */
10389 	addr = sin->sin_addr.s_addr;
10390 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10391 		ire_t			*ire;
10392 		dl_unitdata_req_t	*dlup;
10393 		mblk_t			*llmp;
10394 		int			addr_len;
10395 		ill_t			*ipsqill = NULL;
10396 
10397 		if (ifx_arp_ioctl) {
10398 			/*
10399 			 * There's no need to lookup the ill, since
10400 			 * we've already done that when we started
10401 			 * processing the ioctl and sent the message
10402 			 * to ARP on that ill.  So use the ill that
10403 			 * is stored in q->q_ptr.
10404 			 */
10405 			ipsqill = ill;
10406 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10407 			    ipsqill->ill_ipif, ALL_ZONES,
10408 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10409 		} else {
10410 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10411 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10412 			if (ire != NULL)
10413 				ipsqill = ire_to_ill(ire);
10414 		}
10415 
10416 		if ((x_arp_ioctl) && (ipsqill != NULL))
10417 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10418 
10419 		if (ire != NULL) {
10420 			/*
10421 			 * Since the ire obtained from cachetable is used for
10422 			 * mac addr copying below, treat an incomplete ire as if
10423 			 * as if we never found it.
10424 			 */
10425 			if (ire->ire_nce != NULL &&
10426 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10427 				ire_refrele(ire);
10428 				ire = NULL;
10429 				ipsqill = NULL;
10430 				goto errack;
10431 			}
10432 			*flagsp = ATF_INUSE;
10433 			llmp = (ire->ire_nce != NULL ?
10434 			    ire->ire_nce->nce_res_mp : NULL);
10435 			if (llmp != NULL && ipsqill != NULL) {
10436 				uchar_t *macaddr;
10437 
10438 				addr_len = ipsqill->ill_phys_addr_length;
10439 				if (x_arp_ioctl && ((addr_len +
10440 				    ipsqill->ill_name_length) >
10441 				    sizeof (xar->xarp_ha.sdl_data))) {
10442 					ire_refrele(ire);
10443 					freemsg(mp);
10444 					ip_ioctl_finish(q, orig_ioc_mp,
10445 					    EINVAL, NO_COPYOUT, NULL);
10446 					return;
10447 				}
10448 				*flagsp |= ATF_COM;
10449 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10450 				if (ipsqill->ill_sap_length < 0)
10451 					macaddr = llmp->b_rptr +
10452 					    dlup->dl_dest_addr_offset;
10453 				else
10454 					macaddr = llmp->b_rptr +
10455 					    dlup->dl_dest_addr_offset +
10456 					    ipsqill->ill_sap_length;
10457 				/*
10458 				 * For SIOCGARP, MAC address length
10459 				 * validation has already been done
10460 				 * before the ioctl was issued to ARP to
10461 				 * allow it to progress only on 6 byte
10462 				 * addressable (ethernet like) media. Thus
10463 				 * the mac address copying can not overwrite
10464 				 * the sa_data area below.
10465 				 */
10466 				bcopy(macaddr, storage, addr_len);
10467 			}
10468 			/* Ditch the internal IOCTL. */
10469 			freemsg(mp);
10470 			ire_refrele(ire);
10471 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10472 			return;
10473 		}
10474 	}
10475 
10476 	/*
10477 	 * Delete the coresponding IRE_CACHE if any.
10478 	 * Reset the error if there was one (in case there was no entry
10479 	 * in arp.)
10480 	 */
10481 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10482 		ipif_t *ipintf = NULL;
10483 
10484 		if (ifx_arp_ioctl) {
10485 			/*
10486 			 * There's no need to lookup the ill, since
10487 			 * we've already done that when we started
10488 			 * processing the ioctl and sent the message
10489 			 * to ARP on that ill.  So use the ill that
10490 			 * is stored in q->q_ptr.
10491 			 */
10492 			ipintf = ill->ill_ipif;
10493 		}
10494 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10495 			/*
10496 			 * The address in "addr" may be an entry for a
10497 			 * router. If that's true, then any off-net
10498 			 * IRE_CACHE entries that go through the router
10499 			 * with address "addr" must be clobbered. Use
10500 			 * ire_walk to achieve this goal.
10501 			 */
10502 			if (ifx_arp_ioctl)
10503 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10504 				    ire_delete_cache_gw, (char *)&addr, ill);
10505 			else
10506 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10507 				    ALL_ZONES, ipst);
10508 			iocp->ioc_error = 0;
10509 		}
10510 	}
10511 errack:
10512 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10513 		err = iocp->ioc_error;
10514 		freemsg(mp);
10515 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10516 		return;
10517 	}
10518 
10519 	/*
10520 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10521 	 * the area_t into the struct {x}arpreq.
10522 	 */
10523 	if (x_arp_ioctl) {
10524 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10525 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10526 		    sizeof (xar->xarp_ha.sdl_data)) {
10527 			freemsg(mp);
10528 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10529 			    NULL);
10530 			return;
10531 		}
10532 	}
10533 	*flagsp = ATF_INUSE;
10534 	if (area->area_flags & ACE_F_PERMANENT)
10535 		*flagsp |= ATF_PERM;
10536 	if (area->area_flags & ACE_F_PUBLISH)
10537 		*flagsp |= ATF_PUBL;
10538 	if (area->area_flags & ACE_F_AUTHORITY)
10539 		*flagsp |= ATF_AUTHORITY;
10540 	if (area->area_hw_addr_length != 0) {
10541 		*flagsp |= ATF_COM;
10542 		/*
10543 		 * For SIOCGARP, MAC address length validation has
10544 		 * already been done before the ioctl was issued to ARP
10545 		 * to allow it to progress only on 6 byte addressable
10546 		 * (ethernet like) media. Thus the mac address copying
10547 		 * can not overwrite the sa_data area below.
10548 		 */
10549 		bcopy((char *)area + area->area_hw_addr_offset,
10550 		    storage, area->area_hw_addr_length);
10551 	}
10552 
10553 	/* Ditch the internal IOCTL. */
10554 	freemsg(mp);
10555 	/* Complete the original. */
10556 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10557 }
10558 
10559 /*
10560  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10561  * interface) create the next available logical interface for this
10562  * physical interface.
10563  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10564  * ipif with the specified name.
10565  *
10566  * If the address family is not AF_UNSPEC then set the address as well.
10567  *
10568  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10569  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10570  *
10571  * Executed as a writer on the ill or ill group.
10572  * So no lock is needed to traverse the ipif chain, or examine the
10573  * phyint flags.
10574  */
10575 /* ARGSUSED */
10576 int
10577 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10578     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10579 {
10580 	mblk_t	*mp1;
10581 	struct lifreq *lifr;
10582 	boolean_t	isv6;
10583 	boolean_t	exists;
10584 	char 	*name;
10585 	char	*endp;
10586 	char	*cp;
10587 	int	namelen;
10588 	ipif_t	*ipif;
10589 	long	id;
10590 	ipsq_t	*ipsq;
10591 	ill_t	*ill;
10592 	sin_t	*sin;
10593 	int	err = 0;
10594 	boolean_t found_sep = B_FALSE;
10595 	conn_t	*connp;
10596 	zoneid_t zoneid;
10597 	int	orig_ifindex = 0;
10598 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10599 
10600 	ASSERT(q->q_next == NULL);
10601 	ip1dbg(("ip_sioctl_addif\n"));
10602 	/* Existence of mp1 has been checked in ip_wput_nondata */
10603 	mp1 = mp->b_cont->b_cont;
10604 	/*
10605 	 * Null terminate the string to protect against buffer
10606 	 * overrun. String was generated by user code and may not
10607 	 * be trusted.
10608 	 */
10609 	lifr = (struct lifreq *)mp1->b_rptr;
10610 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10611 	name = lifr->lifr_name;
10612 	ASSERT(CONN_Q(q));
10613 	connp = Q_TO_CONN(q);
10614 	isv6 = connp->conn_af_isv6;
10615 	zoneid = connp->conn_zoneid;
10616 	namelen = mi_strlen(name);
10617 	if (namelen == 0)
10618 		return (EINVAL);
10619 
10620 	exists = B_FALSE;
10621 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10622 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10623 		/*
10624 		 * Allow creating lo0 using SIOCLIFADDIF.
10625 		 * can't be any other writer thread. So can pass null below
10626 		 * for the last 4 args to ipif_lookup_name.
10627 		 */
10628 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10629 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10630 		/* Prevent any further action */
10631 		if (ipif == NULL) {
10632 			return (ENOBUFS);
10633 		} else if (!exists) {
10634 			/* We created the ipif now and as writer */
10635 			ipif_refrele(ipif);
10636 			return (0);
10637 		} else {
10638 			ill = ipif->ipif_ill;
10639 			ill_refhold(ill);
10640 			ipif_refrele(ipif);
10641 		}
10642 	} else {
10643 		/* Look for a colon in the name. */
10644 		endp = &name[namelen];
10645 		for (cp = endp; --cp > name; ) {
10646 			if (*cp == IPIF_SEPARATOR_CHAR) {
10647 				found_sep = B_TRUE;
10648 				/*
10649 				 * Reject any non-decimal aliases for plumbing
10650 				 * of logical interfaces. Aliases with leading
10651 				 * zeroes are also rejected as they introduce
10652 				 * ambiguity in the naming of the interfaces.
10653 				 * Comparing with "0" takes care of all such
10654 				 * cases.
10655 				 */
10656 				if ((strncmp("0", cp+1, 1)) == 0)
10657 					return (EINVAL);
10658 
10659 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10660 				    id <= 0 || *endp != '\0') {
10661 					return (EINVAL);
10662 				}
10663 				*cp = '\0';
10664 				break;
10665 			}
10666 		}
10667 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10668 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10669 		if (found_sep)
10670 			*cp = IPIF_SEPARATOR_CHAR;
10671 		if (ill == NULL)
10672 			return (err);
10673 	}
10674 
10675 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10676 	    B_TRUE);
10677 
10678 	/*
10679 	 * Release the refhold due to the lookup, now that we are excl
10680 	 * or we are just returning
10681 	 */
10682 	ill_refrele(ill);
10683 
10684 	if (ipsq == NULL)
10685 		return (EINPROGRESS);
10686 
10687 	/*
10688 	 * If the interface is failed, inactive or offlined, look for a working
10689 	 * interface in the ill group and create the ipif there. If we can't
10690 	 * find a good interface, create the ipif anyway so that in.mpathd can
10691 	 * move it to the first repaired interface.
10692 	 */
10693 	if ((ill->ill_phyint->phyint_flags &
10694 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10695 	    ill->ill_phyint->phyint_groupname_len != 0) {
10696 		phyint_t *phyi;
10697 		char *groupname = ill->ill_phyint->phyint_groupname;
10698 
10699 		/*
10700 		 * We're looking for a working interface, but it doesn't matter
10701 		 * if it's up or down; so instead of following the group lists,
10702 		 * we look at each physical interface and compare the groupname.
10703 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10704 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10705 		 * Otherwise we create the ipif on the failed interface.
10706 		 */
10707 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10708 		phyi = avl_first(&ipst->ips_phyint_g_list->
10709 		    phyint_list_avl_by_index);
10710 		for (; phyi != NULL;
10711 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10712 		    phyint_list_avl_by_index,
10713 		    phyi, AVL_AFTER)) {
10714 			if (phyi->phyint_groupname_len == 0)
10715 				continue;
10716 			ASSERT(phyi->phyint_groupname != NULL);
10717 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10718 			    !(phyi->phyint_flags &
10719 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10720 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10721 			    (phyi->phyint_illv4 != NULL))) {
10722 				break;
10723 			}
10724 		}
10725 		rw_exit(&ipst->ips_ill_g_lock);
10726 
10727 		if (phyi != NULL) {
10728 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10729 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10730 			    phyi->phyint_illv4);
10731 		}
10732 	}
10733 
10734 	/*
10735 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10736 	 * before or after us.
10737 	 */
10738 	ASSERT(IAM_WRITER_ILL(ill));
10739 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10740 
10741 	if (found_sep && orig_ifindex == 0) {
10742 		/* Now see if there is an IPIF with this unit number. */
10743 		for (ipif = ill->ill_ipif; ipif != NULL;
10744 		    ipif = ipif->ipif_next) {
10745 			if (ipif->ipif_id == id) {
10746 				err = EEXIST;
10747 				goto done;
10748 			}
10749 		}
10750 	}
10751 
10752 	/*
10753 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10754 	 * of lo0. We never come here when we plumb lo0:0. It
10755 	 * happens in ipif_lookup_on_name.
10756 	 * The specified unit number is ignored when we create the ipif on a
10757 	 * different interface. However, we save it in ipif_orig_ipifid below so
10758 	 * that the ipif fails back to the right position.
10759 	 */
10760 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10761 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10762 		err = ENOBUFS;
10763 		goto done;
10764 	}
10765 
10766 	/* Return created name with ioctl */
10767 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10768 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10769 	ip1dbg(("created %s\n", lifr->lifr_name));
10770 
10771 	/* Set address */
10772 	sin = (sin_t *)&lifr->lifr_addr;
10773 	if (sin->sin_family != AF_UNSPEC) {
10774 		err = ip_sioctl_addr(ipif, sin, q, mp,
10775 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10776 	}
10777 
10778 	/* Set ifindex and unit number for failback */
10779 	if (err == 0 && orig_ifindex != 0) {
10780 		ipif->ipif_orig_ifindex = orig_ifindex;
10781 		if (found_sep) {
10782 			ipif->ipif_orig_ipifid = id;
10783 		}
10784 	}
10785 
10786 done:
10787 	ipsq_exit(ipsq);
10788 	return (err);
10789 }
10790 
10791 /*
10792  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10793  * interface) delete it based on the IP address (on this physical interface).
10794  * Otherwise delete it based on the ipif_id.
10795  * Also, special handling to allow a removeif of lo0.
10796  */
10797 /* ARGSUSED */
10798 int
10799 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10800     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10801 {
10802 	conn_t		*connp;
10803 	ill_t		*ill = ipif->ipif_ill;
10804 	boolean_t	 success;
10805 	ip_stack_t	*ipst;
10806 
10807 	ipst = CONNQ_TO_IPST(q);
10808 
10809 	ASSERT(q->q_next == NULL);
10810 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10811 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10812 	ASSERT(IAM_WRITER_IPIF(ipif));
10813 
10814 	connp = Q_TO_CONN(q);
10815 	/*
10816 	 * Special case for unplumbing lo0 (the loopback physical interface).
10817 	 * If unplumbing lo0, the incoming address structure has been
10818 	 * initialized to all zeros. When unplumbing lo0, all its logical
10819 	 * interfaces must be removed too.
10820 	 *
10821 	 * Note that this interface may be called to remove a specific
10822 	 * loopback logical interface (eg, lo0:1). But in that case
10823 	 * ipif->ipif_id != 0 so that the code path for that case is the
10824 	 * same as any other interface (meaning it skips the code directly
10825 	 * below).
10826 	 */
10827 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10828 		if (sin->sin_family == AF_UNSPEC &&
10829 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10830 			/*
10831 			 * Mark it condemned. No new ref. will be made to ill.
10832 			 */
10833 			mutex_enter(&ill->ill_lock);
10834 			ill->ill_state_flags |= ILL_CONDEMNED;
10835 			for (ipif = ill->ill_ipif; ipif != NULL;
10836 			    ipif = ipif->ipif_next) {
10837 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10838 			}
10839 			mutex_exit(&ill->ill_lock);
10840 
10841 			ipif = ill->ill_ipif;
10842 			/* unplumb the loopback interface */
10843 			ill_delete(ill);
10844 			mutex_enter(&connp->conn_lock);
10845 			mutex_enter(&ill->ill_lock);
10846 			ASSERT(ill->ill_group == NULL);
10847 
10848 			/* Are any references to this ill active */
10849 			if (ill_is_freeable(ill)) {
10850 				mutex_exit(&ill->ill_lock);
10851 				mutex_exit(&connp->conn_lock);
10852 				ill_delete_tail(ill);
10853 				mutex_enter(&ill->ill_lock);
10854 				ill_nic_info_dispatch(ill);
10855 				mutex_exit(&ill->ill_lock);
10856 				mi_free(ill);
10857 				return (0);
10858 			}
10859 			success = ipsq_pending_mp_add(connp, ipif,
10860 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10861 			mutex_exit(&connp->conn_lock);
10862 			mutex_exit(&ill->ill_lock);
10863 			if (success)
10864 				return (EINPROGRESS);
10865 			else
10866 				return (EINTR);
10867 		}
10868 	}
10869 
10870 	/*
10871 	 * We are exclusive on the ipsq, so an ill move will be serialized
10872 	 * before or after us.
10873 	 */
10874 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10875 
10876 	if (ipif->ipif_id == 0) {
10877 
10878 		ipsq_t *ipsq;
10879 
10880 		/* Find based on address */
10881 		if (ipif->ipif_isv6) {
10882 			sin6_t *sin6;
10883 
10884 			if (sin->sin_family != AF_INET6)
10885 				return (EAFNOSUPPORT);
10886 
10887 			sin6 = (sin6_t *)sin;
10888 			/* We are a writer, so we should be able to lookup */
10889 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10890 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10891 			if (ipif == NULL) {
10892 				/*
10893 				 * Maybe the address in on another interface in
10894 				 * the same IPMP group? We check this below.
10895 				 */
10896 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10897 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10898 				    ipst);
10899 			}
10900 		} else {
10901 			ipaddr_t addr;
10902 
10903 			if (sin->sin_family != AF_INET)
10904 				return (EAFNOSUPPORT);
10905 
10906 			addr = sin->sin_addr.s_addr;
10907 			/* We are a writer, so we should be able to lookup */
10908 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10909 			    NULL, NULL, NULL, ipst);
10910 			if (ipif == NULL) {
10911 				/*
10912 				 * Maybe the address in on another interface in
10913 				 * the same IPMP group? We check this below.
10914 				 */
10915 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10916 				    NULL, NULL, NULL, NULL, ipst);
10917 			}
10918 		}
10919 		if (ipif == NULL) {
10920 			return (EADDRNOTAVAIL);
10921 		}
10922 
10923 		/*
10924 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10925 		 * lifr_name of the physical interface but with an ip address
10926 		 * lifr_addr of a logical interface plumbed over it.
10927 		 * So update ipsq_current_ipif once ipif points to the
10928 		 * correct interface after doing ipif_lookup_addr().
10929 		 */
10930 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10931 		ASSERT(ipsq != NULL);
10932 
10933 		mutex_enter(&ipsq->ipsq_lock);
10934 		ipsq->ipsq_current_ipif = ipif;
10935 		mutex_exit(&ipsq->ipsq_lock);
10936 
10937 		/*
10938 		 * When the address to be removed is hosted on a different
10939 		 * interface, we check if the interface is in the same IPMP
10940 		 * group as the specified one; if so we proceed with the
10941 		 * removal.
10942 		 * ill->ill_group is NULL when the ill is down, so we have to
10943 		 * compare the group names instead.
10944 		 */
10945 		if (ipif->ipif_ill != ill &&
10946 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10947 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10948 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10949 		    ill->ill_phyint->phyint_groupname) != 0)) {
10950 			ipif_refrele(ipif);
10951 			return (EADDRNOTAVAIL);
10952 		}
10953 
10954 		/* This is a writer */
10955 		ipif_refrele(ipif);
10956 	}
10957 
10958 	/*
10959 	 * Can not delete instance zero since it is tied to the ill.
10960 	 */
10961 	if (ipif->ipif_id == 0)
10962 		return (EBUSY);
10963 
10964 	mutex_enter(&ill->ill_lock);
10965 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10966 	mutex_exit(&ill->ill_lock);
10967 
10968 	ipif_free(ipif);
10969 
10970 	mutex_enter(&connp->conn_lock);
10971 	mutex_enter(&ill->ill_lock);
10972 
10973 
10974 	/* Are any references to this ipif active */
10975 	if (ipif_is_freeable(ipif)) {
10976 		mutex_exit(&ill->ill_lock);
10977 		mutex_exit(&connp->conn_lock);
10978 		ipif_non_duplicate(ipif);
10979 		ipif_down_tail(ipif);
10980 		ipif_free_tail(ipif); /* frees ipif */
10981 		return (0);
10982 	}
10983 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10984 	    IPIF_FREE);
10985 	mutex_exit(&ill->ill_lock);
10986 	mutex_exit(&connp->conn_lock);
10987 	if (success)
10988 		return (EINPROGRESS);
10989 	else
10990 		return (EINTR);
10991 }
10992 
10993 /*
10994  * Restart the removeif ioctl. The refcnt has gone down to 0.
10995  * The ipif is already condemned. So can't find it thru lookups.
10996  */
10997 /* ARGSUSED */
10998 int
10999 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11000     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11001 {
11002 	ill_t *ill = ipif->ipif_ill;
11003 
11004 	ASSERT(IAM_WRITER_IPIF(ipif));
11005 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11006 
11007 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11008 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11009 
11010 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11011 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
11012 		ill_delete_tail(ill);
11013 		mutex_enter(&ill->ill_lock);
11014 		ill_nic_info_dispatch(ill);
11015 		mutex_exit(&ill->ill_lock);
11016 		mi_free(ill);
11017 		return (0);
11018 	}
11019 
11020 	ipif_non_duplicate(ipif);
11021 	ipif_down_tail(ipif);
11022 	ipif_free_tail(ipif);
11023 
11024 	ILL_UNMARK_CHANGING(ill);
11025 	return (0);
11026 }
11027 
11028 /*
11029  * Set the local interface address.
11030  * Allow an address of all zero when the interface is down.
11031  */
11032 /* ARGSUSED */
11033 int
11034 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11035     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11036 {
11037 	int err = 0;
11038 	in6_addr_t v6addr;
11039 	boolean_t need_up = B_FALSE;
11040 
11041 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11042 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11043 
11044 	ASSERT(IAM_WRITER_IPIF(ipif));
11045 
11046 	if (ipif->ipif_isv6) {
11047 		sin6_t *sin6;
11048 		ill_t *ill;
11049 		phyint_t *phyi;
11050 
11051 		if (sin->sin_family != AF_INET6)
11052 			return (EAFNOSUPPORT);
11053 
11054 		sin6 = (sin6_t *)sin;
11055 		v6addr = sin6->sin6_addr;
11056 		ill = ipif->ipif_ill;
11057 		phyi = ill->ill_phyint;
11058 
11059 		/*
11060 		 * Enforce that true multicast interfaces have a link-local
11061 		 * address for logical unit 0.
11062 		 */
11063 		if (ipif->ipif_id == 0 &&
11064 		    (ill->ill_flags & ILLF_MULTICAST) &&
11065 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11066 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11067 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11068 			return (EADDRNOTAVAIL);
11069 		}
11070 
11071 		/*
11072 		 * up interfaces shouldn't have the unspecified address
11073 		 * unless they also have the IPIF_NOLOCAL flags set and
11074 		 * have a subnet assigned.
11075 		 */
11076 		if ((ipif->ipif_flags & IPIF_UP) &&
11077 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11078 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11079 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11080 			return (EADDRNOTAVAIL);
11081 		}
11082 
11083 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11084 			return (EADDRNOTAVAIL);
11085 	} else {
11086 		ipaddr_t addr;
11087 
11088 		if (sin->sin_family != AF_INET)
11089 			return (EAFNOSUPPORT);
11090 
11091 		addr = sin->sin_addr.s_addr;
11092 
11093 		/* Allow 0 as the local address. */
11094 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11095 			return (EADDRNOTAVAIL);
11096 
11097 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11098 	}
11099 
11100 	/*
11101 	 * Even if there is no change we redo things just to rerun
11102 	 * ipif_set_default.
11103 	 */
11104 	if (ipif->ipif_flags & IPIF_UP) {
11105 		/*
11106 		 * Setting a new local address, make sure
11107 		 * we have net and subnet bcast ire's for
11108 		 * the old address if we need them.
11109 		 */
11110 		if (!ipif->ipif_isv6)
11111 			ipif_check_bcast_ires(ipif);
11112 		/*
11113 		 * If the interface is already marked up,
11114 		 * we call ipif_down which will take care
11115 		 * of ditching any IREs that have been set
11116 		 * up based on the old interface address.
11117 		 */
11118 		err = ipif_logical_down(ipif, q, mp);
11119 		if (err == EINPROGRESS)
11120 			return (err);
11121 		ipif_down_tail(ipif);
11122 		need_up = 1;
11123 	}
11124 
11125 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11126 	return (err);
11127 }
11128 
11129 int
11130 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11131     boolean_t need_up)
11132 {
11133 	in6_addr_t v6addr;
11134 	in6_addr_t ov6addr;
11135 	ipaddr_t addr;
11136 	sin6_t	*sin6;
11137 	int	sinlen;
11138 	int	err = 0;
11139 	ill_t	*ill = ipif->ipif_ill;
11140 	boolean_t need_dl_down;
11141 	boolean_t need_arp_down;
11142 	struct iocblk *iocp;
11143 
11144 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11145 
11146 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11147 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11148 	ASSERT(IAM_WRITER_IPIF(ipif));
11149 
11150 	/* Must cancel any pending timer before taking the ill_lock */
11151 	if (ipif->ipif_recovery_id != 0)
11152 		(void) untimeout(ipif->ipif_recovery_id);
11153 	ipif->ipif_recovery_id = 0;
11154 
11155 	if (ipif->ipif_isv6) {
11156 		sin6 = (sin6_t *)sin;
11157 		v6addr = sin6->sin6_addr;
11158 		sinlen = sizeof (struct sockaddr_in6);
11159 	} else {
11160 		addr = sin->sin_addr.s_addr;
11161 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11162 		sinlen = sizeof (struct sockaddr_in);
11163 	}
11164 	mutex_enter(&ill->ill_lock);
11165 	ov6addr = ipif->ipif_v6lcl_addr;
11166 	ipif->ipif_v6lcl_addr = v6addr;
11167 	sctp_update_ipif_addr(ipif, ov6addr);
11168 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11169 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11170 	} else {
11171 		ipif->ipif_v6src_addr = v6addr;
11172 	}
11173 	ipif->ipif_addr_ready = 0;
11174 
11175 	/*
11176 	 * If the interface was previously marked as a duplicate, then since
11177 	 * we've now got a "new" address, it should no longer be considered a
11178 	 * duplicate -- even if the "new" address is the same as the old one.
11179 	 * Note that if all ipifs are down, we may have a pending ARP down
11180 	 * event to handle.  This is because we want to recover from duplicates
11181 	 * and thus delay tearing down ARP until the duplicates have been
11182 	 * removed or disabled.
11183 	 */
11184 	need_dl_down = need_arp_down = B_FALSE;
11185 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11186 		need_arp_down = !need_up;
11187 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11188 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11189 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11190 			need_dl_down = B_TRUE;
11191 		}
11192 	}
11193 
11194 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11195 	    !ill->ill_is_6to4tun) {
11196 		queue_t *wqp = ill->ill_wq;
11197 
11198 		/*
11199 		 * The local address of this interface is a 6to4 address,
11200 		 * check if this interface is in fact a 6to4 tunnel or just
11201 		 * an interface configured with a 6to4 address.  We are only
11202 		 * interested in the former.
11203 		 */
11204 		if (wqp != NULL) {
11205 			while ((wqp->q_next != NULL) &&
11206 			    (wqp->q_next->q_qinfo != NULL) &&
11207 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11208 
11209 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11210 				    == TUN6TO4_MODID) {
11211 					/* set for use in IP */
11212 					ill->ill_is_6to4tun = 1;
11213 					break;
11214 				}
11215 				wqp = wqp->q_next;
11216 			}
11217 		}
11218 	}
11219 
11220 	ipif_set_default(ipif);
11221 
11222 	/*
11223 	 * When publishing an interface address change event, we only notify
11224 	 * the event listeners of the new address.  It is assumed that if they
11225 	 * actively care about the addresses assigned that they will have
11226 	 * already discovered the previous address assigned (if there was one.)
11227 	 *
11228 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11229 	 */
11230 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11231 		(void) ill_hook_event_create(ill, MAP_IPIF_ID(ipif->ipif_id),
11232 		    NE_ADDRESS_CHANGE, sin, sinlen);
11233 	}
11234 
11235 	mutex_exit(&ill->ill_lock);
11236 
11237 	if (need_up) {
11238 		/*
11239 		 * Now bring the interface back up.  If this
11240 		 * is the only IPIF for the ILL, ipif_up
11241 		 * will have to re-bind to the device, so
11242 		 * we may get back EINPROGRESS, in which
11243 		 * case, this IOCTL will get completed in
11244 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11245 		 */
11246 		err = ipif_up(ipif, q, mp);
11247 	}
11248 
11249 	if (need_dl_down)
11250 		ill_dl_down(ill);
11251 	if (need_arp_down)
11252 		ipif_arp_down(ipif);
11253 
11254 	return (err);
11255 }
11256 
11257 
11258 /*
11259  * Restart entry point to restart the address set operation after the
11260  * refcounts have dropped to zero.
11261  */
11262 /* ARGSUSED */
11263 int
11264 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11265     ip_ioctl_cmd_t *ipip, void *ifreq)
11266 {
11267 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11268 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11269 	ASSERT(IAM_WRITER_IPIF(ipif));
11270 	ipif_down_tail(ipif);
11271 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11272 }
11273 
11274 /* ARGSUSED */
11275 int
11276 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11277     ip_ioctl_cmd_t *ipip, void *if_req)
11278 {
11279 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11280 	struct lifreq *lifr = (struct lifreq *)if_req;
11281 
11282 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11283 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11284 	/*
11285 	 * The net mask and address can't change since we have a
11286 	 * reference to the ipif. So no lock is necessary.
11287 	 */
11288 	if (ipif->ipif_isv6) {
11289 		*sin6 = sin6_null;
11290 		sin6->sin6_family = AF_INET6;
11291 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11292 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11293 		lifr->lifr_addrlen =
11294 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11295 	} else {
11296 		*sin = sin_null;
11297 		sin->sin_family = AF_INET;
11298 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11299 		if (ipip->ipi_cmd_type == LIF_CMD) {
11300 			lifr->lifr_addrlen =
11301 			    ip_mask_to_plen(ipif->ipif_net_mask);
11302 		}
11303 	}
11304 	return (0);
11305 }
11306 
11307 /*
11308  * Set the destination address for a pt-pt interface.
11309  */
11310 /* ARGSUSED */
11311 int
11312 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11313     ip_ioctl_cmd_t *ipip, void *if_req)
11314 {
11315 	int err = 0;
11316 	in6_addr_t v6addr;
11317 	boolean_t need_up = B_FALSE;
11318 
11319 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11320 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11321 	ASSERT(IAM_WRITER_IPIF(ipif));
11322 
11323 	if (ipif->ipif_isv6) {
11324 		sin6_t *sin6;
11325 
11326 		if (sin->sin_family != AF_INET6)
11327 			return (EAFNOSUPPORT);
11328 
11329 		sin6 = (sin6_t *)sin;
11330 		v6addr = sin6->sin6_addr;
11331 
11332 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11333 			return (EADDRNOTAVAIL);
11334 	} else {
11335 		ipaddr_t addr;
11336 
11337 		if (sin->sin_family != AF_INET)
11338 			return (EAFNOSUPPORT);
11339 
11340 		addr = sin->sin_addr.s_addr;
11341 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11342 			return (EADDRNOTAVAIL);
11343 
11344 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11345 	}
11346 
11347 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11348 		return (0);	/* No change */
11349 
11350 	if (ipif->ipif_flags & IPIF_UP) {
11351 		/*
11352 		 * If the interface is already marked up,
11353 		 * we call ipif_down which will take care
11354 		 * of ditching any IREs that have been set
11355 		 * up based on the old pp dst address.
11356 		 */
11357 		err = ipif_logical_down(ipif, q, mp);
11358 		if (err == EINPROGRESS)
11359 			return (err);
11360 		ipif_down_tail(ipif);
11361 		need_up = B_TRUE;
11362 	}
11363 	/*
11364 	 * could return EINPROGRESS. If so ioctl will complete in
11365 	 * ip_rput_dlpi_writer
11366 	 */
11367 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11368 	return (err);
11369 }
11370 
11371 static int
11372 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11373     boolean_t need_up)
11374 {
11375 	in6_addr_t v6addr;
11376 	ill_t	*ill = ipif->ipif_ill;
11377 	int	err = 0;
11378 	boolean_t need_dl_down;
11379 	boolean_t need_arp_down;
11380 
11381 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11382 	    ipif->ipif_id, (void *)ipif));
11383 
11384 	/* Must cancel any pending timer before taking the ill_lock */
11385 	if (ipif->ipif_recovery_id != 0)
11386 		(void) untimeout(ipif->ipif_recovery_id);
11387 	ipif->ipif_recovery_id = 0;
11388 
11389 	if (ipif->ipif_isv6) {
11390 		sin6_t *sin6;
11391 
11392 		sin6 = (sin6_t *)sin;
11393 		v6addr = sin6->sin6_addr;
11394 	} else {
11395 		ipaddr_t addr;
11396 
11397 		addr = sin->sin_addr.s_addr;
11398 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11399 	}
11400 	mutex_enter(&ill->ill_lock);
11401 	/* Set point to point destination address. */
11402 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11403 		/*
11404 		 * Allow this as a means of creating logical
11405 		 * pt-pt interfaces on top of e.g. an Ethernet.
11406 		 * XXX Undocumented HACK for testing.
11407 		 * pt-pt interfaces are created with NUD disabled.
11408 		 */
11409 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11410 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11411 		if (ipif->ipif_isv6)
11412 			ill->ill_flags |= ILLF_NONUD;
11413 	}
11414 
11415 	/*
11416 	 * If the interface was previously marked as a duplicate, then since
11417 	 * we've now got a "new" address, it should no longer be considered a
11418 	 * duplicate -- even if the "new" address is the same as the old one.
11419 	 * Note that if all ipifs are down, we may have a pending ARP down
11420 	 * event to handle.
11421 	 */
11422 	need_dl_down = need_arp_down = B_FALSE;
11423 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11424 		need_arp_down = !need_up;
11425 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11426 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11427 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11428 			need_dl_down = B_TRUE;
11429 		}
11430 	}
11431 
11432 	/* Set the new address. */
11433 	ipif->ipif_v6pp_dst_addr = v6addr;
11434 	/* Make sure subnet tracks pp_dst */
11435 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11436 	mutex_exit(&ill->ill_lock);
11437 
11438 	if (need_up) {
11439 		/*
11440 		 * Now bring the interface back up.  If this
11441 		 * is the only IPIF for the ILL, ipif_up
11442 		 * will have to re-bind to the device, so
11443 		 * we may get back EINPROGRESS, in which
11444 		 * case, this IOCTL will get completed in
11445 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11446 		 */
11447 		err = ipif_up(ipif, q, mp);
11448 	}
11449 
11450 	if (need_dl_down)
11451 		ill_dl_down(ill);
11452 
11453 	if (need_arp_down)
11454 		ipif_arp_down(ipif);
11455 	return (err);
11456 }
11457 
11458 /*
11459  * Restart entry point to restart the dstaddress set operation after the
11460  * refcounts have dropped to zero.
11461  */
11462 /* ARGSUSED */
11463 int
11464 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11465     ip_ioctl_cmd_t *ipip, void *ifreq)
11466 {
11467 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11468 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11469 	ipif_down_tail(ipif);
11470 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11471 }
11472 
11473 /* ARGSUSED */
11474 int
11475 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11476     ip_ioctl_cmd_t *ipip, void *if_req)
11477 {
11478 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11479 
11480 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11481 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11482 	/*
11483 	 * Get point to point destination address. The addresses can't
11484 	 * change since we hold a reference to the ipif.
11485 	 */
11486 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11487 		return (EADDRNOTAVAIL);
11488 
11489 	if (ipif->ipif_isv6) {
11490 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11491 		*sin6 = sin6_null;
11492 		sin6->sin6_family = AF_INET6;
11493 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11494 	} else {
11495 		*sin = sin_null;
11496 		sin->sin_family = AF_INET;
11497 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11498 	}
11499 	return (0);
11500 }
11501 
11502 /*
11503  * part of ipmp, make this func return the active/inactive state and
11504  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11505  */
11506 /*
11507  * This function either sets or clears the IFF_INACTIVE flag.
11508  *
11509  * As long as there are some addresses or multicast memberships on the
11510  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11511  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11512  * will be used for outbound packets.
11513  *
11514  * Caller needs to verify the validity of setting IFF_INACTIVE.
11515  */
11516 static void
11517 phyint_inactive(phyint_t *phyi)
11518 {
11519 	ill_t *ill_v4;
11520 	ill_t *ill_v6;
11521 	ipif_t *ipif;
11522 	ilm_t *ilm;
11523 
11524 	ill_v4 = phyi->phyint_illv4;
11525 	ill_v6 = phyi->phyint_illv6;
11526 
11527 	/*
11528 	 * No need for a lock while traversing the list since iam
11529 	 * a writer
11530 	 */
11531 	if (ill_v4 != NULL) {
11532 		ASSERT(IAM_WRITER_ILL(ill_v4));
11533 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11534 		    ipif = ipif->ipif_next) {
11535 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11536 				mutex_enter(&phyi->phyint_lock);
11537 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11538 				mutex_exit(&phyi->phyint_lock);
11539 				return;
11540 			}
11541 		}
11542 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11543 		    ilm = ilm->ilm_next) {
11544 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11545 				mutex_enter(&phyi->phyint_lock);
11546 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11547 				mutex_exit(&phyi->phyint_lock);
11548 				return;
11549 			}
11550 		}
11551 	}
11552 	if (ill_v6 != NULL) {
11553 		ill_v6 = phyi->phyint_illv6;
11554 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11555 		    ipif = ipif->ipif_next) {
11556 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11557 				mutex_enter(&phyi->phyint_lock);
11558 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11559 				mutex_exit(&phyi->phyint_lock);
11560 				return;
11561 			}
11562 		}
11563 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11564 		    ilm = ilm->ilm_next) {
11565 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11566 				mutex_enter(&phyi->phyint_lock);
11567 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11568 				mutex_exit(&phyi->phyint_lock);
11569 				return;
11570 			}
11571 		}
11572 	}
11573 	mutex_enter(&phyi->phyint_lock);
11574 	phyi->phyint_flags |= PHYI_INACTIVE;
11575 	mutex_exit(&phyi->phyint_lock);
11576 }
11577 
11578 /*
11579  * This function is called only when the phyint flags change. Currently
11580  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11581  * that we can select a good ill.
11582  */
11583 static void
11584 ip_redo_nomination(phyint_t *phyi)
11585 {
11586 	ill_t *ill_v4;
11587 
11588 	ill_v4 = phyi->phyint_illv4;
11589 
11590 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11591 		ASSERT(IAM_WRITER_ILL(ill_v4));
11592 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11593 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11594 	}
11595 }
11596 
11597 /*
11598  * Heuristic to check if ill is INACTIVE.
11599  * Checks if ill has an ipif with an usable ip address.
11600  *
11601  * Return values:
11602  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11603  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11604  */
11605 static boolean_t
11606 ill_is_inactive(ill_t *ill)
11607 {
11608 	ipif_t *ipif;
11609 
11610 	/* Check whether it is in an IPMP group */
11611 	if (ill->ill_phyint->phyint_groupname == NULL)
11612 		return (B_FALSE);
11613 
11614 	if (ill->ill_ipif_up_count == 0)
11615 		return (B_TRUE);
11616 
11617 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11618 		uint64_t flags = ipif->ipif_flags;
11619 
11620 		/*
11621 		 * This ipif is usable if it is IPIF_UP and not a
11622 		 * dedicated test address.  A dedicated test address
11623 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11624 		 * (note in particular that V6 test addresses are
11625 		 * link-local data addresses and thus are marked
11626 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11627 		 */
11628 		if ((flags & IPIF_UP) &&
11629 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11630 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11631 			return (B_FALSE);
11632 	}
11633 	return (B_TRUE);
11634 }
11635 
11636 /*
11637  * Set interface flags.
11638  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11639  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11640  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11641  *
11642  * NOTE : We really don't enforce that ipif_id zero should be used
11643  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11644  *	  is because applications generally does SICGLIFFLAGS and
11645  *	  ORs in the new flags (that affects the logical) and does a
11646  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11647  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11648  *	  flags that will be turned on is correct with respect to
11649  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11650  */
11651 /* ARGSUSED */
11652 int
11653 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11654     ip_ioctl_cmd_t *ipip, void *if_req)
11655 {
11656 	uint64_t turn_on;
11657 	uint64_t turn_off;
11658 	int	err;
11659 	phyint_t *phyi;
11660 	ill_t *ill;
11661 	uint64_t intf_flags;
11662 	boolean_t phyint_flags_modified = B_FALSE;
11663 	uint64_t flags;
11664 	struct ifreq *ifr;
11665 	struct lifreq *lifr;
11666 	boolean_t set_linklocal = B_FALSE;
11667 	boolean_t zero_source = B_FALSE;
11668 	ip_stack_t *ipst;
11669 
11670 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11671 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11672 
11673 	ASSERT(IAM_WRITER_IPIF(ipif));
11674 
11675 	ill = ipif->ipif_ill;
11676 	phyi = ill->ill_phyint;
11677 	ipst = ill->ill_ipst;
11678 
11679 	if (ipip->ipi_cmd_type == IF_CMD) {
11680 		ifr = (struct ifreq *)if_req;
11681 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11682 	} else {
11683 		lifr = (struct lifreq *)if_req;
11684 		flags = lifr->lifr_flags;
11685 	}
11686 
11687 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11688 
11689 	/*
11690 	 * Have the flags been set correctly until now?
11691 	 */
11692 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11693 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11694 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11695 	/*
11696 	 * Compare the new flags to the old, and partition
11697 	 * into those coming on and those going off.
11698 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11699 	 */
11700 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11701 		flags |= intf_flags & ~0xFFFF;
11702 
11703 	/*
11704 	 * First check which bits will change and then which will
11705 	 * go on and off
11706 	 */
11707 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11708 	if (!turn_on)
11709 		return (0);	/* No change */
11710 
11711 	turn_off = intf_flags & turn_on;
11712 	turn_on ^= turn_off;
11713 	err = 0;
11714 
11715 	/*
11716 	 * Don't allow any bits belonging to the logical interface
11717 	 * to be set or cleared on the replacement ipif that was
11718 	 * created temporarily during a MOVE.
11719 	 */
11720 	if (ipif->ipif_replace_zero &&
11721 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11722 		return (EINVAL);
11723 	}
11724 
11725 	/*
11726 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11727 	 * IPv6 interfaces.
11728 	 */
11729 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11730 		return (EINVAL);
11731 
11732 	/*
11733 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11734 	 */
11735 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11736 		return (EINVAL);
11737 
11738 	/*
11739 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11740 	 * interfaces.  It makes no sense in that context.
11741 	 */
11742 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11743 		return (EINVAL);
11744 
11745 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11746 		zero_source = B_TRUE;
11747 
11748 	/*
11749 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11750 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11751 	 * If the link local address isn't set, and can be set, it will get
11752 	 * set later on in this function.
11753 	 */
11754 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11755 	    (flags & IFF_UP) && !zero_source &&
11756 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11757 		if (ipif_cant_setlinklocal(ipif))
11758 			return (EINVAL);
11759 		set_linklocal = B_TRUE;
11760 	}
11761 
11762 	/*
11763 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11764 	 * same time. No need to grab ill_g_usesrc_lock here, see
11765 	 * synchronization notes in ip.c
11766 	 */
11767 	if (turn_on & PHYI_STANDBY &&
11768 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11769 		return (EINVAL);
11770 	}
11771 
11772 	/*
11773 	 * If we modify physical interface flags, we'll potentially need to
11774 	 * send up two routing socket messages for the changes (one for the
11775 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11776 	 */
11777 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11778 		phyint_flags_modified = B_TRUE;
11779 
11780 	/*
11781 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11782 	 * we need to flush the IRE_CACHES belonging to this ill.
11783 	 * We handle this case here without doing the DOWN/UP dance
11784 	 * like it is done for other flags. If some other flags are
11785 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11786 	 * below will handle it by bringing it down and then
11787 	 * bringing it UP.
11788 	 */
11789 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11790 		ill_t *ill_v4, *ill_v6;
11791 
11792 		ill_v4 = phyi->phyint_illv4;
11793 		ill_v6 = phyi->phyint_illv6;
11794 
11795 		/*
11796 		 * First set the INACTIVE flag if needed. Then delete the ires.
11797 		 * ire_add will atomically prevent creating new IRE_CACHEs
11798 		 * unless hidden flag is set.
11799 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11800 		 */
11801 		if ((turn_on & PHYI_FAILED) &&
11802 		    ((intf_flags & PHYI_STANDBY) ||
11803 		    !ipst->ips_ipmp_enable_failback)) {
11804 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11805 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11806 		}
11807 		if ((turn_off & PHYI_FAILED) &&
11808 		    ((intf_flags & PHYI_STANDBY) ||
11809 		    (!ipst->ips_ipmp_enable_failback &&
11810 		    ill_is_inactive(ill)))) {
11811 			phyint_inactive(phyi);
11812 		}
11813 
11814 		if (turn_on & PHYI_STANDBY) {
11815 			/*
11816 			 * We implicitly set INACTIVE only when STANDBY is set.
11817 			 * INACTIVE is also set on non-STANDBY phyint when user
11818 			 * disables FAILBACK using configuration file.
11819 			 * Do not allow STANDBY to be set on such INACTIVE
11820 			 * phyint
11821 			 */
11822 			if (phyi->phyint_flags & PHYI_INACTIVE)
11823 				return (EINVAL);
11824 			if (!(phyi->phyint_flags & PHYI_FAILED))
11825 				phyint_inactive(phyi);
11826 		}
11827 		if (turn_off & PHYI_STANDBY) {
11828 			if (ipst->ips_ipmp_enable_failback) {
11829 				/*
11830 				 * Reset PHYI_INACTIVE.
11831 				 */
11832 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11833 			} else if (ill_is_inactive(ill) &&
11834 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11835 				/*
11836 				 * Need to set INACTIVE, when user sets
11837 				 * STANDBY on a non-STANDBY phyint and
11838 				 * later resets STANDBY
11839 				 */
11840 				phyint_inactive(phyi);
11841 			}
11842 		}
11843 		/*
11844 		 * We should always send up a message so that the
11845 		 * daemons come to know of it. Note that the zeroth
11846 		 * interface can be down and the check below for IPIF_UP
11847 		 * will not make sense as we are actually setting
11848 		 * a phyint flag here. We assume that the ipif used
11849 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11850 		 * send up any message for non-zero ipifs).
11851 		 */
11852 		phyint_flags_modified = B_TRUE;
11853 
11854 		if (ill_v4 != NULL) {
11855 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11856 			    IRE_CACHE, ill_stq_cache_delete,
11857 			    (char *)ill_v4, ill_v4);
11858 			illgrp_reset_schednext(ill_v4);
11859 		}
11860 		if (ill_v6 != NULL) {
11861 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11862 			    IRE_CACHE, ill_stq_cache_delete,
11863 			    (char *)ill_v6, ill_v6);
11864 			illgrp_reset_schednext(ill_v6);
11865 		}
11866 	}
11867 
11868 	/*
11869 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11870 	 * status of the interface and, if the interface is part of an IPMP
11871 	 * group, all other interfaces that are part of the same IPMP
11872 	 * group.
11873 	 */
11874 	if ((turn_on | turn_off) & ILLF_ROUTER)
11875 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11876 
11877 	/*
11878 	 * If the interface is not UP and we are not going to
11879 	 * bring it UP, record the flags and return. When the
11880 	 * interface comes UP later, the right actions will be
11881 	 * taken.
11882 	 */
11883 	if (!(ipif->ipif_flags & IPIF_UP) &&
11884 	    !(turn_on & IPIF_UP)) {
11885 		/* Record new flags in their respective places. */
11886 		mutex_enter(&ill->ill_lock);
11887 		mutex_enter(&ill->ill_phyint->phyint_lock);
11888 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11889 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11890 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11891 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11892 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11893 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11894 		mutex_exit(&ill->ill_lock);
11895 		mutex_exit(&ill->ill_phyint->phyint_lock);
11896 
11897 		/*
11898 		 * We do the broadcast and nomination here rather
11899 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11900 		 * the case of FAILBACK from INACTIVE standby to the
11901 		 * interface that has been repaired, PHYI_FAILED has not
11902 		 * been cleared yet. If there are only two interfaces in
11903 		 * that group, all we have is a FAILED and INACTIVE
11904 		 * interface. If we do the nomination soon after a failback,
11905 		 * the broadcast nomination code would select the
11906 		 * INACTIVE interface for receiving broadcasts as FAILED is
11907 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11908 		 * receive broadcast packets, we need to redo nomination
11909 		 * when the FAILED is cleared here. Thus, in general we
11910 		 * always do the nomination here for FAILED, STANDBY
11911 		 * and OFFLINE.
11912 		 */
11913 		if (((turn_on | turn_off) &
11914 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11915 			ip_redo_nomination(phyi);
11916 		}
11917 		if (phyint_flags_modified) {
11918 			if (phyi->phyint_illv4 != NULL) {
11919 				ip_rts_ifmsg(phyi->phyint_illv4->
11920 				    ill_ipif);
11921 			}
11922 			if (phyi->phyint_illv6 != NULL) {
11923 				ip_rts_ifmsg(phyi->phyint_illv6->
11924 				    ill_ipif);
11925 			}
11926 		}
11927 		return (0);
11928 	} else if (set_linklocal || zero_source) {
11929 		mutex_enter(&ill->ill_lock);
11930 		if (set_linklocal)
11931 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11932 		if (zero_source)
11933 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11934 		mutex_exit(&ill->ill_lock);
11935 	}
11936 
11937 	/*
11938 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11939 	 * or point-to-point interfaces with an unspecified destination. We do
11940 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11941 	 * have a subnet assigned, which is how in.ndpd currently manages its
11942 	 * onlink prefix list when no addresses are configured with those
11943 	 * prefixes.
11944 	 */
11945 	if (ipif->ipif_isv6 &&
11946 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11947 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11948 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11949 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11950 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11951 		return (EINVAL);
11952 	}
11953 
11954 	/*
11955 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11956 	 * from being brought up.
11957 	 */
11958 	if (!ipif->ipif_isv6 &&
11959 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11960 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11961 		return (EINVAL);
11962 	}
11963 
11964 	/*
11965 	 * The only flag changes that we currently take specific action on
11966 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11967 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11968 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11969 	 * the flags and bringing it back up again.
11970 	 */
11971 	if ((turn_on|turn_off) &
11972 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11973 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11974 		/*
11975 		 * Taking this ipif down, make sure we have
11976 		 * valid net and subnet bcast ire's for other
11977 		 * logical interfaces, if we need them.
11978 		 */
11979 		if (!ipif->ipif_isv6)
11980 			ipif_check_bcast_ires(ipif);
11981 
11982 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11983 		    !(turn_off & IPIF_UP)) {
11984 			if (ipif->ipif_flags & IPIF_UP)
11985 				ill->ill_logical_down = 1;
11986 			turn_on &= ~IPIF_UP;
11987 		}
11988 		err = ipif_down(ipif, q, mp);
11989 		ip1dbg(("ipif_down returns %d err ", err));
11990 		if (err == EINPROGRESS)
11991 			return (err);
11992 		ipif_down_tail(ipif);
11993 	}
11994 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11995 }
11996 
11997 static int
11998 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
11999 {
12000 	ill_t	*ill;
12001 	phyint_t *phyi;
12002 	uint64_t turn_on;
12003 	uint64_t turn_off;
12004 	uint64_t intf_flags;
12005 	boolean_t phyint_flags_modified = B_FALSE;
12006 	int	err = 0;
12007 	boolean_t set_linklocal = B_FALSE;
12008 	boolean_t zero_source = B_FALSE;
12009 
12010 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12011 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12012 
12013 	ASSERT(IAM_WRITER_IPIF(ipif));
12014 
12015 	ill = ipif->ipif_ill;
12016 	phyi = ill->ill_phyint;
12017 
12018 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12019 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12020 
12021 	turn_off = intf_flags & turn_on;
12022 	turn_on ^= turn_off;
12023 
12024 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12025 		phyint_flags_modified = B_TRUE;
12026 
12027 	/*
12028 	 * Now we change the flags. Track current value of
12029 	 * other flags in their respective places.
12030 	 */
12031 	mutex_enter(&ill->ill_lock);
12032 	mutex_enter(&phyi->phyint_lock);
12033 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12034 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12035 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12036 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12037 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12038 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12039 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12040 		set_linklocal = B_TRUE;
12041 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12042 	}
12043 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12044 		zero_source = B_TRUE;
12045 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12046 	}
12047 	mutex_exit(&ill->ill_lock);
12048 	mutex_exit(&phyi->phyint_lock);
12049 
12050 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12051 		ip_redo_nomination(phyi);
12052 
12053 	if (set_linklocal)
12054 		(void) ipif_setlinklocal(ipif);
12055 
12056 	if (zero_source)
12057 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12058 	else
12059 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12060 
12061 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
12062 		/*
12063 		 * XXX ipif_up really does not know whether a phyint flags
12064 		 * was modified or not. So, it sends up information on
12065 		 * only one routing sockets message. As we don't bring up
12066 		 * the interface and also set STANDBY/FAILED simultaneously
12067 		 * it should be okay.
12068 		 */
12069 		err = ipif_up(ipif, q, mp);
12070 	} else {
12071 		/*
12072 		 * Make sure routing socket sees all changes to the flags.
12073 		 * ipif_up_done* handles this when we use ipif_up.
12074 		 */
12075 		if (phyint_flags_modified) {
12076 			if (phyi->phyint_illv4 != NULL) {
12077 				ip_rts_ifmsg(phyi->phyint_illv4->
12078 				    ill_ipif);
12079 			}
12080 			if (phyi->phyint_illv6 != NULL) {
12081 				ip_rts_ifmsg(phyi->phyint_illv6->
12082 				    ill_ipif);
12083 			}
12084 		} else {
12085 			ip_rts_ifmsg(ipif);
12086 		}
12087 		/*
12088 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12089 		 * this in need_up case.
12090 		 */
12091 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12092 	}
12093 	return (err);
12094 }
12095 
12096 /*
12097  * Restart the flags operation now that the refcounts have dropped to zero.
12098  */
12099 /* ARGSUSED */
12100 int
12101 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12102     ip_ioctl_cmd_t *ipip, void *if_req)
12103 {
12104 	uint64_t flags;
12105 	struct ifreq *ifr = if_req;
12106 	struct lifreq *lifr = if_req;
12107 
12108 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12109 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12110 
12111 	ipif_down_tail(ipif);
12112 	if (ipip->ipi_cmd_type == IF_CMD) {
12113 		/* cast to uint16_t prevents unwanted sign extension */
12114 		flags = (uint16_t)ifr->ifr_flags;
12115 	} else {
12116 		flags = lifr->lifr_flags;
12117 	}
12118 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
12119 }
12120 
12121 /*
12122  * Can operate on either a module or a driver queue.
12123  */
12124 /* ARGSUSED */
12125 int
12126 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12127     ip_ioctl_cmd_t *ipip, void *if_req)
12128 {
12129 	/*
12130 	 * Has the flags been set correctly till now ?
12131 	 */
12132 	ill_t *ill = ipif->ipif_ill;
12133 	phyint_t *phyi = ill->ill_phyint;
12134 
12135 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12136 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12137 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12138 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12139 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12140 
12141 	/*
12142 	 * Need a lock since some flags can be set even when there are
12143 	 * references to the ipif.
12144 	 */
12145 	mutex_enter(&ill->ill_lock);
12146 	if (ipip->ipi_cmd_type == IF_CMD) {
12147 		struct ifreq *ifr = (struct ifreq *)if_req;
12148 
12149 		/* Get interface flags (low 16 only). */
12150 		ifr->ifr_flags = ((ipif->ipif_flags |
12151 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12152 	} else {
12153 		struct lifreq *lifr = (struct lifreq *)if_req;
12154 
12155 		/* Get interface flags. */
12156 		lifr->lifr_flags = ipif->ipif_flags |
12157 		    ill->ill_flags | phyi->phyint_flags;
12158 	}
12159 	mutex_exit(&ill->ill_lock);
12160 	return (0);
12161 }
12162 
12163 /* ARGSUSED */
12164 int
12165 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12166     ip_ioctl_cmd_t *ipip, void *if_req)
12167 {
12168 	int mtu;
12169 	int ip_min_mtu;
12170 	struct ifreq	*ifr;
12171 	struct lifreq *lifr;
12172 	ire_t	*ire;
12173 	ip_stack_t *ipst;
12174 
12175 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12176 	    ipif->ipif_id, (void *)ipif));
12177 	if (ipip->ipi_cmd_type == IF_CMD) {
12178 		ifr = (struct ifreq *)if_req;
12179 		mtu = ifr->ifr_metric;
12180 	} else {
12181 		lifr = (struct lifreq *)if_req;
12182 		mtu = lifr->lifr_mtu;
12183 	}
12184 
12185 	if (ipif->ipif_isv6)
12186 		ip_min_mtu = IPV6_MIN_MTU;
12187 	else
12188 		ip_min_mtu = IP_MIN_MTU;
12189 
12190 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12191 		return (EINVAL);
12192 
12193 	/*
12194 	 * Change the MTU size in all relevant ire's.
12195 	 * Mtu change Vs. new ire creation - protocol below.
12196 	 * First change ipif_mtu and the ire_max_frag of the
12197 	 * interface ire. Then do an ire walk and change the
12198 	 * ire_max_frag of all affected ires. During ire_add
12199 	 * under the bucket lock, set the ire_max_frag of the
12200 	 * new ire being created from the ipif/ire from which
12201 	 * it is being derived. If an mtu change happens after
12202 	 * the ire is added, the new ire will be cleaned up.
12203 	 * Conversely if the mtu change happens before the ire
12204 	 * is added, ire_add will see the new value of the mtu.
12205 	 */
12206 	ipif->ipif_mtu = mtu;
12207 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12208 
12209 	if (ipif->ipif_isv6)
12210 		ire = ipif_to_ire_v6(ipif);
12211 	else
12212 		ire = ipif_to_ire(ipif);
12213 	if (ire != NULL) {
12214 		ire->ire_max_frag = ipif->ipif_mtu;
12215 		ire_refrele(ire);
12216 	}
12217 	ipst = ipif->ipif_ill->ill_ipst;
12218 	if (ipif->ipif_flags & IPIF_UP) {
12219 		if (ipif->ipif_isv6)
12220 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12221 			    ipst);
12222 		else
12223 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12224 			    ipst);
12225 	}
12226 	/* Update the MTU in SCTP's list */
12227 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12228 	return (0);
12229 }
12230 
12231 /* Get interface MTU. */
12232 /* ARGSUSED */
12233 int
12234 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12235 	ip_ioctl_cmd_t *ipip, void *if_req)
12236 {
12237 	struct ifreq	*ifr;
12238 	struct lifreq	*lifr;
12239 
12240 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12241 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12242 	if (ipip->ipi_cmd_type == IF_CMD) {
12243 		ifr = (struct ifreq *)if_req;
12244 		ifr->ifr_metric = ipif->ipif_mtu;
12245 	} else {
12246 		lifr = (struct lifreq *)if_req;
12247 		lifr->lifr_mtu = ipif->ipif_mtu;
12248 	}
12249 	return (0);
12250 }
12251 
12252 /* Set interface broadcast address. */
12253 /* ARGSUSED2 */
12254 int
12255 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12256 	ip_ioctl_cmd_t *ipip, void *if_req)
12257 {
12258 	ipaddr_t addr;
12259 	ire_t	*ire;
12260 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12261 
12262 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12263 	    ipif->ipif_id));
12264 
12265 	ASSERT(IAM_WRITER_IPIF(ipif));
12266 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12267 		return (EADDRNOTAVAIL);
12268 
12269 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12270 
12271 	if (sin->sin_family != AF_INET)
12272 		return (EAFNOSUPPORT);
12273 
12274 	addr = sin->sin_addr.s_addr;
12275 	if (ipif->ipif_flags & IPIF_UP) {
12276 		/*
12277 		 * If we are already up, make sure the new
12278 		 * broadcast address makes sense.  If it does,
12279 		 * there should be an IRE for it already.
12280 		 * Don't match on ipif, only on the ill
12281 		 * since we are sharing these now. Don't use
12282 		 * MATCH_IRE_ILL_GROUP as we are looking for
12283 		 * the broadcast ire on this ill and each ill
12284 		 * in the group has its own broadcast ire.
12285 		 */
12286 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12287 		    ipif, ALL_ZONES, NULL,
12288 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12289 		if (ire == NULL) {
12290 			return (EINVAL);
12291 		} else {
12292 			ire_refrele(ire);
12293 		}
12294 	}
12295 	/*
12296 	 * Changing the broadcast addr for this ipif.
12297 	 * Make sure we have valid net and subnet bcast
12298 	 * ire's for other logical interfaces, if needed.
12299 	 */
12300 	if (addr != ipif->ipif_brd_addr)
12301 		ipif_check_bcast_ires(ipif);
12302 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12303 	return (0);
12304 }
12305 
12306 /* Get interface broadcast address. */
12307 /* ARGSUSED */
12308 int
12309 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12310     ip_ioctl_cmd_t *ipip, void *if_req)
12311 {
12312 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12313 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12314 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12315 		return (EADDRNOTAVAIL);
12316 
12317 	/* IPIF_BROADCAST not possible with IPv6 */
12318 	ASSERT(!ipif->ipif_isv6);
12319 	*sin = sin_null;
12320 	sin->sin_family = AF_INET;
12321 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12322 	return (0);
12323 }
12324 
12325 /*
12326  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12327  */
12328 /* ARGSUSED */
12329 int
12330 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12331     ip_ioctl_cmd_t *ipip, void *if_req)
12332 {
12333 	int err = 0;
12334 	in6_addr_t v6mask;
12335 
12336 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12337 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12338 
12339 	ASSERT(IAM_WRITER_IPIF(ipif));
12340 
12341 	if (ipif->ipif_isv6) {
12342 		sin6_t *sin6;
12343 
12344 		if (sin->sin_family != AF_INET6)
12345 			return (EAFNOSUPPORT);
12346 
12347 		sin6 = (sin6_t *)sin;
12348 		v6mask = sin6->sin6_addr;
12349 	} else {
12350 		ipaddr_t mask;
12351 
12352 		if (sin->sin_family != AF_INET)
12353 			return (EAFNOSUPPORT);
12354 
12355 		mask = sin->sin_addr.s_addr;
12356 		V4MASK_TO_V6(mask, v6mask);
12357 	}
12358 
12359 	/*
12360 	 * No big deal if the interface isn't already up, or the mask
12361 	 * isn't really changing, or this is pt-pt.
12362 	 */
12363 	if (!(ipif->ipif_flags & IPIF_UP) ||
12364 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12365 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12366 		ipif->ipif_v6net_mask = v6mask;
12367 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12368 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12369 			    ipif->ipif_v6net_mask,
12370 			    ipif->ipif_v6subnet);
12371 		}
12372 		return (0);
12373 	}
12374 	/*
12375 	 * Make sure we have valid net and subnet broadcast ire's
12376 	 * for the old netmask, if needed by other logical interfaces.
12377 	 */
12378 	if (!ipif->ipif_isv6)
12379 		ipif_check_bcast_ires(ipif);
12380 
12381 	err = ipif_logical_down(ipif, q, mp);
12382 	if (err == EINPROGRESS)
12383 		return (err);
12384 	ipif_down_tail(ipif);
12385 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12386 	return (err);
12387 }
12388 
12389 static int
12390 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12391 {
12392 	in6_addr_t v6mask;
12393 	int err = 0;
12394 
12395 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12396 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12397 
12398 	if (ipif->ipif_isv6) {
12399 		sin6_t *sin6;
12400 
12401 		sin6 = (sin6_t *)sin;
12402 		v6mask = sin6->sin6_addr;
12403 	} else {
12404 		ipaddr_t mask;
12405 
12406 		mask = sin->sin_addr.s_addr;
12407 		V4MASK_TO_V6(mask, v6mask);
12408 	}
12409 
12410 	ipif->ipif_v6net_mask = v6mask;
12411 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12412 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12413 		    ipif->ipif_v6subnet);
12414 	}
12415 	err = ipif_up(ipif, q, mp);
12416 
12417 	if (err == 0 || err == EINPROGRESS) {
12418 		/*
12419 		 * The interface must be DL_BOUND if this packet has to
12420 		 * go out on the wire. Since we only go through a logical
12421 		 * down and are bound with the driver during an internal
12422 		 * down/up that is satisfied.
12423 		 */
12424 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12425 			/* Potentially broadcast an address mask reply. */
12426 			ipif_mask_reply(ipif);
12427 		}
12428 	}
12429 	return (err);
12430 }
12431 
12432 /* ARGSUSED */
12433 int
12434 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12435     ip_ioctl_cmd_t *ipip, void *if_req)
12436 {
12437 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12438 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12439 	ipif_down_tail(ipif);
12440 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12441 }
12442 
12443 /* Get interface net mask. */
12444 /* ARGSUSED */
12445 int
12446 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12447     ip_ioctl_cmd_t *ipip, void *if_req)
12448 {
12449 	struct lifreq *lifr = (struct lifreq *)if_req;
12450 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12451 
12452 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12453 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12454 
12455 	/*
12456 	 * net mask can't change since we have a reference to the ipif.
12457 	 */
12458 	if (ipif->ipif_isv6) {
12459 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12460 		*sin6 = sin6_null;
12461 		sin6->sin6_family = AF_INET6;
12462 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12463 		lifr->lifr_addrlen =
12464 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12465 	} else {
12466 		*sin = sin_null;
12467 		sin->sin_family = AF_INET;
12468 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12469 		if (ipip->ipi_cmd_type == LIF_CMD) {
12470 			lifr->lifr_addrlen =
12471 			    ip_mask_to_plen(ipif->ipif_net_mask);
12472 		}
12473 	}
12474 	return (0);
12475 }
12476 
12477 /* ARGSUSED */
12478 int
12479 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12480     ip_ioctl_cmd_t *ipip, void *if_req)
12481 {
12482 
12483 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12484 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12485 	/*
12486 	 * Set interface metric.  We don't use this for
12487 	 * anything but we keep track of it in case it is
12488 	 * important to routing applications or such.
12489 	 */
12490 	if (ipip->ipi_cmd_type == IF_CMD) {
12491 		struct ifreq    *ifr;
12492 
12493 		ifr = (struct ifreq *)if_req;
12494 		ipif->ipif_metric = ifr->ifr_metric;
12495 	} else {
12496 		struct lifreq   *lifr;
12497 
12498 		lifr = (struct lifreq *)if_req;
12499 		ipif->ipif_metric = lifr->lifr_metric;
12500 	}
12501 	return (0);
12502 }
12503 
12504 /* ARGSUSED */
12505 int
12506 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12507     ip_ioctl_cmd_t *ipip, void *if_req)
12508 {
12509 	/* Get interface metric. */
12510 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12511 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12512 	if (ipip->ipi_cmd_type == IF_CMD) {
12513 		struct ifreq    *ifr;
12514 
12515 		ifr = (struct ifreq *)if_req;
12516 		ifr->ifr_metric = ipif->ipif_metric;
12517 	} else {
12518 		struct lifreq   *lifr;
12519 
12520 		lifr = (struct lifreq *)if_req;
12521 		lifr->lifr_metric = ipif->ipif_metric;
12522 	}
12523 
12524 	return (0);
12525 }
12526 
12527 /* ARGSUSED */
12528 int
12529 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12530     ip_ioctl_cmd_t *ipip, void *if_req)
12531 {
12532 
12533 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12534 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12535 	/*
12536 	 * Set the muxid returned from I_PLINK.
12537 	 */
12538 	if (ipip->ipi_cmd_type == IF_CMD) {
12539 		struct ifreq *ifr = (struct ifreq *)if_req;
12540 
12541 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12542 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12543 	} else {
12544 		struct lifreq *lifr = (struct lifreq *)if_req;
12545 
12546 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12547 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12548 	}
12549 	return (0);
12550 }
12551 
12552 /* ARGSUSED */
12553 int
12554 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12555     ip_ioctl_cmd_t *ipip, void *if_req)
12556 {
12557 
12558 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12559 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12560 	/*
12561 	 * Get the muxid saved in ill for I_PUNLINK.
12562 	 */
12563 	if (ipip->ipi_cmd_type == IF_CMD) {
12564 		struct ifreq *ifr = (struct ifreq *)if_req;
12565 
12566 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12567 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12568 	} else {
12569 		struct lifreq *lifr = (struct lifreq *)if_req;
12570 
12571 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12572 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12573 	}
12574 	return (0);
12575 }
12576 
12577 /*
12578  * Set the subnet prefix. Does not modify the broadcast address.
12579  */
12580 /* ARGSUSED */
12581 int
12582 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12583     ip_ioctl_cmd_t *ipip, void *if_req)
12584 {
12585 	int err = 0;
12586 	in6_addr_t v6addr;
12587 	in6_addr_t v6mask;
12588 	boolean_t need_up = B_FALSE;
12589 	int addrlen;
12590 
12591 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12592 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12593 
12594 	ASSERT(IAM_WRITER_IPIF(ipif));
12595 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12596 
12597 	if (ipif->ipif_isv6) {
12598 		sin6_t *sin6;
12599 
12600 		if (sin->sin_family != AF_INET6)
12601 			return (EAFNOSUPPORT);
12602 
12603 		sin6 = (sin6_t *)sin;
12604 		v6addr = sin6->sin6_addr;
12605 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12606 			return (EADDRNOTAVAIL);
12607 	} else {
12608 		ipaddr_t addr;
12609 
12610 		if (sin->sin_family != AF_INET)
12611 			return (EAFNOSUPPORT);
12612 
12613 		addr = sin->sin_addr.s_addr;
12614 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12615 			return (EADDRNOTAVAIL);
12616 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12617 		/* Add 96 bits */
12618 		addrlen += IPV6_ABITS - IP_ABITS;
12619 	}
12620 
12621 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12622 		return (EINVAL);
12623 
12624 	/* Check if bits in the address is set past the mask */
12625 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12626 		return (EINVAL);
12627 
12628 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12629 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12630 		return (0);	/* No change */
12631 
12632 	if (ipif->ipif_flags & IPIF_UP) {
12633 		/*
12634 		 * If the interface is already marked up,
12635 		 * we call ipif_down which will take care
12636 		 * of ditching any IREs that have been set
12637 		 * up based on the old interface address.
12638 		 */
12639 		err = ipif_logical_down(ipif, q, mp);
12640 		if (err == EINPROGRESS)
12641 			return (err);
12642 		ipif_down_tail(ipif);
12643 		need_up = B_TRUE;
12644 	}
12645 
12646 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12647 	return (err);
12648 }
12649 
12650 static int
12651 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12652     queue_t *q, mblk_t *mp, boolean_t need_up)
12653 {
12654 	ill_t	*ill = ipif->ipif_ill;
12655 	int	err = 0;
12656 
12657 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12658 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12659 
12660 	/* Set the new address. */
12661 	mutex_enter(&ill->ill_lock);
12662 	ipif->ipif_v6net_mask = v6mask;
12663 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12664 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12665 		    ipif->ipif_v6subnet);
12666 	}
12667 	mutex_exit(&ill->ill_lock);
12668 
12669 	if (need_up) {
12670 		/*
12671 		 * Now bring the interface back up.  If this
12672 		 * is the only IPIF for the ILL, ipif_up
12673 		 * will have to re-bind to the device, so
12674 		 * we may get back EINPROGRESS, in which
12675 		 * case, this IOCTL will get completed in
12676 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12677 		 */
12678 		err = ipif_up(ipif, q, mp);
12679 		if (err == EINPROGRESS)
12680 			return (err);
12681 	}
12682 	return (err);
12683 }
12684 
12685 /* ARGSUSED */
12686 int
12687 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12688     ip_ioctl_cmd_t *ipip, void *if_req)
12689 {
12690 	int	addrlen;
12691 	in6_addr_t v6addr;
12692 	in6_addr_t v6mask;
12693 	struct lifreq *lifr = (struct lifreq *)if_req;
12694 
12695 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12696 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12697 	ipif_down_tail(ipif);
12698 
12699 	addrlen = lifr->lifr_addrlen;
12700 	if (ipif->ipif_isv6) {
12701 		sin6_t *sin6;
12702 
12703 		sin6 = (sin6_t *)sin;
12704 		v6addr = sin6->sin6_addr;
12705 	} else {
12706 		ipaddr_t addr;
12707 
12708 		addr = sin->sin_addr.s_addr;
12709 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12710 		addrlen += IPV6_ABITS - IP_ABITS;
12711 	}
12712 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12713 
12714 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12715 }
12716 
12717 /* ARGSUSED */
12718 int
12719 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12720     ip_ioctl_cmd_t *ipip, void *if_req)
12721 {
12722 	struct lifreq *lifr = (struct lifreq *)if_req;
12723 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12724 
12725 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12726 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12727 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12728 
12729 	if (ipif->ipif_isv6) {
12730 		*sin6 = sin6_null;
12731 		sin6->sin6_family = AF_INET6;
12732 		sin6->sin6_addr = ipif->ipif_v6subnet;
12733 		lifr->lifr_addrlen =
12734 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12735 	} else {
12736 		*sin = sin_null;
12737 		sin->sin_family = AF_INET;
12738 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12739 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12740 	}
12741 	return (0);
12742 }
12743 
12744 /*
12745  * Set the IPv6 address token.
12746  */
12747 /* ARGSUSED */
12748 int
12749 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12750     ip_ioctl_cmd_t *ipi, void *if_req)
12751 {
12752 	ill_t *ill = ipif->ipif_ill;
12753 	int err;
12754 	in6_addr_t v6addr;
12755 	in6_addr_t v6mask;
12756 	boolean_t need_up = B_FALSE;
12757 	int i;
12758 	sin6_t *sin6 = (sin6_t *)sin;
12759 	struct lifreq *lifr = (struct lifreq *)if_req;
12760 	int addrlen;
12761 
12762 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12763 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12764 	ASSERT(IAM_WRITER_IPIF(ipif));
12765 
12766 	addrlen = lifr->lifr_addrlen;
12767 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12768 	if (ipif->ipif_id != 0)
12769 		return (EINVAL);
12770 
12771 	if (!ipif->ipif_isv6)
12772 		return (EINVAL);
12773 
12774 	if (addrlen > IPV6_ABITS)
12775 		return (EINVAL);
12776 
12777 	v6addr = sin6->sin6_addr;
12778 
12779 	/*
12780 	 * The length of the token is the length from the end.  To get
12781 	 * the proper mask for this, compute the mask of the bits not
12782 	 * in the token; ie. the prefix, and then xor to get the mask.
12783 	 */
12784 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12785 		return (EINVAL);
12786 	for (i = 0; i < 4; i++) {
12787 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12788 	}
12789 
12790 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12791 	    ill->ill_token_length == addrlen)
12792 		return (0);	/* No change */
12793 
12794 	if (ipif->ipif_flags & IPIF_UP) {
12795 		err = ipif_logical_down(ipif, q, mp);
12796 		if (err == EINPROGRESS)
12797 			return (err);
12798 		ipif_down_tail(ipif);
12799 		need_up = B_TRUE;
12800 	}
12801 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12802 	return (err);
12803 }
12804 
12805 static int
12806 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12807     mblk_t *mp, boolean_t need_up)
12808 {
12809 	in6_addr_t v6addr;
12810 	in6_addr_t v6mask;
12811 	ill_t	*ill = ipif->ipif_ill;
12812 	int	i;
12813 	int	err = 0;
12814 
12815 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12816 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12817 	v6addr = sin6->sin6_addr;
12818 	/*
12819 	 * The length of the token is the length from the end.  To get
12820 	 * the proper mask for this, compute the mask of the bits not
12821 	 * in the token; ie. the prefix, and then xor to get the mask.
12822 	 */
12823 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12824 	for (i = 0; i < 4; i++)
12825 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12826 
12827 	mutex_enter(&ill->ill_lock);
12828 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12829 	ill->ill_token_length = addrlen;
12830 	mutex_exit(&ill->ill_lock);
12831 
12832 	if (need_up) {
12833 		/*
12834 		 * Now bring the interface back up.  If this
12835 		 * is the only IPIF for the ILL, ipif_up
12836 		 * will have to re-bind to the device, so
12837 		 * we may get back EINPROGRESS, in which
12838 		 * case, this IOCTL will get completed in
12839 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12840 		 */
12841 		err = ipif_up(ipif, q, mp);
12842 		if (err == EINPROGRESS)
12843 			return (err);
12844 	}
12845 	return (err);
12846 }
12847 
12848 /* ARGSUSED */
12849 int
12850 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12851     ip_ioctl_cmd_t *ipi, void *if_req)
12852 {
12853 	ill_t *ill;
12854 	sin6_t *sin6 = (sin6_t *)sin;
12855 	struct lifreq *lifr = (struct lifreq *)if_req;
12856 
12857 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12858 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12859 	if (ipif->ipif_id != 0)
12860 		return (EINVAL);
12861 
12862 	ill = ipif->ipif_ill;
12863 	if (!ill->ill_isv6)
12864 		return (ENXIO);
12865 
12866 	*sin6 = sin6_null;
12867 	sin6->sin6_family = AF_INET6;
12868 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12869 	sin6->sin6_addr = ill->ill_token;
12870 	lifr->lifr_addrlen = ill->ill_token_length;
12871 	return (0);
12872 }
12873 
12874 /*
12875  * Set (hardware) link specific information that might override
12876  * what was acquired through the DL_INFO_ACK.
12877  * The logic is as follows.
12878  *
12879  * become exclusive
12880  * set CHANGING flag
12881  * change mtu on affected IREs
12882  * clear CHANGING flag
12883  *
12884  * An ire add that occurs before the CHANGING flag is set will have its mtu
12885  * changed by the ip_sioctl_lnkinfo.
12886  *
12887  * During the time the CHANGING flag is set, no new ires will be added to the
12888  * bucket, and ire add will fail (due the CHANGING flag).
12889  *
12890  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12891  * before it is added to the bucket.
12892  *
12893  * Obviously only 1 thread can set the CHANGING flag and we need to become
12894  * exclusive to set the flag.
12895  */
12896 /* ARGSUSED */
12897 int
12898 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12899     ip_ioctl_cmd_t *ipi, void *if_req)
12900 {
12901 	ill_t		*ill = ipif->ipif_ill;
12902 	ipif_t		*nipif;
12903 	int		ip_min_mtu;
12904 	boolean_t	mtu_walk = B_FALSE;
12905 	struct lifreq	*lifr = (struct lifreq *)if_req;
12906 	lif_ifinfo_req_t *lir;
12907 	ire_t		*ire;
12908 
12909 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12910 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12911 	lir = &lifr->lifr_ifinfo;
12912 	ASSERT(IAM_WRITER_IPIF(ipif));
12913 
12914 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12915 	if (ipif->ipif_id != 0)
12916 		return (EINVAL);
12917 
12918 	/* Set interface MTU. */
12919 	if (ipif->ipif_isv6)
12920 		ip_min_mtu = IPV6_MIN_MTU;
12921 	else
12922 		ip_min_mtu = IP_MIN_MTU;
12923 
12924 	/*
12925 	 * Verify values before we set anything. Allow zero to
12926 	 * mean unspecified.
12927 	 */
12928 	if (lir->lir_maxmtu != 0 &&
12929 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12930 	    lir->lir_maxmtu < ip_min_mtu))
12931 		return (EINVAL);
12932 	if (lir->lir_reachtime != 0 &&
12933 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12934 		return (EINVAL);
12935 	if (lir->lir_reachretrans != 0 &&
12936 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12937 		return (EINVAL);
12938 
12939 	mutex_enter(&ill->ill_lock);
12940 	ill->ill_state_flags |= ILL_CHANGING;
12941 	for (nipif = ill->ill_ipif; nipif != NULL;
12942 	    nipif = nipif->ipif_next) {
12943 		nipif->ipif_state_flags |= IPIF_CHANGING;
12944 	}
12945 
12946 	mutex_exit(&ill->ill_lock);
12947 
12948 	if (lir->lir_maxmtu != 0) {
12949 		ill->ill_max_mtu = lir->lir_maxmtu;
12950 		ill->ill_mtu_userspecified = 1;
12951 		mtu_walk = B_TRUE;
12952 	}
12953 
12954 	if (lir->lir_reachtime != 0)
12955 		ill->ill_reachable_time = lir->lir_reachtime;
12956 
12957 	if (lir->lir_reachretrans != 0)
12958 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12959 
12960 	ill->ill_max_hops = lir->lir_maxhops;
12961 
12962 	ill->ill_max_buf = ND_MAX_Q;
12963 
12964 	if (mtu_walk) {
12965 		/*
12966 		 * Set the MTU on all ipifs associated with this ill except
12967 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12968 		 */
12969 		for (nipif = ill->ill_ipif; nipif != NULL;
12970 		    nipif = nipif->ipif_next) {
12971 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12972 				continue;
12973 
12974 			nipif->ipif_mtu = ill->ill_max_mtu;
12975 
12976 			if (!(nipif->ipif_flags & IPIF_UP))
12977 				continue;
12978 
12979 			if (nipif->ipif_isv6)
12980 				ire = ipif_to_ire_v6(nipif);
12981 			else
12982 				ire = ipif_to_ire(nipif);
12983 			if (ire != NULL) {
12984 				ire->ire_max_frag = ipif->ipif_mtu;
12985 				ire_refrele(ire);
12986 			}
12987 
12988 			ire_walk_ill(MATCH_IRE_ILL, 0, ipif_mtu_change,
12989 			    nipif, ill);
12990 		}
12991 	}
12992 
12993 	mutex_enter(&ill->ill_lock);
12994 	for (nipif = ill->ill_ipif; nipif != NULL;
12995 	    nipif = nipif->ipif_next) {
12996 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12997 	}
12998 	ILL_UNMARK_CHANGING(ill);
12999 	mutex_exit(&ill->ill_lock);
13000 
13001 	return (0);
13002 }
13003 
13004 /* ARGSUSED */
13005 int
13006 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13007     ip_ioctl_cmd_t *ipi, void *if_req)
13008 {
13009 	struct lif_ifinfo_req *lir;
13010 	ill_t *ill = ipif->ipif_ill;
13011 
13012 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13013 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13014 	if (ipif->ipif_id != 0)
13015 		return (EINVAL);
13016 
13017 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13018 	lir->lir_maxhops = ill->ill_max_hops;
13019 	lir->lir_reachtime = ill->ill_reachable_time;
13020 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13021 	lir->lir_maxmtu = ill->ill_max_mtu;
13022 
13023 	return (0);
13024 }
13025 
13026 /*
13027  * Return best guess as to the subnet mask for the specified address.
13028  * Based on the subnet masks for all the configured interfaces.
13029  *
13030  * We end up returning a zero mask in the case of default, multicast or
13031  * experimental.
13032  */
13033 static ipaddr_t
13034 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13035 {
13036 	ipaddr_t net_mask;
13037 	ill_t	*ill;
13038 	ipif_t	*ipif;
13039 	ill_walk_context_t ctx;
13040 	ipif_t	*fallback_ipif = NULL;
13041 
13042 	net_mask = ip_net_mask(addr);
13043 	if (net_mask == 0) {
13044 		*ipifp = NULL;
13045 		return (0);
13046 	}
13047 
13048 	/* Let's check to see if this is maybe a local subnet route. */
13049 	/* this function only applies to IPv4 interfaces */
13050 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13051 	ill = ILL_START_WALK_V4(&ctx, ipst);
13052 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13053 		mutex_enter(&ill->ill_lock);
13054 		for (ipif = ill->ill_ipif; ipif != NULL;
13055 		    ipif = ipif->ipif_next) {
13056 			if (!IPIF_CAN_LOOKUP(ipif))
13057 				continue;
13058 			if (!(ipif->ipif_flags & IPIF_UP))
13059 				continue;
13060 			if ((ipif->ipif_subnet & net_mask) ==
13061 			    (addr & net_mask)) {
13062 				/*
13063 				 * Don't trust pt-pt interfaces if there are
13064 				 * other interfaces.
13065 				 */
13066 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13067 					if (fallback_ipif == NULL) {
13068 						ipif_refhold_locked(ipif);
13069 						fallback_ipif = ipif;
13070 					}
13071 					continue;
13072 				}
13073 
13074 				/*
13075 				 * Fine. Just assume the same net mask as the
13076 				 * directly attached subnet interface is using.
13077 				 */
13078 				ipif_refhold_locked(ipif);
13079 				mutex_exit(&ill->ill_lock);
13080 				rw_exit(&ipst->ips_ill_g_lock);
13081 				if (fallback_ipif != NULL)
13082 					ipif_refrele(fallback_ipif);
13083 				*ipifp = ipif;
13084 				return (ipif->ipif_net_mask);
13085 			}
13086 		}
13087 		mutex_exit(&ill->ill_lock);
13088 	}
13089 	rw_exit(&ipst->ips_ill_g_lock);
13090 
13091 	*ipifp = fallback_ipif;
13092 	return ((fallback_ipif != NULL) ?
13093 	    fallback_ipif->ipif_net_mask : net_mask);
13094 }
13095 
13096 /*
13097  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13098  */
13099 static void
13100 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13101 {
13102 	IOCP	iocp;
13103 	ipft_t	*ipft;
13104 	ipllc_t	*ipllc;
13105 	mblk_t	*mp1;
13106 	cred_t	*cr;
13107 	int	error = 0;
13108 	conn_t	*connp;
13109 
13110 	ip1dbg(("ip_wput_ioctl"));
13111 	iocp = (IOCP)mp->b_rptr;
13112 	mp1 = mp->b_cont;
13113 	if (mp1 == NULL) {
13114 		iocp->ioc_error = EINVAL;
13115 		mp->b_datap->db_type = M_IOCNAK;
13116 		iocp->ioc_count = 0;
13117 		qreply(q, mp);
13118 		return;
13119 	}
13120 
13121 	/*
13122 	 * These IOCTLs provide various control capabilities to
13123 	 * upstream agents such as ULPs and processes.	There
13124 	 * are currently two such IOCTLs implemented.  They
13125 	 * are used by TCP to provide update information for
13126 	 * existing IREs and to forcibly delete an IRE for a
13127 	 * host that is not responding, thereby forcing an
13128 	 * attempt at a new route.
13129 	 */
13130 	iocp->ioc_error = EINVAL;
13131 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13132 		goto done;
13133 
13134 	ipllc = (ipllc_t *)mp1->b_rptr;
13135 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13136 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13137 			break;
13138 	}
13139 	/*
13140 	 * prefer credential from mblk over ioctl;
13141 	 * see ip_sioctl_copyin_setup
13142 	 */
13143 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13144 
13145 	/*
13146 	 * Refhold the conn in case the request gets queued up in some lookup
13147 	 */
13148 	ASSERT(CONN_Q(q));
13149 	connp = Q_TO_CONN(q);
13150 	CONN_INC_REF(connp);
13151 	if (ipft->ipft_pfi &&
13152 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13153 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13154 		error = (*ipft->ipft_pfi)(q,
13155 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13156 	}
13157 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13158 		/*
13159 		 * CONN_OPER_PENDING_DONE happens in the function called
13160 		 * through ipft_pfi above.
13161 		 */
13162 		return;
13163 	}
13164 
13165 	CONN_OPER_PENDING_DONE(connp);
13166 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13167 		freemsg(mp);
13168 		return;
13169 	}
13170 	iocp->ioc_error = error;
13171 
13172 done:
13173 	mp->b_datap->db_type = M_IOCACK;
13174 	if (iocp->ioc_error)
13175 		iocp->ioc_count = 0;
13176 	qreply(q, mp);
13177 }
13178 
13179 /*
13180  * Lookup an ipif using the sequence id (ipif_seqid)
13181  */
13182 ipif_t *
13183 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13184 {
13185 	ipif_t *ipif;
13186 
13187 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13188 
13189 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13190 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13191 			return (ipif);
13192 	}
13193 	return (NULL);
13194 }
13195 
13196 /*
13197  * Assign a unique id for the ipif. This is used later when we send
13198  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13199  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13200  * IRE is added, we verify that ipif has not disappeared.
13201  */
13202 
13203 static void
13204 ipif_assign_seqid(ipif_t *ipif)
13205 {
13206 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13207 
13208 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13209 }
13210 
13211 /*
13212  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13213  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13214  * be inserted into the first space available in the list. The value of
13215  * ipif_id will then be set to the appropriate value for its position.
13216  */
13217 static int
13218 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13219 {
13220 	ill_t *ill;
13221 	ipif_t *tipif;
13222 	ipif_t **tipifp;
13223 	int id;
13224 	ip_stack_t	*ipst;
13225 
13226 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13227 	    IAM_WRITER_IPIF(ipif));
13228 
13229 	ill = ipif->ipif_ill;
13230 	ASSERT(ill != NULL);
13231 	ipst = ill->ill_ipst;
13232 
13233 	/*
13234 	 * In the case of lo0:0 we already hold the ill_g_lock.
13235 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13236 	 * ipif_insert. Another such caller is ipif_move.
13237 	 */
13238 	if (acquire_g_lock)
13239 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13240 	if (acquire_ill_lock)
13241 		mutex_enter(&ill->ill_lock);
13242 	id = ipif->ipif_id;
13243 	tipifp = &(ill->ill_ipif);
13244 	if (id == -1) {	/* need to find a real id */
13245 		id = 0;
13246 		while ((tipif = *tipifp) != NULL) {
13247 			ASSERT(tipif->ipif_id >= id);
13248 			if (tipif->ipif_id != id)
13249 				break; /* non-consecutive id */
13250 			id++;
13251 			tipifp = &(tipif->ipif_next);
13252 		}
13253 		/* limit number of logical interfaces */
13254 		if (id >= ipst->ips_ip_addrs_per_if) {
13255 			if (acquire_ill_lock)
13256 				mutex_exit(&ill->ill_lock);
13257 			if (acquire_g_lock)
13258 				rw_exit(&ipst->ips_ill_g_lock);
13259 			return (-1);
13260 		}
13261 		ipif->ipif_id = id; /* assign new id */
13262 	} else if (id < ipst->ips_ip_addrs_per_if) {
13263 		/* we have a real id; insert ipif in the right place */
13264 		while ((tipif = *tipifp) != NULL) {
13265 			ASSERT(tipif->ipif_id != id);
13266 			if (tipif->ipif_id > id)
13267 				break; /* found correct location */
13268 			tipifp = &(tipif->ipif_next);
13269 		}
13270 	} else {
13271 		if (acquire_ill_lock)
13272 			mutex_exit(&ill->ill_lock);
13273 		if (acquire_g_lock)
13274 			rw_exit(&ipst->ips_ill_g_lock);
13275 		return (-1);
13276 	}
13277 
13278 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13279 
13280 	ipif->ipif_next = tipif;
13281 	*tipifp = ipif;
13282 	if (acquire_ill_lock)
13283 		mutex_exit(&ill->ill_lock);
13284 	if (acquire_g_lock)
13285 		rw_exit(&ipst->ips_ill_g_lock);
13286 	return (0);
13287 }
13288 
13289 static void
13290 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13291 {
13292 	ipif_t	**ipifp;
13293 	ill_t	*ill = ipif->ipif_ill;
13294 
13295 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13296 	if (acquire_ill_lock)
13297 		mutex_enter(&ill->ill_lock);
13298 	else
13299 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13300 
13301 	ipifp = &ill->ill_ipif;
13302 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13303 		if (*ipifp == ipif) {
13304 			*ipifp = ipif->ipif_next;
13305 			break;
13306 		}
13307 	}
13308 
13309 	if (acquire_ill_lock)
13310 		mutex_exit(&ill->ill_lock);
13311 }
13312 
13313 /*
13314  * Allocate and initialize a new interface control structure.  (Always
13315  * called as writer.)
13316  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13317  * is not part of the global linked list of ills. ipif_seqid is unique
13318  * in the system and to preserve the uniqueness, it is assigned only
13319  * when ill becomes part of the global list. At that point ill will
13320  * have a name. If it doesn't get assigned here, it will get assigned
13321  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13322  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13323  * the interface flags or any other information from the DL_INFO_ACK for
13324  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13325  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13326  * second DL_INFO_ACK comes in from the driver.
13327  */
13328 static ipif_t *
13329 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13330 {
13331 	ipif_t	*ipif;
13332 	phyint_t *phyi;
13333 
13334 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13335 	    ill->ill_name, id, (void *)ill));
13336 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13337 
13338 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13339 		return (NULL);
13340 	*ipif = ipif_zero;	/* start clean */
13341 
13342 	ipif->ipif_ill = ill;
13343 	ipif->ipif_id = id;	/* could be -1 */
13344 	/*
13345 	 * Inherit the zoneid from the ill; for the shared stack instance
13346 	 * this is always the global zone
13347 	 */
13348 	ipif->ipif_zoneid = ill->ill_zoneid;
13349 
13350 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13351 
13352 	ipif->ipif_refcnt = 0;
13353 	ipif->ipif_saved_ire_cnt = 0;
13354 
13355 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13356 		mi_free(ipif);
13357 		return (NULL);
13358 	}
13359 	/* -1 id should have been replaced by real id */
13360 	id = ipif->ipif_id;
13361 	ASSERT(id >= 0);
13362 
13363 	if (ill->ill_name[0] != '\0')
13364 		ipif_assign_seqid(ipif);
13365 
13366 	/*
13367 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13368 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13369 	 * ioctl sets ipif_orig_ipifid to zero.
13370 	 */
13371 	ipif->ipif_orig_ipifid = id;
13372 
13373 	/*
13374 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13375 	 * The ipif is still not up and can't be looked up until the
13376 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13377 	 */
13378 	mutex_enter(&ill->ill_lock);
13379 	mutex_enter(&ill->ill_phyint->phyint_lock);
13380 	/*
13381 	 * Set the running flag when logical interface zero is created.
13382 	 * For subsequent logical interfaces, a DLPI link down
13383 	 * notification message may have cleared the running flag to
13384 	 * indicate the link is down, so we shouldn't just blindly set it.
13385 	 */
13386 	if (id == 0)
13387 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13388 	ipif->ipif_ire_type = ire_type;
13389 	phyi = ill->ill_phyint;
13390 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13391 
13392 	if (ipif->ipif_isv6) {
13393 		ill->ill_flags |= ILLF_IPV6;
13394 	} else {
13395 		ipaddr_t inaddr_any = INADDR_ANY;
13396 
13397 		ill->ill_flags |= ILLF_IPV4;
13398 
13399 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13400 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13401 		    &ipif->ipif_v6lcl_addr);
13402 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13403 		    &ipif->ipif_v6src_addr);
13404 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13405 		    &ipif->ipif_v6subnet);
13406 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13407 		    &ipif->ipif_v6net_mask);
13408 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13409 		    &ipif->ipif_v6brd_addr);
13410 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13411 		    &ipif->ipif_v6pp_dst_addr);
13412 	}
13413 
13414 	/*
13415 	 * Don't set the interface flags etc. now, will do it in
13416 	 * ip_ll_subnet_defaults.
13417 	 */
13418 	if (!initialize) {
13419 		mutex_exit(&ill->ill_lock);
13420 		mutex_exit(&ill->ill_phyint->phyint_lock);
13421 		return (ipif);
13422 	}
13423 	ipif->ipif_mtu = ill->ill_max_mtu;
13424 
13425 	if (ill->ill_bcast_addr_length != 0) {
13426 		/*
13427 		 * Later detect lack of DLPI driver multicast
13428 		 * capability by catching DL_ENABMULTI errors in
13429 		 * ip_rput_dlpi.
13430 		 */
13431 		ill->ill_flags |= ILLF_MULTICAST;
13432 		if (!ipif->ipif_isv6)
13433 			ipif->ipif_flags |= IPIF_BROADCAST;
13434 	} else {
13435 		if (ill->ill_net_type != IRE_LOOPBACK) {
13436 			if (ipif->ipif_isv6)
13437 				/*
13438 				 * Note: xresolv interfaces will eventually need
13439 				 * NOARP set here as well, but that will require
13440 				 * those external resolvers to have some
13441 				 * knowledge of that flag and act appropriately.
13442 				 * Not to be changed at present.
13443 				 */
13444 				ill->ill_flags |= ILLF_NONUD;
13445 			else
13446 				ill->ill_flags |= ILLF_NOARP;
13447 		}
13448 		if (ill->ill_phys_addr_length == 0) {
13449 			if (ill->ill_media &&
13450 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13451 				ipif->ipif_flags |= IPIF_NOXMIT;
13452 				phyi->phyint_flags |= PHYI_VIRTUAL;
13453 			} else {
13454 				/* pt-pt supports multicast. */
13455 				ill->ill_flags |= ILLF_MULTICAST;
13456 				if (ill->ill_net_type == IRE_LOOPBACK) {
13457 					phyi->phyint_flags |=
13458 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13459 				} else {
13460 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13461 				}
13462 			}
13463 		}
13464 	}
13465 	mutex_exit(&ill->ill_lock);
13466 	mutex_exit(&ill->ill_phyint->phyint_lock);
13467 	return (ipif);
13468 }
13469 
13470 /*
13471  * If appropriate, send a message up to the resolver delete the entry
13472  * for the address of this interface which is going out of business.
13473  * (Always called as writer).
13474  *
13475  * NOTE : We need to check for NULL mps as some of the fields are
13476  *	  initialized only for some interface types. See ipif_resolver_up()
13477  *	  for details.
13478  */
13479 void
13480 ipif_arp_down(ipif_t *ipif)
13481 {
13482 	mblk_t	*mp;
13483 	ill_t	*ill = ipif->ipif_ill;
13484 
13485 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13486 	ASSERT(IAM_WRITER_IPIF(ipif));
13487 
13488 	/* Delete the mapping for the local address */
13489 	mp = ipif->ipif_arp_del_mp;
13490 	if (mp != NULL) {
13491 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13492 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13493 		putnext(ill->ill_rq, mp);
13494 		ipif->ipif_arp_del_mp = NULL;
13495 	}
13496 
13497 	/*
13498 	 * If this is the last ipif that is going down and there are no
13499 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13500 	 * clean up ARP completely.
13501 	 */
13502 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13503 
13504 		/* Send up AR_INTERFACE_DOWN message */
13505 		mp = ill->ill_arp_down_mp;
13506 		if (mp != NULL) {
13507 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13508 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13509 			    ipif->ipif_id));
13510 			putnext(ill->ill_rq, mp);
13511 			ill->ill_arp_down_mp = NULL;
13512 		}
13513 
13514 		/* Tell ARP to delete the multicast mappings */
13515 		mp = ill->ill_arp_del_mapping_mp;
13516 		if (mp != NULL) {
13517 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13518 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13519 			    ipif->ipif_id));
13520 			putnext(ill->ill_rq, mp);
13521 			ill->ill_arp_del_mapping_mp = NULL;
13522 		}
13523 	}
13524 }
13525 
13526 /*
13527  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13528  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13529  * that it wants the add_mp allocated in this function to be returned
13530  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13531  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13532  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13533  * as it does a ipif_arp_down after calling this function - which will
13534  * remove what we add here.
13535  *
13536  * Returns -1 on failures and 0 on success.
13537  */
13538 int
13539 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13540 {
13541 	mblk_t	*del_mp = NULL;
13542 	mblk_t *add_mp = NULL;
13543 	mblk_t *mp;
13544 	ill_t	*ill = ipif->ipif_ill;
13545 	phyint_t *phyi = ill->ill_phyint;
13546 	ipaddr_t addr, mask, extract_mask = 0;
13547 	arma_t	*arma;
13548 	uint8_t *maddr, *bphys_addr;
13549 	uint32_t hw_start;
13550 	dl_unitdata_req_t *dlur;
13551 
13552 	ASSERT(IAM_WRITER_IPIF(ipif));
13553 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13554 		return (0);
13555 
13556 	/*
13557 	 * Delete the existing mapping from ARP. Normally ipif_down
13558 	 * -> ipif_arp_down should send this up to ARP. The only
13559 	 * reason we would find this when we are switching from
13560 	 * Multicast to Broadcast where we did not do a down.
13561 	 */
13562 	mp = ill->ill_arp_del_mapping_mp;
13563 	if (mp != NULL) {
13564 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13565 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13566 		putnext(ill->ill_rq, mp);
13567 		ill->ill_arp_del_mapping_mp = NULL;
13568 	}
13569 
13570 	if (arp_add_mapping_mp != NULL)
13571 		*arp_add_mapping_mp = NULL;
13572 
13573 	/*
13574 	 * Check that the address is not to long for the constant
13575 	 * length reserved in the template arma_t.
13576 	 */
13577 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13578 		return (-1);
13579 
13580 	/* Add mapping mblk */
13581 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13582 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13583 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13584 	    (caddr_t)&addr);
13585 	if (add_mp == NULL)
13586 		return (-1);
13587 	arma = (arma_t *)add_mp->b_rptr;
13588 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13589 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13590 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13591 
13592 	/*
13593 	 * Determine the broadcast address.
13594 	 */
13595 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13596 	if (ill->ill_sap_length < 0)
13597 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13598 	else
13599 		bphys_addr = (uchar_t *)dlur +
13600 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13601 	/*
13602 	 * Check PHYI_MULTI_BCAST and length of physical
13603 	 * address to determine if we use the mapping or the
13604 	 * broadcast address.
13605 	 */
13606 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13607 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13608 		    bphys_addr, maddr, &hw_start, &extract_mask))
13609 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13610 
13611 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13612 	    (ill->ill_flags & ILLF_MULTICAST)) {
13613 		/* Make sure this will not match the "exact" entry. */
13614 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13615 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13616 		    (caddr_t)&addr);
13617 		if (del_mp == NULL) {
13618 			freemsg(add_mp);
13619 			return (-1);
13620 		}
13621 		bcopy(&extract_mask, (char *)arma +
13622 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13623 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13624 			/* Use link-layer broadcast address for MULTI_BCAST */
13625 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13626 			ip2dbg(("ipif_arp_setup_multicast: adding"
13627 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13628 		} else {
13629 			arma->arma_hw_mapping_start = hw_start;
13630 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13631 			    " ARP setup for %s\n", ill->ill_name));
13632 		}
13633 	} else {
13634 		freemsg(add_mp);
13635 		ASSERT(del_mp == NULL);
13636 		/* It is neither MULTICAST nor MULTI_BCAST */
13637 		return (0);
13638 	}
13639 	ASSERT(add_mp != NULL && del_mp != NULL);
13640 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13641 	ill->ill_arp_del_mapping_mp = del_mp;
13642 	if (arp_add_mapping_mp != NULL) {
13643 		/* The caller just wants the mblks allocated */
13644 		*arp_add_mapping_mp = add_mp;
13645 	} else {
13646 		/* The caller wants us to send it to arp */
13647 		putnext(ill->ill_rq, add_mp);
13648 	}
13649 	return (0);
13650 }
13651 
13652 /*
13653  * Get the resolver set up for a new interface address.
13654  * (Always called as writer.)
13655  * Called both for IPv4 and IPv6 interfaces,
13656  * though it only sets up the resolver for v6
13657  * if it's an xresolv interface (one using an external resolver).
13658  * Honors ILLF_NOARP.
13659  * The enumerated value res_act is used to tune the behavior.
13660  * If set to Res_act_initial, then we set up all the resolver
13661  * structures for a new interface.  If set to Res_act_move, then
13662  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13663  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13664  * asynchronous hardware address change notification.  If set to
13665  * Res_act_defend, then we tell ARP that it needs to send a single
13666  * gratuitous message in defense of the address.
13667  * Returns error on failure.
13668  */
13669 int
13670 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13671 {
13672 	caddr_t	addr;
13673 	mblk_t	*arp_up_mp = NULL;
13674 	mblk_t	*arp_down_mp = NULL;
13675 	mblk_t	*arp_add_mp = NULL;
13676 	mblk_t	*arp_del_mp = NULL;
13677 	mblk_t	*arp_add_mapping_mp = NULL;
13678 	mblk_t	*arp_del_mapping_mp = NULL;
13679 	ill_t	*ill = ipif->ipif_ill;
13680 	uchar_t	*area_p = NULL;
13681 	uchar_t	*ared_p = NULL;
13682 	int	err = ENOMEM;
13683 	boolean_t was_dup;
13684 
13685 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13686 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13687 	ASSERT(IAM_WRITER_IPIF(ipif));
13688 
13689 	was_dup = B_FALSE;
13690 	if (res_act == Res_act_initial) {
13691 		ipif->ipif_addr_ready = 0;
13692 		/*
13693 		 * We're bringing an interface up here.  There's no way that we
13694 		 * should need to shut down ARP now.
13695 		 */
13696 		mutex_enter(&ill->ill_lock);
13697 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13698 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13699 			ill->ill_ipif_dup_count--;
13700 			was_dup = B_TRUE;
13701 		}
13702 		mutex_exit(&ill->ill_lock);
13703 	}
13704 	if (ipif->ipif_recovery_id != 0)
13705 		(void) untimeout(ipif->ipif_recovery_id);
13706 	ipif->ipif_recovery_id = 0;
13707 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13708 		ipif->ipif_addr_ready = 1;
13709 		return (0);
13710 	}
13711 	/* NDP will set the ipif_addr_ready flag when it's ready */
13712 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13713 		return (0);
13714 
13715 	if (ill->ill_isv6) {
13716 		/*
13717 		 * External resolver for IPv6
13718 		 */
13719 		ASSERT(res_act == Res_act_initial);
13720 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13721 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13722 			area_p = (uchar_t *)&ip6_area_template;
13723 			ared_p = (uchar_t *)&ip6_ared_template;
13724 		}
13725 	} else {
13726 		/*
13727 		 * IPv4 arp case. If the ARP stream has already started
13728 		 * closing, fail this request for ARP bringup. Else
13729 		 * record the fact that an ARP bringup is pending.
13730 		 */
13731 		mutex_enter(&ill->ill_lock);
13732 		if (ill->ill_arp_closing) {
13733 			mutex_exit(&ill->ill_lock);
13734 			err = EINVAL;
13735 			goto failed;
13736 		} else {
13737 			if (ill->ill_ipif_up_count == 0 &&
13738 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13739 				ill->ill_arp_bringup_pending = 1;
13740 			mutex_exit(&ill->ill_lock);
13741 		}
13742 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13743 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13744 			area_p = (uchar_t *)&ip_area_template;
13745 			ared_p = (uchar_t *)&ip_ared_template;
13746 		}
13747 	}
13748 
13749 	/*
13750 	 * Add an entry for the local address in ARP only if it
13751 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13752 	 */
13753 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13754 		area_t *area;
13755 
13756 		/* Now ask ARP to publish our address. */
13757 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13758 		if (arp_add_mp == NULL)
13759 			goto failed;
13760 		area = (area_t *)arp_add_mp->b_rptr;
13761 		if (res_act != Res_act_initial) {
13762 			/*
13763 			 * Copy the new hardware address and length into
13764 			 * arp_add_mp to be sent to ARP.
13765 			 */
13766 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13767 			bcopy(ill->ill_phys_addr,
13768 			    ((char *)area + area->area_hw_addr_offset),
13769 			    area->area_hw_addr_length);
13770 		}
13771 
13772 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13773 		    ACE_F_MYADDR;
13774 
13775 		if (res_act == Res_act_defend) {
13776 			area->area_flags |= ACE_F_DEFEND;
13777 			/*
13778 			 * If we're just defending our address now, then
13779 			 * there's no need to set up ARP multicast mappings.
13780 			 * The publish command is enough.
13781 			 */
13782 			goto done;
13783 		}
13784 
13785 		if (res_act != Res_act_initial)
13786 			goto arp_setup_multicast;
13787 
13788 		/*
13789 		 * Allocate an ARP deletion message so we know we can tell ARP
13790 		 * when the interface goes down.
13791 		 */
13792 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13793 		if (arp_del_mp == NULL)
13794 			goto failed;
13795 
13796 	} else {
13797 		if (res_act != Res_act_initial)
13798 			goto done;
13799 	}
13800 	/*
13801 	 * Need to bring up ARP or setup multicast mapping only
13802 	 * when the first interface is coming UP.
13803 	 */
13804 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13805 	    was_dup) {
13806 		goto done;
13807 	}
13808 
13809 	/*
13810 	 * Allocate an ARP down message (to be saved) and an ARP up
13811 	 * message.
13812 	 */
13813 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13814 	if (arp_down_mp == NULL)
13815 		goto failed;
13816 
13817 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13818 	if (arp_up_mp == NULL)
13819 		goto failed;
13820 
13821 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13822 		goto done;
13823 
13824 arp_setup_multicast:
13825 	/*
13826 	 * Setup the multicast mappings. This function initializes
13827 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13828 	 * IPv6.
13829 	 */
13830 	if (!ill->ill_isv6) {
13831 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13832 		if (err != 0)
13833 			goto failed;
13834 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13835 		ASSERT(arp_add_mapping_mp != NULL);
13836 	}
13837 
13838 done:
13839 	if (arp_del_mp != NULL) {
13840 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13841 		ipif->ipif_arp_del_mp = arp_del_mp;
13842 	}
13843 	if (arp_down_mp != NULL) {
13844 		ASSERT(ill->ill_arp_down_mp == NULL);
13845 		ill->ill_arp_down_mp = arp_down_mp;
13846 	}
13847 	if (arp_del_mapping_mp != NULL) {
13848 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13849 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13850 	}
13851 	if (arp_up_mp != NULL) {
13852 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13853 		    ill->ill_name, ipif->ipif_id));
13854 		putnext(ill->ill_rq, arp_up_mp);
13855 	}
13856 	if (arp_add_mp != NULL) {
13857 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13858 		    ill->ill_name, ipif->ipif_id));
13859 		/*
13860 		 * If it's an extended ARP implementation, then we'll wait to
13861 		 * hear that DAD has finished before using the interface.
13862 		 */
13863 		if (!ill->ill_arp_extend)
13864 			ipif->ipif_addr_ready = 1;
13865 		putnext(ill->ill_rq, arp_add_mp);
13866 	} else {
13867 		ipif->ipif_addr_ready = 1;
13868 	}
13869 	if (arp_add_mapping_mp != NULL) {
13870 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13871 		    ill->ill_name, ipif->ipif_id));
13872 		putnext(ill->ill_rq, arp_add_mapping_mp);
13873 	}
13874 	if (res_act != Res_act_initial)
13875 		return (0);
13876 
13877 	if (ill->ill_flags & ILLF_NOARP)
13878 		err = ill_arp_off(ill);
13879 	else
13880 		err = ill_arp_on(ill);
13881 	if (err != 0) {
13882 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13883 		freemsg(ipif->ipif_arp_del_mp);
13884 		freemsg(ill->ill_arp_down_mp);
13885 		freemsg(ill->ill_arp_del_mapping_mp);
13886 		ipif->ipif_arp_del_mp = NULL;
13887 		ill->ill_arp_down_mp = NULL;
13888 		ill->ill_arp_del_mapping_mp = NULL;
13889 		return (err);
13890 	}
13891 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13892 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13893 
13894 failed:
13895 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13896 	freemsg(arp_add_mp);
13897 	freemsg(arp_del_mp);
13898 	freemsg(arp_add_mapping_mp);
13899 	freemsg(arp_up_mp);
13900 	freemsg(arp_down_mp);
13901 	ill->ill_arp_bringup_pending = 0;
13902 	return (err);
13903 }
13904 
13905 /*
13906  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13907  * just gone back up.
13908  */
13909 static void
13910 ipif_arp_start_dad(ipif_t *ipif)
13911 {
13912 	ill_t *ill = ipif->ipif_ill;
13913 	mblk_t *arp_add_mp;
13914 	area_t *area;
13915 
13916 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13917 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13918 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13919 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13920 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13921 		/*
13922 		 * If we can't contact ARP for some reason, that's not really a
13923 		 * problem.  Just send out the routing socket notification that
13924 		 * DAD completion would have done, and continue.
13925 		 */
13926 		ipif_mask_reply(ipif);
13927 		ip_rts_ifmsg(ipif);
13928 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13929 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13930 		ipif->ipif_addr_ready = 1;
13931 		return;
13932 	}
13933 
13934 	/* Setting the 'unverified' flag restarts DAD */
13935 	area = (area_t *)arp_add_mp->b_rptr;
13936 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13937 	    ACE_F_UNVERIFIED;
13938 	putnext(ill->ill_rq, arp_add_mp);
13939 }
13940 
13941 static void
13942 ipif_ndp_start_dad(ipif_t *ipif)
13943 {
13944 	nce_t *nce;
13945 
13946 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13947 	if (nce == NULL)
13948 		return;
13949 
13950 	if (!ndp_restart_dad(nce)) {
13951 		/*
13952 		 * If we can't restart DAD for some reason, that's not really a
13953 		 * problem.  Just send out the routing socket notification that
13954 		 * DAD completion would have done, and continue.
13955 		 */
13956 		ip_rts_ifmsg(ipif);
13957 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13958 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13959 		ipif->ipif_addr_ready = 1;
13960 	}
13961 	NCE_REFRELE(nce);
13962 }
13963 
13964 /*
13965  * Restart duplicate address detection on all interfaces on the given ill.
13966  *
13967  * This is called when an interface transitions from down to up
13968  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13969  *
13970  * Note that since the underlying physical link has transitioned, we must cause
13971  * at least one routing socket message to be sent here, either via DAD
13972  * completion or just by default on the first ipif.  (If we don't do this, then
13973  * in.mpathd will see long delays when doing link-based failure recovery.)
13974  */
13975 void
13976 ill_restart_dad(ill_t *ill, boolean_t went_up)
13977 {
13978 	ipif_t *ipif;
13979 
13980 	if (ill == NULL)
13981 		return;
13982 
13983 	/*
13984 	 * If layer two doesn't support duplicate address detection, then just
13985 	 * send the routing socket message now and be done with it.
13986 	 */
13987 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13988 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13989 		ip_rts_ifmsg(ill->ill_ipif);
13990 		return;
13991 	}
13992 
13993 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13994 		if (went_up) {
13995 			if (ipif->ipif_flags & IPIF_UP) {
13996 				if (ill->ill_isv6)
13997 					ipif_ndp_start_dad(ipif);
13998 				else
13999 					ipif_arp_start_dad(ipif);
14000 			} else if (ill->ill_isv6 &&
14001 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14002 				/*
14003 				 * For IPv4, the ARP module itself will
14004 				 * automatically start the DAD process when it
14005 				 * sees DL_NOTE_LINK_UP.  We respond to the
14006 				 * AR_CN_READY at the completion of that task.
14007 				 * For IPv6, we must kick off the bring-up
14008 				 * process now.
14009 				 */
14010 				ndp_do_recovery(ipif);
14011 			} else {
14012 				/*
14013 				 * Unfortunately, the first ipif is "special"
14014 				 * and represents the underlying ill in the
14015 				 * routing socket messages.  Thus, when this
14016 				 * one ipif is down, we must still notify so
14017 				 * that the user knows the IFF_RUNNING status
14018 				 * change.  (If the first ipif is up, then
14019 				 * we'll handle eventual routing socket
14020 				 * notification via DAD completion.)
14021 				 */
14022 				if (ipif == ill->ill_ipif)
14023 					ip_rts_ifmsg(ill->ill_ipif);
14024 			}
14025 		} else {
14026 			/*
14027 			 * After link down, we'll need to send a new routing
14028 			 * message when the link comes back, so clear
14029 			 * ipif_addr_ready.
14030 			 */
14031 			ipif->ipif_addr_ready = 0;
14032 		}
14033 	}
14034 
14035 	/*
14036 	 * If we've torn down links, then notify the user right away.
14037 	 */
14038 	if (!went_up)
14039 		ip_rts_ifmsg(ill->ill_ipif);
14040 }
14041 
14042 /*
14043  * Wakeup all threads waiting to enter the ipsq, and sleeping
14044  * on any of the ills in this ipsq. The ill_lock of the ill
14045  * must be held so that waiters don't miss wakeups
14046  */
14047 static void
14048 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14049 {
14050 	phyint_t *phyint;
14051 
14052 	phyint = ipsq->ipsq_phyint_list;
14053 	while (phyint != NULL) {
14054 		if (phyint->phyint_illv4) {
14055 			if (!caller_holds_lock)
14056 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14057 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14058 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14059 			if (!caller_holds_lock)
14060 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14061 		}
14062 		if (phyint->phyint_illv6) {
14063 			if (!caller_holds_lock)
14064 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14065 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14066 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14067 			if (!caller_holds_lock)
14068 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14069 		}
14070 		phyint = phyint->phyint_ipsq_next;
14071 	}
14072 }
14073 
14074 static ipsq_t *
14075 ipsq_create(char *groupname, ip_stack_t *ipst)
14076 {
14077 	ipsq_t	*ipsq;
14078 
14079 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14080 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14081 	if (ipsq == NULL) {
14082 		return (NULL);
14083 	}
14084 
14085 	if (groupname != NULL)
14086 		(void) strcpy(ipsq->ipsq_name, groupname);
14087 	else
14088 		ipsq->ipsq_name[0] = '\0';
14089 
14090 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14091 	ipsq->ipsq_flags |= IPSQ_GROUP;
14092 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14093 	ipst->ips_ipsq_g_head = ipsq;
14094 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14095 	return (ipsq);
14096 }
14097 
14098 /*
14099  * Return an ipsq correspoding to the groupname. If 'create' is true
14100  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14101  * uniquely with an IPMP group. However during IPMP groupname operations,
14102  * multiple IPMP groups may be associated with a single ipsq. But no
14103  * IPMP group can be associated with more than 1 ipsq at any time.
14104  * For example
14105  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14106  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14107  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14108  *
14109  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14110  * status shown below during the execution of the above command.
14111  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14112  *
14113  * After the completion of the above groupname command we return to the stable
14114  * state shown below.
14115  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14116  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14117  *
14118  * Because of the above, we don't search based on the ipsq_name since that
14119  * would miss the correct ipsq during certain windows as shown above.
14120  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14121  * natural state.
14122  */
14123 static ipsq_t *
14124 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14125     ip_stack_t *ipst)
14126 {
14127 	ipsq_t	*ipsq;
14128 	int	group_len;
14129 	phyint_t *phyint;
14130 
14131 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14132 
14133 	group_len = strlen(groupname);
14134 	ASSERT(group_len != 0);
14135 	group_len++;
14136 
14137 	for (ipsq = ipst->ips_ipsq_g_head;
14138 	    ipsq != NULL;
14139 	    ipsq = ipsq->ipsq_next) {
14140 		/*
14141 		 * When an ipsq is being split, and ill_split_ipsq
14142 		 * calls this function, we exclude it from being considered.
14143 		 */
14144 		if (ipsq == exclude_ipsq)
14145 			continue;
14146 
14147 		/*
14148 		 * Compare against the ipsq_name. The groupname change happens
14149 		 * in 2 phases. The 1st phase merges the from group into
14150 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14151 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14152 		 * ipsq_name. At this point the phyint_groupname has not been
14153 		 * updated.
14154 		 */
14155 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14156 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14157 			/*
14158 			 * Verify that an ipmp groupname is exactly
14159 			 * part of 1 ipsq and is not found in any other
14160 			 * ipsq.
14161 			 */
14162 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14163 			    NULL);
14164 			return (ipsq);
14165 		}
14166 
14167 		/*
14168 		 * Comparison against ipsq_name alone is not sufficient.
14169 		 * In the case when groups are currently being
14170 		 * merged, the ipsq could hold other IPMP groups temporarily.
14171 		 * so we walk the phyint list and compare against the
14172 		 * phyint_groupname as well.
14173 		 */
14174 		phyint = ipsq->ipsq_phyint_list;
14175 		while (phyint != NULL) {
14176 			if ((group_len == phyint->phyint_groupname_len) &&
14177 			    (bcmp(phyint->phyint_groupname, groupname,
14178 			    group_len) == 0)) {
14179 				/*
14180 				 * Verify that an ipmp groupname is exactly
14181 				 * part of 1 ipsq and is not found in any other
14182 				 * ipsq.
14183 				 */
14184 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14185 				    ipst) == NULL);
14186 				return (ipsq);
14187 			}
14188 			phyint = phyint->phyint_ipsq_next;
14189 		}
14190 	}
14191 	if (create)
14192 		ipsq = ipsq_create(groupname, ipst);
14193 	return (ipsq);
14194 }
14195 
14196 static void
14197 ipsq_delete(ipsq_t *ipsq)
14198 {
14199 	ipsq_t *nipsq;
14200 	ipsq_t *pipsq = NULL;
14201 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14202 
14203 	/*
14204 	 * We don't hold the ipsq lock, but we are sure no new
14205 	 * messages can land up, since the ipsq_refs is zero.
14206 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14207 	 * is associated with this ipsq. (Lookups are based on ill_name
14208 	 * or phyint_groupname)
14209 	 */
14210 	ASSERT(ipsq->ipsq_refs == 0);
14211 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14212 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14213 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14214 		/*
14215 		 * This is not the ipsq of an IPMP group.
14216 		 */
14217 		ipsq->ipsq_ipst = NULL;
14218 		kmem_free(ipsq, sizeof (ipsq_t));
14219 		return;
14220 	}
14221 
14222 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14223 
14224 	/*
14225 	 * Locate the ipsq  before we can remove it from
14226 	 * the singly linked list of ipsq's.
14227 	 */
14228 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14229 	    nipsq = nipsq->ipsq_next) {
14230 		if (nipsq == ipsq) {
14231 			break;
14232 		}
14233 		pipsq = nipsq;
14234 	}
14235 
14236 	ASSERT(nipsq == ipsq);
14237 
14238 	/* unlink ipsq from the list */
14239 	if (pipsq != NULL)
14240 		pipsq->ipsq_next = ipsq->ipsq_next;
14241 	else
14242 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14243 	ipsq->ipsq_ipst = NULL;
14244 	kmem_free(ipsq, sizeof (ipsq_t));
14245 	rw_exit(&ipst->ips_ill_g_lock);
14246 }
14247 
14248 static void
14249 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14250     queue_t *q)
14251 {
14252 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14253 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14254 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14255 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14256 	ASSERT(current_mp != NULL);
14257 
14258 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14259 	    NEW_OP, NULL);
14260 
14261 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14262 	    new_ipsq->ipsq_xopq_mphead != NULL);
14263 
14264 	/*
14265 	 * move from old ipsq to the new ipsq.
14266 	 */
14267 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14268 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14269 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14270 
14271 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14272 }
14273 
14274 void
14275 ill_group_cleanup(ill_t *ill)
14276 {
14277 	ill_t *ill_v4;
14278 	ill_t *ill_v6;
14279 	ipif_t *ipif;
14280 
14281 	ill_v4 = ill->ill_phyint->phyint_illv4;
14282 	ill_v6 = ill->ill_phyint->phyint_illv6;
14283 
14284 	if (ill_v4 != NULL) {
14285 		mutex_enter(&ill_v4->ill_lock);
14286 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14287 		    ipif = ipif->ipif_next) {
14288 			IPIF_UNMARK_MOVING(ipif);
14289 		}
14290 		ill_v4->ill_up_ipifs = B_FALSE;
14291 		mutex_exit(&ill_v4->ill_lock);
14292 	}
14293 
14294 	if (ill_v6 != NULL) {
14295 		mutex_enter(&ill_v6->ill_lock);
14296 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14297 		    ipif = ipif->ipif_next) {
14298 			IPIF_UNMARK_MOVING(ipif);
14299 		}
14300 		ill_v6->ill_up_ipifs = B_FALSE;
14301 		mutex_exit(&ill_v6->ill_lock);
14302 	}
14303 }
14304 /*
14305  * This function is called when an ill has had a change in its group status
14306  * to bring up all the ipifs that were up before the change.
14307  */
14308 int
14309 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14310 {
14311 	ipif_t *ipif;
14312 	ill_t *ill_v4;
14313 	ill_t *ill_v6;
14314 	ill_t *from_ill;
14315 	int err = 0;
14316 
14317 	ASSERT(IAM_WRITER_ILL(ill));
14318 
14319 	/*
14320 	 * Except for ipif_state_flags and ill_state_flags the other
14321 	 * fields of the ipif/ill that are modified below are protected
14322 	 * implicitly since we are a writer. We would have tried to down
14323 	 * even an ipif that was already down, in ill_down_ipifs. So we
14324 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14325 	 */
14326 	ill_v4 = ill->ill_phyint->phyint_illv4;
14327 	ill_v6 = ill->ill_phyint->phyint_illv6;
14328 	if (ill_v4 != NULL) {
14329 		ill_v4->ill_up_ipifs = B_TRUE;
14330 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14331 		    ipif = ipif->ipif_next) {
14332 			mutex_enter(&ill_v4->ill_lock);
14333 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14334 			IPIF_UNMARK_MOVING(ipif);
14335 			mutex_exit(&ill_v4->ill_lock);
14336 			if (ipif->ipif_was_up) {
14337 				if (!(ipif->ipif_flags & IPIF_UP))
14338 					err = ipif_up(ipif, q, mp);
14339 				ipif->ipif_was_up = B_FALSE;
14340 				if (err != 0) {
14341 					/*
14342 					 * Can there be any other error ?
14343 					 */
14344 					ASSERT(err == EINPROGRESS);
14345 					return (err);
14346 				}
14347 			}
14348 		}
14349 		mutex_enter(&ill_v4->ill_lock);
14350 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14351 		mutex_exit(&ill_v4->ill_lock);
14352 		ill_v4->ill_up_ipifs = B_FALSE;
14353 		if (ill_v4->ill_move_in_progress) {
14354 			ASSERT(ill_v4->ill_move_peer != NULL);
14355 			ill_v4->ill_move_in_progress = B_FALSE;
14356 			from_ill = ill_v4->ill_move_peer;
14357 			from_ill->ill_move_in_progress = B_FALSE;
14358 			from_ill->ill_move_peer = NULL;
14359 			mutex_enter(&from_ill->ill_lock);
14360 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14361 			mutex_exit(&from_ill->ill_lock);
14362 			if (ill_v6 == NULL) {
14363 				if (from_ill->ill_phyint->phyint_flags &
14364 				    PHYI_STANDBY) {
14365 					phyint_inactive(from_ill->ill_phyint);
14366 				}
14367 				if (ill_v4->ill_phyint->phyint_flags &
14368 				    PHYI_STANDBY) {
14369 					phyint_inactive(ill_v4->ill_phyint);
14370 				}
14371 			}
14372 			ill_v4->ill_move_peer = NULL;
14373 		}
14374 	}
14375 
14376 	if (ill_v6 != NULL) {
14377 		ill_v6->ill_up_ipifs = B_TRUE;
14378 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14379 		    ipif = ipif->ipif_next) {
14380 			mutex_enter(&ill_v6->ill_lock);
14381 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14382 			IPIF_UNMARK_MOVING(ipif);
14383 			mutex_exit(&ill_v6->ill_lock);
14384 			if (ipif->ipif_was_up) {
14385 				if (!(ipif->ipif_flags & IPIF_UP))
14386 					err = ipif_up(ipif, q, mp);
14387 				ipif->ipif_was_up = B_FALSE;
14388 				if (err != 0) {
14389 					/*
14390 					 * Can there be any other error ?
14391 					 */
14392 					ASSERT(err == EINPROGRESS);
14393 					return (err);
14394 				}
14395 			}
14396 		}
14397 		mutex_enter(&ill_v6->ill_lock);
14398 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14399 		mutex_exit(&ill_v6->ill_lock);
14400 		ill_v6->ill_up_ipifs = B_FALSE;
14401 		if (ill_v6->ill_move_in_progress) {
14402 			ASSERT(ill_v6->ill_move_peer != NULL);
14403 			ill_v6->ill_move_in_progress = B_FALSE;
14404 			from_ill = ill_v6->ill_move_peer;
14405 			from_ill->ill_move_in_progress = B_FALSE;
14406 			from_ill->ill_move_peer = NULL;
14407 			mutex_enter(&from_ill->ill_lock);
14408 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14409 			mutex_exit(&from_ill->ill_lock);
14410 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14411 				phyint_inactive(from_ill->ill_phyint);
14412 			}
14413 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14414 				phyint_inactive(ill_v6->ill_phyint);
14415 			}
14416 			ill_v6->ill_move_peer = NULL;
14417 		}
14418 	}
14419 	return (0);
14420 }
14421 
14422 /*
14423  * bring down all the approriate ipifs.
14424  */
14425 /* ARGSUSED */
14426 static void
14427 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14428 {
14429 	ipif_t *ipif;
14430 
14431 	ASSERT(IAM_WRITER_ILL(ill));
14432 
14433 	/*
14434 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14435 	 * are modified below are protected implicitly since we are a writer
14436 	 */
14437 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14438 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14439 			continue;
14440 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14441 			/*
14442 			 * We go through the ipif_down logic even if the ipif
14443 			 * is already down, since routes can be added based
14444 			 * on down ipifs. Going through ipif_down once again
14445 			 * will delete any IREs created based on these routes.
14446 			 */
14447 			if (ipif->ipif_flags & IPIF_UP)
14448 				ipif->ipif_was_up = B_TRUE;
14449 			/*
14450 			 * If called with chk_nofailover true ipif is moving.
14451 			 */
14452 			mutex_enter(&ill->ill_lock);
14453 			if (chk_nofailover) {
14454 				ipif->ipif_state_flags |=
14455 				    IPIF_MOVING | IPIF_CHANGING;
14456 			} else {
14457 				ipif->ipif_state_flags |= IPIF_CHANGING;
14458 			}
14459 			mutex_exit(&ill->ill_lock);
14460 			/*
14461 			 * Need to re-create net/subnet bcast ires if
14462 			 * they are dependent on ipif.
14463 			 */
14464 			if (!ipif->ipif_isv6)
14465 				ipif_check_bcast_ires(ipif);
14466 			(void) ipif_logical_down(ipif, NULL, NULL);
14467 			ipif_non_duplicate(ipif);
14468 			ipif_down_tail(ipif);
14469 		}
14470 	}
14471 }
14472 
14473 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14474 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14475 	(ipsq)->ipsq_refs++;				\
14476 }
14477 
14478 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14479 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14480 	(ipsq)->ipsq_refs--;				\
14481 	if ((ipsq)->ipsq_refs == 0)				\
14482 		(ipsq)->ipsq_name[0] = '\0'; 		\
14483 }
14484 
14485 /*
14486  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14487  * new_ipsq.
14488  */
14489 static void
14490 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14491 {
14492 	phyint_t *phyint;
14493 	phyint_t *next_phyint;
14494 
14495 	/*
14496 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14497 	 * writer and the ill_lock of the ill in question. Also the dest
14498 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14499 	 */
14500 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14501 
14502 	phyint = cur_ipsq->ipsq_phyint_list;
14503 	cur_ipsq->ipsq_phyint_list = NULL;
14504 	while (phyint != NULL) {
14505 		next_phyint = phyint->phyint_ipsq_next;
14506 		IPSQ_DEC_REF(cur_ipsq, ipst);
14507 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14508 		new_ipsq->ipsq_phyint_list = phyint;
14509 		IPSQ_INC_REF(new_ipsq, ipst);
14510 		phyint->phyint_ipsq = new_ipsq;
14511 		phyint = next_phyint;
14512 	}
14513 }
14514 
14515 #define	SPLIT_SUCCESS		0
14516 #define	SPLIT_NOT_NEEDED	1
14517 #define	SPLIT_FAILED		2
14518 
14519 int
14520 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14521     ip_stack_t *ipst)
14522 {
14523 	ipsq_t *newipsq = NULL;
14524 
14525 	/*
14526 	 * Assertions denote pre-requisites for changing the ipsq of
14527 	 * a phyint
14528 	 */
14529 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14530 	/*
14531 	 * <ill-phyint> assocs can't change while ill_g_lock
14532 	 * is held as writer. See ill_phyint_reinit()
14533 	 */
14534 	ASSERT(phyint->phyint_illv4 == NULL ||
14535 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14536 	ASSERT(phyint->phyint_illv6 == NULL ||
14537 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14538 
14539 	if ((phyint->phyint_groupname_len !=
14540 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14541 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14542 	    phyint->phyint_groupname_len) != 0)) {
14543 		/*
14544 		 * Once we fail in creating a new ipsq due to memory shortage,
14545 		 * don't attempt to create new ipsq again, based on another
14546 		 * phyint, since we want all phyints belonging to an IPMP group
14547 		 * to be in the same ipsq even in the event of mem alloc fails.
14548 		 */
14549 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14550 		    cur_ipsq, ipst);
14551 		if (newipsq == NULL) {
14552 			/* Memory allocation failure */
14553 			return (SPLIT_FAILED);
14554 		} else {
14555 			/* ipsq_refs protected by ill_g_lock (writer) */
14556 			IPSQ_DEC_REF(cur_ipsq, ipst);
14557 			phyint->phyint_ipsq = newipsq;
14558 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14559 			newipsq->ipsq_phyint_list = phyint;
14560 			IPSQ_INC_REF(newipsq, ipst);
14561 			return (SPLIT_SUCCESS);
14562 		}
14563 	}
14564 	return (SPLIT_NOT_NEEDED);
14565 }
14566 
14567 /*
14568  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14569  * to do this split
14570  */
14571 static int
14572 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14573 {
14574 	ipsq_t *newipsq;
14575 
14576 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14577 	/*
14578 	 * <ill-phyint> assocs can't change while ill_g_lock
14579 	 * is held as writer. See ill_phyint_reinit()
14580 	 */
14581 
14582 	ASSERT(phyint->phyint_illv4 == NULL ||
14583 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14584 	ASSERT(phyint->phyint_illv6 == NULL ||
14585 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14586 
14587 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14588 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14589 		/*
14590 		 * ipsq_init failed due to no memory
14591 		 * caller will use the same ipsq
14592 		 */
14593 		return (SPLIT_FAILED);
14594 	}
14595 
14596 	/* ipsq_ref is protected by ill_g_lock (writer) */
14597 	IPSQ_DEC_REF(cur_ipsq, ipst);
14598 
14599 	/*
14600 	 * This is a new ipsq that is unknown to the world.
14601 	 * So we don't need to hold ipsq_lock,
14602 	 */
14603 	newipsq = phyint->phyint_ipsq;
14604 	newipsq->ipsq_writer = NULL;
14605 	newipsq->ipsq_reentry_cnt--;
14606 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14607 #ifdef DEBUG
14608 	newipsq->ipsq_depth = 0;
14609 #endif
14610 
14611 	return (SPLIT_SUCCESS);
14612 }
14613 
14614 /*
14615  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14616  * ipsq's representing their individual groups or themselves. Return
14617  * whether split needs to be retried again later.
14618  */
14619 static boolean_t
14620 ill_split_ipsq(ipsq_t *cur_ipsq)
14621 {
14622 	phyint_t *phyint;
14623 	phyint_t *next_phyint;
14624 	int	error;
14625 	boolean_t need_retry = B_FALSE;
14626 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14627 
14628 	phyint = cur_ipsq->ipsq_phyint_list;
14629 	cur_ipsq->ipsq_phyint_list = NULL;
14630 	while (phyint != NULL) {
14631 		next_phyint = phyint->phyint_ipsq_next;
14632 		/*
14633 		 * 'created' will tell us whether the callee actually
14634 		 * created an ipsq. Lack of memory may force the callee
14635 		 * to return without creating an ipsq.
14636 		 */
14637 		if (phyint->phyint_groupname == NULL) {
14638 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14639 		} else {
14640 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14641 			    need_retry, ipst);
14642 		}
14643 
14644 		switch (error) {
14645 		case SPLIT_FAILED:
14646 			need_retry = B_TRUE;
14647 			/* FALLTHRU */
14648 		case SPLIT_NOT_NEEDED:
14649 			/*
14650 			 * Keep it on the list.
14651 			 */
14652 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14653 			cur_ipsq->ipsq_phyint_list = phyint;
14654 			break;
14655 		case SPLIT_SUCCESS:
14656 			break;
14657 		default:
14658 			ASSERT(0);
14659 		}
14660 
14661 		phyint = next_phyint;
14662 	}
14663 	return (need_retry);
14664 }
14665 
14666 /*
14667  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14668  * and return the ills in the list. This list will be
14669  * needed to unlock all the ills later on by the caller.
14670  * The <ill-ipsq> associations could change between the
14671  * lock and unlock. Hence the unlock can't traverse the
14672  * ipsq to get the list of ills.
14673  */
14674 static int
14675 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14676 {
14677 	int	cnt = 0;
14678 	phyint_t	*phyint;
14679 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14680 
14681 	/*
14682 	 * The caller holds ill_g_lock to ensure that the ill memberships
14683 	 * of the ipsq don't change
14684 	 */
14685 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14686 
14687 	phyint = ipsq->ipsq_phyint_list;
14688 	while (phyint != NULL) {
14689 		if (phyint->phyint_illv4 != NULL) {
14690 			ASSERT(cnt < list_max);
14691 			list[cnt++] = phyint->phyint_illv4;
14692 		}
14693 		if (phyint->phyint_illv6 != NULL) {
14694 			ASSERT(cnt < list_max);
14695 			list[cnt++] = phyint->phyint_illv6;
14696 		}
14697 		phyint = phyint->phyint_ipsq_next;
14698 	}
14699 	ill_lock_ills(list, cnt);
14700 	return (cnt);
14701 }
14702 
14703 void
14704 ill_lock_ills(ill_t **list, int cnt)
14705 {
14706 	int	i;
14707 
14708 	if (cnt > 1) {
14709 		boolean_t try_again;
14710 		do {
14711 			try_again = B_FALSE;
14712 			for (i = 0; i < cnt - 1; i++) {
14713 				if (list[i] < list[i + 1]) {
14714 					ill_t	*tmp;
14715 
14716 					/* swap the elements */
14717 					tmp = list[i];
14718 					list[i] = list[i + 1];
14719 					list[i + 1] = tmp;
14720 					try_again = B_TRUE;
14721 				}
14722 			}
14723 		} while (try_again);
14724 	}
14725 
14726 	for (i = 0; i < cnt; i++) {
14727 		if (i == 0) {
14728 			if (list[i] != NULL)
14729 				mutex_enter(&list[i]->ill_lock);
14730 			else
14731 				return;
14732 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14733 			mutex_enter(&list[i]->ill_lock);
14734 		}
14735 	}
14736 }
14737 
14738 void
14739 ill_unlock_ills(ill_t **list, int cnt)
14740 {
14741 	int	i;
14742 
14743 	for (i = 0; i < cnt; i++) {
14744 		if ((i == 0) && (list[i] != NULL)) {
14745 			mutex_exit(&list[i]->ill_lock);
14746 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14747 			mutex_exit(&list[i]->ill_lock);
14748 		}
14749 	}
14750 }
14751 
14752 /*
14753  * Merge all the ills from 1 ipsq group into another ipsq group.
14754  * The source ipsq group is specified by the ipsq associated with
14755  * 'from_ill'. The destination ipsq group is specified by the ipsq
14756  * associated with 'to_ill' or 'groupname' respectively.
14757  * Note that ipsq itself does not have a reference count mechanism
14758  * and functions don't look up an ipsq and pass it around. Instead
14759  * functions pass around an ill or groupname, and the ipsq is looked
14760  * up from the ill or groupname and the required operation performed
14761  * atomically with the lookup on the ipsq.
14762  */
14763 static int
14764 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14765     queue_t *q)
14766 {
14767 	ipsq_t *old_ipsq;
14768 	ipsq_t *new_ipsq;
14769 	ill_t	**ill_list;
14770 	int	cnt;
14771 	size_t	ill_list_size;
14772 	boolean_t became_writer_on_new_sq = B_FALSE;
14773 	ip_stack_t	*ipst = from_ill->ill_ipst;
14774 
14775 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14776 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14777 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14778 
14779 	/*
14780 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14781 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14782 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14783 	 */
14784 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14785 
14786 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14787 	if (groupname != NULL)
14788 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14789 	else {
14790 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14791 	}
14792 
14793 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14794 
14795 	/*
14796 	 * both groups are on the same ipsq.
14797 	 */
14798 	if (old_ipsq == new_ipsq) {
14799 		rw_exit(&ipst->ips_ill_g_lock);
14800 		return (0);
14801 	}
14802 
14803 	cnt = old_ipsq->ipsq_refs << 1;
14804 	ill_list_size = cnt * sizeof (ill_t *);
14805 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14806 	if (ill_list == NULL) {
14807 		rw_exit(&ipst->ips_ill_g_lock);
14808 		return (ENOMEM);
14809 	}
14810 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14811 
14812 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14813 	mutex_enter(&new_ipsq->ipsq_lock);
14814 	if ((new_ipsq->ipsq_writer == NULL &&
14815 	    new_ipsq->ipsq_current_ipif == NULL) ||
14816 	    (new_ipsq->ipsq_writer == curthread)) {
14817 		new_ipsq->ipsq_writer = curthread;
14818 		new_ipsq->ipsq_reentry_cnt++;
14819 		became_writer_on_new_sq = B_TRUE;
14820 	}
14821 
14822 	/*
14823 	 * We are holding ill_g_lock as writer and all the ill locks of
14824 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14825 	 * message can land up on the old ipsq even though we don't hold the
14826 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14827 	 */
14828 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14829 
14830 	/*
14831 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14832 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14833 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14834 	 */
14835 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14836 
14837 	/*
14838 	 * Mark the new ipsq as needing a split since it is currently
14839 	 * being shared by more than 1 IPMP group. The split will
14840 	 * occur at the end of ipsq_exit
14841 	 */
14842 	new_ipsq->ipsq_split = B_TRUE;
14843 
14844 	/* Now release all the locks */
14845 	mutex_exit(&new_ipsq->ipsq_lock);
14846 	ill_unlock_ills(ill_list, cnt);
14847 	rw_exit(&ipst->ips_ill_g_lock);
14848 
14849 	kmem_free(ill_list, ill_list_size);
14850 
14851 	/*
14852 	 * If we succeeded in becoming writer on the new ipsq, then
14853 	 * drain the new ipsq and start processing  all enqueued messages
14854 	 * including the current ioctl we are processing which is either
14855 	 * a set groupname or failover/failback.
14856 	 */
14857 	if (became_writer_on_new_sq)
14858 		ipsq_exit(new_ipsq);
14859 
14860 	/*
14861 	 * syncq has been changed and all the messages have been moved.
14862 	 */
14863 	mutex_enter(&old_ipsq->ipsq_lock);
14864 	old_ipsq->ipsq_current_ipif = NULL;
14865 	old_ipsq->ipsq_current_ioctl = 0;
14866 	old_ipsq->ipsq_current_done = B_TRUE;
14867 	mutex_exit(&old_ipsq->ipsq_lock);
14868 	return (EINPROGRESS);
14869 }
14870 
14871 /*
14872  * Delete and add the loopback copy and non-loopback copy of
14873  * the BROADCAST ire corresponding to ill and addr. Used to
14874  * group broadcast ires together when ill becomes part of
14875  * a group.
14876  *
14877  * This function is also called when ill is leaving the group
14878  * so that the ires belonging to the group gets re-grouped.
14879  */
14880 static void
14881 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14882 {
14883 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14884 	ire_t **ire_ptpn = &ire_head;
14885 	ip_stack_t	*ipst = ill->ill_ipst;
14886 
14887 	/*
14888 	 * The loopback and non-loopback IREs are inserted in the order in which
14889 	 * they're found, on the basis that they are correctly ordered (loopback
14890 	 * first).
14891 	 */
14892 	for (;;) {
14893 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14894 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14895 		if (ire == NULL)
14896 			break;
14897 
14898 		/*
14899 		 * we are passing in KM_SLEEP because it is not easy to
14900 		 * go back to a sane state in case of memory failure.
14901 		 */
14902 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14903 		ASSERT(nire != NULL);
14904 		bzero(nire, sizeof (ire_t));
14905 		/*
14906 		 * Don't use ire_max_frag directly since we don't
14907 		 * hold on to 'ire' until we add the new ire 'nire' and
14908 		 * we don't want the new ire to have a dangling reference
14909 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14910 		 * be in sync with the ipif_mtu of the associate ipif.
14911 		 * For eg. this happens as a result of SIOCSLIFNAME,
14912 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14913 		 * the driver. A change in ire_max_frag triggered as
14914 		 * as a result of path mtu discovery, or due to an
14915 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14916 		 * route change -mtu command does not apply to broadcast ires.
14917 		 *
14918 		 * XXX We need a recovery strategy here if ire_init fails
14919 		 */
14920 		if (ire_init(nire,
14921 		    (uchar_t *)&ire->ire_addr,
14922 		    (uchar_t *)&ire->ire_mask,
14923 		    (uchar_t *)&ire->ire_src_addr,
14924 		    (uchar_t *)&ire->ire_gateway_addr,
14925 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14926 		    &ire->ire_ipif->ipif_mtu,
14927 		    ire->ire_nce,
14928 		    ire->ire_rfq,
14929 		    ire->ire_stq,
14930 		    ire->ire_type,
14931 		    ire->ire_ipif,
14932 		    ire->ire_cmask,
14933 		    ire->ire_phandle,
14934 		    ire->ire_ihandle,
14935 		    ire->ire_flags,
14936 		    &ire->ire_uinfo,
14937 		    NULL,
14938 		    NULL,
14939 		    ipst) == NULL) {
14940 			cmn_err(CE_PANIC, "ire_init() failed");
14941 		}
14942 		ire_delete(ire);
14943 		ire_refrele(ire);
14944 
14945 		/*
14946 		 * The newly created IREs are inserted at the tail of the list
14947 		 * starting with ire_head. As we've just allocated them no one
14948 		 * knows about them so it's safe.
14949 		 */
14950 		*ire_ptpn = nire;
14951 		ire_ptpn = &nire->ire_next;
14952 	}
14953 
14954 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14955 		int error;
14956 		ire_t *oire;
14957 		/* unlink the IRE from our list before calling ire_add() */
14958 		nire_next = nire->ire_next;
14959 		nire->ire_next = NULL;
14960 
14961 		/* ire_add adds the ire at the right place in the list */
14962 		oire = nire;
14963 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14964 		ASSERT(error == 0);
14965 		ASSERT(oire == nire);
14966 		ire_refrele(nire);	/* Held in ire_add */
14967 	}
14968 }
14969 
14970 /*
14971  * This function is usually called when an ill is inserted in
14972  * a group and all the ipifs are already UP. As all the ipifs
14973  * are already UP, the broadcast ires have already been created
14974  * and been inserted. But, ire_add_v4 would not have grouped properly.
14975  * We need to re-group for the benefit of ip_wput_ire which
14976  * expects BROADCAST ires to be grouped properly to avoid sending
14977  * more than one copy of the broadcast packet per group.
14978  *
14979  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14980  *	  because when ipif_up_done ends up calling this, ires have
14981  *        already been added before illgrp_insert i.e before ill_group
14982  *	  has been initialized.
14983  */
14984 static void
14985 ill_group_bcast_for_xmit(ill_t *ill)
14986 {
14987 	ill_group_t *illgrp;
14988 	ipif_t *ipif;
14989 	ipaddr_t addr;
14990 	ipaddr_t net_mask;
14991 	ipaddr_t subnet_netmask;
14992 
14993 	illgrp = ill->ill_group;
14994 
14995 	/*
14996 	 * This function is called even when an ill is deleted from
14997 	 * the group. Hence, illgrp could be null.
14998 	 */
14999 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15000 		return;
15001 
15002 	/*
15003 	 * Delete all the BROADCAST ires matching this ill and add
15004 	 * them back. This time, ire_add_v4 should take care of
15005 	 * grouping them with others because ill is part of the
15006 	 * group.
15007 	 */
15008 	ill_bcast_delete_and_add(ill, 0);
15009 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15010 
15011 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15012 
15013 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15014 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15015 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15016 		} else {
15017 			net_mask = htonl(IN_CLASSA_NET);
15018 		}
15019 		addr = net_mask & ipif->ipif_subnet;
15020 		ill_bcast_delete_and_add(ill, addr);
15021 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15022 
15023 		subnet_netmask = ipif->ipif_net_mask;
15024 		addr = ipif->ipif_subnet;
15025 		ill_bcast_delete_and_add(ill, addr);
15026 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15027 	}
15028 }
15029 
15030 /*
15031  * This function is called from illgrp_delete when ill is being deleted
15032  * from the group.
15033  *
15034  * As ill is not there in the group anymore, any address belonging
15035  * to this ill should be cleared of IRE_MARK_NORECV.
15036  */
15037 static void
15038 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15039 {
15040 	ire_t *ire;
15041 	irb_t *irb;
15042 	ip_stack_t	*ipst = ill->ill_ipst;
15043 
15044 	ASSERT(ill->ill_group == NULL);
15045 
15046 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15047 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15048 
15049 	if (ire != NULL) {
15050 		/*
15051 		 * IPMP and plumbing operations are serialized on the ipsq, so
15052 		 * no one will insert or delete a broadcast ire under our feet.
15053 		 */
15054 		irb = ire->ire_bucket;
15055 		rw_enter(&irb->irb_lock, RW_READER);
15056 		ire_refrele(ire);
15057 
15058 		for (; ire != NULL; ire = ire->ire_next) {
15059 			if (ire->ire_addr != addr)
15060 				break;
15061 			if (ire_to_ill(ire) != ill)
15062 				continue;
15063 
15064 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15065 			ire->ire_marks &= ~IRE_MARK_NORECV;
15066 		}
15067 		rw_exit(&irb->irb_lock);
15068 	}
15069 }
15070 
15071 /*
15072  * This function must be called only after the broadcast ires
15073  * have been grouped together. For a given address addr, nominate
15074  * only one of the ires whose interface is not FAILED or OFFLINE.
15075  *
15076  * This is also called when an ipif goes down, so that we can nominate
15077  * a different ire with the same address for receiving.
15078  */
15079 static void
15080 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15081 {
15082 	irb_t *irb;
15083 	ire_t *ire;
15084 	ire_t *ire1;
15085 	ire_t *save_ire;
15086 	ire_t **irep = NULL;
15087 	boolean_t first = B_TRUE;
15088 	ire_t *clear_ire = NULL;
15089 	ire_t *start_ire = NULL;
15090 	ire_t	*new_lb_ire;
15091 	ire_t	*new_nlb_ire;
15092 	boolean_t new_lb_ire_used = B_FALSE;
15093 	boolean_t new_nlb_ire_used = B_FALSE;
15094 	uint64_t match_flags;
15095 	uint64_t phyi_flags;
15096 	boolean_t fallback = B_FALSE;
15097 	uint_t	max_frag;
15098 
15099 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15100 	    NULL, MATCH_IRE_TYPE, ipst);
15101 	/*
15102 	 * We may not be able to find some ires if a previous
15103 	 * ire_create failed. This happens when an ipif goes
15104 	 * down and we are unable to create BROADCAST ires due
15105 	 * to memory failure. Thus, we have to check for NULL
15106 	 * below. This should handle the case for LOOPBACK,
15107 	 * POINTOPOINT and interfaces with some POINTOPOINT
15108 	 * logicals for which there are no BROADCAST ires.
15109 	 */
15110 	if (ire == NULL)
15111 		return;
15112 	/*
15113 	 * Currently IRE_BROADCASTS are deleted when an ipif
15114 	 * goes down which runs exclusively. Thus, setting
15115 	 * IRE_MARK_RCVD should not race with ire_delete marking
15116 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15117 	 * be consistent with other parts of the code that walks
15118 	 * a given bucket.
15119 	 */
15120 	save_ire = ire;
15121 	irb = ire->ire_bucket;
15122 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15123 	if (new_lb_ire == NULL) {
15124 		ire_refrele(ire);
15125 		return;
15126 	}
15127 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15128 	if (new_nlb_ire == NULL) {
15129 		ire_refrele(ire);
15130 		kmem_cache_free(ire_cache, new_lb_ire);
15131 		return;
15132 	}
15133 	IRB_REFHOLD(irb);
15134 	rw_enter(&irb->irb_lock, RW_WRITER);
15135 	/*
15136 	 * Get to the first ire matching the address and the
15137 	 * group. If the address does not match we are done
15138 	 * as we could not find the IRE. If the address matches
15139 	 * we should get to the first one matching the group.
15140 	 */
15141 	while (ire != NULL) {
15142 		if (ire->ire_addr != addr ||
15143 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15144 			break;
15145 		}
15146 		ire = ire->ire_next;
15147 	}
15148 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15149 	start_ire = ire;
15150 redo:
15151 	while (ire != NULL && ire->ire_addr == addr &&
15152 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15153 		/*
15154 		 * The first ire for any address within a group
15155 		 * should always be the one with IRE_MARK_NORECV cleared
15156 		 * so that ip_wput_ire can avoid searching for one.
15157 		 * Note down the insertion point which will be used
15158 		 * later.
15159 		 */
15160 		if (first && (irep == NULL))
15161 			irep = ire->ire_ptpn;
15162 		/*
15163 		 * PHYI_FAILED is set when the interface fails.
15164 		 * This interface might have become good, but the
15165 		 * daemon has not yet detected. We should still
15166 		 * not receive on this. PHYI_OFFLINE should never
15167 		 * be picked as this has been offlined and soon
15168 		 * be removed.
15169 		 */
15170 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15171 		if (phyi_flags & PHYI_OFFLINE) {
15172 			ire->ire_marks |= IRE_MARK_NORECV;
15173 			ire = ire->ire_next;
15174 			continue;
15175 		}
15176 		if (phyi_flags & match_flags) {
15177 			ire->ire_marks |= IRE_MARK_NORECV;
15178 			ire = ire->ire_next;
15179 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15180 			    PHYI_INACTIVE) {
15181 				fallback = B_TRUE;
15182 			}
15183 			continue;
15184 		}
15185 		if (first) {
15186 			/*
15187 			 * We will move this to the front of the list later
15188 			 * on.
15189 			 */
15190 			clear_ire = ire;
15191 			ire->ire_marks &= ~IRE_MARK_NORECV;
15192 		} else {
15193 			ire->ire_marks |= IRE_MARK_NORECV;
15194 		}
15195 		first = B_FALSE;
15196 		ire = ire->ire_next;
15197 	}
15198 	/*
15199 	 * If we never nominated anybody, try nominating at least
15200 	 * an INACTIVE, if we found one. Do it only once though.
15201 	 */
15202 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15203 	    fallback) {
15204 		match_flags = PHYI_FAILED;
15205 		ire = start_ire;
15206 		irep = NULL;
15207 		goto redo;
15208 	}
15209 	ire_refrele(save_ire);
15210 
15211 	/*
15212 	 * irep non-NULL indicates that we entered the while loop
15213 	 * above. If clear_ire is at the insertion point, we don't
15214 	 * have to do anything. clear_ire will be NULL if all the
15215 	 * interfaces are failed.
15216 	 *
15217 	 * We cannot unlink and reinsert the ire at the right place
15218 	 * in the list since there can be other walkers of this bucket.
15219 	 * Instead we delete and recreate the ire
15220 	 */
15221 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15222 		ire_t *clear_ire_stq = NULL;
15223 
15224 		bzero(new_lb_ire, sizeof (ire_t));
15225 		/* XXX We need a recovery strategy here. */
15226 		if (ire_init(new_lb_ire,
15227 		    (uchar_t *)&clear_ire->ire_addr,
15228 		    (uchar_t *)&clear_ire->ire_mask,
15229 		    (uchar_t *)&clear_ire->ire_src_addr,
15230 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15231 		    &clear_ire->ire_max_frag,
15232 		    NULL, /* let ire_nce_init derive the resolver info */
15233 		    clear_ire->ire_rfq,
15234 		    clear_ire->ire_stq,
15235 		    clear_ire->ire_type,
15236 		    clear_ire->ire_ipif,
15237 		    clear_ire->ire_cmask,
15238 		    clear_ire->ire_phandle,
15239 		    clear_ire->ire_ihandle,
15240 		    clear_ire->ire_flags,
15241 		    &clear_ire->ire_uinfo,
15242 		    NULL,
15243 		    NULL,
15244 		    ipst) == NULL)
15245 			cmn_err(CE_PANIC, "ire_init() failed");
15246 		if (clear_ire->ire_stq == NULL) {
15247 			ire_t *ire_next = clear_ire->ire_next;
15248 			if (ire_next != NULL &&
15249 			    ire_next->ire_stq != NULL &&
15250 			    ire_next->ire_addr == clear_ire->ire_addr &&
15251 			    ire_next->ire_ipif->ipif_ill ==
15252 			    clear_ire->ire_ipif->ipif_ill) {
15253 				clear_ire_stq = ire_next;
15254 
15255 				bzero(new_nlb_ire, sizeof (ire_t));
15256 				/* XXX We need a recovery strategy here. */
15257 				if (ire_init(new_nlb_ire,
15258 				    (uchar_t *)&clear_ire_stq->ire_addr,
15259 				    (uchar_t *)&clear_ire_stq->ire_mask,
15260 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15261 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15262 				    &clear_ire_stq->ire_max_frag,
15263 				    NULL,
15264 				    clear_ire_stq->ire_rfq,
15265 				    clear_ire_stq->ire_stq,
15266 				    clear_ire_stq->ire_type,
15267 				    clear_ire_stq->ire_ipif,
15268 				    clear_ire_stq->ire_cmask,
15269 				    clear_ire_stq->ire_phandle,
15270 				    clear_ire_stq->ire_ihandle,
15271 				    clear_ire_stq->ire_flags,
15272 				    &clear_ire_stq->ire_uinfo,
15273 				    NULL,
15274 				    NULL,
15275 				    ipst) == NULL)
15276 					cmn_err(CE_PANIC, "ire_init() failed");
15277 			}
15278 		}
15279 
15280 		/*
15281 		 * Delete the ire. We can't call ire_delete() since
15282 		 * we are holding the bucket lock. We can't release the
15283 		 * bucket lock since we can't allow irep to change. So just
15284 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15285 		 * ire from the list and do the refrele.
15286 		 */
15287 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15288 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15289 
15290 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15291 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15292 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15293 		}
15294 
15295 		/*
15296 		 * Also take care of otherfields like ib/ob pkt count
15297 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15298 		 */
15299 
15300 		/* Set the max_frag before adding the ire */
15301 		max_frag = *new_lb_ire->ire_max_fragp;
15302 		new_lb_ire->ire_max_fragp = NULL;
15303 		new_lb_ire->ire_max_frag = max_frag;
15304 
15305 		/* Add the new ire's. Insert at *irep */
15306 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15307 		ire1 = *irep;
15308 		if (ire1 != NULL)
15309 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15310 		new_lb_ire->ire_next = ire1;
15311 		/* Link the new one in. */
15312 		new_lb_ire->ire_ptpn = irep;
15313 		membar_producer();
15314 		*irep = new_lb_ire;
15315 		new_lb_ire_used = B_TRUE;
15316 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15317 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15318 		DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), new_lb_ire->ire_ipif,
15319 		    (char *), "ire", (void *), new_lb_ire);
15320 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15321 
15322 		if (clear_ire_stq != NULL) {
15323 			/* Set the max_frag before adding the ire */
15324 			max_frag = *new_nlb_ire->ire_max_fragp;
15325 			new_nlb_ire->ire_max_fragp = NULL;
15326 			new_nlb_ire->ire_max_frag = max_frag;
15327 
15328 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15329 			irep = &new_lb_ire->ire_next;
15330 			/* Add the new ire. Insert at *irep */
15331 			ire1 = *irep;
15332 			if (ire1 != NULL)
15333 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15334 			new_nlb_ire->ire_next = ire1;
15335 			/* Link the new one in. */
15336 			new_nlb_ire->ire_ptpn = irep;
15337 			membar_producer();
15338 			*irep = new_nlb_ire;
15339 			new_nlb_ire_used = B_TRUE;
15340 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15341 			    ire_stats_inserted);
15342 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15343 			DTRACE_PROBE3(ipif__incr__cnt,
15344 			    (ipif_t *), new_nlb_ire->ire_ipif,
15345 			    (char *), "ire", (void *), new_nlb_ire);
15346 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15347 			DTRACE_PROBE3(ill__incr__cnt,
15348 			    (ill_t *), new_nlb_ire->ire_stq->q_ptr,
15349 			    (char *), "ire", (void *), new_nlb_ire);
15350 			((ill_t *)(new_nlb_ire->ire_stq->q_ptr))->ill_ire_cnt++;
15351 		}
15352 	}
15353 	rw_exit(&irb->irb_lock);
15354 	if (!new_lb_ire_used)
15355 		kmem_cache_free(ire_cache, new_lb_ire);
15356 	if (!new_nlb_ire_used)
15357 		kmem_cache_free(ire_cache, new_nlb_ire);
15358 	IRB_REFRELE(irb);
15359 }
15360 
15361 /*
15362  * Whenever an ipif goes down we have to renominate a different
15363  * broadcast ire to receive. Whenever an ipif comes up, we need
15364  * to make sure that we have only one nominated to receive.
15365  */
15366 static void
15367 ipif_renominate_bcast(ipif_t *ipif)
15368 {
15369 	ill_t *ill = ipif->ipif_ill;
15370 	ipaddr_t subnet_addr;
15371 	ipaddr_t net_addr;
15372 	ipaddr_t net_mask = 0;
15373 	ipaddr_t subnet_netmask;
15374 	ipaddr_t addr;
15375 	ill_group_t *illgrp;
15376 	ip_stack_t	*ipst = ill->ill_ipst;
15377 
15378 	illgrp = ill->ill_group;
15379 	/*
15380 	 * If this is the last ipif going down, it might take
15381 	 * the ill out of the group. In that case ipif_down ->
15382 	 * illgrp_delete takes care of doing the nomination.
15383 	 * ipif_down does not call for this case.
15384 	 */
15385 	ASSERT(illgrp != NULL);
15386 
15387 	/* There could not have been any ires associated with this */
15388 	if (ipif->ipif_subnet == 0)
15389 		return;
15390 
15391 	ill_mark_bcast(illgrp, 0, ipst);
15392 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15393 
15394 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15395 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15396 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15397 	} else {
15398 		net_mask = htonl(IN_CLASSA_NET);
15399 	}
15400 	addr = net_mask & ipif->ipif_subnet;
15401 	ill_mark_bcast(illgrp, addr, ipst);
15402 
15403 	net_addr = ~net_mask | addr;
15404 	ill_mark_bcast(illgrp, net_addr, ipst);
15405 
15406 	subnet_netmask = ipif->ipif_net_mask;
15407 	addr = ipif->ipif_subnet;
15408 	ill_mark_bcast(illgrp, addr, ipst);
15409 
15410 	subnet_addr = ~subnet_netmask | addr;
15411 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15412 }
15413 
15414 /*
15415  * Whenever we form or delete ill groups, we need to nominate one set of
15416  * BROADCAST ires for receiving in the group.
15417  *
15418  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15419  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15420  *    for ill_ipif_up_count to be non-zero. This is the only case where
15421  *    ill_ipif_up_count is zero and we would still find the ires.
15422  *
15423  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15424  *    ipif is UP and we just have to do the nomination.
15425  *
15426  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15427  *    from the group. So, we have to do the nomination.
15428  *
15429  * Because of (3), there could be just one ill in the group. But we have
15430  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15431  * Thus, this function does not optimize when there is only one ill as
15432  * it is not correct for (3).
15433  */
15434 static void
15435 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15436 {
15437 	ill_t *ill;
15438 	ipif_t *ipif;
15439 	ipaddr_t subnet_addr;
15440 	ipaddr_t prev_subnet_addr = 0;
15441 	ipaddr_t net_addr;
15442 	ipaddr_t prev_net_addr = 0;
15443 	ipaddr_t net_mask = 0;
15444 	ipaddr_t subnet_netmask;
15445 	ipaddr_t addr;
15446 	ip_stack_t	*ipst;
15447 
15448 	/*
15449 	 * When the last memeber is leaving, there is nothing to
15450 	 * nominate.
15451 	 */
15452 	if (illgrp->illgrp_ill_count == 0) {
15453 		ASSERT(illgrp->illgrp_ill == NULL);
15454 		return;
15455 	}
15456 
15457 	ill = illgrp->illgrp_ill;
15458 	ASSERT(!ill->ill_isv6);
15459 	ipst = ill->ill_ipst;
15460 	/*
15461 	 * We assume that ires with same address and belonging to the
15462 	 * same group, has been grouped together. Nominating a *single*
15463 	 * ill in the group for sending and receiving broadcast is done
15464 	 * by making sure that the first BROADCAST ire (which will be
15465 	 * the one returned by ire_ctable_lookup for ip_rput and the
15466 	 * one that will be used in ip_wput_ire) will be the one that
15467 	 * will not have IRE_MARK_NORECV set.
15468 	 *
15469 	 * 1) ip_rput checks and discards packets received on ires marked
15470 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15471 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15472 	 *    first ire in the group for every broadcast address in the group.
15473 	 *    ip_rput will accept packets only on the first ire i.e only
15474 	 *    one copy of the ill.
15475 	 *
15476 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15477 	 *    packet for the whole group. It needs to send out on the ill
15478 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15479 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15480 	 *    the copy echoed back on other port where the ire is not marked
15481 	 *    with IRE_MARK_NORECV.
15482 	 *
15483 	 * Note that we just need to have the first IRE either loopback or
15484 	 * non-loopback (either of them may not exist if ire_create failed
15485 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15486 	 * always hit the first one and hence will always accept one copy.
15487 	 *
15488 	 * We have a broadcast ire per ill for all the unique prefixes
15489 	 * hosted on that ill. As we don't have a way of knowing the
15490 	 * unique prefixes on a given ill and hence in the whole group,
15491 	 * we just call ill_mark_bcast on all the prefixes that exist
15492 	 * in the group. For the common case of one prefix, the code
15493 	 * below optimizes by remebering the last address used for
15494 	 * markng. In the case of multiple prefixes, this will still
15495 	 * optimize depending the order of prefixes.
15496 	 *
15497 	 * The only unique address across the whole group is 0.0.0.0 and
15498 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15499 	 * the first ire in the bucket for receiving and disables the
15500 	 * others.
15501 	 */
15502 	ill_mark_bcast(illgrp, 0, ipst);
15503 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15504 	for (; ill != NULL; ill = ill->ill_group_next) {
15505 
15506 		for (ipif = ill->ill_ipif; ipif != NULL;
15507 		    ipif = ipif->ipif_next) {
15508 
15509 			if (!(ipif->ipif_flags & IPIF_UP) ||
15510 			    ipif->ipif_subnet == 0) {
15511 				continue;
15512 			}
15513 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15514 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15515 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15516 			} else {
15517 				net_mask = htonl(IN_CLASSA_NET);
15518 			}
15519 			addr = net_mask & ipif->ipif_subnet;
15520 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15521 				ill_mark_bcast(illgrp, addr, ipst);
15522 				net_addr = ~net_mask | addr;
15523 				ill_mark_bcast(illgrp, net_addr, ipst);
15524 			}
15525 			prev_net_addr = addr;
15526 
15527 			subnet_netmask = ipif->ipif_net_mask;
15528 			addr = ipif->ipif_subnet;
15529 			if (prev_subnet_addr == 0 ||
15530 			    prev_subnet_addr != addr) {
15531 				ill_mark_bcast(illgrp, addr, ipst);
15532 				subnet_addr = ~subnet_netmask | addr;
15533 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15534 			}
15535 			prev_subnet_addr = addr;
15536 		}
15537 	}
15538 }
15539 
15540 /*
15541  * This function is called while forming ill groups.
15542  *
15543  * Currently, we handle only allmulti groups. We want to join
15544  * allmulti on only one of the ills in the groups. In future,
15545  * when we have link aggregation, we may have to join normal
15546  * multicast groups on multiple ills as switch does inbound load
15547  * balancing. Following are the functions that calls this
15548  * function :
15549  *
15550  * 1) ill_recover_multicast : Interface is coming back UP.
15551  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15552  *    will call ill_recover_multicast to recover all the multicast
15553  *    groups. We need to make sure that only one member is joined
15554  *    in the ill group.
15555  *
15556  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15557  *    Somebody is joining allmulti. We need to make sure that only one
15558  *    member is joined in the group.
15559  *
15560  * 3) illgrp_insert : If allmulti has already joined, we need to make
15561  *    sure that only one member is joined in the group.
15562  *
15563  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15564  *    allmulti who we have nominated. We need to pick someother ill.
15565  *
15566  * 5) illgrp_delete : The ill we nominated is leaving the group,
15567  *    we need to pick a new ill to join the group.
15568  *
15569  * For (1), (2), (5) - we just have to check whether there is
15570  * a good ill joined in the group. If we could not find any ills
15571  * joined the group, we should join.
15572  *
15573  * For (4), the one that was nominated to receive, left the group.
15574  * There could be nobody joined in the group when this function is
15575  * called.
15576  *
15577  * For (3) - we need to explicitly check whether there are multiple
15578  * ills joined in the group.
15579  *
15580  * For simplicity, we don't differentiate any of the above cases. We
15581  * just leave the group if it is joined on any of them and join on
15582  * the first good ill.
15583  */
15584 int
15585 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15586 {
15587 	ilm_t *ilm;
15588 	ill_t *ill;
15589 	ill_t *fallback_inactive_ill = NULL;
15590 	ill_t *fallback_failed_ill = NULL;
15591 	int ret = 0;
15592 
15593 	/*
15594 	 * Leave the allmulti on all the ills and start fresh.
15595 	 */
15596 	for (ill = illgrp->illgrp_ill; ill != NULL;
15597 	    ill = ill->ill_group_next) {
15598 		if (ill->ill_join_allmulti)
15599 			(void) ip_leave_allmulti(ill->ill_ipif);
15600 	}
15601 
15602 	/*
15603 	 * Choose a good ill. Fallback to inactive or failed if
15604 	 * none available. We need to fallback to FAILED in the
15605 	 * case where we have 2 interfaces in a group - where
15606 	 * one of them is failed and another is a good one and
15607 	 * the good one (not marked inactive) is leaving the group.
15608 	 */
15609 	ret = 0;
15610 	for (ill = illgrp->illgrp_ill; ill != NULL;
15611 	    ill = ill->ill_group_next) {
15612 		/* Never pick an offline interface */
15613 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15614 			continue;
15615 
15616 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15617 			fallback_failed_ill = ill;
15618 			continue;
15619 		}
15620 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15621 			fallback_inactive_ill = ill;
15622 			continue;
15623 		}
15624 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15625 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15626 				ret = ip_join_allmulti(ill->ill_ipif);
15627 				/*
15628 				 * ip_join_allmulti can fail because of memory
15629 				 * failures. So, make sure we join at least
15630 				 * on one ill.
15631 				 */
15632 				if (ill->ill_join_allmulti)
15633 					return (0);
15634 			}
15635 		}
15636 	}
15637 	if (ret != 0) {
15638 		/*
15639 		 * If we tried nominating above and failed to do so,
15640 		 * return error. We might have tried multiple times.
15641 		 * But, return the latest error.
15642 		 */
15643 		return (ret);
15644 	}
15645 	if ((ill = fallback_inactive_ill) != NULL) {
15646 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15647 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15648 				ret = ip_join_allmulti(ill->ill_ipif);
15649 				return (ret);
15650 			}
15651 		}
15652 	} else if ((ill = fallback_failed_ill) != NULL) {
15653 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15654 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15655 				ret = ip_join_allmulti(ill->ill_ipif);
15656 				return (ret);
15657 			}
15658 		}
15659 	}
15660 	return (0);
15661 }
15662 
15663 /*
15664  * This function is called from illgrp_delete after it is
15665  * deleted from the group to reschedule responsibilities
15666  * to a different ill.
15667  */
15668 static void
15669 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15670 {
15671 	ilm_t	*ilm;
15672 	ipif_t	*ipif;
15673 	ipaddr_t subnet_addr;
15674 	ipaddr_t net_addr;
15675 	ipaddr_t net_mask = 0;
15676 	ipaddr_t subnet_netmask;
15677 	ipaddr_t addr;
15678 	ip_stack_t *ipst = ill->ill_ipst;
15679 
15680 	ASSERT(ill->ill_group == NULL);
15681 	/*
15682 	 * Broadcast Responsibility:
15683 	 *
15684 	 * 1. If this ill has been nominated for receiving broadcast
15685 	 * packets, we need to find a new one. Before we find a new
15686 	 * one, we need to re-group the ires that are part of this new
15687 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15688 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15689 	 * thing for us.
15690 	 *
15691 	 * 2. If this ill was not nominated for receiving broadcast
15692 	 * packets, we need to clear the IRE_MARK_NORECV flag
15693 	 * so that we continue to send up broadcast packets.
15694 	 */
15695 	if (!ill->ill_isv6) {
15696 		/*
15697 		 * Case 1 above : No optimization here. Just redo the
15698 		 * nomination.
15699 		 */
15700 		ill_group_bcast_for_xmit(ill);
15701 		ill_nominate_bcast_rcv(illgrp);
15702 
15703 		/*
15704 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15705 		 */
15706 		ill_clear_bcast_mark(ill, 0);
15707 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15708 
15709 		for (ipif = ill->ill_ipif; ipif != NULL;
15710 		    ipif = ipif->ipif_next) {
15711 
15712 			if (!(ipif->ipif_flags & IPIF_UP) ||
15713 			    ipif->ipif_subnet == 0) {
15714 				continue;
15715 			}
15716 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15717 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15718 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15719 			} else {
15720 				net_mask = htonl(IN_CLASSA_NET);
15721 			}
15722 			addr = net_mask & ipif->ipif_subnet;
15723 			ill_clear_bcast_mark(ill, addr);
15724 
15725 			net_addr = ~net_mask | addr;
15726 			ill_clear_bcast_mark(ill, net_addr);
15727 
15728 			subnet_netmask = ipif->ipif_net_mask;
15729 			addr = ipif->ipif_subnet;
15730 			ill_clear_bcast_mark(ill, addr);
15731 
15732 			subnet_addr = ~subnet_netmask | addr;
15733 			ill_clear_bcast_mark(ill, subnet_addr);
15734 		}
15735 	}
15736 
15737 	/*
15738 	 * Multicast Responsibility.
15739 	 *
15740 	 * If we have joined allmulti on this one, find a new member
15741 	 * in the group to join allmulti. As this ill is already part
15742 	 * of allmulti, we don't have to join on this one.
15743 	 *
15744 	 * If we have not joined allmulti on this one, there is no
15745 	 * responsibility to handoff. But we need to take new
15746 	 * responsibility i.e, join allmulti on this one if we need
15747 	 * to.
15748 	 */
15749 	if (ill->ill_join_allmulti) {
15750 		(void) ill_nominate_mcast_rcv(illgrp);
15751 	} else {
15752 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15753 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15754 				(void) ip_join_allmulti(ill->ill_ipif);
15755 				break;
15756 			}
15757 		}
15758 	}
15759 
15760 	/*
15761 	 * We intentionally do the flushing of IRE_CACHES only matching
15762 	 * on the ill and not on groups. Note that we are already deleted
15763 	 * from the group.
15764 	 *
15765 	 * This will make sure that all IRE_CACHES whose stq is pointing
15766 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15767 	 * deleted and IRE_CACHES that are not pointing at this ill will
15768 	 * be left alone.
15769 	 */
15770 	ire_walk_ill(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_CACHE,
15771 	    illgrp_cache_delete, ill, ill);
15772 
15773 	/*
15774 	 * Some conn may have cached one of the IREs deleted above. By removing
15775 	 * the ire reference, we clean up the extra reference to the ill held in
15776 	 * ire->ire_stq.
15777 	 */
15778 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15779 
15780 	/*
15781 	 * Re-do source address selection for all the members in the
15782 	 * group, if they borrowed source address from one of the ipifs
15783 	 * in this ill.
15784 	 */
15785 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15786 		if (ill->ill_isv6) {
15787 			ipif_update_other_ipifs_v6(ipif, illgrp);
15788 		} else {
15789 			ipif_update_other_ipifs(ipif, illgrp);
15790 		}
15791 	}
15792 }
15793 
15794 /*
15795  * Delete the ill from the group. The caller makes sure that it is
15796  * in a group and it okay to delete from the group. So, we always
15797  * delete here.
15798  */
15799 static void
15800 illgrp_delete(ill_t *ill)
15801 {
15802 	ill_group_t *illgrp;
15803 	ill_group_t *tmpg;
15804 	ill_t *tmp_ill;
15805 	ip_stack_t	*ipst = ill->ill_ipst;
15806 
15807 	/*
15808 	 * Reset illgrp_ill_schednext if it was pointing at us.
15809 	 * We need to do this before we set ill_group to NULL.
15810 	 */
15811 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15812 	mutex_enter(&ill->ill_lock);
15813 
15814 	illgrp_reset_schednext(ill);
15815 
15816 	illgrp = ill->ill_group;
15817 
15818 	/* Delete the ill from illgrp. */
15819 	if (illgrp->illgrp_ill == ill) {
15820 		illgrp->illgrp_ill = ill->ill_group_next;
15821 	} else {
15822 		tmp_ill = illgrp->illgrp_ill;
15823 		while (tmp_ill->ill_group_next != ill) {
15824 			tmp_ill = tmp_ill->ill_group_next;
15825 			ASSERT(tmp_ill != NULL);
15826 		}
15827 		tmp_ill->ill_group_next = ill->ill_group_next;
15828 	}
15829 	ill->ill_group = NULL;
15830 	ill->ill_group_next = NULL;
15831 
15832 	illgrp->illgrp_ill_count--;
15833 	mutex_exit(&ill->ill_lock);
15834 	rw_exit(&ipst->ips_ill_g_lock);
15835 
15836 	/*
15837 	 * As this ill is leaving the group, we need to hand off
15838 	 * the responsibilities to the other ills in the group, if
15839 	 * this ill had some responsibilities.
15840 	 */
15841 
15842 	ill_handoff_responsibility(ill, illgrp);
15843 
15844 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15845 
15846 	if (illgrp->illgrp_ill_count == 0) {
15847 
15848 		ASSERT(illgrp->illgrp_ill == NULL);
15849 		if (ill->ill_isv6) {
15850 			if (illgrp == ipst->ips_illgrp_head_v6) {
15851 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15852 			} else {
15853 				tmpg = ipst->ips_illgrp_head_v6;
15854 				while (tmpg->illgrp_next != illgrp) {
15855 					tmpg = tmpg->illgrp_next;
15856 					ASSERT(tmpg != NULL);
15857 				}
15858 				tmpg->illgrp_next = illgrp->illgrp_next;
15859 			}
15860 		} else {
15861 			if (illgrp == ipst->ips_illgrp_head_v4) {
15862 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15863 			} else {
15864 				tmpg = ipst->ips_illgrp_head_v4;
15865 				while (tmpg->illgrp_next != illgrp) {
15866 					tmpg = tmpg->illgrp_next;
15867 					ASSERT(tmpg != NULL);
15868 				}
15869 				tmpg->illgrp_next = illgrp->illgrp_next;
15870 			}
15871 		}
15872 		mutex_destroy(&illgrp->illgrp_lock);
15873 		mi_free(illgrp);
15874 	}
15875 	rw_exit(&ipst->ips_ill_g_lock);
15876 
15877 	/*
15878 	 * Even though the ill is out of the group its not necessary
15879 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15880 	 * We will split the ipsq when phyint_groupname is set to NULL.
15881 	 */
15882 
15883 	/*
15884 	 * Send a routing sockets message if we are deleting from
15885 	 * groups with names.
15886 	 */
15887 	if (ill->ill_phyint->phyint_groupname_len != 0)
15888 		ip_rts_ifmsg(ill->ill_ipif);
15889 }
15890 
15891 /*
15892  * Re-do source address selection. This is normally called when
15893  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15894  * ipif comes up.
15895  */
15896 void
15897 ill_update_source_selection(ill_t *ill)
15898 {
15899 	ipif_t *ipif;
15900 
15901 	ASSERT(IAM_WRITER_ILL(ill));
15902 
15903 	if (ill->ill_group != NULL)
15904 		ill = ill->ill_group->illgrp_ill;
15905 
15906 	for (; ill != NULL; ill = ill->ill_group_next) {
15907 		for (ipif = ill->ill_ipif; ipif != NULL;
15908 		    ipif = ipif->ipif_next) {
15909 			if (ill->ill_isv6)
15910 				ipif_recreate_interface_routes_v6(NULL, ipif);
15911 			else
15912 				ipif_recreate_interface_routes(NULL, ipif);
15913 		}
15914 	}
15915 }
15916 
15917 /*
15918  * Insert ill in a group headed by illgrp_head. The caller can either
15919  * pass a groupname in which case we search for a group with the
15920  * same name to insert in or pass a group to insert in. This function
15921  * would only search groups with names.
15922  *
15923  * NOTE : The caller should make sure that there is at least one ipif
15924  *	  UP on this ill so that illgrp_scheduler can pick this ill
15925  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15926  *	  already sent a DL_UNBIND to the driver and we don't want to
15927  *	  send anymore packets. We don't assert for ipif_up_count
15928  *	  to be greater than zero, because ipif_up_done wants to call
15929  *	  this function before bumping up the ipif_up_count. See
15930  *	  ipif_up_done() for details.
15931  */
15932 int
15933 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15934     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15935 {
15936 	ill_group_t *illgrp;
15937 	ill_t *prev_ill;
15938 	phyint_t *phyi;
15939 	ip_stack_t	*ipst = ill->ill_ipst;
15940 
15941 	ASSERT(ill->ill_group == NULL);
15942 
15943 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15944 	mutex_enter(&ill->ill_lock);
15945 
15946 	if (groupname != NULL) {
15947 		/*
15948 		 * Look for a group with a matching groupname to insert.
15949 		 */
15950 		for (illgrp = *illgrp_head; illgrp != NULL;
15951 		    illgrp = illgrp->illgrp_next) {
15952 
15953 			ill_t *tmp_ill;
15954 
15955 			/*
15956 			 * If we have an ill_group_t in the list which has
15957 			 * no ill_t assigned then we must be in the process of
15958 			 * removing this group. We skip this as illgrp_delete()
15959 			 * will remove it from the list.
15960 			 */
15961 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15962 				ASSERT(illgrp->illgrp_ill_count == 0);
15963 				continue;
15964 			}
15965 
15966 			ASSERT(tmp_ill->ill_phyint != NULL);
15967 			phyi = tmp_ill->ill_phyint;
15968 			/*
15969 			 * Look at groups which has names only.
15970 			 */
15971 			if (phyi->phyint_groupname_len == 0)
15972 				continue;
15973 			/*
15974 			 * Names are stored in the phyint common to both
15975 			 * IPv4 and IPv6.
15976 			 */
15977 			if (mi_strcmp(phyi->phyint_groupname,
15978 			    groupname) == 0) {
15979 				break;
15980 			}
15981 		}
15982 	} else {
15983 		/*
15984 		 * If the caller passes in a NULL "grp_to_insert", we
15985 		 * allocate one below and insert this singleton.
15986 		 */
15987 		illgrp = grp_to_insert;
15988 	}
15989 
15990 	ill->ill_group_next = NULL;
15991 
15992 	if (illgrp == NULL) {
15993 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
15994 		if (illgrp == NULL) {
15995 			return (ENOMEM);
15996 		}
15997 		illgrp->illgrp_next = *illgrp_head;
15998 		*illgrp_head = illgrp;
15999 		illgrp->illgrp_ill = ill;
16000 		illgrp->illgrp_ill_count = 1;
16001 		ill->ill_group = illgrp;
16002 		/*
16003 		 * Used in illgrp_scheduler to protect multiple threads
16004 		 * from traversing the list.
16005 		 */
16006 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16007 	} else {
16008 		ASSERT(ill->ill_net_type ==
16009 		    illgrp->illgrp_ill->ill_net_type);
16010 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16011 
16012 		/* Insert ill at tail of this group */
16013 		prev_ill = illgrp->illgrp_ill;
16014 		while (prev_ill->ill_group_next != NULL)
16015 			prev_ill = prev_ill->ill_group_next;
16016 		prev_ill->ill_group_next = ill;
16017 		ill->ill_group = illgrp;
16018 		illgrp->illgrp_ill_count++;
16019 		/*
16020 		 * Inherit group properties. Currently only forwarding
16021 		 * is the property we try to keep the same with all the
16022 		 * ills. When there are more, we will abstract this into
16023 		 * a function.
16024 		 */
16025 		ill->ill_flags &= ~ILLF_ROUTER;
16026 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16027 	}
16028 	mutex_exit(&ill->ill_lock);
16029 	rw_exit(&ipst->ips_ill_g_lock);
16030 
16031 	/*
16032 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16033 	 *    may be zero as it has not yet been bumped. But the ires
16034 	 *    have already been added. So, we do the nomination here
16035 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16036 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16037 	 *    ill_ipif_up_count here while nominating broadcast ires for
16038 	 *    receive.
16039 	 *
16040 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16041 	 *    to group them properly as ire_add() has already happened
16042 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16043 	 *    case, we need to do it here anyway.
16044 	 */
16045 	if (!ill->ill_isv6) {
16046 		ill_group_bcast_for_xmit(ill);
16047 		ill_nominate_bcast_rcv(illgrp);
16048 	}
16049 
16050 	if (!ipif_is_coming_up) {
16051 		/*
16052 		 * When ipif_up_done() calls this function, the multicast
16053 		 * groups have not been joined yet. So, there is no point in
16054 		 * nomination. ip_join_allmulti will handle groups when
16055 		 * ill_recover_multicast is called from ipif_up_done() later.
16056 		 */
16057 		(void) ill_nominate_mcast_rcv(illgrp);
16058 		/*
16059 		 * ipif_up_done calls ill_update_source_selection
16060 		 * anyway. Moreover, we don't want to re-create
16061 		 * interface routes while ipif_up_done() still has reference
16062 		 * to them. Refer to ipif_up_done() for more details.
16063 		 */
16064 		ill_update_source_selection(ill);
16065 	}
16066 
16067 	/*
16068 	 * Send a routing sockets message if we are inserting into
16069 	 * groups with names.
16070 	 */
16071 	if (groupname != NULL)
16072 		ip_rts_ifmsg(ill->ill_ipif);
16073 	return (0);
16074 }
16075 
16076 /*
16077  * Return the first phyint matching the groupname. There could
16078  * be more than one when there are ill groups.
16079  *
16080  * If 'usable' is set, then we exclude ones that are marked with any of
16081  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16082  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16083  * emulation of ipmp.
16084  */
16085 phyint_t *
16086 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16087 {
16088 	phyint_t *phyi;
16089 
16090 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16091 	/*
16092 	 * Group names are stored in the phyint - a common structure
16093 	 * to both IPv4 and IPv6.
16094 	 */
16095 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16096 	for (; phyi != NULL;
16097 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16098 	    phyi, AVL_AFTER)) {
16099 		if (phyi->phyint_groupname_len == 0)
16100 			continue;
16101 		/*
16102 		 * Skip the ones that should not be used since the callers
16103 		 * sometime use this for sending packets.
16104 		 */
16105 		if (usable && (phyi->phyint_flags &
16106 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16107 			continue;
16108 
16109 		ASSERT(phyi->phyint_groupname != NULL);
16110 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16111 			return (phyi);
16112 	}
16113 	return (NULL);
16114 }
16115 
16116 
16117 /*
16118  * Return the first usable phyint matching the group index. By 'usable'
16119  * we exclude ones that are marked ununsable with any of
16120  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16121  *
16122  * Used only for the ipmp/netinfo emulation of ipmp.
16123  */
16124 phyint_t *
16125 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16126 {
16127 	phyint_t *phyi;
16128 
16129 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16130 
16131 	if (!ipst->ips_ipmp_hook_emulation)
16132 		return (NULL);
16133 
16134 	/*
16135 	 * Group indicies are stored in the phyint - a common structure
16136 	 * to both IPv4 and IPv6.
16137 	 */
16138 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16139 	for (; phyi != NULL;
16140 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16141 	    phyi, AVL_AFTER)) {
16142 		/* Ignore the ones that do not have a group */
16143 		if (phyi->phyint_groupname_len == 0)
16144 			continue;
16145 
16146 		ASSERT(phyi->phyint_group_ifindex != 0);
16147 		/*
16148 		 * Skip the ones that should not be used since the callers
16149 		 * sometime use this for sending packets.
16150 		 */
16151 		if (phyi->phyint_flags &
16152 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16153 			continue;
16154 		if (phyi->phyint_group_ifindex == group_ifindex)
16155 			return (phyi);
16156 	}
16157 	return (NULL);
16158 }
16159 
16160 /*
16161  * MT notes on creation and deletion of IPMP groups
16162  *
16163  * Creation and deletion of IPMP groups introduce the need to merge or
16164  * split the associated serialization objects i.e the ipsq's. Normally all
16165  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16166  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16167  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16168  * is a need to change the <ill-ipsq> association and we have to operate on both
16169  * the source and destination IPMP groups. For eg. attempting to set the
16170  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16171  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16172  * source or destination IPMP group are mapped to a single ipsq for executing
16173  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16174  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16175  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16176  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16177  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16178  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16179  *
16180  * In the above example the ioctl handling code locates the current ipsq of hme0
16181  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16182  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16183  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16184  * the destination ipsq. If the destination ipsq is not busy, it also enters
16185  * the destination ipsq exclusively. Now the actual groupname setting operation
16186  * can proceed. If the destination ipsq is busy, the operation is enqueued
16187  * on the destination (merged) ipsq and will be handled in the unwind from
16188  * ipsq_exit.
16189  *
16190  * To prevent other threads accessing the ill while the group name change is
16191  * in progres, we bring down the ipifs which also removes the ill from the
16192  * group. The group is changed in phyint and when the first ipif on the ill
16193  * is brought up, the ill is inserted into the right IPMP group by
16194  * illgrp_insert.
16195  */
16196 /* ARGSUSED */
16197 int
16198 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16199     ip_ioctl_cmd_t *ipip, void *ifreq)
16200 {
16201 	int i;
16202 	char *tmp;
16203 	int namelen;
16204 	ill_t *ill = ipif->ipif_ill;
16205 	ill_t *ill_v4, *ill_v6;
16206 	int err = 0;
16207 	phyint_t *phyi;
16208 	phyint_t *phyi_tmp;
16209 	struct lifreq *lifr;
16210 	mblk_t	*mp1;
16211 	char *groupname;
16212 	ipsq_t *ipsq;
16213 	ip_stack_t	*ipst = ill->ill_ipst;
16214 
16215 	ASSERT(IAM_WRITER_IPIF(ipif));
16216 
16217 	/* Existance verified in ip_wput_nondata */
16218 	mp1 = mp->b_cont->b_cont;
16219 	lifr = (struct lifreq *)mp1->b_rptr;
16220 	groupname = lifr->lifr_groupname;
16221 
16222 	if (ipif->ipif_id != 0)
16223 		return (EINVAL);
16224 
16225 	phyi = ill->ill_phyint;
16226 	ASSERT(phyi != NULL);
16227 
16228 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16229 		return (EINVAL);
16230 
16231 	tmp = groupname;
16232 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16233 		;
16234 
16235 	if (i == LIFNAMSIZ) {
16236 		/* no null termination */
16237 		return (EINVAL);
16238 	}
16239 
16240 	/*
16241 	 * Calculate the namelen exclusive of the null
16242 	 * termination character.
16243 	 */
16244 	namelen = tmp - groupname;
16245 
16246 	ill_v4 = phyi->phyint_illv4;
16247 	ill_v6 = phyi->phyint_illv6;
16248 
16249 	/*
16250 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16251 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16252 	 * synchronization notes in ip.c
16253 	 */
16254 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16255 		return (EINVAL);
16256 	}
16257 
16258 	/*
16259 	 * mark the ill as changing.
16260 	 * this should queue all new requests on the syncq.
16261 	 */
16262 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16263 
16264 	if (ill_v4 != NULL)
16265 		ill_v4->ill_state_flags |= ILL_CHANGING;
16266 	if (ill_v6 != NULL)
16267 		ill_v6->ill_state_flags |= ILL_CHANGING;
16268 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16269 
16270 	if (namelen == 0) {
16271 		/*
16272 		 * Null string means remove this interface from the
16273 		 * existing group.
16274 		 */
16275 		if (phyi->phyint_groupname_len == 0) {
16276 			/*
16277 			 * Never was in a group.
16278 			 */
16279 			err = 0;
16280 			goto done;
16281 		}
16282 
16283 		/*
16284 		 * IPv4 or IPv6 may be temporarily out of the group when all
16285 		 * the ipifs are down. Thus, we need to check for ill_group to
16286 		 * be non-NULL.
16287 		 */
16288 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16289 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16290 			mutex_enter(&ill_v4->ill_lock);
16291 			if (!ill_is_quiescent(ill_v4)) {
16292 				/*
16293 				 * ipsq_pending_mp_add will not fail since
16294 				 * connp is NULL
16295 				 */
16296 				(void) ipsq_pending_mp_add(NULL,
16297 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16298 				mutex_exit(&ill_v4->ill_lock);
16299 				err = EINPROGRESS;
16300 				goto done;
16301 			}
16302 			mutex_exit(&ill_v4->ill_lock);
16303 		}
16304 
16305 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16306 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16307 			mutex_enter(&ill_v6->ill_lock);
16308 			if (!ill_is_quiescent(ill_v6)) {
16309 				(void) ipsq_pending_mp_add(NULL,
16310 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16311 				mutex_exit(&ill_v6->ill_lock);
16312 				err = EINPROGRESS;
16313 				goto done;
16314 			}
16315 			mutex_exit(&ill_v6->ill_lock);
16316 		}
16317 
16318 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16319 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16320 		mutex_enter(&phyi->phyint_lock);
16321 		ASSERT(phyi->phyint_groupname != NULL);
16322 		mi_free(phyi->phyint_groupname);
16323 		phyi->phyint_groupname = NULL;
16324 		phyi->phyint_groupname_len = 0;
16325 
16326 		/* Restore the ifindex used to be the per interface one */
16327 		phyi->phyint_group_ifindex = 0;
16328 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16329 		mutex_exit(&phyi->phyint_lock);
16330 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16331 		rw_exit(&ipst->ips_ill_g_lock);
16332 		err = ill_up_ipifs(ill, q, mp);
16333 
16334 		/*
16335 		 * set the split flag so that the ipsq can be split
16336 		 */
16337 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16338 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16339 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16340 
16341 	} else {
16342 		if (phyi->phyint_groupname_len != 0) {
16343 			ASSERT(phyi->phyint_groupname != NULL);
16344 			/* Are we inserting in the same group ? */
16345 			if (mi_strcmp(groupname,
16346 			    phyi->phyint_groupname) == 0) {
16347 				err = 0;
16348 				goto done;
16349 			}
16350 		}
16351 
16352 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16353 		/*
16354 		 * Merge ipsq for the group's.
16355 		 * This check is here as multiple groups/ills might be
16356 		 * sharing the same ipsq.
16357 		 * If we have to merege than the operation is restarted
16358 		 * on the new ipsq.
16359 		 */
16360 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16361 		if (phyi->phyint_ipsq != ipsq) {
16362 			rw_exit(&ipst->ips_ill_g_lock);
16363 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16364 			goto done;
16365 		}
16366 		/*
16367 		 * Running exclusive on new ipsq.
16368 		 */
16369 
16370 		ASSERT(ipsq != NULL);
16371 		ASSERT(ipsq->ipsq_writer == curthread);
16372 
16373 		/*
16374 		 * Check whether the ill_type and ill_net_type matches before
16375 		 * we allocate any memory so that the cleanup is easier.
16376 		 *
16377 		 * We can't group dissimilar ones as we can't load spread
16378 		 * packets across the group because of potential link-level
16379 		 * header differences.
16380 		 */
16381 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16382 		if (phyi_tmp != NULL) {
16383 			if ((ill_v4 != NULL &&
16384 			    phyi_tmp->phyint_illv4 != NULL) &&
16385 			    ((ill_v4->ill_net_type !=
16386 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16387 			    (ill_v4->ill_type !=
16388 			    phyi_tmp->phyint_illv4->ill_type))) {
16389 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16390 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16391 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16392 				rw_exit(&ipst->ips_ill_g_lock);
16393 				return (EINVAL);
16394 			}
16395 			if ((ill_v6 != NULL &&
16396 			    phyi_tmp->phyint_illv6 != NULL) &&
16397 			    ((ill_v6->ill_net_type !=
16398 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16399 			    (ill_v6->ill_type !=
16400 			    phyi_tmp->phyint_illv6->ill_type))) {
16401 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16402 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16403 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16404 				rw_exit(&ipst->ips_ill_g_lock);
16405 				return (EINVAL);
16406 			}
16407 		}
16408 
16409 		rw_exit(&ipst->ips_ill_g_lock);
16410 
16411 		/*
16412 		 * bring down all v4 ipifs.
16413 		 */
16414 		if (ill_v4 != NULL) {
16415 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16416 		}
16417 
16418 		/*
16419 		 * bring down all v6 ipifs.
16420 		 */
16421 		if (ill_v6 != NULL) {
16422 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16423 		}
16424 
16425 		/*
16426 		 * make sure all ipifs are down and there are no active
16427 		 * references. Call to ipsq_pending_mp_add will not fail
16428 		 * since connp is NULL.
16429 		 */
16430 		if (ill_v4 != NULL) {
16431 			mutex_enter(&ill_v4->ill_lock);
16432 			if (!ill_is_quiescent(ill_v4)) {
16433 				(void) ipsq_pending_mp_add(NULL,
16434 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16435 				mutex_exit(&ill_v4->ill_lock);
16436 				err = EINPROGRESS;
16437 				goto done;
16438 			}
16439 			mutex_exit(&ill_v4->ill_lock);
16440 		}
16441 
16442 		if (ill_v6 != NULL) {
16443 			mutex_enter(&ill_v6->ill_lock);
16444 			if (!ill_is_quiescent(ill_v6)) {
16445 				(void) ipsq_pending_mp_add(NULL,
16446 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16447 				mutex_exit(&ill_v6->ill_lock);
16448 				err = EINPROGRESS;
16449 				goto done;
16450 			}
16451 			mutex_exit(&ill_v6->ill_lock);
16452 		}
16453 
16454 		/*
16455 		 * allocate including space for null terminator
16456 		 * before we insert.
16457 		 */
16458 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16459 		if (tmp == NULL)
16460 			return (ENOMEM);
16461 
16462 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16463 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16464 		mutex_enter(&phyi->phyint_lock);
16465 		if (phyi->phyint_groupname_len != 0) {
16466 			ASSERT(phyi->phyint_groupname != NULL);
16467 			mi_free(phyi->phyint_groupname);
16468 		}
16469 
16470 		/*
16471 		 * setup the new group name.
16472 		 */
16473 		phyi->phyint_groupname = tmp;
16474 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16475 		phyi->phyint_groupname_len = namelen + 1;
16476 
16477 		if (ipst->ips_ipmp_hook_emulation) {
16478 			/*
16479 			 * If the group already exists we use the existing
16480 			 * group_ifindex, otherwise we pick a new index here.
16481 			 */
16482 			if (phyi_tmp != NULL) {
16483 				phyi->phyint_group_ifindex =
16484 				    phyi_tmp->phyint_group_ifindex;
16485 			} else {
16486 				/* XXX We need a recovery strategy here. */
16487 				if (!ip_assign_ifindex(
16488 				    &phyi->phyint_group_ifindex, ipst))
16489 					cmn_err(CE_PANIC,
16490 					    "ip_assign_ifindex() failed");
16491 			}
16492 		}
16493 		/*
16494 		 * Select whether the netinfo and hook use the per-interface
16495 		 * or per-group ifindex.
16496 		 */
16497 		if (ipst->ips_ipmp_hook_emulation)
16498 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16499 		else
16500 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16501 
16502 		if (ipst->ips_ipmp_hook_emulation &&
16503 		    phyi_tmp != NULL) {
16504 			/* First phyint in group - group PLUMB event */
16505 			ill_nic_info_plumb(ill, B_TRUE);
16506 		}
16507 		mutex_exit(&phyi->phyint_lock);
16508 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16509 		rw_exit(&ipst->ips_ill_g_lock);
16510 
16511 		err = ill_up_ipifs(ill, q, mp);
16512 	}
16513 
16514 done:
16515 	/*
16516 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16517 	 */
16518 	if (err != EINPROGRESS) {
16519 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16520 		if (ill_v4 != NULL)
16521 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16522 		if (ill_v6 != NULL)
16523 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16524 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16525 	}
16526 	return (err);
16527 }
16528 
16529 /* ARGSUSED */
16530 int
16531 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16532     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16533 {
16534 	ill_t *ill;
16535 	phyint_t *phyi;
16536 	struct lifreq *lifr;
16537 	mblk_t	*mp1;
16538 
16539 	/* Existence verified in ip_wput_nondata */
16540 	mp1 = mp->b_cont->b_cont;
16541 	lifr = (struct lifreq *)mp1->b_rptr;
16542 	ill = ipif->ipif_ill;
16543 	phyi = ill->ill_phyint;
16544 
16545 	lifr->lifr_groupname[0] = '\0';
16546 	/*
16547 	 * ill_group may be null if all the interfaces
16548 	 * are down. But still, the phyint should always
16549 	 * hold the name.
16550 	 */
16551 	if (phyi->phyint_groupname_len != 0) {
16552 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16553 		    phyi->phyint_groupname_len);
16554 	}
16555 
16556 	return (0);
16557 }
16558 
16559 
16560 typedef struct conn_move_s {
16561 	ill_t	*cm_from_ill;
16562 	ill_t	*cm_to_ill;
16563 	int	cm_ifindex;
16564 } conn_move_t;
16565 
16566 /*
16567  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16568  */
16569 static void
16570 conn_move(conn_t *connp, caddr_t arg)
16571 {
16572 	conn_move_t *connm;
16573 	int ifindex;
16574 	int i;
16575 	ill_t *from_ill;
16576 	ill_t *to_ill;
16577 	ilg_t *ilg;
16578 	ilm_t *ret_ilm;
16579 
16580 	connm = (conn_move_t *)arg;
16581 	ifindex = connm->cm_ifindex;
16582 	from_ill = connm->cm_from_ill;
16583 	to_ill = connm->cm_to_ill;
16584 
16585 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16586 
16587 	/* All multicast fields protected by conn_lock */
16588 	mutex_enter(&connp->conn_lock);
16589 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16590 	if ((connp->conn_outgoing_ill == from_ill) &&
16591 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16592 		connp->conn_outgoing_ill = to_ill;
16593 		connp->conn_incoming_ill = to_ill;
16594 	}
16595 
16596 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16597 
16598 	if ((connp->conn_multicast_ill == from_ill) &&
16599 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16600 		connp->conn_multicast_ill = connm->cm_to_ill;
16601 	}
16602 
16603 	/*
16604 	 * Change the ilg_ill to point to the new one. This assumes
16605 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16606 	 * has been told to receive packets on this interface.
16607 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16608 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16609 	 * some ilms may not have moved. We check to see whether
16610 	 * the ilms have moved to to_ill. We can't check on from_ill
16611 	 * as in the process of moving, we could have split an ilm
16612 	 * in to two - which has the same orig_ifindex and v6group.
16613 	 *
16614 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16615 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16616 	 */
16617 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16618 		ilg = &connp->conn_ilg[i];
16619 		if ((ilg->ilg_ill == from_ill) &&
16620 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16621 			/* ifindex != 0 indicates failback */
16622 			if (ifindex != 0) {
16623 				connp->conn_ilg[i].ilg_ill = to_ill;
16624 				continue;
16625 			}
16626 
16627 			mutex_enter(&to_ill->ill_lock);
16628 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16629 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16630 			    connp->conn_zoneid);
16631 			mutex_exit(&to_ill->ill_lock);
16632 
16633 			if (ret_ilm != NULL)
16634 				connp->conn_ilg[i].ilg_ill = to_ill;
16635 		}
16636 	}
16637 	mutex_exit(&connp->conn_lock);
16638 }
16639 
16640 static void
16641 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16642 {
16643 	conn_move_t connm;
16644 	ip_stack_t	*ipst = from_ill->ill_ipst;
16645 
16646 	connm.cm_from_ill = from_ill;
16647 	connm.cm_to_ill = to_ill;
16648 	connm.cm_ifindex = ifindex;
16649 
16650 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16651 }
16652 
16653 /*
16654  * ilm has been moved from from_ill to to_ill.
16655  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16656  * appropriately.
16657  *
16658  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16659  *	  the code there de-references ipif_ill to get the ill to
16660  *	  send multicast requests. It does not work as ipif is on its
16661  *	  move and already moved when this function is called.
16662  *	  Thus, we need to use from_ill and to_ill send down multicast
16663  *	  requests.
16664  */
16665 static void
16666 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16667 {
16668 	ipif_t *ipif;
16669 	ilm_t *ilm;
16670 
16671 	/*
16672 	 * See whether we need to send down DL_ENABMULTI_REQ on
16673 	 * to_ill as ilm has just been added.
16674 	 */
16675 	ASSERT(IAM_WRITER_ILL(to_ill));
16676 	ASSERT(IAM_WRITER_ILL(from_ill));
16677 
16678 	ILM_WALKER_HOLD(to_ill);
16679 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16680 
16681 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16682 			continue;
16683 		/*
16684 		 * no locks held, ill/ipif cannot dissappear as long
16685 		 * as we are writer.
16686 		 */
16687 		ipif = to_ill->ill_ipif;
16688 		/*
16689 		 * No need to hold any lock as we are the writer and this
16690 		 * can only be changed by a writer.
16691 		 */
16692 		ilm->ilm_is_new = B_FALSE;
16693 
16694 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16695 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16696 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16697 			    "resolver\n"));
16698 			continue;		/* Must be IRE_IF_NORESOLVER */
16699 		}
16700 
16701 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16702 			ip1dbg(("ilm_send_multicast_reqs: "
16703 			    "to_ill MULTI_BCAST\n"));
16704 			goto from;
16705 		}
16706 
16707 		if (to_ill->ill_isv6)
16708 			mld_joingroup(ilm);
16709 		else
16710 			igmp_joingroup(ilm);
16711 
16712 		if (to_ill->ill_ipif_up_count == 0) {
16713 			/*
16714 			 * Nobody there. All multicast addresses will be
16715 			 * re-joined when we get the DL_BIND_ACK bringing the
16716 			 * interface up.
16717 			 */
16718 			ilm->ilm_notify_driver = B_FALSE;
16719 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16720 			goto from;
16721 		}
16722 
16723 		/*
16724 		 * For allmulti address, we want to join on only one interface.
16725 		 * Checking for ilm_numentries_v6 is not correct as you may
16726 		 * find an ilm with zero address on to_ill, but we may not
16727 		 * have nominated to_ill for receiving. Thus, if we have
16728 		 * nominated from_ill (ill_join_allmulti is set), nominate
16729 		 * only if to_ill is not already nominated (to_ill normally
16730 		 * should not have been nominated if "from_ill" has already
16731 		 * been nominated. As we don't prevent failovers from happening
16732 		 * across groups, we don't assert).
16733 		 */
16734 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16735 			/*
16736 			 * There is no need to hold ill locks as we are
16737 			 * writer on both ills and when ill_join_allmulti
16738 			 * is changed the thread is always a writer.
16739 			 */
16740 			if (from_ill->ill_join_allmulti &&
16741 			    !to_ill->ill_join_allmulti) {
16742 				(void) ip_join_allmulti(to_ill->ill_ipif);
16743 			}
16744 		} else if (ilm->ilm_notify_driver) {
16745 
16746 			/*
16747 			 * This is a newly moved ilm so we need to tell the
16748 			 * driver about the new group. There can be more than
16749 			 * one ilm's for the same group in the list each with a
16750 			 * different orig_ifindex. We have to inform the driver
16751 			 * once. In ilm_move_v[4,6] we only set the flag
16752 			 * ilm_notify_driver for the first ilm.
16753 			 */
16754 
16755 			(void) ip_ll_send_enabmulti_req(to_ill,
16756 			    &ilm->ilm_v6addr);
16757 		}
16758 
16759 		ilm->ilm_notify_driver = B_FALSE;
16760 
16761 		/*
16762 		 * See whether we need to send down DL_DISABMULTI_REQ on
16763 		 * from_ill as ilm has just been removed.
16764 		 */
16765 from:
16766 		ipif = from_ill->ill_ipif;
16767 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16768 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16769 			ip1dbg(("ilm_send_multicast_reqs: "
16770 			    "from_ill not resolver\n"));
16771 			continue;		/* Must be IRE_IF_NORESOLVER */
16772 		}
16773 
16774 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16775 			ip1dbg(("ilm_send_multicast_reqs: "
16776 			    "from_ill MULTI_BCAST\n"));
16777 			continue;
16778 		}
16779 
16780 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16781 			if (from_ill->ill_join_allmulti)
16782 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16783 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16784 			(void) ip_ll_send_disabmulti_req(from_ill,
16785 			    &ilm->ilm_v6addr);
16786 		}
16787 	}
16788 	ILM_WALKER_RELE(to_ill);
16789 }
16790 
16791 /*
16792  * This function is called when all multicast memberships needs
16793  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16794  * called only once unlike the IPv4 counterpart where it is called after
16795  * every logical interface is moved. The reason is due to multicast
16796  * memberships are joined using an interface address in IPv4 while in
16797  * IPv6, interface index is used.
16798  */
16799 static void
16800 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16801 {
16802 	ilm_t	*ilm;
16803 	ilm_t	*ilm_next;
16804 	ilm_t	*new_ilm;
16805 	ilm_t	**ilmp;
16806 	int	count;
16807 	char buf[INET6_ADDRSTRLEN];
16808 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16809 	ip_stack_t	*ipst = from_ill->ill_ipst;
16810 
16811 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16812 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16813 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16814 
16815 	if (ifindex == 0) {
16816 		/*
16817 		 * Form the solicited node mcast address which is used later.
16818 		 */
16819 		ipif_t *ipif;
16820 
16821 		ipif = from_ill->ill_ipif;
16822 		ASSERT(ipif->ipif_id == 0);
16823 
16824 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16825 	}
16826 
16827 	ilmp = &from_ill->ill_ilm;
16828 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16829 		ilm_next = ilm->ilm_next;
16830 
16831 		if (ilm->ilm_flags & ILM_DELETED) {
16832 			ilmp = &ilm->ilm_next;
16833 			continue;
16834 		}
16835 
16836 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16837 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16838 		ASSERT(ilm->ilm_orig_ifindex != 0);
16839 		if (ilm->ilm_orig_ifindex == ifindex) {
16840 			/*
16841 			 * We are failing back multicast memberships.
16842 			 * If the same ilm exists in to_ill, it means somebody
16843 			 * has joined the same group there e.g. ff02::1
16844 			 * is joined within the kernel when the interfaces
16845 			 * came UP.
16846 			 */
16847 			ASSERT(ilm->ilm_ipif == NULL);
16848 			if (new_ilm != NULL) {
16849 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16850 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16851 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16852 					new_ilm->ilm_is_new = B_TRUE;
16853 				}
16854 			} else {
16855 				/*
16856 				 * check if we can just move the ilm
16857 				 */
16858 				if (from_ill->ill_ilm_walker_cnt != 0) {
16859 					/*
16860 					 * We have walkers we cannot move
16861 					 * the ilm, so allocate a new ilm,
16862 					 * this (old) ilm will be marked
16863 					 * ILM_DELETED at the end of the loop
16864 					 * and will be freed when the
16865 					 * last walker exits.
16866 					 */
16867 					new_ilm = (ilm_t *)mi_zalloc
16868 					    (sizeof (ilm_t));
16869 					if (new_ilm == NULL) {
16870 						ip0dbg(("ilm_move_v6: "
16871 						    "FAILBACK of IPv6"
16872 						    " multicast address %s : "
16873 						    "from %s to"
16874 						    " %s failed : ENOMEM \n",
16875 						    inet_ntop(AF_INET6,
16876 						    &ilm->ilm_v6addr, buf,
16877 						    sizeof (buf)),
16878 						    from_ill->ill_name,
16879 						    to_ill->ill_name));
16880 
16881 							ilmp = &ilm->ilm_next;
16882 							continue;
16883 					}
16884 					*new_ilm = *ilm;
16885 					/*
16886 					 * we don't want new_ilm linked to
16887 					 * ilm's filter list.
16888 					 */
16889 					new_ilm->ilm_filter = NULL;
16890 				} else {
16891 					/*
16892 					 * No walkers we can move the ilm.
16893 					 * lets take it out of the list.
16894 					 */
16895 					*ilmp = ilm->ilm_next;
16896 					ilm->ilm_next = NULL;
16897 					DTRACE_PROBE3(ill__decr__cnt,
16898 					    (ill_t *), from_ill,
16899 					    (char *), "ilm", (void *), ilm);
16900 					ASSERT(from_ill->ill_ilm_cnt > 0);
16901 					from_ill->ill_ilm_cnt--;
16902 
16903 					new_ilm = ilm;
16904 				}
16905 
16906 				/*
16907 				 * if this is the first ilm for the group
16908 				 * set ilm_notify_driver so that we notify the
16909 				 * driver in ilm_send_multicast_reqs.
16910 				 */
16911 				if (ilm_lookup_ill_v6(to_ill,
16912 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16913 					new_ilm->ilm_notify_driver = B_TRUE;
16914 
16915 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
16916 				    (char *), "ilm", (void *), new_ilm);
16917 				new_ilm->ilm_ill = to_ill;
16918 				to_ill->ill_ilm_cnt++;
16919 
16920 				/* Add to the to_ill's list */
16921 				new_ilm->ilm_next = to_ill->ill_ilm;
16922 				to_ill->ill_ilm = new_ilm;
16923 				/*
16924 				 * set the flag so that mld_joingroup is
16925 				 * called in ilm_send_multicast_reqs().
16926 				 */
16927 				new_ilm->ilm_is_new = B_TRUE;
16928 			}
16929 			goto bottom;
16930 		} else if (ifindex != 0) {
16931 			/*
16932 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16933 			 * has not matched above, look at the next ilm.
16934 			 */
16935 			ilmp = &ilm->ilm_next;
16936 			continue;
16937 		}
16938 		/*
16939 		 * If we are here, it means ifindex is 0. Failover
16940 		 * everything.
16941 		 *
16942 		 * We need to handle solicited node mcast address
16943 		 * and all_nodes mcast address differently as they
16944 		 * are joined witin the kenrel (ipif_multicast_up)
16945 		 * and potentially from the userland. We are called
16946 		 * after the ipifs of from_ill has been moved.
16947 		 * If we still find ilms on ill with solicited node
16948 		 * mcast address or all_nodes mcast address, it must
16949 		 * belong to the UP interface that has not moved e.g.
16950 		 * ipif_id 0 with the link local prefix does not move.
16951 		 * We join this on the new ill accounting for all the
16952 		 * userland memberships so that applications don't
16953 		 * see any failure.
16954 		 *
16955 		 * We need to make sure that we account only for the
16956 		 * solicited node and all node multicast addresses
16957 		 * that was brought UP on these. In the case of
16958 		 * a failover from A to B, we might have ilms belonging
16959 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16960 		 * for the membership from the userland. If we are failing
16961 		 * over from B to C now, we will find the ones belonging
16962 		 * to A on B. These don't account for the ill_ipif_up_count.
16963 		 * They just move from B to C. The check below on
16964 		 * ilm_orig_ifindex ensures that.
16965 		 */
16966 		if ((ilm->ilm_orig_ifindex ==
16967 		    from_ill->ill_phyint->phyint_ifindex) &&
16968 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16969 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16970 		    &ilm->ilm_v6addr))) {
16971 			ASSERT(ilm->ilm_refcnt > 0);
16972 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16973 			/*
16974 			 * For indentation reasons, we are not using a
16975 			 * "else" here.
16976 			 */
16977 			if (count == 0) {
16978 				ilmp = &ilm->ilm_next;
16979 				continue;
16980 			}
16981 			ilm->ilm_refcnt -= count;
16982 			if (new_ilm != NULL) {
16983 				/*
16984 				 * Can find one with the same
16985 				 * ilm_orig_ifindex, if we are failing
16986 				 * over to a STANDBY. This happens
16987 				 * when somebody wants to join a group
16988 				 * on a STANDBY interface and we
16989 				 * internally join on a different one.
16990 				 * If we had joined on from_ill then, a
16991 				 * failover now will find a new ilm
16992 				 * with this index.
16993 				 */
16994 				ip1dbg(("ilm_move_v6: FAILOVER, found"
16995 				    " new ilm on %s, group address %s\n",
16996 				    to_ill->ill_name,
16997 				    inet_ntop(AF_INET6,
16998 				    &ilm->ilm_v6addr, buf,
16999 				    sizeof (buf))));
17000 				new_ilm->ilm_refcnt += count;
17001 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17002 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17003 					new_ilm->ilm_is_new = B_TRUE;
17004 				}
17005 			} else {
17006 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17007 				if (new_ilm == NULL) {
17008 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17009 					    " multicast address %s : from %s to"
17010 					    " %s failed : ENOMEM \n",
17011 					    inet_ntop(AF_INET6,
17012 					    &ilm->ilm_v6addr, buf,
17013 					    sizeof (buf)), from_ill->ill_name,
17014 					    to_ill->ill_name));
17015 					ilmp = &ilm->ilm_next;
17016 					continue;
17017 				}
17018 				*new_ilm = *ilm;
17019 				new_ilm->ilm_filter = NULL;
17020 				new_ilm->ilm_refcnt = count;
17021 				new_ilm->ilm_timer = INFINITY;
17022 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17023 				new_ilm->ilm_is_new = B_TRUE;
17024 				/*
17025 				 * If the to_ill has not joined this
17026 				 * group we need to tell the driver in
17027 				 * ill_send_multicast_reqs.
17028 				 */
17029 				if (ilm_lookup_ill_v6(to_ill,
17030 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17031 					new_ilm->ilm_notify_driver = B_TRUE;
17032 
17033 				new_ilm->ilm_ill = to_ill;
17034 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17035 				    (char *), "ilm", (void *), new_ilm);
17036 				to_ill->ill_ilm_cnt++;
17037 
17038 				/* Add to the to_ill's list */
17039 				new_ilm->ilm_next = to_ill->ill_ilm;
17040 				to_ill->ill_ilm = new_ilm;
17041 				ASSERT(new_ilm->ilm_ipif == NULL);
17042 			}
17043 			if (ilm->ilm_refcnt == 0) {
17044 				goto bottom;
17045 			} else {
17046 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17047 				CLEAR_SLIST(new_ilm->ilm_filter);
17048 				ilmp = &ilm->ilm_next;
17049 			}
17050 			continue;
17051 		} else {
17052 			/*
17053 			 * ifindex = 0 means, move everything pointing at
17054 			 * from_ill. We are doing this becuase ill has
17055 			 * either FAILED or became INACTIVE.
17056 			 *
17057 			 * As we would like to move things later back to
17058 			 * from_ill, we want to retain the identity of this
17059 			 * ilm. Thus, we don't blindly increment the reference
17060 			 * count on the ilms matching the address alone. We
17061 			 * need to match on the ilm_orig_index also. new_ilm
17062 			 * was obtained by matching ilm_orig_index also.
17063 			 */
17064 			if (new_ilm != NULL) {
17065 				/*
17066 				 * This is possible only if a previous restore
17067 				 * was incomplete i.e restore to
17068 				 * ilm_orig_ifindex left some ilms because
17069 				 * of some failures. Thus when we are failing
17070 				 * again, we might find our old friends there.
17071 				 */
17072 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17073 				    " on %s, group address %s\n",
17074 				    to_ill->ill_name,
17075 				    inet_ntop(AF_INET6,
17076 				    &ilm->ilm_v6addr, buf,
17077 				    sizeof (buf))));
17078 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17079 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17080 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17081 					new_ilm->ilm_is_new = B_TRUE;
17082 				}
17083 			} else {
17084 				if (from_ill->ill_ilm_walker_cnt != 0) {
17085 					new_ilm = (ilm_t *)
17086 					    mi_zalloc(sizeof (ilm_t));
17087 					if (new_ilm == NULL) {
17088 						ip0dbg(("ilm_move_v6: "
17089 						    "FAILOVER of IPv6"
17090 						    " multicast address %s : "
17091 						    "from %s to"
17092 						    " %s failed : ENOMEM \n",
17093 						    inet_ntop(AF_INET6,
17094 						    &ilm->ilm_v6addr, buf,
17095 						    sizeof (buf)),
17096 						    from_ill->ill_name,
17097 						    to_ill->ill_name));
17098 
17099 							ilmp = &ilm->ilm_next;
17100 							continue;
17101 					}
17102 					*new_ilm = *ilm;
17103 					new_ilm->ilm_filter = NULL;
17104 				} else {
17105 					*ilmp = ilm->ilm_next;
17106 					DTRACE_PROBE3(ill__decr__cnt,
17107 					    (ill_t *), from_ill,
17108 					    (char *), "ilm", (void *), ilm);
17109 					ASSERT(from_ill->ill_ilm_cnt > 0);
17110 					from_ill->ill_ilm_cnt--;
17111 
17112 					new_ilm = ilm;
17113 				}
17114 				/*
17115 				 * If the to_ill has not joined this
17116 				 * group we need to tell the driver in
17117 				 * ill_send_multicast_reqs.
17118 				 */
17119 				if (ilm_lookup_ill_v6(to_ill,
17120 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17121 					new_ilm->ilm_notify_driver = B_TRUE;
17122 
17123 				/* Add to the to_ill's list */
17124 				new_ilm->ilm_next = to_ill->ill_ilm;
17125 				to_ill->ill_ilm = new_ilm;
17126 				ASSERT(ilm->ilm_ipif == NULL);
17127 				new_ilm->ilm_ill = to_ill;
17128 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17129 				    (char *), "ilm", (void *), new_ilm);
17130 				to_ill->ill_ilm_cnt++;
17131 				new_ilm->ilm_is_new = B_TRUE;
17132 			}
17133 
17134 		}
17135 
17136 bottom:
17137 		/*
17138 		 * Revert multicast filter state to (EXCLUDE, NULL).
17139 		 * new_ilm->ilm_is_new should already be set if needed.
17140 		 */
17141 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17142 		CLEAR_SLIST(new_ilm->ilm_filter);
17143 		/*
17144 		 * We allocated/got a new ilm, free the old one.
17145 		 */
17146 		if (new_ilm != ilm) {
17147 			if (from_ill->ill_ilm_walker_cnt == 0) {
17148 				*ilmp = ilm->ilm_next;
17149 
17150 				ASSERT(ilm->ilm_ipif == NULL); /* ipv6 */
17151 				DTRACE_PROBE3(ill__decr__cnt, (ill_t *),
17152 				    from_ill, (char *), "ilm", (void *), ilm);
17153 				ASSERT(from_ill->ill_ilm_cnt > 0);
17154 				from_ill->ill_ilm_cnt--;
17155 
17156 				ilm_inactive(ilm); /* frees this ilm */
17157 
17158 			} else {
17159 				ilm->ilm_flags |= ILM_DELETED;
17160 				from_ill->ill_ilm_cleanup_reqd = 1;
17161 				ilmp = &ilm->ilm_next;
17162 			}
17163 		}
17164 	}
17165 }
17166 
17167 /*
17168  * Move all the multicast memberships to to_ill. Called when
17169  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17170  * different from IPv6 counterpart as multicast memberships are associated
17171  * with ills in IPv6. This function is called after every ipif is moved
17172  * unlike IPv6, where it is moved only once.
17173  */
17174 static void
17175 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17176 {
17177 	ilm_t	*ilm;
17178 	ilm_t	*ilm_next;
17179 	ilm_t	*new_ilm;
17180 	ilm_t	**ilmp;
17181 	ip_stack_t	*ipst = from_ill->ill_ipst;
17182 
17183 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17184 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17185 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17186 
17187 	ilmp = &from_ill->ill_ilm;
17188 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17189 		ilm_next = ilm->ilm_next;
17190 
17191 		if (ilm->ilm_flags & ILM_DELETED) {
17192 			ilmp = &ilm->ilm_next;
17193 			continue;
17194 		}
17195 
17196 		ASSERT(ilm->ilm_ipif != NULL);
17197 
17198 		if (ilm->ilm_ipif != ipif) {
17199 			ilmp = &ilm->ilm_next;
17200 			continue;
17201 		}
17202 
17203 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17204 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17205 			new_ilm = ilm_lookup_ipif(ipif,
17206 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17207 			if (new_ilm != NULL) {
17208 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17209 				/*
17210 				 * We still need to deal with the from_ill.
17211 				 */
17212 				new_ilm->ilm_is_new = B_TRUE;
17213 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17214 				CLEAR_SLIST(new_ilm->ilm_filter);
17215 				ASSERT(ilm->ilm_ipif == ipif);
17216 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17217 				if (from_ill->ill_ilm_walker_cnt == 0) {
17218 					DTRACE_PROBE3(ill__decr__cnt,
17219 					    (ill_t *), from_ill,
17220 					    (char *), "ilm", (void *), ilm);
17221 					ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17222 				}
17223 				goto delete_ilm;
17224 			}
17225 			/*
17226 			 * If we could not find one e.g. ipif is
17227 			 * still down on to_ill, we add this ilm
17228 			 * on ill_new to preserve the reference
17229 			 * count.
17230 			 */
17231 		}
17232 		/*
17233 		 * When ipifs move, ilms always move with it
17234 		 * to the NEW ill. Thus we should never be
17235 		 * able to find ilm till we really move it here.
17236 		 */
17237 		ASSERT(ilm_lookup_ipif(ipif,
17238 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17239 
17240 		if (from_ill->ill_ilm_walker_cnt != 0) {
17241 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17242 			if (new_ilm == NULL) {
17243 				char buf[INET6_ADDRSTRLEN];
17244 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17245 				    " multicast address %s : "
17246 				    "from %s to"
17247 				    " %s failed : ENOMEM \n",
17248 				    inet_ntop(AF_INET,
17249 				    &ilm->ilm_v6addr, buf,
17250 				    sizeof (buf)),
17251 				    from_ill->ill_name,
17252 				    to_ill->ill_name));
17253 
17254 				ilmp = &ilm->ilm_next;
17255 				continue;
17256 			}
17257 			*new_ilm = *ilm;
17258 			DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), ipif,
17259 			    (char *), "ilm", (void *), ilm);
17260 			new_ilm->ilm_ipif->ipif_ilm_cnt++;
17261 			/* We don't want new_ilm linked to ilm's filter list */
17262 			new_ilm->ilm_filter = NULL;
17263 		} else {
17264 			/* Remove from the list */
17265 			*ilmp = ilm->ilm_next;
17266 			new_ilm = ilm;
17267 		}
17268 
17269 		/*
17270 		 * If we have never joined this group on the to_ill
17271 		 * make sure we tell the driver.
17272 		 */
17273 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17274 		    ALL_ZONES) == NULL)
17275 			new_ilm->ilm_notify_driver = B_TRUE;
17276 
17277 		/* Add to the to_ill's list */
17278 		new_ilm->ilm_next = to_ill->ill_ilm;
17279 		to_ill->ill_ilm = new_ilm;
17280 		new_ilm->ilm_is_new = B_TRUE;
17281 
17282 		/*
17283 		 * Revert multicast filter state to (EXCLUDE, NULL)
17284 		 */
17285 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17286 		CLEAR_SLIST(new_ilm->ilm_filter);
17287 
17288 		/*
17289 		 * Delete only if we have allocated a new ilm.
17290 		 */
17291 		if (new_ilm != ilm) {
17292 delete_ilm:
17293 			if (from_ill->ill_ilm_walker_cnt == 0) {
17294 				/* Remove from the list */
17295 				*ilmp = ilm->ilm_next;
17296 				ilm->ilm_next = NULL;
17297 				DTRACE_PROBE3(ipif__decr__cnt,
17298 				    (ipif_t *), ilm->ilm_ipif,
17299 				    (char *), "ilm", (void *), ilm);
17300 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17301 				ilm->ilm_ipif->ipif_ilm_cnt--;
17302 				ilm_inactive(ilm);
17303 			} else {
17304 				ilm->ilm_flags |= ILM_DELETED;
17305 				from_ill->ill_ilm_cleanup_reqd = 1;
17306 				ilmp = &ilm->ilm_next;
17307 			}
17308 		}
17309 	}
17310 }
17311 
17312 static uint_t
17313 ipif_get_id(ill_t *ill, uint_t id)
17314 {
17315 	uint_t	unit;
17316 	ipif_t	*tipif;
17317 	boolean_t found = B_FALSE;
17318 	ip_stack_t	*ipst = ill->ill_ipst;
17319 
17320 	/*
17321 	 * During failback, we want to go back to the same id
17322 	 * instead of the smallest id so that the original
17323 	 * configuration is maintained. id is non-zero in that
17324 	 * case.
17325 	 */
17326 	if (id != 0) {
17327 		/*
17328 		 * While failing back, if we still have an ipif with
17329 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17330 		 * as soon as we return from this function. It was
17331 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17332 		 * we can choose the smallest id. Thus we return zero
17333 		 * in that case ignoring the hint.
17334 		 */
17335 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17336 			return (0);
17337 		for (tipif = ill->ill_ipif; tipif != NULL;
17338 		    tipif = tipif->ipif_next) {
17339 			if (tipif->ipif_id == id) {
17340 				found = B_TRUE;
17341 				break;
17342 			}
17343 		}
17344 		/*
17345 		 * If somebody already plumbed another logical
17346 		 * with the same id, we won't be able to find it.
17347 		 */
17348 		if (!found)
17349 			return (id);
17350 	}
17351 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17352 		found = B_FALSE;
17353 		for (tipif = ill->ill_ipif; tipif != NULL;
17354 		    tipif = tipif->ipif_next) {
17355 			if (tipif->ipif_id == unit) {
17356 				found = B_TRUE;
17357 				break;
17358 			}
17359 		}
17360 		if (!found)
17361 			break;
17362 	}
17363 	return (unit);
17364 }
17365 
17366 /* ARGSUSED */
17367 static int
17368 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17369     ipif_t **rep_ipif_ptr)
17370 {
17371 	ill_t	*from_ill;
17372 	ipif_t	*rep_ipif;
17373 	uint_t	unit;
17374 	int err = 0;
17375 	ipif_t	*to_ipif;
17376 	struct iocblk	*iocp;
17377 	boolean_t failback_cmd;
17378 	boolean_t remove_ipif;
17379 	int	rc;
17380 	ip_stack_t	*ipst;
17381 
17382 	ASSERT(IAM_WRITER_ILL(to_ill));
17383 	ASSERT(IAM_WRITER_IPIF(ipif));
17384 
17385 	iocp = (struct iocblk *)mp->b_rptr;
17386 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17387 	remove_ipif = B_FALSE;
17388 
17389 	from_ill = ipif->ipif_ill;
17390 	ipst = from_ill->ill_ipst;
17391 
17392 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17393 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17394 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17395 
17396 	/*
17397 	 * Don't move LINK LOCAL addresses as they are tied to
17398 	 * physical interface.
17399 	 */
17400 	if (from_ill->ill_isv6 &&
17401 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17402 		ipif->ipif_was_up = B_FALSE;
17403 		IPIF_UNMARK_MOVING(ipif);
17404 		return (0);
17405 	}
17406 
17407 	/*
17408 	 * We set the ipif_id to maximum so that the search for
17409 	 * ipif_id will pick the lowest number i.e 0 in the
17410 	 * following 2 cases :
17411 	 *
17412 	 * 1) We have a replacement ipif at the head of to_ill.
17413 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17414 	 *    on to_ill and hence the MOVE might fail. We want to
17415 	 *    remove it only if we could move the ipif. Thus, by
17416 	 *    setting it to the MAX value, we make the search in
17417 	 *    ipif_get_id return the zeroth id.
17418 	 *
17419 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17420 	 *    we might just have a zero address plumbed on the ipif
17421 	 *    with zero id in the case of IPv4. We remove that while
17422 	 *    doing the failback. We want to remove it only if we
17423 	 *    could move the ipif. Thus, by setting it to the MAX
17424 	 *    value, we make the search in ipif_get_id return the
17425 	 *    zeroth id.
17426 	 *
17427 	 * Both (1) and (2) are done only when when we are moving
17428 	 * an ipif (either due to failover/failback) which originally
17429 	 * belonged to this interface i.e the ipif_orig_ifindex is
17430 	 * the same as to_ill's ifindex. This is needed so that
17431 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17432 	 * from B -> A (B is being removed from the group) and
17433 	 * FAILBACK from A -> B restores the original configuration.
17434 	 * Without the check for orig_ifindex, the second FAILOVER
17435 	 * could make the ipif belonging to B replace the A's zeroth
17436 	 * ipif and the subsequent failback re-creating the replacement
17437 	 * ipif again.
17438 	 *
17439 	 * NOTE : We created the replacement ipif when we did a
17440 	 * FAILOVER (See below). We could check for FAILBACK and
17441 	 * then look for replacement ipif to be removed. But we don't
17442 	 * want to do that because we wan't to allow the possibility
17443 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17444 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17445 	 * from B -> A.
17446 	 */
17447 	to_ipif = to_ill->ill_ipif;
17448 	if ((to_ill->ill_phyint->phyint_ifindex ==
17449 	    ipif->ipif_orig_ifindex) &&
17450 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17451 		ASSERT(to_ipif->ipif_id == 0);
17452 		remove_ipif = B_TRUE;
17453 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17454 	}
17455 	/*
17456 	 * Find the lowest logical unit number on the to_ill.
17457 	 * If we are failing back, try to get the original id
17458 	 * rather than the lowest one so that the original
17459 	 * configuration is maintained.
17460 	 *
17461 	 * XXX need a better scheme for this.
17462 	 */
17463 	if (failback_cmd) {
17464 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17465 	} else {
17466 		unit = ipif_get_id(to_ill, 0);
17467 	}
17468 
17469 	/* Reset back to zero in case we fail below */
17470 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17471 		to_ipif->ipif_id = 0;
17472 
17473 	if (unit == ipst->ips_ip_addrs_per_if) {
17474 		ipif->ipif_was_up = B_FALSE;
17475 		IPIF_UNMARK_MOVING(ipif);
17476 		return (EINVAL);
17477 	}
17478 
17479 	/*
17480 	 * ipif is ready to move from "from_ill" to "to_ill".
17481 	 *
17482 	 * 1) If we are moving ipif with id zero, create a
17483 	 *    replacement ipif for this ipif on from_ill. If this fails
17484 	 *    fail the MOVE operation.
17485 	 *
17486 	 * 2) Remove the replacement ipif on to_ill if any.
17487 	 *    We could remove the replacement ipif when we are moving
17488 	 *    the ipif with id zero. But what if somebody already
17489 	 *    unplumbed it ? Thus we always remove it if it is present.
17490 	 *    We want to do it only if we are sure we are going to
17491 	 *    move the ipif to to_ill which is why there are no
17492 	 *    returns due to error till ipif is linked to to_ill.
17493 	 *    Note that the first ipif that we failback will always
17494 	 *    be zero if it is present.
17495 	 */
17496 	if (ipif->ipif_id == 0) {
17497 		ipaddr_t inaddr_any = INADDR_ANY;
17498 
17499 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17500 		if (rep_ipif == NULL) {
17501 			ipif->ipif_was_up = B_FALSE;
17502 			IPIF_UNMARK_MOVING(ipif);
17503 			return (ENOMEM);
17504 		}
17505 		*rep_ipif = ipif_zero;
17506 		/*
17507 		 * Before we put the ipif on the list, store the addresses
17508 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17509 		 * assumes so. This logic is not any different from what
17510 		 * ipif_allocate does.
17511 		 */
17512 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17513 		    &rep_ipif->ipif_v6lcl_addr);
17514 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17515 		    &rep_ipif->ipif_v6src_addr);
17516 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17517 		    &rep_ipif->ipif_v6subnet);
17518 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17519 		    &rep_ipif->ipif_v6net_mask);
17520 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17521 		    &rep_ipif->ipif_v6brd_addr);
17522 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17523 		    &rep_ipif->ipif_v6pp_dst_addr);
17524 		/*
17525 		 * We mark IPIF_NOFAILOVER so that this can never
17526 		 * move.
17527 		 */
17528 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17529 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17530 		rep_ipif->ipif_replace_zero = B_TRUE;
17531 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17532 		    MUTEX_DEFAULT, NULL);
17533 		rep_ipif->ipif_id = 0;
17534 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17535 		rep_ipif->ipif_ill = from_ill;
17536 		rep_ipif->ipif_orig_ifindex =
17537 		    from_ill->ill_phyint->phyint_ifindex;
17538 		/* Insert at head */
17539 		rep_ipif->ipif_next = from_ill->ill_ipif;
17540 		from_ill->ill_ipif = rep_ipif;
17541 		/*
17542 		 * We don't really care to let apps know about
17543 		 * this interface.
17544 		 */
17545 	}
17546 
17547 	if (remove_ipif) {
17548 		/*
17549 		 * We set to a max value above for this case to get
17550 		 * id zero. ASSERT that we did get one.
17551 		 */
17552 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17553 		rep_ipif = to_ipif;
17554 		to_ill->ill_ipif = rep_ipif->ipif_next;
17555 		rep_ipif->ipif_next = NULL;
17556 		/*
17557 		 * If some apps scanned and find this interface,
17558 		 * it is time to let them know, so that they can
17559 		 * delete it.
17560 		 */
17561 
17562 		*rep_ipif_ptr = rep_ipif;
17563 	}
17564 
17565 	/* Get it out of the ILL interface list. */
17566 	ipif_remove(ipif, B_FALSE);
17567 
17568 	/* Assign the new ill */
17569 	ipif->ipif_ill = to_ill;
17570 	ipif->ipif_id = unit;
17571 	/* id has already been checked */
17572 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17573 	ASSERT(rc == 0);
17574 	/* Let SCTP update its list */
17575 	sctp_move_ipif(ipif, from_ill, to_ill);
17576 	/*
17577 	 * Handle the failover and failback of ipif_t between
17578 	 * ill_t that have differing maximum mtu values.
17579 	 */
17580 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17581 		if (ipif->ipif_saved_mtu == 0) {
17582 			/*
17583 			 * As this ipif_t is moving to an ill_t
17584 			 * that has a lower ill_max_mtu, its
17585 			 * ipif_mtu needs to be saved so it can
17586 			 * be restored during failback or during
17587 			 * failover to an ill_t which has a
17588 			 * higher ill_max_mtu.
17589 			 */
17590 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17591 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17592 		} else {
17593 			/*
17594 			 * The ipif_t is, once again, moving to
17595 			 * an ill_t that has a lower maximum mtu
17596 			 * value.
17597 			 */
17598 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17599 		}
17600 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17601 	    ipif->ipif_saved_mtu != 0) {
17602 		/*
17603 		 * The mtu of this ipif_t had to be reduced
17604 		 * during an earlier failover; this is an
17605 		 * opportunity for it to be increased (either as
17606 		 * part of another failover or a failback).
17607 		 */
17608 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17609 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17610 			ipif->ipif_saved_mtu = 0;
17611 		} else {
17612 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17613 		}
17614 	}
17615 
17616 	/*
17617 	 * We preserve all the other fields of the ipif including
17618 	 * ipif_saved_ire_mp. The routes that are saved here will
17619 	 * be recreated on the new interface and back on the old
17620 	 * interface when we move back.
17621 	 */
17622 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17623 
17624 	return (err);
17625 }
17626 
17627 static int
17628 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17629     int ifindex, ipif_t **rep_ipif_ptr)
17630 {
17631 	ipif_t *mipif;
17632 	ipif_t *ipif_next;
17633 	int err;
17634 
17635 	/*
17636 	 * We don't really try to MOVE back things if some of the
17637 	 * operations fail. The daemon will take care of moving again
17638 	 * later on.
17639 	 */
17640 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17641 		ipif_next = mipif->ipif_next;
17642 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17643 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17644 
17645 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17646 
17647 			/*
17648 			 * When the MOVE fails, it is the job of the
17649 			 * application to take care of this properly
17650 			 * i.e try again if it is ENOMEM.
17651 			 */
17652 			if (mipif->ipif_ill != from_ill) {
17653 				/*
17654 				 * ipif has moved.
17655 				 *
17656 				 * Move the multicast memberships associated
17657 				 * with this ipif to the new ill. For IPv6, we
17658 				 * do it once after all the ipifs are moved
17659 				 * (in ill_move) as they are not associated
17660 				 * with ipifs.
17661 				 *
17662 				 * We need to move the ilms as the ipif has
17663 				 * already been moved to a new ill even
17664 				 * in the case of errors. Neither
17665 				 * ilm_free(ipif) will find the ilm
17666 				 * when somebody unplumbs this ipif nor
17667 				 * ilm_delete(ilm) will be able to find the
17668 				 * ilm, if we don't move now.
17669 				 */
17670 				if (!from_ill->ill_isv6)
17671 					ilm_move_v4(from_ill, to_ill, mipif);
17672 			}
17673 
17674 			if (err != 0)
17675 				return (err);
17676 		}
17677 	}
17678 	return (0);
17679 }
17680 
17681 static int
17682 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17683 {
17684 	int ifindex;
17685 	int err;
17686 	struct iocblk	*iocp;
17687 	ipif_t	*ipif;
17688 	ipif_t *rep_ipif_ptr = NULL;
17689 	ipif_t	*from_ipif = NULL;
17690 	boolean_t check_rep_if = B_FALSE;
17691 	ip_stack_t	*ipst = from_ill->ill_ipst;
17692 
17693 	iocp = (struct iocblk *)mp->b_rptr;
17694 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17695 		/*
17696 		 * Move everything pointing at from_ill to to_ill.
17697 		 * We acheive this by passing in 0 as ifindex.
17698 		 */
17699 		ifindex = 0;
17700 	} else {
17701 		/*
17702 		 * Move everything pointing at from_ill whose original
17703 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17704 		 * We acheive this by passing in ifindex rather than 0.
17705 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17706 		 */
17707 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17708 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17709 	}
17710 
17711 	/*
17712 	 * Determine if there is at least one ipif that would move from
17713 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17714 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17715 	 * the move, in which case we need to quiesce the replacement ipif also.
17716 	 */
17717 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17718 	    from_ipif = from_ipif->ipif_next) {
17719 		if (((ifindex == 0) ||
17720 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17721 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17722 			check_rep_if = B_TRUE;
17723 			break;
17724 		}
17725 	}
17726 
17727 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17728 
17729 	GRAB_ILL_LOCKS(from_ill, to_ill);
17730 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17731 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17732 		    mp, ILL_MOVE_OK);
17733 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17734 		return (EINPROGRESS);
17735 	}
17736 
17737 	/* Check if the replacement ipif is quiescent to delete */
17738 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17739 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17740 		to_ill->ill_ipif->ipif_state_flags |=
17741 		    IPIF_MOVING | IPIF_CHANGING;
17742 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17743 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17744 			    mp, ILL_MOVE_OK);
17745 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17746 			return (EINPROGRESS);
17747 		}
17748 	}
17749 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17750 
17751 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17752 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17753 	GRAB_ILL_LOCKS(from_ill, to_ill);
17754 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17755 
17756 	/* ilm_move is done inside ipif_move for IPv4 */
17757 	if (err == 0 && from_ill->ill_isv6)
17758 		ilm_move_v6(from_ill, to_ill, ifindex);
17759 
17760 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17761 	rw_exit(&ipst->ips_ill_g_lock);
17762 
17763 	/*
17764 	 * send rts messages and multicast messages.
17765 	 */
17766 	if (rep_ipif_ptr != NULL) {
17767 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17768 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17769 			rep_ipif_ptr->ipif_recovery_id = 0;
17770 		}
17771 		ip_rts_ifmsg(rep_ipif_ptr);
17772 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17773 #ifdef DEBUG
17774 		ipif_trace_cleanup(rep_ipif_ptr);
17775 #endif
17776 		mi_free(rep_ipif_ptr);
17777 	}
17778 
17779 	conn_move_ill(from_ill, to_ill, ifindex);
17780 
17781 	return (err);
17782 }
17783 
17784 /*
17785  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17786  * Also checks for the validity of the arguments.
17787  * Note: We are already exclusive inside the from group.
17788  * It is upto the caller to release refcnt on the to_ill's.
17789  */
17790 static int
17791 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17792     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17793 {
17794 	int dst_index;
17795 	ipif_t *ipif_v4, *ipif_v6;
17796 	struct lifreq *lifr;
17797 	mblk_t *mp1;
17798 	boolean_t exists;
17799 	sin_t	*sin;
17800 	int	err = 0;
17801 	ip_stack_t	*ipst;
17802 
17803 	if (CONN_Q(q))
17804 		ipst = CONNQ_TO_IPST(q);
17805 	else
17806 		ipst = ILLQ_TO_IPST(q);
17807 
17808 	if ((mp1 = mp->b_cont) == NULL)
17809 		return (EPROTO);
17810 
17811 	if ((mp1 = mp1->b_cont) == NULL)
17812 		return (EPROTO);
17813 
17814 	lifr = (struct lifreq *)mp1->b_rptr;
17815 	sin = (sin_t *)&lifr->lifr_addr;
17816 
17817 	/*
17818 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17819 	 * specific operations.
17820 	 */
17821 	if (sin->sin_family != AF_UNSPEC)
17822 		return (EINVAL);
17823 
17824 	/*
17825 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17826 	 * NULLs for the last 4 args and we know the lookup won't fail
17827 	 * with EINPROGRESS.
17828 	 */
17829 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17830 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17831 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17832 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17833 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17834 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17835 
17836 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17837 		return (ENXIO);
17838 
17839 	if (ipif_v4 != NULL) {
17840 		ASSERT(ipif_v4->ipif_refcnt != 0);
17841 		if (ipif_v4->ipif_id != 0) {
17842 			err = EINVAL;
17843 			goto done;
17844 		}
17845 
17846 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17847 		*ill_from_v4 = ipif_v4->ipif_ill;
17848 	}
17849 
17850 	if (ipif_v6 != NULL) {
17851 		ASSERT(ipif_v6->ipif_refcnt != 0);
17852 		if (ipif_v6->ipif_id != 0) {
17853 			err = EINVAL;
17854 			goto done;
17855 		}
17856 
17857 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17858 		*ill_from_v6 = ipif_v6->ipif_ill;
17859 	}
17860 
17861 	err = 0;
17862 	dst_index = lifr->lifr_movetoindex;
17863 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17864 	    q, mp, ip_process_ioctl, &err, ipst);
17865 	if (err != 0) {
17866 		/*
17867 		 * There could be only v6.
17868 		 */
17869 		if (err != ENXIO)
17870 			goto done;
17871 		err = 0;
17872 	}
17873 
17874 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17875 	    q, mp, ip_process_ioctl, &err, ipst);
17876 	if (err != 0) {
17877 		if (err != ENXIO)
17878 			goto done;
17879 		if (*ill_to_v4 == NULL) {
17880 			err = ENXIO;
17881 			goto done;
17882 		}
17883 		err = 0;
17884 	}
17885 
17886 	/*
17887 	 * If we have something to MOVE i.e "from" not NULL,
17888 	 * "to" should be non-NULL.
17889 	 */
17890 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17891 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17892 		err = EINVAL;
17893 	}
17894 
17895 done:
17896 	if (ipif_v4 != NULL)
17897 		ipif_refrele(ipif_v4);
17898 	if (ipif_v6 != NULL)
17899 		ipif_refrele(ipif_v6);
17900 	return (err);
17901 }
17902 
17903 /*
17904  * FAILOVER and FAILBACK are modelled as MOVE operations.
17905  *
17906  * We don't check whether the MOVE is within the same group or
17907  * not, because this ioctl can be used as a generic mechanism
17908  * to failover from interface A to B, though things will function
17909  * only if they are really part of the same group. Moreover,
17910  * all ipifs may be down and hence temporarily out of the group.
17911  *
17912  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17913  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17914  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17915  * have been deleted and there are no active references. Once quiescent the
17916  * ipif's are moved and brought up on the new ill.
17917  *
17918  * Normally the source ill and destination ill belong to the same IPMP group
17919  * and hence the same ipsq_t. In the event they don't belong to the same
17920  * same group the two ipsq's are first merged into one ipsq - that of the
17921  * to_ill. The multicast memberships on the source and destination ill cannot
17922  * change during the move operation since multicast joins/leaves also have to
17923  * execute on the same ipsq and are hence serialized.
17924  */
17925 /* ARGSUSED */
17926 int
17927 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17928     ip_ioctl_cmd_t *ipip, void *ifreq)
17929 {
17930 	ill_t *ill_to_v4 = NULL;
17931 	ill_t *ill_to_v6 = NULL;
17932 	ill_t *ill_from_v4 = NULL;
17933 	ill_t *ill_from_v6 = NULL;
17934 	int err = 0;
17935 
17936 	/*
17937 	 * setup from and to ill's, we can get EINPROGRESS only for
17938 	 * to_ill's.
17939 	 */
17940 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17941 	    &ill_to_v4, &ill_to_v6);
17942 
17943 	if (err != 0) {
17944 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17945 		goto done;
17946 	}
17947 
17948 	/*
17949 	 * nothing to do.
17950 	 */
17951 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17952 		goto done;
17953 	}
17954 
17955 	/*
17956 	 * nothing to do.
17957 	 */
17958 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17959 		goto done;
17960 	}
17961 
17962 	/*
17963 	 * Mark the ill as changing.
17964 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17965 	 * in ill_up_ipifs in case of error they are cleared below.
17966 	 */
17967 
17968 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17969 	if (ill_from_v4 != NULL)
17970 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17971 	if (ill_from_v6 != NULL)
17972 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17973 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17974 
17975 	/*
17976 	 * Make sure that both src and dst are
17977 	 * in the same syncq group. If not make it happen.
17978 	 * We are not holding any locks because we are the writer
17979 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17980 	 * to protect to_ipsq against changing.
17981 	 */
17982 	if (ill_from_v4 != NULL) {
17983 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17984 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17985 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17986 			    NULL, mp, q);
17987 			goto err_ret;
17988 
17989 		}
17990 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
17991 	} else {
17992 
17993 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
17994 		    ill_to_v6->ill_phyint->phyint_ipsq) {
17995 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
17996 			    NULL, mp, q);
17997 			goto err_ret;
17998 
17999 		}
18000 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18001 	}
18002 
18003 	/*
18004 	 * Now that the ipsq's have been merged and we are the writer
18005 	 * lets mark to_ill as changing as well.
18006 	 */
18007 
18008 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18009 	if (ill_to_v4 != NULL)
18010 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18011 	if (ill_to_v6 != NULL)
18012 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18013 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18014 
18015 	/*
18016 	 * Its ok for us to proceed with the move even if
18017 	 * ill_pending_mp is non null on one of the from ill's as the reply
18018 	 * should not be looking at the ipif, it should only care about the
18019 	 * ill itself.
18020 	 */
18021 
18022 	/*
18023 	 * lets move ipv4 first.
18024 	 */
18025 	if (ill_from_v4 != NULL) {
18026 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18027 		ill_from_v4->ill_move_in_progress = B_TRUE;
18028 		ill_to_v4->ill_move_in_progress = B_TRUE;
18029 		ill_to_v4->ill_move_peer = ill_from_v4;
18030 		ill_from_v4->ill_move_peer = ill_to_v4;
18031 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18032 	}
18033 
18034 	/*
18035 	 * Now lets move ipv6.
18036 	 */
18037 	if (err == 0 && ill_from_v6 != NULL) {
18038 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18039 		ill_from_v6->ill_move_in_progress = B_TRUE;
18040 		ill_to_v6->ill_move_in_progress = B_TRUE;
18041 		ill_to_v6->ill_move_peer = ill_from_v6;
18042 		ill_from_v6->ill_move_peer = ill_to_v6;
18043 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18044 	}
18045 
18046 err_ret:
18047 	/*
18048 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18049 	 * moved to become quiescent.
18050 	 */
18051 	if (err == EINPROGRESS) {
18052 		goto done;
18053 	}
18054 
18055 	/*
18056 	 * if err is set ill_up_ipifs will not be called
18057 	 * lets clear the flags.
18058 	 */
18059 
18060 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18061 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18062 	/*
18063 	 * Some of the clearing may be redundant. But it is simple
18064 	 * not making any extra checks.
18065 	 */
18066 	if (ill_from_v6 != NULL) {
18067 		ill_from_v6->ill_move_in_progress = B_FALSE;
18068 		ill_from_v6->ill_move_peer = NULL;
18069 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18070 	}
18071 	if (ill_from_v4 != NULL) {
18072 		ill_from_v4->ill_move_in_progress = B_FALSE;
18073 		ill_from_v4->ill_move_peer = NULL;
18074 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18075 	}
18076 	if (ill_to_v6 != NULL) {
18077 		ill_to_v6->ill_move_in_progress = B_FALSE;
18078 		ill_to_v6->ill_move_peer = NULL;
18079 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18080 	}
18081 	if (ill_to_v4 != NULL) {
18082 		ill_to_v4->ill_move_in_progress = B_FALSE;
18083 		ill_to_v4->ill_move_peer = NULL;
18084 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18085 	}
18086 
18087 	/*
18088 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18089 	 * Do this always to maintain proper state i.e even in case of errors.
18090 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18091 	 * we need not call on both v4 and v6 interfaces.
18092 	 */
18093 	if (ill_from_v4 != NULL) {
18094 		if ((ill_from_v4->ill_phyint->phyint_flags &
18095 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18096 			phyint_inactive(ill_from_v4->ill_phyint);
18097 		}
18098 	} else if (ill_from_v6 != NULL) {
18099 		if ((ill_from_v6->ill_phyint->phyint_flags &
18100 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18101 			phyint_inactive(ill_from_v6->ill_phyint);
18102 		}
18103 	}
18104 
18105 	if (ill_to_v4 != NULL) {
18106 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18107 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18108 		}
18109 	} else if (ill_to_v6 != NULL) {
18110 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18111 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18112 		}
18113 	}
18114 
18115 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18116 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18117 
18118 no_err:
18119 	/*
18120 	 * lets bring the interfaces up on the to_ill.
18121 	 */
18122 	if (err == 0) {
18123 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18124 		    q, mp);
18125 	}
18126 
18127 	if (err == 0) {
18128 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18129 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18130 
18131 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18132 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18133 	}
18134 done:
18135 
18136 	if (ill_to_v4 != NULL) {
18137 		ill_refrele(ill_to_v4);
18138 	}
18139 	if (ill_to_v6 != NULL) {
18140 		ill_refrele(ill_to_v6);
18141 	}
18142 
18143 	return (err);
18144 }
18145 
18146 static void
18147 ill_dl_down(ill_t *ill)
18148 {
18149 	/*
18150 	 * The ill is down; unbind but stay attached since we're still
18151 	 * associated with a PPA. If we have negotiated DLPI capabilites
18152 	 * with the data link service provider (IDS_OK) then reset them.
18153 	 * The interval between unbinding and rebinding is potentially
18154 	 * unbounded hence we cannot assume things will be the same.
18155 	 * The DLPI capabilities will be probed again when the data link
18156 	 * is brought up.
18157 	 */
18158 	mblk_t	*mp = ill->ill_unbind_mp;
18159 
18160 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18161 
18162 	ill->ill_unbind_mp = NULL;
18163 	if (mp != NULL) {
18164 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18165 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18166 		    ill->ill_name));
18167 		mutex_enter(&ill->ill_lock);
18168 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18169 		mutex_exit(&ill->ill_lock);
18170 		/*
18171 		 * Reset the capabilities if the negotiation is done or is
18172 		 * still in progress. Note that ill_capability_reset() will
18173 		 * set ill_dlpi_capab_state to IDS_UNKNOWN, so the subsequent
18174 		 * DL_CAPABILITY_ACK and DL_NOTE_CAPAB_RENEG will be ignored.
18175 		 *
18176 		 * Further, reset ill_capab_reneg to be B_FALSE so that the
18177 		 * subsequent DL_CAPABILITY_ACK can be ignored, to prevent
18178 		 * the capabilities renegotiation from happening.
18179 		 */
18180 		if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
18181 			ill_capability_reset(ill);
18182 		ill->ill_capab_reneg = B_FALSE;
18183 
18184 		ill_dlpi_send(ill, mp);
18185 	}
18186 
18187 	/*
18188 	 * Toss all of our multicast memberships.  We could keep them, but
18189 	 * then we'd have to do bookkeeping of any joins and leaves performed
18190 	 * by the application while the the interface is down (we can't just
18191 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18192 	 * on a downed interface).
18193 	 */
18194 	ill_leave_multicast(ill);
18195 
18196 	mutex_enter(&ill->ill_lock);
18197 	ill->ill_dl_up = 0;
18198 	(void) ill_hook_event_create(ill, 0, NE_DOWN, NULL, 0);
18199 	mutex_exit(&ill->ill_lock);
18200 }
18201 
18202 static void
18203 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18204 {
18205 	union DL_primitives *dlp;
18206 	t_uscalar_t prim;
18207 
18208 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18209 
18210 	dlp = (union DL_primitives *)mp->b_rptr;
18211 	prim = dlp->dl_primitive;
18212 
18213 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18214 	    dl_primstr(prim), prim, ill->ill_name));
18215 
18216 	switch (prim) {
18217 	case DL_PHYS_ADDR_REQ:
18218 	{
18219 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18220 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18221 		break;
18222 	}
18223 	case DL_BIND_REQ:
18224 		mutex_enter(&ill->ill_lock);
18225 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18226 		mutex_exit(&ill->ill_lock);
18227 		break;
18228 	}
18229 
18230 	/*
18231 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18232 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18233 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18234 	 */
18235 	mutex_enter(&ill->ill_lock);
18236 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18237 	    (prim == DL_UNBIND_REQ)) {
18238 		ill->ill_dlpi_pending = prim;
18239 	}
18240 	mutex_exit(&ill->ill_lock);
18241 
18242 	putnext(ill->ill_wq, mp);
18243 }
18244 
18245 /*
18246  * Helper function for ill_dlpi_send().
18247  */
18248 /* ARGSUSED */
18249 static void
18250 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18251 {
18252 	ill_dlpi_send(q->q_ptr, mp);
18253 }
18254 
18255 /*
18256  * Send a DLPI control message to the driver but make sure there
18257  * is only one outstanding message. Uses ill_dlpi_pending to tell
18258  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18259  * when an ACK or a NAK is received to process the next queued message.
18260  */
18261 void
18262 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18263 {
18264 	mblk_t **mpp;
18265 
18266 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18267 
18268 	/*
18269 	 * To ensure that any DLPI requests for current exclusive operation
18270 	 * are always completely sent before any DLPI messages for other
18271 	 * operations, require writer access before enqueuing.
18272 	 */
18273 	if (!IAM_WRITER_ILL(ill)) {
18274 		ill_refhold(ill);
18275 		/* qwriter_ip() does the ill_refrele() */
18276 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18277 		    NEW_OP, B_TRUE);
18278 		return;
18279 	}
18280 
18281 	mutex_enter(&ill->ill_lock);
18282 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18283 		/* Must queue message. Tail insertion */
18284 		mpp = &ill->ill_dlpi_deferred;
18285 		while (*mpp != NULL)
18286 			mpp = &((*mpp)->b_next);
18287 
18288 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18289 		    ill->ill_name));
18290 
18291 		*mpp = mp;
18292 		mutex_exit(&ill->ill_lock);
18293 		return;
18294 	}
18295 	mutex_exit(&ill->ill_lock);
18296 	ill_dlpi_dispatch(ill, mp);
18297 }
18298 
18299 /*
18300  * Send all deferred DLPI messages without waiting for their ACKs.
18301  */
18302 void
18303 ill_dlpi_send_deferred(ill_t *ill)
18304 {
18305 	mblk_t *mp, *nextmp;
18306 
18307 	/*
18308 	 * Clear ill_dlpi_pending so that the message is not queued in
18309 	 * ill_dlpi_send().
18310 	 */
18311 	mutex_enter(&ill->ill_lock);
18312 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18313 	mp = ill->ill_dlpi_deferred;
18314 	ill->ill_dlpi_deferred = NULL;
18315 	mutex_exit(&ill->ill_lock);
18316 
18317 	for (; mp != NULL; mp = nextmp) {
18318 		nextmp = mp->b_next;
18319 		mp->b_next = NULL;
18320 		ill_dlpi_send(ill, mp);
18321 	}
18322 }
18323 
18324 /*
18325  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18326  */
18327 boolean_t
18328 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18329 {
18330 	t_uscalar_t pending;
18331 
18332 	mutex_enter(&ill->ill_lock);
18333 	if (ill->ill_dlpi_pending == prim) {
18334 		mutex_exit(&ill->ill_lock);
18335 		return (B_TRUE);
18336 	}
18337 
18338 	/*
18339 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
18340 	 * without waiting, so don't print any warnings in that case.
18341 	 */
18342 	if (ill->ill_state_flags & ILL_CONDEMNED) {
18343 		mutex_exit(&ill->ill_lock);
18344 		return (B_FALSE);
18345 	}
18346 	pending = ill->ill_dlpi_pending;
18347 	mutex_exit(&ill->ill_lock);
18348 
18349 	if (pending == DL_PRIM_INVAL) {
18350 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18351 		    "received unsolicited ack for %s on %s\n",
18352 		    dl_primstr(prim), ill->ill_name);
18353 	} else {
18354 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18355 		    "received unexpected ack for %s on %s (expecting %s)\n",
18356 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
18357 	}
18358 	return (B_FALSE);
18359 }
18360 
18361 /*
18362  * Complete the current DLPI operation associated with `prim' on `ill' and
18363  * start the next queued DLPI operation (if any).  If there are no queued DLPI
18364  * operations and the ill's current exclusive IPSQ operation has finished
18365  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
18366  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
18367  * the comments above ipsq_current_finish() for details.
18368  */
18369 void
18370 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18371 {
18372 	mblk_t *mp;
18373 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
18374 
18375 	ASSERT(IAM_WRITER_IPSQ(ipsq));
18376 	mutex_enter(&ill->ill_lock);
18377 
18378 	ASSERT(prim != DL_PRIM_INVAL);
18379 	ASSERT(ill->ill_dlpi_pending == prim);
18380 
18381 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18382 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18383 
18384 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18385 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18386 
18387 		mutex_enter(&ipsq->ipsq_lock);
18388 		if (ipsq->ipsq_current_done)
18389 			ipsq->ipsq_current_ipif = NULL;
18390 		mutex_exit(&ipsq->ipsq_lock);
18391 
18392 		cv_signal(&ill->ill_cv);
18393 		mutex_exit(&ill->ill_lock);
18394 		return;
18395 	}
18396 
18397 	ill->ill_dlpi_deferred = mp->b_next;
18398 	mp->b_next = NULL;
18399 	mutex_exit(&ill->ill_lock);
18400 
18401 	ill_dlpi_dispatch(ill, mp);
18402 }
18403 
18404 void
18405 conn_delete_ire(conn_t *connp, caddr_t arg)
18406 {
18407 	ipif_t	*ipif = (ipif_t *)arg;
18408 	ire_t	*ire;
18409 
18410 	/*
18411 	 * Look at the cached ires on conns which has pointers to ipifs.
18412 	 * We just call ire_refrele which clears up the reference
18413 	 * to ire. Called when a conn closes. Also called from ipif_free
18414 	 * to cleanup indirect references to the stale ipif via the cached ire.
18415 	 */
18416 	mutex_enter(&connp->conn_lock);
18417 	ire = connp->conn_ire_cache;
18418 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18419 		connp->conn_ire_cache = NULL;
18420 		mutex_exit(&connp->conn_lock);
18421 		IRE_REFRELE_NOTR(ire);
18422 		return;
18423 	}
18424 	mutex_exit(&connp->conn_lock);
18425 
18426 }
18427 
18428 /*
18429  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18430  * of IREs. Those IREs may have been previously cached in the conn structure.
18431  * This ipcl_walk() walker function releases all references to such IREs based
18432  * on the condemned flag.
18433  */
18434 /* ARGSUSED */
18435 void
18436 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18437 {
18438 	ire_t	*ire;
18439 
18440 	mutex_enter(&connp->conn_lock);
18441 	ire = connp->conn_ire_cache;
18442 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18443 		connp->conn_ire_cache = NULL;
18444 		mutex_exit(&connp->conn_lock);
18445 		IRE_REFRELE_NOTR(ire);
18446 		return;
18447 	}
18448 	mutex_exit(&connp->conn_lock);
18449 }
18450 
18451 /*
18452  * Take down a specific interface, but don't lose any information about it.
18453  * Also delete interface from its interface group (ifgrp).
18454  * (Always called as writer.)
18455  * This function goes through the down sequence even if the interface is
18456  * already down. There are 2 reasons.
18457  * a. Currently we permit interface routes that depend on down interfaces
18458  *    to be added. This behaviour itself is questionable. However it appears
18459  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18460  *    time. We go thru the cleanup in order to remove these routes.
18461  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18462  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18463  *    down, but we need to cleanup i.e. do ill_dl_down and
18464  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18465  *
18466  * IP-MT notes:
18467  *
18468  * Model of reference to interfaces.
18469  *
18470  * The following members in ipif_t track references to the ipif.
18471  *	int     ipif_refcnt;    Active reference count
18472  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18473  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
18474  *
18475  * The following members in ill_t track references to the ill.
18476  *	int             ill_refcnt;     active refcnt
18477  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18478  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18479  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
18480  *
18481  * Reference to an ipif or ill can be obtained in any of the following ways.
18482  *
18483  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18484  * Pointers to ipif / ill from other data structures viz ire and conn.
18485  * Implicit reference to the ipif / ill by holding a reference to the ire.
18486  *
18487  * The ipif/ill lookup functions return a reference held ipif / ill.
18488  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18489  * This is a purely dynamic reference count associated with threads holding
18490  * references to the ipif / ill. Pointers from other structures do not
18491  * count towards this reference count.
18492  *
18493  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
18494  * associated with the ipif/ill. This is incremented whenever a new
18495  * ire is created referencing the ipif/ill. This is done atomically inside
18496  * ire_add_v[46] where the ire is actually added to the ire hash table.
18497  * The count is decremented in ire_inactive where the ire is destroyed.
18498  *
18499  * nce's reference ill's thru nce_ill and the count of nce's associated with
18500  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18501  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18502  * table. Similarly it is decremented in ndp_inactive() where the nce
18503  * is destroyed.
18504  *
18505  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
18506  * is incremented in ilm_add_v6() and decremented before the ilm is freed
18507  * in ilm_walker_cleanup() or ilm_delete().
18508  *
18509  * Flow of ioctls involving interface down/up
18510  *
18511  * The following is the sequence of an attempt to set some critical flags on an
18512  * up interface.
18513  * ip_sioctl_flags
18514  * ipif_down
18515  * wait for ipif to be quiescent
18516  * ipif_down_tail
18517  * ip_sioctl_flags_tail
18518  *
18519  * All set ioctls that involve down/up sequence would have a skeleton similar
18520  * to the above. All the *tail functions are called after the refcounts have
18521  * dropped to the appropriate values.
18522  *
18523  * The mechanism to quiesce an ipif is as follows.
18524  *
18525  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18526  * on the ipif. Callers either pass a flag requesting wait or the lookup
18527  *  functions will return NULL.
18528  *
18529  * Delete all ires referencing this ipif
18530  *
18531  * Any thread attempting to do an ipif_refhold on an ipif that has been
18532  * obtained thru a cached pointer will first make sure that
18533  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18534  * increment the refcount.
18535  *
18536  * The above guarantees that the ipif refcount will eventually come down to
18537  * zero and the ipif will quiesce, once all threads that currently hold a
18538  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18539  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18540  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
18541  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
18542  * in ip.h
18543  *
18544  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18545  *
18546  * Threads trying to lookup an ipif or ill can pass a flag requesting
18547  * wait and restart if the ipif / ill cannot be looked up currently.
18548  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18549  * failure if the ipif is currently undergoing an exclusive operation, and
18550  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18551  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18552  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18553  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18554  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18555  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18556  * until we release the ipsq_lock, even though the the ill/ipif state flags
18557  * can change after we drop the ill_lock.
18558  *
18559  * An attempt to send out a packet using an ipif that is currently
18560  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18561  * operation and restart it later when the exclusive condition on the ipif ends.
18562  * This is an example of not passing the wait flag to the lookup functions. For
18563  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18564  * out a multicast packet on that ipif will fail while the ipif is
18565  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18566  * currently IPIF_CHANGING will also fail.
18567  */
18568 int
18569 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18570 {
18571 	ill_t		*ill = ipif->ipif_ill;
18572 	phyint_t	*phyi;
18573 	conn_t		*connp;
18574 	boolean_t	success;
18575 	boolean_t	ipif_was_up = B_FALSE;
18576 	ip_stack_t	*ipst = ill->ill_ipst;
18577 
18578 	ASSERT(IAM_WRITER_IPIF(ipif));
18579 
18580 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18581 
18582 	if (ipif->ipif_flags & IPIF_UP) {
18583 		mutex_enter(&ill->ill_lock);
18584 		ipif->ipif_flags &= ~IPIF_UP;
18585 		ASSERT(ill->ill_ipif_up_count > 0);
18586 		--ill->ill_ipif_up_count;
18587 		mutex_exit(&ill->ill_lock);
18588 		ipif_was_up = B_TRUE;
18589 		/* Update status in SCTP's list */
18590 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18591 	}
18592 
18593 	/*
18594 	 * Blow away memberships we established in ipif_multicast_up().
18595 	 */
18596 	ipif_multicast_down(ipif);
18597 
18598 	/*
18599 	 * Remove from the mapping for __sin6_src_id. We insert only
18600 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18601 	 * stored as mapped addresses, we need to check for mapped
18602 	 * INADDR_ANY also.
18603 	 */
18604 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18605 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18606 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18607 		int err;
18608 
18609 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18610 		    ipif->ipif_zoneid, ipst);
18611 		if (err != 0) {
18612 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18613 		}
18614 	}
18615 
18616 	/*
18617 	 * Before we delete the ill from the group (if any), we need
18618 	 * to make sure that we delete all the routes dependent on
18619 	 * this and also any ipifs dependent on this ipif for
18620 	 * source address. We need to do before we delete from
18621 	 * the group because
18622 	 *
18623 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18624 	 *
18625 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18626 	 *    for re-doing source address selection. Note that
18627 	 *    ipif_select_source[_v6] called from
18628 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18629 	 *    because we have already marked down here i.e cleared
18630 	 *    IPIF_UP.
18631 	 */
18632 	if (ipif->ipif_isv6) {
18633 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18634 		    ipst);
18635 	} else {
18636 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18637 		    ipst);
18638 	}
18639 
18640 	/*
18641 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18642 	 * ires have been deleted above. Otherwise a thread could end up
18643 	 * caching an ire in a conn after we have finished the cleanup of the
18644 	 * conn. The caching is done after making sure that the ire is not yet
18645 	 * condemned. Also documented in the block comment above ip_output
18646 	 */
18647 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18648 	/* Also, delete the ires cached in SCTP */
18649 	sctp_ire_cache_flush(ipif);
18650 
18651 	/*
18652 	 * Update any other ipifs which have used "our" local address as
18653 	 * a source address. This entails removing and recreating IRE_INTERFACE
18654 	 * entries for such ipifs.
18655 	 */
18656 	if (ipif->ipif_isv6)
18657 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18658 	else
18659 		ipif_update_other_ipifs(ipif, ill->ill_group);
18660 
18661 	if (ipif_was_up) {
18662 		/*
18663 		 * Check whether it is last ipif to leave this group.
18664 		 * If this is the last ipif to leave, we should remove
18665 		 * this ill from the group as ipif_select_source will not
18666 		 * be able to find any useful ipifs if this ill is selected
18667 		 * for load balancing.
18668 		 *
18669 		 * For nameless groups, we should call ifgrp_delete if this
18670 		 * belongs to some group. As this ipif is going down, we may
18671 		 * need to reconstruct groups.
18672 		 */
18673 		phyi = ill->ill_phyint;
18674 		/*
18675 		 * If the phyint_groupname_len is 0, it may or may not
18676 		 * be in the nameless group. If the phyint_groupname_len is
18677 		 * not 0, then this ill should be part of some group.
18678 		 * As we always insert this ill in the group if
18679 		 * phyint_groupname_len is not zero when the first ipif
18680 		 * comes up (in ipif_up_done), it should be in a group
18681 		 * when the namelen is not 0.
18682 		 *
18683 		 * NOTE : When we delete the ill from the group,it will
18684 		 * blow away all the IRE_CACHES pointing either at this ipif or
18685 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18686 		 * should be pointing at this ill.
18687 		 */
18688 		ASSERT(phyi->phyint_groupname_len == 0 ||
18689 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18690 
18691 		if (phyi->phyint_groupname_len != 0) {
18692 			if (ill->ill_ipif_up_count == 0)
18693 				illgrp_delete(ill);
18694 		}
18695 
18696 		/*
18697 		 * If we have deleted some of the broadcast ires associated
18698 		 * with this ipif, we need to re-nominate somebody else if
18699 		 * the ires that we deleted were the nominated ones.
18700 		 */
18701 		if (ill->ill_group != NULL && !ill->ill_isv6)
18702 			ipif_renominate_bcast(ipif);
18703 	}
18704 
18705 	/*
18706 	 * neighbor-discovery or arp entries for this interface.
18707 	 */
18708 	ipif_ndp_down(ipif);
18709 
18710 	/*
18711 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18712 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18713 	 * and ill_delete -> ipif_free -> ipif_down
18714 	 */
18715 	if (mp == NULL) {
18716 		ASSERT(q == NULL);
18717 		return (0);
18718 	}
18719 
18720 	if (CONN_Q(q)) {
18721 		connp = Q_TO_CONN(q);
18722 		mutex_enter(&connp->conn_lock);
18723 	} else {
18724 		connp = NULL;
18725 	}
18726 	mutex_enter(&ill->ill_lock);
18727 	/*
18728 	 * Are there any ire's pointing to this ipif that are still active ?
18729 	 * If this is the last ipif going down, are there any ire's pointing
18730 	 * to this ill that are still active ?
18731 	 */
18732 	if (ipif_is_quiescent(ipif)) {
18733 		mutex_exit(&ill->ill_lock);
18734 		if (connp != NULL)
18735 			mutex_exit(&connp->conn_lock);
18736 		return (0);
18737 	}
18738 
18739 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18740 	    ill->ill_name, (void *)ill));
18741 	/*
18742 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18743 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18744 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18745 	 */
18746 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18747 	if (!success) {
18748 		/* The conn is closing. So just return */
18749 		ASSERT(connp != NULL);
18750 		mutex_exit(&ill->ill_lock);
18751 		mutex_exit(&connp->conn_lock);
18752 		return (EINTR);
18753 	}
18754 
18755 	mutex_exit(&ill->ill_lock);
18756 	if (connp != NULL)
18757 		mutex_exit(&connp->conn_lock);
18758 	return (EINPROGRESS);
18759 }
18760 
18761 void
18762 ipif_down_tail(ipif_t *ipif)
18763 {
18764 	ill_t	*ill = ipif->ipif_ill;
18765 
18766 	/*
18767 	 * Skip any loopback interface (null wq).
18768 	 * If this is the last logical interface on the ill
18769 	 * have ill_dl_down tell the driver we are gone (unbind)
18770 	 * Note that lun 0 can ipif_down even though
18771 	 * there are other logical units that are up.
18772 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18773 	 */
18774 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18775 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18776 	    ill->ill_dl_up) {
18777 		ill_dl_down(ill);
18778 	}
18779 	ill->ill_logical_down = 0;
18780 
18781 	/*
18782 	 * Have to be after removing the routes in ipif_down_delete_ire.
18783 	 */
18784 	if (ipif->ipif_isv6) {
18785 		if (ill->ill_flags & ILLF_XRESOLV)
18786 			ipif_arp_down(ipif);
18787 	} else {
18788 		ipif_arp_down(ipif);
18789 	}
18790 
18791 	ip_rts_ifmsg(ipif);
18792 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18793 }
18794 
18795 /*
18796  * Bring interface logically down without bringing the physical interface
18797  * down e.g. when the netmask is changed. This avoids long lasting link
18798  * negotiations between an ethernet interface and a certain switches.
18799  */
18800 static int
18801 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18802 {
18803 	/*
18804 	 * The ill_logical_down flag is a transient flag. It is set here
18805 	 * and is cleared once the down has completed in ipif_down_tail.
18806 	 * This flag does not indicate whether the ill stream is in the
18807 	 * DL_BOUND state with the driver. Instead this flag is used by
18808 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18809 	 * the driver. The state of the ill stream i.e. whether it is
18810 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18811 	 */
18812 	ipif->ipif_ill->ill_logical_down = 1;
18813 	return (ipif_down(ipif, q, mp));
18814 }
18815 
18816 /*
18817  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18818  * If the usesrc client ILL is already part of a usesrc group or not,
18819  * in either case a ire_stq with the matching usesrc client ILL will
18820  * locate the IRE's that need to be deleted. We want IREs to be created
18821  * with the new source address.
18822  */
18823 static void
18824 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18825 {
18826 	ill_t	*ucill = (ill_t *)ill_arg;
18827 
18828 	ASSERT(IAM_WRITER_ILL(ucill));
18829 
18830 	if (ire->ire_stq == NULL)
18831 		return;
18832 
18833 	if ((ire->ire_type == IRE_CACHE) &&
18834 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18835 		ire_delete(ire);
18836 }
18837 
18838 /*
18839  * ire_walk routine to delete every IRE dependent on the interface
18840  * address that is going down.	(Always called as writer.)
18841  * Works for both v4 and v6.
18842  * In addition for checking for ire_ipif matches it also checks for
18843  * IRE_CACHE entries which have the same source address as the
18844  * disappearing ipif since ipif_select_source might have picked
18845  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18846  * care of any IRE_INTERFACE with the disappearing source address.
18847  */
18848 static void
18849 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18850 {
18851 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18852 	ill_t *ire_ill;
18853 	ill_t *ipif_ill;
18854 
18855 	ASSERT(IAM_WRITER_IPIF(ipif));
18856 	if (ire->ire_ipif == NULL)
18857 		return;
18858 
18859 	/*
18860 	 * For IPv4, we derive source addresses for an IRE from ipif's
18861 	 * belonging to the same IPMP group as the IRE's outgoing
18862 	 * interface.  If an IRE's outgoing interface isn't in the
18863 	 * same IPMP group as a particular ipif, then that ipif
18864 	 * couldn't have been used as a source address for this IRE.
18865 	 *
18866 	 * For IPv6, source addresses are only restricted to the IPMP group
18867 	 * if the IRE is for a link-local address or a multicast address.
18868 	 * Otherwise, source addresses for an IRE can be chosen from
18869 	 * interfaces other than the the outgoing interface for that IRE.
18870 	 *
18871 	 * For source address selection details, see ipif_select_source()
18872 	 * and ipif_select_source_v6().
18873 	 */
18874 	if (ire->ire_ipversion == IPV4_VERSION ||
18875 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18876 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18877 		ire_ill = ire->ire_ipif->ipif_ill;
18878 		ipif_ill = ipif->ipif_ill;
18879 
18880 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18881 			return;
18882 		}
18883 	}
18884 
18885 	if (ire->ire_ipif != ipif) {
18886 		/*
18887 		 * Look for a matching source address.
18888 		 */
18889 		if (ire->ire_type != IRE_CACHE)
18890 			return;
18891 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18892 			return;
18893 
18894 		if (ire->ire_ipversion == IPV4_VERSION) {
18895 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18896 				return;
18897 		} else {
18898 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18899 			    &ipif->ipif_v6lcl_addr))
18900 				return;
18901 		}
18902 		ire_delete(ire);
18903 		return;
18904 	}
18905 	/*
18906 	 * ire_delete() will do an ire_flush_cache which will delete
18907 	 * all ire_ipif matches
18908 	 */
18909 	ire_delete(ire);
18910 }
18911 
18912 /*
18913  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18914  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18915  * 2) when an interface is brought up or down (on that ill).
18916  * This ensures that the IRE_CACHE entries don't retain stale source
18917  * address selection results.
18918  */
18919 void
18920 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18921 {
18922 	ill_t	*ill = (ill_t *)ill_arg;
18923 	ill_t	*ipif_ill;
18924 
18925 	ASSERT(IAM_WRITER_ILL(ill));
18926 	/*
18927 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18928 	 * Hence this should be IRE_CACHE.
18929 	 */
18930 	ASSERT(ire->ire_type == IRE_CACHE);
18931 
18932 	/*
18933 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18934 	 * We are only interested in IRE_CACHES that has borrowed
18935 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18936 	 * for which we need to look at ire_ipif->ipif_ill match
18937 	 * with ill.
18938 	 */
18939 	ASSERT(ire->ire_ipif != NULL);
18940 	ipif_ill = ire->ire_ipif->ipif_ill;
18941 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18942 	    ipif_ill->ill_group == ill->ill_group)) {
18943 		ire_delete(ire);
18944 	}
18945 }
18946 
18947 /*
18948  * Delete all the ire whose stq references ill_arg.
18949  */
18950 static void
18951 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18952 {
18953 	ill_t	*ill = (ill_t *)ill_arg;
18954 	ill_t	*ire_ill;
18955 
18956 	ASSERT(IAM_WRITER_ILL(ill));
18957 	/*
18958 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18959 	 * Hence this should be IRE_CACHE.
18960 	 */
18961 	ASSERT(ire->ire_type == IRE_CACHE);
18962 
18963 	/*
18964 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18965 	 * matches ill. We are only interested in IRE_CACHES that
18966 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
18967 	 * filtering here.
18968 	 */
18969 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
18970 
18971 	if (ire_ill == ill)
18972 		ire_delete(ire);
18973 }
18974 
18975 /*
18976  * This is called when an ill leaves the group. We want to delete
18977  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
18978  * pointing at ill.
18979  */
18980 static void
18981 illgrp_cache_delete(ire_t *ire, char *ill_arg)
18982 {
18983 	ill_t	*ill = (ill_t *)ill_arg;
18984 
18985 	ASSERT(IAM_WRITER_ILL(ill));
18986 	ASSERT(ill->ill_group == NULL);
18987 	/*
18988 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18989 	 * Hence this should be IRE_CACHE.
18990 	 */
18991 	ASSERT(ire->ire_type == IRE_CACHE);
18992 	/*
18993 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18994 	 * matches ill. We are interested in both.
18995 	 */
18996 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
18997 	    (ire->ire_ipif->ipif_ill == ill));
18998 
18999 	ire_delete(ire);
19000 }
19001 
19002 /*
19003  * Initiate deallocate of an IPIF. Always called as writer. Called by
19004  * ill_delete or ip_sioctl_removeif.
19005  */
19006 static void
19007 ipif_free(ipif_t *ipif)
19008 {
19009 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19010 
19011 	ASSERT(IAM_WRITER_IPIF(ipif));
19012 
19013 	if (ipif->ipif_recovery_id != 0)
19014 		(void) untimeout(ipif->ipif_recovery_id);
19015 	ipif->ipif_recovery_id = 0;
19016 
19017 	/* Remove conn references */
19018 	reset_conn_ipif(ipif);
19019 
19020 	/*
19021 	 * Make sure we have valid net and subnet broadcast ire's for the
19022 	 * other ipif's which share them with this ipif.
19023 	 */
19024 	if (!ipif->ipif_isv6)
19025 		ipif_check_bcast_ires(ipif);
19026 
19027 	/*
19028 	 * Take down the interface. We can be called either from ill_delete
19029 	 * or from ip_sioctl_removeif.
19030 	 */
19031 	(void) ipif_down(ipif, NULL, NULL);
19032 
19033 	/*
19034 	 * Now that the interface is down, there's no chance it can still
19035 	 * become a duplicate.  Cancel any timer that may have been set while
19036 	 * tearing down.
19037 	 */
19038 	if (ipif->ipif_recovery_id != 0)
19039 		(void) untimeout(ipif->ipif_recovery_id);
19040 	ipif->ipif_recovery_id = 0;
19041 
19042 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19043 	/* Remove pointers to this ill in the multicast routing tables */
19044 	reset_mrt_vif_ipif(ipif);
19045 	rw_exit(&ipst->ips_ill_g_lock);
19046 }
19047 
19048 /*
19049  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19050  * also ill_move().
19051  */
19052 static void
19053 ipif_free_tail(ipif_t *ipif)
19054 {
19055 	mblk_t	*mp;
19056 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19057 
19058 	/*
19059 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19060 	 */
19061 	mutex_enter(&ipif->ipif_saved_ire_lock);
19062 	mp = ipif->ipif_saved_ire_mp;
19063 	ipif->ipif_saved_ire_mp = NULL;
19064 	mutex_exit(&ipif->ipif_saved_ire_lock);
19065 	freemsg(mp);
19066 
19067 	/*
19068 	 * Need to hold both ill_g_lock and ill_lock while
19069 	 * inserting or removing an ipif from the linked list
19070 	 * of ipifs hanging off the ill.
19071 	 */
19072 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19073 
19074 	ASSERT(ilm_walk_ipif(ipif) == 0);
19075 
19076 #ifdef DEBUG
19077 	ipif_trace_cleanup(ipif);
19078 #endif
19079 
19080 	/* Ask SCTP to take it out of it list */
19081 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19082 
19083 	/* Get it out of the ILL interface list. */
19084 	ipif_remove(ipif, B_TRUE);
19085 	rw_exit(&ipst->ips_ill_g_lock);
19086 
19087 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19088 
19089 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19090 	ASSERT(ipif->ipif_recovery_id == 0);
19091 
19092 	/* Free the memory. */
19093 	mi_free(ipif);
19094 }
19095 
19096 /*
19097  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19098  * is zero.
19099  */
19100 void
19101 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19102 {
19103 	char	lbuf[LIFNAMSIZ];
19104 	char	*name;
19105 	size_t	name_len;
19106 
19107 	buf[0] = '\0';
19108 	name = ipif->ipif_ill->ill_name;
19109 	name_len = ipif->ipif_ill->ill_name_length;
19110 	if (ipif->ipif_id != 0) {
19111 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19112 		    ipif->ipif_id);
19113 		name = lbuf;
19114 		name_len = mi_strlen(name) + 1;
19115 	}
19116 	len -= 1;
19117 	buf[len] = '\0';
19118 	len = MIN(len, name_len);
19119 	bcopy(name, buf, len);
19120 }
19121 
19122 /*
19123  * Find an IPIF based on the name passed in.  Names can be of the
19124  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19125  * The <phys> string can have forms like <dev><#> (e.g., le0),
19126  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19127  * When there is no colon, the implied unit id is zero. <phys> must
19128  * correspond to the name of an ILL.  (May be called as writer.)
19129  */
19130 static ipif_t *
19131 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19132     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19133     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19134 {
19135 	char	*cp;
19136 	char	*endp;
19137 	long	id;
19138 	ill_t	*ill;
19139 	ipif_t	*ipif;
19140 	uint_t	ire_type;
19141 	boolean_t did_alloc = B_FALSE;
19142 	ipsq_t	*ipsq;
19143 
19144 	if (error != NULL)
19145 		*error = 0;
19146 
19147 	/*
19148 	 * If the caller wants to us to create the ipif, make sure we have a
19149 	 * valid zoneid
19150 	 */
19151 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19152 
19153 	if (namelen == 0) {
19154 		if (error != NULL)
19155 			*error = ENXIO;
19156 		return (NULL);
19157 	}
19158 
19159 	*exists = B_FALSE;
19160 	/* Look for a colon in the name. */
19161 	endp = &name[namelen];
19162 	for (cp = endp; --cp > name; ) {
19163 		if (*cp == IPIF_SEPARATOR_CHAR)
19164 			break;
19165 	}
19166 
19167 	if (*cp == IPIF_SEPARATOR_CHAR) {
19168 		/*
19169 		 * Reject any non-decimal aliases for logical
19170 		 * interfaces. Aliases with leading zeroes
19171 		 * are also rejected as they introduce ambiguity
19172 		 * in the naming of the interfaces.
19173 		 * In order to confirm with existing semantics,
19174 		 * and to not break any programs/script relying
19175 		 * on that behaviour, if<0>:0 is considered to be
19176 		 * a valid interface.
19177 		 *
19178 		 * If alias has two or more digits and the first
19179 		 * is zero, fail.
19180 		 */
19181 		if (&cp[2] < endp && cp[1] == '0') {
19182 			if (error != NULL)
19183 				*error = EINVAL;
19184 			return (NULL);
19185 		}
19186 	}
19187 
19188 	if (cp <= name) {
19189 		cp = endp;
19190 	} else {
19191 		*cp = '\0';
19192 	}
19193 
19194 	/*
19195 	 * Look up the ILL, based on the portion of the name
19196 	 * before the slash. ill_lookup_on_name returns a held ill.
19197 	 * Temporary to check whether ill exists already. If so
19198 	 * ill_lookup_on_name will clear it.
19199 	 */
19200 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19201 	    q, mp, func, error, &did_alloc, ipst);
19202 	if (cp != endp)
19203 		*cp = IPIF_SEPARATOR_CHAR;
19204 	if (ill == NULL)
19205 		return (NULL);
19206 
19207 	/* Establish the unit number in the name. */
19208 	id = 0;
19209 	if (cp < endp && *endp == '\0') {
19210 		/* If there was a colon, the unit number follows. */
19211 		cp++;
19212 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19213 			ill_refrele(ill);
19214 			if (error != NULL)
19215 				*error = ENXIO;
19216 			return (NULL);
19217 		}
19218 	}
19219 
19220 	GRAB_CONN_LOCK(q);
19221 	mutex_enter(&ill->ill_lock);
19222 	/* Now see if there is an IPIF with this unit number. */
19223 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19224 		if (ipif->ipif_id == id) {
19225 			if (zoneid != ALL_ZONES &&
19226 			    zoneid != ipif->ipif_zoneid &&
19227 			    ipif->ipif_zoneid != ALL_ZONES) {
19228 				mutex_exit(&ill->ill_lock);
19229 				RELEASE_CONN_LOCK(q);
19230 				ill_refrele(ill);
19231 				if (error != NULL)
19232 					*error = ENXIO;
19233 				return (NULL);
19234 			}
19235 			/*
19236 			 * The block comment at the start of ipif_down
19237 			 * explains the use of the macros used below
19238 			 */
19239 			if (IPIF_CAN_LOOKUP(ipif)) {
19240 				ipif_refhold_locked(ipif);
19241 				mutex_exit(&ill->ill_lock);
19242 				if (!did_alloc)
19243 					*exists = B_TRUE;
19244 				/*
19245 				 * Drop locks before calling ill_refrele
19246 				 * since it can potentially call into
19247 				 * ipif_ill_refrele_tail which can end up
19248 				 * in trying to acquire any lock.
19249 				 */
19250 				RELEASE_CONN_LOCK(q);
19251 				ill_refrele(ill);
19252 				return (ipif);
19253 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19254 				ipsq = ill->ill_phyint->phyint_ipsq;
19255 				mutex_enter(&ipsq->ipsq_lock);
19256 				mutex_exit(&ill->ill_lock);
19257 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19258 				mutex_exit(&ipsq->ipsq_lock);
19259 				RELEASE_CONN_LOCK(q);
19260 				ill_refrele(ill);
19261 				if (error != NULL)
19262 					*error = EINPROGRESS;
19263 				return (NULL);
19264 			}
19265 		}
19266 	}
19267 	RELEASE_CONN_LOCK(q);
19268 
19269 	if (!do_alloc) {
19270 		mutex_exit(&ill->ill_lock);
19271 		ill_refrele(ill);
19272 		if (error != NULL)
19273 			*error = ENXIO;
19274 		return (NULL);
19275 	}
19276 
19277 	/*
19278 	 * If none found, atomically allocate and return a new one.
19279 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19280 	 * to support "receive only" use of lo0:1 etc. as is still done
19281 	 * below as an initial guess.
19282 	 * However, this is now likely to be overriden later in ipif_up_done()
19283 	 * when we know for sure what address has been configured on the
19284 	 * interface, since we might have more than one loopback interface
19285 	 * with a loopback address, e.g. in the case of zones, and all the
19286 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19287 	 */
19288 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19289 		ire_type = IRE_LOOPBACK;
19290 	else
19291 		ire_type = IRE_LOCAL;
19292 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19293 	if (ipif != NULL)
19294 		ipif_refhold_locked(ipif);
19295 	else if (error != NULL)
19296 		*error = ENOMEM;
19297 	mutex_exit(&ill->ill_lock);
19298 	ill_refrele(ill);
19299 	return (ipif);
19300 }
19301 
19302 /*
19303  * This routine is called whenever a new address comes up on an ipif.  If
19304  * we are configured to respond to address mask requests, then we are supposed
19305  * to broadcast an address mask reply at this time.  This routine is also
19306  * called if we are already up, but a netmask change is made.  This is legal
19307  * but might not make the system manager very popular.	(May be called
19308  * as writer.)
19309  */
19310 void
19311 ipif_mask_reply(ipif_t *ipif)
19312 {
19313 	icmph_t	*icmph;
19314 	ipha_t	*ipha;
19315 	mblk_t	*mp;
19316 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19317 
19318 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19319 
19320 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19321 		return;
19322 
19323 	/* ICMP mask reply is IPv4 only */
19324 	ASSERT(!ipif->ipif_isv6);
19325 	/* ICMP mask reply is not for a loopback interface */
19326 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19327 
19328 	mp = allocb(REPLY_LEN, BPRI_HI);
19329 	if (mp == NULL)
19330 		return;
19331 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19332 
19333 	ipha = (ipha_t *)mp->b_rptr;
19334 	bzero(ipha, REPLY_LEN);
19335 	*ipha = icmp_ipha;
19336 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19337 	ipha->ipha_src = ipif->ipif_src_addr;
19338 	ipha->ipha_dst = ipif->ipif_brd_addr;
19339 	ipha->ipha_length = htons(REPLY_LEN);
19340 	ipha->ipha_ident = 0;
19341 
19342 	icmph = (icmph_t *)&ipha[1];
19343 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19344 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19345 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19346 
19347 	put(ipif->ipif_wq, mp);
19348 
19349 #undef	REPLY_LEN
19350 }
19351 
19352 /*
19353  * When the mtu in the ipif changes, we call this routine through ire_walk
19354  * to update all the relevant IREs.
19355  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19356  */
19357 static void
19358 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19359 {
19360 	ipif_t *ipif = (ipif_t *)ipif_arg;
19361 
19362 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19363 		return;
19364 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19365 }
19366 
19367 /*
19368  * When the mtu in the ill changes, we call this routine through ire_walk
19369  * to update all the relevant IREs.
19370  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19371  */
19372 void
19373 ill_mtu_change(ire_t *ire, char *ill_arg)
19374 {
19375 	ill_t	*ill = (ill_t *)ill_arg;
19376 
19377 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19378 		return;
19379 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19380 }
19381 
19382 /*
19383  * Join the ipif specific multicast groups.
19384  * Must be called after a mapping has been set up in the resolver.  (Always
19385  * called as writer.)
19386  */
19387 void
19388 ipif_multicast_up(ipif_t *ipif)
19389 {
19390 	int err, index;
19391 	ill_t *ill;
19392 
19393 	ASSERT(IAM_WRITER_IPIF(ipif));
19394 
19395 	ill = ipif->ipif_ill;
19396 	index = ill->ill_phyint->phyint_ifindex;
19397 
19398 	ip1dbg(("ipif_multicast_up\n"));
19399 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19400 		return;
19401 
19402 	if (ipif->ipif_isv6) {
19403 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19404 			return;
19405 
19406 		/* Join the all hosts multicast address */
19407 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19408 		/*
19409 		 * Passing B_TRUE means we have to join the multicast
19410 		 * membership on this interface even though this is
19411 		 * FAILED. If we join on a different one in the group,
19412 		 * we will not be able to delete the membership later
19413 		 * as we currently don't track where we join when we
19414 		 * join within the kernel unlike applications where
19415 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19416 		 * for more on this.
19417 		 */
19418 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19419 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19420 		if (err != 0) {
19421 			ip0dbg(("ipif_multicast_up: "
19422 			    "all_hosts_mcast failed %d\n",
19423 			    err));
19424 			return;
19425 		}
19426 		/*
19427 		 * Enable multicast for the solicited node multicast address
19428 		 */
19429 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19430 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19431 
19432 			ipv6_multi.s6_addr32[3] |=
19433 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19434 
19435 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19436 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19437 			    NULL);
19438 			if (err != 0) {
19439 				ip0dbg(("ipif_multicast_up: solicited MC"
19440 				    " failed %d\n", err));
19441 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19442 				    ill, ill->ill_phyint->phyint_ifindex,
19443 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19444 				return;
19445 			}
19446 		}
19447 	} else {
19448 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19449 			return;
19450 
19451 		/* Join the all hosts multicast address */
19452 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19453 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19454 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19455 		if (err) {
19456 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19457 			return;
19458 		}
19459 	}
19460 	ipif->ipif_multicast_up = 1;
19461 }
19462 
19463 /*
19464  * Blow away any multicast groups that we joined in ipif_multicast_up().
19465  * (Explicit memberships are blown away in ill_leave_multicast() when the
19466  * ill is brought down.)
19467  */
19468 static void
19469 ipif_multicast_down(ipif_t *ipif)
19470 {
19471 	int err;
19472 
19473 	ASSERT(IAM_WRITER_IPIF(ipif));
19474 
19475 	ip1dbg(("ipif_multicast_down\n"));
19476 	if (!ipif->ipif_multicast_up)
19477 		return;
19478 
19479 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19480 
19481 	if (!ipif->ipif_isv6) {
19482 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19483 		    B_TRUE);
19484 		if (err != 0)
19485 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19486 
19487 		ipif->ipif_multicast_up = 0;
19488 		return;
19489 	}
19490 
19491 	/*
19492 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19493 	 * we should look for ilms on this ill rather than the ones that have
19494 	 * been failed over here.  They are here temporarily. As
19495 	 * ipif_multicast_up has joined on this ill, we should delete only
19496 	 * from this ill.
19497 	 */
19498 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19499 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19500 	    B_TRUE, B_TRUE);
19501 	if (err != 0) {
19502 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19503 		    err));
19504 	}
19505 	/*
19506 	 * Disable multicast for the solicited node multicast address
19507 	 */
19508 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19509 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19510 
19511 		ipv6_multi.s6_addr32[3] |=
19512 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19513 
19514 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19515 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19516 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19517 
19518 		if (err != 0) {
19519 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19520 			    err));
19521 		}
19522 	}
19523 
19524 	ipif->ipif_multicast_up = 0;
19525 }
19526 
19527 /*
19528  * Used when an interface comes up to recreate any extra routes on this
19529  * interface.
19530  */
19531 static ire_t **
19532 ipif_recover_ire(ipif_t *ipif)
19533 {
19534 	mblk_t	*mp;
19535 	ire_t	**ipif_saved_irep;
19536 	ire_t	**irep;
19537 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19538 
19539 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19540 	    ipif->ipif_id));
19541 
19542 	mutex_enter(&ipif->ipif_saved_ire_lock);
19543 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19544 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19545 	if (ipif_saved_irep == NULL) {
19546 		mutex_exit(&ipif->ipif_saved_ire_lock);
19547 		return (NULL);
19548 	}
19549 
19550 	irep = ipif_saved_irep;
19551 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19552 		ire_t		*ire;
19553 		queue_t		*rfq;
19554 		queue_t		*stq;
19555 		ifrt_t		*ifrt;
19556 		uchar_t		*src_addr;
19557 		uchar_t		*gateway_addr;
19558 		ushort_t	type;
19559 
19560 		/*
19561 		 * When the ire was initially created and then added in
19562 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19563 		 * in the case of a traditional interface route, or as one of
19564 		 * the IRE_OFFSUBNET types (with the exception of
19565 		 * IRE_HOST types ire which is created by icmp_redirect() and
19566 		 * which we don't need to save or recover).  In the case where
19567 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19568 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19569 		 * to satisfy software like GateD and Sun Cluster which creates
19570 		 * routes using the the loopback interface's address as a
19571 		 * gateway.
19572 		 *
19573 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19574 		 * ire_create() will be called in the same way here as
19575 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19576 		 * the route looks like a traditional interface route (where
19577 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19578 		 * the saved ifrt->ifrt_type.  This means that in the case where
19579 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19580 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19581 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19582 		 */
19583 		ifrt = (ifrt_t *)mp->b_rptr;
19584 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19585 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19586 			rfq = NULL;
19587 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19588 			    ? ipif->ipif_rq : ipif->ipif_wq;
19589 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19590 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19591 			    : (uint8_t *)&ipif->ipif_src_addr;
19592 			gateway_addr = NULL;
19593 			type = ipif->ipif_net_type;
19594 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19595 			/* Recover multiroute broadcast IRE. */
19596 			rfq = ipif->ipif_rq;
19597 			stq = ipif->ipif_wq;
19598 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19599 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19600 			    : (uint8_t *)&ipif->ipif_src_addr;
19601 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19602 			type = ifrt->ifrt_type;
19603 		} else {
19604 			rfq = NULL;
19605 			stq = NULL;
19606 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19607 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19608 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19609 			type = ifrt->ifrt_type;
19610 		}
19611 
19612 		/*
19613 		 * Create a copy of the IRE with the saved address and netmask.
19614 		 */
19615 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19616 		    "0x%x/0x%x\n",
19617 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19618 		    ntohl(ifrt->ifrt_addr),
19619 		    ntohl(ifrt->ifrt_mask)));
19620 		ire = ire_create(
19621 		    (uint8_t *)&ifrt->ifrt_addr,
19622 		    (uint8_t *)&ifrt->ifrt_mask,
19623 		    src_addr,
19624 		    gateway_addr,
19625 		    &ifrt->ifrt_max_frag,
19626 		    NULL,
19627 		    rfq,
19628 		    stq,
19629 		    type,
19630 		    ipif,
19631 		    0,
19632 		    0,
19633 		    0,
19634 		    ifrt->ifrt_flags,
19635 		    &ifrt->ifrt_iulp_info,
19636 		    NULL,
19637 		    NULL,
19638 		    ipst);
19639 
19640 		if (ire == NULL) {
19641 			mutex_exit(&ipif->ipif_saved_ire_lock);
19642 			kmem_free(ipif_saved_irep,
19643 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19644 			return (NULL);
19645 		}
19646 
19647 		/*
19648 		 * Some software (for example, GateD and Sun Cluster) attempts
19649 		 * to create (what amount to) IRE_PREFIX routes with the
19650 		 * loopback address as the gateway.  This is primarily done to
19651 		 * set up prefixes with the RTF_REJECT flag set (for example,
19652 		 * when generating aggregate routes.)
19653 		 *
19654 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19655 		 * IRE_LOOPBACK, then we map the request into a
19656 		 * IRE_IF_NORESOLVER.
19657 		 */
19658 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19659 			ire->ire_type = IRE_IF_NORESOLVER;
19660 		/*
19661 		 * ire held by ire_add, will be refreled' towards the
19662 		 * the end of ipif_up_done
19663 		 */
19664 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19665 		*irep = ire;
19666 		irep++;
19667 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19668 	}
19669 	mutex_exit(&ipif->ipif_saved_ire_lock);
19670 	return (ipif_saved_irep);
19671 }
19672 
19673 /*
19674  * Used to set the netmask and broadcast address to default values when the
19675  * interface is brought up.  (Always called as writer.)
19676  */
19677 static void
19678 ipif_set_default(ipif_t *ipif)
19679 {
19680 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19681 
19682 	if (!ipif->ipif_isv6) {
19683 		/*
19684 		 * Interface holds an IPv4 address. Default
19685 		 * mask is the natural netmask.
19686 		 */
19687 		if (!ipif->ipif_net_mask) {
19688 			ipaddr_t	v4mask;
19689 
19690 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19691 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19692 		}
19693 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19694 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19695 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19696 		} else {
19697 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19698 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19699 		}
19700 		/*
19701 		 * NOTE: SunOS 4.X does this even if the broadcast address
19702 		 * has been already set thus we do the same here.
19703 		 */
19704 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19705 			ipaddr_t	v4addr;
19706 
19707 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19708 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19709 		}
19710 	} else {
19711 		/*
19712 		 * Interface holds an IPv6-only address.  Default
19713 		 * mask is all-ones.
19714 		 */
19715 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19716 			ipif->ipif_v6net_mask = ipv6_all_ones;
19717 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19718 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19719 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19720 		} else {
19721 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19722 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19723 		}
19724 	}
19725 }
19726 
19727 /*
19728  * Return 0 if this address can be used as local address without causing
19729  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19730  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19731  * Special checks are needed to allow the same IPv6 link-local address
19732  * on different ills.
19733  * TODO: allowing the same site-local address on different ill's.
19734  */
19735 int
19736 ip_addr_availability_check(ipif_t *new_ipif)
19737 {
19738 	in6_addr_t our_v6addr;
19739 	ill_t *ill;
19740 	ipif_t *ipif;
19741 	ill_walk_context_t ctx;
19742 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19743 
19744 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19745 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19746 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19747 
19748 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19749 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19750 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19751 		return (0);
19752 
19753 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19754 
19755 	if (new_ipif->ipif_isv6)
19756 		ill = ILL_START_WALK_V6(&ctx, ipst);
19757 	else
19758 		ill = ILL_START_WALK_V4(&ctx, ipst);
19759 
19760 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19761 		for (ipif = ill->ill_ipif; ipif != NULL;
19762 		    ipif = ipif->ipif_next) {
19763 			if ((ipif == new_ipif) ||
19764 			    !(ipif->ipif_flags & IPIF_UP) ||
19765 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19766 				continue;
19767 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19768 			    &our_v6addr)) {
19769 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19770 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19771 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19772 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19773 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19774 				    new_ipif->ipif_ill != ill)
19775 					continue;
19776 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19777 				    new_ipif->ipif_ill != ill)
19778 					continue;
19779 				else if (new_ipif->ipif_zoneid !=
19780 				    ipif->ipif_zoneid &&
19781 				    ipif->ipif_zoneid != ALL_ZONES &&
19782 				    IS_LOOPBACK(ill))
19783 					continue;
19784 				else if (new_ipif->ipif_ill == ill)
19785 					return (EADDRINUSE);
19786 				else
19787 					return (EADDRNOTAVAIL);
19788 			}
19789 		}
19790 	}
19791 
19792 	return (0);
19793 }
19794 
19795 /*
19796  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19797  * IREs for the ipif.
19798  * When the routine returns EINPROGRESS then mp has been consumed and
19799  * the ioctl will be acked from ip_rput_dlpi.
19800  */
19801 static int
19802 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19803 {
19804 	ill_t	*ill = ipif->ipif_ill;
19805 	boolean_t isv6 = ipif->ipif_isv6;
19806 	int	err = 0;
19807 	boolean_t success;
19808 
19809 	ASSERT(IAM_WRITER_IPIF(ipif));
19810 
19811 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19812 
19813 	/* Shouldn't get here if it is already up. */
19814 	if (ipif->ipif_flags & IPIF_UP)
19815 		return (EALREADY);
19816 
19817 	/* Skip arp/ndp for any loopback interface. */
19818 	if (ill->ill_wq != NULL) {
19819 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19820 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19821 
19822 		if (!ill->ill_dl_up) {
19823 			/*
19824 			 * ill_dl_up is not yet set. i.e. we are yet to
19825 			 * DL_BIND with the driver and this is the first
19826 			 * logical interface on the ill to become "up".
19827 			 * Tell the driver to get going (via DL_BIND_REQ).
19828 			 * Note that changing "significant" IFF_ flags
19829 			 * address/netmask etc cause a down/up dance, but
19830 			 * does not cause an unbind (DL_UNBIND) with the driver
19831 			 */
19832 			return (ill_dl_up(ill, ipif, mp, q));
19833 		}
19834 
19835 		/*
19836 		 * ipif_resolver_up may end up sending an
19837 		 * AR_INTERFACE_UP message to ARP, which would, in
19838 		 * turn send a DLPI message to the driver. ioctls are
19839 		 * serialized and so we cannot send more than one
19840 		 * interface up message at a time. If ipif_resolver_up
19841 		 * does send an interface up message to ARP, we get
19842 		 * EINPROGRESS and we will complete in ip_arp_done.
19843 		 */
19844 
19845 		ASSERT(connp != NULL || !CONN_Q(q));
19846 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19847 		if (connp != NULL)
19848 			mutex_enter(&connp->conn_lock);
19849 		mutex_enter(&ill->ill_lock);
19850 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19851 		mutex_exit(&ill->ill_lock);
19852 		if (connp != NULL)
19853 			mutex_exit(&connp->conn_lock);
19854 		if (!success)
19855 			return (EINTR);
19856 
19857 		/*
19858 		 * Crank up IPv6 neighbor discovery
19859 		 * Unlike ARP, this should complete when
19860 		 * ipif_ndp_up returns. However, for
19861 		 * ILLF_XRESOLV interfaces we also send a
19862 		 * AR_INTERFACE_UP to the external resolver.
19863 		 * That ioctl will complete in ip_rput.
19864 		 */
19865 		if (isv6) {
19866 			err = ipif_ndp_up(ipif);
19867 			if (err != 0) {
19868 				if (err != EINPROGRESS)
19869 					mp = ipsq_pending_mp_get(ipsq, &connp);
19870 				return (err);
19871 			}
19872 		}
19873 		/* Now, ARP */
19874 		err = ipif_resolver_up(ipif, Res_act_initial);
19875 		if (err == EINPROGRESS) {
19876 			/* We will complete it in ip_arp_done */
19877 			return (err);
19878 		}
19879 		mp = ipsq_pending_mp_get(ipsq, &connp);
19880 		ASSERT(mp != NULL);
19881 		if (err != 0)
19882 			return (err);
19883 	} else {
19884 		/*
19885 		 * Interfaces without underlying hardware don't do duplicate
19886 		 * address detection.
19887 		 */
19888 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19889 		ipif->ipif_addr_ready = 1;
19890 	}
19891 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19892 }
19893 
19894 /*
19895  * Perform a bind for the physical device.
19896  * When the routine returns EINPROGRESS then mp has been consumed and
19897  * the ioctl will be acked from ip_rput_dlpi.
19898  * Allocate an unbind message and save it until ipif_down.
19899  */
19900 static int
19901 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19902 {
19903 	areq_t	*areq;
19904 	mblk_t	*areq_mp = NULL;
19905 	mblk_t	*bind_mp = NULL;
19906 	mblk_t	*unbind_mp = NULL;
19907 	conn_t	*connp;
19908 	boolean_t success;
19909 	uint16_t sap_addr;
19910 
19911 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19912 	ASSERT(IAM_WRITER_ILL(ill));
19913 	ASSERT(mp != NULL);
19914 
19915 	/* Create a resolver cookie for ARP */
19916 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19917 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19918 		if (areq_mp == NULL)
19919 			return (ENOMEM);
19920 
19921 		freemsg(ill->ill_resolver_mp);
19922 		ill->ill_resolver_mp = areq_mp;
19923 		areq = (areq_t *)areq_mp->b_rptr;
19924 		sap_addr = ill->ill_sap;
19925 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19926 	}
19927 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19928 	    DL_BIND_REQ);
19929 	if (bind_mp == NULL)
19930 		goto bad;
19931 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19932 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19933 
19934 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19935 	if (unbind_mp == NULL)
19936 		goto bad;
19937 
19938 	/*
19939 	 * Record state needed to complete this operation when the
19940 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19941 	 */
19942 	ASSERT(WR(q)->q_next == NULL);
19943 	connp = Q_TO_CONN(q);
19944 
19945 	mutex_enter(&connp->conn_lock);
19946 	mutex_enter(&ipif->ipif_ill->ill_lock);
19947 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19948 	mutex_exit(&ipif->ipif_ill->ill_lock);
19949 	mutex_exit(&connp->conn_lock);
19950 	if (!success)
19951 		goto bad;
19952 
19953 	/*
19954 	 * Save the unbind message for ill_dl_down(); it will be consumed when
19955 	 * the interface goes down.
19956 	 */
19957 	ASSERT(ill->ill_unbind_mp == NULL);
19958 	ill->ill_unbind_mp = unbind_mp;
19959 
19960 	ill_dlpi_send(ill, bind_mp);
19961 	/* Send down link-layer capabilities probe if not already done. */
19962 	ill_capability_probe(ill);
19963 
19964 	/*
19965 	 * Sysid used to rely on the fact that netboots set domainname
19966 	 * and the like. Now that miniroot boots aren't strictly netboots
19967 	 * and miniroot network configuration is driven from userland
19968 	 * these things still need to be set. This situation can be detected
19969 	 * by comparing the interface being configured here to the one
19970 	 * dhcifname was set to reference by the boot loader. Once sysid is
19971 	 * converted to use dhcp_ipc_getinfo() this call can go away.
19972 	 */
19973 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
19974 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
19975 	    (strlen(srpc_domain) == 0)) {
19976 		if (dhcpinit() != 0)
19977 			cmn_err(CE_WARN, "no cached dhcp response");
19978 	}
19979 
19980 	/*
19981 	 * This operation will complete in ip_rput_dlpi with either
19982 	 * a DL_BIND_ACK or DL_ERROR_ACK.
19983 	 */
19984 	return (EINPROGRESS);
19985 bad:
19986 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
19987 	/*
19988 	 * We don't have to check for possible removal from illgrp
19989 	 * as we have not yet inserted in illgrp. For groups
19990 	 * without names, this ipif is still not UP and hence
19991 	 * this could not have possibly had any influence in forming
19992 	 * groups.
19993 	 */
19994 
19995 	freemsg(bind_mp);
19996 	freemsg(unbind_mp);
19997 	return (ENOMEM);
19998 }
19999 
20000 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20001 
20002 /*
20003  * DLPI and ARP is up.
20004  * Create all the IREs associated with an interface bring up multicast.
20005  * Set the interface flag and finish other initialization
20006  * that potentially had to be differed to after DL_BIND_ACK.
20007  */
20008 int
20009 ipif_up_done(ipif_t *ipif)
20010 {
20011 	ire_t	*ire_array[20];
20012 	ire_t	**irep = ire_array;
20013 	ire_t	**irep1;
20014 	ipaddr_t net_mask = 0;
20015 	ipaddr_t subnet_mask, route_mask;
20016 	ill_t	*ill = ipif->ipif_ill;
20017 	queue_t	*stq;
20018 	ipif_t	 *src_ipif;
20019 	ipif_t   *tmp_ipif;
20020 	boolean_t	flush_ire_cache = B_TRUE;
20021 	int	err = 0;
20022 	phyint_t *phyi;
20023 	ire_t	**ipif_saved_irep = NULL;
20024 	int ipif_saved_ire_cnt;
20025 	int	cnt;
20026 	boolean_t	src_ipif_held = B_FALSE;
20027 	boolean_t	ire_added = B_FALSE;
20028 	boolean_t	loopback = B_FALSE;
20029 	ip_stack_t	*ipst = ill->ill_ipst;
20030 
20031 	ip1dbg(("ipif_up_done(%s:%u)\n",
20032 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20033 	/* Check if this is a loopback interface */
20034 	if (ipif->ipif_ill->ill_wq == NULL)
20035 		loopback = B_TRUE;
20036 
20037 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20038 	/*
20039 	 * If all other interfaces for this ill are down or DEPRECATED,
20040 	 * or otherwise unsuitable for source address selection, remove
20041 	 * any IRE_CACHE entries for this ill to make sure source
20042 	 * address selection gets to take this new ipif into account.
20043 	 * No need to hold ill_lock while traversing the ipif list since
20044 	 * we are writer
20045 	 */
20046 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20047 	    tmp_ipif = tmp_ipif->ipif_next) {
20048 		if (((tmp_ipif->ipif_flags &
20049 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20050 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20051 		    (tmp_ipif == ipif))
20052 			continue;
20053 		/* first useable pre-existing interface */
20054 		flush_ire_cache = B_FALSE;
20055 		break;
20056 	}
20057 	if (flush_ire_cache)
20058 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20059 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20060 
20061 	/*
20062 	 * Figure out which way the send-to queue should go.  Only
20063 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20064 	 * should show up here.
20065 	 */
20066 	switch (ill->ill_net_type) {
20067 	case IRE_IF_RESOLVER:
20068 		stq = ill->ill_rq;
20069 		break;
20070 	case IRE_IF_NORESOLVER:
20071 	case IRE_LOOPBACK:
20072 		stq = ill->ill_wq;
20073 		break;
20074 	default:
20075 		return (EINVAL);
20076 	}
20077 
20078 	if (IS_LOOPBACK(ill)) {
20079 		/*
20080 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20081 		 * ipif_lookup_on_name(), but in the case of zones we can have
20082 		 * several loopback addresses on lo0. So all the interfaces with
20083 		 * loopback addresses need to be marked IRE_LOOPBACK.
20084 		 */
20085 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20086 		    htonl(INADDR_LOOPBACK))
20087 			ipif->ipif_ire_type = IRE_LOOPBACK;
20088 		else
20089 			ipif->ipif_ire_type = IRE_LOCAL;
20090 	}
20091 
20092 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20093 		/*
20094 		 * Can't use our source address. Select a different
20095 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20096 		 */
20097 		src_ipif = ipif_select_source(ipif->ipif_ill,
20098 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20099 		if (src_ipif == NULL)
20100 			src_ipif = ipif;	/* Last resort */
20101 		else
20102 			src_ipif_held = B_TRUE;
20103 	} else {
20104 		src_ipif = ipif;
20105 	}
20106 
20107 	/* Create all the IREs associated with this interface */
20108 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20109 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20110 
20111 		/*
20112 		 * If we're on a labeled system then make sure that zone-
20113 		 * private addresses have proper remote host database entries.
20114 		 */
20115 		if (is_system_labeled() &&
20116 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20117 		    !tsol_check_interface_address(ipif))
20118 			return (EINVAL);
20119 
20120 		/* Register the source address for __sin6_src_id */
20121 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20122 		    ipif->ipif_zoneid, ipst);
20123 		if (err != 0) {
20124 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20125 			return (err);
20126 		}
20127 
20128 		/* If the interface address is set, create the local IRE. */
20129 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20130 		    (void *)ipif,
20131 		    ipif->ipif_ire_type,
20132 		    ntohl(ipif->ipif_lcl_addr)));
20133 		*irep++ = ire_create(
20134 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20135 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20136 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20137 		    NULL,				/* no gateway */
20138 		    &ip_loopback_mtuplus,		/* max frag size */
20139 		    NULL,
20140 		    ipif->ipif_rq,			/* recv-from queue */
20141 		    NULL,				/* no send-to queue */
20142 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20143 		    ipif,
20144 		    0,
20145 		    0,
20146 		    0,
20147 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20148 		    RTF_PRIVATE : 0,
20149 		    &ire_uinfo_null,
20150 		    NULL,
20151 		    NULL,
20152 		    ipst);
20153 	} else {
20154 		ip1dbg((
20155 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20156 		    ipif->ipif_ire_type,
20157 		    ntohl(ipif->ipif_lcl_addr),
20158 		    (uint_t)ipif->ipif_flags));
20159 	}
20160 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20161 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20162 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20163 	} else {
20164 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20165 	}
20166 
20167 	subnet_mask = ipif->ipif_net_mask;
20168 
20169 	/*
20170 	 * If mask was not specified, use natural netmask of
20171 	 * interface address. Also, store this mask back into the
20172 	 * ipif struct.
20173 	 */
20174 	if (subnet_mask == 0) {
20175 		subnet_mask = net_mask;
20176 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20177 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20178 		    ipif->ipif_v6subnet);
20179 	}
20180 
20181 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20182 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20183 	    ipif->ipif_subnet != INADDR_ANY) {
20184 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20185 
20186 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20187 			route_mask = IP_HOST_MASK;
20188 		} else {
20189 			route_mask = subnet_mask;
20190 		}
20191 
20192 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20193 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20194 		    (void *)ipif, (void *)ill,
20195 		    ill->ill_net_type,
20196 		    ntohl(ipif->ipif_subnet)));
20197 		*irep++ = ire_create(
20198 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20199 		    (uchar_t *)&route_mask,		/* mask */
20200 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20201 		    NULL,				/* no gateway */
20202 		    &ipif->ipif_mtu,			/* max frag */
20203 		    NULL,
20204 		    NULL,				/* no recv queue */
20205 		    stq,				/* send-to queue */
20206 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20207 		    ipif,
20208 		    0,
20209 		    0,
20210 		    0,
20211 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20212 		    &ire_uinfo_null,
20213 		    NULL,
20214 		    NULL,
20215 		    ipst);
20216 	}
20217 
20218 	/*
20219 	 * Create any necessary broadcast IREs.
20220 	 */
20221 	if (ipif->ipif_flags & IPIF_BROADCAST)
20222 		irep = ipif_create_bcast_ires(ipif, irep);
20223 
20224 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20225 
20226 	/* If an earlier ire_create failed, get out now */
20227 	for (irep1 = irep; irep1 > ire_array; ) {
20228 		irep1--;
20229 		if (*irep1 == NULL) {
20230 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20231 			err = ENOMEM;
20232 			goto bad;
20233 		}
20234 	}
20235 
20236 	/*
20237 	 * Need to atomically check for ip_addr_availablity_check
20238 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20239 	 * from group also.The ill_g_lock is grabbed as reader
20240 	 * just to make sure no new ills or new ipifs are being added
20241 	 * to the system while we are checking the uniqueness of addresses.
20242 	 */
20243 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20244 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20245 	/* Mark it up, and increment counters. */
20246 	ipif->ipif_flags |= IPIF_UP;
20247 	ill->ill_ipif_up_count++;
20248 	err = ip_addr_availability_check(ipif);
20249 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20250 	rw_exit(&ipst->ips_ill_g_lock);
20251 
20252 	if (err != 0) {
20253 		/*
20254 		 * Our address may already be up on the same ill. In this case,
20255 		 * the ARP entry for our ipif replaced the one for the other
20256 		 * ipif. So we don't want to delete it (otherwise the other ipif
20257 		 * would be unable to send packets).
20258 		 * ip_addr_availability_check() identifies this case for us and
20259 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20260 		 * which is the expected error code.
20261 		 */
20262 		if (err == EADDRINUSE) {
20263 			freemsg(ipif->ipif_arp_del_mp);
20264 			ipif->ipif_arp_del_mp = NULL;
20265 			err = EADDRNOTAVAIL;
20266 		}
20267 		ill->ill_ipif_up_count--;
20268 		ipif->ipif_flags &= ~IPIF_UP;
20269 		goto bad;
20270 	}
20271 
20272 	/*
20273 	 * Add in all newly created IREs.  ire_create_bcast() has
20274 	 * already checked for duplicates of the IRE_BROADCAST type.
20275 	 * We want to add before we call ifgrp_insert which wants
20276 	 * to know whether IRE_IF_RESOLVER exists or not.
20277 	 *
20278 	 * NOTE : We refrele the ire though we may branch to "bad"
20279 	 *	  later on where we do ire_delete. This is okay
20280 	 *	  because nobody can delete it as we are running
20281 	 *	  exclusively.
20282 	 */
20283 	for (irep1 = irep; irep1 > ire_array; ) {
20284 		irep1--;
20285 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20286 		/*
20287 		 * refheld by ire_add. refele towards the end of the func
20288 		 */
20289 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20290 	}
20291 	ire_added = B_TRUE;
20292 	/*
20293 	 * Form groups if possible.
20294 	 *
20295 	 * If we are supposed to be in a ill_group with a name, insert it
20296 	 * now as we know that at least one ipif is UP. Otherwise form
20297 	 * nameless groups.
20298 	 *
20299 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20300 	 * this ipif into the appropriate interface group, or create a
20301 	 * new one. If this is already in a nameless group, we try to form
20302 	 * a bigger group looking at other ills potentially sharing this
20303 	 * ipif's prefix.
20304 	 */
20305 	phyi = ill->ill_phyint;
20306 	if (phyi->phyint_groupname_len != 0) {
20307 		ASSERT(phyi->phyint_groupname != NULL);
20308 		if (ill->ill_ipif_up_count == 1) {
20309 			ASSERT(ill->ill_group == NULL);
20310 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20311 			    phyi->phyint_groupname, NULL, B_TRUE);
20312 			if (err != 0) {
20313 				ip1dbg(("ipif_up_done: illgrp allocation "
20314 				    "failed, error %d\n", err));
20315 				goto bad;
20316 			}
20317 		}
20318 		ASSERT(ill->ill_group != NULL);
20319 	}
20320 
20321 	/*
20322 	 * When this is part of group, we need to make sure that
20323 	 * any broadcast ires created because of this ipif coming
20324 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20325 	 * so that we don't receive duplicate broadcast packets.
20326 	 */
20327 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20328 		ipif_renominate_bcast(ipif);
20329 
20330 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20331 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20332 	ipif_saved_irep = ipif_recover_ire(ipif);
20333 
20334 	if (!loopback) {
20335 		/*
20336 		 * If the broadcast address has been set, make sure it makes
20337 		 * sense based on the interface address.
20338 		 * Only match on ill since we are sharing broadcast addresses.
20339 		 */
20340 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20341 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20342 			ire_t	*ire;
20343 
20344 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20345 			    IRE_BROADCAST, ipif, ALL_ZONES,
20346 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20347 
20348 			if (ire == NULL) {
20349 				/*
20350 				 * If there isn't a matching broadcast IRE,
20351 				 * revert to the default for this netmask.
20352 				 */
20353 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20354 				mutex_enter(&ipif->ipif_ill->ill_lock);
20355 				ipif_set_default(ipif);
20356 				mutex_exit(&ipif->ipif_ill->ill_lock);
20357 			} else {
20358 				ire_refrele(ire);
20359 			}
20360 		}
20361 
20362 	}
20363 
20364 	/* This is the first interface on this ill */
20365 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20366 		/*
20367 		 * Need to recover all multicast memberships in the driver.
20368 		 * This had to be deferred until we had attached.
20369 		 */
20370 		ill_recover_multicast(ill);
20371 	}
20372 	/* Join the allhosts multicast address */
20373 	ipif_multicast_up(ipif);
20374 
20375 	if (!loopback) {
20376 		/*
20377 		 * See whether anybody else would benefit from the
20378 		 * new ipif that we added. We call this always rather
20379 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20380 		 * ipif is for the benefit of illgrp_insert (done above)
20381 		 * which does not do source address selection as it does
20382 		 * not want to re-create interface routes that we are
20383 		 * having reference to it here.
20384 		 */
20385 		ill_update_source_selection(ill);
20386 	}
20387 
20388 	for (irep1 = irep; irep1 > ire_array; ) {
20389 		irep1--;
20390 		if (*irep1 != NULL) {
20391 			/* was held in ire_add */
20392 			ire_refrele(*irep1);
20393 		}
20394 	}
20395 
20396 	cnt = ipif_saved_ire_cnt;
20397 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20398 		if (*irep1 != NULL) {
20399 			/* was held in ire_add */
20400 			ire_refrele(*irep1);
20401 		}
20402 	}
20403 
20404 	if (!loopback && ipif->ipif_addr_ready) {
20405 		/* Broadcast an address mask reply. */
20406 		ipif_mask_reply(ipif);
20407 	}
20408 	if (ipif_saved_irep != NULL) {
20409 		kmem_free(ipif_saved_irep,
20410 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20411 	}
20412 	if (src_ipif_held)
20413 		ipif_refrele(src_ipif);
20414 
20415 	/*
20416 	 * This had to be deferred until we had bound.  Tell routing sockets and
20417 	 * others that this interface is up if it looks like the address has
20418 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20419 	 * duplicate address detection to do its thing.
20420 	 */
20421 	if (ipif->ipif_addr_ready) {
20422 		ip_rts_ifmsg(ipif);
20423 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20424 		/* Let SCTP update the status for this ipif */
20425 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20426 	}
20427 	return (0);
20428 
20429 bad:
20430 	ip1dbg(("ipif_up_done: FAILED \n"));
20431 	/*
20432 	 * We don't have to bother removing from ill groups because
20433 	 *
20434 	 * 1) For groups with names, we insert only when the first ipif
20435 	 *    comes up. In that case if it fails, it will not be in any
20436 	 *    group. So, we need not try to remove for that case.
20437 	 *
20438 	 * 2) For groups without names, either we tried to insert ipif_ill
20439 	 *    in a group as singleton or found some other group to become
20440 	 *    a bigger group. For the former, if it fails we don't have
20441 	 *    anything to do as ipif_ill is not in the group and for the
20442 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20443 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20444 	 */
20445 	while (irep > ire_array) {
20446 		irep--;
20447 		if (*irep != NULL) {
20448 			ire_delete(*irep);
20449 			if (ire_added)
20450 				ire_refrele(*irep);
20451 		}
20452 	}
20453 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20454 
20455 	if (ipif_saved_irep != NULL) {
20456 		kmem_free(ipif_saved_irep,
20457 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20458 	}
20459 	if (src_ipif_held)
20460 		ipif_refrele(src_ipif);
20461 
20462 	ipif_arp_down(ipif);
20463 	return (err);
20464 }
20465 
20466 /*
20467  * Turn off the ARP with the ILLF_NOARP flag.
20468  */
20469 static int
20470 ill_arp_off(ill_t *ill)
20471 {
20472 	mblk_t	*arp_off_mp = NULL;
20473 	mblk_t	*arp_on_mp = NULL;
20474 
20475 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20476 
20477 	ASSERT(IAM_WRITER_ILL(ill));
20478 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20479 
20480 	/*
20481 	 * If the on message is still around we've already done
20482 	 * an arp_off without doing an arp_on thus there is no
20483 	 * work needed.
20484 	 */
20485 	if (ill->ill_arp_on_mp != NULL)
20486 		return (0);
20487 
20488 	/*
20489 	 * Allocate an ARP on message (to be saved) and an ARP off message
20490 	 */
20491 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20492 	if (!arp_off_mp)
20493 		return (ENOMEM);
20494 
20495 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20496 	if (!arp_on_mp)
20497 		goto failed;
20498 
20499 	ASSERT(ill->ill_arp_on_mp == NULL);
20500 	ill->ill_arp_on_mp = arp_on_mp;
20501 
20502 	/* Send an AR_INTERFACE_OFF request */
20503 	putnext(ill->ill_rq, arp_off_mp);
20504 	return (0);
20505 failed:
20506 
20507 	if (arp_off_mp)
20508 		freemsg(arp_off_mp);
20509 	return (ENOMEM);
20510 }
20511 
20512 /*
20513  * Turn on ARP by turning off the ILLF_NOARP flag.
20514  */
20515 static int
20516 ill_arp_on(ill_t *ill)
20517 {
20518 	mblk_t	*mp;
20519 
20520 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20521 
20522 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20523 
20524 	ASSERT(IAM_WRITER_ILL(ill));
20525 	/*
20526 	 * Send an AR_INTERFACE_ON request if we have already done
20527 	 * an arp_off (which allocated the message).
20528 	 */
20529 	if (ill->ill_arp_on_mp != NULL) {
20530 		mp = ill->ill_arp_on_mp;
20531 		ill->ill_arp_on_mp = NULL;
20532 		putnext(ill->ill_rq, mp);
20533 	}
20534 	return (0);
20535 }
20536 
20537 /*
20538  * Called after either deleting ill from the group or when setting
20539  * FAILED or STANDBY on the interface.
20540  */
20541 static void
20542 illgrp_reset_schednext(ill_t *ill)
20543 {
20544 	ill_group_t *illgrp;
20545 	ill_t *save_ill;
20546 
20547 	ASSERT(IAM_WRITER_ILL(ill));
20548 	/*
20549 	 * When called from illgrp_delete, ill_group will be non-NULL.
20550 	 * But when called from ip_sioctl_flags, it could be NULL if
20551 	 * somebody is setting FAILED/INACTIVE on some interface which
20552 	 * is not part of a group.
20553 	 */
20554 	illgrp = ill->ill_group;
20555 	if (illgrp == NULL)
20556 		return;
20557 	if (illgrp->illgrp_ill_schednext != ill)
20558 		return;
20559 
20560 	illgrp->illgrp_ill_schednext = NULL;
20561 	save_ill = ill;
20562 	/*
20563 	 * Choose a good ill to be the next one for
20564 	 * outbound traffic. As the flags FAILED/STANDBY is
20565 	 * not yet marked when called from ip_sioctl_flags,
20566 	 * we check for ill separately.
20567 	 */
20568 	for (ill = illgrp->illgrp_ill; ill != NULL;
20569 	    ill = ill->ill_group_next) {
20570 		if ((ill != save_ill) &&
20571 		    !(ill->ill_phyint->phyint_flags &
20572 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20573 			illgrp->illgrp_ill_schednext = ill;
20574 			return;
20575 		}
20576 	}
20577 }
20578 
20579 /*
20580  * Given an ill, find the next ill in the group to be scheduled.
20581  * (This should be called by ip_newroute() before ire_create().)
20582  * The passed in ill may be pulled out of the group, after we have picked
20583  * up a different outgoing ill from the same group. However ire add will
20584  * atomically check this.
20585  */
20586 ill_t *
20587 illgrp_scheduler(ill_t *ill)
20588 {
20589 	ill_t *retill;
20590 	ill_group_t *illgrp;
20591 	int illcnt;
20592 	int i;
20593 	uint64_t flags;
20594 	ip_stack_t	*ipst = ill->ill_ipst;
20595 
20596 	/*
20597 	 * We don't use a lock to check for the ill_group. If this ill
20598 	 * is currently being inserted we may end up just returning this
20599 	 * ill itself. That is ok.
20600 	 */
20601 	if (ill->ill_group == NULL) {
20602 		ill_refhold(ill);
20603 		return (ill);
20604 	}
20605 
20606 	/*
20607 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20608 	 * a set of stable ills. No ill can be added or deleted or change
20609 	 * group while we hold the reader lock.
20610 	 */
20611 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20612 	if ((illgrp = ill->ill_group) == NULL) {
20613 		rw_exit(&ipst->ips_ill_g_lock);
20614 		ill_refhold(ill);
20615 		return (ill);
20616 	}
20617 
20618 	illcnt = illgrp->illgrp_ill_count;
20619 	mutex_enter(&illgrp->illgrp_lock);
20620 	retill = illgrp->illgrp_ill_schednext;
20621 
20622 	if (retill == NULL)
20623 		retill = illgrp->illgrp_ill;
20624 
20625 	/*
20626 	 * We do a circular search beginning at illgrp_ill_schednext
20627 	 * or illgrp_ill. We don't check the flags against the ill lock
20628 	 * since it can change anytime. The ire creation will be atomic
20629 	 * and will fail if the ill is FAILED or OFFLINE.
20630 	 */
20631 	for (i = 0; i < illcnt; i++) {
20632 		flags = retill->ill_phyint->phyint_flags;
20633 
20634 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20635 		    ILL_CAN_LOOKUP(retill)) {
20636 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20637 			ill_refhold(retill);
20638 			break;
20639 		}
20640 		retill = retill->ill_group_next;
20641 		if (retill == NULL)
20642 			retill = illgrp->illgrp_ill;
20643 	}
20644 	mutex_exit(&illgrp->illgrp_lock);
20645 	rw_exit(&ipst->ips_ill_g_lock);
20646 
20647 	return (i == illcnt ? NULL : retill);
20648 }
20649 
20650 /*
20651  * Checks for availbility of a usable source address (if there is one) when the
20652  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20653  * this selection is done regardless of the destination.
20654  */
20655 boolean_t
20656 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20657 {
20658 	uint_t	ifindex;
20659 	ipif_t	*ipif = NULL;
20660 	ill_t	*uill;
20661 	boolean_t isv6;
20662 	ip_stack_t	*ipst = ill->ill_ipst;
20663 
20664 	ASSERT(ill != NULL);
20665 
20666 	isv6 = ill->ill_isv6;
20667 	ifindex = ill->ill_usesrc_ifindex;
20668 	if (ifindex != 0) {
20669 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20670 		    NULL, ipst);
20671 		if (uill == NULL)
20672 			return (NULL);
20673 		mutex_enter(&uill->ill_lock);
20674 		for (ipif = uill->ill_ipif; ipif != NULL;
20675 		    ipif = ipif->ipif_next) {
20676 			if (!IPIF_CAN_LOOKUP(ipif))
20677 				continue;
20678 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20679 				continue;
20680 			if (!(ipif->ipif_flags & IPIF_UP))
20681 				continue;
20682 			if (ipif->ipif_zoneid != zoneid)
20683 				continue;
20684 			if ((isv6 &&
20685 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20686 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20687 				continue;
20688 			mutex_exit(&uill->ill_lock);
20689 			ill_refrele(uill);
20690 			return (B_TRUE);
20691 		}
20692 		mutex_exit(&uill->ill_lock);
20693 		ill_refrele(uill);
20694 	}
20695 	return (B_FALSE);
20696 }
20697 
20698 /*
20699  * Determine the best source address given a destination address and an ill.
20700  * Prefers non-deprecated over deprecated but will return a deprecated
20701  * address if there is no other choice. If there is a usable source address
20702  * on the interface pointed to by ill_usesrc_ifindex then that is given
20703  * first preference.
20704  *
20705  * Returns NULL if there is no suitable source address for the ill.
20706  * This only occurs when there is no valid source address for the ill.
20707  */
20708 ipif_t *
20709 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20710 {
20711 	ipif_t *ipif;
20712 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20713 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20714 	int index = 0;
20715 	boolean_t wrapped = B_FALSE;
20716 	boolean_t same_subnet_only = B_FALSE;
20717 	boolean_t ipif_same_found, ipif_other_found;
20718 	boolean_t specific_found;
20719 	ill_t	*till, *usill = NULL;
20720 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20721 	ip_stack_t	*ipst = ill->ill_ipst;
20722 
20723 	if (ill->ill_usesrc_ifindex != 0) {
20724 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20725 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20726 		if (usill != NULL)
20727 			ill = usill;	/* Select source from usesrc ILL */
20728 		else
20729 			return (NULL);
20730 	}
20731 
20732 	/*
20733 	 * If we're dealing with an unlabeled destination on a labeled system,
20734 	 * make sure that we ignore source addresses that are incompatible with
20735 	 * the destination's default label.  That destination's default label
20736 	 * must dominate the minimum label on the source address.
20737 	 */
20738 	dst_rhtp = NULL;
20739 	if (is_system_labeled()) {
20740 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20741 		if (dst_rhtp == NULL)
20742 			return (NULL);
20743 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20744 			TPC_RELE(dst_rhtp);
20745 			dst_rhtp = NULL;
20746 		}
20747 	}
20748 
20749 	/*
20750 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20751 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20752 	 * After selecting the right ipif, under ill_lock make sure ipif is
20753 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20754 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20755 	 * but not under a lock.
20756 	 */
20757 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20758 
20759 retry:
20760 	till = ill;
20761 	ipif_arr[0] = NULL;
20762 
20763 	if (till->ill_group != NULL)
20764 		till = till->ill_group->illgrp_ill;
20765 
20766 	/*
20767 	 * Choose one good source address from each ill across the group.
20768 	 * If possible choose a source address in the same subnet as
20769 	 * the destination address.
20770 	 *
20771 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20772 	 * This is okay because of the following.
20773 	 *
20774 	 *    If PHYI_FAILED is set and we still have non-deprecated
20775 	 *    addresses, it means the addresses have not yet been
20776 	 *    failed over to a different interface. We potentially
20777 	 *    select them to create IRE_CACHES, which will be later
20778 	 *    flushed when the addresses move over.
20779 	 *
20780 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20781 	 *    addresses, it means either the user has configured them
20782 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20783 	 *    been moved over. For the former, in.mpathd does a failover
20784 	 *    when the interface becomes INACTIVE and hence we should
20785 	 *    not find them. Once INACTIVE is set, we don't allow them
20786 	 *    to create logical interfaces anymore. For the latter, a
20787 	 *    flush will happen when INACTIVE is cleared which will
20788 	 *    flush the IRE_CACHES.
20789 	 *
20790 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20791 	 *    over soon. We potentially select them to create IRE_CACHEs,
20792 	 *    which will be later flushed when the addresses move over.
20793 	 *
20794 	 * NOTE : As ipif_select_source is called to borrow source address
20795 	 * for an ipif that is part of a group, source address selection
20796 	 * will be re-done whenever the group changes i.e either an
20797 	 * insertion/deletion in the group.
20798 	 *
20799 	 * Fill ipif_arr[] with source addresses, using these rules:
20800 	 *
20801 	 *	1. At most one source address from a given ill ends up
20802 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20803 	 *	   associated with a given ill ends up in ipif_arr[].
20804 	 *
20805 	 *	2. If there is at least one non-deprecated ipif in the
20806 	 *	   IPMP group with a source address on the same subnet as
20807 	 *	   our destination, then fill ipif_arr[] only with
20808 	 *	   source addresses on the same subnet as our destination.
20809 	 *	   Note that because of (1), only the first
20810 	 *	   non-deprecated ipif found with a source address
20811 	 *	   matching the destination ends up in ipif_arr[].
20812 	 *
20813 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20814 	 *	   addresses not in the same subnet as our destination.
20815 	 *	   Again, because of (1), only the first off-subnet source
20816 	 *	   address will be chosen.
20817 	 *
20818 	 *	4. If there are no non-deprecated ipifs, then just use
20819 	 *	   the source address associated with the last deprecated
20820 	 *	   one we find that happens to be on the same subnet,
20821 	 *	   otherwise the first one not in the same subnet.
20822 	 */
20823 	specific_found = B_FALSE;
20824 	for (; till != NULL; till = till->ill_group_next) {
20825 		ipif_same_found = B_FALSE;
20826 		ipif_other_found = B_FALSE;
20827 		for (ipif = till->ill_ipif; ipif != NULL;
20828 		    ipif = ipif->ipif_next) {
20829 			if (!IPIF_CAN_LOOKUP(ipif))
20830 				continue;
20831 			/* Always skip NOLOCAL and ANYCAST interfaces */
20832 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20833 				continue;
20834 			if (!(ipif->ipif_flags & IPIF_UP) ||
20835 			    !ipif->ipif_addr_ready)
20836 				continue;
20837 			if (ipif->ipif_zoneid != zoneid &&
20838 			    ipif->ipif_zoneid != ALL_ZONES)
20839 				continue;
20840 			/*
20841 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20842 			 * but are not valid as source addresses.
20843 			 */
20844 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20845 				continue;
20846 
20847 			/*
20848 			 * Check compatibility of local address for
20849 			 * destination's default label if we're on a labeled
20850 			 * system.  Incompatible addresses can't be used at
20851 			 * all.
20852 			 */
20853 			if (dst_rhtp != NULL) {
20854 				boolean_t incompat;
20855 
20856 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20857 				    IPV4_VERSION, B_FALSE);
20858 				if (src_rhtp == NULL)
20859 					continue;
20860 				incompat =
20861 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20862 				    src_rhtp->tpc_tp.tp_doi !=
20863 				    dst_rhtp->tpc_tp.tp_doi ||
20864 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20865 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20866 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20867 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20868 				TPC_RELE(src_rhtp);
20869 				if (incompat)
20870 					continue;
20871 			}
20872 
20873 			/*
20874 			 * We prefer not to use all all-zones addresses, if we
20875 			 * can avoid it, as they pose problems with unlabeled
20876 			 * destinations.
20877 			 */
20878 			if (ipif->ipif_zoneid != ALL_ZONES) {
20879 				if (!specific_found &&
20880 				    (!same_subnet_only ||
20881 				    (ipif->ipif_net_mask & dst) ==
20882 				    ipif->ipif_subnet)) {
20883 					index = 0;
20884 					specific_found = B_TRUE;
20885 					ipif_other_found = B_FALSE;
20886 				}
20887 			} else {
20888 				if (specific_found)
20889 					continue;
20890 			}
20891 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20892 				if (ipif_dep == NULL ||
20893 				    (ipif->ipif_net_mask & dst) ==
20894 				    ipif->ipif_subnet)
20895 					ipif_dep = ipif;
20896 				continue;
20897 			}
20898 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20899 				/* found a source address in the same subnet */
20900 				if (!same_subnet_only) {
20901 					same_subnet_only = B_TRUE;
20902 					index = 0;
20903 				}
20904 				ipif_same_found = B_TRUE;
20905 			} else {
20906 				if (same_subnet_only || ipif_other_found)
20907 					continue;
20908 				ipif_other_found = B_TRUE;
20909 			}
20910 			ipif_arr[index++] = ipif;
20911 			if (index == MAX_IPIF_SELECT_SOURCE) {
20912 				wrapped = B_TRUE;
20913 				index = 0;
20914 			}
20915 			if (ipif_same_found)
20916 				break;
20917 		}
20918 	}
20919 
20920 	if (ipif_arr[0] == NULL) {
20921 		ipif = ipif_dep;
20922 	} else {
20923 		if (wrapped)
20924 			index = MAX_IPIF_SELECT_SOURCE;
20925 		ipif = ipif_arr[ipif_rand(ipst) % index];
20926 		ASSERT(ipif != NULL);
20927 	}
20928 
20929 	if (ipif != NULL) {
20930 		mutex_enter(&ipif->ipif_ill->ill_lock);
20931 		if (!IPIF_CAN_LOOKUP(ipif)) {
20932 			mutex_exit(&ipif->ipif_ill->ill_lock);
20933 			goto retry;
20934 		}
20935 		ipif_refhold_locked(ipif);
20936 		mutex_exit(&ipif->ipif_ill->ill_lock);
20937 	}
20938 
20939 	rw_exit(&ipst->ips_ill_g_lock);
20940 	if (usill != NULL)
20941 		ill_refrele(usill);
20942 	if (dst_rhtp != NULL)
20943 		TPC_RELE(dst_rhtp);
20944 
20945 #ifdef DEBUG
20946 	if (ipif == NULL) {
20947 		char buf1[INET6_ADDRSTRLEN];
20948 
20949 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20950 		    ill->ill_name,
20951 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20952 	} else {
20953 		char buf1[INET6_ADDRSTRLEN];
20954 		char buf2[INET6_ADDRSTRLEN];
20955 
20956 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20957 		    ipif->ipif_ill->ill_name,
20958 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
20959 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
20960 		    buf2, sizeof (buf2))));
20961 	}
20962 #endif /* DEBUG */
20963 	return (ipif);
20964 }
20965 
20966 
20967 /*
20968  * If old_ipif is not NULL, see if ipif was derived from old
20969  * ipif and if so, recreate the interface route by re-doing
20970  * source address selection. This happens when ipif_down ->
20971  * ipif_update_other_ipifs calls us.
20972  *
20973  * If old_ipif is NULL, just redo the source address selection
20974  * if needed. This happens when illgrp_insert or ipif_up_done
20975  * calls us.
20976  */
20977 static void
20978 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
20979 {
20980 	ire_t *ire;
20981 	ire_t *ipif_ire;
20982 	queue_t *stq;
20983 	ipif_t *nipif;
20984 	ill_t *ill;
20985 	boolean_t need_rele = B_FALSE;
20986 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
20987 
20988 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
20989 	ASSERT(IAM_WRITER_IPIF(ipif));
20990 
20991 	ill = ipif->ipif_ill;
20992 	if (!(ipif->ipif_flags &
20993 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
20994 		/*
20995 		 * Can't possibly have borrowed the source
20996 		 * from old_ipif.
20997 		 */
20998 		return;
20999 	}
21000 
21001 	/*
21002 	 * Is there any work to be done? No work if the address
21003 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21004 	 * ipif_select_source() does not borrow addresses from
21005 	 * NOLOCAL and ANYCAST interfaces).
21006 	 */
21007 	if ((old_ipif != NULL) &&
21008 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21009 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21010 	    (old_ipif->ipif_flags &
21011 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21012 		return;
21013 	}
21014 
21015 	/*
21016 	 * Perform the same checks as when creating the
21017 	 * IRE_INTERFACE in ipif_up_done.
21018 	 */
21019 	if (!(ipif->ipif_flags & IPIF_UP))
21020 		return;
21021 
21022 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21023 	    (ipif->ipif_subnet == INADDR_ANY))
21024 		return;
21025 
21026 	ipif_ire = ipif_to_ire(ipif);
21027 	if (ipif_ire == NULL)
21028 		return;
21029 
21030 	/*
21031 	 * We know that ipif uses some other source for its
21032 	 * IRE_INTERFACE. Is it using the source of this
21033 	 * old_ipif?
21034 	 */
21035 	if (old_ipif != NULL &&
21036 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21037 		ire_refrele(ipif_ire);
21038 		return;
21039 	}
21040 	if (ip_debug > 2) {
21041 		/* ip1dbg */
21042 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21043 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21044 	}
21045 
21046 	stq = ipif_ire->ire_stq;
21047 
21048 	/*
21049 	 * Can't use our source address. Select a different
21050 	 * source address for the IRE_INTERFACE.
21051 	 */
21052 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21053 	if (nipif == NULL) {
21054 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21055 		nipif = ipif;
21056 	} else {
21057 		need_rele = B_TRUE;
21058 	}
21059 
21060 	ire = ire_create(
21061 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21062 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21063 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21064 	    NULL,				/* no gateway */
21065 	    &ipif->ipif_mtu,			/* max frag */
21066 	    NULL,				/* no src nce */
21067 	    NULL,				/* no recv from queue */
21068 	    stq,				/* send-to queue */
21069 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21070 	    ipif,
21071 	    0,
21072 	    0,
21073 	    0,
21074 	    0,
21075 	    &ire_uinfo_null,
21076 	    NULL,
21077 	    NULL,
21078 	    ipst);
21079 
21080 	if (ire != NULL) {
21081 		ire_t *ret_ire;
21082 		int error;
21083 
21084 		/*
21085 		 * We don't need ipif_ire anymore. We need to delete
21086 		 * before we add so that ire_add does not detect
21087 		 * duplicates.
21088 		 */
21089 		ire_delete(ipif_ire);
21090 		ret_ire = ire;
21091 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21092 		ASSERT(error == 0);
21093 		ASSERT(ire == ret_ire);
21094 		/* Held in ire_add */
21095 		ire_refrele(ret_ire);
21096 	}
21097 	/*
21098 	 * Either we are falling through from above or could not
21099 	 * allocate a replacement.
21100 	 */
21101 	ire_refrele(ipif_ire);
21102 	if (need_rele)
21103 		ipif_refrele(nipif);
21104 }
21105 
21106 /*
21107  * This old_ipif is going away.
21108  *
21109  * Determine if any other ipif's is using our address as
21110  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21111  * IPIF_DEPRECATED).
21112  * Find the IRE_INTERFACE for such ipifs and recreate them
21113  * to use an different source address following the rules in
21114  * ipif_up_done.
21115  *
21116  * This function takes an illgrp as an argument so that illgrp_delete
21117  * can call this to update source address even after deleting the
21118  * old_ipif->ipif_ill from the ill group.
21119  */
21120 static void
21121 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21122 {
21123 	ipif_t *ipif;
21124 	ill_t *ill;
21125 	char	buf[INET6_ADDRSTRLEN];
21126 
21127 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21128 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21129 
21130 	ill = old_ipif->ipif_ill;
21131 
21132 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21133 	    ill->ill_name,
21134 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21135 	    buf, sizeof (buf))));
21136 	/*
21137 	 * If this part of a group, look at all ills as ipif_select_source
21138 	 * borrows source address across all the ills in the group.
21139 	 */
21140 	if (illgrp != NULL)
21141 		ill = illgrp->illgrp_ill;
21142 
21143 	for (; ill != NULL; ill = ill->ill_group_next) {
21144 		for (ipif = ill->ill_ipif; ipif != NULL;
21145 		    ipif = ipif->ipif_next) {
21146 
21147 			if (ipif == old_ipif)
21148 				continue;
21149 
21150 			ipif_recreate_interface_routes(old_ipif, ipif);
21151 		}
21152 	}
21153 }
21154 
21155 /* ARGSUSED */
21156 int
21157 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21158 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21159 {
21160 	/*
21161 	 * ill_phyint_reinit merged the v4 and v6 into a single
21162 	 * ipsq. Could also have become part of a ipmp group in the
21163 	 * process, and we might not have been able to complete the
21164 	 * operation in ipif_set_values, if we could not become
21165 	 * exclusive.  If so restart it here.
21166 	 */
21167 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21168 }
21169 
21170 /*
21171  * Can operate on either a module or a driver queue.
21172  * Returns an error if not a module queue.
21173  */
21174 /* ARGSUSED */
21175 int
21176 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21177     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21178 {
21179 	queue_t		*q1 = q;
21180 	char 		*cp;
21181 	char		interf_name[LIFNAMSIZ];
21182 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21183 
21184 	if (q->q_next == NULL) {
21185 		ip1dbg((
21186 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21187 		return (EINVAL);
21188 	}
21189 
21190 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21191 		return (EALREADY);
21192 
21193 	do {
21194 		q1 = q1->q_next;
21195 	} while (q1->q_next);
21196 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21197 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21198 
21199 	/*
21200 	 * Here we are not going to delay the ioack until after
21201 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21202 	 * original ioctl message before sending the requests.
21203 	 */
21204 	return (ipif_set_values(q, mp, interf_name, &ppa));
21205 }
21206 
21207 /* ARGSUSED */
21208 int
21209 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21210     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21211 {
21212 	return (ENXIO);
21213 }
21214 
21215 /*
21216  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21217  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21218  * ire_check_and_create_bcast()).
21219  */
21220 static ire_t **
21221 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21222 {
21223 	ipaddr_t addr;
21224 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21225 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21226 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21227 
21228 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21229 
21230 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21231 
21232 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21233 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21234 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21235 
21236 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21237 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21238 
21239 	/*
21240 	 * For backward compatibility, we create net broadcast IREs based on
21241 	 * the old "IP address class system", since some old machines only
21242 	 * respond to these class derived net broadcast.  However, we must not
21243 	 * create these net broadcast IREs if the subnetmask is shorter than
21244 	 * the IP address class based derived netmask.  Otherwise, we may
21245 	 * create a net broadcast address which is the same as an IP address
21246 	 * on the subnet -- and then TCP will refuse to talk to that address.
21247 	 */
21248 	if (netmask < subnetmask) {
21249 		addr = netmask & ipif->ipif_subnet;
21250 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21251 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21252 		    flags);
21253 	}
21254 
21255 	/*
21256 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21257 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21258 	 * created.  Creating these broadcast IREs will only create confusion
21259 	 * as `addr' will be the same as the IP address.
21260 	 */
21261 	if (subnetmask != 0xFFFFFFFF) {
21262 		addr = ipif->ipif_subnet;
21263 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21264 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21265 		    irep, flags);
21266 	}
21267 
21268 	return (irep);
21269 }
21270 
21271 /*
21272  * Broadcast IRE info structure used in the functions below.  Since we
21273  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21274  */
21275 typedef struct bcast_ireinfo {
21276 	uchar_t		bi_type;	/* BCAST_* value from below */
21277 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21278 			bi_needrep:1,	/* do we need to replace it? */
21279 			bi_haverep:1,	/* have we replaced it? */
21280 			bi_pad:5;
21281 	ipaddr_t	bi_addr;	/* IRE address */
21282 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21283 } bcast_ireinfo_t;
21284 
21285 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21286 
21287 /*
21288  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21289  * return B_TRUE if it should immediately be used to recreate the IRE.
21290  */
21291 static boolean_t
21292 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21293 {
21294 	ipaddr_t addr;
21295 
21296 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21297 
21298 	switch (bireinfop->bi_type) {
21299 	case BCAST_NET:
21300 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21301 		if (addr != bireinfop->bi_addr)
21302 			return (B_FALSE);
21303 		break;
21304 	case BCAST_SUBNET:
21305 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21306 			return (B_FALSE);
21307 		break;
21308 	}
21309 
21310 	bireinfop->bi_needrep = 1;
21311 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21312 		if (bireinfop->bi_backup == NULL)
21313 			bireinfop->bi_backup = ipif;
21314 		return (B_FALSE);
21315 	}
21316 	return (B_TRUE);
21317 }
21318 
21319 /*
21320  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21321  * them ala ire_check_and_create_bcast().
21322  */
21323 static ire_t **
21324 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21325 {
21326 	ipaddr_t mask, addr;
21327 
21328 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21329 
21330 	addr = bireinfop->bi_addr;
21331 	irep = ire_create_bcast(ipif, addr, irep);
21332 
21333 	switch (bireinfop->bi_type) {
21334 	case BCAST_NET:
21335 		mask = ip_net_mask(ipif->ipif_subnet);
21336 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21337 		break;
21338 	case BCAST_SUBNET:
21339 		mask = ipif->ipif_net_mask;
21340 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21341 		break;
21342 	}
21343 
21344 	bireinfop->bi_haverep = 1;
21345 	return (irep);
21346 }
21347 
21348 /*
21349  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21350  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21351  * that are going away are still needed.  If so, have ipif_create_bcast()
21352  * recreate them (except for the deprecated case, as explained below).
21353  */
21354 static ire_t **
21355 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21356     ire_t **irep)
21357 {
21358 	int i;
21359 	ipif_t *ipif;
21360 
21361 	ASSERT(!ill->ill_isv6);
21362 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21363 		/*
21364 		 * Skip this ipif if it's (a) the one being taken down, (b)
21365 		 * not in the same zone, or (c) has no valid local address.
21366 		 */
21367 		if (ipif == test_ipif ||
21368 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21369 		    ipif->ipif_subnet == 0 ||
21370 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21371 		    (IPIF_UP|IPIF_BROADCAST))
21372 			continue;
21373 
21374 		/*
21375 		 * For each dying IRE that hasn't yet been replaced, see if
21376 		 * `ipif' needs it and whether the IRE should be recreated on
21377 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21378 		 * will return B_FALSE even if `ipif' needs the IRE on the
21379 		 * hopes that we'll later find a needy non-deprecated ipif.
21380 		 * However, the ipif is recorded in bi_backup for possible
21381 		 * subsequent use by ipif_check_bcast_ires().
21382 		 */
21383 		for (i = 0; i < BCAST_COUNT; i++) {
21384 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21385 				continue;
21386 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21387 				continue;
21388 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21389 		}
21390 
21391 		/*
21392 		 * If we've replaced all of the broadcast IREs that are going
21393 		 * to be taken down, we know we're done.
21394 		 */
21395 		for (i = 0; i < BCAST_COUNT; i++) {
21396 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21397 				break;
21398 		}
21399 		if (i == BCAST_COUNT)
21400 			break;
21401 	}
21402 	return (irep);
21403 }
21404 
21405 /*
21406  * Check if `test_ipif' (which is going away) is associated with any existing
21407  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21408  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21409  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21410  *
21411  * This is necessary because broadcast IREs are shared.  In particular, a
21412  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21413  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21414  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21415  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21416  * same zone, they will share the same set of broadcast IREs.
21417  *
21418  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21419  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21420  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21421  */
21422 static void
21423 ipif_check_bcast_ires(ipif_t *test_ipif)
21424 {
21425 	ill_t		*ill = test_ipif->ipif_ill;
21426 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21427 	ire_t		**irep1, **irep = &ire_array[0];
21428 	uint_t 		i, willdie;
21429 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21430 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21431 
21432 	ASSERT(!test_ipif->ipif_isv6);
21433 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21434 
21435 	/*
21436 	 * No broadcast IREs for the LOOPBACK interface
21437 	 * or others such as point to point and IPIF_NOXMIT.
21438 	 */
21439 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21440 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21441 		return;
21442 
21443 	bzero(bireinfo, sizeof (bireinfo));
21444 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21445 	bireinfo[0].bi_addr = 0;
21446 
21447 	bireinfo[1].bi_type = BCAST_ALLONES;
21448 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21449 
21450 	bireinfo[2].bi_type = BCAST_NET;
21451 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21452 
21453 	if (test_ipif->ipif_net_mask != 0)
21454 		mask = test_ipif->ipif_net_mask;
21455 	bireinfo[3].bi_type = BCAST_SUBNET;
21456 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21457 
21458 	/*
21459 	 * Figure out what (if any) broadcast IREs will die as a result of
21460 	 * `test_ipif' going away.  If none will die, we're done.
21461 	 */
21462 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21463 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21464 		    test_ipif, ALL_ZONES, NULL,
21465 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21466 		if (ire != NULL) {
21467 			willdie++;
21468 			bireinfo[i].bi_willdie = 1;
21469 			ire_refrele(ire);
21470 		}
21471 	}
21472 
21473 	if (willdie == 0)
21474 		return;
21475 
21476 	/*
21477 	 * Walk through all the ipifs that will be affected by the dying IREs,
21478 	 * and recreate the IREs as necessary.
21479 	 */
21480 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21481 
21482 	/*
21483 	 * Scan through the set of broadcast IREs and see if there are any
21484 	 * that we need to replace that have not yet been replaced.  If so,
21485 	 * replace them using the appropriate backup ipif.
21486 	 */
21487 	for (i = 0; i < BCAST_COUNT; i++) {
21488 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21489 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21490 			    &bireinfo[i], irep);
21491 	}
21492 
21493 	/*
21494 	 * If we can't create all of them, don't add any of them.  (Code in
21495 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21496 	 * non-loopback copy and loopback copy for a given address.)
21497 	 */
21498 	for (irep1 = irep; irep1 > ire_array; ) {
21499 		irep1--;
21500 		if (*irep1 == NULL) {
21501 			ip0dbg(("ipif_check_bcast_ires: can't create "
21502 			    "IRE_BROADCAST, memory allocation failure\n"));
21503 			while (irep > ire_array) {
21504 				irep--;
21505 				if (*irep != NULL)
21506 					ire_delete(*irep);
21507 			}
21508 			return;
21509 		}
21510 	}
21511 
21512 	for (irep1 = irep; irep1 > ire_array; ) {
21513 		irep1--;
21514 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21515 			ire_refrele(*irep1);		/* Held in ire_add */
21516 	}
21517 }
21518 
21519 /*
21520  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21521  * from lifr_flags and the name from lifr_name.
21522  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21523  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21524  * Returns EINPROGRESS when mp has been consumed by queueing it on
21525  * ill_pending_mp and the ioctl will complete in ip_rput.
21526  *
21527  * Can operate on either a module or a driver queue.
21528  * Returns an error if not a module queue.
21529  */
21530 /* ARGSUSED */
21531 int
21532 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21533     ip_ioctl_cmd_t *ipip, void *if_req)
21534 {
21535 	ill_t	*ill = q->q_ptr;
21536 	phyint_t *phyi;
21537 	ip_stack_t *ipst;
21538 	struct lifreq *lifr = if_req;
21539 
21540 	ASSERT(ipif != NULL);
21541 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21542 
21543 	if (q->q_next == NULL) {
21544 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21545 		return (EINVAL);
21546 	}
21547 
21548 	/*
21549 	 * If we are not writer on 'q' then this interface exists already
21550 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
21551 	 * so return EALREADY.
21552 	 */
21553 	if (ill != ipif->ipif_ill)
21554 		return (EALREADY);
21555 
21556 	if (ill->ill_name[0] != '\0')
21557 		return (EALREADY);
21558 
21559 	/*
21560 	 * Set all the flags. Allows all kinds of override. Provide some
21561 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21562 	 * unless there is either multicast/broadcast support in the driver
21563 	 * or it is a pt-pt link.
21564 	 */
21565 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21566 		/* Meaningless to IP thus don't allow them to be set. */
21567 		ip1dbg(("ip_setname: EINVAL 1\n"));
21568 		return (EINVAL);
21569 	}
21570 
21571 	/*
21572 	 * If there's another ill already with the requested name, ensure
21573 	 * that it's of the same type.	Otherwise, ill_phyint_reinit() will
21574 	 * fuse together two unrelated ills, which will cause chaos.
21575 	 */
21576 	ipst = ill->ill_ipst;
21577 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
21578 	    lifr->lifr_name, NULL);
21579 	if (phyi != NULL) {
21580 		ill_t *ill_mate = phyi->phyint_illv4;
21581 
21582 		if (ill_mate == NULL)
21583 			ill_mate = phyi->phyint_illv6;
21584 		ASSERT(ill_mate != NULL);
21585 
21586 		if (ill_mate->ill_media->ip_m_mac_type !=
21587 		    ill->ill_media->ip_m_mac_type) {
21588 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
21589 			    "use the same ill name on differing media\n"));
21590 			return (EINVAL);
21591 		}
21592 	}
21593 
21594 	/*
21595 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21596 	 * ill_bcast_addr_length info.
21597 	 */
21598 	if (!ill->ill_needs_attach &&
21599 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21600 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21601 	    ill->ill_bcast_addr_length == 0)) {
21602 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21603 		ip1dbg(("ip_setname: EINVAL 2\n"));
21604 		return (EINVAL);
21605 	}
21606 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21607 	    ((lifr->lifr_flags & IFF_IPV6) ||
21608 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21609 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21610 		ip1dbg(("ip_setname: EINVAL 3\n"));
21611 		return (EINVAL);
21612 	}
21613 	if (lifr->lifr_flags & IFF_UP) {
21614 		/* Can only be set with SIOCSLIFFLAGS */
21615 		ip1dbg(("ip_setname: EINVAL 4\n"));
21616 		return (EINVAL);
21617 	}
21618 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21619 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21620 		ip1dbg(("ip_setname: EINVAL 5\n"));
21621 		return (EINVAL);
21622 	}
21623 	/*
21624 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21625 	 */
21626 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21627 	    !(lifr->lifr_flags & IFF_IPV6) &&
21628 	    !(ipif->ipif_isv6)) {
21629 		ip1dbg(("ip_setname: EINVAL 6\n"));
21630 		return (EINVAL);
21631 	}
21632 
21633 	/*
21634 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21635 	 * we have all the flags here. So, we assign rather than we OR.
21636 	 * We can't OR the flags here because we don't want to set
21637 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21638 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21639 	 * on lifr_flags value here.
21640 	 */
21641 	/*
21642 	 * This ill has not been inserted into the global list.
21643 	 * So we are still single threaded and don't need any lock
21644 	 */
21645 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
21646 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21647 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21648 
21649 	/* We started off as V4. */
21650 	if (ill->ill_flags & ILLF_IPV6) {
21651 		ill->ill_phyint->phyint_illv6 = ill;
21652 		ill->ill_phyint->phyint_illv4 = NULL;
21653 	}
21654 
21655 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
21656 }
21657 
21658 /* ARGSUSED */
21659 int
21660 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21661     ip_ioctl_cmd_t *ipip, void *if_req)
21662 {
21663 	/*
21664 	 * ill_phyint_reinit merged the v4 and v6 into a single
21665 	 * ipsq. Could also have become part of a ipmp group in the
21666 	 * process, and we might not have been able to complete the
21667 	 * slifname in ipif_set_values, if we could not become
21668 	 * exclusive.  If so restart it here
21669 	 */
21670 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21671 }
21672 
21673 /*
21674  * Return a pointer to the ipif which matches the index, IP version type and
21675  * zoneid.
21676  */
21677 ipif_t *
21678 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21679     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21680 {
21681 	ill_t	*ill;
21682 	ipif_t	*ipif = NULL;
21683 
21684 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21685 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21686 
21687 	if (err != NULL)
21688 		*err = 0;
21689 
21690 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
21691 	if (ill != NULL) {
21692 		mutex_enter(&ill->ill_lock);
21693 		for (ipif = ill->ill_ipif; ipif != NULL;
21694 		    ipif = ipif->ipif_next) {
21695 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
21696 			    zoneid == ipif->ipif_zoneid ||
21697 			    ipif->ipif_zoneid == ALL_ZONES)) {
21698 				ipif_refhold_locked(ipif);
21699 				break;
21700 			}
21701 		}
21702 		mutex_exit(&ill->ill_lock);
21703 		ill_refrele(ill);
21704 		if (ipif == NULL && err != NULL)
21705 			*err = ENXIO;
21706 	}
21707 	return (ipif);
21708 }
21709 
21710 typedef struct conn_change_s {
21711 	uint_t cc_old_ifindex;
21712 	uint_t cc_new_ifindex;
21713 } conn_change_t;
21714 
21715 /*
21716  * ipcl_walk function for changing interface index.
21717  */
21718 static void
21719 conn_change_ifindex(conn_t *connp, caddr_t arg)
21720 {
21721 	conn_change_t *connc;
21722 	uint_t old_ifindex;
21723 	uint_t new_ifindex;
21724 	int i;
21725 	ilg_t *ilg;
21726 
21727 	connc = (conn_change_t *)arg;
21728 	old_ifindex = connc->cc_old_ifindex;
21729 	new_ifindex = connc->cc_new_ifindex;
21730 
21731 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21732 		connp->conn_orig_bound_ifindex = new_ifindex;
21733 
21734 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21735 		connp->conn_orig_multicast_ifindex = new_ifindex;
21736 
21737 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21738 		ilg = &connp->conn_ilg[i];
21739 		if (ilg->ilg_orig_ifindex == old_ifindex)
21740 			ilg->ilg_orig_ifindex = new_ifindex;
21741 	}
21742 }
21743 
21744 /*
21745  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21746  * to new_index if it matches the old_index.
21747  *
21748  * Failovers typically happen within a group of ills. But somebody
21749  * can remove an ill from the group after a failover happened. If
21750  * we are setting the ifindex after this, we potentially need to
21751  * look at all the ills rather than just the ones in the group.
21752  * We cut down the work by looking at matching ill_net_types
21753  * and ill_types as we could not possibly grouped them together.
21754  */
21755 static void
21756 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21757 {
21758 	ill_t *ill;
21759 	ipif_t *ipif;
21760 	uint_t old_ifindex;
21761 	uint_t new_ifindex;
21762 	ilm_t *ilm;
21763 	ill_walk_context_t ctx;
21764 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21765 
21766 	old_ifindex = connc->cc_old_ifindex;
21767 	new_ifindex = connc->cc_new_ifindex;
21768 
21769 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21770 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21771 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21772 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21773 		    (ill_orig->ill_type != ill->ill_type)) {
21774 			continue;
21775 		}
21776 		for (ipif = ill->ill_ipif; ipif != NULL;
21777 		    ipif = ipif->ipif_next) {
21778 			if (ipif->ipif_orig_ifindex == old_ifindex)
21779 				ipif->ipif_orig_ifindex = new_ifindex;
21780 		}
21781 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21782 			if (ilm->ilm_orig_ifindex == old_ifindex)
21783 				ilm->ilm_orig_ifindex = new_ifindex;
21784 		}
21785 	}
21786 	rw_exit(&ipst->ips_ill_g_lock);
21787 }
21788 
21789 /*
21790  * We first need to ensure that the new index is unique, and
21791  * then carry the change across both v4 and v6 ill representation
21792  * of the physical interface.
21793  */
21794 /* ARGSUSED */
21795 int
21796 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21797     ip_ioctl_cmd_t *ipip, void *ifreq)
21798 {
21799 	ill_t		*ill;
21800 	ill_t		*ill_other;
21801 	phyint_t	*phyi;
21802 	int		old_index;
21803 	conn_change_t	connc;
21804 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21805 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21806 	uint_t	index;
21807 	ill_t	*ill_v4;
21808 	ill_t	*ill_v6;
21809 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21810 
21811 	if (ipip->ipi_cmd_type == IF_CMD)
21812 		index = ifr->ifr_index;
21813 	else
21814 		index = lifr->lifr_index;
21815 
21816 	/*
21817 	 * Only allow on physical interface. Also, index zero is illegal.
21818 	 *
21819 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21820 	 *
21821 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21822 	 *    implies a possible failback might have to happen. As failback
21823 	 *    depends on the old index, we should fail setting the index.
21824 	 *
21825 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21826 	 *    any addresses or multicast memberships are failed over to
21827 	 *    a non-STANDBY interface. As failback depends on the old
21828 	 *    index, we should fail setting the index for this case also.
21829 	 *
21830 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21831 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21832 	 */
21833 	ill = ipif->ipif_ill;
21834 	phyi = ill->ill_phyint;
21835 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21836 	    ipif->ipif_id != 0 || index == 0) {
21837 		return (EINVAL);
21838 	}
21839 	old_index = phyi->phyint_ifindex;
21840 
21841 	/* If the index is not changing, no work to do */
21842 	if (old_index == index)
21843 		return (0);
21844 
21845 	/*
21846 	 * Use ill_lookup_on_ifindex to determine if the
21847 	 * new index is unused and if so allow the change.
21848 	 */
21849 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21850 	    ipst);
21851 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21852 	    ipst);
21853 	if (ill_v6 != NULL || ill_v4 != NULL) {
21854 		if (ill_v4 != NULL)
21855 			ill_refrele(ill_v4);
21856 		if (ill_v6 != NULL)
21857 			ill_refrele(ill_v6);
21858 		return (EBUSY);
21859 	}
21860 
21861 	/*
21862 	 * The new index is unused. Set it in the phyint.
21863 	 * Locate the other ill so that we can send a routing
21864 	 * sockets message.
21865 	 */
21866 	if (ill->ill_isv6) {
21867 		ill_other = phyi->phyint_illv4;
21868 	} else {
21869 		ill_other = phyi->phyint_illv6;
21870 	}
21871 
21872 	phyi->phyint_ifindex = index;
21873 
21874 	/* Update SCTP's ILL list */
21875 	sctp_ill_reindex(ill, old_index);
21876 
21877 	connc.cc_old_ifindex = old_index;
21878 	connc.cc_new_ifindex = index;
21879 	ip_change_ifindex(ill, &connc);
21880 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21881 
21882 	/* Send the routing sockets message */
21883 	ip_rts_ifmsg(ipif);
21884 	if (ill_other != NULL)
21885 		ip_rts_ifmsg(ill_other->ill_ipif);
21886 
21887 	return (0);
21888 }
21889 
21890 /* ARGSUSED */
21891 int
21892 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21893     ip_ioctl_cmd_t *ipip, void *ifreq)
21894 {
21895 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21896 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21897 
21898 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21899 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21900 	/* Get the interface index */
21901 	if (ipip->ipi_cmd_type == IF_CMD) {
21902 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21903 	} else {
21904 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21905 	}
21906 	return (0);
21907 }
21908 
21909 /* ARGSUSED */
21910 int
21911 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21912     ip_ioctl_cmd_t *ipip, void *ifreq)
21913 {
21914 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21915 
21916 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21917 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21918 	/* Get the interface zone */
21919 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21920 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21921 	return (0);
21922 }
21923 
21924 /*
21925  * Set the zoneid of an interface.
21926  */
21927 /* ARGSUSED */
21928 int
21929 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21930     ip_ioctl_cmd_t *ipip, void *ifreq)
21931 {
21932 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21933 	int err = 0;
21934 	boolean_t need_up = B_FALSE;
21935 	zone_t *zptr;
21936 	zone_status_t status;
21937 	zoneid_t zoneid;
21938 
21939 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21940 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21941 		if (!is_system_labeled())
21942 			return (ENOTSUP);
21943 		zoneid = GLOBAL_ZONEID;
21944 	}
21945 
21946 	/* cannot assign instance zero to a non-global zone */
21947 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
21948 		return (ENOTSUP);
21949 
21950 	/*
21951 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21952 	 * the event of a race with the zone shutdown processing, since IP
21953 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
21954 	 * interface will be cleaned up even if the zone is shut down
21955 	 * immediately after the status check. If the interface can't be brought
21956 	 * down right away, and the zone is shut down before the restart
21957 	 * function is called, we resolve the possible races by rechecking the
21958 	 * zone status in the restart function.
21959 	 */
21960 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
21961 		return (EINVAL);
21962 	status = zone_status_get(zptr);
21963 	zone_rele(zptr);
21964 
21965 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
21966 		return (EINVAL);
21967 
21968 	if (ipif->ipif_flags & IPIF_UP) {
21969 		/*
21970 		 * If the interface is already marked up,
21971 		 * we call ipif_down which will take care
21972 		 * of ditching any IREs that have been set
21973 		 * up based on the old interface address.
21974 		 */
21975 		err = ipif_logical_down(ipif, q, mp);
21976 		if (err == EINPROGRESS)
21977 			return (err);
21978 		ipif_down_tail(ipif);
21979 		need_up = B_TRUE;
21980 	}
21981 
21982 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
21983 	return (err);
21984 }
21985 
21986 static int
21987 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
21988     queue_t *q, mblk_t *mp, boolean_t need_up)
21989 {
21990 	int	err = 0;
21991 	ip_stack_t	*ipst;
21992 
21993 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
21994 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21995 
21996 	if (CONN_Q(q))
21997 		ipst = CONNQ_TO_IPST(q);
21998 	else
21999 		ipst = ILLQ_TO_IPST(q);
22000 
22001 	/*
22002 	 * For exclusive stacks we don't allow a different zoneid than
22003 	 * global.
22004 	 */
22005 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22006 	    zoneid != GLOBAL_ZONEID)
22007 		return (EINVAL);
22008 
22009 	/* Set the new zone id. */
22010 	ipif->ipif_zoneid = zoneid;
22011 
22012 	/* Update sctp list */
22013 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22014 
22015 	if (need_up) {
22016 		/*
22017 		 * Now bring the interface back up.  If this
22018 		 * is the only IPIF for the ILL, ipif_up
22019 		 * will have to re-bind to the device, so
22020 		 * we may get back EINPROGRESS, in which
22021 		 * case, this IOCTL will get completed in
22022 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22023 		 */
22024 		err = ipif_up(ipif, q, mp);
22025 	}
22026 	return (err);
22027 }
22028 
22029 /* ARGSUSED */
22030 int
22031 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22032     ip_ioctl_cmd_t *ipip, void *if_req)
22033 {
22034 	struct lifreq *lifr = (struct lifreq *)if_req;
22035 	zoneid_t zoneid;
22036 	zone_t *zptr;
22037 	zone_status_t status;
22038 
22039 	ASSERT(ipif->ipif_id != 0);
22040 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22041 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22042 		zoneid = GLOBAL_ZONEID;
22043 
22044 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22045 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22046 
22047 	/*
22048 	 * We recheck the zone status to resolve the following race condition:
22049 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22050 	 * 2) hme0:1 is up and can't be brought down right away;
22051 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22052 	 * 3) zone "myzone" is halted; the zone status switches to
22053 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22054 	 * the interfaces to remove - hme0:1 is not returned because it's not
22055 	 * yet in "myzone", so it won't be removed;
22056 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22057 	 * status check here, we would have hme0:1 in "myzone" after it's been
22058 	 * destroyed.
22059 	 * Note that if the status check fails, we need to bring the interface
22060 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22061 	 * ipif_up_done[_v6]().
22062 	 */
22063 	status = ZONE_IS_UNINITIALIZED;
22064 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22065 		status = zone_status_get(zptr);
22066 		zone_rele(zptr);
22067 	}
22068 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22069 		if (ipif->ipif_isv6) {
22070 			(void) ipif_up_done_v6(ipif);
22071 		} else {
22072 			(void) ipif_up_done(ipif);
22073 		}
22074 		return (EINVAL);
22075 	}
22076 
22077 	ipif_down_tail(ipif);
22078 
22079 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22080 	    B_TRUE));
22081 }
22082 
22083 /* ARGSUSED */
22084 int
22085 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22086 	ip_ioctl_cmd_t *ipip, void *ifreq)
22087 {
22088 	struct lifreq	*lifr = ifreq;
22089 
22090 	ASSERT(q->q_next == NULL);
22091 	ASSERT(CONN_Q(q));
22092 
22093 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22094 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22095 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22096 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22097 
22098 	return (0);
22099 }
22100 
22101 /* Find the previous ILL in this usesrc group */
22102 static ill_t *
22103 ill_prev_usesrc(ill_t *uill)
22104 {
22105 	ill_t *ill;
22106 
22107 	for (ill = uill->ill_usesrc_grp_next;
22108 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22109 	    ill = ill->ill_usesrc_grp_next)
22110 		/* do nothing */;
22111 	return (ill);
22112 }
22113 
22114 /*
22115  * Release all members of the usesrc group. This routine is called
22116  * from ill_delete when the interface being unplumbed is the
22117  * group head.
22118  */
22119 static void
22120 ill_disband_usesrc_group(ill_t *uill)
22121 {
22122 	ill_t *next_ill, *tmp_ill;
22123 	ip_stack_t	*ipst = uill->ill_ipst;
22124 
22125 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22126 	next_ill = uill->ill_usesrc_grp_next;
22127 
22128 	do {
22129 		ASSERT(next_ill != NULL);
22130 		tmp_ill = next_ill->ill_usesrc_grp_next;
22131 		ASSERT(tmp_ill != NULL);
22132 		next_ill->ill_usesrc_grp_next = NULL;
22133 		next_ill->ill_usesrc_ifindex = 0;
22134 		next_ill = tmp_ill;
22135 	} while (next_ill->ill_usesrc_ifindex != 0);
22136 	uill->ill_usesrc_grp_next = NULL;
22137 }
22138 
22139 /*
22140  * Remove the client usesrc ILL from the list and relink to a new list
22141  */
22142 int
22143 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22144 {
22145 	ill_t *ill, *tmp_ill;
22146 	ip_stack_t	*ipst = ucill->ill_ipst;
22147 
22148 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22149 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22150 
22151 	/*
22152 	 * Check if the usesrc client ILL passed in is not already
22153 	 * in use as a usesrc ILL i.e one whose source address is
22154 	 * in use OR a usesrc ILL is not already in use as a usesrc
22155 	 * client ILL
22156 	 */
22157 	if ((ucill->ill_usesrc_ifindex == 0) ||
22158 	    (uill->ill_usesrc_ifindex != 0)) {
22159 		return (-1);
22160 	}
22161 
22162 	ill = ill_prev_usesrc(ucill);
22163 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22164 
22165 	/* Remove from the current list */
22166 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22167 		/* Only two elements in the list */
22168 		ASSERT(ill->ill_usesrc_ifindex == 0);
22169 		ill->ill_usesrc_grp_next = NULL;
22170 	} else {
22171 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22172 	}
22173 
22174 	if (ifindex == 0) {
22175 		ucill->ill_usesrc_ifindex = 0;
22176 		ucill->ill_usesrc_grp_next = NULL;
22177 		return (0);
22178 	}
22179 
22180 	ucill->ill_usesrc_ifindex = ifindex;
22181 	tmp_ill = uill->ill_usesrc_grp_next;
22182 	uill->ill_usesrc_grp_next = ucill;
22183 	ucill->ill_usesrc_grp_next =
22184 	    (tmp_ill != NULL) ? tmp_ill : uill;
22185 	return (0);
22186 }
22187 
22188 /*
22189  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22190  * ip.c for locking details.
22191  */
22192 /* ARGSUSED */
22193 int
22194 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22195     ip_ioctl_cmd_t *ipip, void *ifreq)
22196 {
22197 	struct lifreq *lifr = (struct lifreq *)ifreq;
22198 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22199 	    ill_flag_changed = B_FALSE;
22200 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22201 	int err = 0, ret;
22202 	uint_t ifindex;
22203 	phyint_t *us_phyint, *us_cli_phyint;
22204 	ipsq_t *ipsq = NULL;
22205 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22206 
22207 	ASSERT(IAM_WRITER_IPIF(ipif));
22208 	ASSERT(q->q_next == NULL);
22209 	ASSERT(CONN_Q(q));
22210 
22211 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22212 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22213 
22214 	ASSERT(us_cli_phyint != NULL);
22215 
22216 	/*
22217 	 * If the client ILL is being used for IPMP, abort.
22218 	 * Note, this can be done before ipsq_try_enter since we are already
22219 	 * exclusive on this ILL
22220 	 */
22221 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22222 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22223 		return (EINVAL);
22224 	}
22225 
22226 	ifindex = lifr->lifr_index;
22227 	if (ifindex == 0) {
22228 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22229 			/* non usesrc group interface, nothing to reset */
22230 			return (0);
22231 		}
22232 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22233 		/* valid reset request */
22234 		reset_flg = B_TRUE;
22235 	}
22236 
22237 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22238 	    ip_process_ioctl, &err, ipst);
22239 
22240 	if (usesrc_ill == NULL) {
22241 		return (err);
22242 	}
22243 
22244 	/*
22245 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22246 	 * group nor can either of the interfaces be used for standy. So
22247 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22248 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22249 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22250 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22251 	 * the usesrc_cli_ill
22252 	 */
22253 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22254 	    NEW_OP, B_TRUE);
22255 	if (ipsq == NULL) {
22256 		err = EINPROGRESS;
22257 		/* Operation enqueued on the ipsq of the usesrc ILL */
22258 		goto done;
22259 	}
22260 
22261 	/* Check if the usesrc_ill is used for IPMP */
22262 	us_phyint = usesrc_ill->ill_phyint;
22263 	if ((us_phyint->phyint_groupname != NULL) ||
22264 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22265 		err = EINVAL;
22266 		goto done;
22267 	}
22268 
22269 	/*
22270 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22271 	 * already a client then return EINVAL
22272 	 */
22273 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22274 		err = EINVAL;
22275 		goto done;
22276 	}
22277 
22278 	/*
22279 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22280 	 * be then this is a duplicate operation.
22281 	 */
22282 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22283 		err = 0;
22284 		goto done;
22285 	}
22286 
22287 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22288 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22289 	    usesrc_ill->ill_isv6));
22290 
22291 	/*
22292 	 * The next step ensures that no new ires will be created referencing
22293 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22294 	 * we go through an ire walk deleting all ire caches that reference
22295 	 * the client ill. New ires referencing the client ill that are added
22296 	 * to the ire table before the ILL_CHANGING flag is set, will be
22297 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22298 	 * the client ill while the ILL_CHANGING flag is set will be failed
22299 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22300 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22301 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22302 	 * belong to the same usesrc group.
22303 	 */
22304 	mutex_enter(&usesrc_cli_ill->ill_lock);
22305 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22306 	mutex_exit(&usesrc_cli_ill->ill_lock);
22307 	ill_flag_changed = B_TRUE;
22308 
22309 	if (ipif->ipif_isv6)
22310 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22311 		    ALL_ZONES, ipst);
22312 	else
22313 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22314 		    ALL_ZONES, ipst);
22315 
22316 	/*
22317 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22318 	 * and the ill_usesrc_ifindex fields
22319 	 */
22320 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22321 
22322 	if (reset_flg) {
22323 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22324 		if (ret != 0) {
22325 			err = EINVAL;
22326 		}
22327 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22328 		goto done;
22329 	}
22330 
22331 	/*
22332 	 * Four possibilities to consider:
22333 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22334 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22335 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22336 	 * 4. Both are part of their respective usesrc groups
22337 	 */
22338 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22339 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22340 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22341 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22342 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22343 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22344 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22345 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22346 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22347 		/* Insert at head of list */
22348 		usesrc_cli_ill->ill_usesrc_grp_next =
22349 		    usesrc_ill->ill_usesrc_grp_next;
22350 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22351 	} else {
22352 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22353 		    ifindex);
22354 		if (ret != 0)
22355 			err = EINVAL;
22356 	}
22357 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22358 
22359 done:
22360 	if (ill_flag_changed) {
22361 		mutex_enter(&usesrc_cli_ill->ill_lock);
22362 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22363 		mutex_exit(&usesrc_cli_ill->ill_lock);
22364 	}
22365 	if (ipsq != NULL)
22366 		ipsq_exit(ipsq);
22367 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22368 	ill_refrele(usesrc_ill);
22369 	return (err);
22370 }
22371 
22372 /*
22373  * comparison function used by avl.
22374  */
22375 static int
22376 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22377 {
22378 
22379 	uint_t index;
22380 
22381 	ASSERT(phyip != NULL && index_ptr != NULL);
22382 
22383 	index = *((uint_t *)index_ptr);
22384 	/*
22385 	 * let the phyint with the lowest index be on top.
22386 	 */
22387 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22388 		return (1);
22389 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22390 		return (-1);
22391 	return (0);
22392 }
22393 
22394 /*
22395  * comparison function used by avl.
22396  */
22397 static int
22398 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22399 {
22400 	ill_t *ill;
22401 	int res = 0;
22402 
22403 	ASSERT(phyip != NULL && name_ptr != NULL);
22404 
22405 	if (((phyint_t *)phyip)->phyint_illv4)
22406 		ill = ((phyint_t *)phyip)->phyint_illv4;
22407 	else
22408 		ill = ((phyint_t *)phyip)->phyint_illv6;
22409 	ASSERT(ill != NULL);
22410 
22411 	res = strcmp(ill->ill_name, (char *)name_ptr);
22412 	if (res > 0)
22413 		return (1);
22414 	else if (res < 0)
22415 		return (-1);
22416 	return (0);
22417 }
22418 /*
22419  * This function is called from ill_delete when the ill is being
22420  * unplumbed. We remove the reference from the phyint and we also
22421  * free the phyint when there are no more references to it.
22422  */
22423 static void
22424 ill_phyint_free(ill_t *ill)
22425 {
22426 	phyint_t *phyi;
22427 	phyint_t *next_phyint;
22428 	ipsq_t *cur_ipsq;
22429 	ip_stack_t	*ipst = ill->ill_ipst;
22430 
22431 	ASSERT(ill->ill_phyint != NULL);
22432 
22433 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22434 	phyi = ill->ill_phyint;
22435 	ill->ill_phyint = NULL;
22436 	/*
22437 	 * ill_init allocates a phyint always to store the copy
22438 	 * of flags relevant to phyint. At that point in time, we could
22439 	 * not assign the name and hence phyint_illv4/v6 could not be
22440 	 * initialized. Later in ipif_set_values, we assign the name to
22441 	 * the ill, at which point in time we assign phyint_illv4/v6.
22442 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22443 	 */
22444 	if (ill->ill_flags & ILLF_IPV6) {
22445 		phyi->phyint_illv6 = NULL;
22446 	} else {
22447 		phyi->phyint_illv4 = NULL;
22448 	}
22449 	/*
22450 	 * ipif_down removes it from the group when the last ipif goes
22451 	 * down.
22452 	 */
22453 	ASSERT(ill->ill_group == NULL);
22454 
22455 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22456 		return;
22457 
22458 	/*
22459 	 * Make sure this phyint was put in the list.
22460 	 */
22461 	if (phyi->phyint_ifindex > 0) {
22462 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22463 		    phyi);
22464 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22465 		    phyi);
22466 	}
22467 	/*
22468 	 * remove phyint from the ipsq list.
22469 	 */
22470 	cur_ipsq = phyi->phyint_ipsq;
22471 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22472 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22473 	} else {
22474 		next_phyint = cur_ipsq->ipsq_phyint_list;
22475 		while (next_phyint != NULL) {
22476 			if (next_phyint->phyint_ipsq_next == phyi) {
22477 				next_phyint->phyint_ipsq_next =
22478 				    phyi->phyint_ipsq_next;
22479 				break;
22480 			}
22481 			next_phyint = next_phyint->phyint_ipsq_next;
22482 		}
22483 		ASSERT(next_phyint != NULL);
22484 	}
22485 	IPSQ_DEC_REF(cur_ipsq, ipst);
22486 
22487 	if (phyi->phyint_groupname_len != 0) {
22488 		ASSERT(phyi->phyint_groupname != NULL);
22489 		mi_free(phyi->phyint_groupname);
22490 	}
22491 	mi_free(phyi);
22492 }
22493 
22494 /*
22495  * Attach the ill to the phyint structure which can be shared by both
22496  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22497  * function is called from ipif_set_values and ill_lookup_on_name (for
22498  * loopback) where we know the name of the ill. We lookup the ill and if
22499  * there is one present already with the name use that phyint. Otherwise
22500  * reuse the one allocated by ill_init.
22501  */
22502 static void
22503 ill_phyint_reinit(ill_t *ill)
22504 {
22505 	boolean_t isv6 = ill->ill_isv6;
22506 	phyint_t *phyi_old;
22507 	phyint_t *phyi;
22508 	avl_index_t where = 0;
22509 	ill_t	*ill_other = NULL;
22510 	ipsq_t	*ipsq;
22511 	ip_stack_t	*ipst = ill->ill_ipst;
22512 
22513 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22514 
22515 	phyi_old = ill->ill_phyint;
22516 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22517 	    phyi_old->phyint_illv6 == NULL));
22518 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22519 	    phyi_old->phyint_illv4 == NULL));
22520 	ASSERT(phyi_old->phyint_ifindex == 0);
22521 
22522 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22523 	    ill->ill_name, &where);
22524 
22525 	/*
22526 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22527 	 *    the global list of ills. So no other thread could have located
22528 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22529 	 * 2. Now locate the other protocol instance of this ill.
22530 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22531 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22532 	 *    of neither ill can change.
22533 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22534 	 *    other ill.
22535 	 * 5. Release all locks.
22536 	 */
22537 
22538 	/*
22539 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22540 	 * we are initializing IPv4.
22541 	 */
22542 	if (phyi != NULL) {
22543 		ill_other = (isv6) ? phyi->phyint_illv4 :
22544 		    phyi->phyint_illv6;
22545 		ASSERT(ill_other->ill_phyint != NULL);
22546 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22547 		    (!isv6 && ill_other->ill_isv6));
22548 		GRAB_ILL_LOCKS(ill, ill_other);
22549 		/*
22550 		 * We are potentially throwing away phyint_flags which
22551 		 * could be different from the one that we obtain from
22552 		 * ill_other->ill_phyint. But it is okay as we are assuming
22553 		 * that the state maintained within IP is correct.
22554 		 */
22555 		mutex_enter(&phyi->phyint_lock);
22556 		if (isv6) {
22557 			ASSERT(phyi->phyint_illv6 == NULL);
22558 			phyi->phyint_illv6 = ill;
22559 		} else {
22560 			ASSERT(phyi->phyint_illv4 == NULL);
22561 			phyi->phyint_illv4 = ill;
22562 		}
22563 		/*
22564 		 * This is a new ill, currently undergoing SLIFNAME
22565 		 * So we could not have joined an IPMP group until now.
22566 		 */
22567 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22568 		    phyi_old->phyint_groupname == NULL);
22569 
22570 		/*
22571 		 * This phyi_old is going away. Decref ipsq_refs and
22572 		 * assert it is zero. The ipsq itself will be freed in
22573 		 * ipsq_exit
22574 		 */
22575 		ipsq = phyi_old->phyint_ipsq;
22576 		IPSQ_DEC_REF(ipsq, ipst);
22577 		ASSERT(ipsq->ipsq_refs == 0);
22578 		/* Get the singleton phyint out of the ipsq list */
22579 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22580 		ipsq->ipsq_phyint_list = NULL;
22581 		phyi_old->phyint_illv4 = NULL;
22582 		phyi_old->phyint_illv6 = NULL;
22583 		mi_free(phyi_old);
22584 	} else {
22585 		mutex_enter(&ill->ill_lock);
22586 		/*
22587 		 * We don't need to acquire any lock, since
22588 		 * the ill is not yet visible globally  and we
22589 		 * have not yet released the ill_g_lock.
22590 		 */
22591 		phyi = phyi_old;
22592 		mutex_enter(&phyi->phyint_lock);
22593 		/* XXX We need a recovery strategy here. */
22594 		if (!phyint_assign_ifindex(phyi, ipst))
22595 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22596 
22597 		/* No IPMP group yet, thus the hook uses the ifindex */
22598 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22599 
22600 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22601 		    (void *)phyi, where);
22602 
22603 		(void) avl_find(&ipst->ips_phyint_g_list->
22604 		    phyint_list_avl_by_index,
22605 		    &phyi->phyint_ifindex, &where);
22606 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22607 		    (void *)phyi, where);
22608 	}
22609 
22610 	/*
22611 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22612 	 * pending mp is not affected because that is per ill basis.
22613 	 */
22614 	ill->ill_phyint = phyi;
22615 
22616 	/*
22617 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22618 	 * We do this here as when the first ipif was allocated,
22619 	 * ipif_allocate does not know the right interface index.
22620 	 */
22621 
22622 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22623 	/*
22624 	 * Now that the phyint's ifindex has been assigned, complete the
22625 	 * remaining
22626 	 */
22627 
22628 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22629 	if (ill->ill_isv6) {
22630 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22631 		    ill->ill_phyint->phyint_ifindex;
22632 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22633 	} else {
22634 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22635 	}
22636 
22637 	/*
22638 	 * Generate an event within the hooks framework to indicate that
22639 	 * a new interface has just been added to IP.  For this event to
22640 	 * be generated, the network interface must, at least, have an
22641 	 * ifindex assigned to it.
22642 	 *
22643 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22644 	 * that the ordering of delivered events to listeners matches the
22645 	 * order of them in the kernel.
22646 	 *
22647 	 * This function could be called from ill_lookup_on_name. In that case
22648 	 * the interface is loopback "lo", which will not generate a NIC event.
22649 	 */
22650 	if (ill->ill_name_length <= 2 ||
22651 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22652 		/*
22653 		 * Generate nic plumb event for ill_name even if
22654 		 * ipmp_hook_emulation is set. That avoids generating events
22655 		 * for the ill_names should ipmp_hook_emulation be turned on
22656 		 * later.
22657 		 */
22658 		ill_nic_info_plumb(ill, B_FALSE);
22659 	}
22660 	RELEASE_ILL_LOCKS(ill, ill_other);
22661 	mutex_exit(&phyi->phyint_lock);
22662 }
22663 
22664 /*
22665  * Allocate a NE_PLUMB nic info event and store in the ill.
22666  * If 'group' is set we do it for the group name, otherwise the ill name.
22667  * It will be sent when we leave the ipsq.
22668  */
22669 void
22670 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22671 {
22672 	phyint_t	*phyi = ill->ill_phyint;
22673 	char		*name;
22674 	int		namelen;
22675 
22676 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22677 
22678 	if (group) {
22679 		ASSERT(phyi->phyint_groupname_len != 0);
22680 		namelen = phyi->phyint_groupname_len;
22681 		name = phyi->phyint_groupname;
22682 	} else {
22683 		namelen = ill->ill_name_length;
22684 		name = ill->ill_name;
22685 	}
22686 
22687 	(void) ill_hook_event_create(ill, 0, NE_PLUMB, name, namelen);
22688 }
22689 
22690 /*
22691  * Unhook the nic event message from the ill and enqueue it
22692  * into the nic event taskq.
22693  */
22694 void
22695 ill_nic_info_dispatch(ill_t *ill)
22696 {
22697 	hook_nic_event_t *info;
22698 
22699 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22700 
22701 	if ((info = ill->ill_nic_event_info) != NULL) {
22702 		if (ddi_taskq_dispatch(eventq_queue_nic,
22703 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22704 			ip2dbg(("ill_nic_info_dispatch: "
22705 			    "ddi_taskq_dispatch failed\n"));
22706 			if (info->hne_data != NULL)
22707 				kmem_free(info->hne_data, info->hne_datalen);
22708 			kmem_free(info, sizeof (hook_nic_event_t));
22709 		}
22710 		ill->ill_nic_event_info = NULL;
22711 	}
22712 }
22713 
22714 /*
22715  * Notify any downstream modules of the name of this interface.
22716  * An M_IOCTL is used even though we don't expect a successful reply.
22717  * Any reply message from the driver (presumably an M_IOCNAK) will
22718  * eventually get discarded somewhere upstream.  The message format is
22719  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22720  * to IP.
22721  */
22722 static void
22723 ip_ifname_notify(ill_t *ill, queue_t *q)
22724 {
22725 	mblk_t *mp1, *mp2;
22726 	struct iocblk *iocp;
22727 	struct lifreq *lifr;
22728 
22729 	mp1 = mkiocb(SIOCSLIFNAME);
22730 	if (mp1 == NULL)
22731 		return;
22732 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22733 	if (mp2 == NULL) {
22734 		freeb(mp1);
22735 		return;
22736 	}
22737 
22738 	mp1->b_cont = mp2;
22739 	iocp = (struct iocblk *)mp1->b_rptr;
22740 	iocp->ioc_count = sizeof (struct lifreq);
22741 
22742 	lifr = (struct lifreq *)mp2->b_rptr;
22743 	mp2->b_wptr += sizeof (struct lifreq);
22744 	bzero(lifr, sizeof (struct lifreq));
22745 
22746 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22747 	lifr->lifr_ppa = ill->ill_ppa;
22748 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22749 
22750 	putnext(q, mp1);
22751 }
22752 
22753 static int
22754 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22755 {
22756 	int err;
22757 	ip_stack_t	*ipst = ill->ill_ipst;
22758 
22759 	/* Set the obsolete NDD per-interface forwarding name. */
22760 	err = ill_set_ndd_name(ill);
22761 	if (err != 0) {
22762 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22763 		    err);
22764 	}
22765 
22766 	/* Tell downstream modules where they are. */
22767 	ip_ifname_notify(ill, q);
22768 
22769 	/*
22770 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22771 	 * Error cases are ENOMEM ...
22772 	 */
22773 	err = ill_dl_phys(ill, ipif, mp, q);
22774 
22775 	/*
22776 	 * If there is no IRE expiration timer running, get one started.
22777 	 * igmp and mld timers will be triggered by the first multicast
22778 	 */
22779 	if (ipst->ips_ip_ire_expire_id == 0) {
22780 		/*
22781 		 * acquire the lock and check again.
22782 		 */
22783 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22784 		if (ipst->ips_ip_ire_expire_id == 0) {
22785 			ipst->ips_ip_ire_expire_id = timeout(
22786 			    ip_trash_timer_expire, ipst,
22787 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22788 		}
22789 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22790 	}
22791 
22792 	if (ill->ill_isv6) {
22793 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22794 		if (ipst->ips_mld_slowtimeout_id == 0) {
22795 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22796 			    (void *)ipst,
22797 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22798 		}
22799 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22800 	} else {
22801 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22802 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22803 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22804 			    (void *)ipst,
22805 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22806 		}
22807 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22808 	}
22809 
22810 	return (err);
22811 }
22812 
22813 /*
22814  * Common routine for ppa and ifname setting. Should be called exclusive.
22815  *
22816  * Returns EINPROGRESS when mp has been consumed by queueing it on
22817  * ill_pending_mp and the ioctl will complete in ip_rput.
22818  *
22819  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22820  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22821  * For SLIFNAME, we pass these values back to the userland.
22822  */
22823 static int
22824 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22825 {
22826 	ill_t	*ill;
22827 	ipif_t	*ipif;
22828 	ipsq_t	*ipsq;
22829 	char	*ppa_ptr;
22830 	char	*old_ptr;
22831 	char	old_char;
22832 	int	error;
22833 	ip_stack_t	*ipst;
22834 
22835 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22836 	ASSERT(q->q_next != NULL);
22837 	ASSERT(interf_name != NULL);
22838 
22839 	ill = (ill_t *)q->q_ptr;
22840 	ipst = ill->ill_ipst;
22841 
22842 	ASSERT(ill->ill_ipst != NULL);
22843 	ASSERT(ill->ill_name[0] == '\0');
22844 	ASSERT(IAM_WRITER_ILL(ill));
22845 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22846 	ASSERT(ill->ill_ppa == UINT_MAX);
22847 
22848 	/* The ppa is sent down by ifconfig or is chosen */
22849 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22850 		return (EINVAL);
22851 	}
22852 
22853 	/*
22854 	 * make sure ppa passed in is same as ppa in the name.
22855 	 * This check is not made when ppa == UINT_MAX in that case ppa
22856 	 * in the name could be anything. System will choose a ppa and
22857 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22858 	 */
22859 	if (*new_ppa_ptr != UINT_MAX) {
22860 		/* stoi changes the pointer */
22861 		old_ptr = ppa_ptr;
22862 		/*
22863 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22864 		 * (they don't have an externally visible ppa).  We assign one
22865 		 * here so that we can manage the interface.  Note that in
22866 		 * the past this value was always 0 for DLPI 1 drivers.
22867 		 */
22868 		if (*new_ppa_ptr == 0)
22869 			*new_ppa_ptr = stoi(&old_ptr);
22870 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22871 			return (EINVAL);
22872 	}
22873 	/*
22874 	 * terminate string before ppa
22875 	 * save char at that location.
22876 	 */
22877 	old_char = ppa_ptr[0];
22878 	ppa_ptr[0] = '\0';
22879 
22880 	ill->ill_ppa = *new_ppa_ptr;
22881 	/*
22882 	 * Finish as much work now as possible before calling ill_glist_insert
22883 	 * which makes the ill globally visible and also merges it with the
22884 	 * other protocol instance of this phyint. The remaining work is
22885 	 * done after entering the ipsq which may happen sometime later.
22886 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22887 	 */
22888 	ipif = ill->ill_ipif;
22889 
22890 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22891 	ipif_assign_seqid(ipif);
22892 
22893 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22894 		ill->ill_flags |= ILLF_IPV4;
22895 
22896 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22897 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22898 
22899 	if (ill->ill_flags & ILLF_IPV6) {
22900 
22901 		ill->ill_isv6 = B_TRUE;
22902 		if (ill->ill_rq != NULL) {
22903 			ill->ill_rq->q_qinfo = &iprinitv6;
22904 			ill->ill_wq->q_qinfo = &ipwinitv6;
22905 		}
22906 
22907 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22908 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22909 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22910 		ipif->ipif_v6subnet = ipv6_all_zeros;
22911 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22912 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22913 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22914 		/*
22915 		 * point-to-point or Non-mulicast capable
22916 		 * interfaces won't do NUD unless explicitly
22917 		 * configured to do so.
22918 		 */
22919 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22920 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22921 			ill->ill_flags |= ILLF_NONUD;
22922 		}
22923 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22924 		if (ill->ill_flags & ILLF_NOARP) {
22925 			/*
22926 			 * Note: xresolv interfaces will eventually need
22927 			 * NOARP set here as well, but that will require
22928 			 * those external resolvers to have some
22929 			 * knowledge of that flag and act appropriately.
22930 			 * Not to be changed at present.
22931 			 */
22932 			ill->ill_flags &= ~ILLF_NOARP;
22933 		}
22934 		/*
22935 		 * Set the ILLF_ROUTER flag according to the global
22936 		 * IPv6 forwarding policy.
22937 		 */
22938 		if (ipst->ips_ipv6_forward != 0)
22939 			ill->ill_flags |= ILLF_ROUTER;
22940 	} else if (ill->ill_flags & ILLF_IPV4) {
22941 		ill->ill_isv6 = B_FALSE;
22942 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
22943 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
22944 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
22945 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
22946 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
22947 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
22948 		/*
22949 		 * Set the ILLF_ROUTER flag according to the global
22950 		 * IPv4 forwarding policy.
22951 		 */
22952 		if (ipst->ips_ip_g_forward != 0)
22953 			ill->ill_flags |= ILLF_ROUTER;
22954 	}
22955 
22956 	ASSERT(ill->ill_phyint != NULL);
22957 
22958 	/*
22959 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
22960 	 * be completed in ill_glist_insert -> ill_phyint_reinit
22961 	 */
22962 	if (!ill_allocate_mibs(ill))
22963 		return (ENOMEM);
22964 
22965 	/*
22966 	 * Pick a default sap until we get the DL_INFO_ACK back from
22967 	 * the driver.
22968 	 */
22969 	if (ill->ill_sap == 0) {
22970 		if (ill->ill_isv6)
22971 			ill->ill_sap  = IP6_DL_SAP;
22972 		else
22973 			ill->ill_sap  = IP_DL_SAP;
22974 	}
22975 
22976 	ill->ill_ifname_pending = 1;
22977 	ill->ill_ifname_pending_err = 0;
22978 
22979 	ill_refhold(ill);
22980 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
22981 	if ((error = ill_glist_insert(ill, interf_name,
22982 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
22983 		ill->ill_ppa = UINT_MAX;
22984 		ill->ill_name[0] = '\0';
22985 		/*
22986 		 * undo null termination done above.
22987 		 */
22988 		ppa_ptr[0] = old_char;
22989 		rw_exit(&ipst->ips_ill_g_lock);
22990 		ill_refrele(ill);
22991 		return (error);
22992 	}
22993 
22994 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
22995 
22996 	/*
22997 	 * When we return the buffer pointed to by interf_name should contain
22998 	 * the same name as in ill_name.
22999 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23000 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23001 	 * so copy full name and update the ppa ptr.
23002 	 * When ppa passed in != UINT_MAX all values are correct just undo
23003 	 * null termination, this saves a bcopy.
23004 	 */
23005 	if (*new_ppa_ptr == UINT_MAX) {
23006 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23007 		*new_ppa_ptr = ill->ill_ppa;
23008 	} else {
23009 		/*
23010 		 * undo null termination done above.
23011 		 */
23012 		ppa_ptr[0] = old_char;
23013 	}
23014 
23015 	/* Let SCTP know about this ILL */
23016 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23017 
23018 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23019 	    B_TRUE);
23020 
23021 	rw_exit(&ipst->ips_ill_g_lock);
23022 	ill_refrele(ill);
23023 	if (ipsq == NULL)
23024 		return (EINPROGRESS);
23025 
23026 	/*
23027 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23028 	 */
23029 	if (ipsq->ipsq_current_ipif == NULL)
23030 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23031 	else
23032 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23033 
23034 	error = ipif_set_values_tail(ill, ipif, mp, q);
23035 	ipsq_exit(ipsq);
23036 	if (error != 0 && error != EINPROGRESS) {
23037 		/*
23038 		 * restore previous values
23039 		 */
23040 		ill->ill_isv6 = B_FALSE;
23041 	}
23042 	return (error);
23043 }
23044 
23045 
23046 void
23047 ipif_init(ip_stack_t *ipst)
23048 {
23049 	hrtime_t hrt;
23050 	int i;
23051 
23052 	/*
23053 	 * Can't call drv_getparm here as it is too early in the boot.
23054 	 * As we use ipif_src_random just for picking a different
23055 	 * source address everytime, this need not be really random.
23056 	 */
23057 	hrt = gethrtime();
23058 	ipst->ips_ipif_src_random =
23059 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23060 
23061 	for (i = 0; i < MAX_G_HEADS; i++) {
23062 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23063 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23064 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23065 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23066 	}
23067 
23068 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23069 	    ill_phyint_compare_index,
23070 	    sizeof (phyint_t),
23071 	    offsetof(struct phyint, phyint_avl_by_index));
23072 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23073 	    ill_phyint_compare_name,
23074 	    sizeof (phyint_t),
23075 	    offsetof(struct phyint, phyint_avl_by_name));
23076 }
23077 
23078 /*
23079  * Lookup the ipif corresponding to the onlink destination address. For
23080  * point-to-point interfaces, it matches with remote endpoint destination
23081  * address. For point-to-multipoint interfaces it only tries to match the
23082  * destination with the interface's subnet address. The longest, most specific
23083  * match is found to take care of such rare network configurations like -
23084  * le0: 129.146.1.1/16
23085  * le1: 129.146.2.2/24
23086  * It is used only by SO_DONTROUTE at the moment.
23087  */
23088 ipif_t *
23089 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23090 {
23091 	ipif_t	*ipif, *best_ipif;
23092 	ill_t	*ill;
23093 	ill_walk_context_t ctx;
23094 
23095 	ASSERT(zoneid != ALL_ZONES);
23096 	best_ipif = NULL;
23097 
23098 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23099 	ill = ILL_START_WALK_V4(&ctx, ipst);
23100 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23101 		mutex_enter(&ill->ill_lock);
23102 		for (ipif = ill->ill_ipif; ipif != NULL;
23103 		    ipif = ipif->ipif_next) {
23104 			if (!IPIF_CAN_LOOKUP(ipif))
23105 				continue;
23106 			if (ipif->ipif_zoneid != zoneid &&
23107 			    ipif->ipif_zoneid != ALL_ZONES)
23108 				continue;
23109 			/*
23110 			 * Point-to-point case. Look for exact match with
23111 			 * destination address.
23112 			 */
23113 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23114 				if (ipif->ipif_pp_dst_addr == addr) {
23115 					ipif_refhold_locked(ipif);
23116 					mutex_exit(&ill->ill_lock);
23117 					rw_exit(&ipst->ips_ill_g_lock);
23118 					if (best_ipif != NULL)
23119 						ipif_refrele(best_ipif);
23120 					return (ipif);
23121 				}
23122 			} else if (ipif->ipif_subnet == (addr &
23123 			    ipif->ipif_net_mask)) {
23124 				/*
23125 				 * Point-to-multipoint case. Looping through to
23126 				 * find the most specific match. If there are
23127 				 * multiple best match ipif's then prefer ipif's
23128 				 * that are UP. If there is only one best match
23129 				 * ipif and it is DOWN we must still return it.
23130 				 */
23131 				if ((best_ipif == NULL) ||
23132 				    (ipif->ipif_net_mask >
23133 				    best_ipif->ipif_net_mask) ||
23134 				    ((ipif->ipif_net_mask ==
23135 				    best_ipif->ipif_net_mask) &&
23136 				    ((ipif->ipif_flags & IPIF_UP) &&
23137 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23138 					ipif_refhold_locked(ipif);
23139 					mutex_exit(&ill->ill_lock);
23140 					rw_exit(&ipst->ips_ill_g_lock);
23141 					if (best_ipif != NULL)
23142 						ipif_refrele(best_ipif);
23143 					best_ipif = ipif;
23144 					rw_enter(&ipst->ips_ill_g_lock,
23145 					    RW_READER);
23146 					mutex_enter(&ill->ill_lock);
23147 				}
23148 			}
23149 		}
23150 		mutex_exit(&ill->ill_lock);
23151 	}
23152 	rw_exit(&ipst->ips_ill_g_lock);
23153 	return (best_ipif);
23154 }
23155 
23156 /*
23157  * Save enough information so that we can recreate the IRE if
23158  * the interface goes down and then up.
23159  */
23160 static void
23161 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23162 {
23163 	mblk_t	*save_mp;
23164 
23165 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23166 	if (save_mp != NULL) {
23167 		ifrt_t	*ifrt;
23168 
23169 		save_mp->b_wptr += sizeof (ifrt_t);
23170 		ifrt = (ifrt_t *)save_mp->b_rptr;
23171 		bzero(ifrt, sizeof (ifrt_t));
23172 		ifrt->ifrt_type = ire->ire_type;
23173 		ifrt->ifrt_addr = ire->ire_addr;
23174 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23175 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23176 		ifrt->ifrt_mask = ire->ire_mask;
23177 		ifrt->ifrt_flags = ire->ire_flags;
23178 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23179 		mutex_enter(&ipif->ipif_saved_ire_lock);
23180 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23181 		ipif->ipif_saved_ire_mp = save_mp;
23182 		ipif->ipif_saved_ire_cnt++;
23183 		mutex_exit(&ipif->ipif_saved_ire_lock);
23184 	}
23185 }
23186 
23187 static void
23188 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23189 {
23190 	mblk_t	**mpp;
23191 	mblk_t	*mp;
23192 	ifrt_t	*ifrt;
23193 
23194 	/* Remove from ipif_saved_ire_mp list if it is there */
23195 	mutex_enter(&ipif->ipif_saved_ire_lock);
23196 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23197 	    mpp = &(*mpp)->b_cont) {
23198 		/*
23199 		 * On a given ipif, the triple of address, gateway and
23200 		 * mask is unique for each saved IRE (in the case of
23201 		 * ordinary interface routes, the gateway address is
23202 		 * all-zeroes).
23203 		 */
23204 		mp = *mpp;
23205 		ifrt = (ifrt_t *)mp->b_rptr;
23206 		if (ifrt->ifrt_addr == ire->ire_addr &&
23207 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23208 		    ifrt->ifrt_mask == ire->ire_mask) {
23209 			*mpp = mp->b_cont;
23210 			ipif->ipif_saved_ire_cnt--;
23211 			freeb(mp);
23212 			break;
23213 		}
23214 	}
23215 	mutex_exit(&ipif->ipif_saved_ire_lock);
23216 }
23217 
23218 /*
23219  * IP multirouting broadcast routes handling
23220  * Append CGTP broadcast IREs to regular ones created
23221  * at ifconfig time.
23222  */
23223 static void
23224 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23225 {
23226 	ire_t *ire_prim;
23227 
23228 	ASSERT(ire != NULL);
23229 	ASSERT(ire_dst != NULL);
23230 
23231 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23232 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23233 	if (ire_prim != NULL) {
23234 		/*
23235 		 * We are in the special case of broadcasts for
23236 		 * CGTP. We add an IRE_BROADCAST that holds
23237 		 * the RTF_MULTIRT flag, the destination
23238 		 * address of ire_dst and the low level
23239 		 * info of ire_prim. In other words, CGTP
23240 		 * broadcast is added to the redundant ipif.
23241 		 */
23242 		ipif_t *ipif_prim;
23243 		ire_t  *bcast_ire;
23244 
23245 		ipif_prim = ire_prim->ire_ipif;
23246 
23247 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23248 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23249 		    (void *)ire_dst, (void *)ire_prim,
23250 		    (void *)ipif_prim));
23251 
23252 		bcast_ire = ire_create(
23253 		    (uchar_t *)&ire->ire_addr,
23254 		    (uchar_t *)&ip_g_all_ones,
23255 		    (uchar_t *)&ire_dst->ire_src_addr,
23256 		    (uchar_t *)&ire->ire_gateway_addr,
23257 		    &ipif_prim->ipif_mtu,
23258 		    NULL,
23259 		    ipif_prim->ipif_rq,
23260 		    ipif_prim->ipif_wq,
23261 		    IRE_BROADCAST,
23262 		    ipif_prim,
23263 		    0,
23264 		    0,
23265 		    0,
23266 		    ire->ire_flags,
23267 		    &ire_uinfo_null,
23268 		    NULL,
23269 		    NULL,
23270 		    ipst);
23271 
23272 		if (bcast_ire != NULL) {
23273 
23274 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23275 			    B_FALSE) == 0) {
23276 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23277 				    "added bcast_ire %p\n",
23278 				    (void *)bcast_ire));
23279 
23280 				ipif_save_ire(bcast_ire->ire_ipif,
23281 				    bcast_ire);
23282 				ire_refrele(bcast_ire);
23283 			}
23284 		}
23285 		ire_refrele(ire_prim);
23286 	}
23287 }
23288 
23289 
23290 /*
23291  * IP multirouting broadcast routes handling
23292  * Remove the broadcast ire
23293  */
23294 static void
23295 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23296 {
23297 	ire_t *ire_dst;
23298 
23299 	ASSERT(ire != NULL);
23300 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23301 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23302 	if (ire_dst != NULL) {
23303 		ire_t *ire_prim;
23304 
23305 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23306 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23307 		if (ire_prim != NULL) {
23308 			ipif_t *ipif_prim;
23309 			ire_t  *bcast_ire;
23310 
23311 			ipif_prim = ire_prim->ire_ipif;
23312 
23313 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23314 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23315 			    (void *)ire_dst, (void *)ire_prim,
23316 			    (void *)ipif_prim));
23317 
23318 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23319 			    ire->ire_gateway_addr,
23320 			    IRE_BROADCAST,
23321 			    ipif_prim, ALL_ZONES,
23322 			    NULL,
23323 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23324 			    MATCH_IRE_MASK, ipst);
23325 
23326 			if (bcast_ire != NULL) {
23327 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23328 				    "looked up bcast_ire %p\n",
23329 				    (void *)bcast_ire));
23330 				ipif_remove_ire(bcast_ire->ire_ipif,
23331 				    bcast_ire);
23332 				ire_delete(bcast_ire);
23333 				ire_refrele(bcast_ire);
23334 			}
23335 			ire_refrele(ire_prim);
23336 		}
23337 		ire_refrele(ire_dst);
23338 	}
23339 }
23340 
23341 /*
23342  * IPsec hardware acceleration capabilities related functions.
23343  */
23344 
23345 /*
23346  * Free a per-ill IPsec capabilities structure.
23347  */
23348 static void
23349 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23350 {
23351 	if (capab->auth_hw_algs != NULL)
23352 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23353 	if (capab->encr_hw_algs != NULL)
23354 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23355 	if (capab->encr_algparm != NULL)
23356 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23357 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23358 }
23359 
23360 /*
23361  * Allocate a new per-ill IPsec capabilities structure. This structure
23362  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23363  * an array which specifies, for each algorithm, whether this algorithm
23364  * is supported by the ill or not.
23365  */
23366 static ill_ipsec_capab_t *
23367 ill_ipsec_capab_alloc(void)
23368 {
23369 	ill_ipsec_capab_t *capab;
23370 	uint_t nelems;
23371 
23372 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23373 	if (capab == NULL)
23374 		return (NULL);
23375 
23376 	/* we need one bit per algorithm */
23377 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23378 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23379 
23380 	/* allocate memory to store algorithm flags */
23381 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23382 	if (capab->encr_hw_algs == NULL)
23383 		goto nomem;
23384 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23385 	if (capab->auth_hw_algs == NULL)
23386 		goto nomem;
23387 	/*
23388 	 * Leave encr_algparm NULL for now since we won't need it half
23389 	 * the time
23390 	 */
23391 	return (capab);
23392 
23393 nomem:
23394 	ill_ipsec_capab_free(capab);
23395 	return (NULL);
23396 }
23397 
23398 /*
23399  * Resize capability array.  Since we're exclusive, this is OK.
23400  */
23401 static boolean_t
23402 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23403 {
23404 	ipsec_capab_algparm_t *nalp, *oalp;
23405 	uint32_t olen, nlen;
23406 
23407 	oalp = capab->encr_algparm;
23408 	olen = capab->encr_algparm_size;
23409 
23410 	if (oalp != NULL) {
23411 		if (algid < capab->encr_algparm_end)
23412 			return (B_TRUE);
23413 	}
23414 
23415 	nlen = (algid + 1) * sizeof (*nalp);
23416 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23417 	if (nalp == NULL)
23418 		return (B_FALSE);
23419 
23420 	if (oalp != NULL) {
23421 		bcopy(oalp, nalp, olen);
23422 		kmem_free(oalp, olen);
23423 	}
23424 	capab->encr_algparm = nalp;
23425 	capab->encr_algparm_size = nlen;
23426 	capab->encr_algparm_end = algid + 1;
23427 
23428 	return (B_TRUE);
23429 }
23430 
23431 /*
23432  * Compare the capabilities of the specified ill with the protocol
23433  * and algorithms specified by the SA passed as argument.
23434  * If they match, returns B_TRUE, B_FALSE if they do not match.
23435  *
23436  * The ill can be passed as a pointer to it, or by specifying its index
23437  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23438  *
23439  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23440  * packet is eligible for hardware acceleration, and by
23441  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23442  * to a particular ill.
23443  */
23444 boolean_t
23445 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23446     ipsa_t *sa, netstack_t *ns)
23447 {
23448 	boolean_t sa_isv6;
23449 	uint_t algid;
23450 	struct ill_ipsec_capab_s *cpp;
23451 	boolean_t need_refrele = B_FALSE;
23452 	ip_stack_t	*ipst = ns->netstack_ip;
23453 
23454 	if (ill == NULL) {
23455 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23456 		    NULL, NULL, NULL, ipst);
23457 		if (ill == NULL) {
23458 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23459 			return (B_FALSE);
23460 		}
23461 		need_refrele = B_TRUE;
23462 	}
23463 
23464 	/*
23465 	 * Use the address length specified by the SA to determine
23466 	 * if it corresponds to a IPv6 address, and fail the matching
23467 	 * if the isv6 flag passed as argument does not match.
23468 	 * Note: this check is used for SADB capability checking before
23469 	 * sending SA information to an ill.
23470 	 */
23471 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23472 	if (sa_isv6 != ill_isv6)
23473 		/* protocol mismatch */
23474 		goto done;
23475 
23476 	/*
23477 	 * Check if the ill supports the protocol, algorithm(s) and
23478 	 * key size(s) specified by the SA, and get the pointers to
23479 	 * the algorithms supported by the ill.
23480 	 */
23481 	switch (sa->ipsa_type) {
23482 
23483 	case SADB_SATYPE_ESP:
23484 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23485 			/* ill does not support ESP acceleration */
23486 			goto done;
23487 		cpp = ill->ill_ipsec_capab_esp;
23488 		algid = sa->ipsa_auth_alg;
23489 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23490 			goto done;
23491 		algid = sa->ipsa_encr_alg;
23492 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23493 			goto done;
23494 		if (algid < cpp->encr_algparm_end) {
23495 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23496 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23497 				goto done;
23498 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23499 				goto done;
23500 		}
23501 		break;
23502 
23503 	case SADB_SATYPE_AH:
23504 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23505 			/* ill does not support AH acceleration */
23506 			goto done;
23507 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23508 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23509 			goto done;
23510 		break;
23511 	}
23512 
23513 	if (need_refrele)
23514 		ill_refrele(ill);
23515 	return (B_TRUE);
23516 done:
23517 	if (need_refrele)
23518 		ill_refrele(ill);
23519 	return (B_FALSE);
23520 }
23521 
23522 /*
23523  * Add a new ill to the list of IPsec capable ills.
23524  * Called from ill_capability_ipsec_ack() when an ACK was received
23525  * indicating that IPsec hardware processing was enabled for an ill.
23526  *
23527  * ill must point to the ill for which acceleration was enabled.
23528  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23529  */
23530 static void
23531 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23532 {
23533 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23534 	uint_t sa_type;
23535 	uint_t ipproto;
23536 	ip_stack_t	*ipst = ill->ill_ipst;
23537 
23538 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23539 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23540 
23541 	switch (dl_cap) {
23542 	case DL_CAPAB_IPSEC_AH:
23543 		sa_type = SADB_SATYPE_AH;
23544 		ills = &ipst->ips_ipsec_capab_ills_ah;
23545 		ipproto = IPPROTO_AH;
23546 		break;
23547 	case DL_CAPAB_IPSEC_ESP:
23548 		sa_type = SADB_SATYPE_ESP;
23549 		ills = &ipst->ips_ipsec_capab_ills_esp;
23550 		ipproto = IPPROTO_ESP;
23551 		break;
23552 	}
23553 
23554 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23555 
23556 	/*
23557 	 * Add ill index to list of hardware accelerators. If
23558 	 * already in list, do nothing.
23559 	 */
23560 	for (cur_ill = *ills; cur_ill != NULL &&
23561 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23562 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23563 		;
23564 
23565 	if (cur_ill == NULL) {
23566 		/* if this is a new entry for this ill */
23567 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23568 		if (new_ill == NULL) {
23569 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23570 			return;
23571 		}
23572 
23573 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23574 		new_ill->ill_isv6 = ill->ill_isv6;
23575 		new_ill->next = *ills;
23576 		*ills = new_ill;
23577 	} else if (!sadb_resync) {
23578 		/* not resync'ing SADB and an entry exists for this ill */
23579 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23580 		return;
23581 	}
23582 
23583 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23584 
23585 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23586 		/*
23587 		 * IPsec module for protocol loaded, initiate dump
23588 		 * of the SADB to this ill.
23589 		 */
23590 		sadb_ill_download(ill, sa_type);
23591 }
23592 
23593 /*
23594  * Remove an ill from the list of IPsec capable ills.
23595  */
23596 static void
23597 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23598 {
23599 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23600 	ip_stack_t	*ipst = ill->ill_ipst;
23601 
23602 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23603 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23604 
23605 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23606 	    &ipst->ips_ipsec_capab_ills_esp;
23607 
23608 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23609 
23610 	prev_ill = NULL;
23611 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23612 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23613 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23614 		;
23615 	if (cur_ill == NULL) {
23616 		/* entry not found */
23617 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23618 		return;
23619 	}
23620 	if (prev_ill == NULL) {
23621 		/* entry at front of list */
23622 		*ills = NULL;
23623 	} else {
23624 		prev_ill->next = cur_ill->next;
23625 	}
23626 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23627 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23628 }
23629 
23630 /*
23631  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23632  * supporting the specified IPsec protocol acceleration.
23633  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23634  * We free the mblk and, if sa is non-null, release the held referece.
23635  */
23636 void
23637 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23638     netstack_t *ns)
23639 {
23640 	ipsec_capab_ill_t *ici, *cur_ici;
23641 	ill_t *ill;
23642 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23643 	ip_stack_t	*ipst = ns->netstack_ip;
23644 
23645 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23646 	    ipst->ips_ipsec_capab_ills_esp;
23647 
23648 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23649 
23650 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23651 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23652 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23653 
23654 		/*
23655 		 * Handle the case where the ill goes away while the SADB is
23656 		 * attempting to send messages.  If it's going away, it's
23657 		 * nuking its shadow SADB, so we don't care..
23658 		 */
23659 
23660 		if (ill == NULL)
23661 			continue;
23662 
23663 		if (sa != NULL) {
23664 			/*
23665 			 * Make sure capabilities match before
23666 			 * sending SA to ill.
23667 			 */
23668 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23669 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23670 				ill_refrele(ill);
23671 				continue;
23672 			}
23673 
23674 			mutex_enter(&sa->ipsa_lock);
23675 			sa->ipsa_flags |= IPSA_F_HW;
23676 			mutex_exit(&sa->ipsa_lock);
23677 		}
23678 
23679 		/*
23680 		 * Copy template message, and add it to the front
23681 		 * of the mblk ship list. We want to avoid holding
23682 		 * the ipsec_capab_ills_lock while sending the
23683 		 * message to the ills.
23684 		 *
23685 		 * The b_next and b_prev are temporarily used
23686 		 * to build a list of mblks to be sent down, and to
23687 		 * save the ill to which they must be sent.
23688 		 */
23689 		nmp = copymsg(mp);
23690 		if (nmp == NULL) {
23691 			ill_refrele(ill);
23692 			continue;
23693 		}
23694 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23695 		nmp->b_next = mp_ship_list;
23696 		mp_ship_list = nmp;
23697 		nmp->b_prev = (mblk_t *)ill;
23698 	}
23699 
23700 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23701 
23702 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23703 		/* restore the mblk to a sane state */
23704 		next_mp = nmp->b_next;
23705 		nmp->b_next = NULL;
23706 		ill = (ill_t *)nmp->b_prev;
23707 		nmp->b_prev = NULL;
23708 
23709 		ill_dlpi_send(ill, nmp);
23710 		ill_refrele(ill);
23711 	}
23712 
23713 	if (sa != NULL)
23714 		IPSA_REFRELE(sa);
23715 	freemsg(mp);
23716 }
23717 
23718 /*
23719  * Derive an interface id from the link layer address.
23720  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23721  */
23722 static boolean_t
23723 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23724 {
23725 	char		*addr;
23726 
23727 	if (phys_length != ETHERADDRL)
23728 		return (B_FALSE);
23729 
23730 	/* Form EUI-64 like address */
23731 	addr = (char *)&v6addr->s6_addr32[2];
23732 	bcopy((char *)phys_addr, addr, 3);
23733 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23734 	addr[3] = (char)0xff;
23735 	addr[4] = (char)0xfe;
23736 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23737 	return (B_TRUE);
23738 }
23739 
23740 /* ARGSUSED */
23741 static boolean_t
23742 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23743 {
23744 	return (B_FALSE);
23745 }
23746 
23747 /* ARGSUSED */
23748 static boolean_t
23749 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23750     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23751 {
23752 	/*
23753 	 * Multicast address mappings used over Ethernet/802.X.
23754 	 * This address is used as a base for mappings.
23755 	 */
23756 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23757 	    0x00, 0x00, 0x00};
23758 
23759 	/*
23760 	 * Extract low order 32 bits from IPv6 multicast address.
23761 	 * Or that into the link layer address, starting from the
23762 	 * second byte.
23763 	 */
23764 	*hw_start = 2;
23765 	v6_extract_mask->s6_addr32[0] = 0;
23766 	v6_extract_mask->s6_addr32[1] = 0;
23767 	v6_extract_mask->s6_addr32[2] = 0;
23768 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23769 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23770 	return (B_TRUE);
23771 }
23772 
23773 /*
23774  * Indicate by return value whether multicast is supported. If not,
23775  * this code should not touch/change any parameters.
23776  */
23777 /* ARGSUSED */
23778 static boolean_t
23779 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23780     uint32_t *hw_start, ipaddr_t *extract_mask)
23781 {
23782 	/*
23783 	 * Multicast address mappings used over Ethernet/802.X.
23784 	 * This address is used as a base for mappings.
23785 	 */
23786 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23787 	    0x00, 0x00, 0x00 };
23788 
23789 	if (phys_length != ETHERADDRL)
23790 		return (B_FALSE);
23791 
23792 	*extract_mask = htonl(0x007fffff);
23793 	*hw_start = 2;
23794 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23795 	return (B_TRUE);
23796 }
23797 
23798 /*
23799  * Derive IPoIB interface id from the link layer address.
23800  */
23801 static boolean_t
23802 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23803 {
23804 	char		*addr;
23805 
23806 	if (phys_length != 20)
23807 		return (B_FALSE);
23808 	addr = (char *)&v6addr->s6_addr32[2];
23809 	bcopy(phys_addr + 12, addr, 8);
23810 	/*
23811 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23812 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23813 	 * rules. In these cases, the IBA considers these GUIDs to be in
23814 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23815 	 * required; vendors are required not to assign global EUI-64's
23816 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23817 	 * of the interface identifier. Whether the GUID is in modified
23818 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23819 	 * bit set to 1.
23820 	 */
23821 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23822 	return (B_TRUE);
23823 }
23824 
23825 /*
23826  * Note on mapping from multicast IP addresses to IPoIB multicast link
23827  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23828  * The format of an IPoIB multicast address is:
23829  *
23830  *  4 byte QPN      Scope Sign.  Pkey
23831  * +--------------------------------------------+
23832  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23833  * +--------------------------------------------+
23834  *
23835  * The Scope and Pkey components are properties of the IBA port and
23836  * network interface. They can be ascertained from the broadcast address.
23837  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23838  */
23839 
23840 static boolean_t
23841 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23842     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23843 {
23844 	/*
23845 	 * Base IPoIB IPv6 multicast address used for mappings.
23846 	 * Does not contain the IBA scope/Pkey values.
23847 	 */
23848 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23849 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23850 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23851 
23852 	/*
23853 	 * Extract low order 80 bits from IPv6 multicast address.
23854 	 * Or that into the link layer address, starting from the
23855 	 * sixth byte.
23856 	 */
23857 	*hw_start = 6;
23858 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23859 
23860 	/*
23861 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23862 	 */
23863 	*(maddr + 5) = *(bphys_addr + 5);
23864 	*(maddr + 8) = *(bphys_addr + 8);
23865 	*(maddr + 9) = *(bphys_addr + 9);
23866 
23867 	v6_extract_mask->s6_addr32[0] = 0;
23868 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23869 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23870 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23871 	return (B_TRUE);
23872 }
23873 
23874 static boolean_t
23875 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23876     uint32_t *hw_start, ipaddr_t *extract_mask)
23877 {
23878 	/*
23879 	 * Base IPoIB IPv4 multicast address used for mappings.
23880 	 * Does not contain the IBA scope/Pkey values.
23881 	 */
23882 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23883 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23884 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23885 
23886 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23887 		return (B_FALSE);
23888 
23889 	/*
23890 	 * Extract low order 28 bits from IPv4 multicast address.
23891 	 * Or that into the link layer address, starting from the
23892 	 * sixteenth byte.
23893 	 */
23894 	*extract_mask = htonl(0x0fffffff);
23895 	*hw_start = 16;
23896 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23897 
23898 	/*
23899 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23900 	 */
23901 	*(maddr + 5) = *(bphys_addr + 5);
23902 	*(maddr + 8) = *(bphys_addr + 8);
23903 	*(maddr + 9) = *(bphys_addr + 9);
23904 	return (B_TRUE);
23905 }
23906 
23907 /*
23908  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23909  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23910  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23911  * the link-local address is preferred.
23912  */
23913 boolean_t
23914 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23915 {
23916 	ipif_t	*ipif;
23917 	ipif_t	*maybe_ipif = NULL;
23918 
23919 	mutex_enter(&ill->ill_lock);
23920 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23921 		mutex_exit(&ill->ill_lock);
23922 		if (ipifp != NULL)
23923 			*ipifp = NULL;
23924 		return (B_FALSE);
23925 	}
23926 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23927 		if (!IPIF_CAN_LOOKUP(ipif))
23928 			continue;
23929 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23930 		    ipif->ipif_zoneid != ALL_ZONES)
23931 			continue;
23932 		if ((ipif->ipif_flags & flags) != flags)
23933 			continue;
23934 
23935 		if (ipifp == NULL) {
23936 			mutex_exit(&ill->ill_lock);
23937 			ASSERT(maybe_ipif == NULL);
23938 			return (B_TRUE);
23939 		}
23940 		if (!ill->ill_isv6 ||
23941 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23942 			ipif_refhold_locked(ipif);
23943 			mutex_exit(&ill->ill_lock);
23944 			*ipifp = ipif;
23945 			return (B_TRUE);
23946 		}
23947 		if (maybe_ipif == NULL)
23948 			maybe_ipif = ipif;
23949 	}
23950 	if (ipifp != NULL) {
23951 		if (maybe_ipif != NULL)
23952 			ipif_refhold_locked(maybe_ipif);
23953 		*ipifp = maybe_ipif;
23954 	}
23955 	mutex_exit(&ill->ill_lock);
23956 	return (maybe_ipif != NULL);
23957 }
23958 
23959 /*
23960  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
23961  */
23962 boolean_t
23963 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23964 {
23965 	ill_t *illg;
23966 	ip_stack_t	*ipst = ill->ill_ipst;
23967 
23968 	/*
23969 	 * We look at the passed-in ill first without grabbing ill_g_lock.
23970 	 */
23971 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
23972 		return (B_TRUE);
23973 	}
23974 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23975 	if (ill->ill_group == NULL) {
23976 		/* ill not in a group */
23977 		rw_exit(&ipst->ips_ill_g_lock);
23978 		return (B_FALSE);
23979 	}
23980 
23981 	/*
23982 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
23983 	 * group. We need to look for an ipif in the zone on all the ills in the
23984 	 * group.
23985 	 */
23986 	illg = ill->ill_group->illgrp_ill;
23987 	do {
23988 		/*
23989 		 * We don't call ipif_lookup_zoneid() on ill as we already know
23990 		 * that it's not there.
23991 		 */
23992 		if (illg != ill &&
23993 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
23994 			break;
23995 		}
23996 	} while ((illg = illg->ill_group_next) != NULL);
23997 	rw_exit(&ipst->ips_ill_g_lock);
23998 	return (illg != NULL);
23999 }
24000 
24001 /*
24002  * Check if this ill is only being used to send ICMP probes for IPMP
24003  */
24004 boolean_t
24005 ill_is_probeonly(ill_t *ill)
24006 {
24007 	/*
24008 	 * Check if the interface is FAILED, or INACTIVE
24009 	 */
24010 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24011 		return (B_TRUE);
24012 
24013 	return (B_FALSE);
24014 }
24015 
24016 /*
24017  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24018  * If a pointer to an ipif_t is returned then the caller will need to do
24019  * an ill_refrele().
24020  *
24021  * If there is no real interface which matches the ifindex, then it looks
24022  * for a group that has a matching index. In the case of a group match the
24023  * lifidx must be zero. We don't need emulate the logical interfaces
24024  * since IP Filter's use of netinfo doesn't use that.
24025  */
24026 ipif_t *
24027 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24028     ip_stack_t *ipst)
24029 {
24030 	ipif_t *ipif;
24031 	ill_t *ill;
24032 
24033 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24034 	    ipst);
24035 
24036 	if (ill == NULL) {
24037 		/* Fallback to group names only if hook_emulation set */
24038 		if (!ipst->ips_ipmp_hook_emulation)
24039 			return (NULL);
24040 
24041 		if (lifidx != 0)
24042 			return (NULL);
24043 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24044 		if (ill == NULL)
24045 			return (NULL);
24046 	}
24047 
24048 	mutex_enter(&ill->ill_lock);
24049 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24050 		mutex_exit(&ill->ill_lock);
24051 		ill_refrele(ill);
24052 		return (NULL);
24053 	}
24054 
24055 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24056 		if (!IPIF_CAN_LOOKUP(ipif))
24057 			continue;
24058 		if (lifidx == ipif->ipif_id) {
24059 			ipif_refhold_locked(ipif);
24060 			break;
24061 		}
24062 	}
24063 
24064 	mutex_exit(&ill->ill_lock);
24065 	ill_refrele(ill);
24066 	return (ipif);
24067 }
24068 
24069 /*
24070  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24071  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24072  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24073  * for details.
24074  */
24075 void
24076 ill_fastpath_flush(ill_t *ill)
24077 {
24078 	ip_stack_t *ipst = ill->ill_ipst;
24079 
24080 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24081 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24082 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24083 }
24084 
24085 /*
24086  * Set the physical address information for `ill' to the contents of the
24087  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24088  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24089  * EINPROGRESS will be returned.
24090  */
24091 int
24092 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24093 {
24094 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24095 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24096 
24097 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24098 
24099 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24100 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24101 		/* Changing DL_IPV6_TOKEN is not yet supported */
24102 		return (0);
24103 	}
24104 
24105 	/*
24106 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24107 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24108 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24109 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24110 	 */
24111 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24112 		freemsg(mp);
24113 		return (ENOMEM);
24114 	}
24115 
24116 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24117 
24118 	/*
24119 	 * If we can quiesce the ill, then set the address.  If not, then
24120 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24121 	 */
24122 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24123 	mutex_enter(&ill->ill_lock);
24124 	if (!ill_is_quiescent(ill)) {
24125 		/* call cannot fail since `conn_t *' argument is NULL */
24126 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24127 		    mp, ILL_DOWN);
24128 		mutex_exit(&ill->ill_lock);
24129 		return (EINPROGRESS);
24130 	}
24131 	mutex_exit(&ill->ill_lock);
24132 
24133 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24134 	return (0);
24135 }
24136 
24137 /*
24138  * Once the ill associated with `q' has quiesced, set its physical address
24139  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24140  * are passed (linked by b_cont), since we sometimes need to save two distinct
24141  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24142  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24143  * is quiesced, we know any stale IREs with the old address information have
24144  * already been removed, so we don't need to call ill_fastpath_flush().
24145  */
24146 /* ARGSUSED */
24147 static void
24148 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24149 {
24150 	ill_t		*ill = q->q_ptr;
24151 	mblk_t		*addrmp2 = unlinkb(addrmp);
24152 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24153 	uint_t		addrlen, addroff;
24154 
24155 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24156 
24157 	addroff	= dlindp->dl_addr_offset;
24158 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24159 
24160 	switch (dlindp->dl_data) {
24161 	case DL_IPV6_LINK_LAYER_ADDR:
24162 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24163 		freemsg(addrmp2);
24164 		break;
24165 
24166 	case DL_CURR_PHYS_ADDR:
24167 		freemsg(ill->ill_phys_addr_mp);
24168 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24169 		ill->ill_phys_addr_mp = addrmp;
24170 		ill->ill_phys_addr_length = addrlen;
24171 
24172 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24173 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24174 		else
24175 			freemsg(addrmp2);
24176 		break;
24177 	default:
24178 		ASSERT(0);
24179 	}
24180 
24181 	/*
24182 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24183 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24184 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24185 	 * brought up.
24186 	 */
24187 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24188 		ipsq_current_finish(ipsq);
24189 }
24190 
24191 /*
24192  * Helper routine for setting the ill_nd_lla fields.
24193  */
24194 void
24195 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24196 {
24197 	freemsg(ill->ill_nd_lla_mp);
24198 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24199 	ill->ill_nd_lla_mp = ndmp;
24200 	ill->ill_nd_lla_len = addrlen;
24201 }
24202 
24203 major_t IP_MAJ;
24204 #define	IP	"ip"
24205 
24206 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24207 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24208 
24209 /*
24210  * Issue REMOVEIF ioctls to have the loopback interfaces
24211  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24212  * the former going away when the user-level processes in the zone
24213  * are killed  * and the latter are cleaned up by the stream head
24214  * str_stack_shutdown callback that undoes all I_PLINKs.
24215  */
24216 void
24217 ip_loopback_cleanup(ip_stack_t *ipst)
24218 {
24219 	int error;
24220 	ldi_handle_t	lh = NULL;
24221 	ldi_ident_t	li = NULL;
24222 	int		rval;
24223 	cred_t		*cr;
24224 	struct strioctl iocb;
24225 	struct lifreq	lifreq;
24226 
24227 	IP_MAJ = ddi_name_to_major(IP);
24228 
24229 #ifdef NS_DEBUG
24230 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24231 	    ipst->ips_netstack->netstack_stackid);
24232 #endif
24233 
24234 	bzero(&lifreq, sizeof (lifreq));
24235 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24236 
24237 	error = ldi_ident_from_major(IP_MAJ, &li);
24238 	if (error) {
24239 #ifdef DEBUG
24240 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24241 		    error);
24242 #endif
24243 		return;
24244 	}
24245 
24246 	cr = zone_get_kcred(netstackid_to_zoneid(
24247 	    ipst->ips_netstack->netstack_stackid));
24248 	ASSERT(cr != NULL);
24249 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24250 	if (error) {
24251 #ifdef DEBUG
24252 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24253 		    error);
24254 #endif
24255 		goto out;
24256 	}
24257 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24258 	iocb.ic_timout = 15;
24259 	iocb.ic_len = sizeof (lifreq);
24260 	iocb.ic_dp = (char *)&lifreq;
24261 
24262 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24263 	/* LINTED - statement has no consequent */
24264 	if (error) {
24265 #ifdef NS_DEBUG
24266 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24267 		    "UDP6 error %d\n", error);
24268 #endif
24269 	}
24270 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24271 	lh = NULL;
24272 
24273 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24274 	if (error) {
24275 #ifdef NS_DEBUG
24276 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24277 		    error);
24278 #endif
24279 		goto out;
24280 	}
24281 
24282 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24283 	iocb.ic_timout = 15;
24284 	iocb.ic_len = sizeof (lifreq);
24285 	iocb.ic_dp = (char *)&lifreq;
24286 
24287 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24288 	/* LINTED - statement has no consequent */
24289 	if (error) {
24290 #ifdef NS_DEBUG
24291 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24292 		    "UDP error %d\n", error);
24293 #endif
24294 	}
24295 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24296 	lh = NULL;
24297 
24298 out:
24299 	/* Close layered handles */
24300 	if (lh)
24301 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24302 	if (li)
24303 		ldi_ident_release(li);
24304 
24305 	crfree(cr);
24306 }
24307 
24308 /*
24309  * This needs to be in-sync with nic_event_t definition
24310  */
24311 static const char *
24312 ill_hook_event2str(nic_event_t event)
24313 {
24314 	switch (event) {
24315 	case NE_PLUMB:
24316 		return ("PLUMB");
24317 	case NE_UNPLUMB:
24318 		return ("UNPLUMB");
24319 	case NE_UP:
24320 		return ("UP");
24321 	case NE_DOWN:
24322 		return ("DOWN");
24323 	case NE_ADDRESS_CHANGE:
24324 		return ("ADDRESS_CHANGE");
24325 	default:
24326 		return ("UNKNOWN");
24327 	}
24328 }
24329 
24330 static void
24331 ill_hook_event_destroy(ill_t *ill)
24332 {
24333 	hook_nic_event_t	*info;
24334 
24335 	if ((info = ill->ill_nic_event_info) != NULL) {
24336 		if (info->hne_data != NULL)
24337 			kmem_free(info->hne_data, info->hne_datalen);
24338 		kmem_free(info, sizeof (hook_nic_event_t));
24339 
24340 		ill->ill_nic_event_info = NULL;
24341 	}
24342 
24343 }
24344 
24345 boolean_t
24346 ill_hook_event_create(ill_t *ill, lif_if_t lif, nic_event_t event,
24347     nic_event_data_t data, size_t datalen)
24348 {
24349 	ip_stack_t		*ipst = ill->ill_ipst;
24350 	hook_nic_event_t	*info;
24351 	const char		*str = NULL;
24352 
24353 	/* destroy nic event info if it exists */
24354 	if ((info = ill->ill_nic_event_info) != NULL) {
24355 		str = ill_hook_event2str(info->hne_event);
24356 		ip2dbg(("ill_hook_event_create: unexpected nic event %s "
24357 		    "attached for %s\n", str, ill->ill_name));
24358 		ill_hook_event_destroy(ill);
24359 	}
24360 
24361 	/* create a new nic event info */
24362 	if ((info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP)) == NULL)
24363 		goto fail;
24364 
24365 	ill->ill_nic_event_info = info;
24366 
24367 	if (event == NE_UNPLUMB)
24368 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
24369 	else
24370 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
24371 	info->hne_lif = lif;
24372 	info->hne_event = event;
24373 	info->hne_family = ill->ill_isv6 ?
24374 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
24375 	info->hne_data = NULL;
24376 	info->hne_datalen = 0;
24377 
24378 	if (data != NULL && datalen != 0) {
24379 		info->hne_data = kmem_alloc(datalen, KM_NOSLEEP);
24380 		if (info->hne_data != NULL) {
24381 			bcopy(data, info->hne_data, datalen);
24382 			info->hne_datalen = datalen;
24383 		} else {
24384 			ill_hook_event_destroy(ill);
24385 			goto fail;
24386 		}
24387 	}
24388 
24389 	return (B_TRUE);
24390 fail:
24391 	str = ill_hook_event2str(event);
24392 	ip2dbg(("ill_hook_event_create: could not attach %s nic event "
24393 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
24394 	return (B_FALSE);
24395 }
24396