xref: /titanic_41/usr/src/uts/common/inet/ip/ip_if.c (revision 0a701b1ec2b55bddc48b62124df936152ff820f7)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 /*
28  * This file contains the interface control functions for IP.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/dlpi.h>
34 #include <sys/stropts.h>
35 #include <sys/strsun.h>
36 #include <sys/sysmacros.h>
37 #include <sys/strlog.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/cmn_err.h>
41 #include <sys/kstat.h>
42 #include <sys/debug.h>
43 #include <sys/zone.h>
44 #include <sys/sunldi.h>
45 #include <sys/file.h>
46 #include <sys/bitmap.h>
47 
48 #include <sys/kmem.h>
49 #include <sys/systm.h>
50 #include <sys/param.h>
51 #include <sys/socket.h>
52 #include <sys/isa_defs.h>
53 #include <net/if.h>
54 #include <net/if_arp.h>
55 #include <net/if_types.h>
56 #include <net/if_dl.h>
57 #include <net/route.h>
58 #include <sys/sockio.h>
59 #include <netinet/in.h>
60 #include <netinet/ip6.h>
61 #include <netinet/icmp6.h>
62 #include <netinet/igmp_var.h>
63 #include <sys/strsun.h>
64 #include <sys/policy.h>
65 #include <sys/ethernet.h>
66 
67 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
68 #include <inet/mi.h>
69 #include <inet/nd.h>
70 #include <inet/arp.h>
71 #include <inet/mib2.h>
72 #include <inet/ip.h>
73 #include <inet/ip6.h>
74 #include <inet/ip6_asp.h>
75 #include <inet/tcp.h>
76 #include <inet/ip_multi.h>
77 #include <inet/ip_ire.h>
78 #include <inet/ip_ftable.h>
79 #include <inet/ip_rts.h>
80 #include <inet/ip_ndp.h>
81 #include <inet/ip_if.h>
82 #include <inet/ip_impl.h>
83 #include <inet/tun.h>
84 #include <inet/sctp_ip.h>
85 #include <inet/ip_netinfo.h>
86 #include <inet/mib2.h>
87 
88 #include <net/pfkeyv2.h>
89 #include <inet/ipsec_info.h>
90 #include <inet/sadb.h>
91 #include <inet/ipsec_impl.h>
92 #include <sys/iphada.h>
93 
94 
95 #include <netinet/igmp.h>
96 #include <inet/ip_listutils.h>
97 #include <inet/ipclassifier.h>
98 #include <sys/mac.h>
99 
100 #include <sys/systeminfo.h>
101 #include <sys/bootconf.h>
102 
103 #include <sys/tsol/tndb.h>
104 #include <sys/tsol/tnet.h>
105 
106 /* The character which tells where the ill_name ends */
107 #define	IPIF_SEPARATOR_CHAR	':'
108 
109 /* IP ioctl function table entry */
110 typedef struct ipft_s {
111 	int	ipft_cmd;
112 	pfi_t	ipft_pfi;
113 	int	ipft_min_size;
114 	int	ipft_flags;
115 } ipft_t;
116 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
117 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
118 
119 typedef struct ip_sock_ar_s {
120 	union {
121 		area_t	ip_sock_area;
122 		ared_t	ip_sock_ared;
123 		areq_t	ip_sock_areq;
124 	} ip_sock_ar_u;
125 	queue_t	*ip_sock_ar_q;
126 } ip_sock_ar_t;
127 
128 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
129 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
130 		    char *value, caddr_t cp, cred_t *ioc_cr);
131 
132 static boolean_t ill_is_quiescent(ill_t *);
133 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
134 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
135 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
136     mblk_t *mp, boolean_t need_up);
137 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
140     queue_t *q, mblk_t *mp, boolean_t need_up);
141 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
142     mblk_t *mp);
143 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
144     mblk_t *mp);
145 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
146     queue_t *q, mblk_t *mp, boolean_t need_up);
147 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
148     int ioccmd, struct linkblk *li, boolean_t doconsist);
149 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
150 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
151 static void	ipsq_flush(ill_t *ill);
152 
153 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
154     queue_t *q, mblk_t *mp, boolean_t need_up);
155 static void	ipsq_delete(ipsq_t *);
156 
157 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
158 		    boolean_t initialize);
159 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
160 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
161 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
162 		    boolean_t isv6);
163 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
164 static void	ipif_delete_cache_ire(ire_t *, char *);
165 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
166 static void	ipif_free(ipif_t *ipif);
167 static void	ipif_free_tail(ipif_t *ipif);
168 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
169 static void	ipif_multicast_down(ipif_t *ipif);
170 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
171 static void	ipif_set_default(ipif_t *ipif);
172 static int	ipif_set_values(queue_t *q, mblk_t *mp,
173     char *interf_name, uint_t *ppa);
174 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
175     queue_t *q);
176 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
177     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
178     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
179 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
180 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
181 
182 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
183 static int	ill_arp_off(ill_t *ill);
184 static int	ill_arp_on(ill_t *ill);
185 static void	ill_delete_interface_type(ill_if_t *);
186 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
187 static void	ill_dl_down(ill_t *ill);
188 static void	ill_down(ill_t *ill);
189 static void	ill_downi(ire_t *ire, char *ill_arg);
190 static void	ill_free_mib(ill_t *ill);
191 static void	ill_glist_delete(ill_t *);
192 static boolean_t ill_has_usable_ipif(ill_t *);
193 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
194 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
195 static void	ill_phyint_free(ill_t *ill);
196 static void	ill_phyint_reinit(ill_t *ill);
197 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
198 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
199 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
200 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
201 static void	ill_stq_cache_delete(ire_t *, char *);
202 
203 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
204 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
205 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
206     in6_addr_t *);
207 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     ipaddr_t *);
209 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
210 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
211     in6_addr_t *);
212 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
213     ipaddr_t *);
214 
215 static void	ipif_save_ire(ipif_t *, ire_t *);
216 static void	ipif_remove_ire(ipif_t *, ire_t *);
217 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
218 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
219 
220 /*
221  * Per-ill IPsec capabilities management.
222  */
223 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
224 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
225 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
226 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
227 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
228 static void ill_capability_proto(ill_t *, int, mblk_t *);
229 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
230     boolean_t);
231 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
232 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
233 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
234 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
235 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
236 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
237 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
238 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
239     dl_capability_sub_t *);
240 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
241 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
242 static void ill_capability_lso_reset(ill_t *, mblk_t **);
243 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
244 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
245 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
246 static void	ill_capability_dls_disable(ill_t *);
247 
248 static void	illgrp_cache_delete(ire_t *, char *);
249 static void	illgrp_delete(ill_t *ill);
250 static void	illgrp_reset_schednext(ill_t *ill);
251 
252 static ill_t	*ill_prev_usesrc(ill_t *);
253 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
254 static void	ill_disband_usesrc_group(ill_t *);
255 
256 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
257 
258 #ifdef DEBUG
259 static	void	ill_trace_cleanup(const ill_t *);
260 static	void	ipif_trace_cleanup(const ipif_t *);
261 #endif
262 
263 /*
264  * if we go over the memory footprint limit more than once in this msec
265  * interval, we'll start pruning aggressively.
266  */
267 int ip_min_frag_prune_time = 0;
268 
269 /*
270  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
271  * and the IPsec DOI
272  */
273 #define	MAX_IPSEC_ALGS	256
274 
275 #define	BITSPERBYTE	8
276 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
277 
278 #define	IPSEC_ALG_ENABLE(algs, algid) \
279 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
280 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
281 
282 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
283 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
284 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
285 
286 typedef uint8_t ipsec_capab_elem_t;
287 
288 /*
289  * Per-algorithm parameters.  Note that at present, only encryption
290  * algorithms have variable keysize (IKE does not provide a way to negotiate
291  * auth algorithm keysize).
292  *
293  * All sizes here are in bits.
294  */
295 typedef struct
296 {
297 	uint16_t	minkeylen;
298 	uint16_t	maxkeylen;
299 } ipsec_capab_algparm_t;
300 
301 /*
302  * Per-ill capabilities.
303  */
304 struct ill_ipsec_capab_s {
305 	ipsec_capab_elem_t *encr_hw_algs;
306 	ipsec_capab_elem_t *auth_hw_algs;
307 	uint32_t algs_size;	/* size of _hw_algs in bytes */
308 	/* algorithm key lengths */
309 	ipsec_capab_algparm_t *encr_algparm;
310 	uint32_t encr_algparm_size;
311 	uint32_t encr_algparm_end;
312 };
313 
314 /*
315  * The field values are larger than strictly necessary for simple
316  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
317  */
318 static area_t	ip_area_template = {
319 	AR_ENTRY_ADD,			/* area_cmd */
320 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
321 					/* area_name_offset */
322 	/* area_name_length temporarily holds this structure length */
323 	sizeof (area_t),			/* area_name_length */
324 	IP_ARP_PROTO_TYPE,		/* area_proto */
325 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
326 	IP_ADDR_LEN,			/* area_proto_addr_length */
327 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
328 					/* area_proto_mask_offset */
329 	0,				/* area_flags */
330 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
331 					/* area_hw_addr_offset */
332 	/* Zero length hw_addr_length means 'use your idea of the address' */
333 	0				/* area_hw_addr_length */
334 };
335 
336 /*
337  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
338  * support
339  */
340 static area_t	ip6_area_template = {
341 	AR_ENTRY_ADD,			/* area_cmd */
342 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
343 					/* area_name_offset */
344 	/* area_name_length temporarily holds this structure length */
345 	sizeof (area_t),			/* area_name_length */
346 	IP_ARP_PROTO_TYPE,		/* area_proto */
347 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
348 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
349 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
350 					/* area_proto_mask_offset */
351 	0,				/* area_flags */
352 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
353 					/* area_hw_addr_offset */
354 	/* Zero length hw_addr_length means 'use your idea of the address' */
355 	0				/* area_hw_addr_length */
356 };
357 
358 static ared_t	ip_ared_template = {
359 	AR_ENTRY_DELETE,
360 	sizeof (ared_t) + IP_ADDR_LEN,
361 	sizeof (ared_t),
362 	IP_ARP_PROTO_TYPE,
363 	sizeof (ared_t),
364 	IP_ADDR_LEN
365 };
366 
367 static ared_t	ip6_ared_template = {
368 	AR_ENTRY_DELETE,
369 	sizeof (ared_t) + IPV6_ADDR_LEN,
370 	sizeof (ared_t),
371 	IP_ARP_PROTO_TYPE,
372 	sizeof (ared_t),
373 	IPV6_ADDR_LEN
374 };
375 
376 /*
377  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
378  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
379  * areq is used).
380  */
381 static areq_t	ip_areq_template = {
382 	AR_ENTRY_QUERY,			/* cmd */
383 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
384 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
385 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
386 	sizeof (areq_t),			/* target addr offset */
387 	IP_ADDR_LEN,			/* target addr_length */
388 	0,				/* flags */
389 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
390 	IP_ADDR_LEN,			/* sender addr length */
391 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
392 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
393 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
394 	/* anything else filled in by the code */
395 };
396 
397 static arc_t	ip_aru_template = {
398 	AR_INTERFACE_UP,
399 	sizeof (arc_t),		/* Name offset */
400 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
401 };
402 
403 static arc_t	ip_ard_template = {
404 	AR_INTERFACE_DOWN,
405 	sizeof (arc_t),		/* Name offset */
406 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
407 };
408 
409 static arc_t	ip_aron_template = {
410 	AR_INTERFACE_ON,
411 	sizeof (arc_t),		/* Name offset */
412 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
413 };
414 
415 static arc_t	ip_aroff_template = {
416 	AR_INTERFACE_OFF,
417 	sizeof (arc_t),		/* Name offset */
418 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
419 };
420 
421 static arma_t	ip_arma_multi_template = {
422 	AR_MAPPING_ADD,
423 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
424 				/* Name offset */
425 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
426 	IP_ARP_PROTO_TYPE,
427 	sizeof (arma_t),			/* proto_addr_offset */
428 	IP_ADDR_LEN,				/* proto_addr_length */
429 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
430 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
431 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
432 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
433 	IP_MAX_HW_LEN,				/* hw_addr_length */
434 	0,					/* hw_mapping_start */
435 };
436 
437 static ipft_t	ip_ioctl_ftbl[] = {
438 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
439 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
440 		IPFT_F_NO_REPLY },
441 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
442 		IPFT_F_NO_REPLY },
443 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
444 	{ 0 }
445 };
446 
447 /* Simple ICMP IP Header Template */
448 static ipha_t icmp_ipha = {
449 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
450 };
451 
452 /* Flag descriptors for ip_ipif_report */
453 static nv_t	ipif_nv_tbl[] = {
454 	{ IPIF_UP,		"UP" },
455 	{ IPIF_BROADCAST,	"BROADCAST" },
456 	{ ILLF_DEBUG,		"DEBUG" },
457 	{ PHYI_LOOPBACK,	"LOOPBACK" },
458 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
459 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
460 	{ PHYI_RUNNING,		"RUNNING" },
461 	{ ILLF_NOARP,		"NOARP" },
462 	{ PHYI_PROMISC,		"PROMISC" },
463 	{ PHYI_ALLMULTI,	"ALLMULTI" },
464 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
465 	{ ILLF_MULTICAST,	"MULTICAST" },
466 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
467 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
468 	{ IPIF_DHCPRUNNING,	"DHCP" },
469 	{ IPIF_PRIVATE,		"PRIVATE" },
470 	{ IPIF_NOXMIT,		"NOXMIT" },
471 	{ IPIF_NOLOCAL,		"NOLOCAL" },
472 	{ IPIF_DEPRECATED,	"DEPRECATED" },
473 	{ IPIF_PREFERRED,	"PREFERRED" },
474 	{ IPIF_TEMPORARY,	"TEMPORARY" },
475 	{ IPIF_ADDRCONF,	"ADDRCONF" },
476 	{ PHYI_VIRTUAL,		"VIRTUAL" },
477 	{ ILLF_ROUTER,		"ROUTER" },
478 	{ ILLF_NONUD,		"NONUD" },
479 	{ IPIF_ANYCAST,		"ANYCAST" },
480 	{ ILLF_NORTEXCH,	"NORTEXCH" },
481 	{ ILLF_IPV4,		"IPV4" },
482 	{ ILLF_IPV6,		"IPV6" },
483 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
484 	{ PHYI_FAILED,		"FAILED" },
485 	{ PHYI_STANDBY,		"STANDBY" },
486 	{ PHYI_INACTIVE,	"INACTIVE" },
487 	{ PHYI_OFFLINE,		"OFFLINE" },
488 };
489 
490 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
491 
492 static ip_m_t	ip_m_tbl[] = {
493 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
494 	    ip_ether_v6intfid },
495 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
496 	    ip_nodef_v6intfid },
497 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_nodef_v6intfid },
499 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_nodef_v6intfid },
501 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
502 	    ip_ether_v6intfid },
503 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
504 	    ip_ib_v6intfid },
505 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
506 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
507 	    ip_nodef_v6intfid }
508 };
509 
510 static ill_t	ill_null;		/* Empty ILL for init. */
511 char	ipif_loopback_name[] = "lo0";
512 static char *ipv4_forward_suffix = ":ip_forwarding";
513 static char *ipv6_forward_suffix = ":ip6_forwarding";
514 static	sin6_t	sin6_null;	/* Zero address for quick clears */
515 static	sin_t	sin_null;	/* Zero address for quick clears */
516 
517 /* When set search for unused ipif_seqid */
518 static ipif_t	ipif_zero;
519 
520 /*
521  * ppa arena is created after these many
522  * interfaces have been plumbed.
523  */
524 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
525 
526 /*
527  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
528  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
529  * set through platform specific code (Niagara/Ontario).
530  */
531 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
532 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
533 
534 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
535 
536 static uint_t
537 ipif_rand(ip_stack_t *ipst)
538 {
539 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
540 	    12345;
541 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
542 }
543 
544 /*
545  * Allocate per-interface mibs.
546  * Returns true if ok. False otherwise.
547  *  ipsq  may not yet be allocated (loopback case ).
548  */
549 static boolean_t
550 ill_allocate_mibs(ill_t *ill)
551 {
552 	/* Already allocated? */
553 	if (ill->ill_ip_mib != NULL) {
554 		if (ill->ill_isv6)
555 			ASSERT(ill->ill_icmp6_mib != NULL);
556 		return (B_TRUE);
557 	}
558 
559 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
560 	    KM_NOSLEEP);
561 	if (ill->ill_ip_mib == NULL) {
562 		return (B_FALSE);
563 	}
564 
565 	/* Setup static information */
566 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
567 	    sizeof (mib2_ipIfStatsEntry_t));
568 	if (ill->ill_isv6) {
569 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
570 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
571 		    sizeof (mib2_ipv6AddrEntry_t));
572 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
573 		    sizeof (mib2_ipv6RouteEntry_t));
574 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
575 		    sizeof (mib2_ipv6NetToMediaEntry_t));
576 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
577 		    sizeof (ipv6_member_t));
578 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
579 		    sizeof (ipv6_grpsrc_t));
580 	} else {
581 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
582 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
583 		    sizeof (mib2_ipAddrEntry_t));
584 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
585 		    sizeof (mib2_ipRouteEntry_t));
586 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
587 		    sizeof (mib2_ipNetToMediaEntry_t));
588 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
589 		    sizeof (ip_member_t));
590 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
591 		    sizeof (ip_grpsrc_t));
592 
593 		/*
594 		 * For a v4 ill, we are done at this point, because per ill
595 		 * icmp mibs are only used for v6.
596 		 */
597 		return (B_TRUE);
598 	}
599 
600 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
601 	    KM_NOSLEEP);
602 	if (ill->ill_icmp6_mib == NULL) {
603 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
604 		ill->ill_ip_mib = NULL;
605 		return (B_FALSE);
606 	}
607 	/* static icmp info */
608 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
609 	    sizeof (mib2_ipv6IfIcmpEntry_t);
610 	/*
611 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
612 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
613 	 * -> ill_phyint_reinit
614 	 */
615 	return (B_TRUE);
616 }
617 
618 /*
619  * Common code for preparation of ARP commands.  Two points to remember:
620  * 	1) The ill_name is tacked on at the end of the allocated space so
621  *	   the templates name_offset field must contain the total space
622  *	   to allocate less the name length.
623  *
624  *	2) The templates name_length field should contain the *template*
625  *	   length.  We use it as a parameter to bcopy() and then write
626  *	   the real ill_name_length into the name_length field of the copy.
627  * (Always called as writer.)
628  */
629 mblk_t *
630 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
631 {
632 	arc_t	*arc = (arc_t *)template;
633 	char	*cp;
634 	int	len;
635 	mblk_t	*mp;
636 	uint_t	name_length = ill->ill_name_length;
637 	uint_t	template_len = arc->arc_name_length;
638 
639 	len = arc->arc_name_offset + name_length;
640 	mp = allocb(len, BPRI_HI);
641 	if (mp == NULL)
642 		return (NULL);
643 	cp = (char *)mp->b_rptr;
644 	mp->b_wptr = (uchar_t *)&cp[len];
645 	if (template_len)
646 		bcopy(template, cp, template_len);
647 	if (len > template_len)
648 		bzero(&cp[template_len], len - template_len);
649 	mp->b_datap->db_type = M_PROTO;
650 
651 	arc = (arc_t *)cp;
652 	arc->arc_name_length = name_length;
653 	cp = (char *)arc + arc->arc_name_offset;
654 	bcopy(ill->ill_name, cp, name_length);
655 
656 	if (addr) {
657 		area_t	*area = (area_t *)mp->b_rptr;
658 
659 		cp = (char *)area + area->area_proto_addr_offset;
660 		bcopy(addr, cp, area->area_proto_addr_length);
661 		if (area->area_cmd == AR_ENTRY_ADD) {
662 			cp = (char *)area;
663 			len = area->area_proto_addr_length;
664 			if (area->area_proto_mask_offset)
665 				cp += area->area_proto_mask_offset;
666 			else
667 				cp += area->area_proto_addr_offset + len;
668 			while (len-- > 0)
669 				*cp++ = (char)~0;
670 		}
671 	}
672 	return (mp);
673 }
674 
675 mblk_t *
676 ipif_area_alloc(ipif_t *ipif)
677 {
678 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
679 	    (char *)&ipif->ipif_lcl_addr));
680 }
681 
682 mblk_t *
683 ipif_ared_alloc(ipif_t *ipif)
684 {
685 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
686 	    (char *)&ipif->ipif_lcl_addr));
687 }
688 
689 mblk_t *
690 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
691 {
692 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
693 	    (char *)&addr));
694 }
695 
696 /*
697  * Completely vaporize a lower level tap and all associated interfaces.
698  * ill_delete is called only out of ip_close when the device control
699  * stream is being closed.
700  */
701 void
702 ill_delete(ill_t *ill)
703 {
704 	ipif_t	*ipif;
705 	ill_t	*prev_ill;
706 	ip_stack_t	*ipst = ill->ill_ipst;
707 
708 	/*
709 	 * ill_delete may be forcibly entering the ipsq. The previous
710 	 * ioctl may not have completed and may need to be aborted.
711 	 * ipsq_flush takes care of it. If we don't need to enter the
712 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
713 	 * ill_delete_tail is sufficient.
714 	 */
715 	ipsq_flush(ill);
716 
717 	/*
718 	 * Nuke all interfaces.  ipif_free will take down the interface,
719 	 * remove it from the list, and free the data structure.
720 	 * Walk down the ipif list and remove the logical interfaces
721 	 * first before removing the main ipif. We can't unplumb
722 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
723 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
724 	 * POINTOPOINT.
725 	 *
726 	 * If ill_ipif was not properly initialized (i.e low on memory),
727 	 * then no interfaces to clean up. In this case just clean up the
728 	 * ill.
729 	 */
730 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
731 		ipif_free(ipif);
732 
733 	/*
734 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
735 	 * So nobody can be using this mp now. Free the mp allocated for
736 	 * honoring ILLF_NOARP
737 	 */
738 	freemsg(ill->ill_arp_on_mp);
739 	ill->ill_arp_on_mp = NULL;
740 
741 	/* Clean up msgs on pending upcalls for mrouted */
742 	reset_mrt_ill(ill);
743 
744 	/*
745 	 * ipif_free -> reset_conn_ipif will remove all multicast
746 	 * references for IPv4. For IPv6, we need to do it here as
747 	 * it points only at ills.
748 	 */
749 	reset_conn_ill(ill);
750 
751 	/*
752 	 * ill_down will arrange to blow off any IRE's dependent on this
753 	 * ILL, and shut down fragmentation reassembly.
754 	 */
755 	ill_down(ill);
756 
757 	/* Let SCTP know, so that it can remove this from its list. */
758 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
759 
760 	/*
761 	 * If an address on this ILL is being used as a source address then
762 	 * clear out the pointers in other ILLs that point to this ILL.
763 	 */
764 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
765 	if (ill->ill_usesrc_grp_next != NULL) {
766 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
767 			ill_disband_usesrc_group(ill);
768 		} else {	/* consumer of the usesrc ILL */
769 			prev_ill = ill_prev_usesrc(ill);
770 			prev_ill->ill_usesrc_grp_next =
771 			    ill->ill_usesrc_grp_next;
772 		}
773 	}
774 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
775 }
776 
777 static void
778 ipif_non_duplicate(ipif_t *ipif)
779 {
780 	ill_t *ill = ipif->ipif_ill;
781 	mutex_enter(&ill->ill_lock);
782 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
783 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
784 		ASSERT(ill->ill_ipif_dup_count > 0);
785 		ill->ill_ipif_dup_count--;
786 	}
787 	mutex_exit(&ill->ill_lock);
788 }
789 
790 /*
791  * ill_delete_tail is called from ip_modclose after all references
792  * to the closing ill are gone. The wait is done in ip_modclose
793  */
794 void
795 ill_delete_tail(ill_t *ill)
796 {
797 	mblk_t	**mpp;
798 	ipif_t	*ipif;
799 	ip_stack_t	*ipst = ill->ill_ipst;
800 
801 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
802 		ipif_non_duplicate(ipif);
803 		ipif_down_tail(ipif);
804 	}
805 
806 	ASSERT(ill->ill_ipif_dup_count == 0 &&
807 	    ill->ill_arp_down_mp == NULL &&
808 	    ill->ill_arp_del_mapping_mp == NULL);
809 
810 	/*
811 	 * If polling capability is enabled (which signifies direct
812 	 * upcall into IP and driver has ill saved as a handle),
813 	 * we need to make sure that unbind has completed before we
814 	 * let the ill disappear and driver no longer has any reference
815 	 * to this ill.
816 	 */
817 	mutex_enter(&ill->ill_lock);
818 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
819 		cv_wait(&ill->ill_cv, &ill->ill_lock);
820 	mutex_exit(&ill->ill_lock);
821 
822 	/*
823 	 * Clean up polling and soft ring capabilities
824 	 */
825 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
826 		ill_capability_dls_disable(ill);
827 
828 	if (ill->ill_net_type != IRE_LOOPBACK)
829 		qprocsoff(ill->ill_rq);
830 
831 	/*
832 	 * We do an ipsq_flush once again now. New messages could have
833 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
834 	 * could also have landed up if an ioctl thread had looked up
835 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
836 	 * enqueued the ioctl when we did the ipsq_flush last time.
837 	 */
838 	ipsq_flush(ill);
839 
840 	/*
841 	 * Free capabilities.
842 	 */
843 	if (ill->ill_ipsec_capab_ah != NULL) {
844 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
845 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
846 		ill->ill_ipsec_capab_ah = NULL;
847 	}
848 
849 	if (ill->ill_ipsec_capab_esp != NULL) {
850 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
851 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
852 		ill->ill_ipsec_capab_esp = NULL;
853 	}
854 
855 	if (ill->ill_mdt_capab != NULL) {
856 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
857 		ill->ill_mdt_capab = NULL;
858 	}
859 
860 	if (ill->ill_hcksum_capab != NULL) {
861 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
862 		ill->ill_hcksum_capab = NULL;
863 	}
864 
865 	if (ill->ill_zerocopy_capab != NULL) {
866 		kmem_free(ill->ill_zerocopy_capab,
867 		    sizeof (ill_zerocopy_capab_t));
868 		ill->ill_zerocopy_capab = NULL;
869 	}
870 
871 	if (ill->ill_lso_capab != NULL) {
872 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
873 		ill->ill_lso_capab = NULL;
874 	}
875 
876 	if (ill->ill_dls_capab != NULL) {
877 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
878 		ill->ill_dls_capab->ill_unbind_conn = NULL;
879 		kmem_free(ill->ill_dls_capab,
880 		    sizeof (ill_dls_capab_t) +
881 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
882 		ill->ill_dls_capab = NULL;
883 	}
884 
885 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
886 
887 	while (ill->ill_ipif != NULL)
888 		ipif_free_tail(ill->ill_ipif);
889 
890 	/*
891 	 * We have removed all references to ilm from conn and the ones joined
892 	 * within the kernel.
893 	 *
894 	 * We don't walk conns, mrts and ires because
895 	 *
896 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
897 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
898 	 *    ill references.
899 	 */
900 	ASSERT(ilm_walk_ill(ill) == 0);
901 	/*
902 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
903 	 * could free the phyint. No more reference to the phyint after this
904 	 * point.
905 	 */
906 	(void) ill_glist_delete(ill);
907 
908 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
909 	if (ill->ill_ndd_name != NULL)
910 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
911 	rw_exit(&ipst->ips_ip_g_nd_lock);
912 
913 	if (ill->ill_frag_ptr != NULL) {
914 		uint_t count;
915 
916 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
917 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
918 		}
919 		mi_free(ill->ill_frag_ptr);
920 		ill->ill_frag_ptr = NULL;
921 		ill->ill_frag_hash_tbl = NULL;
922 	}
923 
924 	freemsg(ill->ill_nd_lla_mp);
925 	/* Free all retained control messages. */
926 	mpp = &ill->ill_first_mp_to_free;
927 	do {
928 		while (mpp[0]) {
929 			mblk_t  *mp;
930 			mblk_t  *mp1;
931 
932 			mp = mpp[0];
933 			mpp[0] = mp->b_next;
934 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
935 				mp1->b_next = NULL;
936 				mp1->b_prev = NULL;
937 			}
938 			freemsg(mp);
939 		}
940 	} while (mpp++ != &ill->ill_last_mp_to_free);
941 
942 	ill_free_mib(ill);
943 
944 #ifdef DEBUG
945 	ill_trace_cleanup(ill);
946 #endif
947 
948 	/* Drop refcnt here */
949 	netstack_rele(ill->ill_ipst->ips_netstack);
950 	ill->ill_ipst = NULL;
951 }
952 
953 static void
954 ill_free_mib(ill_t *ill)
955 {
956 	ip_stack_t *ipst = ill->ill_ipst;
957 
958 	/*
959 	 * MIB statistics must not be lost, so when an interface
960 	 * goes away the counter values will be added to the global
961 	 * MIBs.
962 	 */
963 	if (ill->ill_ip_mib != NULL) {
964 		if (ill->ill_isv6) {
965 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
966 			    ill->ill_ip_mib);
967 		} else {
968 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
969 			    ill->ill_ip_mib);
970 		}
971 
972 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
973 		ill->ill_ip_mib = NULL;
974 	}
975 	if (ill->ill_icmp6_mib != NULL) {
976 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
977 		    ill->ill_icmp6_mib);
978 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
979 		ill->ill_icmp6_mib = NULL;
980 	}
981 }
982 
983 /*
984  * Concatenate together a physical address and a sap.
985  *
986  * Sap_lengths are interpreted as follows:
987  *   sap_length == 0	==>	no sap
988  *   sap_length > 0	==>	sap is at the head of the dlpi address
989  *   sap_length < 0	==>	sap is at the tail of the dlpi address
990  */
991 static void
992 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
993     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
994 {
995 	uint16_t sap_addr = (uint16_t)sap_src;
996 
997 	if (sap_length == 0) {
998 		if (phys_src == NULL)
999 			bzero(dst, phys_length);
1000 		else
1001 			bcopy(phys_src, dst, phys_length);
1002 	} else if (sap_length < 0) {
1003 		if (phys_src == NULL)
1004 			bzero(dst, phys_length);
1005 		else
1006 			bcopy(phys_src, dst, phys_length);
1007 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1008 	} else {
1009 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1010 		if (phys_src == NULL)
1011 			bzero((char *)dst + sap_length, phys_length);
1012 		else
1013 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1014 	}
1015 }
1016 
1017 /*
1018  * Generate a dl_unitdata_req mblk for the device and address given.
1019  * addr_length is the length of the physical portion of the address.
1020  * If addr is NULL include an all zero address of the specified length.
1021  * TRUE? In any case, addr_length is taken to be the entire length of the
1022  * dlpi address, including the absolute value of sap_length.
1023  */
1024 mblk_t *
1025 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1026 		t_scalar_t sap_length)
1027 {
1028 	dl_unitdata_req_t *dlur;
1029 	mblk_t	*mp;
1030 	t_scalar_t	abs_sap_length;		/* absolute value */
1031 
1032 	abs_sap_length = ABS(sap_length);
1033 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1034 	    DL_UNITDATA_REQ);
1035 	if (mp == NULL)
1036 		return (NULL);
1037 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1038 	/* HACK: accomodate incompatible DLPI drivers */
1039 	if (addr_length == 8)
1040 		addr_length = 6;
1041 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1042 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1043 	dlur->dl_priority.dl_min = 0;
1044 	dlur->dl_priority.dl_max = 0;
1045 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1046 	    (uchar_t *)&dlur[1]);
1047 	return (mp);
1048 }
1049 
1050 /*
1051  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1052  * Return an error if we already have 1 or more ioctls in progress.
1053  * This is used only for non-exclusive ioctls. Currently this is used
1054  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1055  * and thus need to use ipsq_pending_mp_add.
1056  */
1057 boolean_t
1058 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1059 {
1060 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1061 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1062 	/*
1063 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1064 	 */
1065 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1066 	    (add_mp->b_datap->db_type == M_IOCTL));
1067 
1068 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1069 	/*
1070 	 * Return error if the conn has started closing. The conn
1071 	 * could have finished cleaning up the pending mp list,
1072 	 * If so we should not add another mp to the list negating
1073 	 * the cleanup.
1074 	 */
1075 	if (connp->conn_state_flags & CONN_CLOSING)
1076 		return (B_FALSE);
1077 	/*
1078 	 * Add the pending mp to the head of the list, chained by b_next.
1079 	 * Note down the conn on which the ioctl request came, in b_prev.
1080 	 * This will be used to later get the conn, when we get a response
1081 	 * on the ill queue, from some other module (typically arp)
1082 	 */
1083 	add_mp->b_next = (void *)ill->ill_pending_mp;
1084 	add_mp->b_queue = CONNP_TO_WQ(connp);
1085 	ill->ill_pending_mp = add_mp;
1086 	if (connp != NULL)
1087 		connp->conn_oper_pending_ill = ill;
1088 	return (B_TRUE);
1089 }
1090 
1091 /*
1092  * Retrieve the ill_pending_mp and return it. We have to walk the list
1093  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1094  */
1095 mblk_t *
1096 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1097 {
1098 	mblk_t	*prev = NULL;
1099 	mblk_t	*curr = NULL;
1100 	uint_t	id;
1101 	conn_t	*connp;
1102 
1103 	/*
1104 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1105 	 * up the pending mp, but it does not know the ioc_id and
1106 	 * passes in a zero for it.
1107 	 */
1108 	mutex_enter(&ill->ill_lock);
1109 	if (ioc_id != 0)
1110 		*connpp = NULL;
1111 
1112 	/* Search the list for the appropriate ioctl based on ioc_id */
1113 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1114 	    prev = curr, curr = curr->b_next) {
1115 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1116 		connp = Q_TO_CONN(curr->b_queue);
1117 		/* Match based on the ioc_id or based on the conn */
1118 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1119 			break;
1120 	}
1121 
1122 	if (curr != NULL) {
1123 		/* Unlink the mblk from the pending mp list */
1124 		if (prev != NULL) {
1125 			prev->b_next = curr->b_next;
1126 		} else {
1127 			ASSERT(ill->ill_pending_mp == curr);
1128 			ill->ill_pending_mp = curr->b_next;
1129 		}
1130 
1131 		/*
1132 		 * conn refcnt must have been bumped up at the start of
1133 		 * the ioctl. So we can safely access the conn.
1134 		 */
1135 		ASSERT(CONN_Q(curr->b_queue));
1136 		*connpp = Q_TO_CONN(curr->b_queue);
1137 		curr->b_next = NULL;
1138 		curr->b_queue = NULL;
1139 	}
1140 
1141 	mutex_exit(&ill->ill_lock);
1142 
1143 	return (curr);
1144 }
1145 
1146 /*
1147  * Add the pending mp to the list. There can be only 1 pending mp
1148  * in the list. Any exclusive ioctl that needs to wait for a response
1149  * from another module or driver needs to use this function to set
1150  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1151  * the other module/driver. This is also used while waiting for the
1152  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1153  */
1154 boolean_t
1155 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1156     int waitfor)
1157 {
1158 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1159 
1160 	ASSERT(IAM_WRITER_IPIF(ipif));
1161 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1162 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1163 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1164 	/*
1165 	 * The caller may be using a different ipif than the one passed into
1166 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1167 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1168 	 * that `ipsq_current_ipif == ipif'.
1169 	 */
1170 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1171 
1172 	/*
1173 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1174 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1175 	 */
1176 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1177 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1178 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1179 
1180 	if (connp != NULL) {
1181 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1182 		/*
1183 		 * Return error if the conn has started closing. The conn
1184 		 * could have finished cleaning up the pending mp list,
1185 		 * If so we should not add another mp to the list negating
1186 		 * the cleanup.
1187 		 */
1188 		if (connp->conn_state_flags & CONN_CLOSING)
1189 			return (B_FALSE);
1190 	}
1191 	mutex_enter(&ipsq->ipsq_lock);
1192 	ipsq->ipsq_pending_ipif = ipif;
1193 	/*
1194 	 * Note down the queue in b_queue. This will be returned by
1195 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1196 	 * the processing
1197 	 */
1198 	add_mp->b_next = NULL;
1199 	add_mp->b_queue = q;
1200 	ipsq->ipsq_pending_mp = add_mp;
1201 	ipsq->ipsq_waitfor = waitfor;
1202 
1203 	if (connp != NULL)
1204 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1205 	mutex_exit(&ipsq->ipsq_lock);
1206 	return (B_TRUE);
1207 }
1208 
1209 /*
1210  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1211  * queued in the list.
1212  */
1213 mblk_t *
1214 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1215 {
1216 	mblk_t	*curr = NULL;
1217 
1218 	mutex_enter(&ipsq->ipsq_lock);
1219 	*connpp = NULL;
1220 	if (ipsq->ipsq_pending_mp == NULL) {
1221 		mutex_exit(&ipsq->ipsq_lock);
1222 		return (NULL);
1223 	}
1224 
1225 	/* There can be only 1 such excl message */
1226 	curr = ipsq->ipsq_pending_mp;
1227 	ASSERT(curr != NULL && curr->b_next == NULL);
1228 	ipsq->ipsq_pending_ipif = NULL;
1229 	ipsq->ipsq_pending_mp = NULL;
1230 	ipsq->ipsq_waitfor = 0;
1231 	mutex_exit(&ipsq->ipsq_lock);
1232 
1233 	if (CONN_Q(curr->b_queue)) {
1234 		/*
1235 		 * This mp did a refhold on the conn, at the start of the ioctl.
1236 		 * So we can safely return a pointer to the conn to the caller.
1237 		 */
1238 		*connpp = Q_TO_CONN(curr->b_queue);
1239 	} else {
1240 		*connpp = NULL;
1241 	}
1242 	curr->b_next = NULL;
1243 	curr->b_prev = NULL;
1244 	return (curr);
1245 }
1246 
1247 /*
1248  * Cleanup the ioctl mp queued in ipsq_pending_mp
1249  * - Called in the ill_delete path
1250  * - Called in the M_ERROR or M_HANGUP path on the ill.
1251  * - Called in the conn close path.
1252  */
1253 boolean_t
1254 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1255 {
1256 	mblk_t	*mp;
1257 	ipsq_t	*ipsq;
1258 	queue_t	*q;
1259 	ipif_t	*ipif;
1260 
1261 	ASSERT(IAM_WRITER_ILL(ill));
1262 	ipsq = ill->ill_phyint->phyint_ipsq;
1263 	mutex_enter(&ipsq->ipsq_lock);
1264 	/*
1265 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1266 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1267 	 * even if it is meant for another ill, since we have to enqueue
1268 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1269 	 * If connp is non-null we are called from the conn close path.
1270 	 */
1271 	mp = ipsq->ipsq_pending_mp;
1272 	if (mp == NULL || (connp != NULL &&
1273 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1274 		mutex_exit(&ipsq->ipsq_lock);
1275 		return (B_FALSE);
1276 	}
1277 	/* Now remove from the ipsq_pending_mp */
1278 	ipsq->ipsq_pending_mp = NULL;
1279 	q = mp->b_queue;
1280 	mp->b_next = NULL;
1281 	mp->b_prev = NULL;
1282 	mp->b_queue = NULL;
1283 
1284 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1285 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1286 	if (ill->ill_move_in_progress) {
1287 		ILL_CLEAR_MOVE(ill);
1288 	} else if (ill->ill_up_ipifs) {
1289 		ill_group_cleanup(ill);
1290 	}
1291 
1292 	ipif = ipsq->ipsq_pending_ipif;
1293 	ipsq->ipsq_pending_ipif = NULL;
1294 	ipsq->ipsq_waitfor = 0;
1295 	ipsq->ipsq_current_ipif = NULL;
1296 	ipsq->ipsq_current_ioctl = 0;
1297 	ipsq->ipsq_current_done = B_TRUE;
1298 	mutex_exit(&ipsq->ipsq_lock);
1299 
1300 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1301 		if (connp == NULL) {
1302 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1303 		} else {
1304 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1305 			mutex_enter(&ipif->ipif_ill->ill_lock);
1306 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1307 			mutex_exit(&ipif->ipif_ill->ill_lock);
1308 		}
1309 	} else {
1310 		/*
1311 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1312 		 * be just inet_freemsg. we have to restart it
1313 		 * otherwise the thread will be stuck.
1314 		 */
1315 		inet_freemsg(mp);
1316 	}
1317 	return (B_TRUE);
1318 }
1319 
1320 /*
1321  * The ill is closing. Cleanup all the pending mps. Called exclusively
1322  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1323  * knows this ill, and hence nobody can add an mp to this list
1324  */
1325 static void
1326 ill_pending_mp_cleanup(ill_t *ill)
1327 {
1328 	mblk_t	*mp;
1329 	queue_t	*q;
1330 
1331 	ASSERT(IAM_WRITER_ILL(ill));
1332 
1333 	mutex_enter(&ill->ill_lock);
1334 	/*
1335 	 * Every mp on the pending mp list originating from an ioctl
1336 	 * added 1 to the conn refcnt, at the start of the ioctl.
1337 	 * So bump it down now.  See comments in ip_wput_nondata()
1338 	 */
1339 	while (ill->ill_pending_mp != NULL) {
1340 		mp = ill->ill_pending_mp;
1341 		ill->ill_pending_mp = mp->b_next;
1342 		mutex_exit(&ill->ill_lock);
1343 
1344 		q = mp->b_queue;
1345 		ASSERT(CONN_Q(q));
1346 		mp->b_next = NULL;
1347 		mp->b_prev = NULL;
1348 		mp->b_queue = NULL;
1349 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1350 		mutex_enter(&ill->ill_lock);
1351 	}
1352 	ill->ill_pending_ipif = NULL;
1353 
1354 	mutex_exit(&ill->ill_lock);
1355 }
1356 
1357 /*
1358  * Called in the conn close path and ill delete path
1359  */
1360 static void
1361 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1362 {
1363 	ipsq_t	*ipsq;
1364 	mblk_t	*prev;
1365 	mblk_t	*curr;
1366 	mblk_t	*next;
1367 	queue_t	*q;
1368 	mblk_t	*tmp_list = NULL;
1369 
1370 	ASSERT(IAM_WRITER_ILL(ill));
1371 	if (connp != NULL)
1372 		q = CONNP_TO_WQ(connp);
1373 	else
1374 		q = ill->ill_wq;
1375 
1376 	ipsq = ill->ill_phyint->phyint_ipsq;
1377 	/*
1378 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1379 	 * In the case of ioctl from a conn, there can be only 1 mp
1380 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1381 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1382 	 * ioctls meant for this ill form conn's are not flushed. They will
1383 	 * be processed during ipsq_exit and will not find the ill and will
1384 	 * return error.
1385 	 */
1386 	mutex_enter(&ipsq->ipsq_lock);
1387 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1388 	    curr = next) {
1389 		next = curr->b_next;
1390 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1391 			/* Unlink the mblk from the pending mp list */
1392 			if (prev != NULL) {
1393 				prev->b_next = curr->b_next;
1394 			} else {
1395 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1396 				ipsq->ipsq_xopq_mphead = curr->b_next;
1397 			}
1398 			if (ipsq->ipsq_xopq_mptail == curr)
1399 				ipsq->ipsq_xopq_mptail = prev;
1400 			/*
1401 			 * Create a temporary list and release the ipsq lock
1402 			 * New elements are added to the head of the tmp_list
1403 			 */
1404 			curr->b_next = tmp_list;
1405 			tmp_list = curr;
1406 		} else {
1407 			prev = curr;
1408 		}
1409 	}
1410 	mutex_exit(&ipsq->ipsq_lock);
1411 
1412 	while (tmp_list != NULL) {
1413 		curr = tmp_list;
1414 		tmp_list = curr->b_next;
1415 		curr->b_next = NULL;
1416 		curr->b_prev = NULL;
1417 		curr->b_queue = NULL;
1418 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1419 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1420 			    CONN_CLOSE : NO_COPYOUT, NULL);
1421 		} else {
1422 			/*
1423 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1424 			 * this can't be just inet_freemsg. we have to
1425 			 * restart it otherwise the thread will be stuck.
1426 			 */
1427 			inet_freemsg(curr);
1428 		}
1429 	}
1430 }
1431 
1432 /*
1433  * This conn has started closing. Cleanup any pending ioctl from this conn.
1434  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1435  */
1436 void
1437 conn_ioctl_cleanup(conn_t *connp)
1438 {
1439 	mblk_t *curr;
1440 	ipsq_t	*ipsq;
1441 	ill_t	*ill;
1442 	boolean_t refheld;
1443 
1444 	/*
1445 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1446 	 * ioctl has not yet started, the mp is pending in the list headed by
1447 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1448 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1449 	 * is currently executing now the mp is not queued anywhere but
1450 	 * conn_oper_pending_ill is null. The conn close will wait
1451 	 * till the conn_ref drops to zero.
1452 	 */
1453 	mutex_enter(&connp->conn_lock);
1454 	ill = connp->conn_oper_pending_ill;
1455 	if (ill == NULL) {
1456 		mutex_exit(&connp->conn_lock);
1457 		return;
1458 	}
1459 
1460 	curr = ill_pending_mp_get(ill, &connp, 0);
1461 	if (curr != NULL) {
1462 		mutex_exit(&connp->conn_lock);
1463 		CONN_DEC_REF(connp);
1464 		inet_freemsg(curr);
1465 		return;
1466 	}
1467 	/*
1468 	 * We may not be able to refhold the ill if the ill/ipif
1469 	 * is changing. But we need to make sure that the ill will
1470 	 * not vanish. So we just bump up the ill_waiter count.
1471 	 */
1472 	refheld = ill_waiter_inc(ill);
1473 	mutex_exit(&connp->conn_lock);
1474 	if (refheld) {
1475 		if (ipsq_enter(ill, B_TRUE)) {
1476 			ill_waiter_dcr(ill);
1477 			/*
1478 			 * Check whether this ioctl has started and is
1479 			 * pending now in ipsq_pending_mp. If it is not
1480 			 * found there then check whether this ioctl has
1481 			 * not even started and is in the ipsq_xopq list.
1482 			 */
1483 			if (!ipsq_pending_mp_cleanup(ill, connp))
1484 				ipsq_xopq_mp_cleanup(ill, connp);
1485 			ipsq = ill->ill_phyint->phyint_ipsq;
1486 			ipsq_exit(ipsq);
1487 			return;
1488 		}
1489 	}
1490 
1491 	/*
1492 	 * The ill is also closing and we could not bump up the
1493 	 * ill_waiter_count or we could not enter the ipsq. Leave
1494 	 * the cleanup to ill_delete
1495 	 */
1496 	mutex_enter(&connp->conn_lock);
1497 	while (connp->conn_oper_pending_ill != NULL)
1498 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1499 	mutex_exit(&connp->conn_lock);
1500 	if (refheld)
1501 		ill_waiter_dcr(ill);
1502 }
1503 
1504 /*
1505  * ipcl_walk function for cleaning up conn_*_ill fields.
1506  */
1507 static void
1508 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1509 {
1510 	ill_t	*ill = (ill_t *)arg;
1511 	ire_t	*ire;
1512 
1513 	mutex_enter(&connp->conn_lock);
1514 	if (connp->conn_multicast_ill == ill) {
1515 		/* Revert to late binding */
1516 		connp->conn_multicast_ill = NULL;
1517 		connp->conn_orig_multicast_ifindex = 0;
1518 	}
1519 	if (connp->conn_incoming_ill == ill)
1520 		connp->conn_incoming_ill = NULL;
1521 	if (connp->conn_outgoing_ill == ill)
1522 		connp->conn_outgoing_ill = NULL;
1523 	if (connp->conn_outgoing_pill == ill)
1524 		connp->conn_outgoing_pill = NULL;
1525 	if (connp->conn_nofailover_ill == ill)
1526 		connp->conn_nofailover_ill = NULL;
1527 	if (connp->conn_dhcpinit_ill == ill) {
1528 		connp->conn_dhcpinit_ill = NULL;
1529 		ASSERT(ill->ill_dhcpinit != 0);
1530 		atomic_dec_32(&ill->ill_dhcpinit);
1531 	}
1532 	if (connp->conn_ire_cache != NULL) {
1533 		ire = connp->conn_ire_cache;
1534 		/*
1535 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1536 		 * interface X and ipif coming from interface Y, if interface
1537 		 * X and Y are part of the same IPMPgroup. Thus whenever
1538 		 * interface X goes down, remove all references to it by
1539 		 * checking both on ire_ipif and ire_stq.
1540 		 */
1541 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1542 		    (ire->ire_type == IRE_CACHE &&
1543 		    ire->ire_stq == ill->ill_wq)) {
1544 			connp->conn_ire_cache = NULL;
1545 			mutex_exit(&connp->conn_lock);
1546 			ire_refrele_notr(ire);
1547 			return;
1548 		}
1549 	}
1550 	mutex_exit(&connp->conn_lock);
1551 }
1552 
1553 /* ARGSUSED */
1554 void
1555 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1556 {
1557 	ill_t	*ill = q->q_ptr;
1558 	ipif_t	*ipif;
1559 
1560 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1561 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1562 		ipif_non_duplicate(ipif);
1563 		ipif_down_tail(ipif);
1564 	}
1565 	freemsg(mp);
1566 	ipsq_current_finish(ipsq);
1567 }
1568 
1569 /*
1570  * ill_down_start is called when we want to down this ill and bring it up again
1571  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1572  * all interfaces, but don't tear down any plumbing.
1573  */
1574 boolean_t
1575 ill_down_start(queue_t *q, mblk_t *mp)
1576 {
1577 	ill_t	*ill = q->q_ptr;
1578 	ipif_t	*ipif;
1579 
1580 	ASSERT(IAM_WRITER_ILL(ill));
1581 
1582 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1583 		(void) ipif_down(ipif, NULL, NULL);
1584 
1585 	ill_down(ill);
1586 
1587 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1588 
1589 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1590 
1591 	/*
1592 	 * Atomically test and add the pending mp if references are active.
1593 	 */
1594 	mutex_enter(&ill->ill_lock);
1595 	if (!ill_is_quiescent(ill)) {
1596 		/* call cannot fail since `conn_t *' argument is NULL */
1597 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1598 		    mp, ILL_DOWN);
1599 		mutex_exit(&ill->ill_lock);
1600 		return (B_FALSE);
1601 	}
1602 	mutex_exit(&ill->ill_lock);
1603 	return (B_TRUE);
1604 }
1605 
1606 static void
1607 ill_down(ill_t *ill)
1608 {
1609 	ip_stack_t	*ipst = ill->ill_ipst;
1610 
1611 	/* Blow off any IREs dependent on this ILL. */
1612 	ire_walk(ill_downi, (char *)ill, ipst);
1613 
1614 	/* Remove any conn_*_ill depending on this ill */
1615 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1616 
1617 	if (ill->ill_group != NULL) {
1618 		illgrp_delete(ill);
1619 	}
1620 }
1621 
1622 /*
1623  * ire_walk routine used to delete every IRE that depends on queues
1624  * associated with 'ill'.  (Always called as writer.)
1625  */
1626 static void
1627 ill_downi(ire_t *ire, char *ill_arg)
1628 {
1629 	ill_t	*ill = (ill_t *)ill_arg;
1630 
1631 	/*
1632 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1633 	 * interface X and ipif coming from interface Y, if interface
1634 	 * X and Y are part of the same IPMP group. Thus whenever interface
1635 	 * X goes down, remove all references to it by checking both
1636 	 * on ire_ipif and ire_stq.
1637 	 */
1638 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1639 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1640 		ire_delete(ire);
1641 	}
1642 }
1643 
1644 /*
1645  * Remove ire/nce from the fastpath list.
1646  */
1647 void
1648 ill_fastpath_nack(ill_t *ill)
1649 {
1650 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1651 }
1652 
1653 /* Consume an M_IOCACK of the fastpath probe. */
1654 void
1655 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1656 {
1657 	mblk_t	*mp1 = mp;
1658 
1659 	/*
1660 	 * If this was the first attempt turn on the fastpath probing.
1661 	 */
1662 	mutex_enter(&ill->ill_lock);
1663 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1664 		ill->ill_dlpi_fastpath_state = IDS_OK;
1665 	mutex_exit(&ill->ill_lock);
1666 
1667 	/* Free the M_IOCACK mblk, hold on to the data */
1668 	mp = mp->b_cont;
1669 	freeb(mp1);
1670 	if (mp == NULL)
1671 		return;
1672 	if (mp->b_cont != NULL) {
1673 		/*
1674 		 * Update all IRE's or NCE's that are waiting for
1675 		 * fastpath update.
1676 		 */
1677 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1678 		mp1 = mp->b_cont;
1679 		freeb(mp);
1680 		mp = mp1;
1681 	} else {
1682 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1683 	}
1684 
1685 	freeb(mp);
1686 }
1687 
1688 /*
1689  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1690  * The data portion of the request is a dl_unitdata_req_t template for
1691  * what we would send downstream in the absence of a fastpath confirmation.
1692  */
1693 int
1694 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1695 {
1696 	struct iocblk	*ioc;
1697 	mblk_t	*mp;
1698 
1699 	if (dlur_mp == NULL)
1700 		return (EINVAL);
1701 
1702 	mutex_enter(&ill->ill_lock);
1703 	switch (ill->ill_dlpi_fastpath_state) {
1704 	case IDS_FAILED:
1705 		/*
1706 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1707 		 * support it.
1708 		 */
1709 		mutex_exit(&ill->ill_lock);
1710 		return (ENOTSUP);
1711 	case IDS_UNKNOWN:
1712 		/* This is the first probe */
1713 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1714 		break;
1715 	default:
1716 		break;
1717 	}
1718 	mutex_exit(&ill->ill_lock);
1719 
1720 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1721 		return (EAGAIN);
1722 
1723 	mp->b_cont = copyb(dlur_mp);
1724 	if (mp->b_cont == NULL) {
1725 		freeb(mp);
1726 		return (EAGAIN);
1727 	}
1728 
1729 	ioc = (struct iocblk *)mp->b_rptr;
1730 	ioc->ioc_count = msgdsize(mp->b_cont);
1731 
1732 	putnext(ill->ill_wq, mp);
1733 	return (0);
1734 }
1735 
1736 void
1737 ill_capability_probe(ill_t *ill)
1738 {
1739 	/*
1740 	 * Do so only if capabilities are still unknown.
1741 	 */
1742 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
1743 		return;
1744 
1745 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1746 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1747 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1748 }
1749 
1750 void
1751 ill_capability_reset(ill_t *ill)
1752 {
1753 	mblk_t *sc_mp = NULL;
1754 	mblk_t *tmp;
1755 
1756 	/*
1757 	 * Note here that we reset the state to UNKNOWN, and later send
1758 	 * down the DL_CAPABILITY_REQ without first setting the state to
1759 	 * INPROGRESS.  We do this in order to distinguish the
1760 	 * DL_CAPABILITY_ACK response which may come back in response to
1761 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1762 	 * also handle the case where the driver doesn't send us back
1763 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1764 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1765 	 * features are turned off until the state reaches IDS_OK.
1766 	 */
1767 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1768 	ill->ill_capab_reneg = B_FALSE;
1769 
1770 	/*
1771 	 * Disable sub-capabilities and request a list of sub-capability
1772 	 * messages which will be sent down to the driver.  Each handler
1773 	 * allocates the corresponding dl_capability_sub_t inside an
1774 	 * mblk, and links it to the existing sc_mp mblk, or return it
1775 	 * as sc_mp if it's the first sub-capability (the passed in
1776 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1777 	 * sc_mp will be pulled-up, before passing it downstream.
1778 	 */
1779 	ill_capability_mdt_reset(ill, &sc_mp);
1780 	ill_capability_hcksum_reset(ill, &sc_mp);
1781 	ill_capability_zerocopy_reset(ill, &sc_mp);
1782 	ill_capability_ipsec_reset(ill, &sc_mp);
1783 	ill_capability_dls_reset(ill, &sc_mp);
1784 	ill_capability_lso_reset(ill, &sc_mp);
1785 
1786 	/* Nothing to send down in order to disable the capabilities? */
1787 	if (sc_mp == NULL)
1788 		return;
1789 
1790 	tmp = msgpullup(sc_mp, -1);
1791 	freemsg(sc_mp);
1792 	if ((sc_mp = tmp) == NULL) {
1793 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1794 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1795 		return;
1796 	}
1797 
1798 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1799 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1800 }
1801 
1802 /*
1803  * Request or set new-style hardware capabilities supported by DLS provider.
1804  */
1805 static void
1806 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1807 {
1808 	mblk_t *mp;
1809 	dl_capability_req_t *capb;
1810 	size_t size = 0;
1811 	uint8_t *ptr;
1812 
1813 	if (reqp != NULL)
1814 		size = MBLKL(reqp);
1815 
1816 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1817 	if (mp == NULL) {
1818 		freemsg(reqp);
1819 		return;
1820 	}
1821 	ptr = mp->b_rptr;
1822 
1823 	capb = (dl_capability_req_t *)ptr;
1824 	ptr += sizeof (dl_capability_req_t);
1825 
1826 	if (reqp != NULL) {
1827 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1828 		capb->dl_sub_length = size;
1829 		bcopy(reqp->b_rptr, ptr, size);
1830 		ptr += size;
1831 		mp->b_cont = reqp->b_cont;
1832 		freeb(reqp);
1833 	}
1834 	ASSERT(ptr == mp->b_wptr);
1835 
1836 	ill_dlpi_send(ill, mp);
1837 }
1838 
1839 static void
1840 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1841 {
1842 	dl_capab_id_t *id_ic;
1843 	uint_t sub_dl_cap = outers->dl_cap;
1844 	dl_capability_sub_t *inners;
1845 	uint8_t *capend;
1846 
1847 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1848 
1849 	/*
1850 	 * Note: range checks here are not absolutely sufficient to
1851 	 * make us robust against malformed messages sent by drivers;
1852 	 * this is in keeping with the rest of IP's dlpi handling.
1853 	 * (Remember, it's coming from something else in the kernel
1854 	 * address space)
1855 	 */
1856 
1857 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1858 	if (capend > mp->b_wptr) {
1859 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1860 		    "malformed sub-capability too long for mblk");
1861 		return;
1862 	}
1863 
1864 	id_ic = (dl_capab_id_t *)(outers + 1);
1865 
1866 	if (outers->dl_length < sizeof (*id_ic) ||
1867 	    (inners = &id_ic->id_subcap,
1868 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1869 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1870 		    "encapsulated capab type %d too long for mblk",
1871 		    inners->dl_cap);
1872 		return;
1873 	}
1874 
1875 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1876 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1877 		    "isn't as expected; pass-thru module(s) detected, "
1878 		    "discarding capability\n", inners->dl_cap));
1879 		return;
1880 	}
1881 
1882 	/* Process the encapsulated sub-capability */
1883 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1884 }
1885 
1886 /*
1887  * Process Multidata Transmit capability negotiation ack received from a
1888  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1889  * DL_CAPABILITY_ACK message.
1890  */
1891 static void
1892 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1893 {
1894 	mblk_t *nmp = NULL;
1895 	dl_capability_req_t *oc;
1896 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1897 	ill_mdt_capab_t **ill_mdt_capab;
1898 	uint_t sub_dl_cap = isub->dl_cap;
1899 	uint8_t *capend;
1900 
1901 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1902 
1903 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1904 
1905 	/*
1906 	 * Note: range checks here are not absolutely sufficient to
1907 	 * make us robust against malformed messages sent by drivers;
1908 	 * this is in keeping with the rest of IP's dlpi handling.
1909 	 * (Remember, it's coming from something else in the kernel
1910 	 * address space)
1911 	 */
1912 
1913 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1914 	if (capend > mp->b_wptr) {
1915 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1916 		    "malformed sub-capability too long for mblk");
1917 		return;
1918 	}
1919 
1920 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1921 
1922 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1923 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1924 		    "unsupported MDT sub-capability (version %d, expected %d)",
1925 		    mdt_ic->mdt_version, MDT_VERSION_2);
1926 		return;
1927 	}
1928 
1929 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1930 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1931 		    "capability isn't as expected; pass-thru module(s) "
1932 		    "detected, discarding capability\n"));
1933 		return;
1934 	}
1935 
1936 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1937 
1938 		if (*ill_mdt_capab == NULL) {
1939 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1940 			    KM_NOSLEEP);
1941 
1942 			if (*ill_mdt_capab == NULL) {
1943 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1944 				    "could not enable MDT version %d "
1945 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1946 				    ill->ill_name);
1947 				return;
1948 			}
1949 		}
1950 
1951 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1952 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1953 		    "header spaces, %d max pld bufs, %d span limit)\n",
1954 		    ill->ill_name, MDT_VERSION_2,
1955 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1956 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1957 
1958 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1959 		(*ill_mdt_capab)->ill_mdt_on = 1;
1960 		/*
1961 		 * Round the following values to the nearest 32-bit; ULP
1962 		 * may further adjust them to accomodate for additional
1963 		 * protocol headers.  We pass these values to ULP during
1964 		 * bind time.
1965 		 */
1966 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1967 		    roundup(mdt_ic->mdt_hdr_head, 4);
1968 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1969 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1970 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1971 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1972 
1973 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1974 	} else {
1975 		uint_t size;
1976 		uchar_t *rptr;
1977 
1978 		size = sizeof (dl_capability_req_t) +
1979 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1980 
1981 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1982 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1983 			    "could not enable MDT for %s (ENOMEM)\n",
1984 			    ill->ill_name);
1985 			return;
1986 		}
1987 
1988 		rptr = nmp->b_rptr;
1989 		/* initialize dl_capability_req_t */
1990 		oc = (dl_capability_req_t *)nmp->b_rptr;
1991 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1992 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1993 		    sizeof (dl_capab_mdt_t);
1994 		nmp->b_rptr += sizeof (dl_capability_req_t);
1995 
1996 		/* initialize dl_capability_sub_t */
1997 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1998 		nmp->b_rptr += sizeof (*isub);
1999 
2000 		/* initialize dl_capab_mdt_t */
2001 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2002 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2003 
2004 		nmp->b_rptr = rptr;
2005 
2006 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2007 		    "to enable MDT version %d\n", ill->ill_name,
2008 		    MDT_VERSION_2));
2009 
2010 		/* set ENABLE flag */
2011 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2012 
2013 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2014 		ill_dlpi_send(ill, nmp);
2015 	}
2016 }
2017 
2018 static void
2019 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2020 {
2021 	mblk_t *mp;
2022 	dl_capab_mdt_t *mdt_subcap;
2023 	dl_capability_sub_t *dl_subcap;
2024 	int size;
2025 
2026 	if (!ILL_MDT_CAPABLE(ill))
2027 		return;
2028 
2029 	ASSERT(ill->ill_mdt_capab != NULL);
2030 	/*
2031 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2032 	 * structure since it's possible that another thread is still
2033 	 * referring to it.  The structure only gets deallocated when
2034 	 * we destroy the ill.
2035 	 */
2036 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2037 
2038 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2039 
2040 	mp = allocb(size, BPRI_HI);
2041 	if (mp == NULL) {
2042 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2043 		    "request to disable MDT\n"));
2044 		return;
2045 	}
2046 
2047 	mp->b_wptr = mp->b_rptr + size;
2048 
2049 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2050 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2051 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2052 
2053 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2054 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2055 	mdt_subcap->mdt_flags = 0;
2056 	mdt_subcap->mdt_hdr_head = 0;
2057 	mdt_subcap->mdt_hdr_tail = 0;
2058 
2059 	if (*sc_mp != NULL)
2060 		linkb(*sc_mp, mp);
2061 	else
2062 		*sc_mp = mp;
2063 }
2064 
2065 /*
2066  * Send a DL_NOTIFY_REQ to the specified ill to enable
2067  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2068  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2069  * acceleration.
2070  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2071  */
2072 static boolean_t
2073 ill_enable_promisc_notify(ill_t *ill)
2074 {
2075 	mblk_t *mp;
2076 	dl_notify_req_t *req;
2077 
2078 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2079 
2080 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2081 	if (mp == NULL)
2082 		return (B_FALSE);
2083 
2084 	req = (dl_notify_req_t *)mp->b_rptr;
2085 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2086 	    DL_NOTE_PROMISC_OFF_PHYS;
2087 
2088 	ill_dlpi_send(ill, mp);
2089 
2090 	return (B_TRUE);
2091 }
2092 
2093 /*
2094  * Allocate an IPsec capability request which will be filled by our
2095  * caller to turn on support for one or more algorithms.
2096  */
2097 static mblk_t *
2098 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2099 {
2100 	mblk_t *nmp;
2101 	dl_capability_req_t	*ocap;
2102 	dl_capab_ipsec_t	*ocip;
2103 	dl_capab_ipsec_t	*icip;
2104 	uint8_t			*ptr;
2105 	icip = (dl_capab_ipsec_t *)(isub + 1);
2106 
2107 	/*
2108 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2109 	 * PROMISC_ON/OFF notification from the provider. We need to
2110 	 * do this before enabling the algorithms to avoid leakage of
2111 	 * cleartext packets.
2112 	 */
2113 
2114 	if (!ill_enable_promisc_notify(ill))
2115 		return (NULL);
2116 
2117 	/*
2118 	 * Allocate new mblk which will contain a new capability
2119 	 * request to enable the capabilities.
2120 	 */
2121 
2122 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2123 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2124 	if (nmp == NULL)
2125 		return (NULL);
2126 
2127 	ptr = nmp->b_rptr;
2128 
2129 	/* initialize dl_capability_req_t */
2130 	ocap = (dl_capability_req_t *)ptr;
2131 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2132 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2133 	ptr += sizeof (dl_capability_req_t);
2134 
2135 	/* initialize dl_capability_sub_t */
2136 	bcopy(isub, ptr, sizeof (*isub));
2137 	ptr += sizeof (*isub);
2138 
2139 	/* initialize dl_capab_ipsec_t */
2140 	ocip = (dl_capab_ipsec_t *)ptr;
2141 	bcopy(icip, ocip, sizeof (*icip));
2142 
2143 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2144 	return (nmp);
2145 }
2146 
2147 /*
2148  * Process an IPsec capability negotiation ack received from a DLS Provider.
2149  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2150  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2151  */
2152 static void
2153 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2154 {
2155 	dl_capab_ipsec_t	*icip;
2156 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2157 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2158 	uint_t cipher, nciphers;
2159 	mblk_t *nmp;
2160 	uint_t alg_len;
2161 	boolean_t need_sadb_dump;
2162 	uint_t sub_dl_cap = isub->dl_cap;
2163 	ill_ipsec_capab_t **ill_capab;
2164 	uint64_t ill_capab_flag;
2165 	uint8_t *capend, *ciphend;
2166 	boolean_t sadb_resync;
2167 
2168 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2169 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2170 
2171 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2172 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2173 		ill_capab_flag = ILL_CAPAB_AH;
2174 	} else {
2175 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2176 		ill_capab_flag = ILL_CAPAB_ESP;
2177 	}
2178 
2179 	/*
2180 	 * If the ill capability structure exists, then this incoming
2181 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2182 	 * If this is so, then we'd need to resynchronize the SADB
2183 	 * after re-enabling the offloaded ciphers.
2184 	 */
2185 	sadb_resync = (*ill_capab != NULL);
2186 
2187 	/*
2188 	 * Note: range checks here are not absolutely sufficient to
2189 	 * make us robust against malformed messages sent by drivers;
2190 	 * this is in keeping with the rest of IP's dlpi handling.
2191 	 * (Remember, it's coming from something else in the kernel
2192 	 * address space)
2193 	 */
2194 
2195 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2196 	if (capend > mp->b_wptr) {
2197 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2198 		    "malformed sub-capability too long for mblk");
2199 		return;
2200 	}
2201 
2202 	/*
2203 	 * There are two types of acks we process here:
2204 	 * 1. acks in reply to a (first form) generic capability req
2205 	 *    (no ENABLE flag set)
2206 	 * 2. acks in reply to a ENABLE capability req.
2207 	 *    (ENABLE flag set)
2208 	 *
2209 	 * We process the subcapability passed as argument as follows:
2210 	 * 1 do initializations
2211 	 *   1.1 initialize nmp = NULL
2212 	 *   1.2 set need_sadb_dump to B_FALSE
2213 	 * 2 for each cipher in subcapability:
2214 	 *   2.1 if ENABLE flag is set:
2215 	 *	2.1.1 update per-ill ipsec capabilities info
2216 	 *	2.1.2 set need_sadb_dump to B_TRUE
2217 	 *   2.2 if ENABLE flag is not set:
2218 	 *	2.2.1 if nmp is NULL:
2219 	 *		2.2.1.1 allocate and initialize nmp
2220 	 *		2.2.1.2 init current pos in nmp
2221 	 *	2.2.2 copy current cipher to current pos in nmp
2222 	 *	2.2.3 set ENABLE flag in nmp
2223 	 *	2.2.4 update current pos
2224 	 * 3 if nmp is not equal to NULL, send enable request
2225 	 *   3.1 send capability request
2226 	 * 4 if need_sadb_dump is B_TRUE
2227 	 *   4.1 enable promiscuous on/off notifications
2228 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2229 	 *	AH or ESP SA's to interface.
2230 	 */
2231 
2232 	nmp = NULL;
2233 	oalg = NULL;
2234 	need_sadb_dump = B_FALSE;
2235 	icip = (dl_capab_ipsec_t *)(isub + 1);
2236 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2237 
2238 	nciphers = icip->cip_nciphers;
2239 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2240 
2241 	if (ciphend > capend) {
2242 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2243 		    "too many ciphers for sub-capability len");
2244 		return;
2245 	}
2246 
2247 	for (cipher = 0; cipher < nciphers; cipher++) {
2248 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2249 
2250 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2251 			/*
2252 			 * TBD: when we provide a way to disable capabilities
2253 			 * from above, need to manage the request-pending state
2254 			 * and fail if we were not expecting this ACK.
2255 			 */
2256 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2257 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2258 
2259 			/*
2260 			 * Update IPsec capabilities for this ill
2261 			 */
2262 
2263 			if (*ill_capab == NULL) {
2264 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2265 				    ("ill_capability_ipsec_ack: "
2266 				    "allocating ipsec_capab for ill\n"));
2267 				*ill_capab = ill_ipsec_capab_alloc();
2268 
2269 				if (*ill_capab == NULL) {
2270 					cmn_err(CE_WARN,
2271 					    "ill_capability_ipsec_ack: "
2272 					    "could not enable IPsec Hardware "
2273 					    "acceleration for %s (ENOMEM)\n",
2274 					    ill->ill_name);
2275 					return;
2276 				}
2277 			}
2278 
2279 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2280 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2281 
2282 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2283 				cmn_err(CE_WARN,
2284 				    "ill_capability_ipsec_ack: "
2285 				    "malformed IPsec algorithm id %d",
2286 				    ialg->alg_prim);
2287 				continue;
2288 			}
2289 
2290 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2291 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2292 				    ialg->alg_prim);
2293 			} else {
2294 				ipsec_capab_algparm_t *alp;
2295 
2296 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2297 				    ialg->alg_prim);
2298 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2299 				    ialg->alg_prim)) {
2300 					cmn_err(CE_WARN,
2301 					    "ill_capability_ipsec_ack: "
2302 					    "no space for IPsec alg id %d",
2303 					    ialg->alg_prim);
2304 					continue;
2305 				}
2306 				alp = &((*ill_capab)->encr_algparm[
2307 				    ialg->alg_prim]);
2308 				alp->minkeylen = ialg->alg_minbits;
2309 				alp->maxkeylen = ialg->alg_maxbits;
2310 			}
2311 			ill->ill_capabilities |= ill_capab_flag;
2312 			/*
2313 			 * indicate that a capability was enabled, which
2314 			 * will be used below to kick off a SADB dump
2315 			 * to the ill.
2316 			 */
2317 			need_sadb_dump = B_TRUE;
2318 		} else {
2319 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2320 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2321 			    ialg->alg_prim));
2322 
2323 			if (nmp == NULL) {
2324 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2325 				if (nmp == NULL) {
2326 					/*
2327 					 * Sending the PROMISC_ON/OFF
2328 					 * notification request failed.
2329 					 * We cannot enable the algorithms
2330 					 * since the Provider will not
2331 					 * notify IP of promiscous mode
2332 					 * changes, which could lead
2333 					 * to leakage of packets.
2334 					 */
2335 					cmn_err(CE_WARN,
2336 					    "ill_capability_ipsec_ack: "
2337 					    "could not enable IPsec Hardware "
2338 					    "acceleration for %s (ENOMEM)\n",
2339 					    ill->ill_name);
2340 					return;
2341 				}
2342 				/* ptr to current output alg specifier */
2343 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2344 			}
2345 
2346 			/*
2347 			 * Copy current alg specifier, set ENABLE
2348 			 * flag, and advance to next output alg.
2349 			 * For now we enable all IPsec capabilities.
2350 			 */
2351 			ASSERT(oalg != NULL);
2352 			bcopy(ialg, oalg, alg_len);
2353 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2354 			nmp->b_wptr += alg_len;
2355 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2356 		}
2357 
2358 		/* move to next input algorithm specifier */
2359 		ialg = (dl_capab_ipsec_alg_t *)
2360 		    ((char *)ialg + alg_len);
2361 	}
2362 
2363 	if (nmp != NULL)
2364 		/*
2365 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2366 		 * IPsec hardware acceleration.
2367 		 */
2368 		ill_dlpi_send(ill, nmp);
2369 
2370 	if (need_sadb_dump)
2371 		/*
2372 		 * An acknowledgement corresponding to a request to
2373 		 * enable acceleration was received, notify SADB.
2374 		 */
2375 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2376 }
2377 
2378 /*
2379  * Given an mblk with enough space in it, create sub-capability entries for
2380  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2381  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2382  * in preparation for the reset the DL_CAPABILITY_REQ message.
2383  */
2384 static void
2385 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2386     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2387 {
2388 	dl_capab_ipsec_t *oipsec;
2389 	dl_capab_ipsec_alg_t *oalg;
2390 	dl_capability_sub_t *dl_subcap;
2391 	int i, k;
2392 
2393 	ASSERT(nciphers > 0);
2394 	ASSERT(ill_cap != NULL);
2395 	ASSERT(mp != NULL);
2396 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2397 
2398 	/* dl_capability_sub_t for "stype" */
2399 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2400 	dl_subcap->dl_cap = stype;
2401 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2402 	mp->b_wptr += sizeof (dl_capability_sub_t);
2403 
2404 	/* dl_capab_ipsec_t for "stype" */
2405 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2406 	oipsec->cip_version = 1;
2407 	oipsec->cip_nciphers = nciphers;
2408 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2409 
2410 	/* create entries for "stype" AUTH ciphers */
2411 	for (i = 0; i < ill_cap->algs_size; i++) {
2412 		for (k = 0; k < BITSPERBYTE; k++) {
2413 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2414 				continue;
2415 
2416 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2417 			bzero((void *)oalg, sizeof (*oalg));
2418 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2419 			oalg->alg_prim = k + (BITSPERBYTE * i);
2420 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2421 		}
2422 	}
2423 	/* create entries for "stype" ENCR ciphers */
2424 	for (i = 0; i < ill_cap->algs_size; i++) {
2425 		for (k = 0; k < BITSPERBYTE; k++) {
2426 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2427 				continue;
2428 
2429 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2430 			bzero((void *)oalg, sizeof (*oalg));
2431 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2432 			oalg->alg_prim = k + (BITSPERBYTE * i);
2433 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2434 		}
2435 	}
2436 }
2437 
2438 /*
2439  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2440  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2441  * POPC instruction, but our macro is more flexible for an arbitrary length
2442  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2443  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2444  * stays that way, we can reduce the number of iterations required.
2445  */
2446 #define	COUNT_1S(val, sum) {					\
2447 	uint8_t x = val & 0xff;					\
2448 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2449 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2450 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2451 }
2452 
2453 /* ARGSUSED */
2454 static void
2455 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2456 {
2457 	mblk_t *mp;
2458 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2459 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2460 	uint64_t ill_capabilities = ill->ill_capabilities;
2461 	int ah_cnt = 0, esp_cnt = 0;
2462 	int ah_len = 0, esp_len = 0;
2463 	int i, size = 0;
2464 
2465 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2466 		return;
2467 
2468 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2469 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2470 
2471 	/* Find out the number of ciphers for AH */
2472 	if (cap_ah != NULL) {
2473 		for (i = 0; i < cap_ah->algs_size; i++) {
2474 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2475 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2476 		}
2477 		if (ah_cnt > 0) {
2478 			size += sizeof (dl_capability_sub_t) +
2479 			    sizeof (dl_capab_ipsec_t);
2480 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2481 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2482 			size += ah_len;
2483 		}
2484 	}
2485 
2486 	/* Find out the number of ciphers for ESP */
2487 	if (cap_esp != NULL) {
2488 		for (i = 0; i < cap_esp->algs_size; i++) {
2489 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2490 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2491 		}
2492 		if (esp_cnt > 0) {
2493 			size += sizeof (dl_capability_sub_t) +
2494 			    sizeof (dl_capab_ipsec_t);
2495 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2496 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2497 			size += esp_len;
2498 		}
2499 	}
2500 
2501 	if (size == 0) {
2502 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2503 		    "there's nothing to reset\n"));
2504 		return;
2505 	}
2506 
2507 	mp = allocb(size, BPRI_HI);
2508 	if (mp == NULL) {
2509 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2510 		    "request to disable IPSEC Hardware Acceleration\n"));
2511 		return;
2512 	}
2513 
2514 	/*
2515 	 * Clear the capability flags for IPsec HA but retain the ill
2516 	 * capability structures since it's possible that another thread
2517 	 * is still referring to them.  The structures only get deallocated
2518 	 * when we destroy the ill.
2519 	 *
2520 	 * Various places check the flags to see if the ill is capable of
2521 	 * hardware acceleration, and by clearing them we ensure that new
2522 	 * outbound IPsec packets are sent down encrypted.
2523 	 */
2524 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2525 
2526 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2527 	if (ah_cnt > 0) {
2528 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2529 		    cap_ah, mp);
2530 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2531 	}
2532 
2533 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2534 	if (esp_cnt > 0) {
2535 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2536 		    cap_esp, mp);
2537 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2538 	}
2539 
2540 	/*
2541 	 * At this point we've composed a bunch of sub-capabilities to be
2542 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2543 	 * by the caller.  Upon receiving this reset message, the driver
2544 	 * must stop inbound decryption (by destroying all inbound SAs)
2545 	 * and let the corresponding packets come in encrypted.
2546 	 */
2547 
2548 	if (*sc_mp != NULL)
2549 		linkb(*sc_mp, mp);
2550 	else
2551 		*sc_mp = mp;
2552 }
2553 
2554 static void
2555 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2556     boolean_t encapsulated)
2557 {
2558 	boolean_t legacy = B_FALSE;
2559 
2560 	/*
2561 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2562 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2563 	 * instructed the driver to disable its advertised capabilities,
2564 	 * so there's no point in accepting any response at this moment.
2565 	 */
2566 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2567 		return;
2568 
2569 	/*
2570 	 * Note that only the following two sub-capabilities may be
2571 	 * considered as "legacy", since their original definitions
2572 	 * do not incorporate the dl_mid_t module ID token, and hence
2573 	 * may require the use of the wrapper sub-capability.
2574 	 */
2575 	switch (subp->dl_cap) {
2576 	case DL_CAPAB_IPSEC_AH:
2577 	case DL_CAPAB_IPSEC_ESP:
2578 		legacy = B_TRUE;
2579 		break;
2580 	}
2581 
2582 	/*
2583 	 * For legacy sub-capabilities which don't incorporate a queue_t
2584 	 * pointer in their structures, discard them if we detect that
2585 	 * there are intermediate modules in between IP and the driver.
2586 	 */
2587 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2588 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2589 		    "%d discarded; %d module(s) present below IP\n",
2590 		    subp->dl_cap, ill->ill_lmod_cnt));
2591 		return;
2592 	}
2593 
2594 	switch (subp->dl_cap) {
2595 	case DL_CAPAB_IPSEC_AH:
2596 	case DL_CAPAB_IPSEC_ESP:
2597 		ill_capability_ipsec_ack(ill, mp, subp);
2598 		break;
2599 	case DL_CAPAB_MDT:
2600 		ill_capability_mdt_ack(ill, mp, subp);
2601 		break;
2602 	case DL_CAPAB_HCKSUM:
2603 		ill_capability_hcksum_ack(ill, mp, subp);
2604 		break;
2605 	case DL_CAPAB_ZEROCOPY:
2606 		ill_capability_zerocopy_ack(ill, mp, subp);
2607 		break;
2608 	case DL_CAPAB_POLL:
2609 		if (!SOFT_RINGS_ENABLED())
2610 			ill_capability_dls_ack(ill, mp, subp);
2611 		break;
2612 	case DL_CAPAB_SOFT_RING:
2613 		if (SOFT_RINGS_ENABLED())
2614 			ill_capability_dls_ack(ill, mp, subp);
2615 		break;
2616 	case DL_CAPAB_LSO:
2617 		ill_capability_lso_ack(ill, mp, subp);
2618 		break;
2619 	default:
2620 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2621 		    subp->dl_cap));
2622 	}
2623 }
2624 
2625 /*
2626  * As part of negotiating polling capability, the driver tells us
2627  * the default (or normal) blanking interval and packet threshold
2628  * (the receive timer fires if blanking interval is reached or
2629  * the packet threshold is reached).
2630  *
2631  * As part of manipulating the polling interval, we always use our
2632  * estimated interval (avg service time * number of packets queued
2633  * on the squeue) but we try to blank for a minimum of
2634  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2635  * packet threshold during this time. When we are not in polling mode
2636  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2637  * rr_min_blank_ratio but up the packet cnt by a ratio of
2638  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2639  * possible although for a shorter interval.
2640  */
2641 #define	RR_MAX_BLANK_RATIO	20
2642 #define	RR_MIN_BLANK_RATIO	10
2643 #define	RR_MAX_PKT_CNT_RATIO	3
2644 #define	RR_MIN_PKT_CNT_RATIO	3
2645 
2646 /*
2647  * These can be tuned via /etc/system.
2648  */
2649 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2650 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2651 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2652 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2653 
2654 static mac_resource_handle_t
2655 ill_ring_add(void *arg, mac_resource_t *mrp)
2656 {
2657 	ill_t			*ill = (ill_t *)arg;
2658 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2659 	ill_rx_ring_t		*rx_ring;
2660 	int			ip_rx_index;
2661 
2662 	ASSERT(mrp != NULL);
2663 	if (mrp->mr_type != MAC_RX_FIFO) {
2664 		return (NULL);
2665 	}
2666 	ASSERT(ill != NULL);
2667 	ASSERT(ill->ill_dls_capab != NULL);
2668 
2669 	mutex_enter(&ill->ill_lock);
2670 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2671 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2672 		ASSERT(rx_ring != NULL);
2673 
2674 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2675 			time_t normal_blank_time =
2676 			    mrfp->mrf_normal_blank_time;
2677 			uint_t normal_pkt_cnt =
2678 			    mrfp->mrf_normal_pkt_count;
2679 
2680 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2681 
2682 	rx_ring->rr_blank = mrfp->mrf_blank;
2683 	rx_ring->rr_handle = mrfp->mrf_arg;
2684 	rx_ring->rr_ill = ill;
2685 	rx_ring->rr_normal_blank_time = normal_blank_time;
2686 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2687 
2688 			rx_ring->rr_max_blank_time =
2689 			    normal_blank_time * rr_max_blank_ratio;
2690 			rx_ring->rr_min_blank_time =
2691 			    normal_blank_time * rr_min_blank_ratio;
2692 			rx_ring->rr_max_pkt_cnt =
2693 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2694 			rx_ring->rr_min_pkt_cnt =
2695 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2696 
2697 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2698 			mutex_exit(&ill->ill_lock);
2699 
2700 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2701 			    (int), ip_rx_index);
2702 			return ((mac_resource_handle_t)rx_ring);
2703 		}
2704 	}
2705 
2706 	/*
2707 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2708 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2709 	 * should be made configurable. Meanwhile it cause no panic because
2710 	 * driver will pass ip_input a NULL handle which will make
2711 	 * IP allocate the default squeue and Polling mode will not
2712 	 * be used for this ring.
2713 	 */
2714 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2715 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2716 
2717 	mutex_exit(&ill->ill_lock);
2718 	return (NULL);
2719 }
2720 
2721 static boolean_t
2722 ill_capability_dls_init(ill_t *ill)
2723 {
2724 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2725 	conn_t 			*connp;
2726 	size_t			sz;
2727 	ip_stack_t *ipst = ill->ill_ipst;
2728 
2729 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2730 		if (ill_dls == NULL) {
2731 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2732 			    "soft_ring enabled for ill=%s (%p) but data "
2733 			    "structs uninitialized\n", ill->ill_name,
2734 			    (void *)ill);
2735 		}
2736 		return (B_TRUE);
2737 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2738 		if (ill_dls == NULL) {
2739 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2740 			    "polling enabled for ill=%s (%p) but data "
2741 			    "structs uninitialized\n", ill->ill_name,
2742 			    (void *)ill);
2743 		}
2744 		return (B_TRUE);
2745 	}
2746 
2747 	if (ill_dls != NULL) {
2748 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2749 		/* Soft_Ring or polling is being re-enabled */
2750 
2751 		connp = ill_dls->ill_unbind_conn;
2752 		ASSERT(rx_ring != NULL);
2753 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2754 		bzero((void *)rx_ring,
2755 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2756 		ill_dls->ill_ring_tbl = rx_ring;
2757 		ill_dls->ill_unbind_conn = connp;
2758 		return (B_TRUE);
2759 	}
2760 
2761 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2762 	    ipst->ips_netstack)) == NULL)
2763 		return (B_FALSE);
2764 
2765 	sz = sizeof (ill_dls_capab_t);
2766 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2767 
2768 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2769 	if (ill_dls == NULL) {
2770 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2771 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2772 		    (void *)ill);
2773 		CONN_DEC_REF(connp);
2774 		return (B_FALSE);
2775 	}
2776 
2777 	/* Allocate space to hold ring table */
2778 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2779 	ill->ill_dls_capab = ill_dls;
2780 	ill_dls->ill_unbind_conn = connp;
2781 	return (B_TRUE);
2782 }
2783 
2784 /*
2785  * ill_capability_dls_disable: disable soft_ring and/or polling
2786  * capability. Since any of the rings might already be in use, need
2787  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2788  * direct calls if necessary.
2789  */
2790 static void
2791 ill_capability_dls_disable(ill_t *ill)
2792 {
2793 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2794 
2795 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2796 		ip_squeue_clean_all(ill);
2797 		ill_dls->ill_tx = NULL;
2798 		ill_dls->ill_tx_handle = NULL;
2799 		ill_dls->ill_dls_change_status = NULL;
2800 		ill_dls->ill_dls_bind = NULL;
2801 		ill_dls->ill_dls_unbind = NULL;
2802 	}
2803 
2804 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2805 }
2806 
2807 static void
2808 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2809     dl_capability_sub_t *isub)
2810 {
2811 	uint_t			size;
2812 	uchar_t			*rptr;
2813 	dl_capab_dls_t	dls, *odls;
2814 	ill_dls_capab_t	*ill_dls;
2815 	mblk_t			*nmp = NULL;
2816 	dl_capability_req_t	*ocap;
2817 	uint_t			sub_dl_cap = isub->dl_cap;
2818 
2819 	if (!ill_capability_dls_init(ill))
2820 		return;
2821 	ill_dls = ill->ill_dls_capab;
2822 
2823 	/* Copy locally to get the members aligned */
2824 	bcopy((void *)idls, (void *)&dls,
2825 	    sizeof (dl_capab_dls_t));
2826 
2827 	/* Get the tx function and handle from dld */
2828 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2829 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2830 
2831 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2832 		ill_dls->ill_dls_change_status =
2833 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2834 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2835 		ill_dls->ill_dls_unbind =
2836 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2837 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2838 	}
2839 
2840 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2841 	    isub->dl_length;
2842 
2843 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2844 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2845 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2846 		    ill->ill_name, (void *)ill);
2847 		return;
2848 	}
2849 
2850 	/* initialize dl_capability_req_t */
2851 	rptr = nmp->b_rptr;
2852 	ocap = (dl_capability_req_t *)rptr;
2853 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2854 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2855 	rptr += sizeof (dl_capability_req_t);
2856 
2857 	/* initialize dl_capability_sub_t */
2858 	bcopy(isub, rptr, sizeof (*isub));
2859 	rptr += sizeof (*isub);
2860 
2861 	odls = (dl_capab_dls_t *)rptr;
2862 	rptr += sizeof (dl_capab_dls_t);
2863 
2864 	/* initialize dl_capab_dls_t to be sent down */
2865 	dls.dls_rx_handle = (uintptr_t)ill;
2866 	dls.dls_rx = (uintptr_t)ip_input;
2867 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2868 
2869 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2870 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2871 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2872 		dls.dls_flags = SOFT_RING_ENABLE;
2873 	} else {
2874 		dls.dls_flags = POLL_ENABLE;
2875 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2876 		    "to enable polling\n", ill->ill_name));
2877 	}
2878 	bcopy((void *)&dls, (void *)odls,
2879 	    sizeof (dl_capab_dls_t));
2880 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2881 	/*
2882 	 * nmp points to a DL_CAPABILITY_REQ message to
2883 	 * enable either soft_ring or polling
2884 	 */
2885 	ill_dlpi_send(ill, nmp);
2886 }
2887 
2888 static void
2889 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2890 {
2891 	mblk_t *mp;
2892 	dl_capab_dls_t *idls;
2893 	dl_capability_sub_t *dl_subcap;
2894 	int size;
2895 
2896 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2897 		return;
2898 
2899 	ASSERT(ill->ill_dls_capab != NULL);
2900 
2901 	size = sizeof (*dl_subcap) + sizeof (*idls);
2902 
2903 	mp = allocb(size, BPRI_HI);
2904 	if (mp == NULL) {
2905 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2906 		    "request to disable soft_ring\n"));
2907 		return;
2908 	}
2909 
2910 	mp->b_wptr = mp->b_rptr + size;
2911 
2912 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2913 	dl_subcap->dl_length = sizeof (*idls);
2914 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2915 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2916 	else
2917 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2918 
2919 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2920 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2921 		idls->dls_flags = SOFT_RING_DISABLE;
2922 	else
2923 		idls->dls_flags = POLL_DISABLE;
2924 
2925 	if (*sc_mp != NULL)
2926 		linkb(*sc_mp, mp);
2927 	else
2928 		*sc_mp = mp;
2929 }
2930 
2931 /*
2932  * Process a soft_ring/poll capability negotiation ack received
2933  * from a DLS Provider.isub must point to the sub-capability
2934  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2935  */
2936 static void
2937 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2938 {
2939 	dl_capab_dls_t		*idls;
2940 	uint_t			sub_dl_cap = isub->dl_cap;
2941 	uint8_t			*capend;
2942 
2943 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2944 	    sub_dl_cap == DL_CAPAB_POLL);
2945 
2946 	if (ill->ill_isv6)
2947 		return;
2948 
2949 	/*
2950 	 * Note: range checks here are not absolutely sufficient to
2951 	 * make us robust against malformed messages sent by drivers;
2952 	 * this is in keeping with the rest of IP's dlpi handling.
2953 	 * (Remember, it's coming from something else in the kernel
2954 	 * address space)
2955 	 */
2956 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2957 	if (capend > mp->b_wptr) {
2958 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2959 		    "malformed sub-capability too long for mblk");
2960 		return;
2961 	}
2962 
2963 	/*
2964 	 * There are two types of acks we process here:
2965 	 * 1. acks in reply to a (first form) generic capability req
2966 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2967 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2968 	 *    capability req.
2969 	 */
2970 	idls = (dl_capab_dls_t *)(isub + 1);
2971 
2972 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2973 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2974 		    "capability isn't as expected; pass-thru "
2975 		    "module(s) detected, discarding capability\n"));
2976 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2977 			/*
2978 			 * This is a capability renegotitation case.
2979 			 * The interface better be unusable at this
2980 			 * point other wise bad things will happen
2981 			 * if we disable direct calls on a running
2982 			 * and up interface.
2983 			 */
2984 			ill_capability_dls_disable(ill);
2985 		}
2986 		return;
2987 	}
2988 
2989 	switch (idls->dls_flags) {
2990 	default:
2991 		/* Disable if unknown flag */
2992 	case SOFT_RING_DISABLE:
2993 	case POLL_DISABLE:
2994 		ill_capability_dls_disable(ill);
2995 		break;
2996 	case SOFT_RING_CAPABLE:
2997 	case POLL_CAPABLE:
2998 		/*
2999 		 * If the capability was already enabled, its safe
3000 		 * to disable it first to get rid of stale information
3001 		 * and then start enabling it again.
3002 		 */
3003 		ill_capability_dls_disable(ill);
3004 		ill_capability_dls_capable(ill, idls, isub);
3005 		break;
3006 	case SOFT_RING_ENABLE:
3007 	case POLL_ENABLE:
3008 		mutex_enter(&ill->ill_lock);
3009 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3010 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3011 			ASSERT(ill->ill_dls_capab != NULL);
3012 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3013 		}
3014 		if (sub_dl_cap == DL_CAPAB_POLL &&
3015 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3016 			ASSERT(ill->ill_dls_capab != NULL);
3017 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3018 			ip1dbg(("ill_capability_dls_ack: interface %s "
3019 			    "has enabled polling\n", ill->ill_name));
3020 		}
3021 		mutex_exit(&ill->ill_lock);
3022 		break;
3023 	}
3024 }
3025 
3026 /*
3027  * Process a hardware checksum offload capability negotiation ack received
3028  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3029  * of a DL_CAPABILITY_ACK message.
3030  */
3031 static void
3032 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3033 {
3034 	dl_capability_req_t	*ocap;
3035 	dl_capab_hcksum_t	*ihck, *ohck;
3036 	ill_hcksum_capab_t	**ill_hcksum;
3037 	mblk_t			*nmp = NULL;
3038 	uint_t			sub_dl_cap = isub->dl_cap;
3039 	uint8_t			*capend;
3040 
3041 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3042 
3043 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3044 
3045 	/*
3046 	 * Note: range checks here are not absolutely sufficient to
3047 	 * make us robust against malformed messages sent by drivers;
3048 	 * this is in keeping with the rest of IP's dlpi handling.
3049 	 * (Remember, it's coming from something else in the kernel
3050 	 * address space)
3051 	 */
3052 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3053 	if (capend > mp->b_wptr) {
3054 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3055 		    "malformed sub-capability too long for mblk");
3056 		return;
3057 	}
3058 
3059 	/*
3060 	 * There are two types of acks we process here:
3061 	 * 1. acks in reply to a (first form) generic capability req
3062 	 *    (no ENABLE flag set)
3063 	 * 2. acks in reply to a ENABLE capability req.
3064 	 *    (ENABLE flag set)
3065 	 */
3066 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3067 
3068 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3069 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3070 		    "unsupported hardware checksum "
3071 		    "sub-capability (version %d, expected %d)",
3072 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3073 		return;
3074 	}
3075 
3076 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3077 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3078 		    "checksum capability isn't as expected; pass-thru "
3079 		    "module(s) detected, discarding capability\n"));
3080 		return;
3081 	}
3082 
3083 #define	CURR_HCKSUM_CAPAB				\
3084 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3085 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3086 
3087 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3088 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3089 		/* do ENABLE processing */
3090 		if (*ill_hcksum == NULL) {
3091 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3092 			    KM_NOSLEEP);
3093 
3094 			if (*ill_hcksum == NULL) {
3095 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3096 				    "could not enable hcksum version %d "
3097 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3098 				    ill->ill_name);
3099 				return;
3100 			}
3101 		}
3102 
3103 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3104 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3105 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3106 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3107 		    "has enabled hardware checksumming\n ",
3108 		    ill->ill_name));
3109 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3110 		/*
3111 		 * Enabling hardware checksum offload
3112 		 * Currently IP supports {TCP,UDP}/IPv4
3113 		 * partial and full cksum offload and
3114 		 * IPv4 header checksum offload.
3115 		 * Allocate new mblk which will
3116 		 * contain a new capability request
3117 		 * to enable hardware checksum offload.
3118 		 */
3119 		uint_t	size;
3120 		uchar_t	*rptr;
3121 
3122 		size = sizeof (dl_capability_req_t) +
3123 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3124 
3125 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3126 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3127 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3128 			    ill->ill_name);
3129 			return;
3130 		}
3131 
3132 		rptr = nmp->b_rptr;
3133 		/* initialize dl_capability_req_t */
3134 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3135 		ocap->dl_sub_offset =
3136 		    sizeof (dl_capability_req_t);
3137 		ocap->dl_sub_length =
3138 		    sizeof (dl_capability_sub_t) +
3139 		    isub->dl_length;
3140 		nmp->b_rptr += sizeof (dl_capability_req_t);
3141 
3142 		/* initialize dl_capability_sub_t */
3143 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3144 		nmp->b_rptr += sizeof (*isub);
3145 
3146 		/* initialize dl_capab_hcksum_t */
3147 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3148 		bcopy(ihck, ohck, sizeof (*ihck));
3149 
3150 		nmp->b_rptr = rptr;
3151 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3152 
3153 		/* Set ENABLE flag */
3154 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3155 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3156 
3157 		/*
3158 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3159 		 * hardware checksum acceleration.
3160 		 */
3161 		ill_dlpi_send(ill, nmp);
3162 	} else {
3163 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3164 		    "advertised %x hardware checksum capability flags\n",
3165 		    ill->ill_name, ihck->hcksum_txflags));
3166 	}
3167 }
3168 
3169 static void
3170 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3171 {
3172 	mblk_t *mp;
3173 	dl_capab_hcksum_t *hck_subcap;
3174 	dl_capability_sub_t *dl_subcap;
3175 	int size;
3176 
3177 	if (!ILL_HCKSUM_CAPABLE(ill))
3178 		return;
3179 
3180 	ASSERT(ill->ill_hcksum_capab != NULL);
3181 	/*
3182 	 * Clear the capability flag for hardware checksum offload but
3183 	 * retain the ill_hcksum_capab structure since it's possible that
3184 	 * another thread is still referring to it.  The structure only
3185 	 * gets deallocated when we destroy the ill.
3186 	 */
3187 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3188 
3189 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3190 
3191 	mp = allocb(size, BPRI_HI);
3192 	if (mp == NULL) {
3193 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3194 		    "request to disable hardware checksum offload\n"));
3195 		return;
3196 	}
3197 
3198 	mp->b_wptr = mp->b_rptr + size;
3199 
3200 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3201 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3202 	dl_subcap->dl_length = sizeof (*hck_subcap);
3203 
3204 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3205 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3206 	hck_subcap->hcksum_txflags = 0;
3207 
3208 	if (*sc_mp != NULL)
3209 		linkb(*sc_mp, mp);
3210 	else
3211 		*sc_mp = mp;
3212 }
3213 
3214 static void
3215 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3216 {
3217 	mblk_t *nmp = NULL;
3218 	dl_capability_req_t *oc;
3219 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3220 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3221 	uint_t sub_dl_cap = isub->dl_cap;
3222 	uint8_t *capend;
3223 
3224 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3225 
3226 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3227 
3228 	/*
3229 	 * Note: range checks here are not absolutely sufficient to
3230 	 * make us robust against malformed messages sent by drivers;
3231 	 * this is in keeping with the rest of IP's dlpi handling.
3232 	 * (Remember, it's coming from something else in the kernel
3233 	 * address space)
3234 	 */
3235 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3236 	if (capend > mp->b_wptr) {
3237 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3238 		    "malformed sub-capability too long for mblk");
3239 		return;
3240 	}
3241 
3242 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3243 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3244 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3245 		    "unsupported ZEROCOPY sub-capability (version %d, "
3246 		    "expected %d)", zc_ic->zerocopy_version,
3247 		    ZEROCOPY_VERSION_1);
3248 		return;
3249 	}
3250 
3251 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3252 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3253 		    "capability isn't as expected; pass-thru module(s) "
3254 		    "detected, discarding capability\n"));
3255 		return;
3256 	}
3257 
3258 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3259 		if (*ill_zerocopy_capab == NULL) {
3260 			*ill_zerocopy_capab =
3261 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3262 			    KM_NOSLEEP);
3263 
3264 			if (*ill_zerocopy_capab == NULL) {
3265 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3266 				    "could not enable Zero-copy version %d "
3267 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3268 				    ill->ill_name);
3269 				return;
3270 			}
3271 		}
3272 
3273 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3274 		    "supports Zero-copy version %d\n", ill->ill_name,
3275 		    ZEROCOPY_VERSION_1));
3276 
3277 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3278 		    zc_ic->zerocopy_version;
3279 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3280 		    zc_ic->zerocopy_flags;
3281 
3282 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3283 	} else {
3284 		uint_t size;
3285 		uchar_t *rptr;
3286 
3287 		size = sizeof (dl_capability_req_t) +
3288 		    sizeof (dl_capability_sub_t) +
3289 		    sizeof (dl_capab_zerocopy_t);
3290 
3291 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3292 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3293 			    "could not enable zerocopy for %s (ENOMEM)\n",
3294 			    ill->ill_name);
3295 			return;
3296 		}
3297 
3298 		rptr = nmp->b_rptr;
3299 		/* initialize dl_capability_req_t */
3300 		oc = (dl_capability_req_t *)rptr;
3301 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3302 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3303 		    sizeof (dl_capab_zerocopy_t);
3304 		rptr += sizeof (dl_capability_req_t);
3305 
3306 		/* initialize dl_capability_sub_t */
3307 		bcopy(isub, rptr, sizeof (*isub));
3308 		rptr += sizeof (*isub);
3309 
3310 		/* initialize dl_capab_zerocopy_t */
3311 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3312 		*zc_oc = *zc_ic;
3313 
3314 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3315 		    "to enable zero-copy version %d\n", ill->ill_name,
3316 		    ZEROCOPY_VERSION_1));
3317 
3318 		/* set VMSAFE_MEM flag */
3319 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3320 
3321 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3322 		ill_dlpi_send(ill, nmp);
3323 	}
3324 }
3325 
3326 static void
3327 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3328 {
3329 	mblk_t *mp;
3330 	dl_capab_zerocopy_t *zerocopy_subcap;
3331 	dl_capability_sub_t *dl_subcap;
3332 	int size;
3333 
3334 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3335 		return;
3336 
3337 	ASSERT(ill->ill_zerocopy_capab != NULL);
3338 	/*
3339 	 * Clear the capability flag for Zero-copy but retain the
3340 	 * ill_zerocopy_capab structure since it's possible that another
3341 	 * thread is still referring to it.  The structure only gets
3342 	 * deallocated when we destroy the ill.
3343 	 */
3344 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3345 
3346 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3347 
3348 	mp = allocb(size, BPRI_HI);
3349 	if (mp == NULL) {
3350 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3351 		    "request to disable Zero-copy\n"));
3352 		return;
3353 	}
3354 
3355 	mp->b_wptr = mp->b_rptr + size;
3356 
3357 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3358 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3359 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3360 
3361 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3362 	zerocopy_subcap->zerocopy_version =
3363 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3364 	zerocopy_subcap->zerocopy_flags = 0;
3365 
3366 	if (*sc_mp != NULL)
3367 		linkb(*sc_mp, mp);
3368 	else
3369 		*sc_mp = mp;
3370 }
3371 
3372 /*
3373  * Process Large Segment Offload capability negotiation ack received from a
3374  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3375  * DL_CAPABILITY_ACK message.
3376  */
3377 static void
3378 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3379 {
3380 	mblk_t *nmp = NULL;
3381 	dl_capability_req_t *oc;
3382 	dl_capab_lso_t *lso_ic, *lso_oc;
3383 	ill_lso_capab_t **ill_lso_capab;
3384 	uint_t sub_dl_cap = isub->dl_cap;
3385 	uint8_t *capend;
3386 
3387 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3388 
3389 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3390 
3391 	/*
3392 	 * Note: range checks here are not absolutely sufficient to
3393 	 * make us robust against malformed messages sent by drivers;
3394 	 * this is in keeping with the rest of IP's dlpi handling.
3395 	 * (Remember, it's coming from something else in the kernel
3396 	 * address space)
3397 	 */
3398 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3399 	if (capend > mp->b_wptr) {
3400 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3401 		    "malformed sub-capability too long for mblk");
3402 		return;
3403 	}
3404 
3405 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3406 
3407 	if (lso_ic->lso_version != LSO_VERSION_1) {
3408 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3409 		    "unsupported LSO sub-capability (version %d, expected %d)",
3410 		    lso_ic->lso_version, LSO_VERSION_1);
3411 		return;
3412 	}
3413 
3414 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3415 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3416 		    "capability isn't as expected; pass-thru module(s) "
3417 		    "detected, discarding capability\n"));
3418 		return;
3419 	}
3420 
3421 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3422 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3423 		if (*ill_lso_capab == NULL) {
3424 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3425 			    KM_NOSLEEP);
3426 
3427 			if (*ill_lso_capab == NULL) {
3428 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3429 				    "could not enable LSO version %d "
3430 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3431 				    ill->ill_name);
3432 				return;
3433 			}
3434 		}
3435 
3436 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3437 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3438 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3439 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3440 
3441 		ip1dbg(("ill_capability_lso_ack: interface %s "
3442 		    "has enabled LSO\n ", ill->ill_name));
3443 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3444 		uint_t size;
3445 		uchar_t *rptr;
3446 
3447 		size = sizeof (dl_capability_req_t) +
3448 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3449 
3450 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3451 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3452 			    "could not enable LSO for %s (ENOMEM)\n",
3453 			    ill->ill_name);
3454 			return;
3455 		}
3456 
3457 		rptr = nmp->b_rptr;
3458 		/* initialize dl_capability_req_t */
3459 		oc = (dl_capability_req_t *)nmp->b_rptr;
3460 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3461 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3462 		    sizeof (dl_capab_lso_t);
3463 		nmp->b_rptr += sizeof (dl_capability_req_t);
3464 
3465 		/* initialize dl_capability_sub_t */
3466 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3467 		nmp->b_rptr += sizeof (*isub);
3468 
3469 		/* initialize dl_capab_lso_t */
3470 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3471 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3472 
3473 		nmp->b_rptr = rptr;
3474 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3475 
3476 		/* set ENABLE flag */
3477 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3478 
3479 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3480 		ill_dlpi_send(ill, nmp);
3481 	} else {
3482 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3483 		    "advertised %x LSO capability flags\n",
3484 		    ill->ill_name, lso_ic->lso_flags));
3485 	}
3486 }
3487 
3488 static void
3489 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3490 {
3491 	mblk_t *mp;
3492 	dl_capab_lso_t *lso_subcap;
3493 	dl_capability_sub_t *dl_subcap;
3494 	int size;
3495 
3496 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3497 		return;
3498 
3499 	ASSERT(ill->ill_lso_capab != NULL);
3500 	/*
3501 	 * Clear the capability flag for LSO but retain the
3502 	 * ill_lso_capab structure since it's possible that another
3503 	 * thread is still referring to it.  The structure only gets
3504 	 * deallocated when we destroy the ill.
3505 	 */
3506 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3507 
3508 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3509 
3510 	mp = allocb(size, BPRI_HI);
3511 	if (mp == NULL) {
3512 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3513 		    "request to disable LSO\n"));
3514 		return;
3515 	}
3516 
3517 	mp->b_wptr = mp->b_rptr + size;
3518 
3519 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3520 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3521 	dl_subcap->dl_length = sizeof (*lso_subcap);
3522 
3523 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3524 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3525 	lso_subcap->lso_flags = 0;
3526 
3527 	if (*sc_mp != NULL)
3528 		linkb(*sc_mp, mp);
3529 	else
3530 		*sc_mp = mp;
3531 }
3532 
3533 /*
3534  * Consume a new-style hardware capabilities negotiation ack.
3535  * Called from ip_rput_dlpi_writer().
3536  */
3537 void
3538 ill_capability_ack(ill_t *ill, mblk_t *mp)
3539 {
3540 	dl_capability_ack_t *capp;
3541 	dl_capability_sub_t *subp, *endp;
3542 
3543 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3544 		ill->ill_dlpi_capab_state = IDS_OK;
3545 
3546 	capp = (dl_capability_ack_t *)mp->b_rptr;
3547 
3548 	if (capp->dl_sub_length == 0)
3549 		/* no new-style capabilities */
3550 		return;
3551 
3552 	/* make sure the driver supplied correct dl_sub_length */
3553 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3554 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3555 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3556 		return;
3557 	}
3558 
3559 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3560 	/*
3561 	 * There are sub-capabilities. Process the ones we know about.
3562 	 * Loop until we don't have room for another sub-cap header..
3563 	 */
3564 	for (subp = SC(capp, capp->dl_sub_offset),
3565 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3566 	    subp <= endp;
3567 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3568 
3569 		switch (subp->dl_cap) {
3570 		case DL_CAPAB_ID_WRAPPER:
3571 			ill_capability_id_ack(ill, mp, subp);
3572 			break;
3573 		default:
3574 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3575 			break;
3576 		}
3577 	}
3578 #undef SC
3579 }
3580 
3581 /*
3582  * This routine is called to scan the fragmentation reassembly table for
3583  * the specified ILL for any packets that are starting to smell.
3584  * dead_interval is the maximum time in seconds that will be tolerated.  It
3585  * will either be the value specified in ip_g_frag_timeout, or zero if the
3586  * ILL is shutting down and it is time to blow everything off.
3587  *
3588  * It returns the number of seconds (as a time_t) that the next frag timer
3589  * should be scheduled for, 0 meaning that the timer doesn't need to be
3590  * re-started.  Note that the method of calculating next_timeout isn't
3591  * entirely accurate since time will flow between the time we grab
3592  * current_time and the time we schedule the next timeout.  This isn't a
3593  * big problem since this is the timer for sending an ICMP reassembly time
3594  * exceeded messages, and it doesn't have to be exactly accurate.
3595  *
3596  * This function is
3597  * sometimes called as writer, although this is not required.
3598  */
3599 time_t
3600 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3601 {
3602 	ipfb_t	*ipfb;
3603 	ipfb_t	*endp;
3604 	ipf_t	*ipf;
3605 	ipf_t	*ipfnext;
3606 	mblk_t	*mp;
3607 	time_t	current_time = gethrestime_sec();
3608 	time_t	next_timeout = 0;
3609 	uint32_t	hdr_length;
3610 	mblk_t	*send_icmp_head;
3611 	mblk_t	*send_icmp_head_v6;
3612 	zoneid_t zoneid;
3613 	ip_stack_t *ipst = ill->ill_ipst;
3614 
3615 	ipfb = ill->ill_frag_hash_tbl;
3616 	if (ipfb == NULL)
3617 		return (B_FALSE);
3618 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3619 	/* Walk the frag hash table. */
3620 	for (; ipfb < endp; ipfb++) {
3621 		send_icmp_head = NULL;
3622 		send_icmp_head_v6 = NULL;
3623 		mutex_enter(&ipfb->ipfb_lock);
3624 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3625 			time_t frag_time = current_time - ipf->ipf_timestamp;
3626 			time_t frag_timeout;
3627 
3628 			if (frag_time < dead_interval) {
3629 				/*
3630 				 * There are some outstanding fragments
3631 				 * that will timeout later.  Make note of
3632 				 * the time so that we can reschedule the
3633 				 * next timeout appropriately.
3634 				 */
3635 				frag_timeout = dead_interval - frag_time;
3636 				if (next_timeout == 0 ||
3637 				    frag_timeout < next_timeout) {
3638 					next_timeout = frag_timeout;
3639 				}
3640 				break;
3641 			}
3642 			/* Time's up.  Get it out of here. */
3643 			hdr_length = ipf->ipf_nf_hdr_len;
3644 			ipfnext = ipf->ipf_hash_next;
3645 			if (ipfnext)
3646 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3647 			*ipf->ipf_ptphn = ipfnext;
3648 			mp = ipf->ipf_mp->b_cont;
3649 			for (; mp; mp = mp->b_cont) {
3650 				/* Extra points for neatness. */
3651 				IP_REASS_SET_START(mp, 0);
3652 				IP_REASS_SET_END(mp, 0);
3653 			}
3654 			mp = ipf->ipf_mp->b_cont;
3655 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
3656 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3657 			ipfb->ipfb_count -= ipf->ipf_count;
3658 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3659 			ipfb->ipfb_frag_pkts--;
3660 			/*
3661 			 * We do not send any icmp message from here because
3662 			 * we currently are holding the ipfb_lock for this
3663 			 * hash chain. If we try and send any icmp messages
3664 			 * from here we may end up via a put back into ip
3665 			 * trying to get the same lock, causing a recursive
3666 			 * mutex panic. Instead we build a list and send all
3667 			 * the icmp messages after we have dropped the lock.
3668 			 */
3669 			if (ill->ill_isv6) {
3670 				if (hdr_length != 0) {
3671 					mp->b_next = send_icmp_head_v6;
3672 					send_icmp_head_v6 = mp;
3673 				} else {
3674 					freemsg(mp);
3675 				}
3676 			} else {
3677 				if (hdr_length != 0) {
3678 					mp->b_next = send_icmp_head;
3679 					send_icmp_head = mp;
3680 				} else {
3681 					freemsg(mp);
3682 				}
3683 			}
3684 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3685 			freeb(ipf->ipf_mp);
3686 		}
3687 		mutex_exit(&ipfb->ipfb_lock);
3688 		/*
3689 		 * Now need to send any icmp messages that we delayed from
3690 		 * above.
3691 		 */
3692 		while (send_icmp_head_v6 != NULL) {
3693 			ip6_t *ip6h;
3694 
3695 			mp = send_icmp_head_v6;
3696 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3697 			mp->b_next = NULL;
3698 			if (mp->b_datap->db_type == M_CTL)
3699 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3700 			else
3701 				ip6h = (ip6_t *)mp->b_rptr;
3702 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3703 			    ill, ipst);
3704 			if (zoneid == ALL_ZONES) {
3705 				freemsg(mp);
3706 			} else {
3707 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3708 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3709 				    B_FALSE, zoneid, ipst);
3710 			}
3711 		}
3712 		while (send_icmp_head != NULL) {
3713 			ipaddr_t dst;
3714 
3715 			mp = send_icmp_head;
3716 			send_icmp_head = send_icmp_head->b_next;
3717 			mp->b_next = NULL;
3718 
3719 			if (mp->b_datap->db_type == M_CTL)
3720 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3721 			else
3722 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3723 
3724 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3725 			if (zoneid == ALL_ZONES) {
3726 				freemsg(mp);
3727 			} else {
3728 				icmp_time_exceeded(ill->ill_wq, mp,
3729 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3730 				    ipst);
3731 			}
3732 		}
3733 	}
3734 	/*
3735 	 * A non-dying ILL will use the return value to decide whether to
3736 	 * restart the frag timer, and for how long.
3737 	 */
3738 	return (next_timeout);
3739 }
3740 
3741 /*
3742  * This routine is called when the approximate count of mblk memory used
3743  * for the specified ILL has exceeded max_count.
3744  */
3745 void
3746 ill_frag_prune(ill_t *ill, uint_t max_count)
3747 {
3748 	ipfb_t	*ipfb;
3749 	ipf_t	*ipf;
3750 	size_t	count;
3751 
3752 	/*
3753 	 * If we are here within ip_min_frag_prune_time msecs remove
3754 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3755 	 * ill_frag_free_num_pkts.
3756 	 */
3757 	mutex_enter(&ill->ill_lock);
3758 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3759 	    (ip_min_frag_prune_time != 0 ?
3760 	    ip_min_frag_prune_time : msec_per_tick)) {
3761 
3762 		ill->ill_frag_free_num_pkts++;
3763 
3764 	} else {
3765 		ill->ill_frag_free_num_pkts = 0;
3766 	}
3767 	ill->ill_last_frag_clean_time = lbolt;
3768 	mutex_exit(&ill->ill_lock);
3769 
3770 	/*
3771 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3772 	 */
3773 	if (ill->ill_frag_free_num_pkts != 0) {
3774 		int ix;
3775 
3776 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3777 			ipfb = &ill->ill_frag_hash_tbl[ix];
3778 			mutex_enter(&ipfb->ipfb_lock);
3779 			if (ipfb->ipfb_ipf != NULL) {
3780 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3781 				    ill->ill_frag_free_num_pkts);
3782 			}
3783 			mutex_exit(&ipfb->ipfb_lock);
3784 		}
3785 	}
3786 	/*
3787 	 * While the reassembly list for this ILL is too big, prune a fragment
3788 	 * queue by age, oldest first.
3789 	 */
3790 	while (ill->ill_frag_count > max_count) {
3791 		int	ix;
3792 		ipfb_t	*oipfb = NULL;
3793 		uint_t	oldest = UINT_MAX;
3794 
3795 		count = 0;
3796 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3797 			ipfb = &ill->ill_frag_hash_tbl[ix];
3798 			mutex_enter(&ipfb->ipfb_lock);
3799 			ipf = ipfb->ipfb_ipf;
3800 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3801 				oldest = ipf->ipf_gen;
3802 				oipfb = ipfb;
3803 			}
3804 			count += ipfb->ipfb_count;
3805 			mutex_exit(&ipfb->ipfb_lock);
3806 		}
3807 		if (oipfb == NULL)
3808 			break;
3809 
3810 		if (count <= max_count)
3811 			return;	/* Somebody beat us to it, nothing to do */
3812 		mutex_enter(&oipfb->ipfb_lock);
3813 		ipf = oipfb->ipfb_ipf;
3814 		if (ipf != NULL) {
3815 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3816 		}
3817 		mutex_exit(&oipfb->ipfb_lock);
3818 	}
3819 }
3820 
3821 /*
3822  * free 'free_cnt' fragmented packets starting at ipf.
3823  */
3824 void
3825 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3826 {
3827 	size_t	count;
3828 	mblk_t	*mp;
3829 	mblk_t	*tmp;
3830 	ipf_t **ipfp = ipf->ipf_ptphn;
3831 
3832 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3833 	ASSERT(ipfp != NULL);
3834 	ASSERT(ipf != NULL);
3835 
3836 	while (ipf != NULL && free_cnt-- > 0) {
3837 		count = ipf->ipf_count;
3838 		mp = ipf->ipf_mp;
3839 		ipf = ipf->ipf_hash_next;
3840 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3841 			IP_REASS_SET_START(tmp, 0);
3842 			IP_REASS_SET_END(tmp, 0);
3843 		}
3844 		atomic_add_32(&ill->ill_frag_count, -count);
3845 		ASSERT(ipfb->ipfb_count >= count);
3846 		ipfb->ipfb_count -= count;
3847 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3848 		ipfb->ipfb_frag_pkts--;
3849 		freemsg(mp);
3850 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3851 	}
3852 
3853 	if (ipf)
3854 		ipf->ipf_ptphn = ipfp;
3855 	ipfp[0] = ipf;
3856 }
3857 
3858 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3859 	"obsolete and may be removed in a future release of Solaris.  Use " \
3860 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3861 
3862 /*
3863  * For obsolete per-interface forwarding configuration;
3864  * called in response to ND_GET.
3865  */
3866 /* ARGSUSED */
3867 static int
3868 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3869 {
3870 	ill_t *ill = (ill_t *)cp;
3871 
3872 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3873 
3874 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3875 	return (0);
3876 }
3877 
3878 /*
3879  * For obsolete per-interface forwarding configuration;
3880  * called in response to ND_SET.
3881  */
3882 /* ARGSUSED */
3883 static int
3884 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3885     cred_t *ioc_cr)
3886 {
3887 	long value;
3888 	int retval;
3889 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3890 
3891 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3892 
3893 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3894 	    value < 0 || value > 1) {
3895 		return (EINVAL);
3896 	}
3897 
3898 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3899 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3900 	rw_exit(&ipst->ips_ill_g_lock);
3901 	return (retval);
3902 }
3903 
3904 /*
3905  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3906  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3907  * up RTS_IFINFO routing socket messages for each interface whose flags we
3908  * change.
3909  */
3910 int
3911 ill_forward_set(ill_t *ill, boolean_t enable)
3912 {
3913 	ill_group_t *illgrp;
3914 	ip_stack_t	*ipst = ill->ill_ipst;
3915 
3916 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3917 
3918 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3919 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3920 		return (0);
3921 
3922 	if (IS_LOOPBACK(ill))
3923 		return (EINVAL);
3924 
3925 	/*
3926 	 * If the ill is in an IPMP group, set the forwarding policy on all
3927 	 * members of the group to the same value.
3928 	 */
3929 	illgrp = ill->ill_group;
3930 	if (illgrp != NULL) {
3931 		ill_t *tmp_ill;
3932 
3933 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3934 		    tmp_ill = tmp_ill->ill_group_next) {
3935 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3936 			    (enable ? "Enabling" : "Disabling"),
3937 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3938 			    tmp_ill->ill_name));
3939 			mutex_enter(&tmp_ill->ill_lock);
3940 			if (enable)
3941 				tmp_ill->ill_flags |= ILLF_ROUTER;
3942 			else
3943 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3944 			mutex_exit(&tmp_ill->ill_lock);
3945 			if (tmp_ill->ill_isv6)
3946 				ill_set_nce_router_flags(tmp_ill, enable);
3947 			/* Notify routing socket listeners of this change. */
3948 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3949 		}
3950 	} else {
3951 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3952 		    (enable ? "Enabling" : "Disabling"),
3953 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3954 		mutex_enter(&ill->ill_lock);
3955 		if (enable)
3956 			ill->ill_flags |= ILLF_ROUTER;
3957 		else
3958 			ill->ill_flags &= ~ILLF_ROUTER;
3959 		mutex_exit(&ill->ill_lock);
3960 		if (ill->ill_isv6)
3961 			ill_set_nce_router_flags(ill, enable);
3962 		/* Notify routing socket listeners of this change. */
3963 		ip_rts_ifmsg(ill->ill_ipif);
3964 	}
3965 
3966 	return (0);
3967 }
3968 
3969 /*
3970  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3971  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3972  * set or clear.
3973  */
3974 static void
3975 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3976 {
3977 	ipif_t *ipif;
3978 	nce_t *nce;
3979 
3980 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3981 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3982 		if (nce != NULL) {
3983 			mutex_enter(&nce->nce_lock);
3984 			if (enable)
3985 				nce->nce_flags |= NCE_F_ISROUTER;
3986 			else
3987 				nce->nce_flags &= ~NCE_F_ISROUTER;
3988 			mutex_exit(&nce->nce_lock);
3989 			NCE_REFRELE(nce);
3990 		}
3991 	}
3992 }
3993 
3994 /*
3995  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3996  * for this ill.  Make sure the v6/v4 question has been answered about this
3997  * ill.  The creation of this ndd variable is only for backwards compatibility.
3998  * The preferred way to control per-interface IP forwarding is through the
3999  * ILLF_ROUTER interface flag.
4000  */
4001 static int
4002 ill_set_ndd_name(ill_t *ill)
4003 {
4004 	char *suffix;
4005 	ip_stack_t	*ipst = ill->ill_ipst;
4006 
4007 	ASSERT(IAM_WRITER_ILL(ill));
4008 
4009 	if (ill->ill_isv6)
4010 		suffix = ipv6_forward_suffix;
4011 	else
4012 		suffix = ipv4_forward_suffix;
4013 
4014 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4015 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4016 	/*
4017 	 * Copies over the '\0'.
4018 	 * Note that strlen(suffix) is always bounded.
4019 	 */
4020 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4021 	    strlen(suffix) + 1);
4022 
4023 	/*
4024 	 * Use of the nd table requires holding the reader lock.
4025 	 * Modifying the nd table thru nd_load/nd_unload requires
4026 	 * the writer lock.
4027 	 */
4028 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4029 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4030 	    nd_ill_forward_set, (caddr_t)ill)) {
4031 		/*
4032 		 * If the nd_load failed, it only meant that it could not
4033 		 * allocate a new bunch of room for further NDD expansion.
4034 		 * Because of that, the ill_ndd_name will be set to 0, and
4035 		 * this interface is at the mercy of the global ip_forwarding
4036 		 * variable.
4037 		 */
4038 		rw_exit(&ipst->ips_ip_g_nd_lock);
4039 		ill->ill_ndd_name = NULL;
4040 		return (ENOMEM);
4041 	}
4042 	rw_exit(&ipst->ips_ip_g_nd_lock);
4043 	return (0);
4044 }
4045 
4046 /*
4047  * Intializes the context structure and returns the first ill in the list
4048  * cuurently start_list and end_list can have values:
4049  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4050  * IP_V4_G_HEAD		Traverse IPV4 list only.
4051  * IP_V6_G_HEAD		Traverse IPV6 list only.
4052  */
4053 
4054 /*
4055  * We don't check for CONDEMNED ills here. Caller must do that if
4056  * necessary under the ill lock.
4057  */
4058 ill_t *
4059 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4060     ip_stack_t *ipst)
4061 {
4062 	ill_if_t *ifp;
4063 	ill_t *ill;
4064 	avl_tree_t *avl_tree;
4065 
4066 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4067 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4068 
4069 	/*
4070 	 * setup the lists to search
4071 	 */
4072 	if (end_list != MAX_G_HEADS) {
4073 		ctx->ctx_current_list = start_list;
4074 		ctx->ctx_last_list = end_list;
4075 	} else {
4076 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4077 		ctx->ctx_current_list = 0;
4078 	}
4079 
4080 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4081 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4082 		if (ifp != (ill_if_t *)
4083 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4084 			avl_tree = &ifp->illif_avl_by_ppa;
4085 			ill = avl_first(avl_tree);
4086 			/*
4087 			 * ill is guaranteed to be non NULL or ifp should have
4088 			 * not existed.
4089 			 */
4090 			ASSERT(ill != NULL);
4091 			return (ill);
4092 		}
4093 		ctx->ctx_current_list++;
4094 	}
4095 
4096 	return (NULL);
4097 }
4098 
4099 /*
4100  * returns the next ill in the list. ill_first() must have been called
4101  * before calling ill_next() or bad things will happen.
4102  */
4103 
4104 /*
4105  * We don't check for CONDEMNED ills here. Caller must do that if
4106  * necessary under the ill lock.
4107  */
4108 ill_t *
4109 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4110 {
4111 	ill_if_t *ifp;
4112 	ill_t *ill;
4113 	ip_stack_t	*ipst = lastill->ill_ipst;
4114 
4115 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4116 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4117 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4118 	    AVL_AFTER)) != NULL) {
4119 		return (ill);
4120 	}
4121 
4122 	/* goto next ill_ifp in the list. */
4123 	ifp = lastill->ill_ifptr->illif_next;
4124 
4125 	/* make sure not at end of circular list */
4126 	while (ifp ==
4127 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4128 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4129 			return (NULL);
4130 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4131 	}
4132 
4133 	return (avl_first(&ifp->illif_avl_by_ppa));
4134 }
4135 
4136 /*
4137  * Check interface name for correct format which is name+ppa.
4138  * name can contain characters and digits, the right most digits
4139  * make up the ppa number. use of octal is not allowed, name must contain
4140  * a ppa, return pointer to the start of ppa.
4141  * In case of error return NULL.
4142  */
4143 static char *
4144 ill_get_ppa_ptr(char *name)
4145 {
4146 	int namelen = mi_strlen(name);
4147 
4148 	int len = namelen;
4149 
4150 	name += len;
4151 	while (len > 0) {
4152 		name--;
4153 		if (*name < '0' || *name > '9')
4154 			break;
4155 		len--;
4156 	}
4157 
4158 	/* empty string, all digits, or no trailing digits */
4159 	if (len == 0 || len == (int)namelen)
4160 		return (NULL);
4161 
4162 	name++;
4163 	/* check for attempted use of octal */
4164 	if (*name == '0' && len != (int)namelen - 1)
4165 		return (NULL);
4166 	return (name);
4167 }
4168 
4169 /*
4170  * use avl tree to locate the ill.
4171  */
4172 static ill_t *
4173 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4174     ipsq_func_t func, int *error, ip_stack_t *ipst)
4175 {
4176 	char *ppa_ptr = NULL;
4177 	int len;
4178 	uint_t ppa;
4179 	ill_t *ill = NULL;
4180 	ill_if_t *ifp;
4181 	int list;
4182 	ipsq_t *ipsq;
4183 
4184 	if (error != NULL)
4185 		*error = 0;
4186 
4187 	/*
4188 	 * get ppa ptr
4189 	 */
4190 	if (isv6)
4191 		list = IP_V6_G_HEAD;
4192 	else
4193 		list = IP_V4_G_HEAD;
4194 
4195 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4196 		if (error != NULL)
4197 			*error = ENXIO;
4198 		return (NULL);
4199 	}
4200 
4201 	len = ppa_ptr - name + 1;
4202 
4203 	ppa = stoi(&ppa_ptr);
4204 
4205 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4206 
4207 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4208 		/*
4209 		 * match is done on len - 1 as the name is not null
4210 		 * terminated it contains ppa in addition to the interface
4211 		 * name.
4212 		 */
4213 		if ((ifp->illif_name_len == len) &&
4214 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4215 			break;
4216 		} else {
4217 			ifp = ifp->illif_next;
4218 		}
4219 	}
4220 
4221 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4222 		/*
4223 		 * Even the interface type does not exist.
4224 		 */
4225 		if (error != NULL)
4226 			*error = ENXIO;
4227 		return (NULL);
4228 	}
4229 
4230 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4231 	if (ill != NULL) {
4232 		/*
4233 		 * The block comment at the start of ipif_down
4234 		 * explains the use of the macros used below
4235 		 */
4236 		GRAB_CONN_LOCK(q);
4237 		mutex_enter(&ill->ill_lock);
4238 		if (ILL_CAN_LOOKUP(ill)) {
4239 			ill_refhold_locked(ill);
4240 			mutex_exit(&ill->ill_lock);
4241 			RELEASE_CONN_LOCK(q);
4242 			return (ill);
4243 		} else if (ILL_CAN_WAIT(ill, q)) {
4244 			ipsq = ill->ill_phyint->phyint_ipsq;
4245 			mutex_enter(&ipsq->ipsq_lock);
4246 			mutex_exit(&ill->ill_lock);
4247 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4248 			mutex_exit(&ipsq->ipsq_lock);
4249 			RELEASE_CONN_LOCK(q);
4250 			if (error != NULL)
4251 				*error = EINPROGRESS;
4252 			return (NULL);
4253 		}
4254 		mutex_exit(&ill->ill_lock);
4255 		RELEASE_CONN_LOCK(q);
4256 	}
4257 	if (error != NULL)
4258 		*error = ENXIO;
4259 	return (NULL);
4260 }
4261 
4262 /*
4263  * comparison function for use with avl.
4264  */
4265 static int
4266 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4267 {
4268 	uint_t ppa;
4269 	uint_t ill_ppa;
4270 
4271 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4272 
4273 	ppa = *((uint_t *)ppa_ptr);
4274 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4275 	/*
4276 	 * We want the ill with the lowest ppa to be on the
4277 	 * top.
4278 	 */
4279 	if (ill_ppa < ppa)
4280 		return (1);
4281 	if (ill_ppa > ppa)
4282 		return (-1);
4283 	return (0);
4284 }
4285 
4286 /*
4287  * remove an interface type from the global list.
4288  */
4289 static void
4290 ill_delete_interface_type(ill_if_t *interface)
4291 {
4292 	ASSERT(interface != NULL);
4293 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4294 
4295 	avl_destroy(&interface->illif_avl_by_ppa);
4296 	if (interface->illif_ppa_arena != NULL)
4297 		vmem_destroy(interface->illif_ppa_arena);
4298 
4299 	remque(interface);
4300 
4301 	mi_free(interface);
4302 }
4303 
4304 /*
4305  * remove ill from the global list.
4306  */
4307 static void
4308 ill_glist_delete(ill_t *ill)
4309 {
4310 	hook_nic_event_t *info;
4311 	ip_stack_t	*ipst;
4312 
4313 	if (ill == NULL)
4314 		return;
4315 	ipst = ill->ill_ipst;
4316 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4317 
4318 	/*
4319 	 * If the ill was never inserted into the AVL tree
4320 	 * we skip the if branch.
4321 	 */
4322 	if (ill->ill_ifptr != NULL) {
4323 		/*
4324 		 * remove from AVL tree and free ppa number
4325 		 */
4326 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4327 
4328 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4329 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4330 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4331 		}
4332 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4333 			ill_delete_interface_type(ill->ill_ifptr);
4334 		}
4335 
4336 		/*
4337 		 * Indicate ill is no longer in the list.
4338 		 */
4339 		ill->ill_ifptr = NULL;
4340 		ill->ill_name_length = 0;
4341 		ill->ill_name[0] = '\0';
4342 		ill->ill_ppa = UINT_MAX;
4343 	}
4344 
4345 	/*
4346 	 * Run the unplumb hook after the NIC has disappeared from being
4347 	 * visible so that attempts to revalidate its existance will fail.
4348 	 *
4349 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4350 	 * that the ordering of delivered events to listeners matches the
4351 	 * order of them in the kernel.
4352 	 */
4353 	info = ill->ill_nic_event_info;
4354 	if (info != NULL && info->hne_event == NE_DOWN) {
4355 		mutex_enter(&ill->ill_lock);
4356 		ill_nic_info_dispatch(ill);
4357 		mutex_exit(&ill->ill_lock);
4358 	}
4359 
4360 	/* Generate NE_UNPLUMB event for ill_name. */
4361 	(void) ill_hook_event_create(ill, 0, NE_UNPLUMB, ill->ill_name,
4362 	    ill->ill_name_length);
4363 
4364 	ill_phyint_free(ill);
4365 	rw_exit(&ipst->ips_ill_g_lock);
4366 }
4367 
4368 /*
4369  * allocate a ppa, if the number of plumbed interfaces of this type are
4370  * less than ill_no_arena do a linear search to find a unused ppa.
4371  * When the number goes beyond ill_no_arena switch to using an arena.
4372  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4373  * is the return value for an error condition, so allocation starts at one
4374  * and is decremented by one.
4375  */
4376 static int
4377 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4378 {
4379 	ill_t *tmp_ill;
4380 	uint_t start, end;
4381 	int ppa;
4382 
4383 	if (ifp->illif_ppa_arena == NULL &&
4384 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4385 		/*
4386 		 * Create an arena.
4387 		 */
4388 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4389 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4390 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4391 			/* allocate what has already been assigned */
4392 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4393 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4394 		    tmp_ill, AVL_AFTER)) {
4395 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4396 			    1,		/* size */
4397 			    1,		/* align/quantum */
4398 			    0,		/* phase */
4399 			    0,		/* nocross */
4400 			    /* minaddr */
4401 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4402 			    /* maxaddr */
4403 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4404 			    VM_NOSLEEP|VM_FIRSTFIT);
4405 			if (ppa == 0) {
4406 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4407 				    " failed while switching"));
4408 				vmem_destroy(ifp->illif_ppa_arena);
4409 				ifp->illif_ppa_arena = NULL;
4410 				break;
4411 			}
4412 		}
4413 	}
4414 
4415 	if (ifp->illif_ppa_arena != NULL) {
4416 		if (ill->ill_ppa == UINT_MAX) {
4417 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4418 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4419 			if (ppa == 0)
4420 				return (EAGAIN);
4421 			ill->ill_ppa = --ppa;
4422 		} else {
4423 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4424 			    1, 		/* size */
4425 			    1, 		/* align/quantum */
4426 			    0, 		/* phase */
4427 			    0, 		/* nocross */
4428 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4429 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4430 			    VM_NOSLEEP|VM_FIRSTFIT);
4431 			/*
4432 			 * Most likely the allocation failed because
4433 			 * the requested ppa was in use.
4434 			 */
4435 			if (ppa == 0)
4436 				return (EEXIST);
4437 		}
4438 		return (0);
4439 	}
4440 
4441 	/*
4442 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4443 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4444 	 */
4445 	if (ill->ill_ppa == UINT_MAX) {
4446 		end = UINT_MAX - 1;
4447 		start = 0;
4448 	} else {
4449 		end = start = ill->ill_ppa;
4450 	}
4451 
4452 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4453 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4454 		if (start++ >= end) {
4455 			if (ill->ill_ppa == UINT_MAX)
4456 				return (EAGAIN);
4457 			else
4458 				return (EEXIST);
4459 		}
4460 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4461 	}
4462 	ill->ill_ppa = start;
4463 	return (0);
4464 }
4465 
4466 /*
4467  * Insert ill into the list of configured ill's. Once this function completes,
4468  * the ill is globally visible and is available through lookups. More precisely
4469  * this happens after the caller drops the ill_g_lock.
4470  */
4471 static int
4472 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4473 {
4474 	ill_if_t *ill_interface;
4475 	avl_index_t where = 0;
4476 	int error;
4477 	int name_length;
4478 	int index;
4479 	boolean_t check_length = B_FALSE;
4480 	ip_stack_t	*ipst = ill->ill_ipst;
4481 
4482 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4483 
4484 	name_length = mi_strlen(name) + 1;
4485 
4486 	if (isv6)
4487 		index = IP_V6_G_HEAD;
4488 	else
4489 		index = IP_V4_G_HEAD;
4490 
4491 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4492 	/*
4493 	 * Search for interface type based on name
4494 	 */
4495 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4496 		if ((ill_interface->illif_name_len == name_length) &&
4497 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4498 			break;
4499 		}
4500 		ill_interface = ill_interface->illif_next;
4501 	}
4502 
4503 	/*
4504 	 * Interface type not found, create one.
4505 	 */
4506 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4507 
4508 		ill_g_head_t ghead;
4509 
4510 		/*
4511 		 * allocate ill_if_t structure
4512 		 */
4513 
4514 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4515 		if (ill_interface == NULL) {
4516 			return (ENOMEM);
4517 		}
4518 
4519 
4520 
4521 		(void) strcpy(ill_interface->illif_name, name);
4522 		ill_interface->illif_name_len = name_length;
4523 
4524 		avl_create(&ill_interface->illif_avl_by_ppa,
4525 		    ill_compare_ppa, sizeof (ill_t),
4526 		    offsetof(struct ill_s, ill_avl_byppa));
4527 
4528 		/*
4529 		 * link the structure in the back to maintain order
4530 		 * of configuration for ifconfig output.
4531 		 */
4532 		ghead = ipst->ips_ill_g_heads[index];
4533 		insque(ill_interface, ghead.ill_g_list_tail);
4534 
4535 	}
4536 
4537 	if (ill->ill_ppa == UINT_MAX)
4538 		check_length = B_TRUE;
4539 
4540 	error = ill_alloc_ppa(ill_interface, ill);
4541 	if (error != 0) {
4542 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4543 			ill_delete_interface_type(ill->ill_ifptr);
4544 		return (error);
4545 	}
4546 
4547 	/*
4548 	 * When the ppa is choosen by the system, check that there is
4549 	 * enough space to insert ppa. if a specific ppa was passed in this
4550 	 * check is not required as the interface name passed in will have
4551 	 * the right ppa in it.
4552 	 */
4553 	if (check_length) {
4554 		/*
4555 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4556 		 */
4557 		char buf[sizeof (uint_t) * 3];
4558 
4559 		/*
4560 		 * convert ppa to string to calculate the amount of space
4561 		 * required for it in the name.
4562 		 */
4563 		numtos(ill->ill_ppa, buf);
4564 
4565 		/* Do we have enough space to insert ppa ? */
4566 
4567 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4568 			/* Free ppa and interface type struct */
4569 			if (ill_interface->illif_ppa_arena != NULL) {
4570 				vmem_free(ill_interface->illif_ppa_arena,
4571 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4572 			}
4573 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4574 				ill_delete_interface_type(ill->ill_ifptr);
4575 
4576 			return (EINVAL);
4577 		}
4578 	}
4579 
4580 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4581 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4582 
4583 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4584 	    &where);
4585 	ill->ill_ifptr = ill_interface;
4586 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4587 
4588 	ill_phyint_reinit(ill);
4589 	return (0);
4590 }
4591 
4592 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4593 static boolean_t
4594 ipsq_init(ill_t *ill)
4595 {
4596 	ipsq_t  *ipsq;
4597 
4598 	/* Init the ipsq and impicitly enter as writer */
4599 	ill->ill_phyint->phyint_ipsq =
4600 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4601 	if (ill->ill_phyint->phyint_ipsq == NULL)
4602 		return (B_FALSE);
4603 	ipsq = ill->ill_phyint->phyint_ipsq;
4604 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4605 	ill->ill_phyint->phyint_ipsq_next = NULL;
4606 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4607 	ipsq->ipsq_refs = 1;
4608 	ipsq->ipsq_writer = curthread;
4609 	ipsq->ipsq_reentry_cnt = 1;
4610 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4611 #ifdef DEBUG
4612 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack,
4613 	    IPSQ_STACK_DEPTH);
4614 #endif
4615 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4616 	return (B_TRUE);
4617 }
4618 
4619 /*
4620  * ill_init is called by ip_open when a device control stream is opened.
4621  * It does a few initializations, and shoots a DL_INFO_REQ message down
4622  * to the driver.  The response is later picked up in ip_rput_dlpi and
4623  * used to set up default mechanisms for talking to the driver.  (Always
4624  * called as writer.)
4625  *
4626  * If this function returns error, ip_open will call ip_close which in
4627  * turn will call ill_delete to clean up any memory allocated here that
4628  * is not yet freed.
4629  */
4630 int
4631 ill_init(queue_t *q, ill_t *ill)
4632 {
4633 	int	count;
4634 	dl_info_req_t	*dlir;
4635 	mblk_t	*info_mp;
4636 	uchar_t *frag_ptr;
4637 
4638 	/*
4639 	 * The ill is initialized to zero by mi_alloc*(). In addition
4640 	 * some fields already contain valid values, initialized in
4641 	 * ip_open(), before we reach here.
4642 	 */
4643 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4644 
4645 	ill->ill_rq = q;
4646 	ill->ill_wq = WR(q);
4647 
4648 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4649 	    BPRI_HI);
4650 	if (info_mp == NULL)
4651 		return (ENOMEM);
4652 
4653 	/*
4654 	 * Allocate sufficient space to contain our fragment hash table and
4655 	 * the device name.
4656 	 */
4657 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4658 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4659 	if (frag_ptr == NULL) {
4660 		freemsg(info_mp);
4661 		return (ENOMEM);
4662 	}
4663 	ill->ill_frag_ptr = frag_ptr;
4664 	ill->ill_frag_free_num_pkts = 0;
4665 	ill->ill_last_frag_clean_time = 0;
4666 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4667 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4668 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4669 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4670 		    NULL, MUTEX_DEFAULT, NULL);
4671 	}
4672 
4673 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4674 	if (ill->ill_phyint == NULL) {
4675 		freemsg(info_mp);
4676 		mi_free(frag_ptr);
4677 		return (ENOMEM);
4678 	}
4679 
4680 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4681 	/*
4682 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4683 	 * at this point because of the following reason. If we can't
4684 	 * enter the ipsq at some point and cv_wait, the writer that
4685 	 * wakes us up tries to locate us using the list of all phyints
4686 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4687 	 * If we don't set it now, we risk a missed wakeup.
4688 	 */
4689 	ill->ill_phyint->phyint_illv4 = ill;
4690 	ill->ill_ppa = UINT_MAX;
4691 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4692 
4693 	if (!ipsq_init(ill)) {
4694 		freemsg(info_mp);
4695 		mi_free(frag_ptr);
4696 		mi_free(ill->ill_phyint);
4697 		return (ENOMEM);
4698 	}
4699 
4700 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4701 
4702 	/* Frag queue limit stuff */
4703 	ill->ill_frag_count = 0;
4704 	ill->ill_ipf_gen = 0;
4705 
4706 	ill->ill_global_timer = INFINITY;
4707 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4708 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4709 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4710 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4711 
4712 	/*
4713 	 * Initialize IPv6 configuration variables.  The IP module is always
4714 	 * opened as an IPv4 module.  Instead tracking down the cases where
4715 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4716 	 * here for convenience, this has no effect until the ill is set to do
4717 	 * IPv6.
4718 	 */
4719 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4720 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4721 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4722 	ill->ill_max_buf = ND_MAX_Q;
4723 	ill->ill_refcnt = 0;
4724 
4725 	/* Send down the Info Request to the driver. */
4726 	info_mp->b_datap->db_type = M_PCPROTO;
4727 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4728 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4729 	dlir->dl_primitive = DL_INFO_REQ;
4730 
4731 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4732 
4733 	qprocson(q);
4734 	ill_dlpi_send(ill, info_mp);
4735 
4736 	return (0);
4737 }
4738 
4739 /*
4740  * ill_dls_info
4741  * creates datalink socket info from the device.
4742  */
4743 int
4744 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4745 {
4746 	size_t	len;
4747 	ill_t	*ill = ipif->ipif_ill;
4748 
4749 	sdl->sdl_family = AF_LINK;
4750 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4751 	sdl->sdl_type = ill->ill_type;
4752 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4753 	len = strlen(sdl->sdl_data);
4754 	ASSERT(len < 256);
4755 	sdl->sdl_nlen = (uchar_t)len;
4756 	sdl->sdl_alen = ill->ill_phys_addr_length;
4757 	sdl->sdl_slen = 0;
4758 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4759 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4760 
4761 	return (sizeof (struct sockaddr_dl));
4762 }
4763 
4764 /*
4765  * ill_xarp_info
4766  * creates xarp info from the device.
4767  */
4768 static int
4769 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4770 {
4771 	sdl->sdl_family = AF_LINK;
4772 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4773 	sdl->sdl_type = ill->ill_type;
4774 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4775 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4776 	sdl->sdl_alen = ill->ill_phys_addr_length;
4777 	sdl->sdl_slen = 0;
4778 	return (sdl->sdl_nlen);
4779 }
4780 
4781 static int
4782 loopback_kstat_update(kstat_t *ksp, int rw)
4783 {
4784 	kstat_named_t *kn;
4785 	netstackid_t	stackid;
4786 	netstack_t	*ns;
4787 	ip_stack_t	*ipst;
4788 
4789 	if (ksp == NULL || ksp->ks_data == NULL)
4790 		return (EIO);
4791 
4792 	if (rw == KSTAT_WRITE)
4793 		return (EACCES);
4794 
4795 	kn = KSTAT_NAMED_PTR(ksp);
4796 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4797 
4798 	ns = netstack_find_by_stackid(stackid);
4799 	if (ns == NULL)
4800 		return (-1);
4801 
4802 	ipst = ns->netstack_ip;
4803 	if (ipst == NULL) {
4804 		netstack_rele(ns);
4805 		return (-1);
4806 	}
4807 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4808 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4809 	netstack_rele(ns);
4810 	return (0);
4811 }
4812 
4813 /*
4814  * Has ifindex been plumbed already.
4815  * Compares both phyint_ifindex and phyint_group_ifindex.
4816  */
4817 static boolean_t
4818 phyint_exists(uint_t index, ip_stack_t *ipst)
4819 {
4820 	phyint_t *phyi;
4821 
4822 	ASSERT(index != 0);
4823 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4824 	/*
4825 	 * Indexes are stored in the phyint - a common structure
4826 	 * to both IPv4 and IPv6.
4827 	 */
4828 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4829 	for (; phyi != NULL;
4830 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4831 	    phyi, AVL_AFTER)) {
4832 		if (phyi->phyint_ifindex == index ||
4833 		    phyi->phyint_group_ifindex == index)
4834 			return (B_TRUE);
4835 	}
4836 	return (B_FALSE);
4837 }
4838 
4839 /* Pick a unique ifindex */
4840 boolean_t
4841 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4842 {
4843 	uint_t starting_index;
4844 
4845 	if (!ipst->ips_ill_index_wrap) {
4846 		*indexp = ipst->ips_ill_index++;
4847 		if (ipst->ips_ill_index == 0) {
4848 			/* Reached the uint_t limit Next time wrap  */
4849 			ipst->ips_ill_index_wrap = B_TRUE;
4850 		}
4851 		return (B_TRUE);
4852 	}
4853 
4854 	/*
4855 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4856 	 * at this point and don't want to call any function that attempts
4857 	 * to get the lock again.
4858 	 */
4859 	starting_index = ipst->ips_ill_index++;
4860 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4861 		if (ipst->ips_ill_index != 0 &&
4862 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4863 			/* found unused index - use it */
4864 			*indexp = ipst->ips_ill_index;
4865 			return (B_TRUE);
4866 		}
4867 	}
4868 
4869 	/*
4870 	 * all interface indicies are inuse.
4871 	 */
4872 	return (B_FALSE);
4873 }
4874 
4875 /*
4876  * Assign a unique interface index for the phyint.
4877  */
4878 static boolean_t
4879 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4880 {
4881 	ASSERT(phyi->phyint_ifindex == 0);
4882 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4883 }
4884 
4885 /*
4886  * Return a pointer to the ill which matches the supplied name.  Note that
4887  * the ill name length includes the null termination character.  (May be
4888  * called as writer.)
4889  * If do_alloc and the interface is "lo0" it will be automatically created.
4890  * Cannot bump up reference on condemned ills. So dup detect can't be done
4891  * using this func.
4892  */
4893 ill_t *
4894 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4895     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4896     ip_stack_t *ipst)
4897 {
4898 	ill_t	*ill;
4899 	ipif_t	*ipif;
4900 	kstat_named_t	*kn;
4901 	boolean_t isloopback;
4902 	ipsq_t *old_ipsq;
4903 	in6_addr_t ov6addr;
4904 
4905 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4906 
4907 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4908 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4909 	rw_exit(&ipst->ips_ill_g_lock);
4910 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4911 		return (ill);
4912 
4913 	/*
4914 	 * Couldn't find it.  Does this happen to be a lookup for the
4915 	 * loopback device and are we allowed to allocate it?
4916 	 */
4917 	if (!isloopback || !do_alloc)
4918 		return (NULL);
4919 
4920 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4921 
4922 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4923 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4924 		rw_exit(&ipst->ips_ill_g_lock);
4925 		return (ill);
4926 	}
4927 
4928 	/* Create the loopback device on demand */
4929 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4930 	    sizeof (ipif_loopback_name), BPRI_MED));
4931 	if (ill == NULL)
4932 		goto done;
4933 
4934 	*ill = ill_null;
4935 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4936 	ill->ill_ipst = ipst;
4937 	netstack_hold(ipst->ips_netstack);
4938 	/*
4939 	 * For exclusive stacks we set the zoneid to zero
4940 	 * to make IP operate as if in the global zone.
4941 	 */
4942 	ill->ill_zoneid = GLOBAL_ZONEID;
4943 
4944 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4945 	if (ill->ill_phyint == NULL)
4946 		goto done;
4947 
4948 	if (isv6)
4949 		ill->ill_phyint->phyint_illv6 = ill;
4950 	else
4951 		ill->ill_phyint->phyint_illv4 = ill;
4952 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4953 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4954 	/* Add room for tcp+ip headers */
4955 	if (isv6) {
4956 		ill->ill_isv6 = B_TRUE;
4957 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4958 	} else {
4959 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4960 	}
4961 	if (!ill_allocate_mibs(ill))
4962 		goto done;
4963 	ill->ill_max_mtu = ill->ill_max_frag;
4964 	/*
4965 	 * ipif_loopback_name can't be pointed at directly because its used
4966 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4967 	 * from the glist, ill_glist_delete() sets the first character of
4968 	 * ill_name to '\0'.
4969 	 */
4970 	ill->ill_name = (char *)ill + sizeof (*ill);
4971 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4972 	ill->ill_name_length = sizeof (ipif_loopback_name);
4973 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4974 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4975 
4976 	ill->ill_global_timer = INFINITY;
4977 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4978 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4979 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4980 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4981 
4982 	/* No resolver here. */
4983 	ill->ill_net_type = IRE_LOOPBACK;
4984 
4985 	/* Initialize the ipsq */
4986 	if (!ipsq_init(ill))
4987 		goto done;
4988 
4989 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
4990 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
4991 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
4992 #ifdef DEBUG
4993 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
4994 #endif
4995 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
4996 	if (ipif == NULL)
4997 		goto done;
4998 
4999 	ill->ill_flags = ILLF_MULTICAST;
5000 
5001 	ov6addr = ipif->ipif_v6lcl_addr;
5002 	/* Set up default loopback address and mask. */
5003 	if (!isv6) {
5004 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5005 
5006 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5007 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5008 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5009 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5010 		    ipif->ipif_v6subnet);
5011 		ill->ill_flags |= ILLF_IPV4;
5012 	} else {
5013 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5014 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5015 		ipif->ipif_v6net_mask = ipv6_all_ones;
5016 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5017 		    ipif->ipif_v6subnet);
5018 		ill->ill_flags |= ILLF_IPV6;
5019 	}
5020 
5021 	/*
5022 	 * Chain us in at the end of the ill list. hold the ill
5023 	 * before we make it globally visible. 1 for the lookup.
5024 	 */
5025 	ill->ill_refcnt = 0;
5026 	ill_refhold(ill);
5027 
5028 	ill->ill_frag_count = 0;
5029 	ill->ill_frag_free_num_pkts = 0;
5030 	ill->ill_last_frag_clean_time = 0;
5031 
5032 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5033 
5034 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5035 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5036 
5037 	/* Let SCTP know so that it can add this to its list */
5038 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5039 
5040 	/*
5041 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5042 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5043 	 * requires to be after ill_glist_insert() since we need the
5044 	 * ill_index set. Pass on ipv6_loopback as the old address.
5045 	 */
5046 	sctp_update_ipif_addr(ipif, ov6addr);
5047 
5048 	/*
5049 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5050 	 */
5051 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5052 		/* Loopback ills aren't in any IPMP group */
5053 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5054 		ipsq_delete(old_ipsq);
5055 	}
5056 
5057 	/*
5058 	 * Delay this till the ipif is allocated as ipif_allocate
5059 	 * de-references ill_phyint for getting the ifindex. We
5060 	 * can't do this before ipif_allocate because ill_phyint_reinit
5061 	 * -> phyint_assign_ifindex expects ipif to be present.
5062 	 */
5063 	mutex_enter(&ill->ill_phyint->phyint_lock);
5064 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5065 	mutex_exit(&ill->ill_phyint->phyint_lock);
5066 
5067 	if (ipst->ips_loopback_ksp == NULL) {
5068 		/* Export loopback interface statistics */
5069 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5070 		    ipif_loopback_name, "net",
5071 		    KSTAT_TYPE_NAMED, 2, 0,
5072 		    ipst->ips_netstack->netstack_stackid);
5073 		if (ipst->ips_loopback_ksp != NULL) {
5074 			ipst->ips_loopback_ksp->ks_update =
5075 			    loopback_kstat_update;
5076 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5077 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5078 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5079 			ipst->ips_loopback_ksp->ks_private =
5080 			    (void *)(uintptr_t)ipst->ips_netstack->
5081 			    netstack_stackid;
5082 			kstat_install(ipst->ips_loopback_ksp);
5083 		}
5084 	}
5085 
5086 	if (error != NULL)
5087 		*error = 0;
5088 	*did_alloc = B_TRUE;
5089 	rw_exit(&ipst->ips_ill_g_lock);
5090 	return (ill);
5091 done:
5092 	if (ill != NULL) {
5093 		if (ill->ill_phyint != NULL) {
5094 			ipsq_t	*ipsq;
5095 
5096 			ipsq = ill->ill_phyint->phyint_ipsq;
5097 			if (ipsq != NULL) {
5098 				ipsq->ipsq_ipst = NULL;
5099 				kmem_free(ipsq, sizeof (ipsq_t));
5100 			}
5101 			mi_free(ill->ill_phyint);
5102 		}
5103 		ill_free_mib(ill);
5104 		if (ill->ill_ipst != NULL)
5105 			netstack_rele(ill->ill_ipst->ips_netstack);
5106 		mi_free(ill);
5107 	}
5108 	rw_exit(&ipst->ips_ill_g_lock);
5109 	if (error != NULL)
5110 		*error = ENOMEM;
5111 	return (NULL);
5112 }
5113 
5114 /*
5115  * For IPP calls - use the ip_stack_t for global stack.
5116  */
5117 ill_t *
5118 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5119     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5120 {
5121 	ip_stack_t	*ipst;
5122 	ill_t		*ill;
5123 
5124 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5125 	if (ipst == NULL) {
5126 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5127 		return (NULL);
5128 	}
5129 
5130 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5131 	netstack_rele(ipst->ips_netstack);
5132 	return (ill);
5133 }
5134 
5135 /*
5136  * Return a pointer to the ill which matches the index and IP version type.
5137  */
5138 ill_t *
5139 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5140     ipsq_func_t func, int *err, ip_stack_t *ipst)
5141 {
5142 	ill_t	*ill;
5143 	ipsq_t  *ipsq;
5144 	phyint_t *phyi;
5145 
5146 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5147 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5148 
5149 	if (err != NULL)
5150 		*err = 0;
5151 
5152 	/*
5153 	 * Indexes are stored in the phyint - a common structure
5154 	 * to both IPv4 and IPv6.
5155 	 */
5156 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5157 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5158 	    (void *) &index, NULL);
5159 	if (phyi != NULL) {
5160 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5161 		if (ill != NULL) {
5162 			/*
5163 			 * The block comment at the start of ipif_down
5164 			 * explains the use of the macros used below
5165 			 */
5166 			GRAB_CONN_LOCK(q);
5167 			mutex_enter(&ill->ill_lock);
5168 			if (ILL_CAN_LOOKUP(ill)) {
5169 				ill_refhold_locked(ill);
5170 				mutex_exit(&ill->ill_lock);
5171 				RELEASE_CONN_LOCK(q);
5172 				rw_exit(&ipst->ips_ill_g_lock);
5173 				return (ill);
5174 			} else if (ILL_CAN_WAIT(ill, q)) {
5175 				ipsq = ill->ill_phyint->phyint_ipsq;
5176 				mutex_enter(&ipsq->ipsq_lock);
5177 				rw_exit(&ipst->ips_ill_g_lock);
5178 				mutex_exit(&ill->ill_lock);
5179 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5180 				mutex_exit(&ipsq->ipsq_lock);
5181 				RELEASE_CONN_LOCK(q);
5182 				if (err != NULL)
5183 					*err = EINPROGRESS;
5184 				return (NULL);
5185 			}
5186 			RELEASE_CONN_LOCK(q);
5187 			mutex_exit(&ill->ill_lock);
5188 		}
5189 	}
5190 	rw_exit(&ipst->ips_ill_g_lock);
5191 	if (err != NULL)
5192 		*err = ENXIO;
5193 	return (NULL);
5194 }
5195 
5196 /*
5197  * Return the ifindex next in sequence after the passed in ifindex.
5198  * If there is no next ifindex for the given protocol, return 0.
5199  */
5200 uint_t
5201 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5202 {
5203 	phyint_t *phyi;
5204 	phyint_t *phyi_initial;
5205 	uint_t   ifindex;
5206 
5207 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5208 
5209 	if (index == 0) {
5210 		phyi = avl_first(
5211 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5212 	} else {
5213 		phyi = phyi_initial = avl_find(
5214 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5215 		    (void *) &index, NULL);
5216 	}
5217 
5218 	for (; phyi != NULL;
5219 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5220 	    phyi, AVL_AFTER)) {
5221 		/*
5222 		 * If we're not returning the first interface in the tree
5223 		 * and we still haven't moved past the phyint_t that
5224 		 * corresponds to index, avl_walk needs to be called again
5225 		 */
5226 		if (!((index != 0) && (phyi == phyi_initial))) {
5227 			if (isv6) {
5228 				if ((phyi->phyint_illv6) &&
5229 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5230 				    (phyi->phyint_illv6->ill_isv6 == 1))
5231 					break;
5232 			} else {
5233 				if ((phyi->phyint_illv4) &&
5234 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5235 				    (phyi->phyint_illv4->ill_isv6 == 0))
5236 					break;
5237 			}
5238 		}
5239 	}
5240 
5241 	rw_exit(&ipst->ips_ill_g_lock);
5242 
5243 	if (phyi != NULL)
5244 		ifindex = phyi->phyint_ifindex;
5245 	else
5246 		ifindex = 0;
5247 
5248 	return (ifindex);
5249 }
5250 
5251 /*
5252  * Return the ifindex for the named interface.
5253  * If there is no next ifindex for the interface, return 0.
5254  */
5255 uint_t
5256 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5257 {
5258 	phyint_t	*phyi;
5259 	avl_index_t	where = 0;
5260 	uint_t		ifindex;
5261 
5262 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5263 
5264 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5265 	    name, &where)) == NULL) {
5266 		rw_exit(&ipst->ips_ill_g_lock);
5267 		return (0);
5268 	}
5269 
5270 	ifindex = phyi->phyint_ifindex;
5271 
5272 	rw_exit(&ipst->ips_ill_g_lock);
5273 
5274 	return (ifindex);
5275 }
5276 
5277 /*
5278  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5279  * that gives a running thread a reference to the ill. This reference must be
5280  * released by the thread when it is done accessing the ill and related
5281  * objects. ill_refcnt can not be used to account for static references
5282  * such as other structures pointing to an ill. Callers must generally
5283  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5284  * or be sure that the ill is not being deleted or changing state before
5285  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5286  * ill won't change any of its critical state such as address, netmask etc.
5287  */
5288 void
5289 ill_refhold(ill_t *ill)
5290 {
5291 	mutex_enter(&ill->ill_lock);
5292 	ill->ill_refcnt++;
5293 	ILL_TRACE_REF(ill);
5294 	mutex_exit(&ill->ill_lock);
5295 }
5296 
5297 void
5298 ill_refhold_locked(ill_t *ill)
5299 {
5300 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5301 	ill->ill_refcnt++;
5302 	ILL_TRACE_REF(ill);
5303 }
5304 
5305 int
5306 ill_check_and_refhold(ill_t *ill)
5307 {
5308 	mutex_enter(&ill->ill_lock);
5309 	if (ILL_CAN_LOOKUP(ill)) {
5310 		ill_refhold_locked(ill);
5311 		mutex_exit(&ill->ill_lock);
5312 		return (0);
5313 	}
5314 	mutex_exit(&ill->ill_lock);
5315 	return (ILL_LOOKUP_FAILED);
5316 }
5317 
5318 /*
5319  * Must not be called while holding any locks. Otherwise if this is
5320  * the last reference to be released, there is a chance of recursive mutex
5321  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5322  * to restart an ioctl.
5323  */
5324 void
5325 ill_refrele(ill_t *ill)
5326 {
5327 	mutex_enter(&ill->ill_lock);
5328 	ASSERT(ill->ill_refcnt != 0);
5329 	ill->ill_refcnt--;
5330 	ILL_UNTRACE_REF(ill);
5331 	if (ill->ill_refcnt != 0) {
5332 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5333 		mutex_exit(&ill->ill_lock);
5334 		return;
5335 	}
5336 
5337 	/* Drops the ill_lock */
5338 	ipif_ill_refrele_tail(ill);
5339 }
5340 
5341 /*
5342  * Obtain a weak reference count on the ill. This reference ensures the
5343  * ill won't be freed, but the ill may change any of its critical state
5344  * such as netmask, address etc. Returns an error if the ill has started
5345  * closing.
5346  */
5347 boolean_t
5348 ill_waiter_inc(ill_t *ill)
5349 {
5350 	mutex_enter(&ill->ill_lock);
5351 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5352 		mutex_exit(&ill->ill_lock);
5353 		return (B_FALSE);
5354 	}
5355 	ill->ill_waiters++;
5356 	mutex_exit(&ill->ill_lock);
5357 	return (B_TRUE);
5358 }
5359 
5360 void
5361 ill_waiter_dcr(ill_t *ill)
5362 {
5363 	mutex_enter(&ill->ill_lock);
5364 	ill->ill_waiters--;
5365 	if (ill->ill_waiters == 0)
5366 		cv_broadcast(&ill->ill_cv);
5367 	mutex_exit(&ill->ill_lock);
5368 }
5369 
5370 /*
5371  * Named Dispatch routine to produce a formatted report on all ILLs.
5372  * This report is accessed by using the ndd utility to "get" ND variable
5373  * "ip_ill_status".
5374  */
5375 /* ARGSUSED */
5376 int
5377 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5378 {
5379 	ill_t		*ill;
5380 	ill_walk_context_t ctx;
5381 	ip_stack_t	*ipst;
5382 
5383 	ipst = CONNQ_TO_IPST(q);
5384 
5385 	(void) mi_mpprintf(mp,
5386 	    "ILL      " MI_COL_HDRPAD_STR
5387 	/*   01234567[89ABCDEF] */
5388 	    "rq       " MI_COL_HDRPAD_STR
5389 	/*   01234567[89ABCDEF] */
5390 	    "wq       " MI_COL_HDRPAD_STR
5391 	/*   01234567[89ABCDEF] */
5392 	    "upcnt mxfrg err name");
5393 	/*   12345 12345 123 xxxxxxxx  */
5394 
5395 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5396 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5397 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5398 		(void) mi_mpprintf(mp,
5399 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5400 		    "%05u %05u %03d %s",
5401 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5402 		    ill->ill_ipif_up_count,
5403 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5404 	}
5405 	rw_exit(&ipst->ips_ill_g_lock);
5406 
5407 	return (0);
5408 }
5409 
5410 /*
5411  * Named Dispatch routine to produce a formatted report on all IPIFs.
5412  * This report is accessed by using the ndd utility to "get" ND variable
5413  * "ip_ipif_status".
5414  */
5415 /* ARGSUSED */
5416 int
5417 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5418 {
5419 	char	buf1[INET6_ADDRSTRLEN];
5420 	char	buf2[INET6_ADDRSTRLEN];
5421 	char	buf3[INET6_ADDRSTRLEN];
5422 	char	buf4[INET6_ADDRSTRLEN];
5423 	char	buf5[INET6_ADDRSTRLEN];
5424 	char	buf6[INET6_ADDRSTRLEN];
5425 	char	buf[LIFNAMSIZ];
5426 	ill_t	*ill;
5427 	ipif_t	*ipif;
5428 	nv_t	*nvp;
5429 	uint64_t flags;
5430 	zoneid_t zoneid;
5431 	ill_walk_context_t ctx;
5432 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5433 
5434 	(void) mi_mpprintf(mp,
5435 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5436 	    "\tlocal address\n"
5437 	    "\tsrc address\n"
5438 	    "\tsubnet\n"
5439 	    "\tmask\n"
5440 	    "\tbroadcast\n"
5441 	    "\tp-p-dst");
5442 
5443 	ASSERT(q->q_next == NULL);
5444 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5445 
5446 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5447 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5448 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5449 		for (ipif = ill->ill_ipif; ipif != NULL;
5450 		    ipif = ipif->ipif_next) {
5451 			if (zoneid != GLOBAL_ZONEID &&
5452 			    zoneid != ipif->ipif_zoneid &&
5453 			    ipif->ipif_zoneid != ALL_ZONES)
5454 				continue;
5455 
5456 			ipif_get_name(ipif, buf, sizeof (buf));
5457 			(void) mi_mpprintf(mp,
5458 			    MI_COL_PTRFMT_STR
5459 			    "%04u %05u %u/%u/%u %s %d",
5460 			    (void *)ipif,
5461 			    ipif->ipif_metric, ipif->ipif_mtu,
5462 			    ipif->ipif_ib_pkt_count,
5463 			    ipif->ipif_ob_pkt_count,
5464 			    ipif->ipif_fo_pkt_count,
5465 			    buf,
5466 			    ipif->ipif_zoneid);
5467 
5468 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5469 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5470 
5471 		/* Tack on text strings for any flags. */
5472 		nvp = ipif_nv_tbl;
5473 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5474 			if (nvp->nv_value & flags)
5475 				(void) mi_mpprintf_nr(mp, " %s",
5476 				    nvp->nv_name);
5477 		}
5478 		(void) mi_mpprintf(mp,
5479 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5480 		    inet_ntop(AF_INET6,
5481 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5482 		    inet_ntop(AF_INET6,
5483 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5484 		    inet_ntop(AF_INET6,
5485 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5486 		    inet_ntop(AF_INET6,
5487 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5488 		    inet_ntop(AF_INET6,
5489 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5490 		    inet_ntop(AF_INET6,
5491 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5492 		}
5493 	}
5494 	rw_exit(&ipst->ips_ill_g_lock);
5495 	return (0);
5496 }
5497 
5498 /*
5499  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5500  * driver.  We construct best guess defaults for lower level information that
5501  * we need.  If an interface is brought up without injection of any overriding
5502  * information from outside, we have to be ready to go with these defaults.
5503  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5504  * we primarely want the dl_provider_style.
5505  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5506  * at which point we assume the other part of the information is valid.
5507  */
5508 void
5509 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5510 {
5511 	uchar_t		*brdcst_addr;
5512 	uint_t		brdcst_addr_length, phys_addr_length;
5513 	t_scalar_t	sap_length;
5514 	dl_info_ack_t	*dlia;
5515 	ip_m_t		*ipm;
5516 	dl_qos_cl_sel1_t *sel1;
5517 
5518 	ASSERT(IAM_WRITER_ILL(ill));
5519 
5520 	/*
5521 	 * Till the ill is fully up ILL_CHANGING will be set and
5522 	 * the ill is not globally visible. So no need for a lock.
5523 	 */
5524 	dlia = (dl_info_ack_t *)mp->b_rptr;
5525 	ill->ill_mactype = dlia->dl_mac_type;
5526 
5527 	ipm = ip_m_lookup(dlia->dl_mac_type);
5528 	if (ipm == NULL) {
5529 		ipm = ip_m_lookup(DL_OTHER);
5530 		ASSERT(ipm != NULL);
5531 	}
5532 	ill->ill_media = ipm;
5533 
5534 	/*
5535 	 * When the new DLPI stuff is ready we'll pull lengths
5536 	 * from dlia.
5537 	 */
5538 	if (dlia->dl_version == DL_VERSION_2) {
5539 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5540 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5541 		    brdcst_addr_length);
5542 		if (brdcst_addr == NULL) {
5543 			brdcst_addr_length = 0;
5544 		}
5545 		sap_length = dlia->dl_sap_length;
5546 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5547 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5548 		    brdcst_addr_length, sap_length, phys_addr_length));
5549 	} else {
5550 		brdcst_addr_length = 6;
5551 		brdcst_addr = ip_six_byte_all_ones;
5552 		sap_length = -2;
5553 		phys_addr_length = brdcst_addr_length;
5554 	}
5555 
5556 	ill->ill_bcast_addr_length = brdcst_addr_length;
5557 	ill->ill_phys_addr_length = phys_addr_length;
5558 	ill->ill_sap_length = sap_length;
5559 	ill->ill_max_frag = dlia->dl_max_sdu;
5560 	ill->ill_max_mtu = ill->ill_max_frag;
5561 
5562 	ill->ill_type = ipm->ip_m_type;
5563 
5564 	if (!ill->ill_dlpi_style_set) {
5565 		if (dlia->dl_provider_style == DL_STYLE2)
5566 			ill->ill_needs_attach = 1;
5567 
5568 		/*
5569 		 * Allocate the first ipif on this ill. We don't delay it
5570 		 * further as ioctl handling assumes atleast one ipif to
5571 		 * be present.
5572 		 *
5573 		 * At this point we don't know whether the ill is v4 or v6.
5574 		 * We will know this whan the SIOCSLIFNAME happens and
5575 		 * the correct value for ill_isv6 will be assigned in
5576 		 * ipif_set_values(). We need to hold the ill lock and
5577 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5578 		 * the wakeup.
5579 		 */
5580 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5581 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5582 		mutex_enter(&ill->ill_lock);
5583 		ASSERT(ill->ill_dlpi_style_set == 0);
5584 		ill->ill_dlpi_style_set = 1;
5585 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5586 		cv_broadcast(&ill->ill_cv);
5587 		mutex_exit(&ill->ill_lock);
5588 		freemsg(mp);
5589 		return;
5590 	}
5591 	ASSERT(ill->ill_ipif != NULL);
5592 	/*
5593 	 * We know whether it is IPv4 or IPv6 now, as this is the
5594 	 * second DL_INFO_ACK we are recieving in response to the
5595 	 * DL_INFO_REQ sent in ipif_set_values.
5596 	 */
5597 	if (ill->ill_isv6)
5598 		ill->ill_sap = IP6_DL_SAP;
5599 	else
5600 		ill->ill_sap = IP_DL_SAP;
5601 	/*
5602 	 * Set ipif_mtu which is used to set the IRE's
5603 	 * ire_max_frag value. The driver could have sent
5604 	 * a different mtu from what it sent last time. No
5605 	 * need to call ipif_mtu_change because IREs have
5606 	 * not yet been created.
5607 	 */
5608 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5609 	/*
5610 	 * Clear all the flags that were set based on ill_bcast_addr_length
5611 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5612 	 * changed now and we need to re-evaluate.
5613 	 */
5614 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5615 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5616 
5617 	/*
5618 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5619 	 * changed now.
5620 	 */
5621 	if (ill->ill_bcast_addr_length == 0) {
5622 		if (ill->ill_resolver_mp != NULL)
5623 			freemsg(ill->ill_resolver_mp);
5624 		if (ill->ill_bcast_mp != NULL)
5625 			freemsg(ill->ill_bcast_mp);
5626 		if (ill->ill_flags & ILLF_XRESOLV)
5627 			ill->ill_net_type = IRE_IF_RESOLVER;
5628 		else
5629 			ill->ill_net_type = IRE_IF_NORESOLVER;
5630 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5631 		    ill->ill_phys_addr_length,
5632 		    ill->ill_sap,
5633 		    ill->ill_sap_length);
5634 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5635 
5636 		if (ill->ill_isv6)
5637 			/*
5638 			 * Note: xresolv interfaces will eventually need NOARP
5639 			 * set here as well, but that will require those
5640 			 * external resolvers to have some knowledge of
5641 			 * that flag and act appropriately. Not to be changed
5642 			 * at present.
5643 			 */
5644 			ill->ill_flags |= ILLF_NONUD;
5645 		else
5646 			ill->ill_flags |= ILLF_NOARP;
5647 
5648 		if (ill->ill_phys_addr_length == 0) {
5649 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5650 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5651 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5652 			} else {
5653 				/* pt-pt supports multicast. */
5654 				ill->ill_flags |= ILLF_MULTICAST;
5655 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5656 			}
5657 		}
5658 	} else {
5659 		ill->ill_net_type = IRE_IF_RESOLVER;
5660 		if (ill->ill_bcast_mp != NULL)
5661 			freemsg(ill->ill_bcast_mp);
5662 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5663 		    ill->ill_bcast_addr_length, ill->ill_sap,
5664 		    ill->ill_sap_length);
5665 		/*
5666 		 * Later detect lack of DLPI driver multicast
5667 		 * capability by catching DL_ENABMULTI errors in
5668 		 * ip_rput_dlpi.
5669 		 */
5670 		ill->ill_flags |= ILLF_MULTICAST;
5671 		if (!ill->ill_isv6)
5672 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5673 	}
5674 	/* By default an interface does not support any CoS marking */
5675 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5676 
5677 	/*
5678 	 * If we get QoS information in DL_INFO_ACK, the device supports
5679 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5680 	 */
5681 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5682 	    dlia->dl_qos_length);
5683 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5684 		ill->ill_flags |= ILLF_COS_ENABLED;
5685 	}
5686 
5687 	/* Clear any previous error indication. */
5688 	ill->ill_error = 0;
5689 	freemsg(mp);
5690 }
5691 
5692 /*
5693  * Perform various checks to verify that an address would make sense as a
5694  * local, remote, or subnet interface address.
5695  */
5696 static boolean_t
5697 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5698 {
5699 	ipaddr_t	net_mask;
5700 
5701 	/*
5702 	 * Don't allow all zeroes, or all ones, but allow
5703 	 * all ones netmask.
5704 	 */
5705 	if ((net_mask = ip_net_mask(addr)) == 0)
5706 		return (B_FALSE);
5707 	/* A given netmask overrides the "guess" netmask */
5708 	if (subnet_mask != 0)
5709 		net_mask = subnet_mask;
5710 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5711 	    (addr == (addr | ~net_mask)))) {
5712 		return (B_FALSE);
5713 	}
5714 
5715 	/*
5716 	 * Even if the netmask is all ones, we do not allow address to be
5717 	 * 255.255.255.255
5718 	 */
5719 	if (addr == INADDR_BROADCAST)
5720 		return (B_FALSE);
5721 
5722 	if (CLASSD(addr))
5723 		return (B_FALSE);
5724 
5725 	return (B_TRUE);
5726 }
5727 
5728 #define	V6_IPIF_LINKLOCAL(p)	\
5729 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5730 
5731 /*
5732  * Compare two given ipifs and check if the second one is better than
5733  * the first one using the order of preference (not taking deprecated
5734  * into acount) specified in ipif_lookup_multicast().
5735  */
5736 static boolean_t
5737 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5738 {
5739 	/* Check the least preferred first. */
5740 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5741 		/* If both ipifs are the same, use the first one. */
5742 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5743 			return (B_FALSE);
5744 		else
5745 			return (B_TRUE);
5746 	}
5747 
5748 	/* For IPv6, check for link local address. */
5749 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5750 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5751 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5752 			/* The second one is equal or less preferred. */
5753 			return (B_FALSE);
5754 		} else {
5755 			return (B_TRUE);
5756 		}
5757 	}
5758 
5759 	/* Then check for point to point interface. */
5760 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5761 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5762 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5763 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5764 			return (B_FALSE);
5765 		} else {
5766 			return (B_TRUE);
5767 		}
5768 	}
5769 
5770 	/* old_ipif is a normal interface, so no need to use the new one. */
5771 	return (B_FALSE);
5772 }
5773 
5774 /*
5775  * Find any non-virtual, not condemned, and up multicast capable interface
5776  * given an IP instance and zoneid.  Order of preference is:
5777  *
5778  * 1. normal
5779  * 1.1 normal, but deprecated
5780  * 2. point to point
5781  * 2.1 point to point, but deprecated
5782  * 3. link local
5783  * 3.1 link local, but deprecated
5784  * 4. loopback.
5785  */
5786 ipif_t *
5787 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5788 {
5789 	ill_t			*ill;
5790 	ill_walk_context_t	ctx;
5791 	ipif_t			*ipif;
5792 	ipif_t			*saved_ipif = NULL;
5793 	ipif_t			*dep_ipif = NULL;
5794 
5795 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5796 	if (isv6)
5797 		ill = ILL_START_WALK_V6(&ctx, ipst);
5798 	else
5799 		ill = ILL_START_WALK_V4(&ctx, ipst);
5800 
5801 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5802 		mutex_enter(&ill->ill_lock);
5803 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5804 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5805 			mutex_exit(&ill->ill_lock);
5806 			continue;
5807 		}
5808 		for (ipif = ill->ill_ipif; ipif != NULL;
5809 		    ipif = ipif->ipif_next) {
5810 			if (zoneid != ipif->ipif_zoneid &&
5811 			    zoneid != ALL_ZONES &&
5812 			    ipif->ipif_zoneid != ALL_ZONES) {
5813 				continue;
5814 			}
5815 			if (!(ipif->ipif_flags & IPIF_UP) ||
5816 			    !IPIF_CAN_LOOKUP(ipif)) {
5817 				continue;
5818 			}
5819 
5820 			/*
5821 			 * Found one candidate.  If it is deprecated,
5822 			 * remember it in dep_ipif.  If it is not deprecated,
5823 			 * remember it in saved_ipif.
5824 			 */
5825 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5826 				if (dep_ipif == NULL) {
5827 					dep_ipif = ipif;
5828 				} else if (ipif_comp_multi(dep_ipif, ipif,
5829 				    isv6)) {
5830 					/*
5831 					 * If the previous dep_ipif does not
5832 					 * belong to the same ill, we've done
5833 					 * a ipif_refhold() on it.  So we need
5834 					 * to release it.
5835 					 */
5836 					if (dep_ipif->ipif_ill != ill)
5837 						ipif_refrele(dep_ipif);
5838 					dep_ipif = ipif;
5839 				}
5840 				continue;
5841 			}
5842 			if (saved_ipif == NULL) {
5843 				saved_ipif = ipif;
5844 			} else {
5845 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5846 					if (saved_ipif->ipif_ill != ill)
5847 						ipif_refrele(saved_ipif);
5848 					saved_ipif = ipif;
5849 				}
5850 			}
5851 		}
5852 		/*
5853 		 * Before going to the next ill, do a ipif_refhold() on the
5854 		 * saved ones.
5855 		 */
5856 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5857 			ipif_refhold_locked(saved_ipif);
5858 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5859 			ipif_refhold_locked(dep_ipif);
5860 		mutex_exit(&ill->ill_lock);
5861 	}
5862 	rw_exit(&ipst->ips_ill_g_lock);
5863 
5864 	/*
5865 	 * If we have only the saved_ipif, return it.  But if we have both
5866 	 * saved_ipif and dep_ipif, check to see which one is better.
5867 	 */
5868 	if (saved_ipif != NULL) {
5869 		if (dep_ipif != NULL) {
5870 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5871 				ipif_refrele(saved_ipif);
5872 				return (dep_ipif);
5873 			} else {
5874 				ipif_refrele(dep_ipif);
5875 				return (saved_ipif);
5876 			}
5877 		}
5878 		return (saved_ipif);
5879 	} else {
5880 		return (dep_ipif);
5881 	}
5882 }
5883 
5884 /*
5885  * This function is called when an application does not specify an interface
5886  * to be used for multicast traffic (joining a group/sending data).  It
5887  * calls ire_lookup_multi() to look for an interface route for the
5888  * specified multicast group.  Doing this allows the administrator to add
5889  * prefix routes for multicast to indicate which interface to be used for
5890  * multicast traffic in the above scenario.  The route could be for all
5891  * multicast (224.0/4), for a single multicast group (a /32 route) or
5892  * anything in between.  If there is no such multicast route, we just find
5893  * any multicast capable interface and return it.  The returned ipif
5894  * is refhold'ed.
5895  */
5896 ipif_t *
5897 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5898 {
5899 	ire_t			*ire;
5900 	ipif_t			*ipif;
5901 
5902 	ire = ire_lookup_multi(group, zoneid, ipst);
5903 	if (ire != NULL) {
5904 		ipif = ire->ire_ipif;
5905 		ipif_refhold(ipif);
5906 		ire_refrele(ire);
5907 		return (ipif);
5908 	}
5909 
5910 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5911 }
5912 
5913 /*
5914  * Look for an ipif with the specified interface address and destination.
5915  * The destination address is used only for matching point-to-point interfaces.
5916  */
5917 ipif_t *
5918 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5919     ipsq_func_t func, int *error, ip_stack_t *ipst)
5920 {
5921 	ipif_t	*ipif;
5922 	ill_t	*ill;
5923 	ill_walk_context_t ctx;
5924 	ipsq_t	*ipsq;
5925 
5926 	if (error != NULL)
5927 		*error = 0;
5928 
5929 	/*
5930 	 * First match all the point-to-point interfaces
5931 	 * before looking at non-point-to-point interfaces.
5932 	 * This is done to avoid returning non-point-to-point
5933 	 * ipif instead of unnumbered point-to-point ipif.
5934 	 */
5935 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5936 	ill = ILL_START_WALK_V4(&ctx, ipst);
5937 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5938 		GRAB_CONN_LOCK(q);
5939 		mutex_enter(&ill->ill_lock);
5940 		for (ipif = ill->ill_ipif; ipif != NULL;
5941 		    ipif = ipif->ipif_next) {
5942 			/* Allow the ipif to be down */
5943 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5944 			    (ipif->ipif_lcl_addr == if_addr) &&
5945 			    (ipif->ipif_pp_dst_addr == dst)) {
5946 				/*
5947 				 * The block comment at the start of ipif_down
5948 				 * explains the use of the macros used below
5949 				 */
5950 				if (IPIF_CAN_LOOKUP(ipif)) {
5951 					ipif_refhold_locked(ipif);
5952 					mutex_exit(&ill->ill_lock);
5953 					RELEASE_CONN_LOCK(q);
5954 					rw_exit(&ipst->ips_ill_g_lock);
5955 					return (ipif);
5956 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5957 					ipsq = ill->ill_phyint->phyint_ipsq;
5958 					mutex_enter(&ipsq->ipsq_lock);
5959 					mutex_exit(&ill->ill_lock);
5960 					rw_exit(&ipst->ips_ill_g_lock);
5961 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5962 					    ill);
5963 					mutex_exit(&ipsq->ipsq_lock);
5964 					RELEASE_CONN_LOCK(q);
5965 					if (error != NULL)
5966 						*error = EINPROGRESS;
5967 					return (NULL);
5968 				}
5969 			}
5970 		}
5971 		mutex_exit(&ill->ill_lock);
5972 		RELEASE_CONN_LOCK(q);
5973 	}
5974 	rw_exit(&ipst->ips_ill_g_lock);
5975 
5976 	/* lookup the ipif based on interface address */
5977 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5978 	    ipst);
5979 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5980 	return (ipif);
5981 }
5982 
5983 /*
5984  * Look for an ipif with the specified address. For point-point links
5985  * we look for matches on either the destination address and the local
5986  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5987  * is set.
5988  * Matches on a specific ill if match_ill is set.
5989  */
5990 ipif_t *
5991 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5992     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5993 {
5994 	ipif_t  *ipif;
5995 	ill_t   *ill;
5996 	boolean_t ptp = B_FALSE;
5997 	ipsq_t	*ipsq;
5998 	ill_walk_context_t	ctx;
5999 
6000 	if (error != NULL)
6001 		*error = 0;
6002 
6003 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6004 	/*
6005 	 * Repeat twice, first based on local addresses and
6006 	 * next time for pointopoint.
6007 	 */
6008 repeat:
6009 	ill = ILL_START_WALK_V4(&ctx, ipst);
6010 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6011 		if (match_ill != NULL && ill != match_ill) {
6012 			continue;
6013 		}
6014 		GRAB_CONN_LOCK(q);
6015 		mutex_enter(&ill->ill_lock);
6016 		for (ipif = ill->ill_ipif; ipif != NULL;
6017 		    ipif = ipif->ipif_next) {
6018 			if (zoneid != ALL_ZONES &&
6019 			    zoneid != ipif->ipif_zoneid &&
6020 			    ipif->ipif_zoneid != ALL_ZONES)
6021 				continue;
6022 			/* Allow the ipif to be down */
6023 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6024 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6025 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6026 			    (ipif->ipif_pp_dst_addr == addr))) {
6027 				/*
6028 				 * The block comment at the start of ipif_down
6029 				 * explains the use of the macros used below
6030 				 */
6031 				if (IPIF_CAN_LOOKUP(ipif)) {
6032 					ipif_refhold_locked(ipif);
6033 					mutex_exit(&ill->ill_lock);
6034 					RELEASE_CONN_LOCK(q);
6035 					rw_exit(&ipst->ips_ill_g_lock);
6036 					return (ipif);
6037 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6038 					ipsq = ill->ill_phyint->phyint_ipsq;
6039 					mutex_enter(&ipsq->ipsq_lock);
6040 					mutex_exit(&ill->ill_lock);
6041 					rw_exit(&ipst->ips_ill_g_lock);
6042 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6043 					    ill);
6044 					mutex_exit(&ipsq->ipsq_lock);
6045 					RELEASE_CONN_LOCK(q);
6046 					if (error != NULL)
6047 						*error = EINPROGRESS;
6048 					return (NULL);
6049 				}
6050 			}
6051 		}
6052 		mutex_exit(&ill->ill_lock);
6053 		RELEASE_CONN_LOCK(q);
6054 	}
6055 
6056 	/* If we already did the ptp case, then we are done */
6057 	if (ptp) {
6058 		rw_exit(&ipst->ips_ill_g_lock);
6059 		if (error != NULL)
6060 			*error = ENXIO;
6061 		return (NULL);
6062 	}
6063 	ptp = B_TRUE;
6064 	goto repeat;
6065 }
6066 
6067 /*
6068  * Look for an ipif with the specified address. For point-point links
6069  * we look for matches on either the destination address and the local
6070  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6071  * is set.
6072  * Matches on a specific ill if match_ill is set.
6073  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6074  */
6075 zoneid_t
6076 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6077 {
6078 	zoneid_t zoneid;
6079 	ipif_t  *ipif;
6080 	ill_t   *ill;
6081 	boolean_t ptp = B_FALSE;
6082 	ill_walk_context_t	ctx;
6083 
6084 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6085 	/*
6086 	 * Repeat twice, first based on local addresses and
6087 	 * next time for pointopoint.
6088 	 */
6089 repeat:
6090 	ill = ILL_START_WALK_V4(&ctx, ipst);
6091 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6092 		if (match_ill != NULL && ill != match_ill) {
6093 			continue;
6094 		}
6095 		mutex_enter(&ill->ill_lock);
6096 		for (ipif = ill->ill_ipif; ipif != NULL;
6097 		    ipif = ipif->ipif_next) {
6098 			/* Allow the ipif to be down */
6099 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6100 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6101 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6102 			    (ipif->ipif_pp_dst_addr == addr)) &&
6103 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6104 				zoneid = ipif->ipif_zoneid;
6105 				mutex_exit(&ill->ill_lock);
6106 				rw_exit(&ipst->ips_ill_g_lock);
6107 				/*
6108 				 * If ipif_zoneid was ALL_ZONES then we have
6109 				 * a trusted extensions shared IP address.
6110 				 * In that case GLOBAL_ZONEID works to send.
6111 				 */
6112 				if (zoneid == ALL_ZONES)
6113 					zoneid = GLOBAL_ZONEID;
6114 				return (zoneid);
6115 			}
6116 		}
6117 		mutex_exit(&ill->ill_lock);
6118 	}
6119 
6120 	/* If we already did the ptp case, then we are done */
6121 	if (ptp) {
6122 		rw_exit(&ipst->ips_ill_g_lock);
6123 		return (ALL_ZONES);
6124 	}
6125 	ptp = B_TRUE;
6126 	goto repeat;
6127 }
6128 
6129 /*
6130  * Look for an ipif that matches the specified remote address i.e. the
6131  * ipif that would receive the specified packet.
6132  * First look for directly connected interfaces and then do a recursive
6133  * IRE lookup and pick the first ipif corresponding to the source address in the
6134  * ire.
6135  * Returns: held ipif
6136  */
6137 ipif_t *
6138 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6139 {
6140 	ipif_t	*ipif;
6141 	ire_t	*ire;
6142 	ip_stack_t	*ipst = ill->ill_ipst;
6143 
6144 	ASSERT(!ill->ill_isv6);
6145 
6146 	/*
6147 	 * Someone could be changing this ipif currently or change it
6148 	 * after we return this. Thus  a few packets could use the old
6149 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6150 	 * will atomically be updated or cleaned up with the new value
6151 	 * Thus we don't need a lock to check the flags or other attrs below.
6152 	 */
6153 	mutex_enter(&ill->ill_lock);
6154 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6155 		if (!IPIF_CAN_LOOKUP(ipif))
6156 			continue;
6157 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6158 		    ipif->ipif_zoneid != ALL_ZONES)
6159 			continue;
6160 		/* Allow the ipif to be down */
6161 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6162 			if ((ipif->ipif_pp_dst_addr == addr) ||
6163 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6164 			    ipif->ipif_lcl_addr == addr)) {
6165 				ipif_refhold_locked(ipif);
6166 				mutex_exit(&ill->ill_lock);
6167 				return (ipif);
6168 			}
6169 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6170 			ipif_refhold_locked(ipif);
6171 			mutex_exit(&ill->ill_lock);
6172 			return (ipif);
6173 		}
6174 	}
6175 	mutex_exit(&ill->ill_lock);
6176 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6177 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6178 	if (ire != NULL) {
6179 		/*
6180 		 * The callers of this function wants to know the
6181 		 * interface on which they have to send the replies
6182 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6183 		 * derived from different ills, we really don't care
6184 		 * what we return here.
6185 		 */
6186 		ipif = ire->ire_ipif;
6187 		if (ipif != NULL) {
6188 			ipif_refhold(ipif);
6189 			ire_refrele(ire);
6190 			return (ipif);
6191 		}
6192 		ire_refrele(ire);
6193 	}
6194 	/* Pick the first interface */
6195 	ipif = ipif_get_next_ipif(NULL, ill);
6196 	return (ipif);
6197 }
6198 
6199 /*
6200  * This func does not prevent refcnt from increasing. But if
6201  * the caller has taken steps to that effect, then this func
6202  * can be used to determine whether the ill has become quiescent
6203  */
6204 static boolean_t
6205 ill_is_quiescent(ill_t *ill)
6206 {
6207 	ipif_t	*ipif;
6208 
6209 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6210 
6211 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6212 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6213 			return (B_FALSE);
6214 		}
6215 	}
6216 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6217 		return (B_FALSE);
6218 	}
6219 	return (B_TRUE);
6220 }
6221 
6222 boolean_t
6223 ill_is_freeable(ill_t *ill)
6224 {
6225 	ipif_t	*ipif;
6226 
6227 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6228 
6229 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6230 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6231 			return (B_FALSE);
6232 		}
6233 	}
6234 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6235 		return (B_FALSE);
6236 	}
6237 	return (B_TRUE);
6238 }
6239 
6240 /*
6241  * This func does not prevent refcnt from increasing. But if
6242  * the caller has taken steps to that effect, then this func
6243  * can be used to determine whether the ipif has become quiescent
6244  */
6245 static boolean_t
6246 ipif_is_quiescent(ipif_t *ipif)
6247 {
6248 	ill_t *ill;
6249 
6250 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6251 
6252 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6253 		return (B_FALSE);
6254 	}
6255 
6256 	ill = ipif->ipif_ill;
6257 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6258 	    ill->ill_logical_down) {
6259 		return (B_TRUE);
6260 	}
6261 
6262 	/* This is the last ipif going down or being deleted on this ill */
6263 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6264 		return (B_FALSE);
6265 	}
6266 
6267 	return (B_TRUE);
6268 }
6269 
6270 /*
6271  * return true if the ipif can be destroyed: the ipif has to be quiescent
6272  * with zero references from ire/nce/ilm to it.
6273  */
6274 static boolean_t
6275 ipif_is_freeable(ipif_t *ipif)
6276 {
6277 
6278 	ill_t *ill;
6279 
6280 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6281 
6282 	if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6283 		return (B_FALSE);
6284 	}
6285 
6286 	ill = ipif->ipif_ill;
6287 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6288 	    ill->ill_logical_down) {
6289 		return (B_TRUE);
6290 	}
6291 
6292 	/* This is the last ipif going down or being deleted on this ill */
6293 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6294 		return (B_FALSE);
6295 	}
6296 
6297 	return (B_TRUE);
6298 }
6299 
6300 /*
6301  * This func does not prevent refcnt from increasing. But if
6302  * the caller has taken steps to that effect, then this func
6303  * can be used to determine whether the ipifs marked with IPIF_MOVING
6304  * have become quiescent and can be moved in a failover/failback.
6305  */
6306 static ipif_t *
6307 ill_quiescent_to_move(ill_t *ill)
6308 {
6309 	ipif_t  *ipif;
6310 
6311 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6312 
6313 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6314 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6315 			if (ipif->ipif_refcnt != 0 ||
6316 			    !IPIF_DOWN_OK(ipif)) {
6317 				return (ipif);
6318 			}
6319 		}
6320 	}
6321 	return (NULL);
6322 }
6323 
6324 /*
6325  * The ipif/ill/ire has been refreled. Do the tail processing.
6326  * Determine if the ipif or ill in question has become quiescent and if so
6327  * wakeup close and/or restart any queued pending ioctl that is waiting
6328  * for the ipif_down (or ill_down)
6329  */
6330 void
6331 ipif_ill_refrele_tail(ill_t *ill)
6332 {
6333 	mblk_t	*mp;
6334 	conn_t	*connp;
6335 	ipsq_t	*ipsq;
6336 	ipif_t	*ipif;
6337 	dl_notify_ind_t *dlindp;
6338 
6339 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6340 
6341 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6342 	    ill_is_freeable(ill)) {
6343 		/* ill_close may be waiting */
6344 		cv_broadcast(&ill->ill_cv);
6345 	}
6346 
6347 	/* ipsq can't change because ill_lock  is held */
6348 	ipsq = ill->ill_phyint->phyint_ipsq;
6349 	if (ipsq->ipsq_waitfor == 0) {
6350 		/* Not waiting for anything, just return. */
6351 		mutex_exit(&ill->ill_lock);
6352 		return;
6353 	}
6354 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6355 	    ipsq->ipsq_pending_ipif != NULL);
6356 	/*
6357 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6358 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6359 	 * be zero for restarting an ioctl that ends up downing the ill.
6360 	 */
6361 	ipif = ipsq->ipsq_pending_ipif;
6362 	if (ipif->ipif_ill != ill) {
6363 		/* The ioctl is pending on some other ill. */
6364 		mutex_exit(&ill->ill_lock);
6365 		return;
6366 	}
6367 
6368 	switch (ipsq->ipsq_waitfor) {
6369 	case IPIF_DOWN:
6370 		if (!ipif_is_quiescent(ipif)) {
6371 			mutex_exit(&ill->ill_lock);
6372 			return;
6373 		}
6374 		break;
6375 	case IPIF_FREE:
6376 		if (!ipif_is_freeable(ipif)) {
6377 			mutex_exit(&ill->ill_lock);
6378 			return;
6379 		}
6380 		break;
6381 
6382 	case ILL_DOWN:
6383 		if (!ill_is_quiescent(ill)) {
6384 			mutex_exit(&ill->ill_lock);
6385 			return;
6386 		}
6387 		break;
6388 	case ILL_FREE:
6389 		/*
6390 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6391 		 * waits synchronously in ip_close, and no message is queued in
6392 		 * ipsq_pending_mp at all in this case
6393 		 */
6394 		if (!ill_is_freeable(ill)) {
6395 			mutex_exit(&ill->ill_lock);
6396 			return;
6397 		}
6398 		break;
6399 
6400 	case ILL_MOVE_OK:
6401 		if (ill_quiescent_to_move(ill) != NULL) {
6402 			mutex_exit(&ill->ill_lock);
6403 			return;
6404 		}
6405 		break;
6406 	default:
6407 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6408 		    (void *)ipsq, ipsq->ipsq_waitfor);
6409 	}
6410 
6411 	/*
6412 	 * Incr refcnt for the qwriter_ip call below which
6413 	 * does a refrele
6414 	 */
6415 	ill_refhold_locked(ill);
6416 	mp = ipsq_pending_mp_get(ipsq, &connp);
6417 	mutex_exit(&ill->ill_lock);
6418 
6419 	ASSERT(mp != NULL);
6420 	/*
6421 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6422 	 * we can only get here when the current operation decides it
6423 	 * it needs to quiesce via ipsq_pending_mp_add().
6424 	 */
6425 	switch (mp->b_datap->db_type) {
6426 	case M_PCPROTO:
6427 	case M_PROTO:
6428 		/*
6429 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6430 		 */
6431 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6432 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6433 
6434 		switch (dlindp->dl_notification) {
6435 		case DL_NOTE_PHYS_ADDR:
6436 			qwriter_ip(ill, ill->ill_rq, mp,
6437 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6438 			return;
6439 		default:
6440 			ASSERT(0);
6441 		}
6442 		break;
6443 
6444 	case M_ERROR:
6445 	case M_HANGUP:
6446 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6447 		    B_TRUE);
6448 		return;
6449 
6450 	case M_IOCTL:
6451 	case M_IOCDATA:
6452 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6453 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6454 		return;
6455 
6456 	default:
6457 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6458 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6459 	}
6460 }
6461 
6462 #ifdef DEBUG
6463 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6464 static void
6465 th_trace_rrecord(th_trace_t *th_trace)
6466 {
6467 	tr_buf_t *tr_buf;
6468 	uint_t lastref;
6469 
6470 	lastref = th_trace->th_trace_lastref;
6471 	lastref++;
6472 	if (lastref == TR_BUF_MAX)
6473 		lastref = 0;
6474 	th_trace->th_trace_lastref = lastref;
6475 	tr_buf = &th_trace->th_trbuf[lastref];
6476 	tr_buf->tr_time = lbolt;
6477 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6478 }
6479 
6480 static void
6481 th_trace_free(void *value)
6482 {
6483 	th_trace_t *th_trace = value;
6484 
6485 	ASSERT(th_trace->th_refcnt == 0);
6486 	kmem_free(th_trace, sizeof (*th_trace));
6487 }
6488 
6489 /*
6490  * Find or create the per-thread hash table used to track object references.
6491  * The ipst argument is NULL if we shouldn't allocate.
6492  *
6493  * Accesses per-thread data, so there's no need to lock here.
6494  */
6495 static mod_hash_t *
6496 th_trace_gethash(ip_stack_t *ipst)
6497 {
6498 	th_hash_t *thh;
6499 
6500 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6501 		mod_hash_t *mh;
6502 		char name[256];
6503 		size_t objsize, rshift;
6504 		int retv;
6505 
6506 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6507 			return (NULL);
6508 		(void) snprintf(name, sizeof (name), "th_trace_%p", curthread);
6509 
6510 		/*
6511 		 * We use mod_hash_create_extended here rather than the more
6512 		 * obvious mod_hash_create_ptrhash because the latter has a
6513 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6514 		 * block.
6515 		 */
6516 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6517 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6518 		rshift = highbit(objsize);
6519 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6520 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6521 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6522 		if (mh == NULL) {
6523 			kmem_free(thh, sizeof (*thh));
6524 			return (NULL);
6525 		}
6526 		thh->thh_hash = mh;
6527 		thh->thh_ipst = ipst;
6528 		/*
6529 		 * We trace ills, ipifs, ires, and nces.  All of these are
6530 		 * per-IP-stack, so the lock on the thread list is as well.
6531 		 */
6532 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6533 		list_insert_tail(&ip_thread_list, thh);
6534 		rw_exit(&ip_thread_rwlock);
6535 		retv = tsd_set(ip_thread_data, thh);
6536 		ASSERT(retv == 0);
6537 	}
6538 	return (thh != NULL ? thh->thh_hash : NULL);
6539 }
6540 
6541 boolean_t
6542 th_trace_ref(const void *obj, ip_stack_t *ipst)
6543 {
6544 	th_trace_t *th_trace;
6545 	mod_hash_t *mh;
6546 	mod_hash_val_t val;
6547 
6548 	if ((mh = th_trace_gethash(ipst)) == NULL)
6549 		return (B_FALSE);
6550 
6551 	/*
6552 	 * Attempt to locate the trace buffer for this obj and thread.
6553 	 * If it does not exist, then allocate a new trace buffer and
6554 	 * insert into the hash.
6555 	 */
6556 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6557 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6558 		if (th_trace == NULL)
6559 			return (B_FALSE);
6560 
6561 		th_trace->th_id = curthread;
6562 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6563 		    (mod_hash_val_t)th_trace) != 0) {
6564 			kmem_free(th_trace, sizeof (th_trace_t));
6565 			return (B_FALSE);
6566 		}
6567 	} else {
6568 		th_trace = (th_trace_t *)val;
6569 	}
6570 
6571 	ASSERT(th_trace->th_refcnt >= 0 &&
6572 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6573 
6574 	th_trace->th_refcnt++;
6575 	th_trace_rrecord(th_trace);
6576 	return (B_TRUE);
6577 }
6578 
6579 /*
6580  * For the purpose of tracing a reference release, we assume that global
6581  * tracing is always on and that the same thread initiated the reference hold
6582  * is releasing.
6583  */
6584 void
6585 th_trace_unref(const void *obj)
6586 {
6587 	int retv;
6588 	mod_hash_t *mh;
6589 	th_trace_t *th_trace;
6590 	mod_hash_val_t val;
6591 
6592 	mh = th_trace_gethash(NULL);
6593 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6594 	ASSERT(retv == 0);
6595 	th_trace = (th_trace_t *)val;
6596 
6597 	ASSERT(th_trace->th_refcnt > 0);
6598 	th_trace->th_refcnt--;
6599 	th_trace_rrecord(th_trace);
6600 }
6601 
6602 /*
6603  * If tracing has been disabled, then we assume that the reference counts are
6604  * now useless, and we clear them out before destroying the entries.
6605  */
6606 void
6607 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6608 {
6609 	th_hash_t	*thh;
6610 	mod_hash_t	*mh;
6611 	mod_hash_val_t	val;
6612 	th_trace_t	*th_trace;
6613 	int		retv;
6614 
6615 	rw_enter(&ip_thread_rwlock, RW_READER);
6616 	for (thh = list_head(&ip_thread_list); thh != NULL;
6617 	    thh = list_next(&ip_thread_list, thh)) {
6618 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6619 		    &val) == 0) {
6620 			th_trace = (th_trace_t *)val;
6621 			if (trace_disable)
6622 				th_trace->th_refcnt = 0;
6623 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6624 			ASSERT(retv == 0);
6625 		}
6626 	}
6627 	rw_exit(&ip_thread_rwlock);
6628 }
6629 
6630 void
6631 ipif_trace_ref(ipif_t *ipif)
6632 {
6633 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6634 
6635 	if (ipif->ipif_trace_disable)
6636 		return;
6637 
6638 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6639 		ipif->ipif_trace_disable = B_TRUE;
6640 		ipif_trace_cleanup(ipif);
6641 	}
6642 }
6643 
6644 void
6645 ipif_untrace_ref(ipif_t *ipif)
6646 {
6647 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6648 
6649 	if (!ipif->ipif_trace_disable)
6650 		th_trace_unref(ipif);
6651 }
6652 
6653 void
6654 ill_trace_ref(ill_t *ill)
6655 {
6656 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6657 
6658 	if (ill->ill_trace_disable)
6659 		return;
6660 
6661 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6662 		ill->ill_trace_disable = B_TRUE;
6663 		ill_trace_cleanup(ill);
6664 	}
6665 }
6666 
6667 void
6668 ill_untrace_ref(ill_t *ill)
6669 {
6670 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6671 
6672 	if (!ill->ill_trace_disable)
6673 		th_trace_unref(ill);
6674 }
6675 
6676 /*
6677  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6678  * failure, ipif_trace_disable is set.
6679  */
6680 static void
6681 ipif_trace_cleanup(const ipif_t *ipif)
6682 {
6683 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6684 }
6685 
6686 /*
6687  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6688  * failure, ill_trace_disable is set.
6689  */
6690 static void
6691 ill_trace_cleanup(const ill_t *ill)
6692 {
6693 	th_trace_cleanup(ill, ill->ill_trace_disable);
6694 }
6695 #endif /* DEBUG */
6696 
6697 void
6698 ipif_refhold_locked(ipif_t *ipif)
6699 {
6700 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6701 	ipif->ipif_refcnt++;
6702 	IPIF_TRACE_REF(ipif);
6703 }
6704 
6705 void
6706 ipif_refhold(ipif_t *ipif)
6707 {
6708 	ill_t	*ill;
6709 
6710 	ill = ipif->ipif_ill;
6711 	mutex_enter(&ill->ill_lock);
6712 	ipif->ipif_refcnt++;
6713 	IPIF_TRACE_REF(ipif);
6714 	mutex_exit(&ill->ill_lock);
6715 }
6716 
6717 /*
6718  * Must not be called while holding any locks. Otherwise if this is
6719  * the last reference to be released there is a chance of recursive mutex
6720  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6721  * to restart an ioctl.
6722  */
6723 void
6724 ipif_refrele(ipif_t *ipif)
6725 {
6726 	ill_t	*ill;
6727 
6728 	ill = ipif->ipif_ill;
6729 
6730 	mutex_enter(&ill->ill_lock);
6731 	ASSERT(ipif->ipif_refcnt != 0);
6732 	ipif->ipif_refcnt--;
6733 	IPIF_UNTRACE_REF(ipif);
6734 	if (ipif->ipif_refcnt != 0) {
6735 		mutex_exit(&ill->ill_lock);
6736 		return;
6737 	}
6738 
6739 	/* Drops the ill_lock */
6740 	ipif_ill_refrele_tail(ill);
6741 }
6742 
6743 ipif_t *
6744 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6745 {
6746 	ipif_t	*ipif;
6747 
6748 	mutex_enter(&ill->ill_lock);
6749 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6750 	    ipif != NULL; ipif = ipif->ipif_next) {
6751 		if (!IPIF_CAN_LOOKUP(ipif))
6752 			continue;
6753 		ipif_refhold_locked(ipif);
6754 		mutex_exit(&ill->ill_lock);
6755 		return (ipif);
6756 	}
6757 	mutex_exit(&ill->ill_lock);
6758 	return (NULL);
6759 }
6760 
6761 /*
6762  * TODO: make this table extendible at run time
6763  * Return a pointer to the mac type info for 'mac_type'
6764  */
6765 static ip_m_t *
6766 ip_m_lookup(t_uscalar_t mac_type)
6767 {
6768 	ip_m_t	*ipm;
6769 
6770 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6771 		if (ipm->ip_m_mac_type == mac_type)
6772 			return (ipm);
6773 	return (NULL);
6774 }
6775 
6776 /*
6777  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6778  * ipif_arg is passed in to associate it with the correct interface.
6779  * We may need to restart this operation if the ipif cannot be looked up
6780  * due to an exclusive operation that is currently in progress. The restart
6781  * entry point is specified by 'func'
6782  */
6783 int
6784 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6785     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6786     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6787     struct rtsa_s *sp, ip_stack_t *ipst)
6788 {
6789 	ire_t	*ire;
6790 	ire_t	*gw_ire = NULL;
6791 	ipif_t	*ipif = NULL;
6792 	boolean_t ipif_refheld = B_FALSE;
6793 	uint_t	type;
6794 	int	match_flags = MATCH_IRE_TYPE;
6795 	int	error;
6796 	tsol_gc_t *gc = NULL;
6797 	tsol_gcgrp_t *gcgrp = NULL;
6798 	boolean_t gcgrp_xtraref = B_FALSE;
6799 
6800 	ip1dbg(("ip_rt_add:"));
6801 
6802 	if (ire_arg != NULL)
6803 		*ire_arg = NULL;
6804 
6805 	/*
6806 	 * If this is the case of RTF_HOST being set, then we set the netmask
6807 	 * to all ones (regardless if one was supplied).
6808 	 */
6809 	if (flags & RTF_HOST)
6810 		mask = IP_HOST_MASK;
6811 
6812 	/*
6813 	 * Prevent routes with a zero gateway from being created (since
6814 	 * interfaces can currently be plumbed and brought up no assigned
6815 	 * address).
6816 	 */
6817 	if (gw_addr == 0)
6818 		return (ENETUNREACH);
6819 	/*
6820 	 * Get the ipif, if any, corresponding to the gw_addr
6821 	 */
6822 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6823 	    ipst);
6824 	if (ipif != NULL) {
6825 		if (IS_VNI(ipif->ipif_ill)) {
6826 			ipif_refrele(ipif);
6827 			return (EINVAL);
6828 		}
6829 		ipif_refheld = B_TRUE;
6830 	} else if (error == EINPROGRESS) {
6831 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6832 		return (EINPROGRESS);
6833 	} else {
6834 		error = 0;
6835 	}
6836 
6837 	if (ipif != NULL) {
6838 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6839 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6840 	} else {
6841 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6842 	}
6843 
6844 	/*
6845 	 * GateD will attempt to create routes with a loopback interface
6846 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6847 	 * these routes to be added, but create them as interface routes
6848 	 * since the gateway is an interface address.
6849 	 */
6850 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6851 		flags &= ~RTF_GATEWAY;
6852 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6853 		    mask == IP_HOST_MASK) {
6854 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6855 			    ALL_ZONES, NULL, match_flags, ipst);
6856 			if (ire != NULL) {
6857 				ire_refrele(ire);
6858 				if (ipif_refheld)
6859 					ipif_refrele(ipif);
6860 				return (EEXIST);
6861 			}
6862 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6863 			    "for 0x%x\n", (void *)ipif,
6864 			    ipif->ipif_ire_type,
6865 			    ntohl(ipif->ipif_lcl_addr)));
6866 			ire = ire_create(
6867 			    (uchar_t *)&dst_addr,	/* dest address */
6868 			    (uchar_t *)&mask,		/* mask */
6869 			    (uchar_t *)&ipif->ipif_src_addr,
6870 			    NULL,			/* no gateway */
6871 			    &ipif->ipif_mtu,
6872 			    NULL,
6873 			    ipif->ipif_rq,		/* recv-from queue */
6874 			    NULL,			/* no send-to queue */
6875 			    ipif->ipif_ire_type,	/* LOOPBACK */
6876 			    ipif,
6877 			    0,
6878 			    0,
6879 			    0,
6880 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6881 			    RTF_PRIVATE : 0,
6882 			    &ire_uinfo_null,
6883 			    NULL,
6884 			    NULL,
6885 			    ipst);
6886 
6887 			if (ire == NULL) {
6888 				if (ipif_refheld)
6889 					ipif_refrele(ipif);
6890 				return (ENOMEM);
6891 			}
6892 			error = ire_add(&ire, q, mp, func, B_FALSE);
6893 			if (error == 0)
6894 				goto save_ire;
6895 			if (ipif_refheld)
6896 				ipif_refrele(ipif);
6897 			return (error);
6898 
6899 		}
6900 	}
6901 
6902 	/*
6903 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6904 	 * and the gateway address provided is one of the system's interface
6905 	 * addresses.  By using the routing socket interface and supplying an
6906 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6907 	 * specifying an interface route to be created is available which uses
6908 	 * the interface index that specifies the outgoing interface rather than
6909 	 * the address of an outgoing interface (which may not be able to
6910 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6911 	 * flag, routes can be specified which not only specify the next-hop to
6912 	 * be used when routing to a certain prefix, but also which outgoing
6913 	 * interface should be used.
6914 	 *
6915 	 * Previously, interfaces would have unique addresses assigned to them
6916 	 * and so the address assigned to a particular interface could be used
6917 	 * to identify a particular interface.  One exception to this was the
6918 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6919 	 *
6920 	 * With the advent of IPv6 and its link-local addresses, this
6921 	 * restriction was relaxed and interfaces could share addresses between
6922 	 * themselves.  In fact, typically all of the link-local interfaces on
6923 	 * an IPv6 node or router will have the same link-local address.  In
6924 	 * order to differentiate between these interfaces, the use of an
6925 	 * interface index is necessary and this index can be carried inside a
6926 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6927 	 * of using the interface index, however, is that all of the ipif's that
6928 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6929 	 * cannot be used to differentiate between ipif's (or logical
6930 	 * interfaces) that belong to the same ill (physical interface).
6931 	 *
6932 	 * For example, in the following case involving IPv4 interfaces and
6933 	 * logical interfaces
6934 	 *
6935 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6936 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6937 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6938 	 *
6939 	 * the ipif's corresponding to each of these interface routes can be
6940 	 * uniquely identified by the "gateway" (actually interface address).
6941 	 *
6942 	 * In this case involving multiple IPv6 default routes to a particular
6943 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6944 	 * default route is of interest:
6945 	 *
6946 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6947 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6948 	 */
6949 
6950 	/* RTF_GATEWAY not set */
6951 	if (!(flags & RTF_GATEWAY)) {
6952 		queue_t	*stq;
6953 
6954 		if (sp != NULL) {
6955 			ip2dbg(("ip_rt_add: gateway security attributes "
6956 			    "cannot be set with interface route\n"));
6957 			if (ipif_refheld)
6958 				ipif_refrele(ipif);
6959 			return (EINVAL);
6960 		}
6961 
6962 		/*
6963 		 * As the interface index specified with the RTA_IFP sockaddr is
6964 		 * the same for all ipif's off of an ill, the matching logic
6965 		 * below uses MATCH_IRE_ILL if such an index was specified.
6966 		 * This means that routes sharing the same prefix when added
6967 		 * using a RTA_IFP sockaddr must have distinct interface
6968 		 * indices (namely, they must be on distinct ill's).
6969 		 *
6970 		 * On the other hand, since the gateway address will usually be
6971 		 * different for each ipif on the system, the matching logic
6972 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6973 		 * route.  This means that interface routes for the same prefix
6974 		 * can be created if they belong to distinct ipif's and if a
6975 		 * RTA_IFP sockaddr is not present.
6976 		 */
6977 		if (ipif_arg != NULL) {
6978 			if (ipif_refheld)  {
6979 				ipif_refrele(ipif);
6980 				ipif_refheld = B_FALSE;
6981 			}
6982 			ipif = ipif_arg;
6983 			match_flags |= MATCH_IRE_ILL;
6984 		} else {
6985 			/*
6986 			 * Check the ipif corresponding to the gw_addr
6987 			 */
6988 			if (ipif == NULL)
6989 				return (ENETUNREACH);
6990 			match_flags |= MATCH_IRE_IPIF;
6991 		}
6992 		ASSERT(ipif != NULL);
6993 
6994 		/*
6995 		 * We check for an existing entry at this point.
6996 		 *
6997 		 * Since a netmask isn't passed in via the ioctl interface
6998 		 * (SIOCADDRT), we don't check for a matching netmask in that
6999 		 * case.
7000 		 */
7001 		if (!ioctl_msg)
7002 			match_flags |= MATCH_IRE_MASK;
7003 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
7004 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7005 		if (ire != NULL) {
7006 			ire_refrele(ire);
7007 			if (ipif_refheld)
7008 				ipif_refrele(ipif);
7009 			return (EEXIST);
7010 		}
7011 
7012 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7013 		    ? ipif->ipif_rq : ipif->ipif_wq;
7014 
7015 		/*
7016 		 * Create a copy of the IRE_LOOPBACK,
7017 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7018 		 * the modified address and netmask.
7019 		 */
7020 		ire = ire_create(
7021 		    (uchar_t *)&dst_addr,
7022 		    (uint8_t *)&mask,
7023 		    (uint8_t *)&ipif->ipif_src_addr,
7024 		    NULL,
7025 		    &ipif->ipif_mtu,
7026 		    NULL,
7027 		    NULL,
7028 		    stq,
7029 		    ipif->ipif_net_type,
7030 		    ipif,
7031 		    0,
7032 		    0,
7033 		    0,
7034 		    flags,
7035 		    &ire_uinfo_null,
7036 		    NULL,
7037 		    NULL,
7038 		    ipst);
7039 		if (ire == NULL) {
7040 			if (ipif_refheld)
7041 				ipif_refrele(ipif);
7042 			return (ENOMEM);
7043 		}
7044 
7045 		/*
7046 		 * Some software (for example, GateD and Sun Cluster) attempts
7047 		 * to create (what amount to) IRE_PREFIX routes with the
7048 		 * loopback address as the gateway.  This is primarily done to
7049 		 * set up prefixes with the RTF_REJECT flag set (for example,
7050 		 * when generating aggregate routes.)
7051 		 *
7052 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7053 		 * IRE_LOOPBACK, then we map the request into a
7054 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
7055 		 * these interface routes, by definition, can only be that.
7056 		 *
7057 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7058 		 * routine, but rather using ire_create() directly.
7059 		 *
7060 		 */
7061 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
7062 			ire->ire_type = IRE_IF_NORESOLVER;
7063 			ire->ire_flags |= RTF_BLACKHOLE;
7064 		}
7065 
7066 		error = ire_add(&ire, q, mp, func, B_FALSE);
7067 		if (error == 0)
7068 			goto save_ire;
7069 
7070 		/*
7071 		 * In the result of failure, ire_add() will have already
7072 		 * deleted the ire in question, so there is no need to
7073 		 * do that here.
7074 		 */
7075 		if (ipif_refheld)
7076 			ipif_refrele(ipif);
7077 		return (error);
7078 	}
7079 	if (ipif_refheld) {
7080 		ipif_refrele(ipif);
7081 		ipif_refheld = B_FALSE;
7082 	}
7083 
7084 	/*
7085 	 * Get an interface IRE for the specified gateway.
7086 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7087 	 * gateway, it is currently unreachable and we fail the request
7088 	 * accordingly.
7089 	 */
7090 	ipif = ipif_arg;
7091 	if (ipif_arg != NULL)
7092 		match_flags |= MATCH_IRE_ILL;
7093 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7094 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7095 	if (gw_ire == NULL)
7096 		return (ENETUNREACH);
7097 
7098 	/*
7099 	 * We create one of three types of IREs as a result of this request
7100 	 * based on the netmask.  A netmask of all ones (which is automatically
7101 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7102 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7103 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7104 	 * destination prefix.
7105 	 */
7106 	if (mask == IP_HOST_MASK)
7107 		type = IRE_HOST;
7108 	else if (mask == 0)
7109 		type = IRE_DEFAULT;
7110 	else
7111 		type = IRE_PREFIX;
7112 
7113 	/* check for a duplicate entry */
7114 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7115 	    NULL, ALL_ZONES, 0, NULL,
7116 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7117 	if (ire != NULL) {
7118 		ire_refrele(gw_ire);
7119 		ire_refrele(ire);
7120 		return (EEXIST);
7121 	}
7122 
7123 	/* Security attribute exists */
7124 	if (sp != NULL) {
7125 		tsol_gcgrp_addr_t ga;
7126 
7127 		/* find or create the gateway credentials group */
7128 		ga.ga_af = AF_INET;
7129 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7130 
7131 		/* we hold reference to it upon success */
7132 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7133 		if (gcgrp == NULL) {
7134 			ire_refrele(gw_ire);
7135 			return (ENOMEM);
7136 		}
7137 
7138 		/*
7139 		 * Create and add the security attribute to the group; a
7140 		 * reference to the group is made upon allocating a new
7141 		 * entry successfully.  If it finds an already-existing
7142 		 * entry for the security attribute in the group, it simply
7143 		 * returns it and no new reference is made to the group.
7144 		 */
7145 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7146 		if (gc == NULL) {
7147 			/* release reference held by gcgrp_lookup */
7148 			GCGRP_REFRELE(gcgrp);
7149 			ire_refrele(gw_ire);
7150 			return (ENOMEM);
7151 		}
7152 	}
7153 
7154 	/* Create the IRE. */
7155 	ire = ire_create(
7156 	    (uchar_t *)&dst_addr,		/* dest address */
7157 	    (uchar_t *)&mask,			/* mask */
7158 	    /* src address assigned by the caller? */
7159 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7160 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7161 	    (uchar_t *)&gw_addr,		/* gateway address */
7162 	    &gw_ire->ire_max_frag,
7163 	    NULL,				/* no src nce */
7164 	    NULL,				/* no recv-from queue */
7165 	    NULL,				/* no send-to queue */
7166 	    (ushort_t)type,			/* IRE type */
7167 	    ipif_arg,
7168 	    0,
7169 	    0,
7170 	    0,
7171 	    flags,
7172 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7173 	    gc,					/* security attribute */
7174 	    NULL,
7175 	    ipst);
7176 
7177 	/*
7178 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7179 	 * reference to the 'gcgrp'. We can now release the extra reference
7180 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7181 	 */
7182 	if (gcgrp_xtraref)
7183 		GCGRP_REFRELE(gcgrp);
7184 	if (ire == NULL) {
7185 		if (gc != NULL)
7186 			GC_REFRELE(gc);
7187 		ire_refrele(gw_ire);
7188 		return (ENOMEM);
7189 	}
7190 
7191 	/*
7192 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7193 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7194 	 */
7195 
7196 	/* Add the new IRE. */
7197 	error = ire_add(&ire, q, mp, func, B_FALSE);
7198 	if (error != 0) {
7199 		/*
7200 		 * In the result of failure, ire_add() will have already
7201 		 * deleted the ire in question, so there is no need to
7202 		 * do that here.
7203 		 */
7204 		ire_refrele(gw_ire);
7205 		return (error);
7206 	}
7207 
7208 	if (flags & RTF_MULTIRT) {
7209 		/*
7210 		 * Invoke the CGTP (multirouting) filtering module
7211 		 * to add the dst address in the filtering database.
7212 		 * Replicated inbound packets coming from that address
7213 		 * will be filtered to discard the duplicates.
7214 		 * It is not necessary to call the CGTP filter hook
7215 		 * when the dst address is a broadcast or multicast,
7216 		 * because an IP source address cannot be a broadcast
7217 		 * or a multicast.
7218 		 */
7219 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7220 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7221 		if (ire_dst != NULL) {
7222 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7223 			ire_refrele(ire_dst);
7224 			goto save_ire;
7225 		}
7226 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7227 		    !CLASSD(ire->ire_addr)) {
7228 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7229 			    ipst->ips_netstack->netstack_stackid,
7230 			    ire->ire_addr,
7231 			    ire->ire_gateway_addr,
7232 			    ire->ire_src_addr,
7233 			    gw_ire->ire_src_addr);
7234 			if (res != 0) {
7235 				ire_refrele(gw_ire);
7236 				ire_delete(ire);
7237 				return (res);
7238 			}
7239 		}
7240 	}
7241 
7242 	/*
7243 	 * Now that the prefix IRE entry has been created, delete any
7244 	 * existing gateway IRE cache entries as well as any IRE caches
7245 	 * using the gateway, and force them to be created through
7246 	 * ip_newroute.
7247 	 */
7248 	if (gc != NULL) {
7249 		ASSERT(gcgrp != NULL);
7250 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7251 	}
7252 
7253 save_ire:
7254 	if (gw_ire != NULL) {
7255 		ire_refrele(gw_ire);
7256 	}
7257 	if (ipif != NULL) {
7258 		/*
7259 		 * Save enough information so that we can recreate the IRE if
7260 		 * the interface goes down and then up.  The metrics associated
7261 		 * with the route will be saved as well when rts_setmetrics() is
7262 		 * called after the IRE has been created.  In the case where
7263 		 * memory cannot be allocated, none of this information will be
7264 		 * saved.
7265 		 */
7266 		ipif_save_ire(ipif, ire);
7267 	}
7268 	if (ioctl_msg)
7269 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7270 	if (ire_arg != NULL) {
7271 		/*
7272 		 * Store the ire that was successfully added into where ire_arg
7273 		 * points to so that callers don't have to look it up
7274 		 * themselves (but they are responsible for ire_refrele()ing
7275 		 * the ire when they are finished with it).
7276 		 */
7277 		*ire_arg = ire;
7278 	} else {
7279 		ire_refrele(ire);		/* Held in ire_add */
7280 	}
7281 	if (ipif_refheld)
7282 		ipif_refrele(ipif);
7283 	return (0);
7284 }
7285 
7286 /*
7287  * ip_rt_delete is called to delete an IPv4 route.
7288  * ipif_arg is passed in to associate it with the correct interface.
7289  * We may need to restart this operation if the ipif cannot be looked up
7290  * due to an exclusive operation that is currently in progress. The restart
7291  * entry point is specified by 'func'
7292  */
7293 /* ARGSUSED4 */
7294 int
7295 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7296     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7297     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7298 {
7299 	ire_t	*ire = NULL;
7300 	ipif_t	*ipif;
7301 	boolean_t ipif_refheld = B_FALSE;
7302 	uint_t	type;
7303 	uint_t	match_flags = MATCH_IRE_TYPE;
7304 	int	err = 0;
7305 
7306 	ip1dbg(("ip_rt_delete:"));
7307 	/*
7308 	 * If this is the case of RTF_HOST being set, then we set the netmask
7309 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7310 	 */
7311 	if (flags & RTF_HOST) {
7312 		mask = IP_HOST_MASK;
7313 		match_flags |= MATCH_IRE_MASK;
7314 	} else if (rtm_addrs & RTA_NETMASK) {
7315 		match_flags |= MATCH_IRE_MASK;
7316 	}
7317 
7318 	/*
7319 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7320 	 * we check if the gateway address is one of our interfaces first,
7321 	 * and fall back on RTF_GATEWAY routes.
7322 	 *
7323 	 * This makes it possible to delete an original
7324 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7325 	 *
7326 	 * As the interface index specified with the RTA_IFP sockaddr is the
7327 	 * same for all ipif's off of an ill, the matching logic below uses
7328 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7329 	 * sharing the same prefix and interface index as the the route
7330 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7331 	 * is specified in the request.
7332 	 *
7333 	 * On the other hand, since the gateway address will usually be
7334 	 * different for each ipif on the system, the matching logic
7335 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7336 	 * route.  This means that interface routes for the same prefix can be
7337 	 * uniquely identified if they belong to distinct ipif's and if a
7338 	 * RTA_IFP sockaddr is not present.
7339 	 *
7340 	 * For more detail on specifying routes by gateway address and by
7341 	 * interface index, see the comments in ip_rt_add().
7342 	 */
7343 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7344 	    ipst);
7345 	if (ipif != NULL)
7346 		ipif_refheld = B_TRUE;
7347 	else if (err == EINPROGRESS)
7348 		return (err);
7349 	else
7350 		err = 0;
7351 	if (ipif != NULL) {
7352 		if (ipif_arg != NULL) {
7353 			if (ipif_refheld) {
7354 				ipif_refrele(ipif);
7355 				ipif_refheld = B_FALSE;
7356 			}
7357 			ipif = ipif_arg;
7358 			match_flags |= MATCH_IRE_ILL;
7359 		} else {
7360 			match_flags |= MATCH_IRE_IPIF;
7361 		}
7362 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7363 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7364 			    ALL_ZONES, NULL, match_flags, ipst);
7365 		}
7366 		if (ire == NULL) {
7367 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7368 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7369 			    match_flags, ipst);
7370 		}
7371 	}
7372 
7373 	if (ire == NULL) {
7374 		/*
7375 		 * At this point, the gateway address is not one of our own
7376 		 * addresses or a matching interface route was not found.  We
7377 		 * set the IRE type to lookup based on whether
7378 		 * this is a host route, a default route or just a prefix.
7379 		 *
7380 		 * If an ipif_arg was passed in, then the lookup is based on an
7381 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7382 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7383 		 * set as the route being looked up is not a traditional
7384 		 * interface route.
7385 		 */
7386 		match_flags &= ~MATCH_IRE_IPIF;
7387 		match_flags |= MATCH_IRE_GW;
7388 		if (ipif_arg != NULL)
7389 			match_flags |= MATCH_IRE_ILL;
7390 		if (mask == IP_HOST_MASK)
7391 			type = IRE_HOST;
7392 		else if (mask == 0)
7393 			type = IRE_DEFAULT;
7394 		else
7395 			type = IRE_PREFIX;
7396 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7397 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7398 	}
7399 
7400 	if (ipif_refheld)
7401 		ipif_refrele(ipif);
7402 
7403 	/* ipif is not refheld anymore */
7404 	if (ire == NULL)
7405 		return (ESRCH);
7406 
7407 	if (ire->ire_flags & RTF_MULTIRT) {
7408 		/*
7409 		 * Invoke the CGTP (multirouting) filtering module
7410 		 * to remove the dst address from the filtering database.
7411 		 * Packets coming from that address will no longer be
7412 		 * filtered to remove duplicates.
7413 		 */
7414 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7415 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7416 			    ipst->ips_netstack->netstack_stackid,
7417 			    ire->ire_addr, ire->ire_gateway_addr);
7418 		}
7419 		ip_cgtp_bcast_delete(ire, ipst);
7420 	}
7421 
7422 	ipif = ire->ire_ipif;
7423 	if (ipif != NULL)
7424 		ipif_remove_ire(ipif, ire);
7425 	if (ioctl_msg)
7426 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7427 	ire_delete(ire);
7428 	ire_refrele(ire);
7429 	return (err);
7430 }
7431 
7432 /*
7433  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7434  */
7435 /* ARGSUSED */
7436 int
7437 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7438     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7439 {
7440 	ipaddr_t dst_addr;
7441 	ipaddr_t gw_addr;
7442 	ipaddr_t mask;
7443 	int error = 0;
7444 	mblk_t *mp1;
7445 	struct rtentry *rt;
7446 	ipif_t *ipif = NULL;
7447 	ip_stack_t	*ipst;
7448 
7449 	ASSERT(q->q_next == NULL);
7450 	ipst = CONNQ_TO_IPST(q);
7451 
7452 	ip1dbg(("ip_siocaddrt:"));
7453 	/* Existence of mp1 verified in ip_wput_nondata */
7454 	mp1 = mp->b_cont->b_cont;
7455 	rt = (struct rtentry *)mp1->b_rptr;
7456 
7457 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7458 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7459 
7460 	/*
7461 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7462 	 * to a particular host address.  In this case, we set the netmask to
7463 	 * all ones for the particular destination address.  Otherwise,
7464 	 * determine the netmask to be used based on dst_addr and the interfaces
7465 	 * in use.
7466 	 */
7467 	if (rt->rt_flags & RTF_HOST) {
7468 		mask = IP_HOST_MASK;
7469 	} else {
7470 		/*
7471 		 * Note that ip_subnet_mask returns a zero mask in the case of
7472 		 * default (an all-zeroes address).
7473 		 */
7474 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7475 	}
7476 
7477 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7478 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7479 	if (ipif != NULL)
7480 		ipif_refrele(ipif);
7481 	return (error);
7482 }
7483 
7484 /*
7485  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7486  */
7487 /* ARGSUSED */
7488 int
7489 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7490     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7491 {
7492 	ipaddr_t dst_addr;
7493 	ipaddr_t gw_addr;
7494 	ipaddr_t mask;
7495 	int error;
7496 	mblk_t *mp1;
7497 	struct rtentry *rt;
7498 	ipif_t *ipif = NULL;
7499 	ip_stack_t	*ipst;
7500 
7501 	ASSERT(q->q_next == NULL);
7502 	ipst = CONNQ_TO_IPST(q);
7503 
7504 	ip1dbg(("ip_siocdelrt:"));
7505 	/* Existence of mp1 verified in ip_wput_nondata */
7506 	mp1 = mp->b_cont->b_cont;
7507 	rt = (struct rtentry *)mp1->b_rptr;
7508 
7509 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7510 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7511 
7512 	/*
7513 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7514 	 * to a particular host address.  In this case, we set the netmask to
7515 	 * all ones for the particular destination address.  Otherwise,
7516 	 * determine the netmask to be used based on dst_addr and the interfaces
7517 	 * in use.
7518 	 */
7519 	if (rt->rt_flags & RTF_HOST) {
7520 		mask = IP_HOST_MASK;
7521 	} else {
7522 		/*
7523 		 * Note that ip_subnet_mask returns a zero mask in the case of
7524 		 * default (an all-zeroes address).
7525 		 */
7526 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7527 	}
7528 
7529 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7530 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7531 	    mp, ip_process_ioctl, ipst);
7532 	if (ipif != NULL)
7533 		ipif_refrele(ipif);
7534 	return (error);
7535 }
7536 
7537 /*
7538  * Enqueue the mp onto the ipsq, chained by b_next.
7539  * b_prev stores the function to be executed later, and b_queue the queue
7540  * where this mp originated.
7541  */
7542 void
7543 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7544     ill_t *pending_ill)
7545 {
7546 	conn_t	*connp = NULL;
7547 
7548 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7549 	ASSERT(func != NULL);
7550 
7551 	mp->b_queue = q;
7552 	mp->b_prev = (void *)func;
7553 	mp->b_next = NULL;
7554 
7555 	switch (type) {
7556 	case CUR_OP:
7557 		if (ipsq->ipsq_mptail != NULL) {
7558 			ASSERT(ipsq->ipsq_mphead != NULL);
7559 			ipsq->ipsq_mptail->b_next = mp;
7560 		} else {
7561 			ASSERT(ipsq->ipsq_mphead == NULL);
7562 			ipsq->ipsq_mphead = mp;
7563 		}
7564 		ipsq->ipsq_mptail = mp;
7565 		break;
7566 
7567 	case NEW_OP:
7568 		if (ipsq->ipsq_xopq_mptail != NULL) {
7569 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7570 			ipsq->ipsq_xopq_mptail->b_next = mp;
7571 		} else {
7572 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7573 			ipsq->ipsq_xopq_mphead = mp;
7574 		}
7575 		ipsq->ipsq_xopq_mptail = mp;
7576 		break;
7577 	default:
7578 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7579 	}
7580 
7581 	if (CONN_Q(q) && pending_ill != NULL) {
7582 		connp = Q_TO_CONN(q);
7583 
7584 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7585 		connp->conn_oper_pending_ill = pending_ill;
7586 	}
7587 }
7588 
7589 /*
7590  * Return the mp at the head of the ipsq. After emptying the ipsq
7591  * look at the next ioctl, if this ioctl is complete. Otherwise
7592  * return, we will resume when we complete the current ioctl.
7593  * The current ioctl will wait till it gets a response from the
7594  * driver below.
7595  */
7596 static mblk_t *
7597 ipsq_dq(ipsq_t *ipsq)
7598 {
7599 	mblk_t	*mp;
7600 
7601 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7602 
7603 	mp = ipsq->ipsq_mphead;
7604 	if (mp != NULL) {
7605 		ipsq->ipsq_mphead = mp->b_next;
7606 		if (ipsq->ipsq_mphead == NULL)
7607 			ipsq->ipsq_mptail = NULL;
7608 		mp->b_next = NULL;
7609 		return (mp);
7610 	}
7611 	if (ipsq->ipsq_current_ipif != NULL)
7612 		return (NULL);
7613 	mp = ipsq->ipsq_xopq_mphead;
7614 	if (mp != NULL) {
7615 		ipsq->ipsq_xopq_mphead = mp->b_next;
7616 		if (ipsq->ipsq_xopq_mphead == NULL)
7617 			ipsq->ipsq_xopq_mptail = NULL;
7618 		mp->b_next = NULL;
7619 		return (mp);
7620 	}
7621 	return (NULL);
7622 }
7623 
7624 /*
7625  * Enter the ipsq corresponding to ill, by waiting synchronously till
7626  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7627  * will have to drain completely before ipsq_enter returns success.
7628  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7629  * and the ipsq_exit logic will start the next enqueued ioctl after
7630  * completion of the current ioctl. If 'force' is used, we don't wait
7631  * for the enqueued ioctls. This is needed when a conn_close wants to
7632  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7633  * of an ill can also use this option. But we dont' use it currently.
7634  */
7635 #define	ENTER_SQ_WAIT_TICKS 100
7636 boolean_t
7637 ipsq_enter(ill_t *ill, boolean_t force)
7638 {
7639 	ipsq_t	*ipsq;
7640 	boolean_t waited_enough = B_FALSE;
7641 
7642 	/*
7643 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7644 	 * Since the <ill-ipsq> assocs could change while we wait for the
7645 	 * writer, it is easier to wait on a fixed global rather than try to
7646 	 * cv_wait on a changing ipsq.
7647 	 */
7648 	mutex_enter(&ill->ill_lock);
7649 	for (;;) {
7650 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7651 			mutex_exit(&ill->ill_lock);
7652 			return (B_FALSE);
7653 		}
7654 
7655 		ipsq = ill->ill_phyint->phyint_ipsq;
7656 		mutex_enter(&ipsq->ipsq_lock);
7657 		if (ipsq->ipsq_writer == NULL &&
7658 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7659 			break;
7660 		} else if (ipsq->ipsq_writer != NULL) {
7661 			mutex_exit(&ipsq->ipsq_lock);
7662 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7663 		} else {
7664 			mutex_exit(&ipsq->ipsq_lock);
7665 			if (force) {
7666 				(void) cv_timedwait(&ill->ill_cv,
7667 				    &ill->ill_lock,
7668 				    lbolt + ENTER_SQ_WAIT_TICKS);
7669 				waited_enough = B_TRUE;
7670 				continue;
7671 			} else {
7672 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7673 			}
7674 		}
7675 	}
7676 
7677 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7678 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7679 	ipsq->ipsq_writer = curthread;
7680 	ipsq->ipsq_reentry_cnt++;
7681 #ifdef DEBUG
7682 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IPSQ_STACK_DEPTH);
7683 #endif
7684 	mutex_exit(&ipsq->ipsq_lock);
7685 	mutex_exit(&ill->ill_lock);
7686 	return (B_TRUE);
7687 }
7688 
7689 /*
7690  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7691  * certain critical operations like plumbing (i.e. most set ioctls),
7692  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7693  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7694  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7695  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7696  * threads executing in the ipsq. Responses from the driver pertain to the
7697  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7698  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7699  *
7700  * If a thread does not want to reenter the ipsq when it is already writer,
7701  * it must make sure that the specified reentry point to be called later
7702  * when the ipsq is empty, nor any code path starting from the specified reentry
7703  * point must never ever try to enter the ipsq again. Otherwise it can lead
7704  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7705  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7706  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7707  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7708  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7709  * ioctl if the current ioctl has completed. If the current ioctl is still
7710  * in progress it simply returns. The current ioctl could be waiting for
7711  * a response from another module (arp_ or the driver or could be waiting for
7712  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7713  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7714  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7715  * ipsq_current_ipif is clear which happens only on ioctl completion.
7716  */
7717 
7718 /*
7719  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7720  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7721  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7722  * completion.
7723  */
7724 ipsq_t *
7725 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7726     ipsq_func_t func, int type, boolean_t reentry_ok)
7727 {
7728 	ipsq_t	*ipsq;
7729 
7730 	/* Only 1 of ipif or ill can be specified */
7731 	ASSERT((ipif != NULL) ^ (ill != NULL));
7732 	if (ipif != NULL)
7733 		ill = ipif->ipif_ill;
7734 
7735 	/*
7736 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7737 	 * ipsq of an ill can't change when ill_lock is held.
7738 	 */
7739 	GRAB_CONN_LOCK(q);
7740 	mutex_enter(&ill->ill_lock);
7741 	ipsq = ill->ill_phyint->phyint_ipsq;
7742 	mutex_enter(&ipsq->ipsq_lock);
7743 
7744 	/*
7745 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7746 	 *    (Note: If the caller does not specify reentry_ok then neither
7747 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7748 	 *    again. Otherwise it can lead to an infinite loop
7749 	 * 2. Enter the ipsq if there is no current writer and this attempted
7750 	 *    entry is part of the current ioctl or operation
7751 	 * 3. Enter the ipsq if there is no current writer and this is a new
7752 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7753 	 *    empty and there is no ioctl (or operation) currently in progress
7754 	 */
7755 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7756 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7757 	    ipsq->ipsq_current_ipif == NULL))) ||
7758 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7759 		/* Success. */
7760 		ipsq->ipsq_reentry_cnt++;
7761 		ipsq->ipsq_writer = curthread;
7762 		mutex_exit(&ipsq->ipsq_lock);
7763 		mutex_exit(&ill->ill_lock);
7764 		RELEASE_CONN_LOCK(q);
7765 #ifdef DEBUG
7766 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack,
7767 		    IPSQ_STACK_DEPTH);
7768 #endif
7769 		return (ipsq);
7770 	}
7771 
7772 	ipsq_enq(ipsq, q, mp, func, type, ill);
7773 
7774 	mutex_exit(&ipsq->ipsq_lock);
7775 	mutex_exit(&ill->ill_lock);
7776 	RELEASE_CONN_LOCK(q);
7777 	return (NULL);
7778 }
7779 
7780 /*
7781  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7782  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7783  * cannot be entered, the mp is queued for completion.
7784  */
7785 void
7786 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7787     boolean_t reentry_ok)
7788 {
7789 	ipsq_t	*ipsq;
7790 
7791 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7792 
7793 	/*
7794 	 * Drop the caller's refhold on the ill.  This is safe since we either
7795 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7796 	 * IPSQ, in which case we return without accessing ill anymore.  This
7797 	 * is needed because func needs to see the correct refcount.
7798 	 * e.g. removeif can work only then.
7799 	 */
7800 	ill_refrele(ill);
7801 	if (ipsq != NULL) {
7802 		(*func)(ipsq, q, mp, NULL);
7803 		ipsq_exit(ipsq);
7804 	}
7805 }
7806 
7807 /*
7808  * If there are more than ILL_GRP_CNT ills in a group,
7809  * we use kmem alloc'd buffers, else use the stack
7810  */
7811 #define	ILL_GRP_CNT	14
7812 /*
7813  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7814  * Called by a thread that is currently exclusive on this ipsq.
7815  */
7816 void
7817 ipsq_exit(ipsq_t *ipsq)
7818 {
7819 	queue_t	*q;
7820 	mblk_t	*mp;
7821 	ipsq_func_t	func;
7822 	int	next;
7823 	ill_t	**ill_list = NULL;
7824 	size_t	ill_list_size = 0;
7825 	int	cnt = 0;
7826 	boolean_t need_ipsq_free = B_FALSE;
7827 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7828 
7829 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7830 	mutex_enter(&ipsq->ipsq_lock);
7831 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7832 	if (ipsq->ipsq_reentry_cnt != 1) {
7833 		ipsq->ipsq_reentry_cnt--;
7834 		mutex_exit(&ipsq->ipsq_lock);
7835 		return;
7836 	}
7837 
7838 	mp = ipsq_dq(ipsq);
7839 	while (mp != NULL) {
7840 again:
7841 		mutex_exit(&ipsq->ipsq_lock);
7842 		func = (ipsq_func_t)mp->b_prev;
7843 		q = (queue_t *)mp->b_queue;
7844 		mp->b_prev = NULL;
7845 		mp->b_queue = NULL;
7846 
7847 		/*
7848 		 * If 'q' is an conn queue, it is valid, since we did a
7849 		 * a refhold on the connp, at the start of the ioctl.
7850 		 * If 'q' is an ill queue, it is valid, since close of an
7851 		 * ill will clean up the 'ipsq'.
7852 		 */
7853 		(*func)(ipsq, q, mp, NULL);
7854 
7855 		mutex_enter(&ipsq->ipsq_lock);
7856 		mp = ipsq_dq(ipsq);
7857 	}
7858 
7859 	mutex_exit(&ipsq->ipsq_lock);
7860 
7861 	/*
7862 	 * Need to grab the locks in the right order. Need to
7863 	 * atomically check (under ipsq_lock) that there are no
7864 	 * messages before relinquishing the ipsq. Also need to
7865 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7866 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7867 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7868 	 * to grab ill_g_lock as writer.
7869 	 */
7870 	rw_enter(&ipst->ips_ill_g_lock,
7871 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7872 
7873 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7874 	if (ipsq->ipsq_refs != 0) {
7875 		/* At most 2 ills v4/v6 per phyint */
7876 		cnt = ipsq->ipsq_refs << 1;
7877 		ill_list_size = cnt * sizeof (ill_t *);
7878 		/*
7879 		 * If memory allocation fails, we will do the split
7880 		 * the next time ipsq_exit is called for whatever reason.
7881 		 * As long as the ipsq_split flag is set the need to
7882 		 * split is remembered.
7883 		 */
7884 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7885 		if (ill_list != NULL)
7886 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7887 	}
7888 	mutex_enter(&ipsq->ipsq_lock);
7889 	mp = ipsq_dq(ipsq);
7890 	if (mp != NULL) {
7891 		/* oops, some message has landed up, we can't get out */
7892 		if (ill_list != NULL)
7893 			ill_unlock_ills(ill_list, cnt);
7894 		rw_exit(&ipst->ips_ill_g_lock);
7895 		if (ill_list != NULL)
7896 			kmem_free(ill_list, ill_list_size);
7897 		ill_list = NULL;
7898 		ill_list_size = 0;
7899 		cnt = 0;
7900 		goto again;
7901 	}
7902 
7903 	/*
7904 	 * Split only if no ioctl is pending and if memory alloc succeeded
7905 	 * above.
7906 	 */
7907 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7908 	    ill_list != NULL) {
7909 		/*
7910 		 * No new ill can join this ipsq since we are holding the
7911 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7912 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7913 		 * If so we will retry on the next ipsq_exit.
7914 		 */
7915 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7916 	}
7917 
7918 	/*
7919 	 * We are holding the ipsq lock, hence no new messages can
7920 	 * land up on the ipsq, and there are no messages currently.
7921 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7922 	 * atomically while holding ill locks.
7923 	 */
7924 	ipsq->ipsq_writer = NULL;
7925 	ipsq->ipsq_reentry_cnt--;
7926 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7927 #ifdef DEBUG
7928 	ipsq->ipsq_depth = 0;
7929 #endif
7930 	mutex_exit(&ipsq->ipsq_lock);
7931 	/*
7932 	 * For IPMP this should wake up all ills in this ipsq.
7933 	 * We need to hold the ill_lock while waking up waiters to
7934 	 * avoid missed wakeups. But there is no need to acquire all
7935 	 * the ill locks and then wakeup. If we have not acquired all
7936 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7937 	 * wakes up ills one at a time after getting the right ill_lock
7938 	 */
7939 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7940 	if (ill_list != NULL)
7941 		ill_unlock_ills(ill_list, cnt);
7942 	if (ipsq->ipsq_refs == 0)
7943 		need_ipsq_free = B_TRUE;
7944 	rw_exit(&ipst->ips_ill_g_lock);
7945 	if (ill_list != 0)
7946 		kmem_free(ill_list, ill_list_size);
7947 
7948 	if (need_ipsq_free) {
7949 		/*
7950 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7951 		 * looked up. ipsq can be looked up only thru ill or phyint
7952 		 * and there are no ills/phyint on this ipsq.
7953 		 */
7954 		ipsq_delete(ipsq);
7955 	}
7956 
7957 	/*
7958 	 * Now that we're outside the IPSQ, start any IGMP/MLD timers.  We
7959 	 * can't start these inside the IPSQ since e.g. igmp_start_timers() ->
7960 	 * untimeout() (inside the IPSQ, waiting for an executing timeout to
7961 	 * finish) could deadlock with igmp_timeout_handler() -> ipsq_enter()
7962 	 * (executing the timeout, waiting to get inside the IPSQ).
7963 	 *
7964 	 * However, there is one exception to the above: if this thread *is*
7965 	 * the IGMP/MLD timeout handler thread, then we must not start its
7966 	 * timer until the current handler is done.
7967 	 */
7968 	mutex_enter(&ipst->ips_igmp_timer_lock);
7969 	if (curthread != ipst->ips_igmp_timer_thread) {
7970 		next = ipst->ips_igmp_deferred_next;
7971 		ipst->ips_igmp_deferred_next = INFINITY;
7972 		mutex_exit(&ipst->ips_igmp_timer_lock);
7973 
7974 		if (next != INFINITY)
7975 			igmp_start_timers(next, ipst);
7976 	} else {
7977 		mutex_exit(&ipst->ips_igmp_timer_lock);
7978 	}
7979 
7980 	mutex_enter(&ipst->ips_mld_timer_lock);
7981 	if (curthread != ipst->ips_mld_timer_thread) {
7982 		next = ipst->ips_mld_deferred_next;
7983 		ipst->ips_mld_deferred_next = INFINITY;
7984 		mutex_exit(&ipst->ips_mld_timer_lock);
7985 
7986 		if (next != INFINITY)
7987 			mld_start_timers(next, ipst);
7988 	} else {
7989 		mutex_exit(&ipst->ips_mld_timer_lock);
7990 	}
7991 }
7992 
7993 /*
7994  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7995  * and `ioccmd'.
7996  */
7997 void
7998 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
7999 {
8000 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8001 
8002 	mutex_enter(&ipsq->ipsq_lock);
8003 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8004 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8005 	ipsq->ipsq_current_done = B_FALSE;
8006 	ipsq->ipsq_current_ipif = ipif;
8007 	ipsq->ipsq_current_ioctl = ioccmd;
8008 	mutex_exit(&ipsq->ipsq_lock);
8009 }
8010 
8011 /*
8012  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
8013  * the next exclusive operation to begin once we ipsq_exit().  However, if
8014  * pending DLPI operations remain, then we will wait for the queue to drain
8015  * before allowing the next exclusive operation to begin.  This ensures that
8016  * DLPI operations from one exclusive operation are never improperly processed
8017  * as part of a subsequent exclusive operation.
8018  */
8019 void
8020 ipsq_current_finish(ipsq_t *ipsq)
8021 {
8022 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8023 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
8024 
8025 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8026 
8027 	/*
8028 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8029 	 * (but in that case, IPIF_CHANGING will already be clear and no
8030 	 * pending DLPI messages can remain).
8031 	 */
8032 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8033 		ill_t *ill = ipif->ipif_ill;
8034 
8035 		mutex_enter(&ill->ill_lock);
8036 		dlpi_pending = ill->ill_dlpi_pending;
8037 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8038 		/* Send any queued event */
8039 		ill_nic_info_dispatch(ill);
8040 		mutex_exit(&ill->ill_lock);
8041 	}
8042 
8043 	mutex_enter(&ipsq->ipsq_lock);
8044 	ipsq->ipsq_current_ioctl = 0;
8045 	ipsq->ipsq_current_done = B_TRUE;
8046 	if (dlpi_pending == DL_PRIM_INVAL)
8047 		ipsq->ipsq_current_ipif = NULL;
8048 	mutex_exit(&ipsq->ipsq_lock);
8049 }
8050 
8051 /*
8052  * The ill is closing. Flush all messages on the ipsq that originated
8053  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8054  * for this ill since ipsq_enter could not have entered until then.
8055  * New messages can't be queued since the CONDEMNED flag is set.
8056  */
8057 static void
8058 ipsq_flush(ill_t *ill)
8059 {
8060 	queue_t	*q;
8061 	mblk_t	*prev;
8062 	mblk_t	*mp;
8063 	mblk_t	*mp_next;
8064 	ipsq_t	*ipsq;
8065 
8066 	ASSERT(IAM_WRITER_ILL(ill));
8067 	ipsq = ill->ill_phyint->phyint_ipsq;
8068 	/*
8069 	 * Flush any messages sent up by the driver.
8070 	 */
8071 	mutex_enter(&ipsq->ipsq_lock);
8072 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8073 		mp_next = mp->b_next;
8074 		q = mp->b_queue;
8075 		if (q == ill->ill_rq || q == ill->ill_wq) {
8076 			/* Remove the mp from the ipsq */
8077 			if (prev == NULL)
8078 				ipsq->ipsq_mphead = mp->b_next;
8079 			else
8080 				prev->b_next = mp->b_next;
8081 			if (ipsq->ipsq_mptail == mp) {
8082 				ASSERT(mp_next == NULL);
8083 				ipsq->ipsq_mptail = prev;
8084 			}
8085 			inet_freemsg(mp);
8086 		} else {
8087 			prev = mp;
8088 		}
8089 	}
8090 	mutex_exit(&ipsq->ipsq_lock);
8091 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8092 	ipsq_xopq_mp_cleanup(ill, NULL);
8093 	ill_pending_mp_cleanup(ill);
8094 }
8095 
8096 /* ARGSUSED */
8097 int
8098 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8099     ip_ioctl_cmd_t *ipip, void *ifreq)
8100 {
8101 	ill_t	*ill;
8102 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8103 	boolean_t isv6;
8104 	conn_t	*connp;
8105 	ip_stack_t	*ipst;
8106 
8107 	connp = Q_TO_CONN(q);
8108 	ipst = connp->conn_netstack->netstack_ip;
8109 	isv6 = connp->conn_af_isv6;
8110 	/*
8111 	 * Set original index.
8112 	 * Failover and failback move logical interfaces
8113 	 * from one physical interface to another.  The
8114 	 * original index indicates the parent of a logical
8115 	 * interface, in other words, the physical interface
8116 	 * the logical interface will be moved back to on
8117 	 * failback.
8118 	 */
8119 
8120 	/*
8121 	 * Don't allow the original index to be changed
8122 	 * for non-failover addresses, autoconfigured
8123 	 * addresses, or IPv6 link local addresses.
8124 	 */
8125 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8126 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8127 		return (EINVAL);
8128 	}
8129 	/*
8130 	 * The new original index must be in use by some
8131 	 * physical interface.
8132 	 */
8133 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8134 	    NULL, NULL, ipst);
8135 	if (ill == NULL)
8136 		return (ENXIO);
8137 	ill_refrele(ill);
8138 
8139 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8140 	/*
8141 	 * When this ipif gets failed back, don't
8142 	 * preserve the original id, as it is no
8143 	 * longer applicable.
8144 	 */
8145 	ipif->ipif_orig_ipifid = 0;
8146 	/*
8147 	 * For IPv4, change the original index of any
8148 	 * multicast addresses associated with the
8149 	 * ipif to the new value.
8150 	 */
8151 	if (!isv6) {
8152 		ilm_t *ilm;
8153 
8154 		mutex_enter(&ipif->ipif_ill->ill_lock);
8155 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8156 		    ilm = ilm->ilm_next) {
8157 			if (ilm->ilm_ipif == ipif) {
8158 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8159 			}
8160 		}
8161 		mutex_exit(&ipif->ipif_ill->ill_lock);
8162 	}
8163 	return (0);
8164 }
8165 
8166 /* ARGSUSED */
8167 int
8168 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8169     ip_ioctl_cmd_t *ipip, void *ifreq)
8170 {
8171 	struct lifreq *lifr = (struct lifreq *)ifreq;
8172 
8173 	/*
8174 	 * Get the original interface index i.e the one
8175 	 * before FAILOVER if it ever happened.
8176 	 */
8177 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8178 	return (0);
8179 }
8180 
8181 /*
8182  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8183  * refhold and return the associated ipif
8184  */
8185 /* ARGSUSED */
8186 int
8187 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8188     cmd_info_t *ci, ipsq_func_t func)
8189 {
8190 	boolean_t exists;
8191 	struct iftun_req *ta;
8192 	ipif_t	*ipif;
8193 	ill_t	*ill;
8194 	boolean_t isv6;
8195 	mblk_t	*mp1;
8196 	int	error;
8197 	conn_t	*connp;
8198 	ip_stack_t	*ipst;
8199 
8200 	/* Existence verified in ip_wput_nondata */
8201 	mp1 = mp->b_cont->b_cont;
8202 	ta = (struct iftun_req *)mp1->b_rptr;
8203 	/*
8204 	 * Null terminate the string to protect against buffer
8205 	 * overrun. String was generated by user code and may not
8206 	 * be trusted.
8207 	 */
8208 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8209 
8210 	connp = Q_TO_CONN(q);
8211 	isv6 = connp->conn_af_isv6;
8212 	ipst = connp->conn_netstack->netstack_ip;
8213 
8214 	/* Disallows implicit create */
8215 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8216 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8217 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8218 	if (ipif == NULL)
8219 		return (error);
8220 
8221 	if (ipif->ipif_id != 0) {
8222 		/*
8223 		 * We really don't want to set/get tunnel parameters
8224 		 * on virtual tunnel interfaces.  Only allow the
8225 		 * base tunnel to do these.
8226 		 */
8227 		ipif_refrele(ipif);
8228 		return (EINVAL);
8229 	}
8230 
8231 	/*
8232 	 * Send down to tunnel mod for ioctl processing.
8233 	 * Will finish ioctl in ip_rput_other().
8234 	 */
8235 	ill = ipif->ipif_ill;
8236 	if (ill->ill_net_type == IRE_LOOPBACK) {
8237 		ipif_refrele(ipif);
8238 		return (EOPNOTSUPP);
8239 	}
8240 
8241 	if (ill->ill_wq == NULL) {
8242 		ipif_refrele(ipif);
8243 		return (ENXIO);
8244 	}
8245 	/*
8246 	 * Mark the ioctl as coming from an IPv6 interface for
8247 	 * tun's convenience.
8248 	 */
8249 	if (ill->ill_isv6)
8250 		ta->ifta_flags |= 0x80000000;
8251 	ci->ci_ipif = ipif;
8252 	return (0);
8253 }
8254 
8255 /*
8256  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8257  * and return the associated ipif.
8258  * Return value:
8259  *	Non zero: An error has occurred. ci may not be filled out.
8260  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8261  *	a held ipif in ci.ci_ipif.
8262  */
8263 int
8264 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8265     cmd_info_t *ci, ipsq_func_t func)
8266 {
8267 	sin_t		*sin;
8268 	sin6_t		*sin6;
8269 	char		*name;
8270 	struct ifreq    *ifr;
8271 	struct lifreq    *lifr;
8272 	ipif_t		*ipif = NULL;
8273 	ill_t		*ill;
8274 	conn_t		*connp;
8275 	boolean_t	isv6;
8276 	boolean_t	exists;
8277 	int		err;
8278 	mblk_t		*mp1;
8279 	zoneid_t	zoneid;
8280 	ip_stack_t	*ipst;
8281 
8282 	if (q->q_next != NULL) {
8283 		ill = (ill_t *)q->q_ptr;
8284 		isv6 = ill->ill_isv6;
8285 		connp = NULL;
8286 		zoneid = ALL_ZONES;
8287 		ipst = ill->ill_ipst;
8288 	} else {
8289 		ill = NULL;
8290 		connp = Q_TO_CONN(q);
8291 		isv6 = connp->conn_af_isv6;
8292 		zoneid = connp->conn_zoneid;
8293 		if (zoneid == GLOBAL_ZONEID) {
8294 			/* global zone can access ipifs in all zones */
8295 			zoneid = ALL_ZONES;
8296 		}
8297 		ipst = connp->conn_netstack->netstack_ip;
8298 	}
8299 
8300 	/* Has been checked in ip_wput_nondata */
8301 	mp1 = mp->b_cont->b_cont;
8302 
8303 	if (ipip->ipi_cmd_type == IF_CMD) {
8304 		/* This a old style SIOC[GS]IF* command */
8305 		ifr = (struct ifreq *)mp1->b_rptr;
8306 		/*
8307 		 * Null terminate the string to protect against buffer
8308 		 * overrun. String was generated by user code and may not
8309 		 * be trusted.
8310 		 */
8311 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8312 		sin = (sin_t *)&ifr->ifr_addr;
8313 		name = ifr->ifr_name;
8314 		ci->ci_sin = sin;
8315 		ci->ci_sin6 = NULL;
8316 		ci->ci_lifr = (struct lifreq *)ifr;
8317 	} else {
8318 		/* This a new style SIOC[GS]LIF* command */
8319 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8320 		lifr = (struct lifreq *)mp1->b_rptr;
8321 		/*
8322 		 * Null terminate the string to protect against buffer
8323 		 * overrun. String was generated by user code and may not
8324 		 * be trusted.
8325 		 */
8326 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8327 		name = lifr->lifr_name;
8328 		sin = (sin_t *)&lifr->lifr_addr;
8329 		sin6 = (sin6_t *)&lifr->lifr_addr;
8330 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8331 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8332 			    LIFNAMSIZ);
8333 		}
8334 		ci->ci_sin = sin;
8335 		ci->ci_sin6 = sin6;
8336 		ci->ci_lifr = lifr;
8337 	}
8338 
8339 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8340 		/*
8341 		 * The ioctl will be failed if the ioctl comes down
8342 		 * an conn stream
8343 		 */
8344 		if (ill == NULL) {
8345 			/*
8346 			 * Not an ill queue, return EINVAL same as the
8347 			 * old error code.
8348 			 */
8349 			return (ENXIO);
8350 		}
8351 		ipif = ill->ill_ipif;
8352 		ipif_refhold(ipif);
8353 	} else {
8354 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8355 		    &exists, isv6, zoneid,
8356 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8357 		    ipst);
8358 		if (ipif == NULL) {
8359 			if (err == EINPROGRESS)
8360 				return (err);
8361 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8362 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8363 				/*
8364 				 * Need to try both v4 and v6 since this
8365 				 * ioctl can come down either v4 or v6
8366 				 * socket. The lifreq.lifr_family passed
8367 				 * down by this ioctl is AF_UNSPEC.
8368 				 */
8369 				ipif = ipif_lookup_on_name(name,
8370 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8371 				    zoneid, (connp == NULL) ? q :
8372 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8373 				if (err == EINPROGRESS)
8374 					return (err);
8375 			}
8376 			err = 0;	/* Ensure we don't use it below */
8377 		}
8378 	}
8379 
8380 	/*
8381 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8382 	 */
8383 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8384 		ipif_refrele(ipif);
8385 		return (ENXIO);
8386 	}
8387 
8388 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8389 	    name[0] == '\0') {
8390 		/*
8391 		 * Handle a or a SIOC?IF* with a null name
8392 		 * during plumb (on the ill queue before the I_PLINK).
8393 		 */
8394 		ipif = ill->ill_ipif;
8395 		ipif_refhold(ipif);
8396 	}
8397 
8398 	if (ipif == NULL)
8399 		return (ENXIO);
8400 
8401 	/*
8402 	 * Allow only GET operations if this ipif has been created
8403 	 * temporarily due to a MOVE operation.
8404 	 */
8405 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8406 		ipif_refrele(ipif);
8407 		return (EINVAL);
8408 	}
8409 
8410 	ci->ci_ipif = ipif;
8411 	return (0);
8412 }
8413 
8414 /*
8415  * Return the total number of ipifs.
8416  */
8417 static uint_t
8418 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8419 {
8420 	uint_t numifs = 0;
8421 	ill_t	*ill;
8422 	ill_walk_context_t	ctx;
8423 	ipif_t	*ipif;
8424 
8425 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8426 	ill = ILL_START_WALK_V4(&ctx, ipst);
8427 
8428 	while (ill != NULL) {
8429 		for (ipif = ill->ill_ipif; ipif != NULL;
8430 		    ipif = ipif->ipif_next) {
8431 			if (ipif->ipif_zoneid == zoneid ||
8432 			    ipif->ipif_zoneid == ALL_ZONES)
8433 				numifs++;
8434 		}
8435 		ill = ill_next(&ctx, ill);
8436 	}
8437 	rw_exit(&ipst->ips_ill_g_lock);
8438 	return (numifs);
8439 }
8440 
8441 /*
8442  * Return the total number of ipifs.
8443  */
8444 static uint_t
8445 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8446 {
8447 	uint_t numifs = 0;
8448 	ill_t	*ill;
8449 	ipif_t	*ipif;
8450 	ill_walk_context_t	ctx;
8451 
8452 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8453 
8454 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8455 	if (family == AF_INET)
8456 		ill = ILL_START_WALK_V4(&ctx, ipst);
8457 	else if (family == AF_INET6)
8458 		ill = ILL_START_WALK_V6(&ctx, ipst);
8459 	else
8460 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8461 
8462 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8463 		for (ipif = ill->ill_ipif; ipif != NULL;
8464 		    ipif = ipif->ipif_next) {
8465 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8466 			    !(lifn_flags & LIFC_NOXMIT))
8467 				continue;
8468 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8469 			    !(lifn_flags & LIFC_TEMPORARY))
8470 				continue;
8471 			if (((ipif->ipif_flags &
8472 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8473 			    IPIF_DEPRECATED)) ||
8474 			    IS_LOOPBACK(ill) ||
8475 			    !(ipif->ipif_flags & IPIF_UP)) &&
8476 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8477 				continue;
8478 
8479 			if (zoneid != ipif->ipif_zoneid &&
8480 			    ipif->ipif_zoneid != ALL_ZONES &&
8481 			    (zoneid != GLOBAL_ZONEID ||
8482 			    !(lifn_flags & LIFC_ALLZONES)))
8483 				continue;
8484 
8485 			numifs++;
8486 		}
8487 	}
8488 	rw_exit(&ipst->ips_ill_g_lock);
8489 	return (numifs);
8490 }
8491 
8492 uint_t
8493 ip_get_lifsrcofnum(ill_t *ill)
8494 {
8495 	uint_t numifs = 0;
8496 	ill_t	*ill_head = ill;
8497 	ip_stack_t	*ipst = ill->ill_ipst;
8498 
8499 	/*
8500 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8501 	 * other thread may be trying to relink the ILLs in this usesrc group
8502 	 * and adjusting the ill_usesrc_grp_next pointers
8503 	 */
8504 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8505 	if ((ill->ill_usesrc_ifindex == 0) &&
8506 	    (ill->ill_usesrc_grp_next != NULL)) {
8507 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8508 		    ill = ill->ill_usesrc_grp_next)
8509 			numifs++;
8510 	}
8511 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8512 
8513 	return (numifs);
8514 }
8515 
8516 /* Null values are passed in for ipif, sin, and ifreq */
8517 /* ARGSUSED */
8518 int
8519 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8520     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8521 {
8522 	int *nump;
8523 	conn_t *connp = Q_TO_CONN(q);
8524 
8525 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8526 
8527 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8528 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8529 
8530 	*nump = ip_get_numifs(connp->conn_zoneid,
8531 	    connp->conn_netstack->netstack_ip);
8532 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8533 	return (0);
8534 }
8535 
8536 /* Null values are passed in for ipif, sin, and ifreq */
8537 /* ARGSUSED */
8538 int
8539 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8540     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8541 {
8542 	struct lifnum *lifn;
8543 	mblk_t	*mp1;
8544 	conn_t *connp = Q_TO_CONN(q);
8545 
8546 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8547 
8548 	/* Existence checked in ip_wput_nondata */
8549 	mp1 = mp->b_cont->b_cont;
8550 
8551 	lifn = (struct lifnum *)mp1->b_rptr;
8552 	switch (lifn->lifn_family) {
8553 	case AF_UNSPEC:
8554 	case AF_INET:
8555 	case AF_INET6:
8556 		break;
8557 	default:
8558 		return (EAFNOSUPPORT);
8559 	}
8560 
8561 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8562 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8563 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8564 	return (0);
8565 }
8566 
8567 /* ARGSUSED */
8568 int
8569 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8570     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8571 {
8572 	STRUCT_HANDLE(ifconf, ifc);
8573 	mblk_t *mp1;
8574 	struct iocblk *iocp;
8575 	struct ifreq *ifr;
8576 	ill_walk_context_t	ctx;
8577 	ill_t	*ill;
8578 	ipif_t	*ipif;
8579 	struct sockaddr_in *sin;
8580 	int32_t	ifclen;
8581 	zoneid_t zoneid;
8582 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8583 
8584 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8585 
8586 	ip1dbg(("ip_sioctl_get_ifconf"));
8587 	/* Existence verified in ip_wput_nondata */
8588 	mp1 = mp->b_cont->b_cont;
8589 	iocp = (struct iocblk *)mp->b_rptr;
8590 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8591 
8592 	/*
8593 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8594 	 * the user buffer address and length into which the list of struct
8595 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8596 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8597 	 * the SIOCGIFCONF operation was redefined to simply provide
8598 	 * a large output buffer into which we are supposed to jam the ifreq
8599 	 * array.  The same ioctl command code was used, despite the fact that
8600 	 * both the applications and the kernel code had to change, thus making
8601 	 * it impossible to support both interfaces.
8602 	 *
8603 	 * For reasons not good enough to try to explain, the following
8604 	 * algorithm is used for deciding what to do with one of these:
8605 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8606 	 * form with the output buffer coming down as the continuation message.
8607 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8608 	 * and we have to copy in the ifconf structure to find out how big the
8609 	 * output buffer is and where to copy out to.  Sure no problem...
8610 	 *
8611 	 */
8612 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8613 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8614 		int numifs = 0;
8615 		size_t ifc_bufsize;
8616 
8617 		/*
8618 		 * Must be (better be!) continuation of a TRANSPARENT
8619 		 * IOCTL.  We just copied in the ifconf structure.
8620 		 */
8621 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8622 		    (struct ifconf *)mp1->b_rptr);
8623 
8624 		/*
8625 		 * Allocate a buffer to hold requested information.
8626 		 *
8627 		 * If ifc_len is larger than what is needed, we only
8628 		 * allocate what we will use.
8629 		 *
8630 		 * If ifc_len is smaller than what is needed, return
8631 		 * EINVAL.
8632 		 *
8633 		 * XXX: the ill_t structure can hava 2 counters, for
8634 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8635 		 * number of interfaces for a device, so we don't need
8636 		 * to count them here...
8637 		 */
8638 		numifs = ip_get_numifs(zoneid, ipst);
8639 
8640 		ifclen = STRUCT_FGET(ifc, ifc_len);
8641 		ifc_bufsize = numifs * sizeof (struct ifreq);
8642 		if (ifc_bufsize > ifclen) {
8643 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8644 				/* old behaviour */
8645 				return (EINVAL);
8646 			} else {
8647 				ifc_bufsize = ifclen;
8648 			}
8649 		}
8650 
8651 		mp1 = mi_copyout_alloc(q, mp,
8652 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8653 		if (mp1 == NULL)
8654 			return (ENOMEM);
8655 
8656 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8657 	}
8658 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8659 	/*
8660 	 * the SIOCGIFCONF ioctl only knows about
8661 	 * IPv4 addresses, so don't try to tell
8662 	 * it about interfaces with IPv6-only
8663 	 * addresses. (Last parm 'isv6' is B_FALSE)
8664 	 */
8665 
8666 	ifr = (struct ifreq *)mp1->b_rptr;
8667 
8668 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8669 	ill = ILL_START_WALK_V4(&ctx, ipst);
8670 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8671 		for (ipif = ill->ill_ipif; ipif != NULL;
8672 		    ipif = ipif->ipif_next) {
8673 			if (zoneid != ipif->ipif_zoneid &&
8674 			    ipif->ipif_zoneid != ALL_ZONES)
8675 				continue;
8676 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8677 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8678 					/* old behaviour */
8679 					rw_exit(&ipst->ips_ill_g_lock);
8680 					return (EINVAL);
8681 				} else {
8682 					goto if_copydone;
8683 				}
8684 			}
8685 			ipif_get_name(ipif, ifr->ifr_name,
8686 			    sizeof (ifr->ifr_name));
8687 			sin = (sin_t *)&ifr->ifr_addr;
8688 			*sin = sin_null;
8689 			sin->sin_family = AF_INET;
8690 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8691 			ifr++;
8692 		}
8693 	}
8694 if_copydone:
8695 	rw_exit(&ipst->ips_ill_g_lock);
8696 	mp1->b_wptr = (uchar_t *)ifr;
8697 
8698 	if (STRUCT_BUF(ifc) != NULL) {
8699 		STRUCT_FSET(ifc, ifc_len,
8700 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8701 	}
8702 	return (0);
8703 }
8704 
8705 /*
8706  * Get the interfaces using the address hosted on the interface passed in,
8707  * as a source adddress
8708  */
8709 /* ARGSUSED */
8710 int
8711 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8712     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8713 {
8714 	mblk_t *mp1;
8715 	ill_t	*ill, *ill_head;
8716 	ipif_t	*ipif, *orig_ipif;
8717 	int	numlifs = 0;
8718 	size_t	lifs_bufsize, lifsmaxlen;
8719 	struct	lifreq *lifr;
8720 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8721 	uint_t	ifindex;
8722 	zoneid_t zoneid;
8723 	int err = 0;
8724 	boolean_t isv6 = B_FALSE;
8725 	struct	sockaddr_in	*sin;
8726 	struct	sockaddr_in6	*sin6;
8727 	STRUCT_HANDLE(lifsrcof, lifs);
8728 	ip_stack_t		*ipst;
8729 
8730 	ipst = CONNQ_TO_IPST(q);
8731 
8732 	ASSERT(q->q_next == NULL);
8733 
8734 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8735 
8736 	/* Existence verified in ip_wput_nondata */
8737 	mp1 = mp->b_cont->b_cont;
8738 
8739 	/*
8740 	 * Must be (better be!) continuation of a TRANSPARENT
8741 	 * IOCTL.  We just copied in the lifsrcof structure.
8742 	 */
8743 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8744 	    (struct lifsrcof *)mp1->b_rptr);
8745 
8746 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8747 		return (EINVAL);
8748 
8749 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8750 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8751 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8752 	    ip_process_ioctl, &err, ipst);
8753 	if (ipif == NULL) {
8754 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8755 		    ifindex));
8756 		return (err);
8757 	}
8758 
8759 	/* Allocate a buffer to hold requested information */
8760 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8761 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8762 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8763 	/* The actual size needed is always returned in lifs_len */
8764 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8765 
8766 	/* If the amount we need is more than what is passed in, abort */
8767 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8768 		ipif_refrele(ipif);
8769 		return (0);
8770 	}
8771 
8772 	mp1 = mi_copyout_alloc(q, mp,
8773 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8774 	if (mp1 == NULL) {
8775 		ipif_refrele(ipif);
8776 		return (ENOMEM);
8777 	}
8778 
8779 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8780 	bzero(mp1->b_rptr, lifs_bufsize);
8781 
8782 	lifr = (struct lifreq *)mp1->b_rptr;
8783 
8784 	ill = ill_head = ipif->ipif_ill;
8785 	orig_ipif = ipif;
8786 
8787 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8788 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8789 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8790 
8791 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8792 	for (; (ill != NULL) && (ill != ill_head);
8793 	    ill = ill->ill_usesrc_grp_next) {
8794 
8795 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8796 			break;
8797 
8798 		ipif = ill->ill_ipif;
8799 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8800 		if (ipif->ipif_isv6) {
8801 			sin6 = (sin6_t *)&lifr->lifr_addr;
8802 			*sin6 = sin6_null;
8803 			sin6->sin6_family = AF_INET6;
8804 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8805 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8806 			    &ipif->ipif_v6net_mask);
8807 		} else {
8808 			sin = (sin_t *)&lifr->lifr_addr;
8809 			*sin = sin_null;
8810 			sin->sin_family = AF_INET;
8811 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8812 			lifr->lifr_addrlen = ip_mask_to_plen(
8813 			    ipif->ipif_net_mask);
8814 		}
8815 		lifr++;
8816 	}
8817 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8818 	rw_exit(&ipst->ips_ill_g_lock);
8819 	ipif_refrele(orig_ipif);
8820 	mp1->b_wptr = (uchar_t *)lifr;
8821 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8822 
8823 	return (0);
8824 }
8825 
8826 /* ARGSUSED */
8827 int
8828 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8829     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8830 {
8831 	mblk_t *mp1;
8832 	int	list;
8833 	ill_t	*ill;
8834 	ipif_t	*ipif;
8835 	int	flags;
8836 	int	numlifs = 0;
8837 	size_t	lifc_bufsize;
8838 	struct	lifreq *lifr;
8839 	sa_family_t	family;
8840 	struct	sockaddr_in	*sin;
8841 	struct	sockaddr_in6	*sin6;
8842 	ill_walk_context_t	ctx;
8843 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8844 	int32_t	lifclen;
8845 	zoneid_t zoneid;
8846 	STRUCT_HANDLE(lifconf, lifc);
8847 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8848 
8849 	ip1dbg(("ip_sioctl_get_lifconf"));
8850 
8851 	ASSERT(q->q_next == NULL);
8852 
8853 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8854 
8855 	/* Existence verified in ip_wput_nondata */
8856 	mp1 = mp->b_cont->b_cont;
8857 
8858 	/*
8859 	 * An extended version of SIOCGIFCONF that takes an
8860 	 * additional address family and flags field.
8861 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8862 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8863 	 * interfaces are omitted.
8864 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8865 	 * unless LIFC_TEMPORARY is specified.
8866 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8867 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8868 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8869 	 * has priority over LIFC_NOXMIT.
8870 	 */
8871 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8872 
8873 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8874 		return (EINVAL);
8875 
8876 	/*
8877 	 * Must be (better be!) continuation of a TRANSPARENT
8878 	 * IOCTL.  We just copied in the lifconf structure.
8879 	 */
8880 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8881 
8882 	family = STRUCT_FGET(lifc, lifc_family);
8883 	flags = STRUCT_FGET(lifc, lifc_flags);
8884 
8885 	switch (family) {
8886 	case AF_UNSPEC:
8887 		/*
8888 		 * walk all ILL's.
8889 		 */
8890 		list = MAX_G_HEADS;
8891 		break;
8892 	case AF_INET:
8893 		/*
8894 		 * walk only IPV4 ILL's.
8895 		 */
8896 		list = IP_V4_G_HEAD;
8897 		break;
8898 	case AF_INET6:
8899 		/*
8900 		 * walk only IPV6 ILL's.
8901 		 */
8902 		list = IP_V6_G_HEAD;
8903 		break;
8904 	default:
8905 		return (EAFNOSUPPORT);
8906 	}
8907 
8908 	/*
8909 	 * Allocate a buffer to hold requested information.
8910 	 *
8911 	 * If lifc_len is larger than what is needed, we only
8912 	 * allocate what we will use.
8913 	 *
8914 	 * If lifc_len is smaller than what is needed, return
8915 	 * EINVAL.
8916 	 */
8917 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8918 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8919 	lifclen = STRUCT_FGET(lifc, lifc_len);
8920 	if (lifc_bufsize > lifclen) {
8921 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8922 			return (EINVAL);
8923 		else
8924 			lifc_bufsize = lifclen;
8925 	}
8926 
8927 	mp1 = mi_copyout_alloc(q, mp,
8928 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8929 	if (mp1 == NULL)
8930 		return (ENOMEM);
8931 
8932 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8933 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8934 
8935 	lifr = (struct lifreq *)mp1->b_rptr;
8936 
8937 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8938 	ill = ill_first(list, list, &ctx, ipst);
8939 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8940 		for (ipif = ill->ill_ipif; ipif != NULL;
8941 		    ipif = ipif->ipif_next) {
8942 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8943 			    !(flags & LIFC_NOXMIT))
8944 				continue;
8945 
8946 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8947 			    !(flags & LIFC_TEMPORARY))
8948 				continue;
8949 
8950 			if (((ipif->ipif_flags &
8951 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8952 			    IPIF_DEPRECATED)) ||
8953 			    IS_LOOPBACK(ill) ||
8954 			    !(ipif->ipif_flags & IPIF_UP)) &&
8955 			    (flags & LIFC_EXTERNAL_SOURCE))
8956 				continue;
8957 
8958 			if (zoneid != ipif->ipif_zoneid &&
8959 			    ipif->ipif_zoneid != ALL_ZONES &&
8960 			    (zoneid != GLOBAL_ZONEID ||
8961 			    !(flags & LIFC_ALLZONES)))
8962 				continue;
8963 
8964 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8965 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8966 					rw_exit(&ipst->ips_ill_g_lock);
8967 					return (EINVAL);
8968 				} else {
8969 					goto lif_copydone;
8970 				}
8971 			}
8972 
8973 			ipif_get_name(ipif, lifr->lifr_name,
8974 			    sizeof (lifr->lifr_name));
8975 			if (ipif->ipif_isv6) {
8976 				sin6 = (sin6_t *)&lifr->lifr_addr;
8977 				*sin6 = sin6_null;
8978 				sin6->sin6_family = AF_INET6;
8979 				sin6->sin6_addr =
8980 				    ipif->ipif_v6lcl_addr;
8981 				lifr->lifr_addrlen =
8982 				    ip_mask_to_plen_v6(
8983 				    &ipif->ipif_v6net_mask);
8984 			} else {
8985 				sin = (sin_t *)&lifr->lifr_addr;
8986 				*sin = sin_null;
8987 				sin->sin_family = AF_INET;
8988 				sin->sin_addr.s_addr =
8989 				    ipif->ipif_lcl_addr;
8990 				lifr->lifr_addrlen =
8991 				    ip_mask_to_plen(
8992 				    ipif->ipif_net_mask);
8993 			}
8994 			lifr++;
8995 		}
8996 	}
8997 lif_copydone:
8998 	rw_exit(&ipst->ips_ill_g_lock);
8999 
9000 	mp1->b_wptr = (uchar_t *)lifr;
9001 	if (STRUCT_BUF(lifc) != NULL) {
9002 		STRUCT_FSET(lifc, lifc_len,
9003 		    (int)((uchar_t *)lifr - mp1->b_rptr));
9004 	}
9005 	return (0);
9006 }
9007 
9008 /* ARGSUSED */
9009 int
9010 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9011     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9012 {
9013 	ip_stack_t	*ipst;
9014 
9015 	if (q->q_next == NULL)
9016 		ipst = CONNQ_TO_IPST(q);
9017 	else
9018 		ipst = ILLQ_TO_IPST(q);
9019 
9020 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9021 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9022 	return (0);
9023 }
9024 
9025 static void
9026 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9027 {
9028 	ip6_asp_t *table;
9029 	size_t table_size;
9030 	mblk_t *data_mp;
9031 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9032 	ip_stack_t	*ipst;
9033 
9034 	if (q->q_next == NULL)
9035 		ipst = CONNQ_TO_IPST(q);
9036 	else
9037 		ipst = ILLQ_TO_IPST(q);
9038 
9039 	/* These two ioctls are I_STR only */
9040 	if (iocp->ioc_count == TRANSPARENT) {
9041 		miocnak(q, mp, 0, EINVAL);
9042 		return;
9043 	}
9044 
9045 	data_mp = mp->b_cont;
9046 	if (data_mp == NULL) {
9047 		/* The user passed us a NULL argument */
9048 		table = NULL;
9049 		table_size = iocp->ioc_count;
9050 	} else {
9051 		/*
9052 		 * The user provided a table.  The stream head
9053 		 * may have copied in the user data in chunks,
9054 		 * so make sure everything is pulled up
9055 		 * properly.
9056 		 */
9057 		if (MBLKL(data_mp) < iocp->ioc_count) {
9058 			mblk_t *new_data_mp;
9059 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9060 			    NULL) {
9061 				miocnak(q, mp, 0, ENOMEM);
9062 				return;
9063 			}
9064 			freemsg(data_mp);
9065 			data_mp = new_data_mp;
9066 			mp->b_cont = data_mp;
9067 		}
9068 		table = (ip6_asp_t *)data_mp->b_rptr;
9069 		table_size = iocp->ioc_count;
9070 	}
9071 
9072 	switch (iocp->ioc_cmd) {
9073 	case SIOCGIP6ADDRPOLICY:
9074 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9075 		if (iocp->ioc_rval == -1)
9076 			iocp->ioc_error = EINVAL;
9077 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9078 		else if (table != NULL &&
9079 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9080 			ip6_asp_t *src = table;
9081 			ip6_asp32_t *dst = (void *)table;
9082 			int count = table_size / sizeof (ip6_asp_t);
9083 			int i;
9084 
9085 			/*
9086 			 * We need to do an in-place shrink of the array
9087 			 * to match the alignment attributes of the
9088 			 * 32-bit ABI looking at it.
9089 			 */
9090 			/* LINTED: logical expression always true: op "||" */
9091 			ASSERT(sizeof (*src) > sizeof (*dst));
9092 			for (i = 1; i < count; i++)
9093 				bcopy(src + i, dst + i, sizeof (*dst));
9094 		}
9095 #endif
9096 		break;
9097 
9098 	case SIOCSIP6ADDRPOLICY:
9099 		ASSERT(mp->b_prev == NULL);
9100 		mp->b_prev = (void *)q;
9101 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9102 		/*
9103 		 * We pass in the datamodel here so that the ip6_asp_replace()
9104 		 * routine can handle converting from 32-bit to native formats
9105 		 * where necessary.
9106 		 *
9107 		 * A better way to handle this might be to convert the inbound
9108 		 * data structure here, and hang it off a new 'mp'; thus the
9109 		 * ip6_asp_replace() logic would always be dealing with native
9110 		 * format data structures..
9111 		 *
9112 		 * (An even simpler way to handle these ioctls is to just
9113 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9114 		 * and just recompile everything that depends on it.)
9115 		 */
9116 #endif
9117 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9118 		    iocp->ioc_flag & IOC_MODELS);
9119 		return;
9120 	}
9121 
9122 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9123 	qreply(q, mp);
9124 }
9125 
9126 static void
9127 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9128 {
9129 	mblk_t 		*data_mp;
9130 	struct dstinforeq	*dir;
9131 	uint8_t		*end, *cur;
9132 	in6_addr_t	*daddr, *saddr;
9133 	ipaddr_t	v4daddr;
9134 	ire_t		*ire;
9135 	char		*slabel, *dlabel;
9136 	boolean_t	isipv4;
9137 	int		match_ire;
9138 	ill_t		*dst_ill;
9139 	ipif_t		*src_ipif, *ire_ipif;
9140 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9141 	zoneid_t	zoneid;
9142 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9143 
9144 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9145 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9146 
9147 	/*
9148 	 * This ioctl is I_STR only, and must have a
9149 	 * data mblk following the M_IOCTL mblk.
9150 	 */
9151 	data_mp = mp->b_cont;
9152 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9153 		miocnak(q, mp, 0, EINVAL);
9154 		return;
9155 	}
9156 
9157 	if (MBLKL(data_mp) < iocp->ioc_count) {
9158 		mblk_t *new_data_mp;
9159 
9160 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9161 			miocnak(q, mp, 0, ENOMEM);
9162 			return;
9163 		}
9164 		freemsg(data_mp);
9165 		data_mp = new_data_mp;
9166 		mp->b_cont = data_mp;
9167 	}
9168 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9169 
9170 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9171 	    end - cur >= sizeof (struct dstinforeq);
9172 	    cur += sizeof (struct dstinforeq)) {
9173 		dir = (struct dstinforeq *)cur;
9174 		daddr = &dir->dir_daddr;
9175 		saddr = &dir->dir_saddr;
9176 
9177 		/*
9178 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9179 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9180 		 * and ipif_select_source[_v6]() do not.
9181 		 */
9182 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9183 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9184 
9185 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9186 		if (isipv4) {
9187 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9188 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9189 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9190 		} else {
9191 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9192 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9193 		}
9194 		if (ire == NULL) {
9195 			dir->dir_dreachable = 0;
9196 
9197 			/* move on to next dst addr */
9198 			continue;
9199 		}
9200 		dir->dir_dreachable = 1;
9201 
9202 		ire_ipif = ire->ire_ipif;
9203 		if (ire_ipif == NULL)
9204 			goto next_dst;
9205 
9206 		/*
9207 		 * We expect to get back an interface ire or a
9208 		 * gateway ire cache entry.  For both types, the
9209 		 * output interface is ire_ipif->ipif_ill.
9210 		 */
9211 		dst_ill = ire_ipif->ipif_ill;
9212 		dir->dir_dmactype = dst_ill->ill_mactype;
9213 
9214 		if (isipv4) {
9215 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9216 		} else {
9217 			src_ipif = ipif_select_source_v6(dst_ill,
9218 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9219 			    zoneid);
9220 		}
9221 		if (src_ipif == NULL)
9222 			goto next_dst;
9223 
9224 		*saddr = src_ipif->ipif_v6lcl_addr;
9225 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9226 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9227 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9228 		dir->dir_sdeprecated =
9229 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9230 		ipif_refrele(src_ipif);
9231 next_dst:
9232 		ire_refrele(ire);
9233 	}
9234 	miocack(q, mp, iocp->ioc_count, 0);
9235 }
9236 
9237 /*
9238  * Check if this is an address assigned to this machine.
9239  * Skips interfaces that are down by using ire checks.
9240  * Translates mapped addresses to v4 addresses and then
9241  * treats them as such, returning true if the v4 address
9242  * associated with this mapped address is configured.
9243  * Note: Applications will have to be careful what they do
9244  * with the response; use of mapped addresses limits
9245  * what can be done with the socket, especially with
9246  * respect to socket options and ioctls - neither IPv4
9247  * options nor IPv6 sticky options/ancillary data options
9248  * may be used.
9249  */
9250 /* ARGSUSED */
9251 int
9252 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9253     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9254 {
9255 	struct sioc_addrreq *sia;
9256 	sin_t *sin;
9257 	ire_t *ire;
9258 	mblk_t *mp1;
9259 	zoneid_t zoneid;
9260 	ip_stack_t	*ipst;
9261 
9262 	ip1dbg(("ip_sioctl_tmyaddr"));
9263 
9264 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9265 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9266 	ipst = CONNQ_TO_IPST(q);
9267 
9268 	/* Existence verified in ip_wput_nondata */
9269 	mp1 = mp->b_cont->b_cont;
9270 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9271 	sin = (sin_t *)&sia->sa_addr;
9272 	switch (sin->sin_family) {
9273 	case AF_INET6: {
9274 		sin6_t *sin6 = (sin6_t *)sin;
9275 
9276 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9277 			ipaddr_t v4_addr;
9278 
9279 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9280 			    v4_addr);
9281 			ire = ire_ctable_lookup(v4_addr, 0,
9282 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9283 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9284 		} else {
9285 			in6_addr_t v6addr;
9286 
9287 			v6addr = sin6->sin6_addr;
9288 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9289 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9290 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9291 		}
9292 		break;
9293 	}
9294 	case AF_INET: {
9295 		ipaddr_t v4addr;
9296 
9297 		v4addr = sin->sin_addr.s_addr;
9298 		ire = ire_ctable_lookup(v4addr, 0,
9299 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9300 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9301 		break;
9302 	}
9303 	default:
9304 		return (EAFNOSUPPORT);
9305 	}
9306 	if (ire != NULL) {
9307 		sia->sa_res = 1;
9308 		ire_refrele(ire);
9309 	} else {
9310 		sia->sa_res = 0;
9311 	}
9312 	return (0);
9313 }
9314 
9315 /*
9316  * Check if this is an address assigned on-link i.e. neighbor,
9317  * and makes sure it's reachable from the current zone.
9318  * Returns true for my addresses as well.
9319  * Translates mapped addresses to v4 addresses and then
9320  * treats them as such, returning true if the v4 address
9321  * associated with this mapped address is configured.
9322  * Note: Applications will have to be careful what they do
9323  * with the response; use of mapped addresses limits
9324  * what can be done with the socket, especially with
9325  * respect to socket options and ioctls - neither IPv4
9326  * options nor IPv6 sticky options/ancillary data options
9327  * may be used.
9328  */
9329 /* ARGSUSED */
9330 int
9331 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9332     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9333 {
9334 	struct sioc_addrreq *sia;
9335 	sin_t *sin;
9336 	mblk_t	*mp1;
9337 	ire_t *ire = NULL;
9338 	zoneid_t zoneid;
9339 	ip_stack_t	*ipst;
9340 
9341 	ip1dbg(("ip_sioctl_tonlink"));
9342 
9343 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9344 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9345 	ipst = CONNQ_TO_IPST(q);
9346 
9347 	/* Existence verified in ip_wput_nondata */
9348 	mp1 = mp->b_cont->b_cont;
9349 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9350 	sin = (sin_t *)&sia->sa_addr;
9351 
9352 	/*
9353 	 * Match addresses with a zero gateway field to avoid
9354 	 * routes going through a router.
9355 	 * Exclude broadcast and multicast addresses.
9356 	 */
9357 	switch (sin->sin_family) {
9358 	case AF_INET6: {
9359 		sin6_t *sin6 = (sin6_t *)sin;
9360 
9361 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9362 			ipaddr_t v4_addr;
9363 
9364 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9365 			    v4_addr);
9366 			if (!CLASSD(v4_addr)) {
9367 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9368 				    NULL, NULL, zoneid, NULL,
9369 				    MATCH_IRE_GW, ipst);
9370 			}
9371 		} else {
9372 			in6_addr_t v6addr;
9373 			in6_addr_t v6gw;
9374 
9375 			v6addr = sin6->sin6_addr;
9376 			v6gw = ipv6_all_zeros;
9377 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9378 				ire = ire_route_lookup_v6(&v6addr, 0,
9379 				    &v6gw, 0, NULL, NULL, zoneid,
9380 				    NULL, MATCH_IRE_GW, ipst);
9381 			}
9382 		}
9383 		break;
9384 	}
9385 	case AF_INET: {
9386 		ipaddr_t v4addr;
9387 
9388 		v4addr = sin->sin_addr.s_addr;
9389 		if (!CLASSD(v4addr)) {
9390 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9391 			    NULL, NULL, zoneid, NULL,
9392 			    MATCH_IRE_GW, ipst);
9393 		}
9394 		break;
9395 	}
9396 	default:
9397 		return (EAFNOSUPPORT);
9398 	}
9399 	sia->sa_res = 0;
9400 	if (ire != NULL) {
9401 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9402 		    IRE_LOCAL|IRE_LOOPBACK)) {
9403 			sia->sa_res = 1;
9404 		}
9405 		ire_refrele(ire);
9406 	}
9407 	return (0);
9408 }
9409 
9410 /*
9411  * TBD: implement when kernel maintaines a list of site prefixes.
9412  */
9413 /* ARGSUSED */
9414 int
9415 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9416     ip_ioctl_cmd_t *ipip, void *ifreq)
9417 {
9418 	return (ENXIO);
9419 }
9420 
9421 /* ARGSUSED */
9422 int
9423 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9424     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9425 {
9426 	ill_t  		*ill;
9427 	mblk_t		*mp1;
9428 	conn_t		*connp;
9429 	boolean_t	success;
9430 
9431 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9432 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9433 	/* ioctl comes down on an conn */
9434 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9435 	connp = Q_TO_CONN(q);
9436 
9437 	mp->b_datap->db_type = M_IOCTL;
9438 
9439 	/*
9440 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9441 	 * The original mp contains contaminated b_next values due to 'mi',
9442 	 * which is needed to do the mi_copy_done. Unfortunately if we
9443 	 * send down the original mblk itself and if we are popped due to an
9444 	 * an unplumb before the response comes back from tunnel,
9445 	 * the streamhead (which does a freemsg) will see this contaminated
9446 	 * message and the assertion in freemsg about non-null b_next/b_prev
9447 	 * will panic a DEBUG kernel.
9448 	 */
9449 	mp1 = copymsg(mp);
9450 	if (mp1 == NULL)
9451 		return (ENOMEM);
9452 
9453 	ill = ipif->ipif_ill;
9454 	mutex_enter(&connp->conn_lock);
9455 	mutex_enter(&ill->ill_lock);
9456 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9457 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9458 		    mp, 0);
9459 	} else {
9460 		success = ill_pending_mp_add(ill, connp, mp);
9461 	}
9462 	mutex_exit(&ill->ill_lock);
9463 	mutex_exit(&connp->conn_lock);
9464 
9465 	if (success) {
9466 		ip1dbg(("sending down tunparam request "));
9467 		putnext(ill->ill_wq, mp1);
9468 		return (EINPROGRESS);
9469 	} else {
9470 		/* The conn has started closing */
9471 		freemsg(mp1);
9472 		return (EINTR);
9473 	}
9474 }
9475 
9476 /*
9477  * ARP IOCTLs.
9478  * How does IP get in the business of fronting ARP configuration/queries?
9479  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9480  * are by tradition passed in through a datagram socket.  That lands in IP.
9481  * As it happens, this is just as well since the interface is quite crude in
9482  * that it passes in no information about protocol or hardware types, or
9483  * interface association.  After making the protocol assumption, IP is in
9484  * the position to look up the name of the ILL, which ARP will need, and
9485  * format a request that can be handled by ARP.  The request is passed up
9486  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9487  * back a response.  ARP supports its own set of more general IOCTLs, in
9488  * case anyone is interested.
9489  */
9490 /* ARGSUSED */
9491 int
9492 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9493     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9494 {
9495 	mblk_t *mp1;
9496 	mblk_t *mp2;
9497 	mblk_t *pending_mp;
9498 	ipaddr_t ipaddr;
9499 	area_t *area;
9500 	struct iocblk *iocp;
9501 	conn_t *connp;
9502 	struct arpreq *ar;
9503 	struct xarpreq *xar;
9504 	int flags, alength;
9505 	char *lladdr;
9506 	ip_stack_t	*ipst;
9507 	ill_t *ill = ipif->ipif_ill;
9508 	boolean_t if_arp_ioctl = B_FALSE;
9509 
9510 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9511 	connp = Q_TO_CONN(q);
9512 	ipst = connp->conn_netstack->netstack_ip;
9513 
9514 	if (ipip->ipi_cmd_type == XARP_CMD) {
9515 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9516 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9517 		ar = NULL;
9518 
9519 		flags = xar->xarp_flags;
9520 		lladdr = LLADDR(&xar->xarp_ha);
9521 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9522 		/*
9523 		 * Validate against user's link layer address length
9524 		 * input and name and addr length limits.
9525 		 */
9526 		alength = ill->ill_phys_addr_length;
9527 		if (ipip->ipi_cmd == SIOCSXARP) {
9528 			if (alength != xar->xarp_ha.sdl_alen ||
9529 			    (alength + xar->xarp_ha.sdl_nlen >
9530 			    sizeof (xar->xarp_ha.sdl_data)))
9531 				return (EINVAL);
9532 		}
9533 	} else {
9534 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9535 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9536 		xar = NULL;
9537 
9538 		flags = ar->arp_flags;
9539 		lladdr = ar->arp_ha.sa_data;
9540 		/*
9541 		 * Theoretically, the sa_family could tell us what link
9542 		 * layer type this operation is trying to deal with. By
9543 		 * common usage AF_UNSPEC means ethernet. We'll assume
9544 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9545 		 * for now. Our new SIOC*XARP ioctls can be used more
9546 		 * generally.
9547 		 *
9548 		 * If the underlying media happens to have a non 6 byte
9549 		 * address, arp module will fail set/get, but the del
9550 		 * operation will succeed.
9551 		 */
9552 		alength = 6;
9553 		if ((ipip->ipi_cmd != SIOCDARP) &&
9554 		    (alength != ill->ill_phys_addr_length)) {
9555 			return (EINVAL);
9556 		}
9557 	}
9558 
9559 	/*
9560 	 * We are going to pass up to ARP a packet chain that looks
9561 	 * like:
9562 	 *
9563 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9564 	 *
9565 	 * Get a copy of the original IOCTL mblk to head the chain,
9566 	 * to be sent up (in mp1). Also get another copy to store
9567 	 * in the ill_pending_mp list, for matching the response
9568 	 * when it comes back from ARP.
9569 	 */
9570 	mp1 = copyb(mp);
9571 	pending_mp = copymsg(mp);
9572 	if (mp1 == NULL || pending_mp == NULL) {
9573 		if (mp1 != NULL)
9574 			freeb(mp1);
9575 		if (pending_mp != NULL)
9576 			inet_freemsg(pending_mp);
9577 		return (ENOMEM);
9578 	}
9579 
9580 	ipaddr = sin->sin_addr.s_addr;
9581 
9582 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9583 	    (caddr_t)&ipaddr);
9584 	if (mp2 == NULL) {
9585 		freeb(mp1);
9586 		inet_freemsg(pending_mp);
9587 		return (ENOMEM);
9588 	}
9589 	/* Put together the chain. */
9590 	mp1->b_cont = mp2;
9591 	mp1->b_datap->db_type = M_IOCTL;
9592 	mp2->b_cont = mp;
9593 	mp2->b_datap->db_type = M_DATA;
9594 
9595 	iocp = (struct iocblk *)mp1->b_rptr;
9596 
9597 	/*
9598 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9599 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9600 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9601 	 * ioc_count field; set ioc_count to be correct.
9602 	 */
9603 	iocp->ioc_count = MBLKL(mp1->b_cont);
9604 
9605 	/*
9606 	 * Set the proper command in the ARP message.
9607 	 * Convert the SIOC{G|S|D}ARP calls into our
9608 	 * AR_ENTRY_xxx calls.
9609 	 */
9610 	area = (area_t *)mp2->b_rptr;
9611 	switch (iocp->ioc_cmd) {
9612 	case SIOCDARP:
9613 	case SIOCDXARP:
9614 		/*
9615 		 * We defer deleting the corresponding IRE until
9616 		 * we return from arp.
9617 		 */
9618 		area->area_cmd = AR_ENTRY_DELETE;
9619 		area->area_proto_mask_offset = 0;
9620 		break;
9621 	case SIOCGARP:
9622 	case SIOCGXARP:
9623 		area->area_cmd = AR_ENTRY_SQUERY;
9624 		area->area_proto_mask_offset = 0;
9625 		break;
9626 	case SIOCSARP:
9627 	case SIOCSXARP:
9628 		/*
9629 		 * Delete the corresponding ire to make sure IP will
9630 		 * pick up any change from arp.
9631 		 */
9632 		if (!if_arp_ioctl) {
9633 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9634 		} else {
9635 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9636 			if (ipif != NULL) {
9637 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9638 				    ipst);
9639 				ipif_refrele(ipif);
9640 			}
9641 		}
9642 		break;
9643 	}
9644 	iocp->ioc_cmd = area->area_cmd;
9645 
9646 	/*
9647 	 * Fill in the rest of the ARP operation fields.
9648 	 */
9649 	area->area_hw_addr_length = alength;
9650 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9651 
9652 	/* Translate the flags. */
9653 	if (flags & ATF_PERM)
9654 		area->area_flags |= ACE_F_PERMANENT;
9655 	if (flags & ATF_PUBL)
9656 		area->area_flags |= ACE_F_PUBLISH;
9657 	if (flags & ATF_AUTHORITY)
9658 		area->area_flags |= ACE_F_AUTHORITY;
9659 
9660 	/*
9661 	 * Before sending 'mp' to ARP, we have to clear the b_next
9662 	 * and b_prev. Otherwise if STREAMS encounters such a message
9663 	 * in freemsg(), (because ARP can close any time) it can cause
9664 	 * a panic. But mi code needs the b_next and b_prev values of
9665 	 * mp->b_cont, to complete the ioctl. So we store it here
9666 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9667 	 * when the response comes down from ARP.
9668 	 */
9669 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9670 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9671 	mp->b_cont->b_next = NULL;
9672 	mp->b_cont->b_prev = NULL;
9673 
9674 	mutex_enter(&connp->conn_lock);
9675 	mutex_enter(&ill->ill_lock);
9676 	/* conn has not yet started closing, hence this can't fail */
9677 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9678 	mutex_exit(&ill->ill_lock);
9679 	mutex_exit(&connp->conn_lock);
9680 
9681 	/*
9682 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9683 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9684 	 */
9685 	putnext(ill->ill_rq, mp1);
9686 	return (EINPROGRESS);
9687 }
9688 
9689 /*
9690  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9691  * the associated sin and refhold and return the associated ipif via `ci'.
9692  */
9693 int
9694 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9695     cmd_info_t *ci, ipsq_func_t func)
9696 {
9697 	mblk_t	*mp1;
9698 	int	err;
9699 	sin_t	*sin;
9700 	conn_t	*connp;
9701 	ipif_t	*ipif;
9702 	ire_t	*ire = NULL;
9703 	ill_t	*ill = NULL;
9704 	boolean_t exists;
9705 	ip_stack_t *ipst;
9706 	struct arpreq *ar;
9707 	struct xarpreq *xar;
9708 	struct sockaddr_dl *sdl;
9709 
9710 	/* ioctl comes down on a conn */
9711 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9712 	connp = Q_TO_CONN(q);
9713 	if (connp->conn_af_isv6)
9714 		return (ENXIO);
9715 
9716 	ipst = connp->conn_netstack->netstack_ip;
9717 
9718 	/* Verified in ip_wput_nondata */
9719 	mp1 = mp->b_cont->b_cont;
9720 
9721 	if (ipip->ipi_cmd_type == XARP_CMD) {
9722 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9723 		xar = (struct xarpreq *)mp1->b_rptr;
9724 		sin = (sin_t *)&xar->xarp_pa;
9725 		sdl = &xar->xarp_ha;
9726 
9727 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9728 			return (ENXIO);
9729 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9730 			return (EINVAL);
9731 	} else {
9732 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9733 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9734 		ar = (struct arpreq *)mp1->b_rptr;
9735 		sin = (sin_t *)&ar->arp_pa;
9736 	}
9737 
9738 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9739 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9740 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9741 		    mp, func, &err, ipst);
9742 		if (ipif == NULL)
9743 			return (err);
9744 		if (ipif->ipif_id != 0 ||
9745 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9746 			ipif_refrele(ipif);
9747 			return (ENXIO);
9748 		}
9749 	} else {
9750 		/*
9751 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9752 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9753 		 * case, a simple forwarding table lookup will return the
9754 		 * IRE_IF_RESOLVER for the first interface in the group, which
9755 		 * might not be the interface on which the requested IP
9756 		 * address was resolved due to the ill selection algorithm
9757 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9758 		 * lookup first: if the IRE cache entry for the IP address is
9759 		 * still there, it will contain the ill pointer for the right
9760 		 * interface, so we use that. If the cache entry has been
9761 		 * flushed, we fall back to the forwarding table lookup. This
9762 		 * should be rare enough since IRE cache entries have a longer
9763 		 * life expectancy than ARP cache entries.
9764 		 */
9765 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9766 		    ipst);
9767 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9768 		    ((ill = ire_to_ill(ire)) == NULL) ||
9769 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9770 			if (ire != NULL)
9771 				ire_refrele(ire);
9772 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9773 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9774 			    NULL, MATCH_IRE_TYPE, ipst);
9775 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9776 
9777 				if (ire != NULL)
9778 					ire_refrele(ire);
9779 				return (ENXIO);
9780 			}
9781 		}
9782 		ASSERT(ire != NULL && ill != NULL);
9783 		ipif = ill->ill_ipif;
9784 		ipif_refhold(ipif);
9785 		ire_refrele(ire);
9786 	}
9787 	ci->ci_sin = sin;
9788 	ci->ci_ipif = ipif;
9789 	return (0);
9790 }
9791 
9792 /*
9793  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9794  * atomically set/clear the muxids. Also complete the ioctl by acking or
9795  * naking it.  Note that the code is structured such that the link type,
9796  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9797  * its clones use the persistent link, while pppd(1M) and perhaps many
9798  * other daemons may use non-persistent link.  When combined with some
9799  * ill_t states, linking and unlinking lower streams may be used as
9800  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9801  */
9802 /* ARGSUSED */
9803 void
9804 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9805 {
9806 	mblk_t		*mp1, *mp2;
9807 	struct linkblk	*li;
9808 	struct ipmx_s	*ipmxp;
9809 	ill_t		*ill;
9810 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9811 	int		err = 0;
9812 	boolean_t	entered_ipsq = B_FALSE;
9813 	boolean_t	islink;
9814 	ip_stack_t	*ipst;
9815 
9816 	if (CONN_Q(q))
9817 		ipst = CONNQ_TO_IPST(q);
9818 	else
9819 		ipst = ILLQ_TO_IPST(q);
9820 
9821 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9822 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9823 
9824 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9825 
9826 	mp1 = mp->b_cont;	/* This is the linkblk info */
9827 	li = (struct linkblk *)mp1->b_rptr;
9828 
9829 	/*
9830 	 * ARP has added this special mblk, and the utility is asking us
9831 	 * to perform consistency checks, and also atomically set the
9832 	 * muxid. Ifconfig is an example.  It achieves this by using
9833 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9834 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9835 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9836 	 * and other comments in this routine for more details.
9837 	 */
9838 	mp2 = mp1->b_cont;	/* This is added by ARP */
9839 
9840 	/*
9841 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9842 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9843 	 * get the special mblk above.  For backward compatibility, we
9844 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9845 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9846 	 * not atomic, and can leave the streams unplumbable if the utility
9847 	 * is interrupted before it does the SIOCSLIFMUXID.
9848 	 */
9849 	if (mp2 == NULL) {
9850 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9851 		if (err == EINPROGRESS)
9852 			return;
9853 		goto done;
9854 	}
9855 
9856 	/*
9857 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9858 	 * ARP has appended this last mblk to tell us whether the lower stream
9859 	 * is an arp-dev stream or an IP module stream.
9860 	 */
9861 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9862 	if (ipmxp->ipmx_arpdev_stream) {
9863 		/*
9864 		 * The lower stream is the arp-dev stream.
9865 		 */
9866 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9867 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9868 		if (ill == NULL) {
9869 			if (err == EINPROGRESS)
9870 				return;
9871 			err = EINVAL;
9872 			goto done;
9873 		}
9874 
9875 		if (ipsq == NULL) {
9876 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9877 			    NEW_OP, B_TRUE);
9878 			if (ipsq == NULL) {
9879 				ill_refrele(ill);
9880 				return;
9881 			}
9882 			entered_ipsq = B_TRUE;
9883 		}
9884 		ASSERT(IAM_WRITER_ILL(ill));
9885 		ill_refrele(ill);
9886 
9887 		/*
9888 		 * To ensure consistency between IP and ARP, the following
9889 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9890 		 * This is because the muxid's are stored in the IP stream on
9891 		 * the ill.
9892 		 *
9893 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9894 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9895 		 * not yet plinked, and it also checks that the corresponding
9896 		 * IP stream is already plinked.
9897 		 *
9898 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9899 		 * punlinking the IP stream. IP does not allow punlink of the
9900 		 * IP stream unless the arp stream has been punlinked.
9901 		 */
9902 		if ((islink &&
9903 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9904 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9905 			err = EINVAL;
9906 			goto done;
9907 		}
9908 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9909 	} else {
9910 		/*
9911 		 * The lower stream is probably an IP module stream.  Do
9912 		 * consistency checking.
9913 		 */
9914 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9915 		if (err == EINPROGRESS)
9916 			return;
9917 	}
9918 done:
9919 	if (err == 0)
9920 		miocack(q, mp, 0, 0);
9921 	else
9922 		miocnak(q, mp, 0, err);
9923 
9924 	/* Conn was refheld in ip_sioctl_copyin_setup */
9925 	if (CONN_Q(q))
9926 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9927 	if (entered_ipsq)
9928 		ipsq_exit(ipsq);
9929 }
9930 
9931 /*
9932  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9933  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9934  * module stream).  If `doconsist' is set, then do the extended consistency
9935  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9936  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9937  * an error code on failure.
9938  */
9939 static int
9940 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9941     struct linkblk *li, boolean_t doconsist)
9942 {
9943 	ill_t  		*ill;
9944 	queue_t		*ipwq, *dwq;
9945 	const char	*name;
9946 	struct qinit	*qinfo;
9947 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9948 	boolean_t	entered_ipsq = B_FALSE;
9949 
9950 	/*
9951 	 * Walk the lower stream to verify it's the IP module stream.
9952 	 * The IP module is identified by its name, wput function,
9953 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9954 	 * (li->l_qbot) will not vanish until this ioctl completes.
9955 	 */
9956 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9957 		qinfo = ipwq->q_qinfo;
9958 		name = qinfo->qi_minfo->mi_idname;
9959 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9960 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9961 			break;
9962 		}
9963 	}
9964 
9965 	/*
9966 	 * If this isn't an IP module stream, bail.
9967 	 */
9968 	if (ipwq == NULL)
9969 		return (0);
9970 
9971 	ill = ipwq->q_ptr;
9972 	ASSERT(ill != NULL);
9973 
9974 	if (ipsq == NULL) {
9975 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9976 		    NEW_OP, B_TRUE);
9977 		if (ipsq == NULL)
9978 			return (EINPROGRESS);
9979 		entered_ipsq = B_TRUE;
9980 	}
9981 	ASSERT(IAM_WRITER_ILL(ill));
9982 
9983 	if (doconsist) {
9984 		/*
9985 		 * Consistency checking requires that I_{P}LINK occurs
9986 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9987 		 * occurs prior to clearing ill_arp_muxid.
9988 		 */
9989 		if ((islink && ill->ill_ip_muxid != 0) ||
9990 		    (!islink && ill->ill_arp_muxid != 0)) {
9991 			if (entered_ipsq)
9992 				ipsq_exit(ipsq);
9993 			return (EINVAL);
9994 		}
9995 	}
9996 
9997 	/*
9998 	 * As part of I_{P}LINKing, stash the number of downstream modules and
9999 	 * the read queue of the module immediately below IP in the ill.
10000 	 * These are used during the capability negotiation below.
10001 	 */
10002 	ill->ill_lmod_rq = NULL;
10003 	ill->ill_lmod_cnt = 0;
10004 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10005 		ill->ill_lmod_rq = RD(dwq);
10006 		for (; dwq != NULL; dwq = dwq->q_next)
10007 			ill->ill_lmod_cnt++;
10008 	}
10009 
10010 	if (doconsist)
10011 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10012 
10013 	/*
10014 	 * If there's at least one up ipif on this ill, then we're bound to
10015 	 * the underlying driver via DLPI.  In that case, renegotiate
10016 	 * capabilities to account for any possible change in modules
10017 	 * interposed between IP and the driver.
10018 	 */
10019 	if (ill->ill_ipif_up_count > 0) {
10020 		if (islink)
10021 			ill_capability_probe(ill);
10022 		else
10023 			ill_capability_reset(ill);
10024 	}
10025 
10026 	if (entered_ipsq)
10027 		ipsq_exit(ipsq);
10028 
10029 	return (0);
10030 }
10031 
10032 /*
10033  * Search the ioctl command in the ioctl tables and return a pointer
10034  * to the ioctl command information. The ioctl command tables are
10035  * static and fully populated at compile time.
10036  */
10037 ip_ioctl_cmd_t *
10038 ip_sioctl_lookup(int ioc_cmd)
10039 {
10040 	int index;
10041 	ip_ioctl_cmd_t *ipip;
10042 	ip_ioctl_cmd_t *ipip_end;
10043 
10044 	if (ioc_cmd == IPI_DONTCARE)
10045 		return (NULL);
10046 
10047 	/*
10048 	 * Do a 2 step search. First search the indexed table
10049 	 * based on the least significant byte of the ioctl cmd.
10050 	 * If we don't find a match, then search the misc table
10051 	 * serially.
10052 	 */
10053 	index = ioc_cmd & 0xFF;
10054 	if (index < ip_ndx_ioctl_count) {
10055 		ipip = &ip_ndx_ioctl_table[index];
10056 		if (ipip->ipi_cmd == ioc_cmd) {
10057 			/* Found a match in the ndx table */
10058 			return (ipip);
10059 		}
10060 	}
10061 
10062 	/* Search the misc table */
10063 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10064 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10065 		if (ipip->ipi_cmd == ioc_cmd)
10066 			/* Found a match in the misc table */
10067 			return (ipip);
10068 	}
10069 
10070 	return (NULL);
10071 }
10072 
10073 /*
10074  * Wrapper function for resuming deferred ioctl processing
10075  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10076  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10077  */
10078 /* ARGSUSED */
10079 void
10080 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10081     void *dummy_arg)
10082 {
10083 	ip_sioctl_copyin_setup(q, mp);
10084 }
10085 
10086 /*
10087  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10088  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10089  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10090  * We establish here the size of the block to be copied in.  mi_copyin
10091  * arranges for this to happen, an processing continues in ip_wput with
10092  * an M_IOCDATA message.
10093  */
10094 void
10095 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10096 {
10097 	int	copyin_size;
10098 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10099 	ip_ioctl_cmd_t *ipip;
10100 	cred_t *cr;
10101 	ip_stack_t	*ipst;
10102 
10103 	if (CONN_Q(q))
10104 		ipst = CONNQ_TO_IPST(q);
10105 	else
10106 		ipst = ILLQ_TO_IPST(q);
10107 
10108 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10109 	if (ipip == NULL) {
10110 		/*
10111 		 * The ioctl is not one we understand or own.
10112 		 * Pass it along to be processed down stream,
10113 		 * if this is a module instance of IP, else nak
10114 		 * the ioctl.
10115 		 */
10116 		if (q->q_next == NULL) {
10117 			goto nak;
10118 		} else {
10119 			putnext(q, mp);
10120 			return;
10121 		}
10122 	}
10123 
10124 	/*
10125 	 * If this is deferred, then we will do all the checks when we
10126 	 * come back.
10127 	 */
10128 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10129 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10130 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10131 		return;
10132 	}
10133 
10134 	/*
10135 	 * Only allow a very small subset of IP ioctls on this stream if
10136 	 * IP is a module and not a driver. Allowing ioctls to be processed
10137 	 * in this case may cause assert failures or data corruption.
10138 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10139 	 * ioctls allowed on an IP module stream, after which this stream
10140 	 * normally becomes a multiplexor (at which time the stream head
10141 	 * will fail all ioctls).
10142 	 */
10143 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10144 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10145 			/*
10146 			 * Pass common Streams ioctls which the IP
10147 			 * module does not own or consume along to
10148 			 * be processed down stream.
10149 			 */
10150 			putnext(q, mp);
10151 			return;
10152 		} else {
10153 			goto nak;
10154 		}
10155 	}
10156 
10157 	/* Make sure we have ioctl data to process. */
10158 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10159 		goto nak;
10160 
10161 	/*
10162 	 * Prefer dblk credential over ioctl credential; some synthesized
10163 	 * ioctls have kcred set because there's no way to crhold()
10164 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10165 	 * the framework; the caller of ioctl needs to hold the reference
10166 	 * for the duration of the call).
10167 	 */
10168 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10169 
10170 	/* Make sure normal users don't send down privileged ioctls */
10171 	if ((ipip->ipi_flags & IPI_PRIV) &&
10172 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10173 		/* We checked the privilege earlier but log it here */
10174 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10175 		return;
10176 	}
10177 
10178 	/*
10179 	 * The ioctl command tables can only encode fixed length
10180 	 * ioctl data. If the length is variable, the table will
10181 	 * encode the length as zero. Such special cases are handled
10182 	 * below in the switch.
10183 	 */
10184 	if (ipip->ipi_copyin_size != 0) {
10185 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10186 		return;
10187 	}
10188 
10189 	switch (iocp->ioc_cmd) {
10190 	case O_SIOCGIFCONF:
10191 	case SIOCGIFCONF:
10192 		/*
10193 		 * This IOCTL is hilarious.  See comments in
10194 		 * ip_sioctl_get_ifconf for the story.
10195 		 */
10196 		if (iocp->ioc_count == TRANSPARENT)
10197 			copyin_size = SIZEOF_STRUCT(ifconf,
10198 			    iocp->ioc_flag);
10199 		else
10200 			copyin_size = iocp->ioc_count;
10201 		mi_copyin(q, mp, NULL, copyin_size);
10202 		return;
10203 
10204 	case O_SIOCGLIFCONF:
10205 	case SIOCGLIFCONF:
10206 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10207 		mi_copyin(q, mp, NULL, copyin_size);
10208 		return;
10209 
10210 	case SIOCGLIFSRCOF:
10211 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10212 		mi_copyin(q, mp, NULL, copyin_size);
10213 		return;
10214 	case SIOCGIP6ADDRPOLICY:
10215 		ip_sioctl_ip6addrpolicy(q, mp);
10216 		ip6_asp_table_refrele(ipst);
10217 		return;
10218 
10219 	case SIOCSIP6ADDRPOLICY:
10220 		ip_sioctl_ip6addrpolicy(q, mp);
10221 		return;
10222 
10223 	case SIOCGDSTINFO:
10224 		ip_sioctl_dstinfo(q, mp);
10225 		ip6_asp_table_refrele(ipst);
10226 		return;
10227 
10228 	case I_PLINK:
10229 	case I_PUNLINK:
10230 	case I_LINK:
10231 	case I_UNLINK:
10232 		/*
10233 		 * We treat non-persistent link similarly as the persistent
10234 		 * link case, in terms of plumbing/unplumbing, as well as
10235 		 * dynamic re-plumbing events indicator.  See comments
10236 		 * in ip_sioctl_plink() for more.
10237 		 *
10238 		 * Request can be enqueued in the 'ipsq' while waiting
10239 		 * to become exclusive. So bump up the conn ref.
10240 		 */
10241 		if (CONN_Q(q))
10242 			CONN_INC_REF(Q_TO_CONN(q));
10243 		ip_sioctl_plink(NULL, q, mp, NULL);
10244 		return;
10245 
10246 	case ND_GET:
10247 	case ND_SET:
10248 		/*
10249 		 * Use of the nd table requires holding the reader lock.
10250 		 * Modifying the nd table thru nd_load/nd_unload requires
10251 		 * the writer lock.
10252 		 */
10253 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10254 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10255 			rw_exit(&ipst->ips_ip_g_nd_lock);
10256 
10257 			if (iocp->ioc_error)
10258 				iocp->ioc_count = 0;
10259 			mp->b_datap->db_type = M_IOCACK;
10260 			qreply(q, mp);
10261 			return;
10262 		}
10263 		rw_exit(&ipst->ips_ip_g_nd_lock);
10264 		/*
10265 		 * We don't understand this subioctl of ND_GET / ND_SET.
10266 		 * Maybe intended for some driver / module below us
10267 		 */
10268 		if (q->q_next) {
10269 			putnext(q, mp);
10270 		} else {
10271 			iocp->ioc_error = ENOENT;
10272 			mp->b_datap->db_type = M_IOCNAK;
10273 			iocp->ioc_count = 0;
10274 			qreply(q, mp);
10275 		}
10276 		return;
10277 
10278 	case IP_IOCTL:
10279 		ip_wput_ioctl(q, mp);
10280 		return;
10281 	default:
10282 		cmn_err(CE_PANIC, "should not happen ");
10283 	}
10284 nak:
10285 	if (mp->b_cont != NULL) {
10286 		freemsg(mp->b_cont);
10287 		mp->b_cont = NULL;
10288 	}
10289 	iocp->ioc_error = EINVAL;
10290 	mp->b_datap->db_type = M_IOCNAK;
10291 	iocp->ioc_count = 0;
10292 	qreply(q, mp);
10293 }
10294 
10295 /* ip_wput hands off ARP IOCTL responses to us */
10296 void
10297 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10298 {
10299 	struct arpreq *ar;
10300 	struct xarpreq *xar;
10301 	area_t	*area;
10302 	mblk_t	*area_mp;
10303 	struct iocblk *iocp;
10304 	mblk_t	*orig_ioc_mp, *tmp;
10305 	struct iocblk	*orig_iocp;
10306 	ill_t *ill;
10307 	conn_t *connp = NULL;
10308 	uint_t ioc_id;
10309 	mblk_t *pending_mp;
10310 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10311 	int *flagsp;
10312 	char *storage = NULL;
10313 	sin_t *sin;
10314 	ipaddr_t addr;
10315 	int err;
10316 	ip_stack_t *ipst;
10317 
10318 	ill = q->q_ptr;
10319 	ASSERT(ill != NULL);
10320 	ipst = ill->ill_ipst;
10321 
10322 	/*
10323 	 * We should get back from ARP a packet chain that looks like:
10324 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10325 	 */
10326 	if (!(area_mp = mp->b_cont) ||
10327 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10328 	    !(orig_ioc_mp = area_mp->b_cont) ||
10329 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10330 		freemsg(mp);
10331 		return;
10332 	}
10333 
10334 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10335 
10336 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10337 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10338 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10339 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10340 		x_arp_ioctl = B_TRUE;
10341 		xar = (struct xarpreq *)tmp->b_rptr;
10342 		sin = (sin_t *)&xar->xarp_pa;
10343 		flagsp = &xar->xarp_flags;
10344 		storage = xar->xarp_ha.sdl_data;
10345 		if (xar->xarp_ha.sdl_nlen != 0)
10346 			ifx_arp_ioctl = B_TRUE;
10347 	} else {
10348 		ar = (struct arpreq *)tmp->b_rptr;
10349 		sin = (sin_t *)&ar->arp_pa;
10350 		flagsp = &ar->arp_flags;
10351 		storage = ar->arp_ha.sa_data;
10352 	}
10353 
10354 	iocp = (struct iocblk *)mp->b_rptr;
10355 
10356 	/*
10357 	 * Pick out the originating queue based on the ioc_id.
10358 	 */
10359 	ioc_id = iocp->ioc_id;
10360 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10361 	if (pending_mp == NULL) {
10362 		ASSERT(connp == NULL);
10363 		inet_freemsg(mp);
10364 		return;
10365 	}
10366 	ASSERT(connp != NULL);
10367 	q = CONNP_TO_WQ(connp);
10368 
10369 	/* Uncouple the internally generated IOCTL from the original one */
10370 	area = (area_t *)area_mp->b_rptr;
10371 	area_mp->b_cont = NULL;
10372 
10373 	/*
10374 	 * Restore the b_next and b_prev used by mi code. This is needed
10375 	 * to complete the ioctl using mi* functions. We stored them in
10376 	 * the pending mp prior to sending the request to ARP.
10377 	 */
10378 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10379 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10380 	inet_freemsg(pending_mp);
10381 
10382 	/*
10383 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10384 	 * Catch the case where there is an IRE_CACHE by no entry in the
10385 	 * arp table.
10386 	 */
10387 	addr = sin->sin_addr.s_addr;
10388 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10389 		ire_t			*ire;
10390 		dl_unitdata_req_t	*dlup;
10391 		mblk_t			*llmp;
10392 		int			addr_len;
10393 		ill_t			*ipsqill = NULL;
10394 
10395 		if (ifx_arp_ioctl) {
10396 			/*
10397 			 * There's no need to lookup the ill, since
10398 			 * we've already done that when we started
10399 			 * processing the ioctl and sent the message
10400 			 * to ARP on that ill.  So use the ill that
10401 			 * is stored in q->q_ptr.
10402 			 */
10403 			ipsqill = ill;
10404 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10405 			    ipsqill->ill_ipif, ALL_ZONES,
10406 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10407 		} else {
10408 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10409 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10410 			if (ire != NULL)
10411 				ipsqill = ire_to_ill(ire);
10412 		}
10413 
10414 		if ((x_arp_ioctl) && (ipsqill != NULL))
10415 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10416 
10417 		if (ire != NULL) {
10418 			/*
10419 			 * Since the ire obtained from cachetable is used for
10420 			 * mac addr copying below, treat an incomplete ire as if
10421 			 * as if we never found it.
10422 			 */
10423 			if (ire->ire_nce != NULL &&
10424 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10425 				ire_refrele(ire);
10426 				ire = NULL;
10427 				ipsqill = NULL;
10428 				goto errack;
10429 			}
10430 			*flagsp = ATF_INUSE;
10431 			llmp = (ire->ire_nce != NULL ?
10432 			    ire->ire_nce->nce_res_mp : NULL);
10433 			if (llmp != NULL && ipsqill != NULL) {
10434 				uchar_t *macaddr;
10435 
10436 				addr_len = ipsqill->ill_phys_addr_length;
10437 				if (x_arp_ioctl && ((addr_len +
10438 				    ipsqill->ill_name_length) >
10439 				    sizeof (xar->xarp_ha.sdl_data))) {
10440 					ire_refrele(ire);
10441 					freemsg(mp);
10442 					ip_ioctl_finish(q, orig_ioc_mp,
10443 					    EINVAL, NO_COPYOUT, NULL);
10444 					return;
10445 				}
10446 				*flagsp |= ATF_COM;
10447 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10448 				if (ipsqill->ill_sap_length < 0)
10449 					macaddr = llmp->b_rptr +
10450 					    dlup->dl_dest_addr_offset;
10451 				else
10452 					macaddr = llmp->b_rptr +
10453 					    dlup->dl_dest_addr_offset +
10454 					    ipsqill->ill_sap_length;
10455 				/*
10456 				 * For SIOCGARP, MAC address length
10457 				 * validation has already been done
10458 				 * before the ioctl was issued to ARP to
10459 				 * allow it to progress only on 6 byte
10460 				 * addressable (ethernet like) media. Thus
10461 				 * the mac address copying can not overwrite
10462 				 * the sa_data area below.
10463 				 */
10464 				bcopy(macaddr, storage, addr_len);
10465 			}
10466 			/* Ditch the internal IOCTL. */
10467 			freemsg(mp);
10468 			ire_refrele(ire);
10469 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10470 			return;
10471 		}
10472 	}
10473 
10474 	/*
10475 	 * Delete the coresponding IRE_CACHE if any.
10476 	 * Reset the error if there was one (in case there was no entry
10477 	 * in arp.)
10478 	 */
10479 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10480 		ipif_t *ipintf = NULL;
10481 
10482 		if (ifx_arp_ioctl) {
10483 			/*
10484 			 * There's no need to lookup the ill, since
10485 			 * we've already done that when we started
10486 			 * processing the ioctl and sent the message
10487 			 * to ARP on that ill.  So use the ill that
10488 			 * is stored in q->q_ptr.
10489 			 */
10490 			ipintf = ill->ill_ipif;
10491 		}
10492 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10493 			/*
10494 			 * The address in "addr" may be an entry for a
10495 			 * router. If that's true, then any off-net
10496 			 * IRE_CACHE entries that go through the router
10497 			 * with address "addr" must be clobbered. Use
10498 			 * ire_walk to achieve this goal.
10499 			 */
10500 			if (ifx_arp_ioctl)
10501 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10502 				    ire_delete_cache_gw, (char *)&addr, ill);
10503 			else
10504 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10505 				    ALL_ZONES, ipst);
10506 			iocp->ioc_error = 0;
10507 		}
10508 	}
10509 errack:
10510 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10511 		err = iocp->ioc_error;
10512 		freemsg(mp);
10513 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10514 		return;
10515 	}
10516 
10517 	/*
10518 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10519 	 * the area_t into the struct {x}arpreq.
10520 	 */
10521 	if (x_arp_ioctl) {
10522 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10523 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10524 		    sizeof (xar->xarp_ha.sdl_data)) {
10525 			freemsg(mp);
10526 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10527 			    NULL);
10528 			return;
10529 		}
10530 	}
10531 	*flagsp = ATF_INUSE;
10532 	if (area->area_flags & ACE_F_PERMANENT)
10533 		*flagsp |= ATF_PERM;
10534 	if (area->area_flags & ACE_F_PUBLISH)
10535 		*flagsp |= ATF_PUBL;
10536 	if (area->area_flags & ACE_F_AUTHORITY)
10537 		*flagsp |= ATF_AUTHORITY;
10538 	if (area->area_hw_addr_length != 0) {
10539 		*flagsp |= ATF_COM;
10540 		/*
10541 		 * For SIOCGARP, MAC address length validation has
10542 		 * already been done before the ioctl was issued to ARP
10543 		 * to allow it to progress only on 6 byte addressable
10544 		 * (ethernet like) media. Thus the mac address copying
10545 		 * can not overwrite the sa_data area below.
10546 		 */
10547 		bcopy((char *)area + area->area_hw_addr_offset,
10548 		    storage, area->area_hw_addr_length);
10549 	}
10550 
10551 	/* Ditch the internal IOCTL. */
10552 	freemsg(mp);
10553 	/* Complete the original. */
10554 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10555 }
10556 
10557 /*
10558  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10559  * interface) create the next available logical interface for this
10560  * physical interface.
10561  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10562  * ipif with the specified name.
10563  *
10564  * If the address family is not AF_UNSPEC then set the address as well.
10565  *
10566  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10567  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10568  *
10569  * Executed as a writer on the ill or ill group.
10570  * So no lock is needed to traverse the ipif chain, or examine the
10571  * phyint flags.
10572  */
10573 /* ARGSUSED */
10574 int
10575 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10576     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10577 {
10578 	mblk_t	*mp1;
10579 	struct lifreq *lifr;
10580 	boolean_t	isv6;
10581 	boolean_t	exists;
10582 	char 	*name;
10583 	char	*endp;
10584 	char	*cp;
10585 	int	namelen;
10586 	ipif_t	*ipif;
10587 	long	id;
10588 	ipsq_t	*ipsq;
10589 	ill_t	*ill;
10590 	sin_t	*sin;
10591 	int	err = 0;
10592 	boolean_t found_sep = B_FALSE;
10593 	conn_t	*connp;
10594 	zoneid_t zoneid;
10595 	int	orig_ifindex = 0;
10596 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10597 
10598 	ASSERT(q->q_next == NULL);
10599 	ip1dbg(("ip_sioctl_addif\n"));
10600 	/* Existence of mp1 has been checked in ip_wput_nondata */
10601 	mp1 = mp->b_cont->b_cont;
10602 	/*
10603 	 * Null terminate the string to protect against buffer
10604 	 * overrun. String was generated by user code and may not
10605 	 * be trusted.
10606 	 */
10607 	lifr = (struct lifreq *)mp1->b_rptr;
10608 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10609 	name = lifr->lifr_name;
10610 	ASSERT(CONN_Q(q));
10611 	connp = Q_TO_CONN(q);
10612 	isv6 = connp->conn_af_isv6;
10613 	zoneid = connp->conn_zoneid;
10614 	namelen = mi_strlen(name);
10615 	if (namelen == 0)
10616 		return (EINVAL);
10617 
10618 	exists = B_FALSE;
10619 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10620 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10621 		/*
10622 		 * Allow creating lo0 using SIOCLIFADDIF.
10623 		 * can't be any other writer thread. So can pass null below
10624 		 * for the last 4 args to ipif_lookup_name.
10625 		 */
10626 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10627 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10628 		/* Prevent any further action */
10629 		if (ipif == NULL) {
10630 			return (ENOBUFS);
10631 		} else if (!exists) {
10632 			/* We created the ipif now and as writer */
10633 			ipif_refrele(ipif);
10634 			return (0);
10635 		} else {
10636 			ill = ipif->ipif_ill;
10637 			ill_refhold(ill);
10638 			ipif_refrele(ipif);
10639 		}
10640 	} else {
10641 		/* Look for a colon in the name. */
10642 		endp = &name[namelen];
10643 		for (cp = endp; --cp > name; ) {
10644 			if (*cp == IPIF_SEPARATOR_CHAR) {
10645 				found_sep = B_TRUE;
10646 				/*
10647 				 * Reject any non-decimal aliases for plumbing
10648 				 * of logical interfaces. Aliases with leading
10649 				 * zeroes are also rejected as they introduce
10650 				 * ambiguity in the naming of the interfaces.
10651 				 * Comparing with "0" takes care of all such
10652 				 * cases.
10653 				 */
10654 				if ((strncmp("0", cp+1, 1)) == 0)
10655 					return (EINVAL);
10656 
10657 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10658 				    id <= 0 || *endp != '\0') {
10659 					return (EINVAL);
10660 				}
10661 				*cp = '\0';
10662 				break;
10663 			}
10664 		}
10665 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10666 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10667 		if (found_sep)
10668 			*cp = IPIF_SEPARATOR_CHAR;
10669 		if (ill == NULL)
10670 			return (err);
10671 	}
10672 
10673 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10674 	    B_TRUE);
10675 
10676 	/*
10677 	 * Release the refhold due to the lookup, now that we are excl
10678 	 * or we are just returning
10679 	 */
10680 	ill_refrele(ill);
10681 
10682 	if (ipsq == NULL)
10683 		return (EINPROGRESS);
10684 
10685 	/*
10686 	 * If the interface is failed, inactive or offlined, look for a working
10687 	 * interface in the ill group and create the ipif there. If we can't
10688 	 * find a good interface, create the ipif anyway so that in.mpathd can
10689 	 * move it to the first repaired interface.
10690 	 */
10691 	if ((ill->ill_phyint->phyint_flags &
10692 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10693 	    ill->ill_phyint->phyint_groupname_len != 0) {
10694 		phyint_t *phyi;
10695 		char *groupname = ill->ill_phyint->phyint_groupname;
10696 
10697 		/*
10698 		 * We're looking for a working interface, but it doesn't matter
10699 		 * if it's up or down; so instead of following the group lists,
10700 		 * we look at each physical interface and compare the groupname.
10701 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10702 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10703 		 * Otherwise we create the ipif on the failed interface.
10704 		 */
10705 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10706 		phyi = avl_first(&ipst->ips_phyint_g_list->
10707 		    phyint_list_avl_by_index);
10708 		for (; phyi != NULL;
10709 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10710 		    phyint_list_avl_by_index,
10711 		    phyi, AVL_AFTER)) {
10712 			if (phyi->phyint_groupname_len == 0)
10713 				continue;
10714 			ASSERT(phyi->phyint_groupname != NULL);
10715 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10716 			    !(phyi->phyint_flags &
10717 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10718 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10719 			    (phyi->phyint_illv4 != NULL))) {
10720 				break;
10721 			}
10722 		}
10723 		rw_exit(&ipst->ips_ill_g_lock);
10724 
10725 		if (phyi != NULL) {
10726 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10727 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10728 			    phyi->phyint_illv4);
10729 		}
10730 	}
10731 
10732 	/*
10733 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10734 	 * before or after us.
10735 	 */
10736 	ASSERT(IAM_WRITER_ILL(ill));
10737 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10738 
10739 	if (found_sep && orig_ifindex == 0) {
10740 		/* Now see if there is an IPIF with this unit number. */
10741 		for (ipif = ill->ill_ipif; ipif != NULL;
10742 		    ipif = ipif->ipif_next) {
10743 			if (ipif->ipif_id == id) {
10744 				err = EEXIST;
10745 				goto done;
10746 			}
10747 		}
10748 	}
10749 
10750 	/*
10751 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10752 	 * of lo0. We never come here when we plumb lo0:0. It
10753 	 * happens in ipif_lookup_on_name.
10754 	 * The specified unit number is ignored when we create the ipif on a
10755 	 * different interface. However, we save it in ipif_orig_ipifid below so
10756 	 * that the ipif fails back to the right position.
10757 	 */
10758 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10759 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10760 		err = ENOBUFS;
10761 		goto done;
10762 	}
10763 
10764 	/* Return created name with ioctl */
10765 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10766 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10767 	ip1dbg(("created %s\n", lifr->lifr_name));
10768 
10769 	/* Set address */
10770 	sin = (sin_t *)&lifr->lifr_addr;
10771 	if (sin->sin_family != AF_UNSPEC) {
10772 		err = ip_sioctl_addr(ipif, sin, q, mp,
10773 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10774 	}
10775 
10776 	/* Set ifindex and unit number for failback */
10777 	if (err == 0 && orig_ifindex != 0) {
10778 		ipif->ipif_orig_ifindex = orig_ifindex;
10779 		if (found_sep) {
10780 			ipif->ipif_orig_ipifid = id;
10781 		}
10782 	}
10783 
10784 done:
10785 	ipsq_exit(ipsq);
10786 	return (err);
10787 }
10788 
10789 /*
10790  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10791  * interface) delete it based on the IP address (on this physical interface).
10792  * Otherwise delete it based on the ipif_id.
10793  * Also, special handling to allow a removeif of lo0.
10794  */
10795 /* ARGSUSED */
10796 int
10797 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10798     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10799 {
10800 	conn_t		*connp;
10801 	ill_t		*ill = ipif->ipif_ill;
10802 	boolean_t	 success;
10803 	ip_stack_t	*ipst;
10804 
10805 	ipst = CONNQ_TO_IPST(q);
10806 
10807 	ASSERT(q->q_next == NULL);
10808 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10809 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10810 	ASSERT(IAM_WRITER_IPIF(ipif));
10811 
10812 	connp = Q_TO_CONN(q);
10813 	/*
10814 	 * Special case for unplumbing lo0 (the loopback physical interface).
10815 	 * If unplumbing lo0, the incoming address structure has been
10816 	 * initialized to all zeros. When unplumbing lo0, all its logical
10817 	 * interfaces must be removed too.
10818 	 *
10819 	 * Note that this interface may be called to remove a specific
10820 	 * loopback logical interface (eg, lo0:1). But in that case
10821 	 * ipif->ipif_id != 0 so that the code path for that case is the
10822 	 * same as any other interface (meaning it skips the code directly
10823 	 * below).
10824 	 */
10825 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10826 		if (sin->sin_family == AF_UNSPEC &&
10827 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10828 			/*
10829 			 * Mark it condemned. No new ref. will be made to ill.
10830 			 */
10831 			mutex_enter(&ill->ill_lock);
10832 			ill->ill_state_flags |= ILL_CONDEMNED;
10833 			for (ipif = ill->ill_ipif; ipif != NULL;
10834 			    ipif = ipif->ipif_next) {
10835 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10836 			}
10837 			mutex_exit(&ill->ill_lock);
10838 
10839 			ipif = ill->ill_ipif;
10840 			/* unplumb the loopback interface */
10841 			ill_delete(ill);
10842 			mutex_enter(&connp->conn_lock);
10843 			mutex_enter(&ill->ill_lock);
10844 			ASSERT(ill->ill_group == NULL);
10845 
10846 			/* Are any references to this ill active */
10847 			if (ill_is_freeable(ill)) {
10848 				mutex_exit(&ill->ill_lock);
10849 				mutex_exit(&connp->conn_lock);
10850 				ill_delete_tail(ill);
10851 				mutex_enter(&ill->ill_lock);
10852 				ill_nic_info_dispatch(ill);
10853 				mutex_exit(&ill->ill_lock);
10854 				mi_free(ill);
10855 				return (0);
10856 			}
10857 			success = ipsq_pending_mp_add(connp, ipif,
10858 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10859 			mutex_exit(&connp->conn_lock);
10860 			mutex_exit(&ill->ill_lock);
10861 			if (success)
10862 				return (EINPROGRESS);
10863 			else
10864 				return (EINTR);
10865 		}
10866 	}
10867 
10868 	/*
10869 	 * We are exclusive on the ipsq, so an ill move will be serialized
10870 	 * before or after us.
10871 	 */
10872 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10873 
10874 	if (ipif->ipif_id == 0) {
10875 
10876 		ipsq_t *ipsq;
10877 
10878 		/* Find based on address */
10879 		if (ipif->ipif_isv6) {
10880 			sin6_t *sin6;
10881 
10882 			if (sin->sin_family != AF_INET6)
10883 				return (EAFNOSUPPORT);
10884 
10885 			sin6 = (sin6_t *)sin;
10886 			/* We are a writer, so we should be able to lookup */
10887 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10888 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10889 			if (ipif == NULL) {
10890 				/*
10891 				 * Maybe the address in on another interface in
10892 				 * the same IPMP group? We check this below.
10893 				 */
10894 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10895 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10896 				    ipst);
10897 			}
10898 		} else {
10899 			ipaddr_t addr;
10900 
10901 			if (sin->sin_family != AF_INET)
10902 				return (EAFNOSUPPORT);
10903 
10904 			addr = sin->sin_addr.s_addr;
10905 			/* We are a writer, so we should be able to lookup */
10906 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10907 			    NULL, NULL, NULL, ipst);
10908 			if (ipif == NULL) {
10909 				/*
10910 				 * Maybe the address in on another interface in
10911 				 * the same IPMP group? We check this below.
10912 				 */
10913 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10914 				    NULL, NULL, NULL, NULL, ipst);
10915 			}
10916 		}
10917 		if (ipif == NULL) {
10918 			return (EADDRNOTAVAIL);
10919 		}
10920 
10921 		/*
10922 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10923 		 * lifr_name of the physical interface but with an ip address
10924 		 * lifr_addr of a logical interface plumbed over it.
10925 		 * So update ipsq_current_ipif once ipif points to the
10926 		 * correct interface after doing ipif_lookup_addr().
10927 		 */
10928 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10929 		ASSERT(ipsq != NULL);
10930 
10931 		mutex_enter(&ipsq->ipsq_lock);
10932 		ipsq->ipsq_current_ipif = ipif;
10933 		mutex_exit(&ipsq->ipsq_lock);
10934 
10935 		/*
10936 		 * When the address to be removed is hosted on a different
10937 		 * interface, we check if the interface is in the same IPMP
10938 		 * group as the specified one; if so we proceed with the
10939 		 * removal.
10940 		 * ill->ill_group is NULL when the ill is down, so we have to
10941 		 * compare the group names instead.
10942 		 */
10943 		if (ipif->ipif_ill != ill &&
10944 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10945 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10946 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10947 		    ill->ill_phyint->phyint_groupname) != 0)) {
10948 			ipif_refrele(ipif);
10949 			return (EADDRNOTAVAIL);
10950 		}
10951 
10952 		/* This is a writer */
10953 		ipif_refrele(ipif);
10954 	}
10955 
10956 	/*
10957 	 * Can not delete instance zero since it is tied to the ill.
10958 	 */
10959 	if (ipif->ipif_id == 0)
10960 		return (EBUSY);
10961 
10962 	mutex_enter(&ill->ill_lock);
10963 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10964 	mutex_exit(&ill->ill_lock);
10965 
10966 	ipif_free(ipif);
10967 
10968 	mutex_enter(&connp->conn_lock);
10969 	mutex_enter(&ill->ill_lock);
10970 
10971 
10972 	/* Are any references to this ipif active */
10973 	if (ipif_is_freeable(ipif)) {
10974 		mutex_exit(&ill->ill_lock);
10975 		mutex_exit(&connp->conn_lock);
10976 		ipif_non_duplicate(ipif);
10977 		ipif_down_tail(ipif);
10978 		ipif_free_tail(ipif); /* frees ipif */
10979 		return (0);
10980 	}
10981 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10982 	    IPIF_FREE);
10983 	mutex_exit(&ill->ill_lock);
10984 	mutex_exit(&connp->conn_lock);
10985 	if (success)
10986 		return (EINPROGRESS);
10987 	else
10988 		return (EINTR);
10989 }
10990 
10991 /*
10992  * Restart the removeif ioctl. The refcnt has gone down to 0.
10993  * The ipif is already condemned. So can't find it thru lookups.
10994  */
10995 /* ARGSUSED */
10996 int
10997 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10998     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10999 {
11000 	ill_t *ill = ipif->ipif_ill;
11001 
11002 	ASSERT(IAM_WRITER_IPIF(ipif));
11003 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11004 
11005 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11006 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11007 
11008 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11009 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
11010 		ill_delete_tail(ill);
11011 		mutex_enter(&ill->ill_lock);
11012 		ill_nic_info_dispatch(ill);
11013 		mutex_exit(&ill->ill_lock);
11014 		mi_free(ill);
11015 		return (0);
11016 	}
11017 
11018 	ipif_non_duplicate(ipif);
11019 	ipif_down_tail(ipif);
11020 	ipif_free_tail(ipif);
11021 
11022 	ILL_UNMARK_CHANGING(ill);
11023 	return (0);
11024 }
11025 
11026 /*
11027  * Set the local interface address.
11028  * Allow an address of all zero when the interface is down.
11029  */
11030 /* ARGSUSED */
11031 int
11032 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11033     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11034 {
11035 	int err = 0;
11036 	in6_addr_t v6addr;
11037 	boolean_t need_up = B_FALSE;
11038 
11039 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11040 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11041 
11042 	ASSERT(IAM_WRITER_IPIF(ipif));
11043 
11044 	if (ipif->ipif_isv6) {
11045 		sin6_t *sin6;
11046 		ill_t *ill;
11047 		phyint_t *phyi;
11048 
11049 		if (sin->sin_family != AF_INET6)
11050 			return (EAFNOSUPPORT);
11051 
11052 		sin6 = (sin6_t *)sin;
11053 		v6addr = sin6->sin6_addr;
11054 		ill = ipif->ipif_ill;
11055 		phyi = ill->ill_phyint;
11056 
11057 		/*
11058 		 * Enforce that true multicast interfaces have a link-local
11059 		 * address for logical unit 0.
11060 		 */
11061 		if (ipif->ipif_id == 0 &&
11062 		    (ill->ill_flags & ILLF_MULTICAST) &&
11063 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11064 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11065 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11066 			return (EADDRNOTAVAIL);
11067 		}
11068 
11069 		/*
11070 		 * up interfaces shouldn't have the unspecified address
11071 		 * unless they also have the IPIF_NOLOCAL flags set and
11072 		 * have a subnet assigned.
11073 		 */
11074 		if ((ipif->ipif_flags & IPIF_UP) &&
11075 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11076 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11077 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11078 			return (EADDRNOTAVAIL);
11079 		}
11080 
11081 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11082 			return (EADDRNOTAVAIL);
11083 	} else {
11084 		ipaddr_t addr;
11085 
11086 		if (sin->sin_family != AF_INET)
11087 			return (EAFNOSUPPORT);
11088 
11089 		addr = sin->sin_addr.s_addr;
11090 
11091 		/* Allow 0 as the local address. */
11092 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11093 			return (EADDRNOTAVAIL);
11094 
11095 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11096 	}
11097 
11098 	/*
11099 	 * Even if there is no change we redo things just to rerun
11100 	 * ipif_set_default.
11101 	 */
11102 	if (ipif->ipif_flags & IPIF_UP) {
11103 		/*
11104 		 * Setting a new local address, make sure
11105 		 * we have net and subnet bcast ire's for
11106 		 * the old address if we need them.
11107 		 */
11108 		if (!ipif->ipif_isv6)
11109 			ipif_check_bcast_ires(ipif);
11110 		/*
11111 		 * If the interface is already marked up,
11112 		 * we call ipif_down which will take care
11113 		 * of ditching any IREs that have been set
11114 		 * up based on the old interface address.
11115 		 */
11116 		err = ipif_logical_down(ipif, q, mp);
11117 		if (err == EINPROGRESS)
11118 			return (err);
11119 		ipif_down_tail(ipif);
11120 		need_up = 1;
11121 	}
11122 
11123 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11124 	return (err);
11125 }
11126 
11127 int
11128 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11129     boolean_t need_up)
11130 {
11131 	in6_addr_t v6addr;
11132 	in6_addr_t ov6addr;
11133 	ipaddr_t addr;
11134 	sin6_t	*sin6;
11135 	int	sinlen;
11136 	int	err = 0;
11137 	ill_t	*ill = ipif->ipif_ill;
11138 	boolean_t need_dl_down;
11139 	boolean_t need_arp_down;
11140 	struct iocblk *iocp;
11141 
11142 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11143 
11144 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11145 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11146 	ASSERT(IAM_WRITER_IPIF(ipif));
11147 
11148 	/* Must cancel any pending timer before taking the ill_lock */
11149 	if (ipif->ipif_recovery_id != 0)
11150 		(void) untimeout(ipif->ipif_recovery_id);
11151 	ipif->ipif_recovery_id = 0;
11152 
11153 	if (ipif->ipif_isv6) {
11154 		sin6 = (sin6_t *)sin;
11155 		v6addr = sin6->sin6_addr;
11156 		sinlen = sizeof (struct sockaddr_in6);
11157 	} else {
11158 		addr = sin->sin_addr.s_addr;
11159 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11160 		sinlen = sizeof (struct sockaddr_in);
11161 	}
11162 	mutex_enter(&ill->ill_lock);
11163 	ov6addr = ipif->ipif_v6lcl_addr;
11164 	ipif->ipif_v6lcl_addr = v6addr;
11165 	sctp_update_ipif_addr(ipif, ov6addr);
11166 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11167 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11168 	} else {
11169 		ipif->ipif_v6src_addr = v6addr;
11170 	}
11171 	ipif->ipif_addr_ready = 0;
11172 
11173 	/*
11174 	 * If the interface was previously marked as a duplicate, then since
11175 	 * we've now got a "new" address, it should no longer be considered a
11176 	 * duplicate -- even if the "new" address is the same as the old one.
11177 	 * Note that if all ipifs are down, we may have a pending ARP down
11178 	 * event to handle.  This is because we want to recover from duplicates
11179 	 * and thus delay tearing down ARP until the duplicates have been
11180 	 * removed or disabled.
11181 	 */
11182 	need_dl_down = need_arp_down = B_FALSE;
11183 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11184 		need_arp_down = !need_up;
11185 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11186 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11187 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11188 			need_dl_down = B_TRUE;
11189 		}
11190 	}
11191 
11192 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11193 	    !ill->ill_is_6to4tun) {
11194 		queue_t *wqp = ill->ill_wq;
11195 
11196 		/*
11197 		 * The local address of this interface is a 6to4 address,
11198 		 * check if this interface is in fact a 6to4 tunnel or just
11199 		 * an interface configured with a 6to4 address.  We are only
11200 		 * interested in the former.
11201 		 */
11202 		if (wqp != NULL) {
11203 			while ((wqp->q_next != NULL) &&
11204 			    (wqp->q_next->q_qinfo != NULL) &&
11205 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11206 
11207 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11208 				    == TUN6TO4_MODID) {
11209 					/* set for use in IP */
11210 					ill->ill_is_6to4tun = 1;
11211 					break;
11212 				}
11213 				wqp = wqp->q_next;
11214 			}
11215 		}
11216 	}
11217 
11218 	ipif_set_default(ipif);
11219 
11220 	/*
11221 	 * When publishing an interface address change event, we only notify
11222 	 * the event listeners of the new address.  It is assumed that if they
11223 	 * actively care about the addresses assigned that they will have
11224 	 * already discovered the previous address assigned (if there was one.)
11225 	 *
11226 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11227 	 */
11228 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11229 		(void) ill_hook_event_create(ill, MAP_IPIF_ID(ipif->ipif_id),
11230 		    NE_ADDRESS_CHANGE, sin, sinlen);
11231 	}
11232 
11233 	mutex_exit(&ill->ill_lock);
11234 
11235 	if (need_up) {
11236 		/*
11237 		 * Now bring the interface back up.  If this
11238 		 * is the only IPIF for the ILL, ipif_up
11239 		 * will have to re-bind to the device, so
11240 		 * we may get back EINPROGRESS, in which
11241 		 * case, this IOCTL will get completed in
11242 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11243 		 */
11244 		err = ipif_up(ipif, q, mp);
11245 	}
11246 
11247 	if (need_dl_down)
11248 		ill_dl_down(ill);
11249 	if (need_arp_down)
11250 		ipif_arp_down(ipif);
11251 
11252 	return (err);
11253 }
11254 
11255 
11256 /*
11257  * Restart entry point to restart the address set operation after the
11258  * refcounts have dropped to zero.
11259  */
11260 /* ARGSUSED */
11261 int
11262 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11263     ip_ioctl_cmd_t *ipip, void *ifreq)
11264 {
11265 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11266 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11267 	ASSERT(IAM_WRITER_IPIF(ipif));
11268 	ipif_down_tail(ipif);
11269 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11270 }
11271 
11272 /* ARGSUSED */
11273 int
11274 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11275     ip_ioctl_cmd_t *ipip, void *if_req)
11276 {
11277 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11278 	struct lifreq *lifr = (struct lifreq *)if_req;
11279 
11280 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11281 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11282 	/*
11283 	 * The net mask and address can't change since we have a
11284 	 * reference to the ipif. So no lock is necessary.
11285 	 */
11286 	if (ipif->ipif_isv6) {
11287 		*sin6 = sin6_null;
11288 		sin6->sin6_family = AF_INET6;
11289 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11290 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11291 		lifr->lifr_addrlen =
11292 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11293 	} else {
11294 		*sin = sin_null;
11295 		sin->sin_family = AF_INET;
11296 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11297 		if (ipip->ipi_cmd_type == LIF_CMD) {
11298 			lifr->lifr_addrlen =
11299 			    ip_mask_to_plen(ipif->ipif_net_mask);
11300 		}
11301 	}
11302 	return (0);
11303 }
11304 
11305 /*
11306  * Set the destination address for a pt-pt interface.
11307  */
11308 /* ARGSUSED */
11309 int
11310 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11311     ip_ioctl_cmd_t *ipip, void *if_req)
11312 {
11313 	int err = 0;
11314 	in6_addr_t v6addr;
11315 	boolean_t need_up = B_FALSE;
11316 
11317 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11318 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11319 	ASSERT(IAM_WRITER_IPIF(ipif));
11320 
11321 	if (ipif->ipif_isv6) {
11322 		sin6_t *sin6;
11323 
11324 		if (sin->sin_family != AF_INET6)
11325 			return (EAFNOSUPPORT);
11326 
11327 		sin6 = (sin6_t *)sin;
11328 		v6addr = sin6->sin6_addr;
11329 
11330 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11331 			return (EADDRNOTAVAIL);
11332 	} else {
11333 		ipaddr_t addr;
11334 
11335 		if (sin->sin_family != AF_INET)
11336 			return (EAFNOSUPPORT);
11337 
11338 		addr = sin->sin_addr.s_addr;
11339 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11340 			return (EADDRNOTAVAIL);
11341 
11342 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11343 	}
11344 
11345 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11346 		return (0);	/* No change */
11347 
11348 	if (ipif->ipif_flags & IPIF_UP) {
11349 		/*
11350 		 * If the interface is already marked up,
11351 		 * we call ipif_down which will take care
11352 		 * of ditching any IREs that have been set
11353 		 * up based on the old pp dst address.
11354 		 */
11355 		err = ipif_logical_down(ipif, q, mp);
11356 		if (err == EINPROGRESS)
11357 			return (err);
11358 		ipif_down_tail(ipif);
11359 		need_up = B_TRUE;
11360 	}
11361 	/*
11362 	 * could return EINPROGRESS. If so ioctl will complete in
11363 	 * ip_rput_dlpi_writer
11364 	 */
11365 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11366 	return (err);
11367 }
11368 
11369 static int
11370 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11371     boolean_t need_up)
11372 {
11373 	in6_addr_t v6addr;
11374 	ill_t	*ill = ipif->ipif_ill;
11375 	int	err = 0;
11376 	boolean_t need_dl_down;
11377 	boolean_t need_arp_down;
11378 
11379 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11380 	    ipif->ipif_id, (void *)ipif));
11381 
11382 	/* Must cancel any pending timer before taking the ill_lock */
11383 	if (ipif->ipif_recovery_id != 0)
11384 		(void) untimeout(ipif->ipif_recovery_id);
11385 	ipif->ipif_recovery_id = 0;
11386 
11387 	if (ipif->ipif_isv6) {
11388 		sin6_t *sin6;
11389 
11390 		sin6 = (sin6_t *)sin;
11391 		v6addr = sin6->sin6_addr;
11392 	} else {
11393 		ipaddr_t addr;
11394 
11395 		addr = sin->sin_addr.s_addr;
11396 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11397 	}
11398 	mutex_enter(&ill->ill_lock);
11399 	/* Set point to point destination address. */
11400 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11401 		/*
11402 		 * Allow this as a means of creating logical
11403 		 * pt-pt interfaces on top of e.g. an Ethernet.
11404 		 * XXX Undocumented HACK for testing.
11405 		 * pt-pt interfaces are created with NUD disabled.
11406 		 */
11407 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11408 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11409 		if (ipif->ipif_isv6)
11410 			ill->ill_flags |= ILLF_NONUD;
11411 	}
11412 
11413 	/*
11414 	 * If the interface was previously marked as a duplicate, then since
11415 	 * we've now got a "new" address, it should no longer be considered a
11416 	 * duplicate -- even if the "new" address is the same as the old one.
11417 	 * Note that if all ipifs are down, we may have a pending ARP down
11418 	 * event to handle.
11419 	 */
11420 	need_dl_down = need_arp_down = B_FALSE;
11421 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11422 		need_arp_down = !need_up;
11423 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11424 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11425 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11426 			need_dl_down = B_TRUE;
11427 		}
11428 	}
11429 
11430 	/* Set the new address. */
11431 	ipif->ipif_v6pp_dst_addr = v6addr;
11432 	/* Make sure subnet tracks pp_dst */
11433 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11434 	mutex_exit(&ill->ill_lock);
11435 
11436 	if (need_up) {
11437 		/*
11438 		 * Now bring the interface back up.  If this
11439 		 * is the only IPIF for the ILL, ipif_up
11440 		 * will have to re-bind to the device, so
11441 		 * we may get back EINPROGRESS, in which
11442 		 * case, this IOCTL will get completed in
11443 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11444 		 */
11445 		err = ipif_up(ipif, q, mp);
11446 	}
11447 
11448 	if (need_dl_down)
11449 		ill_dl_down(ill);
11450 
11451 	if (need_arp_down)
11452 		ipif_arp_down(ipif);
11453 	return (err);
11454 }
11455 
11456 /*
11457  * Restart entry point to restart the dstaddress set operation after the
11458  * refcounts have dropped to zero.
11459  */
11460 /* ARGSUSED */
11461 int
11462 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11463     ip_ioctl_cmd_t *ipip, void *ifreq)
11464 {
11465 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11466 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11467 	ipif_down_tail(ipif);
11468 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11469 }
11470 
11471 /* ARGSUSED */
11472 int
11473 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11474     ip_ioctl_cmd_t *ipip, void *if_req)
11475 {
11476 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11477 
11478 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11479 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11480 	/*
11481 	 * Get point to point destination address. The addresses can't
11482 	 * change since we hold a reference to the ipif.
11483 	 */
11484 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11485 		return (EADDRNOTAVAIL);
11486 
11487 	if (ipif->ipif_isv6) {
11488 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11489 		*sin6 = sin6_null;
11490 		sin6->sin6_family = AF_INET6;
11491 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11492 	} else {
11493 		*sin = sin_null;
11494 		sin->sin_family = AF_INET;
11495 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11496 	}
11497 	return (0);
11498 }
11499 
11500 /*
11501  * part of ipmp, make this func return the active/inactive state and
11502  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11503  */
11504 /*
11505  * This function either sets or clears the IFF_INACTIVE flag.
11506  *
11507  * As long as there are some addresses or multicast memberships on the
11508  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11509  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11510  * will be used for outbound packets.
11511  *
11512  * Caller needs to verify the validity of setting IFF_INACTIVE.
11513  */
11514 static void
11515 phyint_inactive(phyint_t *phyi)
11516 {
11517 	ill_t *ill_v4;
11518 	ill_t *ill_v6;
11519 	ipif_t *ipif;
11520 	ilm_t *ilm;
11521 
11522 	ill_v4 = phyi->phyint_illv4;
11523 	ill_v6 = phyi->phyint_illv6;
11524 
11525 	/*
11526 	 * No need for a lock while traversing the list since iam
11527 	 * a writer
11528 	 */
11529 	if (ill_v4 != NULL) {
11530 		ASSERT(IAM_WRITER_ILL(ill_v4));
11531 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11532 		    ipif = ipif->ipif_next) {
11533 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11534 				mutex_enter(&phyi->phyint_lock);
11535 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11536 				mutex_exit(&phyi->phyint_lock);
11537 				return;
11538 			}
11539 		}
11540 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11541 		    ilm = ilm->ilm_next) {
11542 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11543 				mutex_enter(&phyi->phyint_lock);
11544 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11545 				mutex_exit(&phyi->phyint_lock);
11546 				return;
11547 			}
11548 		}
11549 	}
11550 	if (ill_v6 != NULL) {
11551 		ill_v6 = phyi->phyint_illv6;
11552 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11553 		    ipif = ipif->ipif_next) {
11554 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11555 				mutex_enter(&phyi->phyint_lock);
11556 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11557 				mutex_exit(&phyi->phyint_lock);
11558 				return;
11559 			}
11560 		}
11561 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11562 		    ilm = ilm->ilm_next) {
11563 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11564 				mutex_enter(&phyi->phyint_lock);
11565 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11566 				mutex_exit(&phyi->phyint_lock);
11567 				return;
11568 			}
11569 		}
11570 	}
11571 	mutex_enter(&phyi->phyint_lock);
11572 	phyi->phyint_flags |= PHYI_INACTIVE;
11573 	mutex_exit(&phyi->phyint_lock);
11574 }
11575 
11576 /*
11577  * This function is called only when the phyint flags change. Currently
11578  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11579  * that we can select a good ill.
11580  */
11581 static void
11582 ip_redo_nomination(phyint_t *phyi)
11583 {
11584 	ill_t *ill_v4;
11585 
11586 	ill_v4 = phyi->phyint_illv4;
11587 
11588 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11589 		ASSERT(IAM_WRITER_ILL(ill_v4));
11590 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11591 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11592 	}
11593 }
11594 
11595 /*
11596  * Heuristic to check if ill is INACTIVE.
11597  * Checks if ill has an ipif with an usable ip address.
11598  *
11599  * Return values:
11600  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11601  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11602  */
11603 static boolean_t
11604 ill_is_inactive(ill_t *ill)
11605 {
11606 	ipif_t *ipif;
11607 
11608 	/* Check whether it is in an IPMP group */
11609 	if (ill->ill_phyint->phyint_groupname == NULL)
11610 		return (B_FALSE);
11611 
11612 	if (ill->ill_ipif_up_count == 0)
11613 		return (B_TRUE);
11614 
11615 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11616 		uint64_t flags = ipif->ipif_flags;
11617 
11618 		/*
11619 		 * This ipif is usable if it is IPIF_UP and not a
11620 		 * dedicated test address.  A dedicated test address
11621 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11622 		 * (note in particular that V6 test addresses are
11623 		 * link-local data addresses and thus are marked
11624 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11625 		 */
11626 		if ((flags & IPIF_UP) &&
11627 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11628 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11629 			return (B_FALSE);
11630 	}
11631 	return (B_TRUE);
11632 }
11633 
11634 /*
11635  * Set interface flags.
11636  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11637  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11638  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11639  *
11640  * NOTE : We really don't enforce that ipif_id zero should be used
11641  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11642  *	  is because applications generally does SICGLIFFLAGS and
11643  *	  ORs in the new flags (that affects the logical) and does a
11644  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11645  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11646  *	  flags that will be turned on is correct with respect to
11647  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11648  */
11649 /* ARGSUSED */
11650 int
11651 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11652     ip_ioctl_cmd_t *ipip, void *if_req)
11653 {
11654 	uint64_t turn_on;
11655 	uint64_t turn_off;
11656 	int	err;
11657 	phyint_t *phyi;
11658 	ill_t *ill;
11659 	uint64_t intf_flags;
11660 	boolean_t phyint_flags_modified = B_FALSE;
11661 	uint64_t flags;
11662 	struct ifreq *ifr;
11663 	struct lifreq *lifr;
11664 	boolean_t set_linklocal = B_FALSE;
11665 	boolean_t zero_source = B_FALSE;
11666 	ip_stack_t *ipst;
11667 
11668 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11669 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11670 
11671 	ASSERT(IAM_WRITER_IPIF(ipif));
11672 
11673 	ill = ipif->ipif_ill;
11674 	phyi = ill->ill_phyint;
11675 	ipst = ill->ill_ipst;
11676 
11677 	if (ipip->ipi_cmd_type == IF_CMD) {
11678 		ifr = (struct ifreq *)if_req;
11679 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11680 	} else {
11681 		lifr = (struct lifreq *)if_req;
11682 		flags = lifr->lifr_flags;
11683 	}
11684 
11685 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11686 
11687 	/*
11688 	 * Have the flags been set correctly until now?
11689 	 */
11690 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11691 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11692 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11693 	/*
11694 	 * Compare the new flags to the old, and partition
11695 	 * into those coming on and those going off.
11696 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11697 	 */
11698 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11699 		flags |= intf_flags & ~0xFFFF;
11700 
11701 	/*
11702 	 * First check which bits will change and then which will
11703 	 * go on and off
11704 	 */
11705 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11706 	if (!turn_on)
11707 		return (0);	/* No change */
11708 
11709 	turn_off = intf_flags & turn_on;
11710 	turn_on ^= turn_off;
11711 	err = 0;
11712 
11713 	/*
11714 	 * Don't allow any bits belonging to the logical interface
11715 	 * to be set or cleared on the replacement ipif that was
11716 	 * created temporarily during a MOVE.
11717 	 */
11718 	if (ipif->ipif_replace_zero &&
11719 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11720 		return (EINVAL);
11721 	}
11722 
11723 	/*
11724 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11725 	 * IPv6 interfaces.
11726 	 */
11727 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11728 		return (EINVAL);
11729 
11730 	/*
11731 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11732 	 */
11733 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11734 		return (EINVAL);
11735 
11736 	/*
11737 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11738 	 * interfaces.  It makes no sense in that context.
11739 	 */
11740 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11741 		return (EINVAL);
11742 
11743 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11744 		zero_source = B_TRUE;
11745 
11746 	/*
11747 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11748 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11749 	 * If the link local address isn't set, and can be set, it will get
11750 	 * set later on in this function.
11751 	 */
11752 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11753 	    (flags & IFF_UP) && !zero_source &&
11754 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11755 		if (ipif_cant_setlinklocal(ipif))
11756 			return (EINVAL);
11757 		set_linklocal = B_TRUE;
11758 	}
11759 
11760 	/*
11761 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11762 	 * same time. No need to grab ill_g_usesrc_lock here, see
11763 	 * synchronization notes in ip.c
11764 	 */
11765 	if (turn_on & PHYI_STANDBY &&
11766 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11767 		return (EINVAL);
11768 	}
11769 
11770 	/*
11771 	 * If we modify physical interface flags, we'll potentially need to
11772 	 * send up two routing socket messages for the changes (one for the
11773 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11774 	 */
11775 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11776 		phyint_flags_modified = B_TRUE;
11777 
11778 	/*
11779 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11780 	 * we need to flush the IRE_CACHES belonging to this ill.
11781 	 * We handle this case here without doing the DOWN/UP dance
11782 	 * like it is done for other flags. If some other flags are
11783 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11784 	 * below will handle it by bringing it down and then
11785 	 * bringing it UP.
11786 	 */
11787 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11788 		ill_t *ill_v4, *ill_v6;
11789 
11790 		ill_v4 = phyi->phyint_illv4;
11791 		ill_v6 = phyi->phyint_illv6;
11792 
11793 		/*
11794 		 * First set the INACTIVE flag if needed. Then delete the ires.
11795 		 * ire_add will atomically prevent creating new IRE_CACHEs
11796 		 * unless hidden flag is set.
11797 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11798 		 */
11799 		if ((turn_on & PHYI_FAILED) &&
11800 		    ((intf_flags & PHYI_STANDBY) ||
11801 		    !ipst->ips_ipmp_enable_failback)) {
11802 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11803 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11804 		}
11805 		if ((turn_off & PHYI_FAILED) &&
11806 		    ((intf_flags & PHYI_STANDBY) ||
11807 		    (!ipst->ips_ipmp_enable_failback &&
11808 		    ill_is_inactive(ill)))) {
11809 			phyint_inactive(phyi);
11810 		}
11811 
11812 		if (turn_on & PHYI_STANDBY) {
11813 			/*
11814 			 * We implicitly set INACTIVE only when STANDBY is set.
11815 			 * INACTIVE is also set on non-STANDBY phyint when user
11816 			 * disables FAILBACK using configuration file.
11817 			 * Do not allow STANDBY to be set on such INACTIVE
11818 			 * phyint
11819 			 */
11820 			if (phyi->phyint_flags & PHYI_INACTIVE)
11821 				return (EINVAL);
11822 			if (!(phyi->phyint_flags & PHYI_FAILED))
11823 				phyint_inactive(phyi);
11824 		}
11825 		if (turn_off & PHYI_STANDBY) {
11826 			if (ipst->ips_ipmp_enable_failback) {
11827 				/*
11828 				 * Reset PHYI_INACTIVE.
11829 				 */
11830 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11831 			} else if (ill_is_inactive(ill) &&
11832 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11833 				/*
11834 				 * Need to set INACTIVE, when user sets
11835 				 * STANDBY on a non-STANDBY phyint and
11836 				 * later resets STANDBY
11837 				 */
11838 				phyint_inactive(phyi);
11839 			}
11840 		}
11841 		/*
11842 		 * We should always send up a message so that the
11843 		 * daemons come to know of it. Note that the zeroth
11844 		 * interface can be down and the check below for IPIF_UP
11845 		 * will not make sense as we are actually setting
11846 		 * a phyint flag here. We assume that the ipif used
11847 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11848 		 * send up any message for non-zero ipifs).
11849 		 */
11850 		phyint_flags_modified = B_TRUE;
11851 
11852 		if (ill_v4 != NULL) {
11853 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11854 			    IRE_CACHE, ill_stq_cache_delete,
11855 			    (char *)ill_v4, ill_v4);
11856 			illgrp_reset_schednext(ill_v4);
11857 		}
11858 		if (ill_v6 != NULL) {
11859 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11860 			    IRE_CACHE, ill_stq_cache_delete,
11861 			    (char *)ill_v6, ill_v6);
11862 			illgrp_reset_schednext(ill_v6);
11863 		}
11864 	}
11865 
11866 	/*
11867 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11868 	 * status of the interface and, if the interface is part of an IPMP
11869 	 * group, all other interfaces that are part of the same IPMP
11870 	 * group.
11871 	 */
11872 	if ((turn_on | turn_off) & ILLF_ROUTER)
11873 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11874 
11875 	/*
11876 	 * If the interface is not UP and we are not going to
11877 	 * bring it UP, record the flags and return. When the
11878 	 * interface comes UP later, the right actions will be
11879 	 * taken.
11880 	 */
11881 	if (!(ipif->ipif_flags & IPIF_UP) &&
11882 	    !(turn_on & IPIF_UP)) {
11883 		/* Record new flags in their respective places. */
11884 		mutex_enter(&ill->ill_lock);
11885 		mutex_enter(&ill->ill_phyint->phyint_lock);
11886 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11887 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11888 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11889 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11890 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11891 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11892 		mutex_exit(&ill->ill_lock);
11893 		mutex_exit(&ill->ill_phyint->phyint_lock);
11894 
11895 		/*
11896 		 * We do the broadcast and nomination here rather
11897 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11898 		 * the case of FAILBACK from INACTIVE standby to the
11899 		 * interface that has been repaired, PHYI_FAILED has not
11900 		 * been cleared yet. If there are only two interfaces in
11901 		 * that group, all we have is a FAILED and INACTIVE
11902 		 * interface. If we do the nomination soon after a failback,
11903 		 * the broadcast nomination code would select the
11904 		 * INACTIVE interface for receiving broadcasts as FAILED is
11905 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11906 		 * receive broadcast packets, we need to redo nomination
11907 		 * when the FAILED is cleared here. Thus, in general we
11908 		 * always do the nomination here for FAILED, STANDBY
11909 		 * and OFFLINE.
11910 		 */
11911 		if (((turn_on | turn_off) &
11912 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11913 			ip_redo_nomination(phyi);
11914 		}
11915 		if (phyint_flags_modified) {
11916 			if (phyi->phyint_illv4 != NULL) {
11917 				ip_rts_ifmsg(phyi->phyint_illv4->
11918 				    ill_ipif);
11919 			}
11920 			if (phyi->phyint_illv6 != NULL) {
11921 				ip_rts_ifmsg(phyi->phyint_illv6->
11922 				    ill_ipif);
11923 			}
11924 		}
11925 		return (0);
11926 	} else if (set_linklocal || zero_source) {
11927 		mutex_enter(&ill->ill_lock);
11928 		if (set_linklocal)
11929 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11930 		if (zero_source)
11931 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11932 		mutex_exit(&ill->ill_lock);
11933 	}
11934 
11935 	/*
11936 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11937 	 * or point-to-point interfaces with an unspecified destination. We do
11938 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11939 	 * have a subnet assigned, which is how in.ndpd currently manages its
11940 	 * onlink prefix list when no addresses are configured with those
11941 	 * prefixes.
11942 	 */
11943 	if (ipif->ipif_isv6 &&
11944 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11945 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11946 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11947 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11948 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11949 		return (EINVAL);
11950 	}
11951 
11952 	/*
11953 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11954 	 * from being brought up.
11955 	 */
11956 	if (!ipif->ipif_isv6 &&
11957 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11958 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11959 		return (EINVAL);
11960 	}
11961 
11962 	/*
11963 	 * The only flag changes that we currently take specific action on
11964 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11965 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11966 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11967 	 * the flags and bringing it back up again.
11968 	 */
11969 	if ((turn_on|turn_off) &
11970 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11971 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11972 		/*
11973 		 * Taking this ipif down, make sure we have
11974 		 * valid net and subnet bcast ire's for other
11975 		 * logical interfaces, if we need them.
11976 		 */
11977 		if (!ipif->ipif_isv6)
11978 			ipif_check_bcast_ires(ipif);
11979 
11980 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11981 		    !(turn_off & IPIF_UP)) {
11982 			if (ipif->ipif_flags & IPIF_UP)
11983 				ill->ill_logical_down = 1;
11984 			turn_on &= ~IPIF_UP;
11985 		}
11986 		err = ipif_down(ipif, q, mp);
11987 		ip1dbg(("ipif_down returns %d err ", err));
11988 		if (err == EINPROGRESS)
11989 			return (err);
11990 		ipif_down_tail(ipif);
11991 	}
11992 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11993 }
11994 
11995 static int
11996 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
11997 {
11998 	ill_t	*ill;
11999 	phyint_t *phyi;
12000 	uint64_t turn_on;
12001 	uint64_t turn_off;
12002 	uint64_t intf_flags;
12003 	boolean_t phyint_flags_modified = B_FALSE;
12004 	int	err = 0;
12005 	boolean_t set_linklocal = B_FALSE;
12006 	boolean_t zero_source = B_FALSE;
12007 
12008 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12009 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12010 
12011 	ASSERT(IAM_WRITER_IPIF(ipif));
12012 
12013 	ill = ipif->ipif_ill;
12014 	phyi = ill->ill_phyint;
12015 
12016 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12017 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12018 
12019 	turn_off = intf_flags & turn_on;
12020 	turn_on ^= turn_off;
12021 
12022 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12023 		phyint_flags_modified = B_TRUE;
12024 
12025 	/*
12026 	 * Now we change the flags. Track current value of
12027 	 * other flags in their respective places.
12028 	 */
12029 	mutex_enter(&ill->ill_lock);
12030 	mutex_enter(&phyi->phyint_lock);
12031 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12032 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12033 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12034 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12035 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12036 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12037 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12038 		set_linklocal = B_TRUE;
12039 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12040 	}
12041 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12042 		zero_source = B_TRUE;
12043 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12044 	}
12045 	mutex_exit(&ill->ill_lock);
12046 	mutex_exit(&phyi->phyint_lock);
12047 
12048 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12049 		ip_redo_nomination(phyi);
12050 
12051 	if (set_linklocal)
12052 		(void) ipif_setlinklocal(ipif);
12053 
12054 	if (zero_source)
12055 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12056 	else
12057 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12058 
12059 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
12060 		/*
12061 		 * XXX ipif_up really does not know whether a phyint flags
12062 		 * was modified or not. So, it sends up information on
12063 		 * only one routing sockets message. As we don't bring up
12064 		 * the interface and also set STANDBY/FAILED simultaneously
12065 		 * it should be okay.
12066 		 */
12067 		err = ipif_up(ipif, q, mp);
12068 	} else {
12069 		/*
12070 		 * Make sure routing socket sees all changes to the flags.
12071 		 * ipif_up_done* handles this when we use ipif_up.
12072 		 */
12073 		if (phyint_flags_modified) {
12074 			if (phyi->phyint_illv4 != NULL) {
12075 				ip_rts_ifmsg(phyi->phyint_illv4->
12076 				    ill_ipif);
12077 			}
12078 			if (phyi->phyint_illv6 != NULL) {
12079 				ip_rts_ifmsg(phyi->phyint_illv6->
12080 				    ill_ipif);
12081 			}
12082 		} else {
12083 			ip_rts_ifmsg(ipif);
12084 		}
12085 		/*
12086 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12087 		 * this in need_up case.
12088 		 */
12089 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12090 	}
12091 	return (err);
12092 }
12093 
12094 /*
12095  * Restart the flags operation now that the refcounts have dropped to zero.
12096  */
12097 /* ARGSUSED */
12098 int
12099 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12100     ip_ioctl_cmd_t *ipip, void *if_req)
12101 {
12102 	uint64_t flags;
12103 	struct ifreq *ifr = if_req;
12104 	struct lifreq *lifr = if_req;
12105 
12106 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12107 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12108 
12109 	ipif_down_tail(ipif);
12110 	if (ipip->ipi_cmd_type == IF_CMD) {
12111 		/* cast to uint16_t prevents unwanted sign extension */
12112 		flags = (uint16_t)ifr->ifr_flags;
12113 	} else {
12114 		flags = lifr->lifr_flags;
12115 	}
12116 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
12117 }
12118 
12119 /*
12120  * Can operate on either a module or a driver queue.
12121  */
12122 /* ARGSUSED */
12123 int
12124 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12125     ip_ioctl_cmd_t *ipip, void *if_req)
12126 {
12127 	/*
12128 	 * Has the flags been set correctly till now ?
12129 	 */
12130 	ill_t *ill = ipif->ipif_ill;
12131 	phyint_t *phyi = ill->ill_phyint;
12132 
12133 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12134 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12135 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12136 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12137 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12138 
12139 	/*
12140 	 * Need a lock since some flags can be set even when there are
12141 	 * references to the ipif.
12142 	 */
12143 	mutex_enter(&ill->ill_lock);
12144 	if (ipip->ipi_cmd_type == IF_CMD) {
12145 		struct ifreq *ifr = (struct ifreq *)if_req;
12146 
12147 		/* Get interface flags (low 16 only). */
12148 		ifr->ifr_flags = ((ipif->ipif_flags |
12149 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12150 	} else {
12151 		struct lifreq *lifr = (struct lifreq *)if_req;
12152 
12153 		/* Get interface flags. */
12154 		lifr->lifr_flags = ipif->ipif_flags |
12155 		    ill->ill_flags | phyi->phyint_flags;
12156 	}
12157 	mutex_exit(&ill->ill_lock);
12158 	return (0);
12159 }
12160 
12161 /* ARGSUSED */
12162 int
12163 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12164     ip_ioctl_cmd_t *ipip, void *if_req)
12165 {
12166 	int mtu;
12167 	int ip_min_mtu;
12168 	struct ifreq	*ifr;
12169 	struct lifreq *lifr;
12170 	ire_t	*ire;
12171 	ip_stack_t *ipst;
12172 
12173 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12174 	    ipif->ipif_id, (void *)ipif));
12175 	if (ipip->ipi_cmd_type == IF_CMD) {
12176 		ifr = (struct ifreq *)if_req;
12177 		mtu = ifr->ifr_metric;
12178 	} else {
12179 		lifr = (struct lifreq *)if_req;
12180 		mtu = lifr->lifr_mtu;
12181 	}
12182 
12183 	if (ipif->ipif_isv6)
12184 		ip_min_mtu = IPV6_MIN_MTU;
12185 	else
12186 		ip_min_mtu = IP_MIN_MTU;
12187 
12188 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12189 		return (EINVAL);
12190 
12191 	/*
12192 	 * Change the MTU size in all relevant ire's.
12193 	 * Mtu change Vs. new ire creation - protocol below.
12194 	 * First change ipif_mtu and the ire_max_frag of the
12195 	 * interface ire. Then do an ire walk and change the
12196 	 * ire_max_frag of all affected ires. During ire_add
12197 	 * under the bucket lock, set the ire_max_frag of the
12198 	 * new ire being created from the ipif/ire from which
12199 	 * it is being derived. If an mtu change happens after
12200 	 * the ire is added, the new ire will be cleaned up.
12201 	 * Conversely if the mtu change happens before the ire
12202 	 * is added, ire_add will see the new value of the mtu.
12203 	 */
12204 	ipif->ipif_mtu = mtu;
12205 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12206 
12207 	if (ipif->ipif_isv6)
12208 		ire = ipif_to_ire_v6(ipif);
12209 	else
12210 		ire = ipif_to_ire(ipif);
12211 	if (ire != NULL) {
12212 		ire->ire_max_frag = ipif->ipif_mtu;
12213 		ire_refrele(ire);
12214 	}
12215 	ipst = ipif->ipif_ill->ill_ipst;
12216 	if (ipif->ipif_flags & IPIF_UP) {
12217 		if (ipif->ipif_isv6)
12218 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12219 			    ipst);
12220 		else
12221 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12222 			    ipst);
12223 	}
12224 	/* Update the MTU in SCTP's list */
12225 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12226 	return (0);
12227 }
12228 
12229 /* Get interface MTU. */
12230 /* ARGSUSED */
12231 int
12232 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12233 	ip_ioctl_cmd_t *ipip, void *if_req)
12234 {
12235 	struct ifreq	*ifr;
12236 	struct lifreq	*lifr;
12237 
12238 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12239 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12240 	if (ipip->ipi_cmd_type == IF_CMD) {
12241 		ifr = (struct ifreq *)if_req;
12242 		ifr->ifr_metric = ipif->ipif_mtu;
12243 	} else {
12244 		lifr = (struct lifreq *)if_req;
12245 		lifr->lifr_mtu = ipif->ipif_mtu;
12246 	}
12247 	return (0);
12248 }
12249 
12250 /* Set interface broadcast address. */
12251 /* ARGSUSED2 */
12252 int
12253 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12254 	ip_ioctl_cmd_t *ipip, void *if_req)
12255 {
12256 	ipaddr_t addr;
12257 	ire_t	*ire;
12258 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12259 
12260 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12261 	    ipif->ipif_id));
12262 
12263 	ASSERT(IAM_WRITER_IPIF(ipif));
12264 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12265 		return (EADDRNOTAVAIL);
12266 
12267 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12268 
12269 	if (sin->sin_family != AF_INET)
12270 		return (EAFNOSUPPORT);
12271 
12272 	addr = sin->sin_addr.s_addr;
12273 	if (ipif->ipif_flags & IPIF_UP) {
12274 		/*
12275 		 * If we are already up, make sure the new
12276 		 * broadcast address makes sense.  If it does,
12277 		 * there should be an IRE for it already.
12278 		 * Don't match on ipif, only on the ill
12279 		 * since we are sharing these now. Don't use
12280 		 * MATCH_IRE_ILL_GROUP as we are looking for
12281 		 * the broadcast ire on this ill and each ill
12282 		 * in the group has its own broadcast ire.
12283 		 */
12284 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12285 		    ipif, ALL_ZONES, NULL,
12286 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12287 		if (ire == NULL) {
12288 			return (EINVAL);
12289 		} else {
12290 			ire_refrele(ire);
12291 		}
12292 	}
12293 	/*
12294 	 * Changing the broadcast addr for this ipif.
12295 	 * Make sure we have valid net and subnet bcast
12296 	 * ire's for other logical interfaces, if needed.
12297 	 */
12298 	if (addr != ipif->ipif_brd_addr)
12299 		ipif_check_bcast_ires(ipif);
12300 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12301 	return (0);
12302 }
12303 
12304 /* Get interface broadcast address. */
12305 /* ARGSUSED */
12306 int
12307 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12308     ip_ioctl_cmd_t *ipip, void *if_req)
12309 {
12310 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12311 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12312 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12313 		return (EADDRNOTAVAIL);
12314 
12315 	/* IPIF_BROADCAST not possible with IPv6 */
12316 	ASSERT(!ipif->ipif_isv6);
12317 	*sin = sin_null;
12318 	sin->sin_family = AF_INET;
12319 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12320 	return (0);
12321 }
12322 
12323 /*
12324  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12325  */
12326 /* ARGSUSED */
12327 int
12328 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12329     ip_ioctl_cmd_t *ipip, void *if_req)
12330 {
12331 	int err = 0;
12332 	in6_addr_t v6mask;
12333 
12334 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12335 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12336 
12337 	ASSERT(IAM_WRITER_IPIF(ipif));
12338 
12339 	if (ipif->ipif_isv6) {
12340 		sin6_t *sin6;
12341 
12342 		if (sin->sin_family != AF_INET6)
12343 			return (EAFNOSUPPORT);
12344 
12345 		sin6 = (sin6_t *)sin;
12346 		v6mask = sin6->sin6_addr;
12347 	} else {
12348 		ipaddr_t mask;
12349 
12350 		if (sin->sin_family != AF_INET)
12351 			return (EAFNOSUPPORT);
12352 
12353 		mask = sin->sin_addr.s_addr;
12354 		V4MASK_TO_V6(mask, v6mask);
12355 	}
12356 
12357 	/*
12358 	 * No big deal if the interface isn't already up, or the mask
12359 	 * isn't really changing, or this is pt-pt.
12360 	 */
12361 	if (!(ipif->ipif_flags & IPIF_UP) ||
12362 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12363 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12364 		ipif->ipif_v6net_mask = v6mask;
12365 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12366 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12367 			    ipif->ipif_v6net_mask,
12368 			    ipif->ipif_v6subnet);
12369 		}
12370 		return (0);
12371 	}
12372 	/*
12373 	 * Make sure we have valid net and subnet broadcast ire's
12374 	 * for the old netmask, if needed by other logical interfaces.
12375 	 */
12376 	if (!ipif->ipif_isv6)
12377 		ipif_check_bcast_ires(ipif);
12378 
12379 	err = ipif_logical_down(ipif, q, mp);
12380 	if (err == EINPROGRESS)
12381 		return (err);
12382 	ipif_down_tail(ipif);
12383 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12384 	return (err);
12385 }
12386 
12387 static int
12388 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12389 {
12390 	in6_addr_t v6mask;
12391 	int err = 0;
12392 
12393 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12394 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12395 
12396 	if (ipif->ipif_isv6) {
12397 		sin6_t *sin6;
12398 
12399 		sin6 = (sin6_t *)sin;
12400 		v6mask = sin6->sin6_addr;
12401 	} else {
12402 		ipaddr_t mask;
12403 
12404 		mask = sin->sin_addr.s_addr;
12405 		V4MASK_TO_V6(mask, v6mask);
12406 	}
12407 
12408 	ipif->ipif_v6net_mask = v6mask;
12409 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12410 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12411 		    ipif->ipif_v6subnet);
12412 	}
12413 	err = ipif_up(ipif, q, mp);
12414 
12415 	if (err == 0 || err == EINPROGRESS) {
12416 		/*
12417 		 * The interface must be DL_BOUND if this packet has to
12418 		 * go out on the wire. Since we only go through a logical
12419 		 * down and are bound with the driver during an internal
12420 		 * down/up that is satisfied.
12421 		 */
12422 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12423 			/* Potentially broadcast an address mask reply. */
12424 			ipif_mask_reply(ipif);
12425 		}
12426 	}
12427 	return (err);
12428 }
12429 
12430 /* ARGSUSED */
12431 int
12432 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12433     ip_ioctl_cmd_t *ipip, void *if_req)
12434 {
12435 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12436 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12437 	ipif_down_tail(ipif);
12438 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12439 }
12440 
12441 /* Get interface net mask. */
12442 /* ARGSUSED */
12443 int
12444 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12445     ip_ioctl_cmd_t *ipip, void *if_req)
12446 {
12447 	struct lifreq *lifr = (struct lifreq *)if_req;
12448 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12449 
12450 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12451 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12452 
12453 	/*
12454 	 * net mask can't change since we have a reference to the ipif.
12455 	 */
12456 	if (ipif->ipif_isv6) {
12457 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12458 		*sin6 = sin6_null;
12459 		sin6->sin6_family = AF_INET6;
12460 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12461 		lifr->lifr_addrlen =
12462 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12463 	} else {
12464 		*sin = sin_null;
12465 		sin->sin_family = AF_INET;
12466 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12467 		if (ipip->ipi_cmd_type == LIF_CMD) {
12468 			lifr->lifr_addrlen =
12469 			    ip_mask_to_plen(ipif->ipif_net_mask);
12470 		}
12471 	}
12472 	return (0);
12473 }
12474 
12475 /* ARGSUSED */
12476 int
12477 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12478     ip_ioctl_cmd_t *ipip, void *if_req)
12479 {
12480 
12481 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12482 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12483 	/*
12484 	 * Set interface metric.  We don't use this for
12485 	 * anything but we keep track of it in case it is
12486 	 * important to routing applications or such.
12487 	 */
12488 	if (ipip->ipi_cmd_type == IF_CMD) {
12489 		struct ifreq    *ifr;
12490 
12491 		ifr = (struct ifreq *)if_req;
12492 		ipif->ipif_metric = ifr->ifr_metric;
12493 	} else {
12494 		struct lifreq   *lifr;
12495 
12496 		lifr = (struct lifreq *)if_req;
12497 		ipif->ipif_metric = lifr->lifr_metric;
12498 	}
12499 	return (0);
12500 }
12501 
12502 /* ARGSUSED */
12503 int
12504 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12505     ip_ioctl_cmd_t *ipip, void *if_req)
12506 {
12507 	/* Get interface metric. */
12508 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12509 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12510 	if (ipip->ipi_cmd_type == IF_CMD) {
12511 		struct ifreq    *ifr;
12512 
12513 		ifr = (struct ifreq *)if_req;
12514 		ifr->ifr_metric = ipif->ipif_metric;
12515 	} else {
12516 		struct lifreq   *lifr;
12517 
12518 		lifr = (struct lifreq *)if_req;
12519 		lifr->lifr_metric = ipif->ipif_metric;
12520 	}
12521 
12522 	return (0);
12523 }
12524 
12525 /* ARGSUSED */
12526 int
12527 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12528     ip_ioctl_cmd_t *ipip, void *if_req)
12529 {
12530 
12531 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12532 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12533 	/*
12534 	 * Set the muxid returned from I_PLINK.
12535 	 */
12536 	if (ipip->ipi_cmd_type == IF_CMD) {
12537 		struct ifreq *ifr = (struct ifreq *)if_req;
12538 
12539 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12540 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12541 	} else {
12542 		struct lifreq *lifr = (struct lifreq *)if_req;
12543 
12544 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12545 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12546 	}
12547 	return (0);
12548 }
12549 
12550 /* ARGSUSED */
12551 int
12552 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12553     ip_ioctl_cmd_t *ipip, void *if_req)
12554 {
12555 
12556 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12557 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12558 	/*
12559 	 * Get the muxid saved in ill for I_PUNLINK.
12560 	 */
12561 	if (ipip->ipi_cmd_type == IF_CMD) {
12562 		struct ifreq *ifr = (struct ifreq *)if_req;
12563 
12564 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12565 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12566 	} else {
12567 		struct lifreq *lifr = (struct lifreq *)if_req;
12568 
12569 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12570 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12571 	}
12572 	return (0);
12573 }
12574 
12575 /*
12576  * Set the subnet prefix. Does not modify the broadcast address.
12577  */
12578 /* ARGSUSED */
12579 int
12580 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12581     ip_ioctl_cmd_t *ipip, void *if_req)
12582 {
12583 	int err = 0;
12584 	in6_addr_t v6addr;
12585 	in6_addr_t v6mask;
12586 	boolean_t need_up = B_FALSE;
12587 	int addrlen;
12588 
12589 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12590 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12591 
12592 	ASSERT(IAM_WRITER_IPIF(ipif));
12593 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12594 
12595 	if (ipif->ipif_isv6) {
12596 		sin6_t *sin6;
12597 
12598 		if (sin->sin_family != AF_INET6)
12599 			return (EAFNOSUPPORT);
12600 
12601 		sin6 = (sin6_t *)sin;
12602 		v6addr = sin6->sin6_addr;
12603 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12604 			return (EADDRNOTAVAIL);
12605 	} else {
12606 		ipaddr_t addr;
12607 
12608 		if (sin->sin_family != AF_INET)
12609 			return (EAFNOSUPPORT);
12610 
12611 		addr = sin->sin_addr.s_addr;
12612 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12613 			return (EADDRNOTAVAIL);
12614 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12615 		/* Add 96 bits */
12616 		addrlen += IPV6_ABITS - IP_ABITS;
12617 	}
12618 
12619 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12620 		return (EINVAL);
12621 
12622 	/* Check if bits in the address is set past the mask */
12623 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12624 		return (EINVAL);
12625 
12626 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12627 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12628 		return (0);	/* No change */
12629 
12630 	if (ipif->ipif_flags & IPIF_UP) {
12631 		/*
12632 		 * If the interface is already marked up,
12633 		 * we call ipif_down which will take care
12634 		 * of ditching any IREs that have been set
12635 		 * up based on the old interface address.
12636 		 */
12637 		err = ipif_logical_down(ipif, q, mp);
12638 		if (err == EINPROGRESS)
12639 			return (err);
12640 		ipif_down_tail(ipif);
12641 		need_up = B_TRUE;
12642 	}
12643 
12644 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12645 	return (err);
12646 }
12647 
12648 static int
12649 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12650     queue_t *q, mblk_t *mp, boolean_t need_up)
12651 {
12652 	ill_t	*ill = ipif->ipif_ill;
12653 	int	err = 0;
12654 
12655 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12656 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12657 
12658 	/* Set the new address. */
12659 	mutex_enter(&ill->ill_lock);
12660 	ipif->ipif_v6net_mask = v6mask;
12661 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12662 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12663 		    ipif->ipif_v6subnet);
12664 	}
12665 	mutex_exit(&ill->ill_lock);
12666 
12667 	if (need_up) {
12668 		/*
12669 		 * Now bring the interface back up.  If this
12670 		 * is the only IPIF for the ILL, ipif_up
12671 		 * will have to re-bind to the device, so
12672 		 * we may get back EINPROGRESS, in which
12673 		 * case, this IOCTL will get completed in
12674 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12675 		 */
12676 		err = ipif_up(ipif, q, mp);
12677 		if (err == EINPROGRESS)
12678 			return (err);
12679 	}
12680 	return (err);
12681 }
12682 
12683 /* ARGSUSED */
12684 int
12685 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12686     ip_ioctl_cmd_t *ipip, void *if_req)
12687 {
12688 	int	addrlen;
12689 	in6_addr_t v6addr;
12690 	in6_addr_t v6mask;
12691 	struct lifreq *lifr = (struct lifreq *)if_req;
12692 
12693 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12694 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12695 	ipif_down_tail(ipif);
12696 
12697 	addrlen = lifr->lifr_addrlen;
12698 	if (ipif->ipif_isv6) {
12699 		sin6_t *sin6;
12700 
12701 		sin6 = (sin6_t *)sin;
12702 		v6addr = sin6->sin6_addr;
12703 	} else {
12704 		ipaddr_t addr;
12705 
12706 		addr = sin->sin_addr.s_addr;
12707 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12708 		addrlen += IPV6_ABITS - IP_ABITS;
12709 	}
12710 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12711 
12712 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12713 }
12714 
12715 /* ARGSUSED */
12716 int
12717 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12718     ip_ioctl_cmd_t *ipip, void *if_req)
12719 {
12720 	struct lifreq *lifr = (struct lifreq *)if_req;
12721 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12722 
12723 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12724 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12725 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12726 
12727 	if (ipif->ipif_isv6) {
12728 		*sin6 = sin6_null;
12729 		sin6->sin6_family = AF_INET6;
12730 		sin6->sin6_addr = ipif->ipif_v6subnet;
12731 		lifr->lifr_addrlen =
12732 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12733 	} else {
12734 		*sin = sin_null;
12735 		sin->sin_family = AF_INET;
12736 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12737 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12738 	}
12739 	return (0);
12740 }
12741 
12742 /*
12743  * Set the IPv6 address token.
12744  */
12745 /* ARGSUSED */
12746 int
12747 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12748     ip_ioctl_cmd_t *ipi, void *if_req)
12749 {
12750 	ill_t *ill = ipif->ipif_ill;
12751 	int err;
12752 	in6_addr_t v6addr;
12753 	in6_addr_t v6mask;
12754 	boolean_t need_up = B_FALSE;
12755 	int i;
12756 	sin6_t *sin6 = (sin6_t *)sin;
12757 	struct lifreq *lifr = (struct lifreq *)if_req;
12758 	int addrlen;
12759 
12760 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12761 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12762 	ASSERT(IAM_WRITER_IPIF(ipif));
12763 
12764 	addrlen = lifr->lifr_addrlen;
12765 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12766 	if (ipif->ipif_id != 0)
12767 		return (EINVAL);
12768 
12769 	if (!ipif->ipif_isv6)
12770 		return (EINVAL);
12771 
12772 	if (addrlen > IPV6_ABITS)
12773 		return (EINVAL);
12774 
12775 	v6addr = sin6->sin6_addr;
12776 
12777 	/*
12778 	 * The length of the token is the length from the end.  To get
12779 	 * the proper mask for this, compute the mask of the bits not
12780 	 * in the token; ie. the prefix, and then xor to get the mask.
12781 	 */
12782 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12783 		return (EINVAL);
12784 	for (i = 0; i < 4; i++) {
12785 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12786 	}
12787 
12788 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12789 	    ill->ill_token_length == addrlen)
12790 		return (0);	/* No change */
12791 
12792 	if (ipif->ipif_flags & IPIF_UP) {
12793 		err = ipif_logical_down(ipif, q, mp);
12794 		if (err == EINPROGRESS)
12795 			return (err);
12796 		ipif_down_tail(ipif);
12797 		need_up = B_TRUE;
12798 	}
12799 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12800 	return (err);
12801 }
12802 
12803 static int
12804 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12805     mblk_t *mp, boolean_t need_up)
12806 {
12807 	in6_addr_t v6addr;
12808 	in6_addr_t v6mask;
12809 	ill_t	*ill = ipif->ipif_ill;
12810 	int	i;
12811 	int	err = 0;
12812 
12813 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12814 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12815 	v6addr = sin6->sin6_addr;
12816 	/*
12817 	 * The length of the token is the length from the end.  To get
12818 	 * the proper mask for this, compute the mask of the bits not
12819 	 * in the token; ie. the prefix, and then xor to get the mask.
12820 	 */
12821 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12822 	for (i = 0; i < 4; i++)
12823 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12824 
12825 	mutex_enter(&ill->ill_lock);
12826 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12827 	ill->ill_token_length = addrlen;
12828 	mutex_exit(&ill->ill_lock);
12829 
12830 	if (need_up) {
12831 		/*
12832 		 * Now bring the interface back up.  If this
12833 		 * is the only IPIF for the ILL, ipif_up
12834 		 * will have to re-bind to the device, so
12835 		 * we may get back EINPROGRESS, in which
12836 		 * case, this IOCTL will get completed in
12837 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12838 		 */
12839 		err = ipif_up(ipif, q, mp);
12840 		if (err == EINPROGRESS)
12841 			return (err);
12842 	}
12843 	return (err);
12844 }
12845 
12846 /* ARGSUSED */
12847 int
12848 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12849     ip_ioctl_cmd_t *ipi, void *if_req)
12850 {
12851 	ill_t *ill;
12852 	sin6_t *sin6 = (sin6_t *)sin;
12853 	struct lifreq *lifr = (struct lifreq *)if_req;
12854 
12855 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12856 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12857 	if (ipif->ipif_id != 0)
12858 		return (EINVAL);
12859 
12860 	ill = ipif->ipif_ill;
12861 	if (!ill->ill_isv6)
12862 		return (ENXIO);
12863 
12864 	*sin6 = sin6_null;
12865 	sin6->sin6_family = AF_INET6;
12866 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12867 	sin6->sin6_addr = ill->ill_token;
12868 	lifr->lifr_addrlen = ill->ill_token_length;
12869 	return (0);
12870 }
12871 
12872 /*
12873  * Set (hardware) link specific information that might override
12874  * what was acquired through the DL_INFO_ACK.
12875  * The logic is as follows.
12876  *
12877  * become exclusive
12878  * set CHANGING flag
12879  * change mtu on affected IREs
12880  * clear CHANGING flag
12881  *
12882  * An ire add that occurs before the CHANGING flag is set will have its mtu
12883  * changed by the ip_sioctl_lnkinfo.
12884  *
12885  * During the time the CHANGING flag is set, no new ires will be added to the
12886  * bucket, and ire add will fail (due the CHANGING flag).
12887  *
12888  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12889  * before it is added to the bucket.
12890  *
12891  * Obviously only 1 thread can set the CHANGING flag and we need to become
12892  * exclusive to set the flag.
12893  */
12894 /* ARGSUSED */
12895 int
12896 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12897     ip_ioctl_cmd_t *ipi, void *if_req)
12898 {
12899 	ill_t		*ill = ipif->ipif_ill;
12900 	ipif_t		*nipif;
12901 	int		ip_min_mtu;
12902 	boolean_t	mtu_walk = B_FALSE;
12903 	struct lifreq	*lifr = (struct lifreq *)if_req;
12904 	lif_ifinfo_req_t *lir;
12905 	ire_t		*ire;
12906 
12907 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12908 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12909 	lir = &lifr->lifr_ifinfo;
12910 	ASSERT(IAM_WRITER_IPIF(ipif));
12911 
12912 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12913 	if (ipif->ipif_id != 0)
12914 		return (EINVAL);
12915 
12916 	/* Set interface MTU. */
12917 	if (ipif->ipif_isv6)
12918 		ip_min_mtu = IPV6_MIN_MTU;
12919 	else
12920 		ip_min_mtu = IP_MIN_MTU;
12921 
12922 	/*
12923 	 * Verify values before we set anything. Allow zero to
12924 	 * mean unspecified.
12925 	 */
12926 	if (lir->lir_maxmtu != 0 &&
12927 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12928 	    lir->lir_maxmtu < ip_min_mtu))
12929 		return (EINVAL);
12930 	if (lir->lir_reachtime != 0 &&
12931 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12932 		return (EINVAL);
12933 	if (lir->lir_reachretrans != 0 &&
12934 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12935 		return (EINVAL);
12936 
12937 	mutex_enter(&ill->ill_lock);
12938 	ill->ill_state_flags |= ILL_CHANGING;
12939 	for (nipif = ill->ill_ipif; nipif != NULL;
12940 	    nipif = nipif->ipif_next) {
12941 		nipif->ipif_state_flags |= IPIF_CHANGING;
12942 	}
12943 
12944 	mutex_exit(&ill->ill_lock);
12945 
12946 	if (lir->lir_maxmtu != 0) {
12947 		ill->ill_max_mtu = lir->lir_maxmtu;
12948 		ill->ill_mtu_userspecified = 1;
12949 		mtu_walk = B_TRUE;
12950 	}
12951 
12952 	if (lir->lir_reachtime != 0)
12953 		ill->ill_reachable_time = lir->lir_reachtime;
12954 
12955 	if (lir->lir_reachretrans != 0)
12956 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12957 
12958 	ill->ill_max_hops = lir->lir_maxhops;
12959 
12960 	ill->ill_max_buf = ND_MAX_Q;
12961 
12962 	if (mtu_walk) {
12963 		/*
12964 		 * Set the MTU on all ipifs associated with this ill except
12965 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12966 		 */
12967 		for (nipif = ill->ill_ipif; nipif != NULL;
12968 		    nipif = nipif->ipif_next) {
12969 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12970 				continue;
12971 
12972 			nipif->ipif_mtu = ill->ill_max_mtu;
12973 
12974 			if (!(nipif->ipif_flags & IPIF_UP))
12975 				continue;
12976 
12977 			if (nipif->ipif_isv6)
12978 				ire = ipif_to_ire_v6(nipif);
12979 			else
12980 				ire = ipif_to_ire(nipif);
12981 			if (ire != NULL) {
12982 				ire->ire_max_frag = ipif->ipif_mtu;
12983 				ire_refrele(ire);
12984 			}
12985 
12986 			ire_walk_ill(MATCH_IRE_ILL, 0, ipif_mtu_change,
12987 			    nipif, ill);
12988 		}
12989 	}
12990 
12991 	mutex_enter(&ill->ill_lock);
12992 	for (nipif = ill->ill_ipif; nipif != NULL;
12993 	    nipif = nipif->ipif_next) {
12994 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12995 	}
12996 	ILL_UNMARK_CHANGING(ill);
12997 	mutex_exit(&ill->ill_lock);
12998 
12999 	return (0);
13000 }
13001 
13002 /* ARGSUSED */
13003 int
13004 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13005     ip_ioctl_cmd_t *ipi, void *if_req)
13006 {
13007 	struct lif_ifinfo_req *lir;
13008 	ill_t *ill = ipif->ipif_ill;
13009 
13010 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13011 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13012 	if (ipif->ipif_id != 0)
13013 		return (EINVAL);
13014 
13015 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13016 	lir->lir_maxhops = ill->ill_max_hops;
13017 	lir->lir_reachtime = ill->ill_reachable_time;
13018 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13019 	lir->lir_maxmtu = ill->ill_max_mtu;
13020 
13021 	return (0);
13022 }
13023 
13024 /*
13025  * Return best guess as to the subnet mask for the specified address.
13026  * Based on the subnet masks for all the configured interfaces.
13027  *
13028  * We end up returning a zero mask in the case of default, multicast or
13029  * experimental.
13030  */
13031 static ipaddr_t
13032 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13033 {
13034 	ipaddr_t net_mask;
13035 	ill_t	*ill;
13036 	ipif_t	*ipif;
13037 	ill_walk_context_t ctx;
13038 	ipif_t	*fallback_ipif = NULL;
13039 
13040 	net_mask = ip_net_mask(addr);
13041 	if (net_mask == 0) {
13042 		*ipifp = NULL;
13043 		return (0);
13044 	}
13045 
13046 	/* Let's check to see if this is maybe a local subnet route. */
13047 	/* this function only applies to IPv4 interfaces */
13048 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13049 	ill = ILL_START_WALK_V4(&ctx, ipst);
13050 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13051 		mutex_enter(&ill->ill_lock);
13052 		for (ipif = ill->ill_ipif; ipif != NULL;
13053 		    ipif = ipif->ipif_next) {
13054 			if (!IPIF_CAN_LOOKUP(ipif))
13055 				continue;
13056 			if (!(ipif->ipif_flags & IPIF_UP))
13057 				continue;
13058 			if ((ipif->ipif_subnet & net_mask) ==
13059 			    (addr & net_mask)) {
13060 				/*
13061 				 * Don't trust pt-pt interfaces if there are
13062 				 * other interfaces.
13063 				 */
13064 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13065 					if (fallback_ipif == NULL) {
13066 						ipif_refhold_locked(ipif);
13067 						fallback_ipif = ipif;
13068 					}
13069 					continue;
13070 				}
13071 
13072 				/*
13073 				 * Fine. Just assume the same net mask as the
13074 				 * directly attached subnet interface is using.
13075 				 */
13076 				ipif_refhold_locked(ipif);
13077 				mutex_exit(&ill->ill_lock);
13078 				rw_exit(&ipst->ips_ill_g_lock);
13079 				if (fallback_ipif != NULL)
13080 					ipif_refrele(fallback_ipif);
13081 				*ipifp = ipif;
13082 				return (ipif->ipif_net_mask);
13083 			}
13084 		}
13085 		mutex_exit(&ill->ill_lock);
13086 	}
13087 	rw_exit(&ipst->ips_ill_g_lock);
13088 
13089 	*ipifp = fallback_ipif;
13090 	return ((fallback_ipif != NULL) ?
13091 	    fallback_ipif->ipif_net_mask : net_mask);
13092 }
13093 
13094 /*
13095  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13096  */
13097 static void
13098 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13099 {
13100 	IOCP	iocp;
13101 	ipft_t	*ipft;
13102 	ipllc_t	*ipllc;
13103 	mblk_t	*mp1;
13104 	cred_t	*cr;
13105 	int	error = 0;
13106 	conn_t	*connp;
13107 
13108 	ip1dbg(("ip_wput_ioctl"));
13109 	iocp = (IOCP)mp->b_rptr;
13110 	mp1 = mp->b_cont;
13111 	if (mp1 == NULL) {
13112 		iocp->ioc_error = EINVAL;
13113 		mp->b_datap->db_type = M_IOCNAK;
13114 		iocp->ioc_count = 0;
13115 		qreply(q, mp);
13116 		return;
13117 	}
13118 
13119 	/*
13120 	 * These IOCTLs provide various control capabilities to
13121 	 * upstream agents such as ULPs and processes.	There
13122 	 * are currently two such IOCTLs implemented.  They
13123 	 * are used by TCP to provide update information for
13124 	 * existing IREs and to forcibly delete an IRE for a
13125 	 * host that is not responding, thereby forcing an
13126 	 * attempt at a new route.
13127 	 */
13128 	iocp->ioc_error = EINVAL;
13129 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13130 		goto done;
13131 
13132 	ipllc = (ipllc_t *)mp1->b_rptr;
13133 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13134 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13135 			break;
13136 	}
13137 	/*
13138 	 * prefer credential from mblk over ioctl;
13139 	 * see ip_sioctl_copyin_setup
13140 	 */
13141 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13142 
13143 	/*
13144 	 * Refhold the conn in case the request gets queued up in some lookup
13145 	 */
13146 	ASSERT(CONN_Q(q));
13147 	connp = Q_TO_CONN(q);
13148 	CONN_INC_REF(connp);
13149 	if (ipft->ipft_pfi &&
13150 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13151 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13152 		error = (*ipft->ipft_pfi)(q,
13153 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13154 	}
13155 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13156 		/*
13157 		 * CONN_OPER_PENDING_DONE happens in the function called
13158 		 * through ipft_pfi above.
13159 		 */
13160 		return;
13161 	}
13162 
13163 	CONN_OPER_PENDING_DONE(connp);
13164 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13165 		freemsg(mp);
13166 		return;
13167 	}
13168 	iocp->ioc_error = error;
13169 
13170 done:
13171 	mp->b_datap->db_type = M_IOCACK;
13172 	if (iocp->ioc_error)
13173 		iocp->ioc_count = 0;
13174 	qreply(q, mp);
13175 }
13176 
13177 /*
13178  * Lookup an ipif using the sequence id (ipif_seqid)
13179  */
13180 ipif_t *
13181 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13182 {
13183 	ipif_t *ipif;
13184 
13185 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13186 
13187 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13188 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13189 			return (ipif);
13190 	}
13191 	return (NULL);
13192 }
13193 
13194 /*
13195  * Assign a unique id for the ipif. This is used later when we send
13196  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13197  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13198  * IRE is added, we verify that ipif has not disappeared.
13199  */
13200 
13201 static void
13202 ipif_assign_seqid(ipif_t *ipif)
13203 {
13204 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13205 
13206 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13207 }
13208 
13209 /*
13210  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13211  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13212  * be inserted into the first space available in the list. The value of
13213  * ipif_id will then be set to the appropriate value for its position.
13214  */
13215 static int
13216 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13217 {
13218 	ill_t *ill;
13219 	ipif_t *tipif;
13220 	ipif_t **tipifp;
13221 	int id;
13222 	ip_stack_t	*ipst;
13223 
13224 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13225 	    IAM_WRITER_IPIF(ipif));
13226 
13227 	ill = ipif->ipif_ill;
13228 	ASSERT(ill != NULL);
13229 	ipst = ill->ill_ipst;
13230 
13231 	/*
13232 	 * In the case of lo0:0 we already hold the ill_g_lock.
13233 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13234 	 * ipif_insert. Another such caller is ipif_move.
13235 	 */
13236 	if (acquire_g_lock)
13237 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13238 	if (acquire_ill_lock)
13239 		mutex_enter(&ill->ill_lock);
13240 	id = ipif->ipif_id;
13241 	tipifp = &(ill->ill_ipif);
13242 	if (id == -1) {	/* need to find a real id */
13243 		id = 0;
13244 		while ((tipif = *tipifp) != NULL) {
13245 			ASSERT(tipif->ipif_id >= id);
13246 			if (tipif->ipif_id != id)
13247 				break; /* non-consecutive id */
13248 			id++;
13249 			tipifp = &(tipif->ipif_next);
13250 		}
13251 		/* limit number of logical interfaces */
13252 		if (id >= ipst->ips_ip_addrs_per_if) {
13253 			if (acquire_ill_lock)
13254 				mutex_exit(&ill->ill_lock);
13255 			if (acquire_g_lock)
13256 				rw_exit(&ipst->ips_ill_g_lock);
13257 			return (-1);
13258 		}
13259 		ipif->ipif_id = id; /* assign new id */
13260 	} else if (id < ipst->ips_ip_addrs_per_if) {
13261 		/* we have a real id; insert ipif in the right place */
13262 		while ((tipif = *tipifp) != NULL) {
13263 			ASSERT(tipif->ipif_id != id);
13264 			if (tipif->ipif_id > id)
13265 				break; /* found correct location */
13266 			tipifp = &(tipif->ipif_next);
13267 		}
13268 	} else {
13269 		if (acquire_ill_lock)
13270 			mutex_exit(&ill->ill_lock);
13271 		if (acquire_g_lock)
13272 			rw_exit(&ipst->ips_ill_g_lock);
13273 		return (-1);
13274 	}
13275 
13276 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13277 
13278 	ipif->ipif_next = tipif;
13279 	*tipifp = ipif;
13280 	if (acquire_ill_lock)
13281 		mutex_exit(&ill->ill_lock);
13282 	if (acquire_g_lock)
13283 		rw_exit(&ipst->ips_ill_g_lock);
13284 	return (0);
13285 }
13286 
13287 static void
13288 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13289 {
13290 	ipif_t	**ipifp;
13291 	ill_t	*ill = ipif->ipif_ill;
13292 
13293 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13294 	if (acquire_ill_lock)
13295 		mutex_enter(&ill->ill_lock);
13296 	else
13297 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13298 
13299 	ipifp = &ill->ill_ipif;
13300 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13301 		if (*ipifp == ipif) {
13302 			*ipifp = ipif->ipif_next;
13303 			break;
13304 		}
13305 	}
13306 
13307 	if (acquire_ill_lock)
13308 		mutex_exit(&ill->ill_lock);
13309 }
13310 
13311 /*
13312  * Allocate and initialize a new interface control structure.  (Always
13313  * called as writer.)
13314  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13315  * is not part of the global linked list of ills. ipif_seqid is unique
13316  * in the system and to preserve the uniqueness, it is assigned only
13317  * when ill becomes part of the global list. At that point ill will
13318  * have a name. If it doesn't get assigned here, it will get assigned
13319  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13320  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13321  * the interface flags or any other information from the DL_INFO_ACK for
13322  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13323  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13324  * second DL_INFO_ACK comes in from the driver.
13325  */
13326 static ipif_t *
13327 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13328 {
13329 	ipif_t	*ipif;
13330 	phyint_t *phyi;
13331 
13332 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13333 	    ill->ill_name, id, (void *)ill));
13334 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13335 
13336 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13337 		return (NULL);
13338 	*ipif = ipif_zero;	/* start clean */
13339 
13340 	ipif->ipif_ill = ill;
13341 	ipif->ipif_id = id;	/* could be -1 */
13342 	/*
13343 	 * Inherit the zoneid from the ill; for the shared stack instance
13344 	 * this is always the global zone
13345 	 */
13346 	ipif->ipif_zoneid = ill->ill_zoneid;
13347 
13348 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13349 
13350 	ipif->ipif_refcnt = 0;
13351 	ipif->ipif_saved_ire_cnt = 0;
13352 
13353 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13354 		mi_free(ipif);
13355 		return (NULL);
13356 	}
13357 	/* -1 id should have been replaced by real id */
13358 	id = ipif->ipif_id;
13359 	ASSERT(id >= 0);
13360 
13361 	if (ill->ill_name[0] != '\0')
13362 		ipif_assign_seqid(ipif);
13363 
13364 	/*
13365 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13366 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13367 	 * ioctl sets ipif_orig_ipifid to zero.
13368 	 */
13369 	ipif->ipif_orig_ipifid = id;
13370 
13371 	/*
13372 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13373 	 * The ipif is still not up and can't be looked up until the
13374 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13375 	 */
13376 	mutex_enter(&ill->ill_lock);
13377 	mutex_enter(&ill->ill_phyint->phyint_lock);
13378 	/*
13379 	 * Set the running flag when logical interface zero is created.
13380 	 * For subsequent logical interfaces, a DLPI link down
13381 	 * notification message may have cleared the running flag to
13382 	 * indicate the link is down, so we shouldn't just blindly set it.
13383 	 */
13384 	if (id == 0)
13385 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13386 	ipif->ipif_ire_type = ire_type;
13387 	phyi = ill->ill_phyint;
13388 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13389 
13390 	if (ipif->ipif_isv6) {
13391 		ill->ill_flags |= ILLF_IPV6;
13392 	} else {
13393 		ipaddr_t inaddr_any = INADDR_ANY;
13394 
13395 		ill->ill_flags |= ILLF_IPV4;
13396 
13397 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13398 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13399 		    &ipif->ipif_v6lcl_addr);
13400 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13401 		    &ipif->ipif_v6src_addr);
13402 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13403 		    &ipif->ipif_v6subnet);
13404 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13405 		    &ipif->ipif_v6net_mask);
13406 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13407 		    &ipif->ipif_v6brd_addr);
13408 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13409 		    &ipif->ipif_v6pp_dst_addr);
13410 	}
13411 
13412 	/*
13413 	 * Don't set the interface flags etc. now, will do it in
13414 	 * ip_ll_subnet_defaults.
13415 	 */
13416 	if (!initialize) {
13417 		mutex_exit(&ill->ill_lock);
13418 		mutex_exit(&ill->ill_phyint->phyint_lock);
13419 		return (ipif);
13420 	}
13421 	ipif->ipif_mtu = ill->ill_max_mtu;
13422 
13423 	if (ill->ill_bcast_addr_length != 0) {
13424 		/*
13425 		 * Later detect lack of DLPI driver multicast
13426 		 * capability by catching DL_ENABMULTI errors in
13427 		 * ip_rput_dlpi.
13428 		 */
13429 		ill->ill_flags |= ILLF_MULTICAST;
13430 		if (!ipif->ipif_isv6)
13431 			ipif->ipif_flags |= IPIF_BROADCAST;
13432 	} else {
13433 		if (ill->ill_net_type != IRE_LOOPBACK) {
13434 			if (ipif->ipif_isv6)
13435 				/*
13436 				 * Note: xresolv interfaces will eventually need
13437 				 * NOARP set here as well, but that will require
13438 				 * those external resolvers to have some
13439 				 * knowledge of that flag and act appropriately.
13440 				 * Not to be changed at present.
13441 				 */
13442 				ill->ill_flags |= ILLF_NONUD;
13443 			else
13444 				ill->ill_flags |= ILLF_NOARP;
13445 		}
13446 		if (ill->ill_phys_addr_length == 0) {
13447 			if (ill->ill_media &&
13448 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13449 				ipif->ipif_flags |= IPIF_NOXMIT;
13450 				phyi->phyint_flags |= PHYI_VIRTUAL;
13451 			} else {
13452 				/* pt-pt supports multicast. */
13453 				ill->ill_flags |= ILLF_MULTICAST;
13454 				if (ill->ill_net_type == IRE_LOOPBACK) {
13455 					phyi->phyint_flags |=
13456 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13457 				} else {
13458 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13459 				}
13460 			}
13461 		}
13462 	}
13463 	mutex_exit(&ill->ill_lock);
13464 	mutex_exit(&ill->ill_phyint->phyint_lock);
13465 	return (ipif);
13466 }
13467 
13468 /*
13469  * If appropriate, send a message up to the resolver delete the entry
13470  * for the address of this interface which is going out of business.
13471  * (Always called as writer).
13472  *
13473  * NOTE : We need to check for NULL mps as some of the fields are
13474  *	  initialized only for some interface types. See ipif_resolver_up()
13475  *	  for details.
13476  */
13477 void
13478 ipif_arp_down(ipif_t *ipif)
13479 {
13480 	mblk_t	*mp;
13481 	ill_t	*ill = ipif->ipif_ill;
13482 
13483 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13484 	ASSERT(IAM_WRITER_IPIF(ipif));
13485 
13486 	/* Delete the mapping for the local address */
13487 	mp = ipif->ipif_arp_del_mp;
13488 	if (mp != NULL) {
13489 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13490 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13491 		putnext(ill->ill_rq, mp);
13492 		ipif->ipif_arp_del_mp = NULL;
13493 	}
13494 
13495 	/*
13496 	 * If this is the last ipif that is going down and there are no
13497 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13498 	 * clean up ARP completely.
13499 	 */
13500 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13501 
13502 		/* Send up AR_INTERFACE_DOWN message */
13503 		mp = ill->ill_arp_down_mp;
13504 		if (mp != NULL) {
13505 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13506 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13507 			    ipif->ipif_id));
13508 			putnext(ill->ill_rq, mp);
13509 			ill->ill_arp_down_mp = NULL;
13510 		}
13511 
13512 		/* Tell ARP to delete the multicast mappings */
13513 		mp = ill->ill_arp_del_mapping_mp;
13514 		if (mp != NULL) {
13515 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13516 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13517 			    ipif->ipif_id));
13518 			putnext(ill->ill_rq, mp);
13519 			ill->ill_arp_del_mapping_mp = NULL;
13520 		}
13521 	}
13522 }
13523 
13524 /*
13525  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13526  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13527  * that it wants the add_mp allocated in this function to be returned
13528  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13529  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13530  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13531  * as it does a ipif_arp_down after calling this function - which will
13532  * remove what we add here.
13533  *
13534  * Returns -1 on failures and 0 on success.
13535  */
13536 int
13537 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13538 {
13539 	mblk_t	*del_mp = NULL;
13540 	mblk_t *add_mp = NULL;
13541 	mblk_t *mp;
13542 	ill_t	*ill = ipif->ipif_ill;
13543 	phyint_t *phyi = ill->ill_phyint;
13544 	ipaddr_t addr, mask, extract_mask = 0;
13545 	arma_t	*arma;
13546 	uint8_t *maddr, *bphys_addr;
13547 	uint32_t hw_start;
13548 	dl_unitdata_req_t *dlur;
13549 
13550 	ASSERT(IAM_WRITER_IPIF(ipif));
13551 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13552 		return (0);
13553 
13554 	/*
13555 	 * Delete the existing mapping from ARP. Normally ipif_down
13556 	 * -> ipif_arp_down should send this up to ARP. The only
13557 	 * reason we would find this when we are switching from
13558 	 * Multicast to Broadcast where we did not do a down.
13559 	 */
13560 	mp = ill->ill_arp_del_mapping_mp;
13561 	if (mp != NULL) {
13562 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13563 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13564 		putnext(ill->ill_rq, mp);
13565 		ill->ill_arp_del_mapping_mp = NULL;
13566 	}
13567 
13568 	if (arp_add_mapping_mp != NULL)
13569 		*arp_add_mapping_mp = NULL;
13570 
13571 	/*
13572 	 * Check that the address is not to long for the constant
13573 	 * length reserved in the template arma_t.
13574 	 */
13575 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13576 		return (-1);
13577 
13578 	/* Add mapping mblk */
13579 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13580 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13581 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13582 	    (caddr_t)&addr);
13583 	if (add_mp == NULL)
13584 		return (-1);
13585 	arma = (arma_t *)add_mp->b_rptr;
13586 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13587 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13588 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13589 
13590 	/*
13591 	 * Determine the broadcast address.
13592 	 */
13593 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13594 	if (ill->ill_sap_length < 0)
13595 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13596 	else
13597 		bphys_addr = (uchar_t *)dlur +
13598 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13599 	/*
13600 	 * Check PHYI_MULTI_BCAST and length of physical
13601 	 * address to determine if we use the mapping or the
13602 	 * broadcast address.
13603 	 */
13604 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13605 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13606 		    bphys_addr, maddr, &hw_start, &extract_mask))
13607 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13608 
13609 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13610 	    (ill->ill_flags & ILLF_MULTICAST)) {
13611 		/* Make sure this will not match the "exact" entry. */
13612 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13613 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13614 		    (caddr_t)&addr);
13615 		if (del_mp == NULL) {
13616 			freemsg(add_mp);
13617 			return (-1);
13618 		}
13619 		bcopy(&extract_mask, (char *)arma +
13620 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13621 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13622 			/* Use link-layer broadcast address for MULTI_BCAST */
13623 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13624 			ip2dbg(("ipif_arp_setup_multicast: adding"
13625 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13626 		} else {
13627 			arma->arma_hw_mapping_start = hw_start;
13628 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13629 			    " ARP setup for %s\n", ill->ill_name));
13630 		}
13631 	} else {
13632 		freemsg(add_mp);
13633 		ASSERT(del_mp == NULL);
13634 		/* It is neither MULTICAST nor MULTI_BCAST */
13635 		return (0);
13636 	}
13637 	ASSERT(add_mp != NULL && del_mp != NULL);
13638 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13639 	ill->ill_arp_del_mapping_mp = del_mp;
13640 	if (arp_add_mapping_mp != NULL) {
13641 		/* The caller just wants the mblks allocated */
13642 		*arp_add_mapping_mp = add_mp;
13643 	} else {
13644 		/* The caller wants us to send it to arp */
13645 		putnext(ill->ill_rq, add_mp);
13646 	}
13647 	return (0);
13648 }
13649 
13650 /*
13651  * Get the resolver set up for a new interface address.
13652  * (Always called as writer.)
13653  * Called both for IPv4 and IPv6 interfaces,
13654  * though it only sets up the resolver for v6
13655  * if it's an xresolv interface (one using an external resolver).
13656  * Honors ILLF_NOARP.
13657  * The enumerated value res_act is used to tune the behavior.
13658  * If set to Res_act_initial, then we set up all the resolver
13659  * structures for a new interface.  If set to Res_act_move, then
13660  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13661  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13662  * asynchronous hardware address change notification.  If set to
13663  * Res_act_defend, then we tell ARP that it needs to send a single
13664  * gratuitous message in defense of the address.
13665  * Returns error on failure.
13666  */
13667 int
13668 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13669 {
13670 	caddr_t	addr;
13671 	mblk_t	*arp_up_mp = NULL;
13672 	mblk_t	*arp_down_mp = NULL;
13673 	mblk_t	*arp_add_mp = NULL;
13674 	mblk_t	*arp_del_mp = NULL;
13675 	mblk_t	*arp_add_mapping_mp = NULL;
13676 	mblk_t	*arp_del_mapping_mp = NULL;
13677 	ill_t	*ill = ipif->ipif_ill;
13678 	uchar_t	*area_p = NULL;
13679 	uchar_t	*ared_p = NULL;
13680 	int	err = ENOMEM;
13681 	boolean_t was_dup;
13682 
13683 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13684 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13685 	ASSERT(IAM_WRITER_IPIF(ipif));
13686 
13687 	was_dup = B_FALSE;
13688 	if (res_act == Res_act_initial) {
13689 		ipif->ipif_addr_ready = 0;
13690 		/*
13691 		 * We're bringing an interface up here.  There's no way that we
13692 		 * should need to shut down ARP now.
13693 		 */
13694 		mutex_enter(&ill->ill_lock);
13695 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13696 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13697 			ill->ill_ipif_dup_count--;
13698 			was_dup = B_TRUE;
13699 		}
13700 		mutex_exit(&ill->ill_lock);
13701 	}
13702 	if (ipif->ipif_recovery_id != 0)
13703 		(void) untimeout(ipif->ipif_recovery_id);
13704 	ipif->ipif_recovery_id = 0;
13705 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13706 		ipif->ipif_addr_ready = 1;
13707 		return (0);
13708 	}
13709 	/* NDP will set the ipif_addr_ready flag when it's ready */
13710 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13711 		return (0);
13712 
13713 	if (ill->ill_isv6) {
13714 		/*
13715 		 * External resolver for IPv6
13716 		 */
13717 		ASSERT(res_act == Res_act_initial);
13718 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13719 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13720 			area_p = (uchar_t *)&ip6_area_template;
13721 			ared_p = (uchar_t *)&ip6_ared_template;
13722 		}
13723 	} else {
13724 		/*
13725 		 * IPv4 arp case. If the ARP stream has already started
13726 		 * closing, fail this request for ARP bringup. Else
13727 		 * record the fact that an ARP bringup is pending.
13728 		 */
13729 		mutex_enter(&ill->ill_lock);
13730 		if (ill->ill_arp_closing) {
13731 			mutex_exit(&ill->ill_lock);
13732 			err = EINVAL;
13733 			goto failed;
13734 		} else {
13735 			if (ill->ill_ipif_up_count == 0 &&
13736 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13737 				ill->ill_arp_bringup_pending = 1;
13738 			mutex_exit(&ill->ill_lock);
13739 		}
13740 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13741 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13742 			area_p = (uchar_t *)&ip_area_template;
13743 			ared_p = (uchar_t *)&ip_ared_template;
13744 		}
13745 	}
13746 
13747 	/*
13748 	 * Add an entry for the local address in ARP only if it
13749 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13750 	 */
13751 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13752 		area_t *area;
13753 
13754 		/* Now ask ARP to publish our address. */
13755 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13756 		if (arp_add_mp == NULL)
13757 			goto failed;
13758 		area = (area_t *)arp_add_mp->b_rptr;
13759 		if (res_act != Res_act_initial) {
13760 			/*
13761 			 * Copy the new hardware address and length into
13762 			 * arp_add_mp to be sent to ARP.
13763 			 */
13764 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13765 			bcopy(ill->ill_phys_addr,
13766 			    ((char *)area + area->area_hw_addr_offset),
13767 			    area->area_hw_addr_length);
13768 		}
13769 
13770 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13771 		    ACE_F_MYADDR;
13772 
13773 		if (res_act == Res_act_defend) {
13774 			area->area_flags |= ACE_F_DEFEND;
13775 			/*
13776 			 * If we're just defending our address now, then
13777 			 * there's no need to set up ARP multicast mappings.
13778 			 * The publish command is enough.
13779 			 */
13780 			goto done;
13781 		}
13782 
13783 		if (res_act != Res_act_initial)
13784 			goto arp_setup_multicast;
13785 
13786 		/*
13787 		 * Allocate an ARP deletion message so we know we can tell ARP
13788 		 * when the interface goes down.
13789 		 */
13790 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13791 		if (arp_del_mp == NULL)
13792 			goto failed;
13793 
13794 	} else {
13795 		if (res_act != Res_act_initial)
13796 			goto done;
13797 	}
13798 	/*
13799 	 * Need to bring up ARP or setup multicast mapping only
13800 	 * when the first interface is coming UP.
13801 	 */
13802 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13803 	    was_dup) {
13804 		goto done;
13805 	}
13806 
13807 	/*
13808 	 * Allocate an ARP down message (to be saved) and an ARP up
13809 	 * message.
13810 	 */
13811 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13812 	if (arp_down_mp == NULL)
13813 		goto failed;
13814 
13815 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13816 	if (arp_up_mp == NULL)
13817 		goto failed;
13818 
13819 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13820 		goto done;
13821 
13822 arp_setup_multicast:
13823 	/*
13824 	 * Setup the multicast mappings. This function initializes
13825 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13826 	 * IPv6.
13827 	 */
13828 	if (!ill->ill_isv6) {
13829 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13830 		if (err != 0)
13831 			goto failed;
13832 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13833 		ASSERT(arp_add_mapping_mp != NULL);
13834 	}
13835 
13836 done:
13837 	if (arp_del_mp != NULL) {
13838 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13839 		ipif->ipif_arp_del_mp = arp_del_mp;
13840 	}
13841 	if (arp_down_mp != NULL) {
13842 		ASSERT(ill->ill_arp_down_mp == NULL);
13843 		ill->ill_arp_down_mp = arp_down_mp;
13844 	}
13845 	if (arp_del_mapping_mp != NULL) {
13846 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13847 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13848 	}
13849 	if (arp_up_mp != NULL) {
13850 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13851 		    ill->ill_name, ipif->ipif_id));
13852 		putnext(ill->ill_rq, arp_up_mp);
13853 	}
13854 	if (arp_add_mp != NULL) {
13855 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13856 		    ill->ill_name, ipif->ipif_id));
13857 		/*
13858 		 * If it's an extended ARP implementation, then we'll wait to
13859 		 * hear that DAD has finished before using the interface.
13860 		 */
13861 		if (!ill->ill_arp_extend)
13862 			ipif->ipif_addr_ready = 1;
13863 		putnext(ill->ill_rq, arp_add_mp);
13864 	} else {
13865 		ipif->ipif_addr_ready = 1;
13866 	}
13867 	if (arp_add_mapping_mp != NULL) {
13868 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13869 		    ill->ill_name, ipif->ipif_id));
13870 		putnext(ill->ill_rq, arp_add_mapping_mp);
13871 	}
13872 	if (res_act != Res_act_initial)
13873 		return (0);
13874 
13875 	if (ill->ill_flags & ILLF_NOARP)
13876 		err = ill_arp_off(ill);
13877 	else
13878 		err = ill_arp_on(ill);
13879 	if (err != 0) {
13880 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13881 		freemsg(ipif->ipif_arp_del_mp);
13882 		freemsg(ill->ill_arp_down_mp);
13883 		freemsg(ill->ill_arp_del_mapping_mp);
13884 		ipif->ipif_arp_del_mp = NULL;
13885 		ill->ill_arp_down_mp = NULL;
13886 		ill->ill_arp_del_mapping_mp = NULL;
13887 		return (err);
13888 	}
13889 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13890 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13891 
13892 failed:
13893 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13894 	freemsg(arp_add_mp);
13895 	freemsg(arp_del_mp);
13896 	freemsg(arp_add_mapping_mp);
13897 	freemsg(arp_up_mp);
13898 	freemsg(arp_down_mp);
13899 	ill->ill_arp_bringup_pending = 0;
13900 	return (err);
13901 }
13902 
13903 /*
13904  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13905  * just gone back up.
13906  */
13907 static void
13908 ipif_arp_start_dad(ipif_t *ipif)
13909 {
13910 	ill_t *ill = ipif->ipif_ill;
13911 	mblk_t *arp_add_mp;
13912 	area_t *area;
13913 
13914 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13915 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13916 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13917 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13918 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13919 		/*
13920 		 * If we can't contact ARP for some reason, that's not really a
13921 		 * problem.  Just send out the routing socket notification that
13922 		 * DAD completion would have done, and continue.
13923 		 */
13924 		ipif_mask_reply(ipif);
13925 		ip_rts_ifmsg(ipif);
13926 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13927 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13928 		ipif->ipif_addr_ready = 1;
13929 		return;
13930 	}
13931 
13932 	/* Setting the 'unverified' flag restarts DAD */
13933 	area = (area_t *)arp_add_mp->b_rptr;
13934 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13935 	    ACE_F_UNVERIFIED;
13936 	putnext(ill->ill_rq, arp_add_mp);
13937 }
13938 
13939 static void
13940 ipif_ndp_start_dad(ipif_t *ipif)
13941 {
13942 	nce_t *nce;
13943 
13944 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13945 	if (nce == NULL)
13946 		return;
13947 
13948 	if (!ndp_restart_dad(nce)) {
13949 		/*
13950 		 * If we can't restart DAD for some reason, that's not really a
13951 		 * problem.  Just send out the routing socket notification that
13952 		 * DAD completion would have done, and continue.
13953 		 */
13954 		ip_rts_ifmsg(ipif);
13955 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13956 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13957 		ipif->ipif_addr_ready = 1;
13958 	}
13959 	NCE_REFRELE(nce);
13960 }
13961 
13962 /*
13963  * Restart duplicate address detection on all interfaces on the given ill.
13964  *
13965  * This is called when an interface transitions from down to up
13966  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13967  *
13968  * Note that since the underlying physical link has transitioned, we must cause
13969  * at least one routing socket message to be sent here, either via DAD
13970  * completion or just by default on the first ipif.  (If we don't do this, then
13971  * in.mpathd will see long delays when doing link-based failure recovery.)
13972  */
13973 void
13974 ill_restart_dad(ill_t *ill, boolean_t went_up)
13975 {
13976 	ipif_t *ipif;
13977 
13978 	if (ill == NULL)
13979 		return;
13980 
13981 	/*
13982 	 * If layer two doesn't support duplicate address detection, then just
13983 	 * send the routing socket message now and be done with it.
13984 	 */
13985 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13986 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13987 		ip_rts_ifmsg(ill->ill_ipif);
13988 		return;
13989 	}
13990 
13991 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13992 		if (went_up) {
13993 			if (ipif->ipif_flags & IPIF_UP) {
13994 				if (ill->ill_isv6)
13995 					ipif_ndp_start_dad(ipif);
13996 				else
13997 					ipif_arp_start_dad(ipif);
13998 			} else if (ill->ill_isv6 &&
13999 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14000 				/*
14001 				 * For IPv4, the ARP module itself will
14002 				 * automatically start the DAD process when it
14003 				 * sees DL_NOTE_LINK_UP.  We respond to the
14004 				 * AR_CN_READY at the completion of that task.
14005 				 * For IPv6, we must kick off the bring-up
14006 				 * process now.
14007 				 */
14008 				ndp_do_recovery(ipif);
14009 			} else {
14010 				/*
14011 				 * Unfortunately, the first ipif is "special"
14012 				 * and represents the underlying ill in the
14013 				 * routing socket messages.  Thus, when this
14014 				 * one ipif is down, we must still notify so
14015 				 * that the user knows the IFF_RUNNING status
14016 				 * change.  (If the first ipif is up, then
14017 				 * we'll handle eventual routing socket
14018 				 * notification via DAD completion.)
14019 				 */
14020 				if (ipif == ill->ill_ipif)
14021 					ip_rts_ifmsg(ill->ill_ipif);
14022 			}
14023 		} else {
14024 			/*
14025 			 * After link down, we'll need to send a new routing
14026 			 * message when the link comes back, so clear
14027 			 * ipif_addr_ready.
14028 			 */
14029 			ipif->ipif_addr_ready = 0;
14030 		}
14031 	}
14032 
14033 	/*
14034 	 * If we've torn down links, then notify the user right away.
14035 	 */
14036 	if (!went_up)
14037 		ip_rts_ifmsg(ill->ill_ipif);
14038 }
14039 
14040 /*
14041  * Wakeup all threads waiting to enter the ipsq, and sleeping
14042  * on any of the ills in this ipsq. The ill_lock of the ill
14043  * must be held so that waiters don't miss wakeups
14044  */
14045 static void
14046 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14047 {
14048 	phyint_t *phyint;
14049 
14050 	phyint = ipsq->ipsq_phyint_list;
14051 	while (phyint != NULL) {
14052 		if (phyint->phyint_illv4) {
14053 			if (!caller_holds_lock)
14054 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14055 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14056 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14057 			if (!caller_holds_lock)
14058 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14059 		}
14060 		if (phyint->phyint_illv6) {
14061 			if (!caller_holds_lock)
14062 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14063 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14064 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14065 			if (!caller_holds_lock)
14066 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14067 		}
14068 		phyint = phyint->phyint_ipsq_next;
14069 	}
14070 }
14071 
14072 static ipsq_t *
14073 ipsq_create(char *groupname, ip_stack_t *ipst)
14074 {
14075 	ipsq_t	*ipsq;
14076 
14077 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14078 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14079 	if (ipsq == NULL) {
14080 		return (NULL);
14081 	}
14082 
14083 	if (groupname != NULL)
14084 		(void) strcpy(ipsq->ipsq_name, groupname);
14085 	else
14086 		ipsq->ipsq_name[0] = '\0';
14087 
14088 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14089 	ipsq->ipsq_flags |= IPSQ_GROUP;
14090 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14091 	ipst->ips_ipsq_g_head = ipsq;
14092 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14093 	return (ipsq);
14094 }
14095 
14096 /*
14097  * Return an ipsq correspoding to the groupname. If 'create' is true
14098  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14099  * uniquely with an IPMP group. However during IPMP groupname operations,
14100  * multiple IPMP groups may be associated with a single ipsq. But no
14101  * IPMP group can be associated with more than 1 ipsq at any time.
14102  * For example
14103  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14104  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14105  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14106  *
14107  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14108  * status shown below during the execution of the above command.
14109  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14110  *
14111  * After the completion of the above groupname command we return to the stable
14112  * state shown below.
14113  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14114  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14115  *
14116  * Because of the above, we don't search based on the ipsq_name since that
14117  * would miss the correct ipsq during certain windows as shown above.
14118  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14119  * natural state.
14120  */
14121 static ipsq_t *
14122 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14123     ip_stack_t *ipst)
14124 {
14125 	ipsq_t	*ipsq;
14126 	int	group_len;
14127 	phyint_t *phyint;
14128 
14129 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14130 
14131 	group_len = strlen(groupname);
14132 	ASSERT(group_len != 0);
14133 	group_len++;
14134 
14135 	for (ipsq = ipst->ips_ipsq_g_head;
14136 	    ipsq != NULL;
14137 	    ipsq = ipsq->ipsq_next) {
14138 		/*
14139 		 * When an ipsq is being split, and ill_split_ipsq
14140 		 * calls this function, we exclude it from being considered.
14141 		 */
14142 		if (ipsq == exclude_ipsq)
14143 			continue;
14144 
14145 		/*
14146 		 * Compare against the ipsq_name. The groupname change happens
14147 		 * in 2 phases. The 1st phase merges the from group into
14148 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14149 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14150 		 * ipsq_name. At this point the phyint_groupname has not been
14151 		 * updated.
14152 		 */
14153 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14154 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14155 			/*
14156 			 * Verify that an ipmp groupname is exactly
14157 			 * part of 1 ipsq and is not found in any other
14158 			 * ipsq.
14159 			 */
14160 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14161 			    NULL);
14162 			return (ipsq);
14163 		}
14164 
14165 		/*
14166 		 * Comparison against ipsq_name alone is not sufficient.
14167 		 * In the case when groups are currently being
14168 		 * merged, the ipsq could hold other IPMP groups temporarily.
14169 		 * so we walk the phyint list and compare against the
14170 		 * phyint_groupname as well.
14171 		 */
14172 		phyint = ipsq->ipsq_phyint_list;
14173 		while (phyint != NULL) {
14174 			if ((group_len == phyint->phyint_groupname_len) &&
14175 			    (bcmp(phyint->phyint_groupname, groupname,
14176 			    group_len) == 0)) {
14177 				/*
14178 				 * Verify that an ipmp groupname is exactly
14179 				 * part of 1 ipsq and is not found in any other
14180 				 * ipsq.
14181 				 */
14182 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14183 				    ipst) == NULL);
14184 				return (ipsq);
14185 			}
14186 			phyint = phyint->phyint_ipsq_next;
14187 		}
14188 	}
14189 	if (create)
14190 		ipsq = ipsq_create(groupname, ipst);
14191 	return (ipsq);
14192 }
14193 
14194 static void
14195 ipsq_delete(ipsq_t *ipsq)
14196 {
14197 	ipsq_t *nipsq;
14198 	ipsq_t *pipsq = NULL;
14199 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14200 
14201 	/*
14202 	 * We don't hold the ipsq lock, but we are sure no new
14203 	 * messages can land up, since the ipsq_refs is zero.
14204 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14205 	 * is associated with this ipsq. (Lookups are based on ill_name
14206 	 * or phyint_groupname)
14207 	 */
14208 	ASSERT(ipsq->ipsq_refs == 0);
14209 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14210 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14211 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14212 		/*
14213 		 * This is not the ipsq of an IPMP group.
14214 		 */
14215 		ipsq->ipsq_ipst = NULL;
14216 		kmem_free(ipsq, sizeof (ipsq_t));
14217 		return;
14218 	}
14219 
14220 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14221 
14222 	/*
14223 	 * Locate the ipsq  before we can remove it from
14224 	 * the singly linked list of ipsq's.
14225 	 */
14226 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14227 	    nipsq = nipsq->ipsq_next) {
14228 		if (nipsq == ipsq) {
14229 			break;
14230 		}
14231 		pipsq = nipsq;
14232 	}
14233 
14234 	ASSERT(nipsq == ipsq);
14235 
14236 	/* unlink ipsq from the list */
14237 	if (pipsq != NULL)
14238 		pipsq->ipsq_next = ipsq->ipsq_next;
14239 	else
14240 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14241 	ipsq->ipsq_ipst = NULL;
14242 	kmem_free(ipsq, sizeof (ipsq_t));
14243 	rw_exit(&ipst->ips_ill_g_lock);
14244 }
14245 
14246 static void
14247 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14248     queue_t *q)
14249 {
14250 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14251 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14252 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14253 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14254 	ASSERT(current_mp != NULL);
14255 
14256 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14257 	    NEW_OP, NULL);
14258 
14259 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14260 	    new_ipsq->ipsq_xopq_mphead != NULL);
14261 
14262 	/*
14263 	 * move from old ipsq to the new ipsq.
14264 	 */
14265 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14266 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14267 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14268 
14269 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14270 }
14271 
14272 void
14273 ill_group_cleanup(ill_t *ill)
14274 {
14275 	ill_t *ill_v4;
14276 	ill_t *ill_v6;
14277 	ipif_t *ipif;
14278 
14279 	ill_v4 = ill->ill_phyint->phyint_illv4;
14280 	ill_v6 = ill->ill_phyint->phyint_illv6;
14281 
14282 	if (ill_v4 != NULL) {
14283 		mutex_enter(&ill_v4->ill_lock);
14284 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14285 		    ipif = ipif->ipif_next) {
14286 			IPIF_UNMARK_MOVING(ipif);
14287 		}
14288 		ill_v4->ill_up_ipifs = B_FALSE;
14289 		mutex_exit(&ill_v4->ill_lock);
14290 	}
14291 
14292 	if (ill_v6 != NULL) {
14293 		mutex_enter(&ill_v6->ill_lock);
14294 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14295 		    ipif = ipif->ipif_next) {
14296 			IPIF_UNMARK_MOVING(ipif);
14297 		}
14298 		ill_v6->ill_up_ipifs = B_FALSE;
14299 		mutex_exit(&ill_v6->ill_lock);
14300 	}
14301 }
14302 /*
14303  * This function is called when an ill has had a change in its group status
14304  * to bring up all the ipifs that were up before the change.
14305  */
14306 int
14307 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14308 {
14309 	ipif_t *ipif;
14310 	ill_t *ill_v4;
14311 	ill_t *ill_v6;
14312 	ill_t *from_ill;
14313 	int err = 0;
14314 
14315 	ASSERT(IAM_WRITER_ILL(ill));
14316 
14317 	/*
14318 	 * Except for ipif_state_flags and ill_state_flags the other
14319 	 * fields of the ipif/ill that are modified below are protected
14320 	 * implicitly since we are a writer. We would have tried to down
14321 	 * even an ipif that was already down, in ill_down_ipifs. So we
14322 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14323 	 */
14324 	ill_v4 = ill->ill_phyint->phyint_illv4;
14325 	ill_v6 = ill->ill_phyint->phyint_illv6;
14326 	if (ill_v4 != NULL) {
14327 		ill_v4->ill_up_ipifs = B_TRUE;
14328 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14329 		    ipif = ipif->ipif_next) {
14330 			mutex_enter(&ill_v4->ill_lock);
14331 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14332 			IPIF_UNMARK_MOVING(ipif);
14333 			mutex_exit(&ill_v4->ill_lock);
14334 			if (ipif->ipif_was_up) {
14335 				if (!(ipif->ipif_flags & IPIF_UP))
14336 					err = ipif_up(ipif, q, mp);
14337 				ipif->ipif_was_up = B_FALSE;
14338 				if (err != 0) {
14339 					/*
14340 					 * Can there be any other error ?
14341 					 */
14342 					ASSERT(err == EINPROGRESS);
14343 					return (err);
14344 				}
14345 			}
14346 		}
14347 		mutex_enter(&ill_v4->ill_lock);
14348 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14349 		mutex_exit(&ill_v4->ill_lock);
14350 		ill_v4->ill_up_ipifs = B_FALSE;
14351 		if (ill_v4->ill_move_in_progress) {
14352 			ASSERT(ill_v4->ill_move_peer != NULL);
14353 			ill_v4->ill_move_in_progress = B_FALSE;
14354 			from_ill = ill_v4->ill_move_peer;
14355 			from_ill->ill_move_in_progress = B_FALSE;
14356 			from_ill->ill_move_peer = NULL;
14357 			mutex_enter(&from_ill->ill_lock);
14358 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14359 			mutex_exit(&from_ill->ill_lock);
14360 			if (ill_v6 == NULL) {
14361 				if (from_ill->ill_phyint->phyint_flags &
14362 				    PHYI_STANDBY) {
14363 					phyint_inactive(from_ill->ill_phyint);
14364 				}
14365 				if (ill_v4->ill_phyint->phyint_flags &
14366 				    PHYI_STANDBY) {
14367 					phyint_inactive(ill_v4->ill_phyint);
14368 				}
14369 			}
14370 			ill_v4->ill_move_peer = NULL;
14371 		}
14372 	}
14373 
14374 	if (ill_v6 != NULL) {
14375 		ill_v6->ill_up_ipifs = B_TRUE;
14376 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14377 		    ipif = ipif->ipif_next) {
14378 			mutex_enter(&ill_v6->ill_lock);
14379 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14380 			IPIF_UNMARK_MOVING(ipif);
14381 			mutex_exit(&ill_v6->ill_lock);
14382 			if (ipif->ipif_was_up) {
14383 				if (!(ipif->ipif_flags & IPIF_UP))
14384 					err = ipif_up(ipif, q, mp);
14385 				ipif->ipif_was_up = B_FALSE;
14386 				if (err != 0) {
14387 					/*
14388 					 * Can there be any other error ?
14389 					 */
14390 					ASSERT(err == EINPROGRESS);
14391 					return (err);
14392 				}
14393 			}
14394 		}
14395 		mutex_enter(&ill_v6->ill_lock);
14396 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14397 		mutex_exit(&ill_v6->ill_lock);
14398 		ill_v6->ill_up_ipifs = B_FALSE;
14399 		if (ill_v6->ill_move_in_progress) {
14400 			ASSERT(ill_v6->ill_move_peer != NULL);
14401 			ill_v6->ill_move_in_progress = B_FALSE;
14402 			from_ill = ill_v6->ill_move_peer;
14403 			from_ill->ill_move_in_progress = B_FALSE;
14404 			from_ill->ill_move_peer = NULL;
14405 			mutex_enter(&from_ill->ill_lock);
14406 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14407 			mutex_exit(&from_ill->ill_lock);
14408 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14409 				phyint_inactive(from_ill->ill_phyint);
14410 			}
14411 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14412 				phyint_inactive(ill_v6->ill_phyint);
14413 			}
14414 			ill_v6->ill_move_peer = NULL;
14415 		}
14416 	}
14417 	return (0);
14418 }
14419 
14420 /*
14421  * bring down all the approriate ipifs.
14422  */
14423 /* ARGSUSED */
14424 static void
14425 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14426 {
14427 	ipif_t *ipif;
14428 
14429 	ASSERT(IAM_WRITER_ILL(ill));
14430 
14431 	/*
14432 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14433 	 * are modified below are protected implicitly since we are a writer
14434 	 */
14435 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14436 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14437 			continue;
14438 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14439 			/*
14440 			 * We go through the ipif_down logic even if the ipif
14441 			 * is already down, since routes can be added based
14442 			 * on down ipifs. Going through ipif_down once again
14443 			 * will delete any IREs created based on these routes.
14444 			 */
14445 			if (ipif->ipif_flags & IPIF_UP)
14446 				ipif->ipif_was_up = B_TRUE;
14447 			/*
14448 			 * If called with chk_nofailover true ipif is moving.
14449 			 */
14450 			mutex_enter(&ill->ill_lock);
14451 			if (chk_nofailover) {
14452 				ipif->ipif_state_flags |=
14453 				    IPIF_MOVING | IPIF_CHANGING;
14454 			} else {
14455 				ipif->ipif_state_flags |= IPIF_CHANGING;
14456 			}
14457 			mutex_exit(&ill->ill_lock);
14458 			/*
14459 			 * Need to re-create net/subnet bcast ires if
14460 			 * they are dependent on ipif.
14461 			 */
14462 			if (!ipif->ipif_isv6)
14463 				ipif_check_bcast_ires(ipif);
14464 			(void) ipif_logical_down(ipif, NULL, NULL);
14465 			ipif_non_duplicate(ipif);
14466 			ipif_down_tail(ipif);
14467 		}
14468 	}
14469 }
14470 
14471 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14472 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14473 	(ipsq)->ipsq_refs++;				\
14474 }
14475 
14476 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14477 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14478 	(ipsq)->ipsq_refs--;				\
14479 	if ((ipsq)->ipsq_refs == 0)				\
14480 		(ipsq)->ipsq_name[0] = '\0'; 		\
14481 }
14482 
14483 /*
14484  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14485  * new_ipsq.
14486  */
14487 static void
14488 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14489 {
14490 	phyint_t *phyint;
14491 	phyint_t *next_phyint;
14492 
14493 	/*
14494 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14495 	 * writer and the ill_lock of the ill in question. Also the dest
14496 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14497 	 */
14498 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14499 
14500 	phyint = cur_ipsq->ipsq_phyint_list;
14501 	cur_ipsq->ipsq_phyint_list = NULL;
14502 	while (phyint != NULL) {
14503 		next_phyint = phyint->phyint_ipsq_next;
14504 		IPSQ_DEC_REF(cur_ipsq, ipst);
14505 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14506 		new_ipsq->ipsq_phyint_list = phyint;
14507 		IPSQ_INC_REF(new_ipsq, ipst);
14508 		phyint->phyint_ipsq = new_ipsq;
14509 		phyint = next_phyint;
14510 	}
14511 }
14512 
14513 #define	SPLIT_SUCCESS		0
14514 #define	SPLIT_NOT_NEEDED	1
14515 #define	SPLIT_FAILED		2
14516 
14517 int
14518 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14519     ip_stack_t *ipst)
14520 {
14521 	ipsq_t *newipsq = NULL;
14522 
14523 	/*
14524 	 * Assertions denote pre-requisites for changing the ipsq of
14525 	 * a phyint
14526 	 */
14527 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14528 	/*
14529 	 * <ill-phyint> assocs can't change while ill_g_lock
14530 	 * is held as writer. See ill_phyint_reinit()
14531 	 */
14532 	ASSERT(phyint->phyint_illv4 == NULL ||
14533 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14534 	ASSERT(phyint->phyint_illv6 == NULL ||
14535 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14536 
14537 	if ((phyint->phyint_groupname_len !=
14538 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14539 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14540 	    phyint->phyint_groupname_len) != 0)) {
14541 		/*
14542 		 * Once we fail in creating a new ipsq due to memory shortage,
14543 		 * don't attempt to create new ipsq again, based on another
14544 		 * phyint, since we want all phyints belonging to an IPMP group
14545 		 * to be in the same ipsq even in the event of mem alloc fails.
14546 		 */
14547 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14548 		    cur_ipsq, ipst);
14549 		if (newipsq == NULL) {
14550 			/* Memory allocation failure */
14551 			return (SPLIT_FAILED);
14552 		} else {
14553 			/* ipsq_refs protected by ill_g_lock (writer) */
14554 			IPSQ_DEC_REF(cur_ipsq, ipst);
14555 			phyint->phyint_ipsq = newipsq;
14556 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14557 			newipsq->ipsq_phyint_list = phyint;
14558 			IPSQ_INC_REF(newipsq, ipst);
14559 			return (SPLIT_SUCCESS);
14560 		}
14561 	}
14562 	return (SPLIT_NOT_NEEDED);
14563 }
14564 
14565 /*
14566  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14567  * to do this split
14568  */
14569 static int
14570 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14571 {
14572 	ipsq_t *newipsq;
14573 
14574 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14575 	/*
14576 	 * <ill-phyint> assocs can't change while ill_g_lock
14577 	 * is held as writer. See ill_phyint_reinit()
14578 	 */
14579 
14580 	ASSERT(phyint->phyint_illv4 == NULL ||
14581 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14582 	ASSERT(phyint->phyint_illv6 == NULL ||
14583 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14584 
14585 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14586 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14587 		/*
14588 		 * ipsq_init failed due to no memory
14589 		 * caller will use the same ipsq
14590 		 */
14591 		return (SPLIT_FAILED);
14592 	}
14593 
14594 	/* ipsq_ref is protected by ill_g_lock (writer) */
14595 	IPSQ_DEC_REF(cur_ipsq, ipst);
14596 
14597 	/*
14598 	 * This is a new ipsq that is unknown to the world.
14599 	 * So we don't need to hold ipsq_lock,
14600 	 */
14601 	newipsq = phyint->phyint_ipsq;
14602 	newipsq->ipsq_writer = NULL;
14603 	newipsq->ipsq_reentry_cnt--;
14604 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14605 #ifdef DEBUG
14606 	newipsq->ipsq_depth = 0;
14607 #endif
14608 
14609 	return (SPLIT_SUCCESS);
14610 }
14611 
14612 /*
14613  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14614  * ipsq's representing their individual groups or themselves. Return
14615  * whether split needs to be retried again later.
14616  */
14617 static boolean_t
14618 ill_split_ipsq(ipsq_t *cur_ipsq)
14619 {
14620 	phyint_t *phyint;
14621 	phyint_t *next_phyint;
14622 	int	error;
14623 	boolean_t need_retry = B_FALSE;
14624 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14625 
14626 	phyint = cur_ipsq->ipsq_phyint_list;
14627 	cur_ipsq->ipsq_phyint_list = NULL;
14628 	while (phyint != NULL) {
14629 		next_phyint = phyint->phyint_ipsq_next;
14630 		/*
14631 		 * 'created' will tell us whether the callee actually
14632 		 * created an ipsq. Lack of memory may force the callee
14633 		 * to return without creating an ipsq.
14634 		 */
14635 		if (phyint->phyint_groupname == NULL) {
14636 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14637 		} else {
14638 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14639 			    need_retry, ipst);
14640 		}
14641 
14642 		switch (error) {
14643 		case SPLIT_FAILED:
14644 			need_retry = B_TRUE;
14645 			/* FALLTHRU */
14646 		case SPLIT_NOT_NEEDED:
14647 			/*
14648 			 * Keep it on the list.
14649 			 */
14650 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14651 			cur_ipsq->ipsq_phyint_list = phyint;
14652 			break;
14653 		case SPLIT_SUCCESS:
14654 			break;
14655 		default:
14656 			ASSERT(0);
14657 		}
14658 
14659 		phyint = next_phyint;
14660 	}
14661 	return (need_retry);
14662 }
14663 
14664 /*
14665  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14666  * and return the ills in the list. This list will be
14667  * needed to unlock all the ills later on by the caller.
14668  * The <ill-ipsq> associations could change between the
14669  * lock and unlock. Hence the unlock can't traverse the
14670  * ipsq to get the list of ills.
14671  */
14672 static int
14673 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14674 {
14675 	int	cnt = 0;
14676 	phyint_t	*phyint;
14677 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14678 
14679 	/*
14680 	 * The caller holds ill_g_lock to ensure that the ill memberships
14681 	 * of the ipsq don't change
14682 	 */
14683 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14684 
14685 	phyint = ipsq->ipsq_phyint_list;
14686 	while (phyint != NULL) {
14687 		if (phyint->phyint_illv4 != NULL) {
14688 			ASSERT(cnt < list_max);
14689 			list[cnt++] = phyint->phyint_illv4;
14690 		}
14691 		if (phyint->phyint_illv6 != NULL) {
14692 			ASSERT(cnt < list_max);
14693 			list[cnt++] = phyint->phyint_illv6;
14694 		}
14695 		phyint = phyint->phyint_ipsq_next;
14696 	}
14697 	ill_lock_ills(list, cnt);
14698 	return (cnt);
14699 }
14700 
14701 void
14702 ill_lock_ills(ill_t **list, int cnt)
14703 {
14704 	int	i;
14705 
14706 	if (cnt > 1) {
14707 		boolean_t try_again;
14708 		do {
14709 			try_again = B_FALSE;
14710 			for (i = 0; i < cnt - 1; i++) {
14711 				if (list[i] < list[i + 1]) {
14712 					ill_t	*tmp;
14713 
14714 					/* swap the elements */
14715 					tmp = list[i];
14716 					list[i] = list[i + 1];
14717 					list[i + 1] = tmp;
14718 					try_again = B_TRUE;
14719 				}
14720 			}
14721 		} while (try_again);
14722 	}
14723 
14724 	for (i = 0; i < cnt; i++) {
14725 		if (i == 0) {
14726 			if (list[i] != NULL)
14727 				mutex_enter(&list[i]->ill_lock);
14728 			else
14729 				return;
14730 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14731 			mutex_enter(&list[i]->ill_lock);
14732 		}
14733 	}
14734 }
14735 
14736 void
14737 ill_unlock_ills(ill_t **list, int cnt)
14738 {
14739 	int	i;
14740 
14741 	for (i = 0; i < cnt; i++) {
14742 		if ((i == 0) && (list[i] != NULL)) {
14743 			mutex_exit(&list[i]->ill_lock);
14744 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14745 			mutex_exit(&list[i]->ill_lock);
14746 		}
14747 	}
14748 }
14749 
14750 /*
14751  * Merge all the ills from 1 ipsq group into another ipsq group.
14752  * The source ipsq group is specified by the ipsq associated with
14753  * 'from_ill'. The destination ipsq group is specified by the ipsq
14754  * associated with 'to_ill' or 'groupname' respectively.
14755  * Note that ipsq itself does not have a reference count mechanism
14756  * and functions don't look up an ipsq and pass it around. Instead
14757  * functions pass around an ill or groupname, and the ipsq is looked
14758  * up from the ill or groupname and the required operation performed
14759  * atomically with the lookup on the ipsq.
14760  */
14761 static int
14762 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14763     queue_t *q)
14764 {
14765 	ipsq_t *old_ipsq;
14766 	ipsq_t *new_ipsq;
14767 	ill_t	**ill_list;
14768 	int	cnt;
14769 	size_t	ill_list_size;
14770 	boolean_t became_writer_on_new_sq = B_FALSE;
14771 	ip_stack_t	*ipst = from_ill->ill_ipst;
14772 
14773 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14774 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14775 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14776 
14777 	/*
14778 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14779 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14780 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14781 	 */
14782 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14783 
14784 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14785 	if (groupname != NULL)
14786 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14787 	else {
14788 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14789 	}
14790 
14791 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14792 
14793 	/*
14794 	 * both groups are on the same ipsq.
14795 	 */
14796 	if (old_ipsq == new_ipsq) {
14797 		rw_exit(&ipst->ips_ill_g_lock);
14798 		return (0);
14799 	}
14800 
14801 	cnt = old_ipsq->ipsq_refs << 1;
14802 	ill_list_size = cnt * sizeof (ill_t *);
14803 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14804 	if (ill_list == NULL) {
14805 		rw_exit(&ipst->ips_ill_g_lock);
14806 		return (ENOMEM);
14807 	}
14808 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14809 
14810 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14811 	mutex_enter(&new_ipsq->ipsq_lock);
14812 	if ((new_ipsq->ipsq_writer == NULL &&
14813 	    new_ipsq->ipsq_current_ipif == NULL) ||
14814 	    (new_ipsq->ipsq_writer == curthread)) {
14815 		new_ipsq->ipsq_writer = curthread;
14816 		new_ipsq->ipsq_reentry_cnt++;
14817 		became_writer_on_new_sq = B_TRUE;
14818 	}
14819 
14820 	/*
14821 	 * We are holding ill_g_lock as writer and all the ill locks of
14822 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14823 	 * message can land up on the old ipsq even though we don't hold the
14824 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14825 	 */
14826 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14827 
14828 	/*
14829 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14830 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14831 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14832 	 */
14833 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14834 
14835 	/*
14836 	 * Mark the new ipsq as needing a split since it is currently
14837 	 * being shared by more than 1 IPMP group. The split will
14838 	 * occur at the end of ipsq_exit
14839 	 */
14840 	new_ipsq->ipsq_split = B_TRUE;
14841 
14842 	/* Now release all the locks */
14843 	mutex_exit(&new_ipsq->ipsq_lock);
14844 	ill_unlock_ills(ill_list, cnt);
14845 	rw_exit(&ipst->ips_ill_g_lock);
14846 
14847 	kmem_free(ill_list, ill_list_size);
14848 
14849 	/*
14850 	 * If we succeeded in becoming writer on the new ipsq, then
14851 	 * drain the new ipsq and start processing  all enqueued messages
14852 	 * including the current ioctl we are processing which is either
14853 	 * a set groupname or failover/failback.
14854 	 */
14855 	if (became_writer_on_new_sq)
14856 		ipsq_exit(new_ipsq);
14857 
14858 	/*
14859 	 * syncq has been changed and all the messages have been moved.
14860 	 */
14861 	mutex_enter(&old_ipsq->ipsq_lock);
14862 	old_ipsq->ipsq_current_ipif = NULL;
14863 	old_ipsq->ipsq_current_ioctl = 0;
14864 	old_ipsq->ipsq_current_done = B_TRUE;
14865 	mutex_exit(&old_ipsq->ipsq_lock);
14866 	return (EINPROGRESS);
14867 }
14868 
14869 /*
14870  * Delete and add the loopback copy and non-loopback copy of
14871  * the BROADCAST ire corresponding to ill and addr. Used to
14872  * group broadcast ires together when ill becomes part of
14873  * a group.
14874  *
14875  * This function is also called when ill is leaving the group
14876  * so that the ires belonging to the group gets re-grouped.
14877  */
14878 static void
14879 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14880 {
14881 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14882 	ire_t **ire_ptpn = &ire_head;
14883 	ip_stack_t	*ipst = ill->ill_ipst;
14884 
14885 	/*
14886 	 * The loopback and non-loopback IREs are inserted in the order in which
14887 	 * they're found, on the basis that they are correctly ordered (loopback
14888 	 * first).
14889 	 */
14890 	for (;;) {
14891 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14892 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14893 		if (ire == NULL)
14894 			break;
14895 
14896 		/*
14897 		 * we are passing in KM_SLEEP because it is not easy to
14898 		 * go back to a sane state in case of memory failure.
14899 		 */
14900 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14901 		ASSERT(nire != NULL);
14902 		bzero(nire, sizeof (ire_t));
14903 		/*
14904 		 * Don't use ire_max_frag directly since we don't
14905 		 * hold on to 'ire' until we add the new ire 'nire' and
14906 		 * we don't want the new ire to have a dangling reference
14907 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14908 		 * be in sync with the ipif_mtu of the associate ipif.
14909 		 * For eg. this happens as a result of SIOCSLIFNAME,
14910 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14911 		 * the driver. A change in ire_max_frag triggered as
14912 		 * as a result of path mtu discovery, or due to an
14913 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14914 		 * route change -mtu command does not apply to broadcast ires.
14915 		 *
14916 		 * XXX We need a recovery strategy here if ire_init fails
14917 		 */
14918 		if (ire_init(nire,
14919 		    (uchar_t *)&ire->ire_addr,
14920 		    (uchar_t *)&ire->ire_mask,
14921 		    (uchar_t *)&ire->ire_src_addr,
14922 		    (uchar_t *)&ire->ire_gateway_addr,
14923 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14924 		    &ire->ire_ipif->ipif_mtu,
14925 		    ire->ire_nce,
14926 		    ire->ire_rfq,
14927 		    ire->ire_stq,
14928 		    ire->ire_type,
14929 		    ire->ire_ipif,
14930 		    ire->ire_cmask,
14931 		    ire->ire_phandle,
14932 		    ire->ire_ihandle,
14933 		    ire->ire_flags,
14934 		    &ire->ire_uinfo,
14935 		    NULL,
14936 		    NULL,
14937 		    ipst) == NULL) {
14938 			cmn_err(CE_PANIC, "ire_init() failed");
14939 		}
14940 		ire_delete(ire);
14941 		ire_refrele(ire);
14942 
14943 		/*
14944 		 * The newly created IREs are inserted at the tail of the list
14945 		 * starting with ire_head. As we've just allocated them no one
14946 		 * knows about them so it's safe.
14947 		 */
14948 		*ire_ptpn = nire;
14949 		ire_ptpn = &nire->ire_next;
14950 	}
14951 
14952 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14953 		int error;
14954 		ire_t *oire;
14955 		/* unlink the IRE from our list before calling ire_add() */
14956 		nire_next = nire->ire_next;
14957 		nire->ire_next = NULL;
14958 
14959 		/* ire_add adds the ire at the right place in the list */
14960 		oire = nire;
14961 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14962 		ASSERT(error == 0);
14963 		ASSERT(oire == nire);
14964 		ire_refrele(nire);	/* Held in ire_add */
14965 	}
14966 }
14967 
14968 /*
14969  * This function is usually called when an ill is inserted in
14970  * a group and all the ipifs are already UP. As all the ipifs
14971  * are already UP, the broadcast ires have already been created
14972  * and been inserted. But, ire_add_v4 would not have grouped properly.
14973  * We need to re-group for the benefit of ip_wput_ire which
14974  * expects BROADCAST ires to be grouped properly to avoid sending
14975  * more than one copy of the broadcast packet per group.
14976  *
14977  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14978  *	  because when ipif_up_done ends up calling this, ires have
14979  *        already been added before illgrp_insert i.e before ill_group
14980  *	  has been initialized.
14981  */
14982 static void
14983 ill_group_bcast_for_xmit(ill_t *ill)
14984 {
14985 	ill_group_t *illgrp;
14986 	ipif_t *ipif;
14987 	ipaddr_t addr;
14988 	ipaddr_t net_mask;
14989 	ipaddr_t subnet_netmask;
14990 
14991 	illgrp = ill->ill_group;
14992 
14993 	/*
14994 	 * This function is called even when an ill is deleted from
14995 	 * the group. Hence, illgrp could be null.
14996 	 */
14997 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
14998 		return;
14999 
15000 	/*
15001 	 * Delete all the BROADCAST ires matching this ill and add
15002 	 * them back. This time, ire_add_v4 should take care of
15003 	 * grouping them with others because ill is part of the
15004 	 * group.
15005 	 */
15006 	ill_bcast_delete_and_add(ill, 0);
15007 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15008 
15009 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15010 
15011 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15012 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15013 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15014 		} else {
15015 			net_mask = htonl(IN_CLASSA_NET);
15016 		}
15017 		addr = net_mask & ipif->ipif_subnet;
15018 		ill_bcast_delete_and_add(ill, addr);
15019 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15020 
15021 		subnet_netmask = ipif->ipif_net_mask;
15022 		addr = ipif->ipif_subnet;
15023 		ill_bcast_delete_and_add(ill, addr);
15024 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15025 	}
15026 }
15027 
15028 /*
15029  * This function is called from illgrp_delete when ill is being deleted
15030  * from the group.
15031  *
15032  * As ill is not there in the group anymore, any address belonging
15033  * to this ill should be cleared of IRE_MARK_NORECV.
15034  */
15035 static void
15036 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15037 {
15038 	ire_t *ire;
15039 	irb_t *irb;
15040 	ip_stack_t	*ipst = ill->ill_ipst;
15041 
15042 	ASSERT(ill->ill_group == NULL);
15043 
15044 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15045 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15046 
15047 	if (ire != NULL) {
15048 		/*
15049 		 * IPMP and plumbing operations are serialized on the ipsq, so
15050 		 * no one will insert or delete a broadcast ire under our feet.
15051 		 */
15052 		irb = ire->ire_bucket;
15053 		rw_enter(&irb->irb_lock, RW_READER);
15054 		ire_refrele(ire);
15055 
15056 		for (; ire != NULL; ire = ire->ire_next) {
15057 			if (ire->ire_addr != addr)
15058 				break;
15059 			if (ire_to_ill(ire) != ill)
15060 				continue;
15061 
15062 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15063 			ire->ire_marks &= ~IRE_MARK_NORECV;
15064 		}
15065 		rw_exit(&irb->irb_lock);
15066 	}
15067 }
15068 
15069 /*
15070  * This function must be called only after the broadcast ires
15071  * have been grouped together. For a given address addr, nominate
15072  * only one of the ires whose interface is not FAILED or OFFLINE.
15073  *
15074  * This is also called when an ipif goes down, so that we can nominate
15075  * a different ire with the same address for receiving.
15076  */
15077 static void
15078 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15079 {
15080 	irb_t *irb;
15081 	ire_t *ire;
15082 	ire_t *ire1;
15083 	ire_t *save_ire;
15084 	ire_t **irep = NULL;
15085 	boolean_t first = B_TRUE;
15086 	ire_t *clear_ire = NULL;
15087 	ire_t *start_ire = NULL;
15088 	ire_t	*new_lb_ire;
15089 	ire_t	*new_nlb_ire;
15090 	boolean_t new_lb_ire_used = B_FALSE;
15091 	boolean_t new_nlb_ire_used = B_FALSE;
15092 	uint64_t match_flags;
15093 	uint64_t phyi_flags;
15094 	boolean_t fallback = B_FALSE;
15095 	uint_t	max_frag;
15096 
15097 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15098 	    NULL, MATCH_IRE_TYPE, ipst);
15099 	/*
15100 	 * We may not be able to find some ires if a previous
15101 	 * ire_create failed. This happens when an ipif goes
15102 	 * down and we are unable to create BROADCAST ires due
15103 	 * to memory failure. Thus, we have to check for NULL
15104 	 * below. This should handle the case for LOOPBACK,
15105 	 * POINTOPOINT and interfaces with some POINTOPOINT
15106 	 * logicals for which there are no BROADCAST ires.
15107 	 */
15108 	if (ire == NULL)
15109 		return;
15110 	/*
15111 	 * Currently IRE_BROADCASTS are deleted when an ipif
15112 	 * goes down which runs exclusively. Thus, setting
15113 	 * IRE_MARK_RCVD should not race with ire_delete marking
15114 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15115 	 * be consistent with other parts of the code that walks
15116 	 * a given bucket.
15117 	 */
15118 	save_ire = ire;
15119 	irb = ire->ire_bucket;
15120 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15121 	if (new_lb_ire == NULL) {
15122 		ire_refrele(ire);
15123 		return;
15124 	}
15125 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15126 	if (new_nlb_ire == NULL) {
15127 		ire_refrele(ire);
15128 		kmem_cache_free(ire_cache, new_lb_ire);
15129 		return;
15130 	}
15131 	IRB_REFHOLD(irb);
15132 	rw_enter(&irb->irb_lock, RW_WRITER);
15133 	/*
15134 	 * Get to the first ire matching the address and the
15135 	 * group. If the address does not match we are done
15136 	 * as we could not find the IRE. If the address matches
15137 	 * we should get to the first one matching the group.
15138 	 */
15139 	while (ire != NULL) {
15140 		if (ire->ire_addr != addr ||
15141 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15142 			break;
15143 		}
15144 		ire = ire->ire_next;
15145 	}
15146 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15147 	start_ire = ire;
15148 redo:
15149 	while (ire != NULL && ire->ire_addr == addr &&
15150 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15151 		/*
15152 		 * The first ire for any address within a group
15153 		 * should always be the one with IRE_MARK_NORECV cleared
15154 		 * so that ip_wput_ire can avoid searching for one.
15155 		 * Note down the insertion point which will be used
15156 		 * later.
15157 		 */
15158 		if (first && (irep == NULL))
15159 			irep = ire->ire_ptpn;
15160 		/*
15161 		 * PHYI_FAILED is set when the interface fails.
15162 		 * This interface might have become good, but the
15163 		 * daemon has not yet detected. We should still
15164 		 * not receive on this. PHYI_OFFLINE should never
15165 		 * be picked as this has been offlined and soon
15166 		 * be removed.
15167 		 */
15168 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15169 		if (phyi_flags & PHYI_OFFLINE) {
15170 			ire->ire_marks |= IRE_MARK_NORECV;
15171 			ire = ire->ire_next;
15172 			continue;
15173 		}
15174 		if (phyi_flags & match_flags) {
15175 			ire->ire_marks |= IRE_MARK_NORECV;
15176 			ire = ire->ire_next;
15177 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15178 			    PHYI_INACTIVE) {
15179 				fallback = B_TRUE;
15180 			}
15181 			continue;
15182 		}
15183 		if (first) {
15184 			/*
15185 			 * We will move this to the front of the list later
15186 			 * on.
15187 			 */
15188 			clear_ire = ire;
15189 			ire->ire_marks &= ~IRE_MARK_NORECV;
15190 		} else {
15191 			ire->ire_marks |= IRE_MARK_NORECV;
15192 		}
15193 		first = B_FALSE;
15194 		ire = ire->ire_next;
15195 	}
15196 	/*
15197 	 * If we never nominated anybody, try nominating at least
15198 	 * an INACTIVE, if we found one. Do it only once though.
15199 	 */
15200 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15201 	    fallback) {
15202 		match_flags = PHYI_FAILED;
15203 		ire = start_ire;
15204 		irep = NULL;
15205 		goto redo;
15206 	}
15207 	ire_refrele(save_ire);
15208 
15209 	/*
15210 	 * irep non-NULL indicates that we entered the while loop
15211 	 * above. If clear_ire is at the insertion point, we don't
15212 	 * have to do anything. clear_ire will be NULL if all the
15213 	 * interfaces are failed.
15214 	 *
15215 	 * We cannot unlink and reinsert the ire at the right place
15216 	 * in the list since there can be other walkers of this bucket.
15217 	 * Instead we delete and recreate the ire
15218 	 */
15219 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15220 		ire_t *clear_ire_stq = NULL;
15221 
15222 		bzero(new_lb_ire, sizeof (ire_t));
15223 		/* XXX We need a recovery strategy here. */
15224 		if (ire_init(new_lb_ire,
15225 		    (uchar_t *)&clear_ire->ire_addr,
15226 		    (uchar_t *)&clear_ire->ire_mask,
15227 		    (uchar_t *)&clear_ire->ire_src_addr,
15228 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15229 		    &clear_ire->ire_max_frag,
15230 		    NULL, /* let ire_nce_init derive the resolver info */
15231 		    clear_ire->ire_rfq,
15232 		    clear_ire->ire_stq,
15233 		    clear_ire->ire_type,
15234 		    clear_ire->ire_ipif,
15235 		    clear_ire->ire_cmask,
15236 		    clear_ire->ire_phandle,
15237 		    clear_ire->ire_ihandle,
15238 		    clear_ire->ire_flags,
15239 		    &clear_ire->ire_uinfo,
15240 		    NULL,
15241 		    NULL,
15242 		    ipst) == NULL)
15243 			cmn_err(CE_PANIC, "ire_init() failed");
15244 		if (clear_ire->ire_stq == NULL) {
15245 			ire_t *ire_next = clear_ire->ire_next;
15246 			if (ire_next != NULL &&
15247 			    ire_next->ire_stq != NULL &&
15248 			    ire_next->ire_addr == clear_ire->ire_addr &&
15249 			    ire_next->ire_ipif->ipif_ill ==
15250 			    clear_ire->ire_ipif->ipif_ill) {
15251 				clear_ire_stq = ire_next;
15252 
15253 				bzero(new_nlb_ire, sizeof (ire_t));
15254 				/* XXX We need a recovery strategy here. */
15255 				if (ire_init(new_nlb_ire,
15256 				    (uchar_t *)&clear_ire_stq->ire_addr,
15257 				    (uchar_t *)&clear_ire_stq->ire_mask,
15258 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15259 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15260 				    &clear_ire_stq->ire_max_frag,
15261 				    NULL,
15262 				    clear_ire_stq->ire_rfq,
15263 				    clear_ire_stq->ire_stq,
15264 				    clear_ire_stq->ire_type,
15265 				    clear_ire_stq->ire_ipif,
15266 				    clear_ire_stq->ire_cmask,
15267 				    clear_ire_stq->ire_phandle,
15268 				    clear_ire_stq->ire_ihandle,
15269 				    clear_ire_stq->ire_flags,
15270 				    &clear_ire_stq->ire_uinfo,
15271 				    NULL,
15272 				    NULL,
15273 				    ipst) == NULL)
15274 					cmn_err(CE_PANIC, "ire_init() failed");
15275 			}
15276 		}
15277 
15278 		/*
15279 		 * Delete the ire. We can't call ire_delete() since
15280 		 * we are holding the bucket lock. We can't release the
15281 		 * bucket lock since we can't allow irep to change. So just
15282 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15283 		 * ire from the list and do the refrele.
15284 		 */
15285 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15286 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15287 
15288 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15289 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15290 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15291 		}
15292 
15293 		/*
15294 		 * Also take care of otherfields like ib/ob pkt count
15295 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15296 		 */
15297 
15298 		/* Set the max_frag before adding the ire */
15299 		max_frag = *new_lb_ire->ire_max_fragp;
15300 		new_lb_ire->ire_max_fragp = NULL;
15301 		new_lb_ire->ire_max_frag = max_frag;
15302 
15303 		/* Add the new ire's. Insert at *irep */
15304 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15305 		ire1 = *irep;
15306 		if (ire1 != NULL)
15307 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15308 		new_lb_ire->ire_next = ire1;
15309 		/* Link the new one in. */
15310 		new_lb_ire->ire_ptpn = irep;
15311 		membar_producer();
15312 		*irep = new_lb_ire;
15313 		new_lb_ire_used = B_TRUE;
15314 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15315 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15316 		DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), new_lb_ire->ire_ipif,
15317 		    (char *), "ire", (void *), new_lb_ire);
15318 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15319 
15320 		if (clear_ire_stq != NULL) {
15321 			/* Set the max_frag before adding the ire */
15322 			max_frag = *new_nlb_ire->ire_max_fragp;
15323 			new_nlb_ire->ire_max_fragp = NULL;
15324 			new_nlb_ire->ire_max_frag = max_frag;
15325 
15326 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15327 			irep = &new_lb_ire->ire_next;
15328 			/* Add the new ire. Insert at *irep */
15329 			ire1 = *irep;
15330 			if (ire1 != NULL)
15331 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15332 			new_nlb_ire->ire_next = ire1;
15333 			/* Link the new one in. */
15334 			new_nlb_ire->ire_ptpn = irep;
15335 			membar_producer();
15336 			*irep = new_nlb_ire;
15337 			new_nlb_ire_used = B_TRUE;
15338 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15339 			    ire_stats_inserted);
15340 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15341 			DTRACE_PROBE3(ipif__incr__cnt,
15342 			    (ipif_t *), new_nlb_ire->ire_ipif,
15343 			    (char *), "ire", (void *), new_nlb_ire);
15344 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15345 			DTRACE_PROBE3(ill__incr__cnt,
15346 			    (ill_t *), new_nlb_ire->ire_stq->q_ptr,
15347 			    (char *), "ire", (void *), new_nlb_ire);
15348 			((ill_t *)(new_nlb_ire->ire_stq->q_ptr))->ill_ire_cnt++;
15349 		}
15350 	}
15351 	rw_exit(&irb->irb_lock);
15352 	if (!new_lb_ire_used)
15353 		kmem_cache_free(ire_cache, new_lb_ire);
15354 	if (!new_nlb_ire_used)
15355 		kmem_cache_free(ire_cache, new_nlb_ire);
15356 	IRB_REFRELE(irb);
15357 }
15358 
15359 /*
15360  * Whenever an ipif goes down we have to renominate a different
15361  * broadcast ire to receive. Whenever an ipif comes up, we need
15362  * to make sure that we have only one nominated to receive.
15363  */
15364 static void
15365 ipif_renominate_bcast(ipif_t *ipif)
15366 {
15367 	ill_t *ill = ipif->ipif_ill;
15368 	ipaddr_t subnet_addr;
15369 	ipaddr_t net_addr;
15370 	ipaddr_t net_mask = 0;
15371 	ipaddr_t subnet_netmask;
15372 	ipaddr_t addr;
15373 	ill_group_t *illgrp;
15374 	ip_stack_t	*ipst = ill->ill_ipst;
15375 
15376 	illgrp = ill->ill_group;
15377 	/*
15378 	 * If this is the last ipif going down, it might take
15379 	 * the ill out of the group. In that case ipif_down ->
15380 	 * illgrp_delete takes care of doing the nomination.
15381 	 * ipif_down does not call for this case.
15382 	 */
15383 	ASSERT(illgrp != NULL);
15384 
15385 	/* There could not have been any ires associated with this */
15386 	if (ipif->ipif_subnet == 0)
15387 		return;
15388 
15389 	ill_mark_bcast(illgrp, 0, ipst);
15390 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15391 
15392 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15393 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15394 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15395 	} else {
15396 		net_mask = htonl(IN_CLASSA_NET);
15397 	}
15398 	addr = net_mask & ipif->ipif_subnet;
15399 	ill_mark_bcast(illgrp, addr, ipst);
15400 
15401 	net_addr = ~net_mask | addr;
15402 	ill_mark_bcast(illgrp, net_addr, ipst);
15403 
15404 	subnet_netmask = ipif->ipif_net_mask;
15405 	addr = ipif->ipif_subnet;
15406 	ill_mark_bcast(illgrp, addr, ipst);
15407 
15408 	subnet_addr = ~subnet_netmask | addr;
15409 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15410 }
15411 
15412 /*
15413  * Whenever we form or delete ill groups, we need to nominate one set of
15414  * BROADCAST ires for receiving in the group.
15415  *
15416  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15417  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15418  *    for ill_ipif_up_count to be non-zero. This is the only case where
15419  *    ill_ipif_up_count is zero and we would still find the ires.
15420  *
15421  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15422  *    ipif is UP and we just have to do the nomination.
15423  *
15424  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15425  *    from the group. So, we have to do the nomination.
15426  *
15427  * Because of (3), there could be just one ill in the group. But we have
15428  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15429  * Thus, this function does not optimize when there is only one ill as
15430  * it is not correct for (3).
15431  */
15432 static void
15433 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15434 {
15435 	ill_t *ill;
15436 	ipif_t *ipif;
15437 	ipaddr_t subnet_addr;
15438 	ipaddr_t prev_subnet_addr = 0;
15439 	ipaddr_t net_addr;
15440 	ipaddr_t prev_net_addr = 0;
15441 	ipaddr_t net_mask = 0;
15442 	ipaddr_t subnet_netmask;
15443 	ipaddr_t addr;
15444 	ip_stack_t	*ipst;
15445 
15446 	/*
15447 	 * When the last memeber is leaving, there is nothing to
15448 	 * nominate.
15449 	 */
15450 	if (illgrp->illgrp_ill_count == 0) {
15451 		ASSERT(illgrp->illgrp_ill == NULL);
15452 		return;
15453 	}
15454 
15455 	ill = illgrp->illgrp_ill;
15456 	ASSERT(!ill->ill_isv6);
15457 	ipst = ill->ill_ipst;
15458 	/*
15459 	 * We assume that ires with same address and belonging to the
15460 	 * same group, has been grouped together. Nominating a *single*
15461 	 * ill in the group for sending and receiving broadcast is done
15462 	 * by making sure that the first BROADCAST ire (which will be
15463 	 * the one returned by ire_ctable_lookup for ip_rput and the
15464 	 * one that will be used in ip_wput_ire) will be the one that
15465 	 * will not have IRE_MARK_NORECV set.
15466 	 *
15467 	 * 1) ip_rput checks and discards packets received on ires marked
15468 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15469 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15470 	 *    first ire in the group for every broadcast address in the group.
15471 	 *    ip_rput will accept packets only on the first ire i.e only
15472 	 *    one copy of the ill.
15473 	 *
15474 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15475 	 *    packet for the whole group. It needs to send out on the ill
15476 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15477 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15478 	 *    the copy echoed back on other port where the ire is not marked
15479 	 *    with IRE_MARK_NORECV.
15480 	 *
15481 	 * Note that we just need to have the first IRE either loopback or
15482 	 * non-loopback (either of them may not exist if ire_create failed
15483 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15484 	 * always hit the first one and hence will always accept one copy.
15485 	 *
15486 	 * We have a broadcast ire per ill for all the unique prefixes
15487 	 * hosted on that ill. As we don't have a way of knowing the
15488 	 * unique prefixes on a given ill and hence in the whole group,
15489 	 * we just call ill_mark_bcast on all the prefixes that exist
15490 	 * in the group. For the common case of one prefix, the code
15491 	 * below optimizes by remebering the last address used for
15492 	 * markng. In the case of multiple prefixes, this will still
15493 	 * optimize depending the order of prefixes.
15494 	 *
15495 	 * The only unique address across the whole group is 0.0.0.0 and
15496 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15497 	 * the first ire in the bucket for receiving and disables the
15498 	 * others.
15499 	 */
15500 	ill_mark_bcast(illgrp, 0, ipst);
15501 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15502 	for (; ill != NULL; ill = ill->ill_group_next) {
15503 
15504 		for (ipif = ill->ill_ipif; ipif != NULL;
15505 		    ipif = ipif->ipif_next) {
15506 
15507 			if (!(ipif->ipif_flags & IPIF_UP) ||
15508 			    ipif->ipif_subnet == 0) {
15509 				continue;
15510 			}
15511 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15512 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15513 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15514 			} else {
15515 				net_mask = htonl(IN_CLASSA_NET);
15516 			}
15517 			addr = net_mask & ipif->ipif_subnet;
15518 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15519 				ill_mark_bcast(illgrp, addr, ipst);
15520 				net_addr = ~net_mask | addr;
15521 				ill_mark_bcast(illgrp, net_addr, ipst);
15522 			}
15523 			prev_net_addr = addr;
15524 
15525 			subnet_netmask = ipif->ipif_net_mask;
15526 			addr = ipif->ipif_subnet;
15527 			if (prev_subnet_addr == 0 ||
15528 			    prev_subnet_addr != addr) {
15529 				ill_mark_bcast(illgrp, addr, ipst);
15530 				subnet_addr = ~subnet_netmask | addr;
15531 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15532 			}
15533 			prev_subnet_addr = addr;
15534 		}
15535 	}
15536 }
15537 
15538 /*
15539  * This function is called while forming ill groups.
15540  *
15541  * Currently, we handle only allmulti groups. We want to join
15542  * allmulti on only one of the ills in the groups. In future,
15543  * when we have link aggregation, we may have to join normal
15544  * multicast groups on multiple ills as switch does inbound load
15545  * balancing. Following are the functions that calls this
15546  * function :
15547  *
15548  * 1) ill_recover_multicast : Interface is coming back UP.
15549  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15550  *    will call ill_recover_multicast to recover all the multicast
15551  *    groups. We need to make sure that only one member is joined
15552  *    in the ill group.
15553  *
15554  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15555  *    Somebody is joining allmulti. We need to make sure that only one
15556  *    member is joined in the group.
15557  *
15558  * 3) illgrp_insert : If allmulti has already joined, we need to make
15559  *    sure that only one member is joined in the group.
15560  *
15561  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15562  *    allmulti who we have nominated. We need to pick someother ill.
15563  *
15564  * 5) illgrp_delete : The ill we nominated is leaving the group,
15565  *    we need to pick a new ill to join the group.
15566  *
15567  * For (1), (2), (5) - we just have to check whether there is
15568  * a good ill joined in the group. If we could not find any ills
15569  * joined the group, we should join.
15570  *
15571  * For (4), the one that was nominated to receive, left the group.
15572  * There could be nobody joined in the group when this function is
15573  * called.
15574  *
15575  * For (3) - we need to explicitly check whether there are multiple
15576  * ills joined in the group.
15577  *
15578  * For simplicity, we don't differentiate any of the above cases. We
15579  * just leave the group if it is joined on any of them and join on
15580  * the first good ill.
15581  */
15582 int
15583 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15584 {
15585 	ilm_t *ilm;
15586 	ill_t *ill;
15587 	ill_t *fallback_inactive_ill = NULL;
15588 	ill_t *fallback_failed_ill = NULL;
15589 	int ret = 0;
15590 
15591 	/*
15592 	 * Leave the allmulti on all the ills and start fresh.
15593 	 */
15594 	for (ill = illgrp->illgrp_ill; ill != NULL;
15595 	    ill = ill->ill_group_next) {
15596 		if (ill->ill_join_allmulti)
15597 			(void) ip_leave_allmulti(ill->ill_ipif);
15598 	}
15599 
15600 	/*
15601 	 * Choose a good ill. Fallback to inactive or failed if
15602 	 * none available. We need to fallback to FAILED in the
15603 	 * case where we have 2 interfaces in a group - where
15604 	 * one of them is failed and another is a good one and
15605 	 * the good one (not marked inactive) is leaving the group.
15606 	 */
15607 	ret = 0;
15608 	for (ill = illgrp->illgrp_ill; ill != NULL;
15609 	    ill = ill->ill_group_next) {
15610 		/* Never pick an offline interface */
15611 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15612 			continue;
15613 
15614 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15615 			fallback_failed_ill = ill;
15616 			continue;
15617 		}
15618 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15619 			fallback_inactive_ill = ill;
15620 			continue;
15621 		}
15622 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15623 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15624 				ret = ip_join_allmulti(ill->ill_ipif);
15625 				/*
15626 				 * ip_join_allmulti can fail because of memory
15627 				 * failures. So, make sure we join at least
15628 				 * on one ill.
15629 				 */
15630 				if (ill->ill_join_allmulti)
15631 					return (0);
15632 			}
15633 		}
15634 	}
15635 	if (ret != 0) {
15636 		/*
15637 		 * If we tried nominating above and failed to do so,
15638 		 * return error. We might have tried multiple times.
15639 		 * But, return the latest error.
15640 		 */
15641 		return (ret);
15642 	}
15643 	if ((ill = fallback_inactive_ill) != NULL) {
15644 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15645 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15646 				ret = ip_join_allmulti(ill->ill_ipif);
15647 				return (ret);
15648 			}
15649 		}
15650 	} else if ((ill = fallback_failed_ill) != NULL) {
15651 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15652 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15653 				ret = ip_join_allmulti(ill->ill_ipif);
15654 				return (ret);
15655 			}
15656 		}
15657 	}
15658 	return (0);
15659 }
15660 
15661 /*
15662  * This function is called from illgrp_delete after it is
15663  * deleted from the group to reschedule responsibilities
15664  * to a different ill.
15665  */
15666 static void
15667 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15668 {
15669 	ilm_t	*ilm;
15670 	ipif_t	*ipif;
15671 	ipaddr_t subnet_addr;
15672 	ipaddr_t net_addr;
15673 	ipaddr_t net_mask = 0;
15674 	ipaddr_t subnet_netmask;
15675 	ipaddr_t addr;
15676 	ip_stack_t *ipst = ill->ill_ipst;
15677 
15678 	ASSERT(ill->ill_group == NULL);
15679 	/*
15680 	 * Broadcast Responsibility:
15681 	 *
15682 	 * 1. If this ill has been nominated for receiving broadcast
15683 	 * packets, we need to find a new one. Before we find a new
15684 	 * one, we need to re-group the ires that are part of this new
15685 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15686 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15687 	 * thing for us.
15688 	 *
15689 	 * 2. If this ill was not nominated for receiving broadcast
15690 	 * packets, we need to clear the IRE_MARK_NORECV flag
15691 	 * so that we continue to send up broadcast packets.
15692 	 */
15693 	if (!ill->ill_isv6) {
15694 		/*
15695 		 * Case 1 above : No optimization here. Just redo the
15696 		 * nomination.
15697 		 */
15698 		ill_group_bcast_for_xmit(ill);
15699 		ill_nominate_bcast_rcv(illgrp);
15700 
15701 		/*
15702 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15703 		 */
15704 		ill_clear_bcast_mark(ill, 0);
15705 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15706 
15707 		for (ipif = ill->ill_ipif; ipif != NULL;
15708 		    ipif = ipif->ipif_next) {
15709 
15710 			if (!(ipif->ipif_flags & IPIF_UP) ||
15711 			    ipif->ipif_subnet == 0) {
15712 				continue;
15713 			}
15714 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15715 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15716 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15717 			} else {
15718 				net_mask = htonl(IN_CLASSA_NET);
15719 			}
15720 			addr = net_mask & ipif->ipif_subnet;
15721 			ill_clear_bcast_mark(ill, addr);
15722 
15723 			net_addr = ~net_mask | addr;
15724 			ill_clear_bcast_mark(ill, net_addr);
15725 
15726 			subnet_netmask = ipif->ipif_net_mask;
15727 			addr = ipif->ipif_subnet;
15728 			ill_clear_bcast_mark(ill, addr);
15729 
15730 			subnet_addr = ~subnet_netmask | addr;
15731 			ill_clear_bcast_mark(ill, subnet_addr);
15732 		}
15733 	}
15734 
15735 	/*
15736 	 * Multicast Responsibility.
15737 	 *
15738 	 * If we have joined allmulti on this one, find a new member
15739 	 * in the group to join allmulti. As this ill is already part
15740 	 * of allmulti, we don't have to join on this one.
15741 	 *
15742 	 * If we have not joined allmulti on this one, there is no
15743 	 * responsibility to handoff. But we need to take new
15744 	 * responsibility i.e, join allmulti on this one if we need
15745 	 * to.
15746 	 */
15747 	if (ill->ill_join_allmulti) {
15748 		(void) ill_nominate_mcast_rcv(illgrp);
15749 	} else {
15750 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15751 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15752 				(void) ip_join_allmulti(ill->ill_ipif);
15753 				break;
15754 			}
15755 		}
15756 	}
15757 
15758 	/*
15759 	 * We intentionally do the flushing of IRE_CACHES only matching
15760 	 * on the ill and not on groups. Note that we are already deleted
15761 	 * from the group.
15762 	 *
15763 	 * This will make sure that all IRE_CACHES whose stq is pointing
15764 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15765 	 * deleted and IRE_CACHES that are not pointing at this ill will
15766 	 * be left alone.
15767 	 */
15768 	ire_walk_ill(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_CACHE,
15769 	    illgrp_cache_delete, ill, ill);
15770 
15771 	/*
15772 	 * Some conn may have cached one of the IREs deleted above. By removing
15773 	 * the ire reference, we clean up the extra reference to the ill held in
15774 	 * ire->ire_stq.
15775 	 */
15776 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15777 
15778 	/*
15779 	 * Re-do source address selection for all the members in the
15780 	 * group, if they borrowed source address from one of the ipifs
15781 	 * in this ill.
15782 	 */
15783 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15784 		if (ill->ill_isv6) {
15785 			ipif_update_other_ipifs_v6(ipif, illgrp);
15786 		} else {
15787 			ipif_update_other_ipifs(ipif, illgrp);
15788 		}
15789 	}
15790 }
15791 
15792 /*
15793  * Delete the ill from the group. The caller makes sure that it is
15794  * in a group and it okay to delete from the group. So, we always
15795  * delete here.
15796  */
15797 static void
15798 illgrp_delete(ill_t *ill)
15799 {
15800 	ill_group_t *illgrp;
15801 	ill_group_t *tmpg;
15802 	ill_t *tmp_ill;
15803 	ip_stack_t	*ipst = ill->ill_ipst;
15804 
15805 	/*
15806 	 * Reset illgrp_ill_schednext if it was pointing at us.
15807 	 * We need to do this before we set ill_group to NULL.
15808 	 */
15809 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15810 	mutex_enter(&ill->ill_lock);
15811 
15812 	illgrp_reset_schednext(ill);
15813 
15814 	illgrp = ill->ill_group;
15815 
15816 	/* Delete the ill from illgrp. */
15817 	if (illgrp->illgrp_ill == ill) {
15818 		illgrp->illgrp_ill = ill->ill_group_next;
15819 	} else {
15820 		tmp_ill = illgrp->illgrp_ill;
15821 		while (tmp_ill->ill_group_next != ill) {
15822 			tmp_ill = tmp_ill->ill_group_next;
15823 			ASSERT(tmp_ill != NULL);
15824 		}
15825 		tmp_ill->ill_group_next = ill->ill_group_next;
15826 	}
15827 	ill->ill_group = NULL;
15828 	ill->ill_group_next = NULL;
15829 
15830 	illgrp->illgrp_ill_count--;
15831 	mutex_exit(&ill->ill_lock);
15832 	rw_exit(&ipst->ips_ill_g_lock);
15833 
15834 	/*
15835 	 * As this ill is leaving the group, we need to hand off
15836 	 * the responsibilities to the other ills in the group, if
15837 	 * this ill had some responsibilities.
15838 	 */
15839 
15840 	ill_handoff_responsibility(ill, illgrp);
15841 
15842 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15843 
15844 	if (illgrp->illgrp_ill_count == 0) {
15845 
15846 		ASSERT(illgrp->illgrp_ill == NULL);
15847 		if (ill->ill_isv6) {
15848 			if (illgrp == ipst->ips_illgrp_head_v6) {
15849 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15850 			} else {
15851 				tmpg = ipst->ips_illgrp_head_v6;
15852 				while (tmpg->illgrp_next != illgrp) {
15853 					tmpg = tmpg->illgrp_next;
15854 					ASSERT(tmpg != NULL);
15855 				}
15856 				tmpg->illgrp_next = illgrp->illgrp_next;
15857 			}
15858 		} else {
15859 			if (illgrp == ipst->ips_illgrp_head_v4) {
15860 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15861 			} else {
15862 				tmpg = ipst->ips_illgrp_head_v4;
15863 				while (tmpg->illgrp_next != illgrp) {
15864 					tmpg = tmpg->illgrp_next;
15865 					ASSERT(tmpg != NULL);
15866 				}
15867 				tmpg->illgrp_next = illgrp->illgrp_next;
15868 			}
15869 		}
15870 		mutex_destroy(&illgrp->illgrp_lock);
15871 		mi_free(illgrp);
15872 	}
15873 	rw_exit(&ipst->ips_ill_g_lock);
15874 
15875 	/*
15876 	 * Even though the ill is out of the group its not necessary
15877 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15878 	 * We will split the ipsq when phyint_groupname is set to NULL.
15879 	 */
15880 
15881 	/*
15882 	 * Send a routing sockets message if we are deleting from
15883 	 * groups with names.
15884 	 */
15885 	if (ill->ill_phyint->phyint_groupname_len != 0)
15886 		ip_rts_ifmsg(ill->ill_ipif);
15887 }
15888 
15889 /*
15890  * Re-do source address selection. This is normally called when
15891  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15892  * ipif comes up.
15893  */
15894 void
15895 ill_update_source_selection(ill_t *ill)
15896 {
15897 	ipif_t *ipif;
15898 
15899 	ASSERT(IAM_WRITER_ILL(ill));
15900 
15901 	if (ill->ill_group != NULL)
15902 		ill = ill->ill_group->illgrp_ill;
15903 
15904 	for (; ill != NULL; ill = ill->ill_group_next) {
15905 		for (ipif = ill->ill_ipif; ipif != NULL;
15906 		    ipif = ipif->ipif_next) {
15907 			if (ill->ill_isv6)
15908 				ipif_recreate_interface_routes_v6(NULL, ipif);
15909 			else
15910 				ipif_recreate_interface_routes(NULL, ipif);
15911 		}
15912 	}
15913 }
15914 
15915 /*
15916  * Insert ill in a group headed by illgrp_head. The caller can either
15917  * pass a groupname in which case we search for a group with the
15918  * same name to insert in or pass a group to insert in. This function
15919  * would only search groups with names.
15920  *
15921  * NOTE : The caller should make sure that there is at least one ipif
15922  *	  UP on this ill so that illgrp_scheduler can pick this ill
15923  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15924  *	  already sent a DL_UNBIND to the driver and we don't want to
15925  *	  send anymore packets. We don't assert for ipif_up_count
15926  *	  to be greater than zero, because ipif_up_done wants to call
15927  *	  this function before bumping up the ipif_up_count. See
15928  *	  ipif_up_done() for details.
15929  */
15930 int
15931 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15932     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15933 {
15934 	ill_group_t *illgrp;
15935 	ill_t *prev_ill;
15936 	phyint_t *phyi;
15937 	ip_stack_t	*ipst = ill->ill_ipst;
15938 
15939 	ASSERT(ill->ill_group == NULL);
15940 
15941 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15942 	mutex_enter(&ill->ill_lock);
15943 
15944 	if (groupname != NULL) {
15945 		/*
15946 		 * Look for a group with a matching groupname to insert.
15947 		 */
15948 		for (illgrp = *illgrp_head; illgrp != NULL;
15949 		    illgrp = illgrp->illgrp_next) {
15950 
15951 			ill_t *tmp_ill;
15952 
15953 			/*
15954 			 * If we have an ill_group_t in the list which has
15955 			 * no ill_t assigned then we must be in the process of
15956 			 * removing this group. We skip this as illgrp_delete()
15957 			 * will remove it from the list.
15958 			 */
15959 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15960 				ASSERT(illgrp->illgrp_ill_count == 0);
15961 				continue;
15962 			}
15963 
15964 			ASSERT(tmp_ill->ill_phyint != NULL);
15965 			phyi = tmp_ill->ill_phyint;
15966 			/*
15967 			 * Look at groups which has names only.
15968 			 */
15969 			if (phyi->phyint_groupname_len == 0)
15970 				continue;
15971 			/*
15972 			 * Names are stored in the phyint common to both
15973 			 * IPv4 and IPv6.
15974 			 */
15975 			if (mi_strcmp(phyi->phyint_groupname,
15976 			    groupname) == 0) {
15977 				break;
15978 			}
15979 		}
15980 	} else {
15981 		/*
15982 		 * If the caller passes in a NULL "grp_to_insert", we
15983 		 * allocate one below and insert this singleton.
15984 		 */
15985 		illgrp = grp_to_insert;
15986 	}
15987 
15988 	ill->ill_group_next = NULL;
15989 
15990 	if (illgrp == NULL) {
15991 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
15992 		if (illgrp == NULL) {
15993 			return (ENOMEM);
15994 		}
15995 		illgrp->illgrp_next = *illgrp_head;
15996 		*illgrp_head = illgrp;
15997 		illgrp->illgrp_ill = ill;
15998 		illgrp->illgrp_ill_count = 1;
15999 		ill->ill_group = illgrp;
16000 		/*
16001 		 * Used in illgrp_scheduler to protect multiple threads
16002 		 * from traversing the list.
16003 		 */
16004 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16005 	} else {
16006 		ASSERT(ill->ill_net_type ==
16007 		    illgrp->illgrp_ill->ill_net_type);
16008 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16009 
16010 		/* Insert ill at tail of this group */
16011 		prev_ill = illgrp->illgrp_ill;
16012 		while (prev_ill->ill_group_next != NULL)
16013 			prev_ill = prev_ill->ill_group_next;
16014 		prev_ill->ill_group_next = ill;
16015 		ill->ill_group = illgrp;
16016 		illgrp->illgrp_ill_count++;
16017 		/*
16018 		 * Inherit group properties. Currently only forwarding
16019 		 * is the property we try to keep the same with all the
16020 		 * ills. When there are more, we will abstract this into
16021 		 * a function.
16022 		 */
16023 		ill->ill_flags &= ~ILLF_ROUTER;
16024 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16025 	}
16026 	mutex_exit(&ill->ill_lock);
16027 	rw_exit(&ipst->ips_ill_g_lock);
16028 
16029 	/*
16030 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16031 	 *    may be zero as it has not yet been bumped. But the ires
16032 	 *    have already been added. So, we do the nomination here
16033 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16034 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16035 	 *    ill_ipif_up_count here while nominating broadcast ires for
16036 	 *    receive.
16037 	 *
16038 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16039 	 *    to group them properly as ire_add() has already happened
16040 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16041 	 *    case, we need to do it here anyway.
16042 	 */
16043 	if (!ill->ill_isv6) {
16044 		ill_group_bcast_for_xmit(ill);
16045 		ill_nominate_bcast_rcv(illgrp);
16046 	}
16047 
16048 	if (!ipif_is_coming_up) {
16049 		/*
16050 		 * When ipif_up_done() calls this function, the multicast
16051 		 * groups have not been joined yet. So, there is no point in
16052 		 * nomination. ip_join_allmulti will handle groups when
16053 		 * ill_recover_multicast is called from ipif_up_done() later.
16054 		 */
16055 		(void) ill_nominate_mcast_rcv(illgrp);
16056 		/*
16057 		 * ipif_up_done calls ill_update_source_selection
16058 		 * anyway. Moreover, we don't want to re-create
16059 		 * interface routes while ipif_up_done() still has reference
16060 		 * to them. Refer to ipif_up_done() for more details.
16061 		 */
16062 		ill_update_source_selection(ill);
16063 	}
16064 
16065 	/*
16066 	 * Send a routing sockets message if we are inserting into
16067 	 * groups with names.
16068 	 */
16069 	if (groupname != NULL)
16070 		ip_rts_ifmsg(ill->ill_ipif);
16071 	return (0);
16072 }
16073 
16074 /*
16075  * Return the first phyint matching the groupname. There could
16076  * be more than one when there are ill groups.
16077  *
16078  * If 'usable' is set, then we exclude ones that are marked with any of
16079  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16080  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16081  * emulation of ipmp.
16082  */
16083 phyint_t *
16084 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16085 {
16086 	phyint_t *phyi;
16087 
16088 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16089 	/*
16090 	 * Group names are stored in the phyint - a common structure
16091 	 * to both IPv4 and IPv6.
16092 	 */
16093 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16094 	for (; phyi != NULL;
16095 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16096 	    phyi, AVL_AFTER)) {
16097 		if (phyi->phyint_groupname_len == 0)
16098 			continue;
16099 		/*
16100 		 * Skip the ones that should not be used since the callers
16101 		 * sometime use this for sending packets.
16102 		 */
16103 		if (usable && (phyi->phyint_flags &
16104 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16105 			continue;
16106 
16107 		ASSERT(phyi->phyint_groupname != NULL);
16108 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16109 			return (phyi);
16110 	}
16111 	return (NULL);
16112 }
16113 
16114 
16115 /*
16116  * Return the first usable phyint matching the group index. By 'usable'
16117  * we exclude ones that are marked ununsable with any of
16118  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16119  *
16120  * Used only for the ipmp/netinfo emulation of ipmp.
16121  */
16122 phyint_t *
16123 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16124 {
16125 	phyint_t *phyi;
16126 
16127 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16128 
16129 	if (!ipst->ips_ipmp_hook_emulation)
16130 		return (NULL);
16131 
16132 	/*
16133 	 * Group indicies are stored in the phyint - a common structure
16134 	 * to both IPv4 and IPv6.
16135 	 */
16136 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16137 	for (; phyi != NULL;
16138 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16139 	    phyi, AVL_AFTER)) {
16140 		/* Ignore the ones that do not have a group */
16141 		if (phyi->phyint_groupname_len == 0)
16142 			continue;
16143 
16144 		ASSERT(phyi->phyint_group_ifindex != 0);
16145 		/*
16146 		 * Skip the ones that should not be used since the callers
16147 		 * sometime use this for sending packets.
16148 		 */
16149 		if (phyi->phyint_flags &
16150 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16151 			continue;
16152 		if (phyi->phyint_group_ifindex == group_ifindex)
16153 			return (phyi);
16154 	}
16155 	return (NULL);
16156 }
16157 
16158 /*
16159  * MT notes on creation and deletion of IPMP groups
16160  *
16161  * Creation and deletion of IPMP groups introduce the need to merge or
16162  * split the associated serialization objects i.e the ipsq's. Normally all
16163  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16164  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16165  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16166  * is a need to change the <ill-ipsq> association and we have to operate on both
16167  * the source and destination IPMP groups. For eg. attempting to set the
16168  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16169  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16170  * source or destination IPMP group are mapped to a single ipsq for executing
16171  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16172  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16173  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16174  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16175  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16176  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16177  *
16178  * In the above example the ioctl handling code locates the current ipsq of hme0
16179  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16180  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16181  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16182  * the destination ipsq. If the destination ipsq is not busy, it also enters
16183  * the destination ipsq exclusively. Now the actual groupname setting operation
16184  * can proceed. If the destination ipsq is busy, the operation is enqueued
16185  * on the destination (merged) ipsq and will be handled in the unwind from
16186  * ipsq_exit.
16187  *
16188  * To prevent other threads accessing the ill while the group name change is
16189  * in progres, we bring down the ipifs which also removes the ill from the
16190  * group. The group is changed in phyint and when the first ipif on the ill
16191  * is brought up, the ill is inserted into the right IPMP group by
16192  * illgrp_insert.
16193  */
16194 /* ARGSUSED */
16195 int
16196 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16197     ip_ioctl_cmd_t *ipip, void *ifreq)
16198 {
16199 	int i;
16200 	char *tmp;
16201 	int namelen;
16202 	ill_t *ill = ipif->ipif_ill;
16203 	ill_t *ill_v4, *ill_v6;
16204 	int err = 0;
16205 	phyint_t *phyi;
16206 	phyint_t *phyi_tmp;
16207 	struct lifreq *lifr;
16208 	mblk_t	*mp1;
16209 	char *groupname;
16210 	ipsq_t *ipsq;
16211 	ip_stack_t	*ipst = ill->ill_ipst;
16212 
16213 	ASSERT(IAM_WRITER_IPIF(ipif));
16214 
16215 	/* Existance verified in ip_wput_nondata */
16216 	mp1 = mp->b_cont->b_cont;
16217 	lifr = (struct lifreq *)mp1->b_rptr;
16218 	groupname = lifr->lifr_groupname;
16219 
16220 	if (ipif->ipif_id != 0)
16221 		return (EINVAL);
16222 
16223 	phyi = ill->ill_phyint;
16224 	ASSERT(phyi != NULL);
16225 
16226 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16227 		return (EINVAL);
16228 
16229 	tmp = groupname;
16230 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16231 		;
16232 
16233 	if (i == LIFNAMSIZ) {
16234 		/* no null termination */
16235 		return (EINVAL);
16236 	}
16237 
16238 	/*
16239 	 * Calculate the namelen exclusive of the null
16240 	 * termination character.
16241 	 */
16242 	namelen = tmp - groupname;
16243 
16244 	ill_v4 = phyi->phyint_illv4;
16245 	ill_v6 = phyi->phyint_illv6;
16246 
16247 	/*
16248 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16249 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16250 	 * synchronization notes in ip.c
16251 	 */
16252 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16253 		return (EINVAL);
16254 	}
16255 
16256 	/*
16257 	 * mark the ill as changing.
16258 	 * this should queue all new requests on the syncq.
16259 	 */
16260 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16261 
16262 	if (ill_v4 != NULL)
16263 		ill_v4->ill_state_flags |= ILL_CHANGING;
16264 	if (ill_v6 != NULL)
16265 		ill_v6->ill_state_flags |= ILL_CHANGING;
16266 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16267 
16268 	if (namelen == 0) {
16269 		/*
16270 		 * Null string means remove this interface from the
16271 		 * existing group.
16272 		 */
16273 		if (phyi->phyint_groupname_len == 0) {
16274 			/*
16275 			 * Never was in a group.
16276 			 */
16277 			err = 0;
16278 			goto done;
16279 		}
16280 
16281 		/*
16282 		 * IPv4 or IPv6 may be temporarily out of the group when all
16283 		 * the ipifs are down. Thus, we need to check for ill_group to
16284 		 * be non-NULL.
16285 		 */
16286 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16287 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16288 			mutex_enter(&ill_v4->ill_lock);
16289 			if (!ill_is_quiescent(ill_v4)) {
16290 				/*
16291 				 * ipsq_pending_mp_add will not fail since
16292 				 * connp is NULL
16293 				 */
16294 				(void) ipsq_pending_mp_add(NULL,
16295 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16296 				mutex_exit(&ill_v4->ill_lock);
16297 				err = EINPROGRESS;
16298 				goto done;
16299 			}
16300 			mutex_exit(&ill_v4->ill_lock);
16301 		}
16302 
16303 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16304 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16305 			mutex_enter(&ill_v6->ill_lock);
16306 			if (!ill_is_quiescent(ill_v6)) {
16307 				(void) ipsq_pending_mp_add(NULL,
16308 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16309 				mutex_exit(&ill_v6->ill_lock);
16310 				err = EINPROGRESS;
16311 				goto done;
16312 			}
16313 			mutex_exit(&ill_v6->ill_lock);
16314 		}
16315 
16316 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16317 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16318 		mutex_enter(&phyi->phyint_lock);
16319 		ASSERT(phyi->phyint_groupname != NULL);
16320 		mi_free(phyi->phyint_groupname);
16321 		phyi->phyint_groupname = NULL;
16322 		phyi->phyint_groupname_len = 0;
16323 
16324 		/* Restore the ifindex used to be the per interface one */
16325 		phyi->phyint_group_ifindex = 0;
16326 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16327 		mutex_exit(&phyi->phyint_lock);
16328 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16329 		rw_exit(&ipst->ips_ill_g_lock);
16330 		err = ill_up_ipifs(ill, q, mp);
16331 
16332 		/*
16333 		 * set the split flag so that the ipsq can be split
16334 		 */
16335 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16336 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16337 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16338 
16339 	} else {
16340 		if (phyi->phyint_groupname_len != 0) {
16341 			ASSERT(phyi->phyint_groupname != NULL);
16342 			/* Are we inserting in the same group ? */
16343 			if (mi_strcmp(groupname,
16344 			    phyi->phyint_groupname) == 0) {
16345 				err = 0;
16346 				goto done;
16347 			}
16348 		}
16349 
16350 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16351 		/*
16352 		 * Merge ipsq for the group's.
16353 		 * This check is here as multiple groups/ills might be
16354 		 * sharing the same ipsq.
16355 		 * If we have to merege than the operation is restarted
16356 		 * on the new ipsq.
16357 		 */
16358 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16359 		if (phyi->phyint_ipsq != ipsq) {
16360 			rw_exit(&ipst->ips_ill_g_lock);
16361 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16362 			goto done;
16363 		}
16364 		/*
16365 		 * Running exclusive on new ipsq.
16366 		 */
16367 
16368 		ASSERT(ipsq != NULL);
16369 		ASSERT(ipsq->ipsq_writer == curthread);
16370 
16371 		/*
16372 		 * Check whether the ill_type and ill_net_type matches before
16373 		 * we allocate any memory so that the cleanup is easier.
16374 		 *
16375 		 * We can't group dissimilar ones as we can't load spread
16376 		 * packets across the group because of potential link-level
16377 		 * header differences.
16378 		 */
16379 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16380 		if (phyi_tmp != NULL) {
16381 			if ((ill_v4 != NULL &&
16382 			    phyi_tmp->phyint_illv4 != NULL) &&
16383 			    ((ill_v4->ill_net_type !=
16384 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16385 			    (ill_v4->ill_type !=
16386 			    phyi_tmp->phyint_illv4->ill_type))) {
16387 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16388 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16389 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16390 				rw_exit(&ipst->ips_ill_g_lock);
16391 				return (EINVAL);
16392 			}
16393 			if ((ill_v6 != NULL &&
16394 			    phyi_tmp->phyint_illv6 != NULL) &&
16395 			    ((ill_v6->ill_net_type !=
16396 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16397 			    (ill_v6->ill_type !=
16398 			    phyi_tmp->phyint_illv6->ill_type))) {
16399 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16400 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16401 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16402 				rw_exit(&ipst->ips_ill_g_lock);
16403 				return (EINVAL);
16404 			}
16405 		}
16406 
16407 		rw_exit(&ipst->ips_ill_g_lock);
16408 
16409 		/*
16410 		 * bring down all v4 ipifs.
16411 		 */
16412 		if (ill_v4 != NULL) {
16413 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16414 		}
16415 
16416 		/*
16417 		 * bring down all v6 ipifs.
16418 		 */
16419 		if (ill_v6 != NULL) {
16420 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16421 		}
16422 
16423 		/*
16424 		 * make sure all ipifs are down and there are no active
16425 		 * references. Call to ipsq_pending_mp_add will not fail
16426 		 * since connp is NULL.
16427 		 */
16428 		if (ill_v4 != NULL) {
16429 			mutex_enter(&ill_v4->ill_lock);
16430 			if (!ill_is_quiescent(ill_v4)) {
16431 				(void) ipsq_pending_mp_add(NULL,
16432 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16433 				mutex_exit(&ill_v4->ill_lock);
16434 				err = EINPROGRESS;
16435 				goto done;
16436 			}
16437 			mutex_exit(&ill_v4->ill_lock);
16438 		}
16439 
16440 		if (ill_v6 != NULL) {
16441 			mutex_enter(&ill_v6->ill_lock);
16442 			if (!ill_is_quiescent(ill_v6)) {
16443 				(void) ipsq_pending_mp_add(NULL,
16444 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16445 				mutex_exit(&ill_v6->ill_lock);
16446 				err = EINPROGRESS;
16447 				goto done;
16448 			}
16449 			mutex_exit(&ill_v6->ill_lock);
16450 		}
16451 
16452 		/*
16453 		 * allocate including space for null terminator
16454 		 * before we insert.
16455 		 */
16456 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16457 		if (tmp == NULL)
16458 			return (ENOMEM);
16459 
16460 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16461 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16462 		mutex_enter(&phyi->phyint_lock);
16463 		if (phyi->phyint_groupname_len != 0) {
16464 			ASSERT(phyi->phyint_groupname != NULL);
16465 			mi_free(phyi->phyint_groupname);
16466 		}
16467 
16468 		/*
16469 		 * setup the new group name.
16470 		 */
16471 		phyi->phyint_groupname = tmp;
16472 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16473 		phyi->phyint_groupname_len = namelen + 1;
16474 
16475 		if (ipst->ips_ipmp_hook_emulation) {
16476 			/*
16477 			 * If the group already exists we use the existing
16478 			 * group_ifindex, otherwise we pick a new index here.
16479 			 */
16480 			if (phyi_tmp != NULL) {
16481 				phyi->phyint_group_ifindex =
16482 				    phyi_tmp->phyint_group_ifindex;
16483 			} else {
16484 				/* XXX We need a recovery strategy here. */
16485 				if (!ip_assign_ifindex(
16486 				    &phyi->phyint_group_ifindex, ipst))
16487 					cmn_err(CE_PANIC,
16488 					    "ip_assign_ifindex() failed");
16489 			}
16490 		}
16491 		/*
16492 		 * Select whether the netinfo and hook use the per-interface
16493 		 * or per-group ifindex.
16494 		 */
16495 		if (ipst->ips_ipmp_hook_emulation)
16496 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16497 		else
16498 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16499 
16500 		if (ipst->ips_ipmp_hook_emulation &&
16501 		    phyi_tmp != NULL) {
16502 			/* First phyint in group - group PLUMB event */
16503 			ill_nic_info_plumb(ill, B_TRUE);
16504 		}
16505 		mutex_exit(&phyi->phyint_lock);
16506 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16507 		rw_exit(&ipst->ips_ill_g_lock);
16508 
16509 		err = ill_up_ipifs(ill, q, mp);
16510 	}
16511 
16512 done:
16513 	/*
16514 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16515 	 */
16516 	if (err != EINPROGRESS) {
16517 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16518 		if (ill_v4 != NULL)
16519 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16520 		if (ill_v6 != NULL)
16521 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16522 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16523 	}
16524 	return (err);
16525 }
16526 
16527 /* ARGSUSED */
16528 int
16529 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16530     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16531 {
16532 	ill_t *ill;
16533 	phyint_t *phyi;
16534 	struct lifreq *lifr;
16535 	mblk_t	*mp1;
16536 
16537 	/* Existence verified in ip_wput_nondata */
16538 	mp1 = mp->b_cont->b_cont;
16539 	lifr = (struct lifreq *)mp1->b_rptr;
16540 	ill = ipif->ipif_ill;
16541 	phyi = ill->ill_phyint;
16542 
16543 	lifr->lifr_groupname[0] = '\0';
16544 	/*
16545 	 * ill_group may be null if all the interfaces
16546 	 * are down. But still, the phyint should always
16547 	 * hold the name.
16548 	 */
16549 	if (phyi->phyint_groupname_len != 0) {
16550 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16551 		    phyi->phyint_groupname_len);
16552 	}
16553 
16554 	return (0);
16555 }
16556 
16557 
16558 typedef struct conn_move_s {
16559 	ill_t	*cm_from_ill;
16560 	ill_t	*cm_to_ill;
16561 	int	cm_ifindex;
16562 } conn_move_t;
16563 
16564 /*
16565  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16566  */
16567 static void
16568 conn_move(conn_t *connp, caddr_t arg)
16569 {
16570 	conn_move_t *connm;
16571 	int ifindex;
16572 	int i;
16573 	ill_t *from_ill;
16574 	ill_t *to_ill;
16575 	ilg_t *ilg;
16576 	ilm_t *ret_ilm;
16577 
16578 	connm = (conn_move_t *)arg;
16579 	ifindex = connm->cm_ifindex;
16580 	from_ill = connm->cm_from_ill;
16581 	to_ill = connm->cm_to_ill;
16582 
16583 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16584 
16585 	/* All multicast fields protected by conn_lock */
16586 	mutex_enter(&connp->conn_lock);
16587 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16588 	if ((connp->conn_outgoing_ill == from_ill) &&
16589 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16590 		connp->conn_outgoing_ill = to_ill;
16591 		connp->conn_incoming_ill = to_ill;
16592 	}
16593 
16594 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16595 
16596 	if ((connp->conn_multicast_ill == from_ill) &&
16597 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16598 		connp->conn_multicast_ill = connm->cm_to_ill;
16599 	}
16600 
16601 	/*
16602 	 * Change the ilg_ill to point to the new one. This assumes
16603 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16604 	 * has been told to receive packets on this interface.
16605 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16606 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16607 	 * some ilms may not have moved. We check to see whether
16608 	 * the ilms have moved to to_ill. We can't check on from_ill
16609 	 * as in the process of moving, we could have split an ilm
16610 	 * in to two - which has the same orig_ifindex and v6group.
16611 	 *
16612 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16613 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16614 	 */
16615 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16616 		ilg = &connp->conn_ilg[i];
16617 		if ((ilg->ilg_ill == from_ill) &&
16618 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16619 			/* ifindex != 0 indicates failback */
16620 			if (ifindex != 0) {
16621 				connp->conn_ilg[i].ilg_ill = to_ill;
16622 				continue;
16623 			}
16624 
16625 			mutex_enter(&to_ill->ill_lock);
16626 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16627 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16628 			    connp->conn_zoneid);
16629 			mutex_exit(&to_ill->ill_lock);
16630 
16631 			if (ret_ilm != NULL)
16632 				connp->conn_ilg[i].ilg_ill = to_ill;
16633 		}
16634 	}
16635 	mutex_exit(&connp->conn_lock);
16636 }
16637 
16638 static void
16639 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16640 {
16641 	conn_move_t connm;
16642 	ip_stack_t	*ipst = from_ill->ill_ipst;
16643 
16644 	connm.cm_from_ill = from_ill;
16645 	connm.cm_to_ill = to_ill;
16646 	connm.cm_ifindex = ifindex;
16647 
16648 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16649 }
16650 
16651 /*
16652  * ilm has been moved from from_ill to to_ill.
16653  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16654  * appropriately.
16655  *
16656  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16657  *	  the code there de-references ipif_ill to get the ill to
16658  *	  send multicast requests. It does not work as ipif is on its
16659  *	  move and already moved when this function is called.
16660  *	  Thus, we need to use from_ill and to_ill send down multicast
16661  *	  requests.
16662  */
16663 static void
16664 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16665 {
16666 	ipif_t *ipif;
16667 	ilm_t *ilm;
16668 
16669 	/*
16670 	 * See whether we need to send down DL_ENABMULTI_REQ on
16671 	 * to_ill as ilm has just been added.
16672 	 */
16673 	ASSERT(IAM_WRITER_ILL(to_ill));
16674 	ASSERT(IAM_WRITER_ILL(from_ill));
16675 
16676 	ILM_WALKER_HOLD(to_ill);
16677 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16678 
16679 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16680 			continue;
16681 		/*
16682 		 * no locks held, ill/ipif cannot dissappear as long
16683 		 * as we are writer.
16684 		 */
16685 		ipif = to_ill->ill_ipif;
16686 		/*
16687 		 * No need to hold any lock as we are the writer and this
16688 		 * can only be changed by a writer.
16689 		 */
16690 		ilm->ilm_is_new = B_FALSE;
16691 
16692 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16693 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16694 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16695 			    "resolver\n"));
16696 			continue;		/* Must be IRE_IF_NORESOLVER */
16697 		}
16698 
16699 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16700 			ip1dbg(("ilm_send_multicast_reqs: "
16701 			    "to_ill MULTI_BCAST\n"));
16702 			goto from;
16703 		}
16704 
16705 		if (to_ill->ill_isv6)
16706 			mld_joingroup(ilm);
16707 		else
16708 			igmp_joingroup(ilm);
16709 
16710 		if (to_ill->ill_ipif_up_count == 0) {
16711 			/*
16712 			 * Nobody there. All multicast addresses will be
16713 			 * re-joined when we get the DL_BIND_ACK bringing the
16714 			 * interface up.
16715 			 */
16716 			ilm->ilm_notify_driver = B_FALSE;
16717 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16718 			goto from;
16719 		}
16720 
16721 		/*
16722 		 * For allmulti address, we want to join on only one interface.
16723 		 * Checking for ilm_numentries_v6 is not correct as you may
16724 		 * find an ilm with zero address on to_ill, but we may not
16725 		 * have nominated to_ill for receiving. Thus, if we have
16726 		 * nominated from_ill (ill_join_allmulti is set), nominate
16727 		 * only if to_ill is not already nominated (to_ill normally
16728 		 * should not have been nominated if "from_ill" has already
16729 		 * been nominated. As we don't prevent failovers from happening
16730 		 * across groups, we don't assert).
16731 		 */
16732 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16733 			/*
16734 			 * There is no need to hold ill locks as we are
16735 			 * writer on both ills and when ill_join_allmulti
16736 			 * is changed the thread is always a writer.
16737 			 */
16738 			if (from_ill->ill_join_allmulti &&
16739 			    !to_ill->ill_join_allmulti) {
16740 				(void) ip_join_allmulti(to_ill->ill_ipif);
16741 			}
16742 		} else if (ilm->ilm_notify_driver) {
16743 
16744 			/*
16745 			 * This is a newly moved ilm so we need to tell the
16746 			 * driver about the new group. There can be more than
16747 			 * one ilm's for the same group in the list each with a
16748 			 * different orig_ifindex. We have to inform the driver
16749 			 * once. In ilm_move_v[4,6] we only set the flag
16750 			 * ilm_notify_driver for the first ilm.
16751 			 */
16752 
16753 			(void) ip_ll_send_enabmulti_req(to_ill,
16754 			    &ilm->ilm_v6addr);
16755 		}
16756 
16757 		ilm->ilm_notify_driver = B_FALSE;
16758 
16759 		/*
16760 		 * See whether we need to send down DL_DISABMULTI_REQ on
16761 		 * from_ill as ilm has just been removed.
16762 		 */
16763 from:
16764 		ipif = from_ill->ill_ipif;
16765 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16766 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16767 			ip1dbg(("ilm_send_multicast_reqs: "
16768 			    "from_ill not resolver\n"));
16769 			continue;		/* Must be IRE_IF_NORESOLVER */
16770 		}
16771 
16772 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16773 			ip1dbg(("ilm_send_multicast_reqs: "
16774 			    "from_ill MULTI_BCAST\n"));
16775 			continue;
16776 		}
16777 
16778 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16779 			if (from_ill->ill_join_allmulti)
16780 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16781 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16782 			(void) ip_ll_send_disabmulti_req(from_ill,
16783 			    &ilm->ilm_v6addr);
16784 		}
16785 	}
16786 	ILM_WALKER_RELE(to_ill);
16787 }
16788 
16789 /*
16790  * This function is called when all multicast memberships needs
16791  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16792  * called only once unlike the IPv4 counterpart where it is called after
16793  * every logical interface is moved. The reason is due to multicast
16794  * memberships are joined using an interface address in IPv4 while in
16795  * IPv6, interface index is used.
16796  */
16797 static void
16798 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16799 {
16800 	ilm_t	*ilm;
16801 	ilm_t	*ilm_next;
16802 	ilm_t	*new_ilm;
16803 	ilm_t	**ilmp;
16804 	int	count;
16805 	char buf[INET6_ADDRSTRLEN];
16806 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16807 	ip_stack_t	*ipst = from_ill->ill_ipst;
16808 
16809 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16810 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16811 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16812 
16813 	if (ifindex == 0) {
16814 		/*
16815 		 * Form the solicited node mcast address which is used later.
16816 		 */
16817 		ipif_t *ipif;
16818 
16819 		ipif = from_ill->ill_ipif;
16820 		ASSERT(ipif->ipif_id == 0);
16821 
16822 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16823 	}
16824 
16825 	ilmp = &from_ill->ill_ilm;
16826 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16827 		ilm_next = ilm->ilm_next;
16828 
16829 		if (ilm->ilm_flags & ILM_DELETED) {
16830 			ilmp = &ilm->ilm_next;
16831 			continue;
16832 		}
16833 
16834 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16835 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16836 		ASSERT(ilm->ilm_orig_ifindex != 0);
16837 		if (ilm->ilm_orig_ifindex == ifindex) {
16838 			/*
16839 			 * We are failing back multicast memberships.
16840 			 * If the same ilm exists in to_ill, it means somebody
16841 			 * has joined the same group there e.g. ff02::1
16842 			 * is joined within the kernel when the interfaces
16843 			 * came UP.
16844 			 */
16845 			ASSERT(ilm->ilm_ipif == NULL);
16846 			if (new_ilm != NULL) {
16847 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16848 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16849 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16850 					new_ilm->ilm_is_new = B_TRUE;
16851 				}
16852 			} else {
16853 				/*
16854 				 * check if we can just move the ilm
16855 				 */
16856 				if (from_ill->ill_ilm_walker_cnt != 0) {
16857 					/*
16858 					 * We have walkers we cannot move
16859 					 * the ilm, so allocate a new ilm,
16860 					 * this (old) ilm will be marked
16861 					 * ILM_DELETED at the end of the loop
16862 					 * and will be freed when the
16863 					 * last walker exits.
16864 					 */
16865 					new_ilm = (ilm_t *)mi_zalloc
16866 					    (sizeof (ilm_t));
16867 					if (new_ilm == NULL) {
16868 						ip0dbg(("ilm_move_v6: "
16869 						    "FAILBACK of IPv6"
16870 						    " multicast address %s : "
16871 						    "from %s to"
16872 						    " %s failed : ENOMEM \n",
16873 						    inet_ntop(AF_INET6,
16874 						    &ilm->ilm_v6addr, buf,
16875 						    sizeof (buf)),
16876 						    from_ill->ill_name,
16877 						    to_ill->ill_name));
16878 
16879 							ilmp = &ilm->ilm_next;
16880 							continue;
16881 					}
16882 					*new_ilm = *ilm;
16883 					/*
16884 					 * we don't want new_ilm linked to
16885 					 * ilm's filter list.
16886 					 */
16887 					new_ilm->ilm_filter = NULL;
16888 				} else {
16889 					/*
16890 					 * No walkers we can move the ilm.
16891 					 * lets take it out of the list.
16892 					 */
16893 					*ilmp = ilm->ilm_next;
16894 					ilm->ilm_next = NULL;
16895 					DTRACE_PROBE3(ill__decr__cnt,
16896 					    (ill_t *), from_ill,
16897 					    (char *), "ilm", (void *), ilm);
16898 					ASSERT(from_ill->ill_ilm_cnt > 0);
16899 					from_ill->ill_ilm_cnt--;
16900 
16901 					new_ilm = ilm;
16902 				}
16903 
16904 				/*
16905 				 * if this is the first ilm for the group
16906 				 * set ilm_notify_driver so that we notify the
16907 				 * driver in ilm_send_multicast_reqs.
16908 				 */
16909 				if (ilm_lookup_ill_v6(to_ill,
16910 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16911 					new_ilm->ilm_notify_driver = B_TRUE;
16912 
16913 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
16914 				    (char *), "ilm", (void *), new_ilm);
16915 				new_ilm->ilm_ill = to_ill;
16916 				to_ill->ill_ilm_cnt++;
16917 
16918 				/* Add to the to_ill's list */
16919 				new_ilm->ilm_next = to_ill->ill_ilm;
16920 				to_ill->ill_ilm = new_ilm;
16921 				/*
16922 				 * set the flag so that mld_joingroup is
16923 				 * called in ilm_send_multicast_reqs().
16924 				 */
16925 				new_ilm->ilm_is_new = B_TRUE;
16926 			}
16927 			goto bottom;
16928 		} else if (ifindex != 0) {
16929 			/*
16930 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16931 			 * has not matched above, look at the next ilm.
16932 			 */
16933 			ilmp = &ilm->ilm_next;
16934 			continue;
16935 		}
16936 		/*
16937 		 * If we are here, it means ifindex is 0. Failover
16938 		 * everything.
16939 		 *
16940 		 * We need to handle solicited node mcast address
16941 		 * and all_nodes mcast address differently as they
16942 		 * are joined witin the kenrel (ipif_multicast_up)
16943 		 * and potentially from the userland. We are called
16944 		 * after the ipifs of from_ill has been moved.
16945 		 * If we still find ilms on ill with solicited node
16946 		 * mcast address or all_nodes mcast address, it must
16947 		 * belong to the UP interface that has not moved e.g.
16948 		 * ipif_id 0 with the link local prefix does not move.
16949 		 * We join this on the new ill accounting for all the
16950 		 * userland memberships so that applications don't
16951 		 * see any failure.
16952 		 *
16953 		 * We need to make sure that we account only for the
16954 		 * solicited node and all node multicast addresses
16955 		 * that was brought UP on these. In the case of
16956 		 * a failover from A to B, we might have ilms belonging
16957 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16958 		 * for the membership from the userland. If we are failing
16959 		 * over from B to C now, we will find the ones belonging
16960 		 * to A on B. These don't account for the ill_ipif_up_count.
16961 		 * They just move from B to C. The check below on
16962 		 * ilm_orig_ifindex ensures that.
16963 		 */
16964 		if ((ilm->ilm_orig_ifindex ==
16965 		    from_ill->ill_phyint->phyint_ifindex) &&
16966 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16967 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16968 		    &ilm->ilm_v6addr))) {
16969 			ASSERT(ilm->ilm_refcnt > 0);
16970 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16971 			/*
16972 			 * For indentation reasons, we are not using a
16973 			 * "else" here.
16974 			 */
16975 			if (count == 0) {
16976 				ilmp = &ilm->ilm_next;
16977 				continue;
16978 			}
16979 			ilm->ilm_refcnt -= count;
16980 			if (new_ilm != NULL) {
16981 				/*
16982 				 * Can find one with the same
16983 				 * ilm_orig_ifindex, if we are failing
16984 				 * over to a STANDBY. This happens
16985 				 * when somebody wants to join a group
16986 				 * on a STANDBY interface and we
16987 				 * internally join on a different one.
16988 				 * If we had joined on from_ill then, a
16989 				 * failover now will find a new ilm
16990 				 * with this index.
16991 				 */
16992 				ip1dbg(("ilm_move_v6: FAILOVER, found"
16993 				    " new ilm on %s, group address %s\n",
16994 				    to_ill->ill_name,
16995 				    inet_ntop(AF_INET6,
16996 				    &ilm->ilm_v6addr, buf,
16997 				    sizeof (buf))));
16998 				new_ilm->ilm_refcnt += count;
16999 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17000 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17001 					new_ilm->ilm_is_new = B_TRUE;
17002 				}
17003 			} else {
17004 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17005 				if (new_ilm == NULL) {
17006 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17007 					    " multicast address %s : from %s to"
17008 					    " %s failed : ENOMEM \n",
17009 					    inet_ntop(AF_INET6,
17010 					    &ilm->ilm_v6addr, buf,
17011 					    sizeof (buf)), from_ill->ill_name,
17012 					    to_ill->ill_name));
17013 					ilmp = &ilm->ilm_next;
17014 					continue;
17015 				}
17016 				*new_ilm = *ilm;
17017 				new_ilm->ilm_filter = NULL;
17018 				new_ilm->ilm_refcnt = count;
17019 				new_ilm->ilm_timer = INFINITY;
17020 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17021 				new_ilm->ilm_is_new = B_TRUE;
17022 				/*
17023 				 * If the to_ill has not joined this
17024 				 * group we need to tell the driver in
17025 				 * ill_send_multicast_reqs.
17026 				 */
17027 				if (ilm_lookup_ill_v6(to_ill,
17028 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17029 					new_ilm->ilm_notify_driver = B_TRUE;
17030 
17031 				new_ilm->ilm_ill = to_ill;
17032 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17033 				    (char *), "ilm", (void *), new_ilm);
17034 				to_ill->ill_ilm_cnt++;
17035 
17036 				/* Add to the to_ill's list */
17037 				new_ilm->ilm_next = to_ill->ill_ilm;
17038 				to_ill->ill_ilm = new_ilm;
17039 				ASSERT(new_ilm->ilm_ipif == NULL);
17040 			}
17041 			if (ilm->ilm_refcnt == 0) {
17042 				goto bottom;
17043 			} else {
17044 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17045 				CLEAR_SLIST(new_ilm->ilm_filter);
17046 				ilmp = &ilm->ilm_next;
17047 			}
17048 			continue;
17049 		} else {
17050 			/*
17051 			 * ifindex = 0 means, move everything pointing at
17052 			 * from_ill. We are doing this becuase ill has
17053 			 * either FAILED or became INACTIVE.
17054 			 *
17055 			 * As we would like to move things later back to
17056 			 * from_ill, we want to retain the identity of this
17057 			 * ilm. Thus, we don't blindly increment the reference
17058 			 * count on the ilms matching the address alone. We
17059 			 * need to match on the ilm_orig_index also. new_ilm
17060 			 * was obtained by matching ilm_orig_index also.
17061 			 */
17062 			if (new_ilm != NULL) {
17063 				/*
17064 				 * This is possible only if a previous restore
17065 				 * was incomplete i.e restore to
17066 				 * ilm_orig_ifindex left some ilms because
17067 				 * of some failures. Thus when we are failing
17068 				 * again, we might find our old friends there.
17069 				 */
17070 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17071 				    " on %s, group address %s\n",
17072 				    to_ill->ill_name,
17073 				    inet_ntop(AF_INET6,
17074 				    &ilm->ilm_v6addr, buf,
17075 				    sizeof (buf))));
17076 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17077 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17078 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17079 					new_ilm->ilm_is_new = B_TRUE;
17080 				}
17081 			} else {
17082 				if (from_ill->ill_ilm_walker_cnt != 0) {
17083 					new_ilm = (ilm_t *)
17084 					    mi_zalloc(sizeof (ilm_t));
17085 					if (new_ilm == NULL) {
17086 						ip0dbg(("ilm_move_v6: "
17087 						    "FAILOVER of IPv6"
17088 						    " multicast address %s : "
17089 						    "from %s to"
17090 						    " %s failed : ENOMEM \n",
17091 						    inet_ntop(AF_INET6,
17092 						    &ilm->ilm_v6addr, buf,
17093 						    sizeof (buf)),
17094 						    from_ill->ill_name,
17095 						    to_ill->ill_name));
17096 
17097 							ilmp = &ilm->ilm_next;
17098 							continue;
17099 					}
17100 					*new_ilm = *ilm;
17101 					new_ilm->ilm_filter = NULL;
17102 				} else {
17103 					*ilmp = ilm->ilm_next;
17104 					DTRACE_PROBE3(ill__decr__cnt,
17105 					    (ill_t *), from_ill,
17106 					    (char *), "ilm", (void *), ilm);
17107 					ASSERT(from_ill->ill_ilm_cnt > 0);
17108 					from_ill->ill_ilm_cnt--;
17109 
17110 					new_ilm = ilm;
17111 				}
17112 				/*
17113 				 * If the to_ill has not joined this
17114 				 * group we need to tell the driver in
17115 				 * ill_send_multicast_reqs.
17116 				 */
17117 				if (ilm_lookup_ill_v6(to_ill,
17118 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17119 					new_ilm->ilm_notify_driver = B_TRUE;
17120 
17121 				/* Add to the to_ill's list */
17122 				new_ilm->ilm_next = to_ill->ill_ilm;
17123 				to_ill->ill_ilm = new_ilm;
17124 				ASSERT(ilm->ilm_ipif == NULL);
17125 				new_ilm->ilm_ill = to_ill;
17126 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17127 				    (char *), "ilm", (void *), new_ilm);
17128 				to_ill->ill_ilm_cnt++;
17129 				new_ilm->ilm_is_new = B_TRUE;
17130 			}
17131 
17132 		}
17133 
17134 bottom:
17135 		/*
17136 		 * Revert multicast filter state to (EXCLUDE, NULL).
17137 		 * new_ilm->ilm_is_new should already be set if needed.
17138 		 */
17139 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17140 		CLEAR_SLIST(new_ilm->ilm_filter);
17141 		/*
17142 		 * We allocated/got a new ilm, free the old one.
17143 		 */
17144 		if (new_ilm != ilm) {
17145 			if (from_ill->ill_ilm_walker_cnt == 0) {
17146 				*ilmp = ilm->ilm_next;
17147 
17148 				ASSERT(ilm->ilm_ipif == NULL); /* ipv6 */
17149 				DTRACE_PROBE3(ill__decr__cnt, (ill_t *),
17150 				    from_ill, (char *), "ilm", (void *), ilm);
17151 				ASSERT(from_ill->ill_ilm_cnt > 0);
17152 				from_ill->ill_ilm_cnt--;
17153 
17154 				ilm_inactive(ilm); /* frees this ilm */
17155 
17156 			} else {
17157 				ilm->ilm_flags |= ILM_DELETED;
17158 				from_ill->ill_ilm_cleanup_reqd = 1;
17159 				ilmp = &ilm->ilm_next;
17160 			}
17161 		}
17162 	}
17163 }
17164 
17165 /*
17166  * Move all the multicast memberships to to_ill. Called when
17167  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17168  * different from IPv6 counterpart as multicast memberships are associated
17169  * with ills in IPv6. This function is called after every ipif is moved
17170  * unlike IPv6, where it is moved only once.
17171  */
17172 static void
17173 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17174 {
17175 	ilm_t	*ilm;
17176 	ilm_t	*ilm_next;
17177 	ilm_t	*new_ilm;
17178 	ilm_t	**ilmp;
17179 	ip_stack_t	*ipst = from_ill->ill_ipst;
17180 
17181 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17182 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17183 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17184 
17185 	ilmp = &from_ill->ill_ilm;
17186 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17187 		ilm_next = ilm->ilm_next;
17188 
17189 		if (ilm->ilm_flags & ILM_DELETED) {
17190 			ilmp = &ilm->ilm_next;
17191 			continue;
17192 		}
17193 
17194 		ASSERT(ilm->ilm_ipif != NULL);
17195 
17196 		if (ilm->ilm_ipif != ipif) {
17197 			ilmp = &ilm->ilm_next;
17198 			continue;
17199 		}
17200 
17201 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17202 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17203 			new_ilm = ilm_lookup_ipif(ipif,
17204 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17205 			if (new_ilm != NULL) {
17206 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17207 				/*
17208 				 * We still need to deal with the from_ill.
17209 				 */
17210 				new_ilm->ilm_is_new = B_TRUE;
17211 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17212 				CLEAR_SLIST(new_ilm->ilm_filter);
17213 				ASSERT(ilm->ilm_ipif == ipif);
17214 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17215 				if (from_ill->ill_ilm_walker_cnt == 0) {
17216 					DTRACE_PROBE3(ill__decr__cnt,
17217 					    (ill_t *), from_ill,
17218 					    (char *), "ilm", (void *), ilm);
17219 					ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17220 				}
17221 				goto delete_ilm;
17222 			}
17223 			/*
17224 			 * If we could not find one e.g. ipif is
17225 			 * still down on to_ill, we add this ilm
17226 			 * on ill_new to preserve the reference
17227 			 * count.
17228 			 */
17229 		}
17230 		/*
17231 		 * When ipifs move, ilms always move with it
17232 		 * to the NEW ill. Thus we should never be
17233 		 * able to find ilm till we really move it here.
17234 		 */
17235 		ASSERT(ilm_lookup_ipif(ipif,
17236 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17237 
17238 		if (from_ill->ill_ilm_walker_cnt != 0) {
17239 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17240 			if (new_ilm == NULL) {
17241 				char buf[INET6_ADDRSTRLEN];
17242 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17243 				    " multicast address %s : "
17244 				    "from %s to"
17245 				    " %s failed : ENOMEM \n",
17246 				    inet_ntop(AF_INET,
17247 				    &ilm->ilm_v6addr, buf,
17248 				    sizeof (buf)),
17249 				    from_ill->ill_name,
17250 				    to_ill->ill_name));
17251 
17252 				ilmp = &ilm->ilm_next;
17253 				continue;
17254 			}
17255 			*new_ilm = *ilm;
17256 			DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), ipif,
17257 			    (char *), "ilm", (void *), ilm);
17258 			new_ilm->ilm_ipif->ipif_ilm_cnt++;
17259 			/* We don't want new_ilm linked to ilm's filter list */
17260 			new_ilm->ilm_filter = NULL;
17261 		} else {
17262 			/* Remove from the list */
17263 			*ilmp = ilm->ilm_next;
17264 			new_ilm = ilm;
17265 		}
17266 
17267 		/*
17268 		 * If we have never joined this group on the to_ill
17269 		 * make sure we tell the driver.
17270 		 */
17271 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17272 		    ALL_ZONES) == NULL)
17273 			new_ilm->ilm_notify_driver = B_TRUE;
17274 
17275 		/* Add to the to_ill's list */
17276 		new_ilm->ilm_next = to_ill->ill_ilm;
17277 		to_ill->ill_ilm = new_ilm;
17278 		new_ilm->ilm_is_new = B_TRUE;
17279 
17280 		/*
17281 		 * Revert multicast filter state to (EXCLUDE, NULL)
17282 		 */
17283 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17284 		CLEAR_SLIST(new_ilm->ilm_filter);
17285 
17286 		/*
17287 		 * Delete only if we have allocated a new ilm.
17288 		 */
17289 		if (new_ilm != ilm) {
17290 delete_ilm:
17291 			if (from_ill->ill_ilm_walker_cnt == 0) {
17292 				/* Remove from the list */
17293 				*ilmp = ilm->ilm_next;
17294 				ilm->ilm_next = NULL;
17295 				DTRACE_PROBE3(ipif__decr__cnt,
17296 				    (ipif_t *), ilm->ilm_ipif,
17297 				    (char *), "ilm", (void *), ilm);
17298 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17299 				ilm->ilm_ipif->ipif_ilm_cnt--;
17300 				ilm_inactive(ilm);
17301 			} else {
17302 				ilm->ilm_flags |= ILM_DELETED;
17303 				from_ill->ill_ilm_cleanup_reqd = 1;
17304 				ilmp = &ilm->ilm_next;
17305 			}
17306 		}
17307 	}
17308 }
17309 
17310 static uint_t
17311 ipif_get_id(ill_t *ill, uint_t id)
17312 {
17313 	uint_t	unit;
17314 	ipif_t	*tipif;
17315 	boolean_t found = B_FALSE;
17316 	ip_stack_t	*ipst = ill->ill_ipst;
17317 
17318 	/*
17319 	 * During failback, we want to go back to the same id
17320 	 * instead of the smallest id so that the original
17321 	 * configuration is maintained. id is non-zero in that
17322 	 * case.
17323 	 */
17324 	if (id != 0) {
17325 		/*
17326 		 * While failing back, if we still have an ipif with
17327 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17328 		 * as soon as we return from this function. It was
17329 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17330 		 * we can choose the smallest id. Thus we return zero
17331 		 * in that case ignoring the hint.
17332 		 */
17333 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17334 			return (0);
17335 		for (tipif = ill->ill_ipif; tipif != NULL;
17336 		    tipif = tipif->ipif_next) {
17337 			if (tipif->ipif_id == id) {
17338 				found = B_TRUE;
17339 				break;
17340 			}
17341 		}
17342 		/*
17343 		 * If somebody already plumbed another logical
17344 		 * with the same id, we won't be able to find it.
17345 		 */
17346 		if (!found)
17347 			return (id);
17348 	}
17349 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17350 		found = B_FALSE;
17351 		for (tipif = ill->ill_ipif; tipif != NULL;
17352 		    tipif = tipif->ipif_next) {
17353 			if (tipif->ipif_id == unit) {
17354 				found = B_TRUE;
17355 				break;
17356 			}
17357 		}
17358 		if (!found)
17359 			break;
17360 	}
17361 	return (unit);
17362 }
17363 
17364 /* ARGSUSED */
17365 static int
17366 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17367     ipif_t **rep_ipif_ptr)
17368 {
17369 	ill_t	*from_ill;
17370 	ipif_t	*rep_ipif;
17371 	uint_t	unit;
17372 	int err = 0;
17373 	ipif_t	*to_ipif;
17374 	struct iocblk	*iocp;
17375 	boolean_t failback_cmd;
17376 	boolean_t remove_ipif;
17377 	int	rc;
17378 	ip_stack_t	*ipst;
17379 
17380 	ASSERT(IAM_WRITER_ILL(to_ill));
17381 	ASSERT(IAM_WRITER_IPIF(ipif));
17382 
17383 	iocp = (struct iocblk *)mp->b_rptr;
17384 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17385 	remove_ipif = B_FALSE;
17386 
17387 	from_ill = ipif->ipif_ill;
17388 	ipst = from_ill->ill_ipst;
17389 
17390 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17391 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17392 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17393 
17394 	/*
17395 	 * Don't move LINK LOCAL addresses as they are tied to
17396 	 * physical interface.
17397 	 */
17398 	if (from_ill->ill_isv6 &&
17399 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17400 		ipif->ipif_was_up = B_FALSE;
17401 		IPIF_UNMARK_MOVING(ipif);
17402 		return (0);
17403 	}
17404 
17405 	/*
17406 	 * We set the ipif_id to maximum so that the search for
17407 	 * ipif_id will pick the lowest number i.e 0 in the
17408 	 * following 2 cases :
17409 	 *
17410 	 * 1) We have a replacement ipif at the head of to_ill.
17411 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17412 	 *    on to_ill and hence the MOVE might fail. We want to
17413 	 *    remove it only if we could move the ipif. Thus, by
17414 	 *    setting it to the MAX value, we make the search in
17415 	 *    ipif_get_id return the zeroth id.
17416 	 *
17417 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17418 	 *    we might just have a zero address plumbed on the ipif
17419 	 *    with zero id in the case of IPv4. We remove that while
17420 	 *    doing the failback. We want to remove it only if we
17421 	 *    could move the ipif. Thus, by setting it to the MAX
17422 	 *    value, we make the search in ipif_get_id return the
17423 	 *    zeroth id.
17424 	 *
17425 	 * Both (1) and (2) are done only when when we are moving
17426 	 * an ipif (either due to failover/failback) which originally
17427 	 * belonged to this interface i.e the ipif_orig_ifindex is
17428 	 * the same as to_ill's ifindex. This is needed so that
17429 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17430 	 * from B -> A (B is being removed from the group) and
17431 	 * FAILBACK from A -> B restores the original configuration.
17432 	 * Without the check for orig_ifindex, the second FAILOVER
17433 	 * could make the ipif belonging to B replace the A's zeroth
17434 	 * ipif and the subsequent failback re-creating the replacement
17435 	 * ipif again.
17436 	 *
17437 	 * NOTE : We created the replacement ipif when we did a
17438 	 * FAILOVER (See below). We could check for FAILBACK and
17439 	 * then look for replacement ipif to be removed. But we don't
17440 	 * want to do that because we wan't to allow the possibility
17441 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17442 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17443 	 * from B -> A.
17444 	 */
17445 	to_ipif = to_ill->ill_ipif;
17446 	if ((to_ill->ill_phyint->phyint_ifindex ==
17447 	    ipif->ipif_orig_ifindex) &&
17448 	    to_ipif->ipif_replace_zero) {
17449 		ASSERT(to_ipif->ipif_id == 0);
17450 		remove_ipif = B_TRUE;
17451 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17452 	}
17453 	/*
17454 	 * Find the lowest logical unit number on the to_ill.
17455 	 * If we are failing back, try to get the original id
17456 	 * rather than the lowest one so that the original
17457 	 * configuration is maintained.
17458 	 *
17459 	 * XXX need a better scheme for this.
17460 	 */
17461 	if (failback_cmd) {
17462 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17463 	} else {
17464 		unit = ipif_get_id(to_ill, 0);
17465 	}
17466 
17467 	/* Reset back to zero in case we fail below */
17468 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17469 		to_ipif->ipif_id = 0;
17470 
17471 	if (unit == ipst->ips_ip_addrs_per_if) {
17472 		ipif->ipif_was_up = B_FALSE;
17473 		IPIF_UNMARK_MOVING(ipif);
17474 		return (EINVAL);
17475 	}
17476 
17477 	/*
17478 	 * ipif is ready to move from "from_ill" to "to_ill".
17479 	 *
17480 	 * 1) If we are moving ipif with id zero, create a
17481 	 *    replacement ipif for this ipif on from_ill. If this fails
17482 	 *    fail the MOVE operation.
17483 	 *
17484 	 * 2) Remove the replacement ipif on to_ill if any.
17485 	 *    We could remove the replacement ipif when we are moving
17486 	 *    the ipif with id zero. But what if somebody already
17487 	 *    unplumbed it ? Thus we always remove it if it is present.
17488 	 *    We want to do it only if we are sure we are going to
17489 	 *    move the ipif to to_ill which is why there are no
17490 	 *    returns due to error till ipif is linked to to_ill.
17491 	 *    Note that the first ipif that we failback will always
17492 	 *    be zero if it is present.
17493 	 */
17494 	if (ipif->ipif_id == 0) {
17495 		ipaddr_t inaddr_any = INADDR_ANY;
17496 
17497 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17498 		if (rep_ipif == NULL) {
17499 			ipif->ipif_was_up = B_FALSE;
17500 			IPIF_UNMARK_MOVING(ipif);
17501 			return (ENOMEM);
17502 		}
17503 		*rep_ipif = ipif_zero;
17504 		/*
17505 		 * Before we put the ipif on the list, store the addresses
17506 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17507 		 * assumes so. This logic is not any different from what
17508 		 * ipif_allocate does.
17509 		 */
17510 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17511 		    &rep_ipif->ipif_v6lcl_addr);
17512 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17513 		    &rep_ipif->ipif_v6src_addr);
17514 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17515 		    &rep_ipif->ipif_v6subnet);
17516 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17517 		    &rep_ipif->ipif_v6net_mask);
17518 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17519 		    &rep_ipif->ipif_v6brd_addr);
17520 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17521 		    &rep_ipif->ipif_v6pp_dst_addr);
17522 		/*
17523 		 * We mark IPIF_NOFAILOVER so that this can never
17524 		 * move.
17525 		 */
17526 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17527 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17528 		rep_ipif->ipif_replace_zero = B_TRUE;
17529 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17530 		    MUTEX_DEFAULT, NULL);
17531 		rep_ipif->ipif_id = 0;
17532 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17533 		rep_ipif->ipif_ill = from_ill;
17534 		rep_ipif->ipif_orig_ifindex =
17535 		    from_ill->ill_phyint->phyint_ifindex;
17536 		/* Insert at head */
17537 		rep_ipif->ipif_next = from_ill->ill_ipif;
17538 		from_ill->ill_ipif = rep_ipif;
17539 		/*
17540 		 * We don't really care to let apps know about
17541 		 * this interface.
17542 		 */
17543 	}
17544 
17545 	if (remove_ipif) {
17546 		/*
17547 		 * We set to a max value above for this case to get
17548 		 * id zero. ASSERT that we did get one.
17549 		 */
17550 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17551 		rep_ipif = to_ipif;
17552 		to_ill->ill_ipif = rep_ipif->ipif_next;
17553 		rep_ipif->ipif_next = NULL;
17554 		/*
17555 		 * If some apps scanned and find this interface,
17556 		 * it is time to let them know, so that they can
17557 		 * delete it.
17558 		 */
17559 
17560 		*rep_ipif_ptr = rep_ipif;
17561 	}
17562 
17563 	/* Get it out of the ILL interface list. */
17564 	ipif_remove(ipif, B_FALSE);
17565 
17566 	/* Assign the new ill */
17567 	ipif->ipif_ill = to_ill;
17568 	ipif->ipif_id = unit;
17569 	/* id has already been checked */
17570 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17571 	ASSERT(rc == 0);
17572 	/* Let SCTP update its list */
17573 	sctp_move_ipif(ipif, from_ill, to_ill);
17574 	/*
17575 	 * Handle the failover and failback of ipif_t between
17576 	 * ill_t that have differing maximum mtu values.
17577 	 */
17578 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17579 		if (ipif->ipif_saved_mtu == 0) {
17580 			/*
17581 			 * As this ipif_t is moving to an ill_t
17582 			 * that has a lower ill_max_mtu, its
17583 			 * ipif_mtu needs to be saved so it can
17584 			 * be restored during failback or during
17585 			 * failover to an ill_t which has a
17586 			 * higher ill_max_mtu.
17587 			 */
17588 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17589 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17590 		} else {
17591 			/*
17592 			 * The ipif_t is, once again, moving to
17593 			 * an ill_t that has a lower maximum mtu
17594 			 * value.
17595 			 */
17596 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17597 		}
17598 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17599 	    ipif->ipif_saved_mtu != 0) {
17600 		/*
17601 		 * The mtu of this ipif_t had to be reduced
17602 		 * during an earlier failover; this is an
17603 		 * opportunity for it to be increased (either as
17604 		 * part of another failover or a failback).
17605 		 */
17606 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17607 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17608 			ipif->ipif_saved_mtu = 0;
17609 		} else {
17610 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17611 		}
17612 	}
17613 
17614 	/*
17615 	 * We preserve all the other fields of the ipif including
17616 	 * ipif_saved_ire_mp. The routes that are saved here will
17617 	 * be recreated on the new interface and back on the old
17618 	 * interface when we move back.
17619 	 */
17620 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17621 
17622 	return (err);
17623 }
17624 
17625 static int
17626 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17627     int ifindex, ipif_t **rep_ipif_ptr)
17628 {
17629 	ipif_t *mipif;
17630 	ipif_t *ipif_next;
17631 	int err;
17632 
17633 	/*
17634 	 * We don't really try to MOVE back things if some of the
17635 	 * operations fail. The daemon will take care of moving again
17636 	 * later on.
17637 	 */
17638 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17639 		ipif_next = mipif->ipif_next;
17640 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17641 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17642 
17643 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17644 
17645 			/*
17646 			 * When the MOVE fails, it is the job of the
17647 			 * application to take care of this properly
17648 			 * i.e try again if it is ENOMEM.
17649 			 */
17650 			if (mipif->ipif_ill != from_ill) {
17651 				/*
17652 				 * ipif has moved.
17653 				 *
17654 				 * Move the multicast memberships associated
17655 				 * with this ipif to the new ill. For IPv6, we
17656 				 * do it once after all the ipifs are moved
17657 				 * (in ill_move) as they are not associated
17658 				 * with ipifs.
17659 				 *
17660 				 * We need to move the ilms as the ipif has
17661 				 * already been moved to a new ill even
17662 				 * in the case of errors. Neither
17663 				 * ilm_free(ipif) will find the ilm
17664 				 * when somebody unplumbs this ipif nor
17665 				 * ilm_delete(ilm) will be able to find the
17666 				 * ilm, if we don't move now.
17667 				 */
17668 				if (!from_ill->ill_isv6)
17669 					ilm_move_v4(from_ill, to_ill, mipif);
17670 			}
17671 
17672 			if (err != 0)
17673 				return (err);
17674 		}
17675 	}
17676 	return (0);
17677 }
17678 
17679 static int
17680 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17681 {
17682 	int ifindex;
17683 	int err;
17684 	struct iocblk	*iocp;
17685 	ipif_t	*ipif;
17686 	ipif_t *rep_ipif_ptr = NULL;
17687 	ipif_t	*from_ipif = NULL;
17688 	boolean_t check_rep_if = B_FALSE;
17689 	ip_stack_t	*ipst = from_ill->ill_ipst;
17690 
17691 	iocp = (struct iocblk *)mp->b_rptr;
17692 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17693 		/*
17694 		 * Move everything pointing at from_ill to to_ill.
17695 		 * We acheive this by passing in 0 as ifindex.
17696 		 */
17697 		ifindex = 0;
17698 	} else {
17699 		/*
17700 		 * Move everything pointing at from_ill whose original
17701 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17702 		 * We acheive this by passing in ifindex rather than 0.
17703 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17704 		 */
17705 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17706 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17707 	}
17708 
17709 	/*
17710 	 * Determine if there is at least one ipif that would move from
17711 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17712 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17713 	 * the move, in which case we need to quiesce the replacement ipif also.
17714 	 */
17715 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17716 	    from_ipif = from_ipif->ipif_next) {
17717 		if (((ifindex == 0) ||
17718 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17719 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17720 			check_rep_if = B_TRUE;
17721 			break;
17722 		}
17723 	}
17724 
17725 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17726 
17727 	GRAB_ILL_LOCKS(from_ill, to_ill);
17728 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17729 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17730 		    mp, ILL_MOVE_OK);
17731 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17732 		return (EINPROGRESS);
17733 	}
17734 
17735 	/* Check if the replacement ipif is quiescent to delete */
17736 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17737 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17738 		to_ill->ill_ipif->ipif_state_flags |=
17739 		    IPIF_MOVING | IPIF_CHANGING;
17740 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17741 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17742 			    mp, ILL_MOVE_OK);
17743 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17744 			return (EINPROGRESS);
17745 		}
17746 	}
17747 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17748 
17749 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17750 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17751 	GRAB_ILL_LOCKS(from_ill, to_ill);
17752 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17753 
17754 	/* ilm_move is done inside ipif_move for IPv4 */
17755 	if (err == 0 && from_ill->ill_isv6)
17756 		ilm_move_v6(from_ill, to_ill, ifindex);
17757 
17758 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17759 	rw_exit(&ipst->ips_ill_g_lock);
17760 
17761 	/*
17762 	 * send rts messages and multicast messages.
17763 	 */
17764 	if (rep_ipif_ptr != NULL) {
17765 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17766 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17767 			rep_ipif_ptr->ipif_recovery_id = 0;
17768 		}
17769 		ip_rts_ifmsg(rep_ipif_ptr);
17770 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17771 #ifdef DEBUG
17772 		ipif_trace_cleanup(rep_ipif_ptr);
17773 #endif
17774 		mi_free(rep_ipif_ptr);
17775 	}
17776 
17777 	conn_move_ill(from_ill, to_ill, ifindex);
17778 
17779 	return (err);
17780 }
17781 
17782 /*
17783  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17784  * Also checks for the validity of the arguments.
17785  * Note: We are already exclusive inside the from group.
17786  * It is upto the caller to release refcnt on the to_ill's.
17787  */
17788 static int
17789 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17790     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17791 {
17792 	int dst_index;
17793 	ipif_t *ipif_v4, *ipif_v6;
17794 	struct lifreq *lifr;
17795 	mblk_t *mp1;
17796 	boolean_t exists;
17797 	sin_t	*sin;
17798 	int	err = 0;
17799 	ip_stack_t	*ipst;
17800 
17801 	if (CONN_Q(q))
17802 		ipst = CONNQ_TO_IPST(q);
17803 	else
17804 		ipst = ILLQ_TO_IPST(q);
17805 
17806 	if ((mp1 = mp->b_cont) == NULL)
17807 		return (EPROTO);
17808 
17809 	if ((mp1 = mp1->b_cont) == NULL)
17810 		return (EPROTO);
17811 
17812 	lifr = (struct lifreq *)mp1->b_rptr;
17813 	sin = (sin_t *)&lifr->lifr_addr;
17814 
17815 	/*
17816 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17817 	 * specific operations.
17818 	 */
17819 	if (sin->sin_family != AF_UNSPEC)
17820 		return (EINVAL);
17821 
17822 	/*
17823 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17824 	 * NULLs for the last 4 args and we know the lookup won't fail
17825 	 * with EINPROGRESS.
17826 	 */
17827 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17828 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17829 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17830 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17831 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17832 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17833 
17834 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17835 		return (ENXIO);
17836 
17837 	if (ipif_v4 != NULL) {
17838 		ASSERT(ipif_v4->ipif_refcnt != 0);
17839 		if (ipif_v4->ipif_id != 0) {
17840 			err = EINVAL;
17841 			goto done;
17842 		}
17843 
17844 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17845 		*ill_from_v4 = ipif_v4->ipif_ill;
17846 	}
17847 
17848 	if (ipif_v6 != NULL) {
17849 		ASSERT(ipif_v6->ipif_refcnt != 0);
17850 		if (ipif_v6->ipif_id != 0) {
17851 			err = EINVAL;
17852 			goto done;
17853 		}
17854 
17855 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17856 		*ill_from_v6 = ipif_v6->ipif_ill;
17857 	}
17858 
17859 	err = 0;
17860 	dst_index = lifr->lifr_movetoindex;
17861 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17862 	    q, mp, ip_process_ioctl, &err, ipst);
17863 	if (err != 0) {
17864 		/*
17865 		 * There could be only v6.
17866 		 */
17867 		if (err != ENXIO)
17868 			goto done;
17869 		err = 0;
17870 	}
17871 
17872 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17873 	    q, mp, ip_process_ioctl, &err, ipst);
17874 	if (err != 0) {
17875 		if (err != ENXIO)
17876 			goto done;
17877 		if (*ill_to_v4 == NULL) {
17878 			err = ENXIO;
17879 			goto done;
17880 		}
17881 		err = 0;
17882 	}
17883 
17884 	/*
17885 	 * If we have something to MOVE i.e "from" not NULL,
17886 	 * "to" should be non-NULL.
17887 	 */
17888 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17889 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17890 		err = EINVAL;
17891 	}
17892 
17893 done:
17894 	if (ipif_v4 != NULL)
17895 		ipif_refrele(ipif_v4);
17896 	if (ipif_v6 != NULL)
17897 		ipif_refrele(ipif_v6);
17898 	return (err);
17899 }
17900 
17901 /*
17902  * FAILOVER and FAILBACK are modelled as MOVE operations.
17903  *
17904  * We don't check whether the MOVE is within the same group or
17905  * not, because this ioctl can be used as a generic mechanism
17906  * to failover from interface A to B, though things will function
17907  * only if they are really part of the same group. Moreover,
17908  * all ipifs may be down and hence temporarily out of the group.
17909  *
17910  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17911  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17912  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17913  * have been deleted and there are no active references. Once quiescent the
17914  * ipif's are moved and brought up on the new ill.
17915  *
17916  * Normally the source ill and destination ill belong to the same IPMP group
17917  * and hence the same ipsq_t. In the event they don't belong to the same
17918  * same group the two ipsq's are first merged into one ipsq - that of the
17919  * to_ill. The multicast memberships on the source and destination ill cannot
17920  * change during the move operation since multicast joins/leaves also have to
17921  * execute on the same ipsq and are hence serialized.
17922  */
17923 /* ARGSUSED */
17924 int
17925 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17926     ip_ioctl_cmd_t *ipip, void *ifreq)
17927 {
17928 	ill_t *ill_to_v4 = NULL;
17929 	ill_t *ill_to_v6 = NULL;
17930 	ill_t *ill_from_v4 = NULL;
17931 	ill_t *ill_from_v6 = NULL;
17932 	int err = 0;
17933 
17934 	/*
17935 	 * setup from and to ill's, we can get EINPROGRESS only for
17936 	 * to_ill's.
17937 	 */
17938 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17939 	    &ill_to_v4, &ill_to_v6);
17940 
17941 	if (err != 0) {
17942 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17943 		goto done;
17944 	}
17945 
17946 	/*
17947 	 * nothing to do.
17948 	 */
17949 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17950 		goto done;
17951 	}
17952 
17953 	/*
17954 	 * nothing to do.
17955 	 */
17956 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17957 		goto done;
17958 	}
17959 
17960 	/*
17961 	 * Mark the ill as changing.
17962 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17963 	 * in ill_up_ipifs in case of error they are cleared below.
17964 	 */
17965 
17966 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17967 	if (ill_from_v4 != NULL)
17968 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17969 	if (ill_from_v6 != NULL)
17970 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17971 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17972 
17973 	/*
17974 	 * Make sure that both src and dst are
17975 	 * in the same syncq group. If not make it happen.
17976 	 * We are not holding any locks because we are the writer
17977 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17978 	 * to protect to_ipsq against changing.
17979 	 */
17980 	if (ill_from_v4 != NULL) {
17981 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17982 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17983 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17984 			    NULL, mp, q);
17985 			goto err_ret;
17986 
17987 		}
17988 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
17989 	} else {
17990 
17991 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
17992 		    ill_to_v6->ill_phyint->phyint_ipsq) {
17993 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
17994 			    NULL, mp, q);
17995 			goto err_ret;
17996 
17997 		}
17998 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
17999 	}
18000 
18001 	/*
18002 	 * Now that the ipsq's have been merged and we are the writer
18003 	 * lets mark to_ill as changing as well.
18004 	 */
18005 
18006 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18007 	if (ill_to_v4 != NULL)
18008 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18009 	if (ill_to_v6 != NULL)
18010 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18011 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18012 
18013 	/*
18014 	 * Its ok for us to proceed with the move even if
18015 	 * ill_pending_mp is non null on one of the from ill's as the reply
18016 	 * should not be looking at the ipif, it should only care about the
18017 	 * ill itself.
18018 	 */
18019 
18020 	/*
18021 	 * lets move ipv4 first.
18022 	 */
18023 	if (ill_from_v4 != NULL) {
18024 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18025 		ill_from_v4->ill_move_in_progress = B_TRUE;
18026 		ill_to_v4->ill_move_in_progress = B_TRUE;
18027 		ill_to_v4->ill_move_peer = ill_from_v4;
18028 		ill_from_v4->ill_move_peer = ill_to_v4;
18029 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18030 	}
18031 
18032 	/*
18033 	 * Now lets move ipv6.
18034 	 */
18035 	if (err == 0 && ill_from_v6 != NULL) {
18036 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18037 		ill_from_v6->ill_move_in_progress = B_TRUE;
18038 		ill_to_v6->ill_move_in_progress = B_TRUE;
18039 		ill_to_v6->ill_move_peer = ill_from_v6;
18040 		ill_from_v6->ill_move_peer = ill_to_v6;
18041 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18042 	}
18043 
18044 err_ret:
18045 	/*
18046 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18047 	 * moved to become quiescent.
18048 	 */
18049 	if (err == EINPROGRESS) {
18050 		goto done;
18051 	}
18052 
18053 	/*
18054 	 * if err is set ill_up_ipifs will not be called
18055 	 * lets clear the flags.
18056 	 */
18057 
18058 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18059 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18060 	/*
18061 	 * Some of the clearing may be redundant. But it is simple
18062 	 * not making any extra checks.
18063 	 */
18064 	if (ill_from_v6 != NULL) {
18065 		ill_from_v6->ill_move_in_progress = B_FALSE;
18066 		ill_from_v6->ill_move_peer = NULL;
18067 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18068 	}
18069 	if (ill_from_v4 != NULL) {
18070 		ill_from_v4->ill_move_in_progress = B_FALSE;
18071 		ill_from_v4->ill_move_peer = NULL;
18072 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18073 	}
18074 	if (ill_to_v6 != NULL) {
18075 		ill_to_v6->ill_move_in_progress = B_FALSE;
18076 		ill_to_v6->ill_move_peer = NULL;
18077 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18078 	}
18079 	if (ill_to_v4 != NULL) {
18080 		ill_to_v4->ill_move_in_progress = B_FALSE;
18081 		ill_to_v4->ill_move_peer = NULL;
18082 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18083 	}
18084 
18085 	/*
18086 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18087 	 * Do this always to maintain proper state i.e even in case of errors.
18088 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18089 	 * we need not call on both v4 and v6 interfaces.
18090 	 */
18091 	if (ill_from_v4 != NULL) {
18092 		if ((ill_from_v4->ill_phyint->phyint_flags &
18093 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18094 			phyint_inactive(ill_from_v4->ill_phyint);
18095 		}
18096 	} else if (ill_from_v6 != NULL) {
18097 		if ((ill_from_v6->ill_phyint->phyint_flags &
18098 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18099 			phyint_inactive(ill_from_v6->ill_phyint);
18100 		}
18101 	}
18102 
18103 	if (ill_to_v4 != NULL) {
18104 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18105 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18106 		}
18107 	} else if (ill_to_v6 != NULL) {
18108 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18109 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18110 		}
18111 	}
18112 
18113 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18114 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18115 
18116 no_err:
18117 	/*
18118 	 * lets bring the interfaces up on the to_ill.
18119 	 */
18120 	if (err == 0) {
18121 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18122 		    q, mp);
18123 	}
18124 
18125 	if (err == 0) {
18126 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18127 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18128 
18129 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18130 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18131 	}
18132 done:
18133 
18134 	if (ill_to_v4 != NULL) {
18135 		ill_refrele(ill_to_v4);
18136 	}
18137 	if (ill_to_v6 != NULL) {
18138 		ill_refrele(ill_to_v6);
18139 	}
18140 
18141 	return (err);
18142 }
18143 
18144 static void
18145 ill_dl_down(ill_t *ill)
18146 {
18147 	/*
18148 	 * The ill is down; unbind but stay attached since we're still
18149 	 * associated with a PPA. If we have negotiated DLPI capabilites
18150 	 * with the data link service provider (IDS_OK) then reset them.
18151 	 * The interval between unbinding and rebinding is potentially
18152 	 * unbounded hence we cannot assume things will be the same.
18153 	 * The DLPI capabilities will be probed again when the data link
18154 	 * is brought up.
18155 	 */
18156 	mblk_t	*mp = ill->ill_unbind_mp;
18157 
18158 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18159 
18160 	ill->ill_unbind_mp = NULL;
18161 	if (mp != NULL) {
18162 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18163 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18164 		    ill->ill_name));
18165 		mutex_enter(&ill->ill_lock);
18166 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18167 		mutex_exit(&ill->ill_lock);
18168 		/*
18169 		 * Reset the capabilities if the negotiation is done or is
18170 		 * still in progress. Note that ill_capability_reset() will
18171 		 * set ill_dlpi_capab_state to IDS_UNKNOWN, so the subsequent
18172 		 * DL_CAPABILITY_ACK and DL_NOTE_CAPAB_RENEG will be ignored.
18173 		 *
18174 		 * Further, reset ill_capab_reneg to be B_FALSE so that the
18175 		 * subsequent DL_CAPABILITY_ACK can be ignored, to prevent
18176 		 * the capabilities renegotiation from happening.
18177 		 */
18178 		if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
18179 			ill_capability_reset(ill);
18180 		ill->ill_capab_reneg = B_FALSE;
18181 
18182 		ill_dlpi_send(ill, mp);
18183 	}
18184 
18185 	/*
18186 	 * Toss all of our multicast memberships.  We could keep them, but
18187 	 * then we'd have to do bookkeeping of any joins and leaves performed
18188 	 * by the application while the the interface is down (we can't just
18189 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18190 	 * on a downed interface).
18191 	 */
18192 	ill_leave_multicast(ill);
18193 
18194 	mutex_enter(&ill->ill_lock);
18195 	ill->ill_dl_up = 0;
18196 	(void) ill_hook_event_create(ill, 0, NE_DOWN, NULL, 0);
18197 	mutex_exit(&ill->ill_lock);
18198 }
18199 
18200 static void
18201 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18202 {
18203 	union DL_primitives *dlp;
18204 	t_uscalar_t prim;
18205 
18206 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18207 
18208 	dlp = (union DL_primitives *)mp->b_rptr;
18209 	prim = dlp->dl_primitive;
18210 
18211 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18212 	    dl_primstr(prim), prim, ill->ill_name));
18213 
18214 	switch (prim) {
18215 	case DL_PHYS_ADDR_REQ:
18216 	{
18217 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18218 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18219 		break;
18220 	}
18221 	case DL_BIND_REQ:
18222 		mutex_enter(&ill->ill_lock);
18223 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18224 		mutex_exit(&ill->ill_lock);
18225 		break;
18226 	}
18227 
18228 	/*
18229 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18230 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18231 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18232 	 */
18233 	mutex_enter(&ill->ill_lock);
18234 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18235 	    (prim == DL_UNBIND_REQ)) {
18236 		ill->ill_dlpi_pending = prim;
18237 	}
18238 	mutex_exit(&ill->ill_lock);
18239 
18240 	putnext(ill->ill_wq, mp);
18241 }
18242 
18243 /*
18244  * Helper function for ill_dlpi_send().
18245  */
18246 /* ARGSUSED */
18247 static void
18248 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18249 {
18250 	ill_dlpi_send(q->q_ptr, mp);
18251 }
18252 
18253 /*
18254  * Send a DLPI control message to the driver but make sure there
18255  * is only one outstanding message. Uses ill_dlpi_pending to tell
18256  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18257  * when an ACK or a NAK is received to process the next queued message.
18258  */
18259 void
18260 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18261 {
18262 	mblk_t **mpp;
18263 
18264 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18265 
18266 	/*
18267 	 * To ensure that any DLPI requests for current exclusive operation
18268 	 * are always completely sent before any DLPI messages for other
18269 	 * operations, require writer access before enqueuing.
18270 	 */
18271 	if (!IAM_WRITER_ILL(ill)) {
18272 		ill_refhold(ill);
18273 		/* qwriter_ip() does the ill_refrele() */
18274 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18275 		    NEW_OP, B_TRUE);
18276 		return;
18277 	}
18278 
18279 	mutex_enter(&ill->ill_lock);
18280 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18281 		/* Must queue message. Tail insertion */
18282 		mpp = &ill->ill_dlpi_deferred;
18283 		while (*mpp != NULL)
18284 			mpp = &((*mpp)->b_next);
18285 
18286 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18287 		    ill->ill_name));
18288 
18289 		*mpp = mp;
18290 		mutex_exit(&ill->ill_lock);
18291 		return;
18292 	}
18293 	mutex_exit(&ill->ill_lock);
18294 	ill_dlpi_dispatch(ill, mp);
18295 }
18296 
18297 /*
18298  * Send all deferred DLPI messages without waiting for their ACKs.
18299  */
18300 void
18301 ill_dlpi_send_deferred(ill_t *ill)
18302 {
18303 	mblk_t *mp, *nextmp;
18304 
18305 	/*
18306 	 * Clear ill_dlpi_pending so that the message is not queued in
18307 	 * ill_dlpi_send().
18308 	 */
18309 	mutex_enter(&ill->ill_lock);
18310 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18311 	mp = ill->ill_dlpi_deferred;
18312 	ill->ill_dlpi_deferred = NULL;
18313 	mutex_exit(&ill->ill_lock);
18314 
18315 	for (; mp != NULL; mp = nextmp) {
18316 		nextmp = mp->b_next;
18317 		mp->b_next = NULL;
18318 		ill_dlpi_send(ill, mp);
18319 	}
18320 }
18321 
18322 /*
18323  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18324  */
18325 boolean_t
18326 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18327 {
18328 	t_uscalar_t pending;
18329 
18330 	mutex_enter(&ill->ill_lock);
18331 	if (ill->ill_dlpi_pending == prim) {
18332 		mutex_exit(&ill->ill_lock);
18333 		return (B_TRUE);
18334 	}
18335 
18336 	/*
18337 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
18338 	 * without waiting, so don't print any warnings in that case.
18339 	 */
18340 	if (ill->ill_state_flags & ILL_CONDEMNED) {
18341 		mutex_exit(&ill->ill_lock);
18342 		return (B_FALSE);
18343 	}
18344 	pending = ill->ill_dlpi_pending;
18345 	mutex_exit(&ill->ill_lock);
18346 
18347 	if (pending == DL_PRIM_INVAL) {
18348 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18349 		    "received unsolicited ack for %s on %s\n",
18350 		    dl_primstr(prim), ill->ill_name);
18351 	} else {
18352 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18353 		    "received unexpected ack for %s on %s (expecting %s)\n",
18354 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
18355 	}
18356 	return (B_FALSE);
18357 }
18358 
18359 /*
18360  * Complete the current DLPI operation associated with `prim' on `ill' and
18361  * start the next queued DLPI operation (if any).  If there are no queued DLPI
18362  * operations and the ill's current exclusive IPSQ operation has finished
18363  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
18364  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
18365  * the comments above ipsq_current_finish() for details.
18366  */
18367 void
18368 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18369 {
18370 	mblk_t *mp;
18371 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
18372 
18373 	ASSERT(IAM_WRITER_IPSQ(ipsq));
18374 	mutex_enter(&ill->ill_lock);
18375 
18376 	ASSERT(prim != DL_PRIM_INVAL);
18377 	ASSERT(ill->ill_dlpi_pending == prim);
18378 
18379 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18380 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18381 
18382 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18383 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18384 
18385 		mutex_enter(&ipsq->ipsq_lock);
18386 		if (ipsq->ipsq_current_done)
18387 			ipsq->ipsq_current_ipif = NULL;
18388 		mutex_exit(&ipsq->ipsq_lock);
18389 
18390 		cv_signal(&ill->ill_cv);
18391 		mutex_exit(&ill->ill_lock);
18392 		return;
18393 	}
18394 
18395 	ill->ill_dlpi_deferred = mp->b_next;
18396 	mp->b_next = NULL;
18397 	mutex_exit(&ill->ill_lock);
18398 
18399 	ill_dlpi_dispatch(ill, mp);
18400 }
18401 
18402 void
18403 conn_delete_ire(conn_t *connp, caddr_t arg)
18404 {
18405 	ipif_t	*ipif = (ipif_t *)arg;
18406 	ire_t	*ire;
18407 
18408 	/*
18409 	 * Look at the cached ires on conns which has pointers to ipifs.
18410 	 * We just call ire_refrele which clears up the reference
18411 	 * to ire. Called when a conn closes. Also called from ipif_free
18412 	 * to cleanup indirect references to the stale ipif via the cached ire.
18413 	 */
18414 	mutex_enter(&connp->conn_lock);
18415 	ire = connp->conn_ire_cache;
18416 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18417 		connp->conn_ire_cache = NULL;
18418 		mutex_exit(&connp->conn_lock);
18419 		IRE_REFRELE_NOTR(ire);
18420 		return;
18421 	}
18422 	mutex_exit(&connp->conn_lock);
18423 
18424 }
18425 
18426 /*
18427  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18428  * of IREs. Those IREs may have been previously cached in the conn structure.
18429  * This ipcl_walk() walker function releases all references to such IREs based
18430  * on the condemned flag.
18431  */
18432 /* ARGSUSED */
18433 void
18434 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18435 {
18436 	ire_t	*ire;
18437 
18438 	mutex_enter(&connp->conn_lock);
18439 	ire = connp->conn_ire_cache;
18440 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18441 		connp->conn_ire_cache = NULL;
18442 		mutex_exit(&connp->conn_lock);
18443 		IRE_REFRELE_NOTR(ire);
18444 		return;
18445 	}
18446 	mutex_exit(&connp->conn_lock);
18447 }
18448 
18449 /*
18450  * Take down a specific interface, but don't lose any information about it.
18451  * Also delete interface from its interface group (ifgrp).
18452  * (Always called as writer.)
18453  * This function goes through the down sequence even if the interface is
18454  * already down. There are 2 reasons.
18455  * a. Currently we permit interface routes that depend on down interfaces
18456  *    to be added. This behaviour itself is questionable. However it appears
18457  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18458  *    time. We go thru the cleanup in order to remove these routes.
18459  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18460  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18461  *    down, but we need to cleanup i.e. do ill_dl_down and
18462  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18463  *
18464  * IP-MT notes:
18465  *
18466  * Model of reference to interfaces.
18467  *
18468  * The following members in ipif_t track references to the ipif.
18469  *	int     ipif_refcnt;    Active reference count
18470  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18471  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
18472  *
18473  * The following members in ill_t track references to the ill.
18474  *	int             ill_refcnt;     active refcnt
18475  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18476  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18477  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
18478  *
18479  * Reference to an ipif or ill can be obtained in any of the following ways.
18480  *
18481  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18482  * Pointers to ipif / ill from other data structures viz ire and conn.
18483  * Implicit reference to the ipif / ill by holding a reference to the ire.
18484  *
18485  * The ipif/ill lookup functions return a reference held ipif / ill.
18486  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18487  * This is a purely dynamic reference count associated with threads holding
18488  * references to the ipif / ill. Pointers from other structures do not
18489  * count towards this reference count.
18490  *
18491  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
18492  * associated with the ipif/ill. This is incremented whenever a new
18493  * ire is created referencing the ipif/ill. This is done atomically inside
18494  * ire_add_v[46] where the ire is actually added to the ire hash table.
18495  * The count is decremented in ire_inactive where the ire is destroyed.
18496  *
18497  * nce's reference ill's thru nce_ill and the count of nce's associated with
18498  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18499  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18500  * table. Similarly it is decremented in ndp_inactive() where the nce
18501  * is destroyed.
18502  *
18503  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
18504  * is incremented in ilm_add_v6() and decremented before the ilm is freed
18505  * in ilm_walker_cleanup() or ilm_delete().
18506  *
18507  * Flow of ioctls involving interface down/up
18508  *
18509  * The following is the sequence of an attempt to set some critical flags on an
18510  * up interface.
18511  * ip_sioctl_flags
18512  * ipif_down
18513  * wait for ipif to be quiescent
18514  * ipif_down_tail
18515  * ip_sioctl_flags_tail
18516  *
18517  * All set ioctls that involve down/up sequence would have a skeleton similar
18518  * to the above. All the *tail functions are called after the refcounts have
18519  * dropped to the appropriate values.
18520  *
18521  * The mechanism to quiesce an ipif is as follows.
18522  *
18523  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18524  * on the ipif. Callers either pass a flag requesting wait or the lookup
18525  *  functions will return NULL.
18526  *
18527  * Delete all ires referencing this ipif
18528  *
18529  * Any thread attempting to do an ipif_refhold on an ipif that has been
18530  * obtained thru a cached pointer will first make sure that
18531  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18532  * increment the refcount.
18533  *
18534  * The above guarantees that the ipif refcount will eventually come down to
18535  * zero and the ipif will quiesce, once all threads that currently hold a
18536  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18537  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18538  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
18539  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
18540  * in ip.h
18541  *
18542  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18543  *
18544  * Threads trying to lookup an ipif or ill can pass a flag requesting
18545  * wait and restart if the ipif / ill cannot be looked up currently.
18546  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18547  * failure if the ipif is currently undergoing an exclusive operation, and
18548  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18549  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18550  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18551  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18552  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18553  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18554  * until we release the ipsq_lock, even though the the ill/ipif state flags
18555  * can change after we drop the ill_lock.
18556  *
18557  * An attempt to send out a packet using an ipif that is currently
18558  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18559  * operation and restart it later when the exclusive condition on the ipif ends.
18560  * This is an example of not passing the wait flag to the lookup functions. For
18561  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18562  * out a multicast packet on that ipif will fail while the ipif is
18563  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18564  * currently IPIF_CHANGING will also fail.
18565  */
18566 int
18567 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18568 {
18569 	ill_t		*ill = ipif->ipif_ill;
18570 	phyint_t	*phyi;
18571 	conn_t		*connp;
18572 	boolean_t	success;
18573 	boolean_t	ipif_was_up = B_FALSE;
18574 	ip_stack_t	*ipst = ill->ill_ipst;
18575 
18576 	ASSERT(IAM_WRITER_IPIF(ipif));
18577 
18578 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18579 
18580 	if (ipif->ipif_flags & IPIF_UP) {
18581 		mutex_enter(&ill->ill_lock);
18582 		ipif->ipif_flags &= ~IPIF_UP;
18583 		ASSERT(ill->ill_ipif_up_count > 0);
18584 		--ill->ill_ipif_up_count;
18585 		mutex_exit(&ill->ill_lock);
18586 		ipif_was_up = B_TRUE;
18587 		/* Update status in SCTP's list */
18588 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18589 	}
18590 
18591 	/*
18592 	 * Blow away memberships we established in ipif_multicast_up().
18593 	 */
18594 	ipif_multicast_down(ipif);
18595 
18596 	/*
18597 	 * Remove from the mapping for __sin6_src_id. We insert only
18598 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18599 	 * stored as mapped addresses, we need to check for mapped
18600 	 * INADDR_ANY also.
18601 	 */
18602 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18603 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18604 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18605 		int err;
18606 
18607 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18608 		    ipif->ipif_zoneid, ipst);
18609 		if (err != 0) {
18610 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18611 		}
18612 	}
18613 
18614 	/*
18615 	 * Before we delete the ill from the group (if any), we need
18616 	 * to make sure that we delete all the routes dependent on
18617 	 * this and also any ipifs dependent on this ipif for
18618 	 * source address. We need to do before we delete from
18619 	 * the group because
18620 	 *
18621 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18622 	 *
18623 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18624 	 *    for re-doing source address selection. Note that
18625 	 *    ipif_select_source[_v6] called from
18626 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18627 	 *    because we have already marked down here i.e cleared
18628 	 *    IPIF_UP.
18629 	 */
18630 	if (ipif->ipif_isv6) {
18631 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18632 		    ipst);
18633 	} else {
18634 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18635 		    ipst);
18636 	}
18637 
18638 	/*
18639 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18640 	 * ires have been deleted above. Otherwise a thread could end up
18641 	 * caching an ire in a conn after we have finished the cleanup of the
18642 	 * conn. The caching is done after making sure that the ire is not yet
18643 	 * condemned. Also documented in the block comment above ip_output
18644 	 */
18645 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18646 	/* Also, delete the ires cached in SCTP */
18647 	sctp_ire_cache_flush(ipif);
18648 
18649 	/*
18650 	 * Update any other ipifs which have used "our" local address as
18651 	 * a source address. This entails removing and recreating IRE_INTERFACE
18652 	 * entries for such ipifs.
18653 	 */
18654 	if (ipif->ipif_isv6)
18655 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18656 	else
18657 		ipif_update_other_ipifs(ipif, ill->ill_group);
18658 
18659 	if (ipif_was_up) {
18660 		/*
18661 		 * Check whether it is last ipif to leave this group.
18662 		 * If this is the last ipif to leave, we should remove
18663 		 * this ill from the group as ipif_select_source will not
18664 		 * be able to find any useful ipifs if this ill is selected
18665 		 * for load balancing.
18666 		 *
18667 		 * For nameless groups, we should call ifgrp_delete if this
18668 		 * belongs to some group. As this ipif is going down, we may
18669 		 * need to reconstruct groups.
18670 		 */
18671 		phyi = ill->ill_phyint;
18672 		/*
18673 		 * If the phyint_groupname_len is 0, it may or may not
18674 		 * be in the nameless group. If the phyint_groupname_len is
18675 		 * not 0, then this ill should be part of some group.
18676 		 * As we always insert this ill in the group if
18677 		 * phyint_groupname_len is not zero when the first ipif
18678 		 * comes up (in ipif_up_done), it should be in a group
18679 		 * when the namelen is not 0.
18680 		 *
18681 		 * NOTE : When we delete the ill from the group,it will
18682 		 * blow away all the IRE_CACHES pointing either at this ipif or
18683 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18684 		 * should be pointing at this ill.
18685 		 */
18686 		ASSERT(phyi->phyint_groupname_len == 0 ||
18687 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18688 
18689 		if (phyi->phyint_groupname_len != 0) {
18690 			if (ill->ill_ipif_up_count == 0)
18691 				illgrp_delete(ill);
18692 		}
18693 
18694 		/*
18695 		 * If we have deleted some of the broadcast ires associated
18696 		 * with this ipif, we need to re-nominate somebody else if
18697 		 * the ires that we deleted were the nominated ones.
18698 		 */
18699 		if (ill->ill_group != NULL && !ill->ill_isv6)
18700 			ipif_renominate_bcast(ipif);
18701 	}
18702 
18703 	/*
18704 	 * neighbor-discovery or arp entries for this interface.
18705 	 */
18706 	ipif_ndp_down(ipif);
18707 
18708 	/*
18709 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18710 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18711 	 * and ill_delete -> ipif_free -> ipif_down
18712 	 */
18713 	if (mp == NULL) {
18714 		ASSERT(q == NULL);
18715 		return (0);
18716 	}
18717 
18718 	if (CONN_Q(q)) {
18719 		connp = Q_TO_CONN(q);
18720 		mutex_enter(&connp->conn_lock);
18721 	} else {
18722 		connp = NULL;
18723 	}
18724 	mutex_enter(&ill->ill_lock);
18725 	/*
18726 	 * Are there any ire's pointing to this ipif that are still active ?
18727 	 * If this is the last ipif going down, are there any ire's pointing
18728 	 * to this ill that are still active ?
18729 	 */
18730 	if (ipif_is_quiescent(ipif)) {
18731 		mutex_exit(&ill->ill_lock);
18732 		if (connp != NULL)
18733 			mutex_exit(&connp->conn_lock);
18734 		return (0);
18735 	}
18736 
18737 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18738 	    ill->ill_name, (void *)ill));
18739 	/*
18740 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18741 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18742 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18743 	 */
18744 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18745 	if (!success) {
18746 		/* The conn is closing. So just return */
18747 		ASSERT(connp != NULL);
18748 		mutex_exit(&ill->ill_lock);
18749 		mutex_exit(&connp->conn_lock);
18750 		return (EINTR);
18751 	}
18752 
18753 	mutex_exit(&ill->ill_lock);
18754 	if (connp != NULL)
18755 		mutex_exit(&connp->conn_lock);
18756 	return (EINPROGRESS);
18757 }
18758 
18759 void
18760 ipif_down_tail(ipif_t *ipif)
18761 {
18762 	ill_t	*ill = ipif->ipif_ill;
18763 
18764 	/*
18765 	 * Skip any loopback interface (null wq).
18766 	 * If this is the last logical interface on the ill
18767 	 * have ill_dl_down tell the driver we are gone (unbind)
18768 	 * Note that lun 0 can ipif_down even though
18769 	 * there are other logical units that are up.
18770 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18771 	 */
18772 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18773 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18774 	    ill->ill_dl_up) {
18775 		ill_dl_down(ill);
18776 	}
18777 	ill->ill_logical_down = 0;
18778 
18779 	/*
18780 	 * Have to be after removing the routes in ipif_down_delete_ire.
18781 	 */
18782 	if (ipif->ipif_isv6) {
18783 		if (ill->ill_flags & ILLF_XRESOLV)
18784 			ipif_arp_down(ipif);
18785 	} else {
18786 		ipif_arp_down(ipif);
18787 	}
18788 
18789 	ip_rts_ifmsg(ipif);
18790 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18791 }
18792 
18793 /*
18794  * Bring interface logically down without bringing the physical interface
18795  * down e.g. when the netmask is changed. This avoids long lasting link
18796  * negotiations between an ethernet interface and a certain switches.
18797  */
18798 static int
18799 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18800 {
18801 	/*
18802 	 * The ill_logical_down flag is a transient flag. It is set here
18803 	 * and is cleared once the down has completed in ipif_down_tail.
18804 	 * This flag does not indicate whether the ill stream is in the
18805 	 * DL_BOUND state with the driver. Instead this flag is used by
18806 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18807 	 * the driver. The state of the ill stream i.e. whether it is
18808 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18809 	 */
18810 	ipif->ipif_ill->ill_logical_down = 1;
18811 	return (ipif_down(ipif, q, mp));
18812 }
18813 
18814 /*
18815  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18816  * If the usesrc client ILL is already part of a usesrc group or not,
18817  * in either case a ire_stq with the matching usesrc client ILL will
18818  * locate the IRE's that need to be deleted. We want IREs to be created
18819  * with the new source address.
18820  */
18821 static void
18822 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18823 {
18824 	ill_t	*ucill = (ill_t *)ill_arg;
18825 
18826 	ASSERT(IAM_WRITER_ILL(ucill));
18827 
18828 	if (ire->ire_stq == NULL)
18829 		return;
18830 
18831 	if ((ire->ire_type == IRE_CACHE) &&
18832 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18833 		ire_delete(ire);
18834 }
18835 
18836 /*
18837  * ire_walk routine to delete every IRE dependent on the interface
18838  * address that is going down.	(Always called as writer.)
18839  * Works for both v4 and v6.
18840  * In addition for checking for ire_ipif matches it also checks for
18841  * IRE_CACHE entries which have the same source address as the
18842  * disappearing ipif since ipif_select_source might have picked
18843  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18844  * care of any IRE_INTERFACE with the disappearing source address.
18845  */
18846 static void
18847 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18848 {
18849 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18850 	ill_t *ire_ill;
18851 	ill_t *ipif_ill;
18852 
18853 	ASSERT(IAM_WRITER_IPIF(ipif));
18854 	if (ire->ire_ipif == NULL)
18855 		return;
18856 
18857 	/*
18858 	 * For IPv4, we derive source addresses for an IRE from ipif's
18859 	 * belonging to the same IPMP group as the IRE's outgoing
18860 	 * interface.  If an IRE's outgoing interface isn't in the
18861 	 * same IPMP group as a particular ipif, then that ipif
18862 	 * couldn't have been used as a source address for this IRE.
18863 	 *
18864 	 * For IPv6, source addresses are only restricted to the IPMP group
18865 	 * if the IRE is for a link-local address or a multicast address.
18866 	 * Otherwise, source addresses for an IRE can be chosen from
18867 	 * interfaces other than the the outgoing interface for that IRE.
18868 	 *
18869 	 * For source address selection details, see ipif_select_source()
18870 	 * and ipif_select_source_v6().
18871 	 */
18872 	if (ire->ire_ipversion == IPV4_VERSION ||
18873 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18874 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18875 		ire_ill = ire->ire_ipif->ipif_ill;
18876 		ipif_ill = ipif->ipif_ill;
18877 
18878 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18879 			return;
18880 		}
18881 	}
18882 
18883 	if (ire->ire_ipif != ipif) {
18884 		/*
18885 		 * Look for a matching source address.
18886 		 */
18887 		if (ire->ire_type != IRE_CACHE)
18888 			return;
18889 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18890 			return;
18891 
18892 		if (ire->ire_ipversion == IPV4_VERSION) {
18893 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18894 				return;
18895 		} else {
18896 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18897 			    &ipif->ipif_v6lcl_addr))
18898 				return;
18899 		}
18900 		ire_delete(ire);
18901 		return;
18902 	}
18903 	/*
18904 	 * ire_delete() will do an ire_flush_cache which will delete
18905 	 * all ire_ipif matches
18906 	 */
18907 	ire_delete(ire);
18908 }
18909 
18910 /*
18911  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18912  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18913  * 2) when an interface is brought up or down (on that ill).
18914  * This ensures that the IRE_CACHE entries don't retain stale source
18915  * address selection results.
18916  */
18917 void
18918 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18919 {
18920 	ill_t	*ill = (ill_t *)ill_arg;
18921 	ill_t	*ipif_ill;
18922 
18923 	ASSERT(IAM_WRITER_ILL(ill));
18924 	/*
18925 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18926 	 * Hence this should be IRE_CACHE.
18927 	 */
18928 	ASSERT(ire->ire_type == IRE_CACHE);
18929 
18930 	/*
18931 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18932 	 * We are only interested in IRE_CACHES that has borrowed
18933 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18934 	 * for which we need to look at ire_ipif->ipif_ill match
18935 	 * with ill.
18936 	 */
18937 	ASSERT(ire->ire_ipif != NULL);
18938 	ipif_ill = ire->ire_ipif->ipif_ill;
18939 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18940 	    ipif_ill->ill_group == ill->ill_group)) {
18941 		ire_delete(ire);
18942 	}
18943 }
18944 
18945 /*
18946  * Delete all the ire whose stq references ill_arg.
18947  */
18948 static void
18949 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18950 {
18951 	ill_t	*ill = (ill_t *)ill_arg;
18952 	ill_t	*ire_ill;
18953 
18954 	ASSERT(IAM_WRITER_ILL(ill));
18955 	/*
18956 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18957 	 * Hence this should be IRE_CACHE.
18958 	 */
18959 	ASSERT(ire->ire_type == IRE_CACHE);
18960 
18961 	/*
18962 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18963 	 * matches ill. We are only interested in IRE_CACHES that
18964 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
18965 	 * filtering here.
18966 	 */
18967 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
18968 
18969 	if (ire_ill == ill)
18970 		ire_delete(ire);
18971 }
18972 
18973 /*
18974  * This is called when an ill leaves the group. We want to delete
18975  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
18976  * pointing at ill.
18977  */
18978 static void
18979 illgrp_cache_delete(ire_t *ire, char *ill_arg)
18980 {
18981 	ill_t	*ill = (ill_t *)ill_arg;
18982 
18983 	ASSERT(IAM_WRITER_ILL(ill));
18984 	ASSERT(ill->ill_group == NULL);
18985 	/*
18986 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18987 	 * Hence this should be IRE_CACHE.
18988 	 */
18989 	ASSERT(ire->ire_type == IRE_CACHE);
18990 	/*
18991 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18992 	 * matches ill. We are interested in both.
18993 	 */
18994 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
18995 	    (ire->ire_ipif->ipif_ill == ill));
18996 
18997 	ire_delete(ire);
18998 }
18999 
19000 /*
19001  * Initiate deallocate of an IPIF. Always called as writer. Called by
19002  * ill_delete or ip_sioctl_removeif.
19003  */
19004 static void
19005 ipif_free(ipif_t *ipif)
19006 {
19007 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19008 
19009 	ASSERT(IAM_WRITER_IPIF(ipif));
19010 
19011 	if (ipif->ipif_recovery_id != 0)
19012 		(void) untimeout(ipif->ipif_recovery_id);
19013 	ipif->ipif_recovery_id = 0;
19014 
19015 	/* Remove conn references */
19016 	reset_conn_ipif(ipif);
19017 
19018 	/*
19019 	 * Make sure we have valid net and subnet broadcast ire's for the
19020 	 * other ipif's which share them with this ipif.
19021 	 */
19022 	if (!ipif->ipif_isv6)
19023 		ipif_check_bcast_ires(ipif);
19024 
19025 	/*
19026 	 * Take down the interface. We can be called either from ill_delete
19027 	 * or from ip_sioctl_removeif.
19028 	 */
19029 	(void) ipif_down(ipif, NULL, NULL);
19030 
19031 	/*
19032 	 * Now that the interface is down, there's no chance it can still
19033 	 * become a duplicate.  Cancel any timer that may have been set while
19034 	 * tearing down.
19035 	 */
19036 	if (ipif->ipif_recovery_id != 0)
19037 		(void) untimeout(ipif->ipif_recovery_id);
19038 	ipif->ipif_recovery_id = 0;
19039 
19040 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19041 	/* Remove pointers to this ill in the multicast routing tables */
19042 	reset_mrt_vif_ipif(ipif);
19043 	rw_exit(&ipst->ips_ill_g_lock);
19044 }
19045 
19046 /*
19047  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19048  * also ill_move().
19049  */
19050 static void
19051 ipif_free_tail(ipif_t *ipif)
19052 {
19053 	mblk_t	*mp;
19054 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19055 
19056 	/*
19057 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19058 	 */
19059 	mutex_enter(&ipif->ipif_saved_ire_lock);
19060 	mp = ipif->ipif_saved_ire_mp;
19061 	ipif->ipif_saved_ire_mp = NULL;
19062 	mutex_exit(&ipif->ipif_saved_ire_lock);
19063 	freemsg(mp);
19064 
19065 	/*
19066 	 * Need to hold both ill_g_lock and ill_lock while
19067 	 * inserting or removing an ipif from the linked list
19068 	 * of ipifs hanging off the ill.
19069 	 */
19070 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19071 
19072 	ASSERT(ilm_walk_ipif(ipif) == 0);
19073 
19074 #ifdef DEBUG
19075 	ipif_trace_cleanup(ipif);
19076 #endif
19077 
19078 	/* Ask SCTP to take it out of it list */
19079 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19080 
19081 	/* Get it out of the ILL interface list. */
19082 	ipif_remove(ipif, B_TRUE);
19083 	rw_exit(&ipst->ips_ill_g_lock);
19084 
19085 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19086 
19087 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19088 	ASSERT(ipif->ipif_recovery_id == 0);
19089 
19090 	/* Free the memory. */
19091 	mi_free(ipif);
19092 }
19093 
19094 /*
19095  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19096  * is zero.
19097  */
19098 void
19099 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19100 {
19101 	char	lbuf[LIFNAMSIZ];
19102 	char	*name;
19103 	size_t	name_len;
19104 
19105 	buf[0] = '\0';
19106 	name = ipif->ipif_ill->ill_name;
19107 	name_len = ipif->ipif_ill->ill_name_length;
19108 	if (ipif->ipif_id != 0) {
19109 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19110 		    ipif->ipif_id);
19111 		name = lbuf;
19112 		name_len = mi_strlen(name) + 1;
19113 	}
19114 	len -= 1;
19115 	buf[len] = '\0';
19116 	len = MIN(len, name_len);
19117 	bcopy(name, buf, len);
19118 }
19119 
19120 /*
19121  * Find an IPIF based on the name passed in.  Names can be of the
19122  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19123  * The <phys> string can have forms like <dev><#> (e.g., le0),
19124  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19125  * When there is no colon, the implied unit id is zero. <phys> must
19126  * correspond to the name of an ILL.  (May be called as writer.)
19127  */
19128 static ipif_t *
19129 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19130     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19131     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19132 {
19133 	char	*cp;
19134 	char	*endp;
19135 	long	id;
19136 	ill_t	*ill;
19137 	ipif_t	*ipif;
19138 	uint_t	ire_type;
19139 	boolean_t did_alloc = B_FALSE;
19140 	ipsq_t	*ipsq;
19141 
19142 	if (error != NULL)
19143 		*error = 0;
19144 
19145 	/*
19146 	 * If the caller wants to us to create the ipif, make sure we have a
19147 	 * valid zoneid
19148 	 */
19149 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19150 
19151 	if (namelen == 0) {
19152 		if (error != NULL)
19153 			*error = ENXIO;
19154 		return (NULL);
19155 	}
19156 
19157 	*exists = B_FALSE;
19158 	/* Look for a colon in the name. */
19159 	endp = &name[namelen];
19160 	for (cp = endp; --cp > name; ) {
19161 		if (*cp == IPIF_SEPARATOR_CHAR)
19162 			break;
19163 	}
19164 
19165 	if (*cp == IPIF_SEPARATOR_CHAR) {
19166 		/*
19167 		 * Reject any non-decimal aliases for logical
19168 		 * interfaces. Aliases with leading zeroes
19169 		 * are also rejected as they introduce ambiguity
19170 		 * in the naming of the interfaces.
19171 		 * In order to confirm with existing semantics,
19172 		 * and to not break any programs/script relying
19173 		 * on that behaviour, if<0>:0 is considered to be
19174 		 * a valid interface.
19175 		 *
19176 		 * If alias has two or more digits and the first
19177 		 * is zero, fail.
19178 		 */
19179 		if (&cp[2] < endp && cp[1] == '0') {
19180 			if (error != NULL)
19181 				*error = EINVAL;
19182 			return (NULL);
19183 		}
19184 	}
19185 
19186 	if (cp <= name) {
19187 		cp = endp;
19188 	} else {
19189 		*cp = '\0';
19190 	}
19191 
19192 	/*
19193 	 * Look up the ILL, based on the portion of the name
19194 	 * before the slash. ill_lookup_on_name returns a held ill.
19195 	 * Temporary to check whether ill exists already. If so
19196 	 * ill_lookup_on_name will clear it.
19197 	 */
19198 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19199 	    q, mp, func, error, &did_alloc, ipst);
19200 	if (cp != endp)
19201 		*cp = IPIF_SEPARATOR_CHAR;
19202 	if (ill == NULL)
19203 		return (NULL);
19204 
19205 	/* Establish the unit number in the name. */
19206 	id = 0;
19207 	if (cp < endp && *endp == '\0') {
19208 		/* If there was a colon, the unit number follows. */
19209 		cp++;
19210 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19211 			ill_refrele(ill);
19212 			if (error != NULL)
19213 				*error = ENXIO;
19214 			return (NULL);
19215 		}
19216 	}
19217 
19218 	GRAB_CONN_LOCK(q);
19219 	mutex_enter(&ill->ill_lock);
19220 	/* Now see if there is an IPIF with this unit number. */
19221 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19222 		if (ipif->ipif_id == id) {
19223 			if (zoneid != ALL_ZONES &&
19224 			    zoneid != ipif->ipif_zoneid &&
19225 			    ipif->ipif_zoneid != ALL_ZONES) {
19226 				mutex_exit(&ill->ill_lock);
19227 				RELEASE_CONN_LOCK(q);
19228 				ill_refrele(ill);
19229 				if (error != NULL)
19230 					*error = ENXIO;
19231 				return (NULL);
19232 			}
19233 			/*
19234 			 * The block comment at the start of ipif_down
19235 			 * explains the use of the macros used below
19236 			 */
19237 			if (IPIF_CAN_LOOKUP(ipif)) {
19238 				ipif_refhold_locked(ipif);
19239 				mutex_exit(&ill->ill_lock);
19240 				if (!did_alloc)
19241 					*exists = B_TRUE;
19242 				/*
19243 				 * Drop locks before calling ill_refrele
19244 				 * since it can potentially call into
19245 				 * ipif_ill_refrele_tail which can end up
19246 				 * in trying to acquire any lock.
19247 				 */
19248 				RELEASE_CONN_LOCK(q);
19249 				ill_refrele(ill);
19250 				return (ipif);
19251 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19252 				ipsq = ill->ill_phyint->phyint_ipsq;
19253 				mutex_enter(&ipsq->ipsq_lock);
19254 				mutex_exit(&ill->ill_lock);
19255 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19256 				mutex_exit(&ipsq->ipsq_lock);
19257 				RELEASE_CONN_LOCK(q);
19258 				ill_refrele(ill);
19259 				if (error != NULL)
19260 					*error = EINPROGRESS;
19261 				return (NULL);
19262 			}
19263 		}
19264 	}
19265 	RELEASE_CONN_LOCK(q);
19266 
19267 	if (!do_alloc) {
19268 		mutex_exit(&ill->ill_lock);
19269 		ill_refrele(ill);
19270 		if (error != NULL)
19271 			*error = ENXIO;
19272 		return (NULL);
19273 	}
19274 
19275 	/*
19276 	 * If none found, atomically allocate and return a new one.
19277 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19278 	 * to support "receive only" use of lo0:1 etc. as is still done
19279 	 * below as an initial guess.
19280 	 * However, this is now likely to be overriden later in ipif_up_done()
19281 	 * when we know for sure what address has been configured on the
19282 	 * interface, since we might have more than one loopback interface
19283 	 * with a loopback address, e.g. in the case of zones, and all the
19284 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19285 	 */
19286 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19287 		ire_type = IRE_LOOPBACK;
19288 	else
19289 		ire_type = IRE_LOCAL;
19290 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19291 	if (ipif != NULL)
19292 		ipif_refhold_locked(ipif);
19293 	else if (error != NULL)
19294 		*error = ENOMEM;
19295 	mutex_exit(&ill->ill_lock);
19296 	ill_refrele(ill);
19297 	return (ipif);
19298 }
19299 
19300 /*
19301  * This routine is called whenever a new address comes up on an ipif.  If
19302  * we are configured to respond to address mask requests, then we are supposed
19303  * to broadcast an address mask reply at this time.  This routine is also
19304  * called if we are already up, but a netmask change is made.  This is legal
19305  * but might not make the system manager very popular.	(May be called
19306  * as writer.)
19307  */
19308 void
19309 ipif_mask_reply(ipif_t *ipif)
19310 {
19311 	icmph_t	*icmph;
19312 	ipha_t	*ipha;
19313 	mblk_t	*mp;
19314 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19315 
19316 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19317 
19318 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19319 		return;
19320 
19321 	/* ICMP mask reply is IPv4 only */
19322 	ASSERT(!ipif->ipif_isv6);
19323 	/* ICMP mask reply is not for a loopback interface */
19324 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19325 
19326 	mp = allocb(REPLY_LEN, BPRI_HI);
19327 	if (mp == NULL)
19328 		return;
19329 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19330 
19331 	ipha = (ipha_t *)mp->b_rptr;
19332 	bzero(ipha, REPLY_LEN);
19333 	*ipha = icmp_ipha;
19334 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19335 	ipha->ipha_src = ipif->ipif_src_addr;
19336 	ipha->ipha_dst = ipif->ipif_brd_addr;
19337 	ipha->ipha_length = htons(REPLY_LEN);
19338 	ipha->ipha_ident = 0;
19339 
19340 	icmph = (icmph_t *)&ipha[1];
19341 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19342 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19343 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19344 
19345 	put(ipif->ipif_wq, mp);
19346 
19347 #undef	REPLY_LEN
19348 }
19349 
19350 /*
19351  * When the mtu in the ipif changes, we call this routine through ire_walk
19352  * to update all the relevant IREs.
19353  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19354  */
19355 static void
19356 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19357 {
19358 	ipif_t *ipif = (ipif_t *)ipif_arg;
19359 
19360 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19361 		return;
19362 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19363 }
19364 
19365 /*
19366  * When the mtu in the ill changes, we call this routine through ire_walk
19367  * to update all the relevant IREs.
19368  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19369  */
19370 void
19371 ill_mtu_change(ire_t *ire, char *ill_arg)
19372 {
19373 	ill_t	*ill = (ill_t *)ill_arg;
19374 
19375 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19376 		return;
19377 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19378 }
19379 
19380 /*
19381  * Join the ipif specific multicast groups.
19382  * Must be called after a mapping has been set up in the resolver.  (Always
19383  * called as writer.)
19384  */
19385 void
19386 ipif_multicast_up(ipif_t *ipif)
19387 {
19388 	int err, index;
19389 	ill_t *ill;
19390 
19391 	ASSERT(IAM_WRITER_IPIF(ipif));
19392 
19393 	ill = ipif->ipif_ill;
19394 	index = ill->ill_phyint->phyint_ifindex;
19395 
19396 	ip1dbg(("ipif_multicast_up\n"));
19397 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19398 		return;
19399 
19400 	if (ipif->ipif_isv6) {
19401 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19402 			return;
19403 
19404 		/* Join the all hosts multicast address */
19405 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19406 		/*
19407 		 * Passing B_TRUE means we have to join the multicast
19408 		 * membership on this interface even though this is
19409 		 * FAILED. If we join on a different one in the group,
19410 		 * we will not be able to delete the membership later
19411 		 * as we currently don't track where we join when we
19412 		 * join within the kernel unlike applications where
19413 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19414 		 * for more on this.
19415 		 */
19416 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19417 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19418 		if (err != 0) {
19419 			ip0dbg(("ipif_multicast_up: "
19420 			    "all_hosts_mcast failed %d\n",
19421 			    err));
19422 			return;
19423 		}
19424 		/*
19425 		 * Enable multicast for the solicited node multicast address
19426 		 */
19427 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19428 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19429 
19430 			ipv6_multi.s6_addr32[3] |=
19431 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19432 
19433 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19434 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19435 			    NULL);
19436 			if (err != 0) {
19437 				ip0dbg(("ipif_multicast_up: solicited MC"
19438 				    " failed %d\n", err));
19439 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19440 				    ill, ill->ill_phyint->phyint_ifindex,
19441 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19442 				return;
19443 			}
19444 		}
19445 	} else {
19446 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19447 			return;
19448 
19449 		/* Join the all hosts multicast address */
19450 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19451 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19452 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19453 		if (err) {
19454 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19455 			return;
19456 		}
19457 	}
19458 	ipif->ipif_multicast_up = 1;
19459 }
19460 
19461 /*
19462  * Blow away any multicast groups that we joined in ipif_multicast_up().
19463  * (Explicit memberships are blown away in ill_leave_multicast() when the
19464  * ill is brought down.)
19465  */
19466 static void
19467 ipif_multicast_down(ipif_t *ipif)
19468 {
19469 	int err;
19470 
19471 	ASSERT(IAM_WRITER_IPIF(ipif));
19472 
19473 	ip1dbg(("ipif_multicast_down\n"));
19474 	if (!ipif->ipif_multicast_up)
19475 		return;
19476 
19477 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19478 
19479 	if (!ipif->ipif_isv6) {
19480 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19481 		    B_TRUE);
19482 		if (err != 0)
19483 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19484 
19485 		ipif->ipif_multicast_up = 0;
19486 		return;
19487 	}
19488 
19489 	/*
19490 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19491 	 * we should look for ilms on this ill rather than the ones that have
19492 	 * been failed over here.  They are here temporarily. As
19493 	 * ipif_multicast_up has joined on this ill, we should delete only
19494 	 * from this ill.
19495 	 */
19496 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19497 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19498 	    B_TRUE, B_TRUE);
19499 	if (err != 0) {
19500 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19501 		    err));
19502 	}
19503 	/*
19504 	 * Disable multicast for the solicited node multicast address
19505 	 */
19506 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19507 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19508 
19509 		ipv6_multi.s6_addr32[3] |=
19510 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19511 
19512 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19513 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19514 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19515 
19516 		if (err != 0) {
19517 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19518 			    err));
19519 		}
19520 	}
19521 
19522 	ipif->ipif_multicast_up = 0;
19523 }
19524 
19525 /*
19526  * Used when an interface comes up to recreate any extra routes on this
19527  * interface.
19528  */
19529 static ire_t **
19530 ipif_recover_ire(ipif_t *ipif)
19531 {
19532 	mblk_t	*mp;
19533 	ire_t	**ipif_saved_irep;
19534 	ire_t	**irep;
19535 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19536 
19537 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19538 	    ipif->ipif_id));
19539 
19540 	mutex_enter(&ipif->ipif_saved_ire_lock);
19541 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19542 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19543 	if (ipif_saved_irep == NULL) {
19544 		mutex_exit(&ipif->ipif_saved_ire_lock);
19545 		return (NULL);
19546 	}
19547 
19548 	irep = ipif_saved_irep;
19549 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19550 		ire_t		*ire;
19551 		queue_t		*rfq;
19552 		queue_t		*stq;
19553 		ifrt_t		*ifrt;
19554 		uchar_t		*src_addr;
19555 		uchar_t		*gateway_addr;
19556 		ushort_t	type;
19557 
19558 		/*
19559 		 * When the ire was initially created and then added in
19560 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19561 		 * in the case of a traditional interface route, or as one of
19562 		 * the IRE_OFFSUBNET types (with the exception of
19563 		 * IRE_HOST types ire which is created by icmp_redirect() and
19564 		 * which we don't need to save or recover).  In the case where
19565 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19566 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19567 		 * to satisfy software like GateD and Sun Cluster which creates
19568 		 * routes using the the loopback interface's address as a
19569 		 * gateway.
19570 		 *
19571 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19572 		 * ire_create() will be called in the same way here as
19573 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19574 		 * the route looks like a traditional interface route (where
19575 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19576 		 * the saved ifrt->ifrt_type.  This means that in the case where
19577 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19578 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19579 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19580 		 */
19581 		ifrt = (ifrt_t *)mp->b_rptr;
19582 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19583 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19584 			rfq = NULL;
19585 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19586 			    ? ipif->ipif_rq : ipif->ipif_wq;
19587 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19588 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19589 			    : (uint8_t *)&ipif->ipif_src_addr;
19590 			gateway_addr = NULL;
19591 			type = ipif->ipif_net_type;
19592 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19593 			/* Recover multiroute broadcast IRE. */
19594 			rfq = ipif->ipif_rq;
19595 			stq = ipif->ipif_wq;
19596 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19597 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19598 			    : (uint8_t *)&ipif->ipif_src_addr;
19599 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19600 			type = ifrt->ifrt_type;
19601 		} else {
19602 			rfq = NULL;
19603 			stq = NULL;
19604 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19605 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19606 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19607 			type = ifrt->ifrt_type;
19608 		}
19609 
19610 		/*
19611 		 * Create a copy of the IRE with the saved address and netmask.
19612 		 */
19613 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19614 		    "0x%x/0x%x\n",
19615 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19616 		    ntohl(ifrt->ifrt_addr),
19617 		    ntohl(ifrt->ifrt_mask)));
19618 		ire = ire_create(
19619 		    (uint8_t *)&ifrt->ifrt_addr,
19620 		    (uint8_t *)&ifrt->ifrt_mask,
19621 		    src_addr,
19622 		    gateway_addr,
19623 		    &ifrt->ifrt_max_frag,
19624 		    NULL,
19625 		    rfq,
19626 		    stq,
19627 		    type,
19628 		    ipif,
19629 		    0,
19630 		    0,
19631 		    0,
19632 		    ifrt->ifrt_flags,
19633 		    &ifrt->ifrt_iulp_info,
19634 		    NULL,
19635 		    NULL,
19636 		    ipst);
19637 
19638 		if (ire == NULL) {
19639 			mutex_exit(&ipif->ipif_saved_ire_lock);
19640 			kmem_free(ipif_saved_irep,
19641 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19642 			return (NULL);
19643 		}
19644 
19645 		/*
19646 		 * Some software (for example, GateD and Sun Cluster) attempts
19647 		 * to create (what amount to) IRE_PREFIX routes with the
19648 		 * loopback address as the gateway.  This is primarily done to
19649 		 * set up prefixes with the RTF_REJECT flag set (for example,
19650 		 * when generating aggregate routes.)
19651 		 *
19652 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19653 		 * IRE_LOOPBACK, then we map the request into a
19654 		 * IRE_IF_NORESOLVER.
19655 		 */
19656 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19657 			ire->ire_type = IRE_IF_NORESOLVER;
19658 		/*
19659 		 * ire held by ire_add, will be refreled' towards the
19660 		 * the end of ipif_up_done
19661 		 */
19662 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19663 		*irep = ire;
19664 		irep++;
19665 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19666 	}
19667 	mutex_exit(&ipif->ipif_saved_ire_lock);
19668 	return (ipif_saved_irep);
19669 }
19670 
19671 /*
19672  * Used to set the netmask and broadcast address to default values when the
19673  * interface is brought up.  (Always called as writer.)
19674  */
19675 static void
19676 ipif_set_default(ipif_t *ipif)
19677 {
19678 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19679 
19680 	if (!ipif->ipif_isv6) {
19681 		/*
19682 		 * Interface holds an IPv4 address. Default
19683 		 * mask is the natural netmask.
19684 		 */
19685 		if (!ipif->ipif_net_mask) {
19686 			ipaddr_t	v4mask;
19687 
19688 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19689 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19690 		}
19691 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19692 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19693 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19694 		} else {
19695 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19696 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19697 		}
19698 		/*
19699 		 * NOTE: SunOS 4.X does this even if the broadcast address
19700 		 * has been already set thus we do the same here.
19701 		 */
19702 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19703 			ipaddr_t	v4addr;
19704 
19705 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19706 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19707 		}
19708 	} else {
19709 		/*
19710 		 * Interface holds an IPv6-only address.  Default
19711 		 * mask is all-ones.
19712 		 */
19713 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19714 			ipif->ipif_v6net_mask = ipv6_all_ones;
19715 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19716 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19717 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19718 		} else {
19719 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19720 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19721 		}
19722 	}
19723 }
19724 
19725 /*
19726  * Return 0 if this address can be used as local address without causing
19727  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19728  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19729  * Special checks are needed to allow the same IPv6 link-local address
19730  * on different ills.
19731  * TODO: allowing the same site-local address on different ill's.
19732  */
19733 int
19734 ip_addr_availability_check(ipif_t *new_ipif)
19735 {
19736 	in6_addr_t our_v6addr;
19737 	ill_t *ill;
19738 	ipif_t *ipif;
19739 	ill_walk_context_t ctx;
19740 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19741 
19742 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19743 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19744 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19745 
19746 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19747 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19748 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19749 		return (0);
19750 
19751 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19752 
19753 	if (new_ipif->ipif_isv6)
19754 		ill = ILL_START_WALK_V6(&ctx, ipst);
19755 	else
19756 		ill = ILL_START_WALK_V4(&ctx, ipst);
19757 
19758 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19759 		for (ipif = ill->ill_ipif; ipif != NULL;
19760 		    ipif = ipif->ipif_next) {
19761 			if ((ipif == new_ipif) ||
19762 			    !(ipif->ipif_flags & IPIF_UP) ||
19763 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19764 				continue;
19765 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19766 			    &our_v6addr)) {
19767 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19768 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19769 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19770 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19771 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19772 				    new_ipif->ipif_ill != ill)
19773 					continue;
19774 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19775 				    new_ipif->ipif_ill != ill)
19776 					continue;
19777 				else if (new_ipif->ipif_zoneid !=
19778 				    ipif->ipif_zoneid &&
19779 				    ipif->ipif_zoneid != ALL_ZONES &&
19780 				    IS_LOOPBACK(ill))
19781 					continue;
19782 				else if (new_ipif->ipif_ill == ill)
19783 					return (EADDRINUSE);
19784 				else
19785 					return (EADDRNOTAVAIL);
19786 			}
19787 		}
19788 	}
19789 
19790 	return (0);
19791 }
19792 
19793 /*
19794  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19795  * IREs for the ipif.
19796  * When the routine returns EINPROGRESS then mp has been consumed and
19797  * the ioctl will be acked from ip_rput_dlpi.
19798  */
19799 static int
19800 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19801 {
19802 	ill_t	*ill = ipif->ipif_ill;
19803 	boolean_t isv6 = ipif->ipif_isv6;
19804 	int	err = 0;
19805 	boolean_t success;
19806 
19807 	ASSERT(IAM_WRITER_IPIF(ipif));
19808 
19809 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19810 
19811 	/* Shouldn't get here if it is already up. */
19812 	if (ipif->ipif_flags & IPIF_UP)
19813 		return (EALREADY);
19814 
19815 	/* Skip arp/ndp for any loopback interface. */
19816 	if (ill->ill_wq != NULL) {
19817 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19818 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19819 
19820 		if (!ill->ill_dl_up) {
19821 			/*
19822 			 * ill_dl_up is not yet set. i.e. we are yet to
19823 			 * DL_BIND with the driver and this is the first
19824 			 * logical interface on the ill to become "up".
19825 			 * Tell the driver to get going (via DL_BIND_REQ).
19826 			 * Note that changing "significant" IFF_ flags
19827 			 * address/netmask etc cause a down/up dance, but
19828 			 * does not cause an unbind (DL_UNBIND) with the driver
19829 			 */
19830 			return (ill_dl_up(ill, ipif, mp, q));
19831 		}
19832 
19833 		/*
19834 		 * ipif_resolver_up may end up sending an
19835 		 * AR_INTERFACE_UP message to ARP, which would, in
19836 		 * turn send a DLPI message to the driver. ioctls are
19837 		 * serialized and so we cannot send more than one
19838 		 * interface up message at a time. If ipif_resolver_up
19839 		 * does send an interface up message to ARP, we get
19840 		 * EINPROGRESS and we will complete in ip_arp_done.
19841 		 */
19842 
19843 		ASSERT(connp != NULL || !CONN_Q(q));
19844 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19845 		if (connp != NULL)
19846 			mutex_enter(&connp->conn_lock);
19847 		mutex_enter(&ill->ill_lock);
19848 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19849 		mutex_exit(&ill->ill_lock);
19850 		if (connp != NULL)
19851 			mutex_exit(&connp->conn_lock);
19852 		if (!success)
19853 			return (EINTR);
19854 
19855 		/*
19856 		 * Crank up IPv6 neighbor discovery
19857 		 * Unlike ARP, this should complete when
19858 		 * ipif_ndp_up returns. However, for
19859 		 * ILLF_XRESOLV interfaces we also send a
19860 		 * AR_INTERFACE_UP to the external resolver.
19861 		 * That ioctl will complete in ip_rput.
19862 		 */
19863 		if (isv6) {
19864 			err = ipif_ndp_up(ipif);
19865 			if (err != 0) {
19866 				if (err != EINPROGRESS)
19867 					mp = ipsq_pending_mp_get(ipsq, &connp);
19868 				return (err);
19869 			}
19870 		}
19871 		/* Now, ARP */
19872 		err = ipif_resolver_up(ipif, Res_act_initial);
19873 		if (err == EINPROGRESS) {
19874 			/* We will complete it in ip_arp_done */
19875 			return (err);
19876 		}
19877 		mp = ipsq_pending_mp_get(ipsq, &connp);
19878 		ASSERT(mp != NULL);
19879 		if (err != 0)
19880 			return (err);
19881 	} else {
19882 		/*
19883 		 * Interfaces without underlying hardware don't do duplicate
19884 		 * address detection.
19885 		 */
19886 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19887 		ipif->ipif_addr_ready = 1;
19888 	}
19889 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19890 }
19891 
19892 /*
19893  * Perform a bind for the physical device.
19894  * When the routine returns EINPROGRESS then mp has been consumed and
19895  * the ioctl will be acked from ip_rput_dlpi.
19896  * Allocate an unbind message and save it until ipif_down.
19897  */
19898 static int
19899 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19900 {
19901 	areq_t	*areq;
19902 	mblk_t	*areq_mp = NULL;
19903 	mblk_t	*bind_mp = NULL;
19904 	mblk_t	*unbind_mp = NULL;
19905 	conn_t	*connp;
19906 	boolean_t success;
19907 	uint16_t sap_addr;
19908 
19909 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19910 	ASSERT(IAM_WRITER_ILL(ill));
19911 	ASSERT(mp != NULL);
19912 
19913 	/* Create a resolver cookie for ARP */
19914 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19915 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19916 		if (areq_mp == NULL)
19917 			return (ENOMEM);
19918 
19919 		freemsg(ill->ill_resolver_mp);
19920 		ill->ill_resolver_mp = areq_mp;
19921 		areq = (areq_t *)areq_mp->b_rptr;
19922 		sap_addr = ill->ill_sap;
19923 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19924 	}
19925 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19926 	    DL_BIND_REQ);
19927 	if (bind_mp == NULL)
19928 		goto bad;
19929 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19930 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19931 
19932 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19933 	if (unbind_mp == NULL)
19934 		goto bad;
19935 
19936 	/*
19937 	 * Record state needed to complete this operation when the
19938 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19939 	 */
19940 	ASSERT(WR(q)->q_next == NULL);
19941 	connp = Q_TO_CONN(q);
19942 
19943 	mutex_enter(&connp->conn_lock);
19944 	mutex_enter(&ipif->ipif_ill->ill_lock);
19945 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19946 	mutex_exit(&ipif->ipif_ill->ill_lock);
19947 	mutex_exit(&connp->conn_lock);
19948 	if (!success)
19949 		goto bad;
19950 
19951 	/*
19952 	 * Save the unbind message for ill_dl_down(); it will be consumed when
19953 	 * the interface goes down.
19954 	 */
19955 	ASSERT(ill->ill_unbind_mp == NULL);
19956 	ill->ill_unbind_mp = unbind_mp;
19957 
19958 	ill_dlpi_send(ill, bind_mp);
19959 	/* Send down link-layer capabilities probe if not already done. */
19960 	ill_capability_probe(ill);
19961 
19962 	/*
19963 	 * Sysid used to rely on the fact that netboots set domainname
19964 	 * and the like. Now that miniroot boots aren't strictly netboots
19965 	 * and miniroot network configuration is driven from userland
19966 	 * these things still need to be set. This situation can be detected
19967 	 * by comparing the interface being configured here to the one
19968 	 * dhcifname was set to reference by the boot loader. Once sysid is
19969 	 * converted to use dhcp_ipc_getinfo() this call can go away.
19970 	 */
19971 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
19972 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
19973 	    (strlen(srpc_domain) == 0)) {
19974 		if (dhcpinit() != 0)
19975 			cmn_err(CE_WARN, "no cached dhcp response");
19976 	}
19977 
19978 	/*
19979 	 * This operation will complete in ip_rput_dlpi with either
19980 	 * a DL_BIND_ACK or DL_ERROR_ACK.
19981 	 */
19982 	return (EINPROGRESS);
19983 bad:
19984 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
19985 	/*
19986 	 * We don't have to check for possible removal from illgrp
19987 	 * as we have not yet inserted in illgrp. For groups
19988 	 * without names, this ipif is still not UP and hence
19989 	 * this could not have possibly had any influence in forming
19990 	 * groups.
19991 	 */
19992 
19993 	freemsg(bind_mp);
19994 	freemsg(unbind_mp);
19995 	return (ENOMEM);
19996 }
19997 
19998 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
19999 
20000 /*
20001  * DLPI and ARP is up.
20002  * Create all the IREs associated with an interface bring up multicast.
20003  * Set the interface flag and finish other initialization
20004  * that potentially had to be differed to after DL_BIND_ACK.
20005  */
20006 int
20007 ipif_up_done(ipif_t *ipif)
20008 {
20009 	ire_t	*ire_array[20];
20010 	ire_t	**irep = ire_array;
20011 	ire_t	**irep1;
20012 	ipaddr_t net_mask = 0;
20013 	ipaddr_t subnet_mask, route_mask;
20014 	ill_t	*ill = ipif->ipif_ill;
20015 	queue_t	*stq;
20016 	ipif_t	 *src_ipif;
20017 	ipif_t   *tmp_ipif;
20018 	boolean_t	flush_ire_cache = B_TRUE;
20019 	int	err = 0;
20020 	phyint_t *phyi;
20021 	ire_t	**ipif_saved_irep = NULL;
20022 	int ipif_saved_ire_cnt;
20023 	int	cnt;
20024 	boolean_t	src_ipif_held = B_FALSE;
20025 	boolean_t	ire_added = B_FALSE;
20026 	boolean_t	loopback = B_FALSE;
20027 	ip_stack_t	*ipst = ill->ill_ipst;
20028 
20029 	ip1dbg(("ipif_up_done(%s:%u)\n",
20030 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20031 	/* Check if this is a loopback interface */
20032 	if (ipif->ipif_ill->ill_wq == NULL)
20033 		loopback = B_TRUE;
20034 
20035 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20036 	/*
20037 	 * If all other interfaces for this ill are down or DEPRECATED,
20038 	 * or otherwise unsuitable for source address selection, remove
20039 	 * any IRE_CACHE entries for this ill to make sure source
20040 	 * address selection gets to take this new ipif into account.
20041 	 * No need to hold ill_lock while traversing the ipif list since
20042 	 * we are writer
20043 	 */
20044 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20045 	    tmp_ipif = tmp_ipif->ipif_next) {
20046 		if (((tmp_ipif->ipif_flags &
20047 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20048 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20049 		    (tmp_ipif == ipif))
20050 			continue;
20051 		/* first useable pre-existing interface */
20052 		flush_ire_cache = B_FALSE;
20053 		break;
20054 	}
20055 	if (flush_ire_cache)
20056 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20057 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20058 
20059 	/*
20060 	 * Figure out which way the send-to queue should go.  Only
20061 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20062 	 * should show up here.
20063 	 */
20064 	switch (ill->ill_net_type) {
20065 	case IRE_IF_RESOLVER:
20066 		stq = ill->ill_rq;
20067 		break;
20068 	case IRE_IF_NORESOLVER:
20069 	case IRE_LOOPBACK:
20070 		stq = ill->ill_wq;
20071 		break;
20072 	default:
20073 		return (EINVAL);
20074 	}
20075 
20076 	if (IS_LOOPBACK(ill)) {
20077 		/*
20078 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20079 		 * ipif_lookup_on_name(), but in the case of zones we can have
20080 		 * several loopback addresses on lo0. So all the interfaces with
20081 		 * loopback addresses need to be marked IRE_LOOPBACK.
20082 		 */
20083 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20084 		    htonl(INADDR_LOOPBACK))
20085 			ipif->ipif_ire_type = IRE_LOOPBACK;
20086 		else
20087 			ipif->ipif_ire_type = IRE_LOCAL;
20088 	}
20089 
20090 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20091 		/*
20092 		 * Can't use our source address. Select a different
20093 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20094 		 */
20095 		src_ipif = ipif_select_source(ipif->ipif_ill,
20096 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20097 		if (src_ipif == NULL)
20098 			src_ipif = ipif;	/* Last resort */
20099 		else
20100 			src_ipif_held = B_TRUE;
20101 	} else {
20102 		src_ipif = ipif;
20103 	}
20104 
20105 	/* Create all the IREs associated with this interface */
20106 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20107 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20108 
20109 		/*
20110 		 * If we're on a labeled system then make sure that zone-
20111 		 * private addresses have proper remote host database entries.
20112 		 */
20113 		if (is_system_labeled() &&
20114 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20115 		    !tsol_check_interface_address(ipif))
20116 			return (EINVAL);
20117 
20118 		/* Register the source address for __sin6_src_id */
20119 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20120 		    ipif->ipif_zoneid, ipst);
20121 		if (err != 0) {
20122 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20123 			return (err);
20124 		}
20125 
20126 		/* If the interface address is set, create the local IRE. */
20127 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20128 		    (void *)ipif,
20129 		    ipif->ipif_ire_type,
20130 		    ntohl(ipif->ipif_lcl_addr)));
20131 		*irep++ = ire_create(
20132 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20133 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20134 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20135 		    NULL,				/* no gateway */
20136 		    &ip_loopback_mtuplus,		/* max frag size */
20137 		    NULL,
20138 		    ipif->ipif_rq,			/* recv-from queue */
20139 		    NULL,				/* no send-to queue */
20140 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20141 		    ipif,
20142 		    0,
20143 		    0,
20144 		    0,
20145 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20146 		    RTF_PRIVATE : 0,
20147 		    &ire_uinfo_null,
20148 		    NULL,
20149 		    NULL,
20150 		    ipst);
20151 	} else {
20152 		ip1dbg((
20153 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20154 		    ipif->ipif_ire_type,
20155 		    ntohl(ipif->ipif_lcl_addr),
20156 		    (uint_t)ipif->ipif_flags));
20157 	}
20158 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20159 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20160 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20161 	} else {
20162 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20163 	}
20164 
20165 	subnet_mask = ipif->ipif_net_mask;
20166 
20167 	/*
20168 	 * If mask was not specified, use natural netmask of
20169 	 * interface address. Also, store this mask back into the
20170 	 * ipif struct.
20171 	 */
20172 	if (subnet_mask == 0) {
20173 		subnet_mask = net_mask;
20174 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20175 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20176 		    ipif->ipif_v6subnet);
20177 	}
20178 
20179 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20180 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20181 	    ipif->ipif_subnet != INADDR_ANY) {
20182 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20183 
20184 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20185 			route_mask = IP_HOST_MASK;
20186 		} else {
20187 			route_mask = subnet_mask;
20188 		}
20189 
20190 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20191 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20192 		    (void *)ipif, (void *)ill,
20193 		    ill->ill_net_type,
20194 		    ntohl(ipif->ipif_subnet)));
20195 		*irep++ = ire_create(
20196 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20197 		    (uchar_t *)&route_mask,		/* mask */
20198 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20199 		    NULL,				/* no gateway */
20200 		    &ipif->ipif_mtu,			/* max frag */
20201 		    NULL,
20202 		    NULL,				/* no recv queue */
20203 		    stq,				/* send-to queue */
20204 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20205 		    ipif,
20206 		    0,
20207 		    0,
20208 		    0,
20209 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20210 		    &ire_uinfo_null,
20211 		    NULL,
20212 		    NULL,
20213 		    ipst);
20214 	}
20215 
20216 	/*
20217 	 * Create any necessary broadcast IREs.
20218 	 */
20219 	if (ipif->ipif_flags & IPIF_BROADCAST)
20220 		irep = ipif_create_bcast_ires(ipif, irep);
20221 
20222 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20223 
20224 	/* If an earlier ire_create failed, get out now */
20225 	for (irep1 = irep; irep1 > ire_array; ) {
20226 		irep1--;
20227 		if (*irep1 == NULL) {
20228 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20229 			err = ENOMEM;
20230 			goto bad;
20231 		}
20232 	}
20233 
20234 	/*
20235 	 * Need to atomically check for ip_addr_availablity_check
20236 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20237 	 * from group also.The ill_g_lock is grabbed as reader
20238 	 * just to make sure no new ills or new ipifs are being added
20239 	 * to the system while we are checking the uniqueness of addresses.
20240 	 */
20241 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20242 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20243 	/* Mark it up, and increment counters. */
20244 	ipif->ipif_flags |= IPIF_UP;
20245 	ill->ill_ipif_up_count++;
20246 	err = ip_addr_availability_check(ipif);
20247 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20248 	rw_exit(&ipst->ips_ill_g_lock);
20249 
20250 	if (err != 0) {
20251 		/*
20252 		 * Our address may already be up on the same ill. In this case,
20253 		 * the ARP entry for our ipif replaced the one for the other
20254 		 * ipif. So we don't want to delete it (otherwise the other ipif
20255 		 * would be unable to send packets).
20256 		 * ip_addr_availability_check() identifies this case for us and
20257 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20258 		 * which is the expected error code.
20259 		 */
20260 		if (err == EADDRINUSE) {
20261 			freemsg(ipif->ipif_arp_del_mp);
20262 			ipif->ipif_arp_del_mp = NULL;
20263 			err = EADDRNOTAVAIL;
20264 		}
20265 		ill->ill_ipif_up_count--;
20266 		ipif->ipif_flags &= ~IPIF_UP;
20267 		goto bad;
20268 	}
20269 
20270 	/*
20271 	 * Add in all newly created IREs.  ire_create_bcast() has
20272 	 * already checked for duplicates of the IRE_BROADCAST type.
20273 	 * We want to add before we call ifgrp_insert which wants
20274 	 * to know whether IRE_IF_RESOLVER exists or not.
20275 	 *
20276 	 * NOTE : We refrele the ire though we may branch to "bad"
20277 	 *	  later on where we do ire_delete. This is okay
20278 	 *	  because nobody can delete it as we are running
20279 	 *	  exclusively.
20280 	 */
20281 	for (irep1 = irep; irep1 > ire_array; ) {
20282 		irep1--;
20283 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20284 		/*
20285 		 * refheld by ire_add. refele towards the end of the func
20286 		 */
20287 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20288 	}
20289 	ire_added = B_TRUE;
20290 	/*
20291 	 * Form groups if possible.
20292 	 *
20293 	 * If we are supposed to be in a ill_group with a name, insert it
20294 	 * now as we know that at least one ipif is UP. Otherwise form
20295 	 * nameless groups.
20296 	 *
20297 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20298 	 * this ipif into the appropriate interface group, or create a
20299 	 * new one. If this is already in a nameless group, we try to form
20300 	 * a bigger group looking at other ills potentially sharing this
20301 	 * ipif's prefix.
20302 	 */
20303 	phyi = ill->ill_phyint;
20304 	if (phyi->phyint_groupname_len != 0) {
20305 		ASSERT(phyi->phyint_groupname != NULL);
20306 		if (ill->ill_ipif_up_count == 1) {
20307 			ASSERT(ill->ill_group == NULL);
20308 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20309 			    phyi->phyint_groupname, NULL, B_TRUE);
20310 			if (err != 0) {
20311 				ip1dbg(("ipif_up_done: illgrp allocation "
20312 				    "failed, error %d\n", err));
20313 				goto bad;
20314 			}
20315 		}
20316 		ASSERT(ill->ill_group != NULL);
20317 	}
20318 
20319 	/*
20320 	 * When this is part of group, we need to make sure that
20321 	 * any broadcast ires created because of this ipif coming
20322 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20323 	 * so that we don't receive duplicate broadcast packets.
20324 	 */
20325 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20326 		ipif_renominate_bcast(ipif);
20327 
20328 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20329 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20330 	ipif_saved_irep = ipif_recover_ire(ipif);
20331 
20332 	if (!loopback) {
20333 		/*
20334 		 * If the broadcast address has been set, make sure it makes
20335 		 * sense based on the interface address.
20336 		 * Only match on ill since we are sharing broadcast addresses.
20337 		 */
20338 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20339 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20340 			ire_t	*ire;
20341 
20342 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20343 			    IRE_BROADCAST, ipif, ALL_ZONES,
20344 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20345 
20346 			if (ire == NULL) {
20347 				/*
20348 				 * If there isn't a matching broadcast IRE,
20349 				 * revert to the default for this netmask.
20350 				 */
20351 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20352 				mutex_enter(&ipif->ipif_ill->ill_lock);
20353 				ipif_set_default(ipif);
20354 				mutex_exit(&ipif->ipif_ill->ill_lock);
20355 			} else {
20356 				ire_refrele(ire);
20357 			}
20358 		}
20359 
20360 	}
20361 
20362 	/* This is the first interface on this ill */
20363 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20364 		/*
20365 		 * Need to recover all multicast memberships in the driver.
20366 		 * This had to be deferred until we had attached.
20367 		 */
20368 		ill_recover_multicast(ill);
20369 	}
20370 	/* Join the allhosts multicast address */
20371 	ipif_multicast_up(ipif);
20372 
20373 	if (!loopback) {
20374 		/*
20375 		 * See whether anybody else would benefit from the
20376 		 * new ipif that we added. We call this always rather
20377 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20378 		 * ipif is for the benefit of illgrp_insert (done above)
20379 		 * which does not do source address selection as it does
20380 		 * not want to re-create interface routes that we are
20381 		 * having reference to it here.
20382 		 */
20383 		ill_update_source_selection(ill);
20384 	}
20385 
20386 	for (irep1 = irep; irep1 > ire_array; ) {
20387 		irep1--;
20388 		if (*irep1 != NULL) {
20389 			/* was held in ire_add */
20390 			ire_refrele(*irep1);
20391 		}
20392 	}
20393 
20394 	cnt = ipif_saved_ire_cnt;
20395 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20396 		if (*irep1 != NULL) {
20397 			/* was held in ire_add */
20398 			ire_refrele(*irep1);
20399 		}
20400 	}
20401 
20402 	if (!loopback && ipif->ipif_addr_ready) {
20403 		/* Broadcast an address mask reply. */
20404 		ipif_mask_reply(ipif);
20405 	}
20406 	if (ipif_saved_irep != NULL) {
20407 		kmem_free(ipif_saved_irep,
20408 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20409 	}
20410 	if (src_ipif_held)
20411 		ipif_refrele(src_ipif);
20412 
20413 	/*
20414 	 * This had to be deferred until we had bound.  Tell routing sockets and
20415 	 * others that this interface is up if it looks like the address has
20416 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20417 	 * duplicate address detection to do its thing.
20418 	 */
20419 	if (ipif->ipif_addr_ready) {
20420 		ip_rts_ifmsg(ipif);
20421 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20422 		/* Let SCTP update the status for this ipif */
20423 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20424 	}
20425 	return (0);
20426 
20427 bad:
20428 	ip1dbg(("ipif_up_done: FAILED \n"));
20429 	/*
20430 	 * We don't have to bother removing from ill groups because
20431 	 *
20432 	 * 1) For groups with names, we insert only when the first ipif
20433 	 *    comes up. In that case if it fails, it will not be in any
20434 	 *    group. So, we need not try to remove for that case.
20435 	 *
20436 	 * 2) For groups without names, either we tried to insert ipif_ill
20437 	 *    in a group as singleton or found some other group to become
20438 	 *    a bigger group. For the former, if it fails we don't have
20439 	 *    anything to do as ipif_ill is not in the group and for the
20440 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20441 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20442 	 */
20443 	while (irep > ire_array) {
20444 		irep--;
20445 		if (*irep != NULL) {
20446 			ire_delete(*irep);
20447 			if (ire_added)
20448 				ire_refrele(*irep);
20449 		}
20450 	}
20451 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20452 
20453 	if (ipif_saved_irep != NULL) {
20454 		kmem_free(ipif_saved_irep,
20455 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20456 	}
20457 	if (src_ipif_held)
20458 		ipif_refrele(src_ipif);
20459 
20460 	ipif_arp_down(ipif);
20461 	return (err);
20462 }
20463 
20464 /*
20465  * Turn off the ARP with the ILLF_NOARP flag.
20466  */
20467 static int
20468 ill_arp_off(ill_t *ill)
20469 {
20470 	mblk_t	*arp_off_mp = NULL;
20471 	mblk_t	*arp_on_mp = NULL;
20472 
20473 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20474 
20475 	ASSERT(IAM_WRITER_ILL(ill));
20476 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20477 
20478 	/*
20479 	 * If the on message is still around we've already done
20480 	 * an arp_off without doing an arp_on thus there is no
20481 	 * work needed.
20482 	 */
20483 	if (ill->ill_arp_on_mp != NULL)
20484 		return (0);
20485 
20486 	/*
20487 	 * Allocate an ARP on message (to be saved) and an ARP off message
20488 	 */
20489 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20490 	if (!arp_off_mp)
20491 		return (ENOMEM);
20492 
20493 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20494 	if (!arp_on_mp)
20495 		goto failed;
20496 
20497 	ASSERT(ill->ill_arp_on_mp == NULL);
20498 	ill->ill_arp_on_mp = arp_on_mp;
20499 
20500 	/* Send an AR_INTERFACE_OFF request */
20501 	putnext(ill->ill_rq, arp_off_mp);
20502 	return (0);
20503 failed:
20504 
20505 	if (arp_off_mp)
20506 		freemsg(arp_off_mp);
20507 	return (ENOMEM);
20508 }
20509 
20510 /*
20511  * Turn on ARP by turning off the ILLF_NOARP flag.
20512  */
20513 static int
20514 ill_arp_on(ill_t *ill)
20515 {
20516 	mblk_t	*mp;
20517 
20518 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20519 
20520 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20521 
20522 	ASSERT(IAM_WRITER_ILL(ill));
20523 	/*
20524 	 * Send an AR_INTERFACE_ON request if we have already done
20525 	 * an arp_off (which allocated the message).
20526 	 */
20527 	if (ill->ill_arp_on_mp != NULL) {
20528 		mp = ill->ill_arp_on_mp;
20529 		ill->ill_arp_on_mp = NULL;
20530 		putnext(ill->ill_rq, mp);
20531 	}
20532 	return (0);
20533 }
20534 
20535 /*
20536  * Called after either deleting ill from the group or when setting
20537  * FAILED or STANDBY on the interface.
20538  */
20539 static void
20540 illgrp_reset_schednext(ill_t *ill)
20541 {
20542 	ill_group_t *illgrp;
20543 	ill_t *save_ill;
20544 
20545 	ASSERT(IAM_WRITER_ILL(ill));
20546 	/*
20547 	 * When called from illgrp_delete, ill_group will be non-NULL.
20548 	 * But when called from ip_sioctl_flags, it could be NULL if
20549 	 * somebody is setting FAILED/INACTIVE on some interface which
20550 	 * is not part of a group.
20551 	 */
20552 	illgrp = ill->ill_group;
20553 	if (illgrp == NULL)
20554 		return;
20555 	if (illgrp->illgrp_ill_schednext != ill)
20556 		return;
20557 
20558 	illgrp->illgrp_ill_schednext = NULL;
20559 	save_ill = ill;
20560 	/*
20561 	 * Choose a good ill to be the next one for
20562 	 * outbound traffic. As the flags FAILED/STANDBY is
20563 	 * not yet marked when called from ip_sioctl_flags,
20564 	 * we check for ill separately.
20565 	 */
20566 	for (ill = illgrp->illgrp_ill; ill != NULL;
20567 	    ill = ill->ill_group_next) {
20568 		if ((ill != save_ill) &&
20569 		    !(ill->ill_phyint->phyint_flags &
20570 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20571 			illgrp->illgrp_ill_schednext = ill;
20572 			return;
20573 		}
20574 	}
20575 }
20576 
20577 /*
20578  * Given an ill, find the next ill in the group to be scheduled.
20579  * (This should be called by ip_newroute() before ire_create().)
20580  * The passed in ill may be pulled out of the group, after we have picked
20581  * up a different outgoing ill from the same group. However ire add will
20582  * atomically check this.
20583  */
20584 ill_t *
20585 illgrp_scheduler(ill_t *ill)
20586 {
20587 	ill_t *retill;
20588 	ill_group_t *illgrp;
20589 	int illcnt;
20590 	int i;
20591 	uint64_t flags;
20592 	ip_stack_t	*ipst = ill->ill_ipst;
20593 
20594 	/*
20595 	 * We don't use a lock to check for the ill_group. If this ill
20596 	 * is currently being inserted we may end up just returning this
20597 	 * ill itself. That is ok.
20598 	 */
20599 	if (ill->ill_group == NULL) {
20600 		ill_refhold(ill);
20601 		return (ill);
20602 	}
20603 
20604 	/*
20605 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20606 	 * a set of stable ills. No ill can be added or deleted or change
20607 	 * group while we hold the reader lock.
20608 	 */
20609 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20610 	if ((illgrp = ill->ill_group) == NULL) {
20611 		rw_exit(&ipst->ips_ill_g_lock);
20612 		ill_refhold(ill);
20613 		return (ill);
20614 	}
20615 
20616 	illcnt = illgrp->illgrp_ill_count;
20617 	mutex_enter(&illgrp->illgrp_lock);
20618 	retill = illgrp->illgrp_ill_schednext;
20619 
20620 	if (retill == NULL)
20621 		retill = illgrp->illgrp_ill;
20622 
20623 	/*
20624 	 * We do a circular search beginning at illgrp_ill_schednext
20625 	 * or illgrp_ill. We don't check the flags against the ill lock
20626 	 * since it can change anytime. The ire creation will be atomic
20627 	 * and will fail if the ill is FAILED or OFFLINE.
20628 	 */
20629 	for (i = 0; i < illcnt; i++) {
20630 		flags = retill->ill_phyint->phyint_flags;
20631 
20632 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20633 		    ILL_CAN_LOOKUP(retill)) {
20634 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20635 			ill_refhold(retill);
20636 			break;
20637 		}
20638 		retill = retill->ill_group_next;
20639 		if (retill == NULL)
20640 			retill = illgrp->illgrp_ill;
20641 	}
20642 	mutex_exit(&illgrp->illgrp_lock);
20643 	rw_exit(&ipst->ips_ill_g_lock);
20644 
20645 	return (i == illcnt ? NULL : retill);
20646 }
20647 
20648 /*
20649  * Checks for availbility of a usable source address (if there is one) when the
20650  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20651  * this selection is done regardless of the destination.
20652  */
20653 boolean_t
20654 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20655 {
20656 	uint_t	ifindex;
20657 	ipif_t	*ipif = NULL;
20658 	ill_t	*uill;
20659 	boolean_t isv6;
20660 	ip_stack_t	*ipst = ill->ill_ipst;
20661 
20662 	ASSERT(ill != NULL);
20663 
20664 	isv6 = ill->ill_isv6;
20665 	ifindex = ill->ill_usesrc_ifindex;
20666 	if (ifindex != 0) {
20667 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20668 		    NULL, ipst);
20669 		if (uill == NULL)
20670 			return (NULL);
20671 		mutex_enter(&uill->ill_lock);
20672 		for (ipif = uill->ill_ipif; ipif != NULL;
20673 		    ipif = ipif->ipif_next) {
20674 			if (!IPIF_CAN_LOOKUP(ipif))
20675 				continue;
20676 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20677 				continue;
20678 			if (!(ipif->ipif_flags & IPIF_UP))
20679 				continue;
20680 			if (ipif->ipif_zoneid != zoneid)
20681 				continue;
20682 			if ((isv6 &&
20683 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20684 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20685 				continue;
20686 			mutex_exit(&uill->ill_lock);
20687 			ill_refrele(uill);
20688 			return (B_TRUE);
20689 		}
20690 		mutex_exit(&uill->ill_lock);
20691 		ill_refrele(uill);
20692 	}
20693 	return (B_FALSE);
20694 }
20695 
20696 /*
20697  * Determine the best source address given a destination address and an ill.
20698  * Prefers non-deprecated over deprecated but will return a deprecated
20699  * address if there is no other choice. If there is a usable source address
20700  * on the interface pointed to by ill_usesrc_ifindex then that is given
20701  * first preference.
20702  *
20703  * Returns NULL if there is no suitable source address for the ill.
20704  * This only occurs when there is no valid source address for the ill.
20705  */
20706 ipif_t *
20707 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20708 {
20709 	ipif_t *ipif;
20710 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20711 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20712 	int index = 0;
20713 	boolean_t wrapped = B_FALSE;
20714 	boolean_t same_subnet_only = B_FALSE;
20715 	boolean_t ipif_same_found, ipif_other_found;
20716 	boolean_t specific_found;
20717 	ill_t	*till, *usill = NULL;
20718 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20719 	ip_stack_t	*ipst = ill->ill_ipst;
20720 
20721 	if (ill->ill_usesrc_ifindex != 0) {
20722 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20723 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20724 		if (usill != NULL)
20725 			ill = usill;	/* Select source from usesrc ILL */
20726 		else
20727 			return (NULL);
20728 	}
20729 
20730 	/*
20731 	 * If we're dealing with an unlabeled destination on a labeled system,
20732 	 * make sure that we ignore source addresses that are incompatible with
20733 	 * the destination's default label.  That destination's default label
20734 	 * must dominate the minimum label on the source address.
20735 	 */
20736 	dst_rhtp = NULL;
20737 	if (is_system_labeled()) {
20738 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20739 		if (dst_rhtp == NULL)
20740 			return (NULL);
20741 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20742 			TPC_RELE(dst_rhtp);
20743 			dst_rhtp = NULL;
20744 		}
20745 	}
20746 
20747 	/*
20748 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20749 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20750 	 * After selecting the right ipif, under ill_lock make sure ipif is
20751 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20752 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20753 	 * but not under a lock.
20754 	 */
20755 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20756 
20757 retry:
20758 	till = ill;
20759 	ipif_arr[0] = NULL;
20760 
20761 	if (till->ill_group != NULL)
20762 		till = till->ill_group->illgrp_ill;
20763 
20764 	/*
20765 	 * Choose one good source address from each ill across the group.
20766 	 * If possible choose a source address in the same subnet as
20767 	 * the destination address.
20768 	 *
20769 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20770 	 * This is okay because of the following.
20771 	 *
20772 	 *    If PHYI_FAILED is set and we still have non-deprecated
20773 	 *    addresses, it means the addresses have not yet been
20774 	 *    failed over to a different interface. We potentially
20775 	 *    select them to create IRE_CACHES, which will be later
20776 	 *    flushed when the addresses move over.
20777 	 *
20778 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20779 	 *    addresses, it means either the user has configured them
20780 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20781 	 *    been moved over. For the former, in.mpathd does a failover
20782 	 *    when the interface becomes INACTIVE and hence we should
20783 	 *    not find them. Once INACTIVE is set, we don't allow them
20784 	 *    to create logical interfaces anymore. For the latter, a
20785 	 *    flush will happen when INACTIVE is cleared which will
20786 	 *    flush the IRE_CACHES.
20787 	 *
20788 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20789 	 *    over soon. We potentially select them to create IRE_CACHEs,
20790 	 *    which will be later flushed when the addresses move over.
20791 	 *
20792 	 * NOTE : As ipif_select_source is called to borrow source address
20793 	 * for an ipif that is part of a group, source address selection
20794 	 * will be re-done whenever the group changes i.e either an
20795 	 * insertion/deletion in the group.
20796 	 *
20797 	 * Fill ipif_arr[] with source addresses, using these rules:
20798 	 *
20799 	 *	1. At most one source address from a given ill ends up
20800 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20801 	 *	   associated with a given ill ends up in ipif_arr[].
20802 	 *
20803 	 *	2. If there is at least one non-deprecated ipif in the
20804 	 *	   IPMP group with a source address on the same subnet as
20805 	 *	   our destination, then fill ipif_arr[] only with
20806 	 *	   source addresses on the same subnet as our destination.
20807 	 *	   Note that because of (1), only the first
20808 	 *	   non-deprecated ipif found with a source address
20809 	 *	   matching the destination ends up in ipif_arr[].
20810 	 *
20811 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20812 	 *	   addresses not in the same subnet as our destination.
20813 	 *	   Again, because of (1), only the first off-subnet source
20814 	 *	   address will be chosen.
20815 	 *
20816 	 *	4. If there are no non-deprecated ipifs, then just use
20817 	 *	   the source address associated with the last deprecated
20818 	 *	   one we find that happens to be on the same subnet,
20819 	 *	   otherwise the first one not in the same subnet.
20820 	 */
20821 	specific_found = B_FALSE;
20822 	for (; till != NULL; till = till->ill_group_next) {
20823 		ipif_same_found = B_FALSE;
20824 		ipif_other_found = B_FALSE;
20825 		for (ipif = till->ill_ipif; ipif != NULL;
20826 		    ipif = ipif->ipif_next) {
20827 			if (!IPIF_CAN_LOOKUP(ipif))
20828 				continue;
20829 			/* Always skip NOLOCAL and ANYCAST interfaces */
20830 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20831 				continue;
20832 			if (!(ipif->ipif_flags & IPIF_UP) ||
20833 			    !ipif->ipif_addr_ready)
20834 				continue;
20835 			if (ipif->ipif_zoneid != zoneid &&
20836 			    ipif->ipif_zoneid != ALL_ZONES)
20837 				continue;
20838 			/*
20839 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20840 			 * but are not valid as source addresses.
20841 			 */
20842 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20843 				continue;
20844 
20845 			/*
20846 			 * Check compatibility of local address for
20847 			 * destination's default label if we're on a labeled
20848 			 * system.  Incompatible addresses can't be used at
20849 			 * all.
20850 			 */
20851 			if (dst_rhtp != NULL) {
20852 				boolean_t incompat;
20853 
20854 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20855 				    IPV4_VERSION, B_FALSE);
20856 				if (src_rhtp == NULL)
20857 					continue;
20858 				incompat =
20859 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20860 				    src_rhtp->tpc_tp.tp_doi !=
20861 				    dst_rhtp->tpc_tp.tp_doi ||
20862 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20863 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20864 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20865 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20866 				TPC_RELE(src_rhtp);
20867 				if (incompat)
20868 					continue;
20869 			}
20870 
20871 			/*
20872 			 * We prefer not to use all all-zones addresses, if we
20873 			 * can avoid it, as they pose problems with unlabeled
20874 			 * destinations.
20875 			 */
20876 			if (ipif->ipif_zoneid != ALL_ZONES) {
20877 				if (!specific_found &&
20878 				    (!same_subnet_only ||
20879 				    (ipif->ipif_net_mask & dst) ==
20880 				    ipif->ipif_subnet)) {
20881 					index = 0;
20882 					specific_found = B_TRUE;
20883 					ipif_other_found = B_FALSE;
20884 				}
20885 			} else {
20886 				if (specific_found)
20887 					continue;
20888 			}
20889 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20890 				if (ipif_dep == NULL ||
20891 				    (ipif->ipif_net_mask & dst) ==
20892 				    ipif->ipif_subnet)
20893 					ipif_dep = ipif;
20894 				continue;
20895 			}
20896 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20897 				/* found a source address in the same subnet */
20898 				if (!same_subnet_only) {
20899 					same_subnet_only = B_TRUE;
20900 					index = 0;
20901 				}
20902 				ipif_same_found = B_TRUE;
20903 			} else {
20904 				if (same_subnet_only || ipif_other_found)
20905 					continue;
20906 				ipif_other_found = B_TRUE;
20907 			}
20908 			ipif_arr[index++] = ipif;
20909 			if (index == MAX_IPIF_SELECT_SOURCE) {
20910 				wrapped = B_TRUE;
20911 				index = 0;
20912 			}
20913 			if (ipif_same_found)
20914 				break;
20915 		}
20916 	}
20917 
20918 	if (ipif_arr[0] == NULL) {
20919 		ipif = ipif_dep;
20920 	} else {
20921 		if (wrapped)
20922 			index = MAX_IPIF_SELECT_SOURCE;
20923 		ipif = ipif_arr[ipif_rand(ipst) % index];
20924 		ASSERT(ipif != NULL);
20925 	}
20926 
20927 	if (ipif != NULL) {
20928 		mutex_enter(&ipif->ipif_ill->ill_lock);
20929 		if (!IPIF_CAN_LOOKUP(ipif)) {
20930 			mutex_exit(&ipif->ipif_ill->ill_lock);
20931 			goto retry;
20932 		}
20933 		ipif_refhold_locked(ipif);
20934 		mutex_exit(&ipif->ipif_ill->ill_lock);
20935 	}
20936 
20937 	rw_exit(&ipst->ips_ill_g_lock);
20938 	if (usill != NULL)
20939 		ill_refrele(usill);
20940 	if (dst_rhtp != NULL)
20941 		TPC_RELE(dst_rhtp);
20942 
20943 #ifdef DEBUG
20944 	if (ipif == NULL) {
20945 		char buf1[INET6_ADDRSTRLEN];
20946 
20947 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20948 		    ill->ill_name,
20949 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20950 	} else {
20951 		char buf1[INET6_ADDRSTRLEN];
20952 		char buf2[INET6_ADDRSTRLEN];
20953 
20954 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20955 		    ipif->ipif_ill->ill_name,
20956 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
20957 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
20958 		    buf2, sizeof (buf2))));
20959 	}
20960 #endif /* DEBUG */
20961 	return (ipif);
20962 }
20963 
20964 
20965 /*
20966  * If old_ipif is not NULL, see if ipif was derived from old
20967  * ipif and if so, recreate the interface route by re-doing
20968  * source address selection. This happens when ipif_down ->
20969  * ipif_update_other_ipifs calls us.
20970  *
20971  * If old_ipif is NULL, just redo the source address selection
20972  * if needed. This happens when illgrp_insert or ipif_up_done
20973  * calls us.
20974  */
20975 static void
20976 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
20977 {
20978 	ire_t *ire;
20979 	ire_t *ipif_ire;
20980 	queue_t *stq;
20981 	ipif_t *nipif;
20982 	ill_t *ill;
20983 	boolean_t need_rele = B_FALSE;
20984 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
20985 
20986 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
20987 	ASSERT(IAM_WRITER_IPIF(ipif));
20988 
20989 	ill = ipif->ipif_ill;
20990 	if (!(ipif->ipif_flags &
20991 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
20992 		/*
20993 		 * Can't possibly have borrowed the source
20994 		 * from old_ipif.
20995 		 */
20996 		return;
20997 	}
20998 
20999 	/*
21000 	 * Is there any work to be done? No work if the address
21001 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21002 	 * ipif_select_source() does not borrow addresses from
21003 	 * NOLOCAL and ANYCAST interfaces).
21004 	 */
21005 	if ((old_ipif != NULL) &&
21006 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21007 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21008 	    (old_ipif->ipif_flags &
21009 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21010 		return;
21011 	}
21012 
21013 	/*
21014 	 * Perform the same checks as when creating the
21015 	 * IRE_INTERFACE in ipif_up_done.
21016 	 */
21017 	if (!(ipif->ipif_flags & IPIF_UP))
21018 		return;
21019 
21020 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21021 	    (ipif->ipif_subnet == INADDR_ANY))
21022 		return;
21023 
21024 	ipif_ire = ipif_to_ire(ipif);
21025 	if (ipif_ire == NULL)
21026 		return;
21027 
21028 	/*
21029 	 * We know that ipif uses some other source for its
21030 	 * IRE_INTERFACE. Is it using the source of this
21031 	 * old_ipif?
21032 	 */
21033 	if (old_ipif != NULL &&
21034 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21035 		ire_refrele(ipif_ire);
21036 		return;
21037 	}
21038 	if (ip_debug > 2) {
21039 		/* ip1dbg */
21040 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21041 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21042 	}
21043 
21044 	stq = ipif_ire->ire_stq;
21045 
21046 	/*
21047 	 * Can't use our source address. Select a different
21048 	 * source address for the IRE_INTERFACE.
21049 	 */
21050 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21051 	if (nipif == NULL) {
21052 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21053 		nipif = ipif;
21054 	} else {
21055 		need_rele = B_TRUE;
21056 	}
21057 
21058 	ire = ire_create(
21059 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21060 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21061 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21062 	    NULL,				/* no gateway */
21063 	    &ipif->ipif_mtu,			/* max frag */
21064 	    NULL,				/* no src nce */
21065 	    NULL,				/* no recv from queue */
21066 	    stq,				/* send-to queue */
21067 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21068 	    ipif,
21069 	    0,
21070 	    0,
21071 	    0,
21072 	    0,
21073 	    &ire_uinfo_null,
21074 	    NULL,
21075 	    NULL,
21076 	    ipst);
21077 
21078 	if (ire != NULL) {
21079 		ire_t *ret_ire;
21080 		int error;
21081 
21082 		/*
21083 		 * We don't need ipif_ire anymore. We need to delete
21084 		 * before we add so that ire_add does not detect
21085 		 * duplicates.
21086 		 */
21087 		ire_delete(ipif_ire);
21088 		ret_ire = ire;
21089 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21090 		ASSERT(error == 0);
21091 		ASSERT(ire == ret_ire);
21092 		/* Held in ire_add */
21093 		ire_refrele(ret_ire);
21094 	}
21095 	/*
21096 	 * Either we are falling through from above or could not
21097 	 * allocate a replacement.
21098 	 */
21099 	ire_refrele(ipif_ire);
21100 	if (need_rele)
21101 		ipif_refrele(nipif);
21102 }
21103 
21104 /*
21105  * This old_ipif is going away.
21106  *
21107  * Determine if any other ipif's is using our address as
21108  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21109  * IPIF_DEPRECATED).
21110  * Find the IRE_INTERFACE for such ipifs and recreate them
21111  * to use an different source address following the rules in
21112  * ipif_up_done.
21113  *
21114  * This function takes an illgrp as an argument so that illgrp_delete
21115  * can call this to update source address even after deleting the
21116  * old_ipif->ipif_ill from the ill group.
21117  */
21118 static void
21119 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21120 {
21121 	ipif_t *ipif;
21122 	ill_t *ill;
21123 	char	buf[INET6_ADDRSTRLEN];
21124 
21125 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21126 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21127 
21128 	ill = old_ipif->ipif_ill;
21129 
21130 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21131 	    ill->ill_name,
21132 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21133 	    buf, sizeof (buf))));
21134 	/*
21135 	 * If this part of a group, look at all ills as ipif_select_source
21136 	 * borrows source address across all the ills in the group.
21137 	 */
21138 	if (illgrp != NULL)
21139 		ill = illgrp->illgrp_ill;
21140 
21141 	for (; ill != NULL; ill = ill->ill_group_next) {
21142 		for (ipif = ill->ill_ipif; ipif != NULL;
21143 		    ipif = ipif->ipif_next) {
21144 
21145 			if (ipif == old_ipif)
21146 				continue;
21147 
21148 			ipif_recreate_interface_routes(old_ipif, ipif);
21149 		}
21150 	}
21151 }
21152 
21153 /* ARGSUSED */
21154 int
21155 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21156 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21157 {
21158 	/*
21159 	 * ill_phyint_reinit merged the v4 and v6 into a single
21160 	 * ipsq. Could also have become part of a ipmp group in the
21161 	 * process, and we might not have been able to complete the
21162 	 * operation in ipif_set_values, if we could not become
21163 	 * exclusive.  If so restart it here.
21164 	 */
21165 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21166 }
21167 
21168 /*
21169  * Can operate on either a module or a driver queue.
21170  * Returns an error if not a module queue.
21171  */
21172 /* ARGSUSED */
21173 int
21174 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21175     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21176 {
21177 	queue_t		*q1 = q;
21178 	char 		*cp;
21179 	char		interf_name[LIFNAMSIZ];
21180 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21181 
21182 	if (q->q_next == NULL) {
21183 		ip1dbg((
21184 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21185 		return (EINVAL);
21186 	}
21187 
21188 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21189 		return (EALREADY);
21190 
21191 	do {
21192 		q1 = q1->q_next;
21193 	} while (q1->q_next);
21194 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21195 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21196 
21197 	/*
21198 	 * Here we are not going to delay the ioack until after
21199 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21200 	 * original ioctl message before sending the requests.
21201 	 */
21202 	return (ipif_set_values(q, mp, interf_name, &ppa));
21203 }
21204 
21205 /* ARGSUSED */
21206 int
21207 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21208     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21209 {
21210 	return (ENXIO);
21211 }
21212 
21213 /*
21214  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21215  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21216  * ire_check_and_create_bcast()).
21217  */
21218 static ire_t **
21219 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21220 {
21221 	ipaddr_t addr;
21222 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21223 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21224 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21225 
21226 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21227 
21228 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21229 
21230 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21231 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21232 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21233 
21234 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21235 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21236 
21237 	/*
21238 	 * For backward compatibility, we create net broadcast IREs based on
21239 	 * the old "IP address class system", since some old machines only
21240 	 * respond to these class derived net broadcast.  However, we must not
21241 	 * create these net broadcast IREs if the subnetmask is shorter than
21242 	 * the IP address class based derived netmask.  Otherwise, we may
21243 	 * create a net broadcast address which is the same as an IP address
21244 	 * on the subnet -- and then TCP will refuse to talk to that address.
21245 	 */
21246 	if (netmask < subnetmask) {
21247 		addr = netmask & ipif->ipif_subnet;
21248 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21249 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21250 		    flags);
21251 	}
21252 
21253 	/*
21254 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21255 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21256 	 * created.  Creating these broadcast IREs will only create confusion
21257 	 * as `addr' will be the same as the IP address.
21258 	 */
21259 	if (subnetmask != 0xFFFFFFFF) {
21260 		addr = ipif->ipif_subnet;
21261 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21262 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21263 		    irep, flags);
21264 	}
21265 
21266 	return (irep);
21267 }
21268 
21269 /*
21270  * Broadcast IRE info structure used in the functions below.  Since we
21271  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21272  */
21273 typedef struct bcast_ireinfo {
21274 	uchar_t		bi_type;	/* BCAST_* value from below */
21275 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21276 			bi_needrep:1,	/* do we need to replace it? */
21277 			bi_haverep:1,	/* have we replaced it? */
21278 			bi_pad:5;
21279 	ipaddr_t	bi_addr;	/* IRE address */
21280 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21281 } bcast_ireinfo_t;
21282 
21283 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21284 
21285 /*
21286  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21287  * return B_TRUE if it should immediately be used to recreate the IRE.
21288  */
21289 static boolean_t
21290 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21291 {
21292 	ipaddr_t addr;
21293 
21294 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21295 
21296 	switch (bireinfop->bi_type) {
21297 	case BCAST_NET:
21298 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21299 		if (addr != bireinfop->bi_addr)
21300 			return (B_FALSE);
21301 		break;
21302 	case BCAST_SUBNET:
21303 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21304 			return (B_FALSE);
21305 		break;
21306 	}
21307 
21308 	bireinfop->bi_needrep = 1;
21309 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21310 		if (bireinfop->bi_backup == NULL)
21311 			bireinfop->bi_backup = ipif;
21312 		return (B_FALSE);
21313 	}
21314 	return (B_TRUE);
21315 }
21316 
21317 /*
21318  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21319  * them ala ire_check_and_create_bcast().
21320  */
21321 static ire_t **
21322 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21323 {
21324 	ipaddr_t mask, addr;
21325 
21326 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21327 
21328 	addr = bireinfop->bi_addr;
21329 	irep = ire_create_bcast(ipif, addr, irep);
21330 
21331 	switch (bireinfop->bi_type) {
21332 	case BCAST_NET:
21333 		mask = ip_net_mask(ipif->ipif_subnet);
21334 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21335 		break;
21336 	case BCAST_SUBNET:
21337 		mask = ipif->ipif_net_mask;
21338 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21339 		break;
21340 	}
21341 
21342 	bireinfop->bi_haverep = 1;
21343 	return (irep);
21344 }
21345 
21346 /*
21347  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21348  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21349  * that are going away are still needed.  If so, have ipif_create_bcast()
21350  * recreate them (except for the deprecated case, as explained below).
21351  */
21352 static ire_t **
21353 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21354     ire_t **irep)
21355 {
21356 	int i;
21357 	ipif_t *ipif;
21358 
21359 	ASSERT(!ill->ill_isv6);
21360 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21361 		/*
21362 		 * Skip this ipif if it's (a) the one being taken down, (b)
21363 		 * not in the same zone, or (c) has no valid local address.
21364 		 */
21365 		if (ipif == test_ipif ||
21366 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21367 		    ipif->ipif_subnet == 0 ||
21368 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21369 		    (IPIF_UP|IPIF_BROADCAST))
21370 			continue;
21371 
21372 		/*
21373 		 * For each dying IRE that hasn't yet been replaced, see if
21374 		 * `ipif' needs it and whether the IRE should be recreated on
21375 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21376 		 * will return B_FALSE even if `ipif' needs the IRE on the
21377 		 * hopes that we'll later find a needy non-deprecated ipif.
21378 		 * However, the ipif is recorded in bi_backup for possible
21379 		 * subsequent use by ipif_check_bcast_ires().
21380 		 */
21381 		for (i = 0; i < BCAST_COUNT; i++) {
21382 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21383 				continue;
21384 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21385 				continue;
21386 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21387 		}
21388 
21389 		/*
21390 		 * If we've replaced all of the broadcast IREs that are going
21391 		 * to be taken down, we know we're done.
21392 		 */
21393 		for (i = 0; i < BCAST_COUNT; i++) {
21394 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21395 				break;
21396 		}
21397 		if (i == BCAST_COUNT)
21398 			break;
21399 	}
21400 	return (irep);
21401 }
21402 
21403 /*
21404  * Check if `test_ipif' (which is going away) is associated with any existing
21405  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21406  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21407  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21408  *
21409  * This is necessary because broadcast IREs are shared.  In particular, a
21410  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21411  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21412  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21413  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21414  * same zone, they will share the same set of broadcast IREs.
21415  *
21416  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21417  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21418  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21419  */
21420 static void
21421 ipif_check_bcast_ires(ipif_t *test_ipif)
21422 {
21423 	ill_t		*ill = test_ipif->ipif_ill;
21424 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21425 	ire_t		**irep1, **irep = &ire_array[0];
21426 	uint_t 		i, willdie;
21427 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21428 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21429 
21430 	ASSERT(!test_ipif->ipif_isv6);
21431 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21432 
21433 	/*
21434 	 * No broadcast IREs for the LOOPBACK interface
21435 	 * or others such as point to point and IPIF_NOXMIT.
21436 	 */
21437 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21438 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21439 		return;
21440 
21441 	bzero(bireinfo, sizeof (bireinfo));
21442 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21443 	bireinfo[0].bi_addr = 0;
21444 
21445 	bireinfo[1].bi_type = BCAST_ALLONES;
21446 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21447 
21448 	bireinfo[2].bi_type = BCAST_NET;
21449 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21450 
21451 	if (test_ipif->ipif_net_mask != 0)
21452 		mask = test_ipif->ipif_net_mask;
21453 	bireinfo[3].bi_type = BCAST_SUBNET;
21454 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21455 
21456 	/*
21457 	 * Figure out what (if any) broadcast IREs will die as a result of
21458 	 * `test_ipif' going away.  If none will die, we're done.
21459 	 */
21460 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21461 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21462 		    test_ipif, ALL_ZONES, NULL,
21463 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21464 		if (ire != NULL) {
21465 			willdie++;
21466 			bireinfo[i].bi_willdie = 1;
21467 			ire_refrele(ire);
21468 		}
21469 	}
21470 
21471 	if (willdie == 0)
21472 		return;
21473 
21474 	/*
21475 	 * Walk through all the ipifs that will be affected by the dying IREs,
21476 	 * and recreate the IREs as necessary.
21477 	 */
21478 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21479 
21480 	/*
21481 	 * Scan through the set of broadcast IREs and see if there are any
21482 	 * that we need to replace that have not yet been replaced.  If so,
21483 	 * replace them using the appropriate backup ipif.
21484 	 */
21485 	for (i = 0; i < BCAST_COUNT; i++) {
21486 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21487 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21488 			    &bireinfo[i], irep);
21489 	}
21490 
21491 	/*
21492 	 * If we can't create all of them, don't add any of them.  (Code in
21493 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21494 	 * non-loopback copy and loopback copy for a given address.)
21495 	 */
21496 	for (irep1 = irep; irep1 > ire_array; ) {
21497 		irep1--;
21498 		if (*irep1 == NULL) {
21499 			ip0dbg(("ipif_check_bcast_ires: can't create "
21500 			    "IRE_BROADCAST, memory allocation failure\n"));
21501 			while (irep > ire_array) {
21502 				irep--;
21503 				if (*irep != NULL)
21504 					ire_delete(*irep);
21505 			}
21506 			return;
21507 		}
21508 	}
21509 
21510 	for (irep1 = irep; irep1 > ire_array; ) {
21511 		irep1--;
21512 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21513 			ire_refrele(*irep1);		/* Held in ire_add */
21514 	}
21515 }
21516 
21517 /*
21518  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21519  * from lifr_flags and the name from lifr_name.
21520  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21521  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21522  * Returns EINPROGRESS when mp has been consumed by queueing it on
21523  * ill_pending_mp and the ioctl will complete in ip_rput.
21524  *
21525  * Can operate on either a module or a driver queue.
21526  * Returns an error if not a module queue.
21527  */
21528 /* ARGSUSED */
21529 int
21530 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21531     ip_ioctl_cmd_t *ipip, void *if_req)
21532 {
21533 	ill_t	*ill = q->q_ptr;
21534 	phyint_t *phyi;
21535 	ip_stack_t *ipst;
21536 	struct lifreq *lifr = if_req;
21537 
21538 	ASSERT(ipif != NULL);
21539 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21540 
21541 	if (q->q_next == NULL) {
21542 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21543 		return (EINVAL);
21544 	}
21545 
21546 	/*
21547 	 * If we are not writer on 'q' then this interface exists already
21548 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
21549 	 * so return EALREADY.
21550 	 */
21551 	if (ill != ipif->ipif_ill)
21552 		return (EALREADY);
21553 
21554 	if (ill->ill_name[0] != '\0')
21555 		return (EALREADY);
21556 
21557 	/*
21558 	 * Set all the flags. Allows all kinds of override. Provide some
21559 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21560 	 * unless there is either multicast/broadcast support in the driver
21561 	 * or it is a pt-pt link.
21562 	 */
21563 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21564 		/* Meaningless to IP thus don't allow them to be set. */
21565 		ip1dbg(("ip_setname: EINVAL 1\n"));
21566 		return (EINVAL);
21567 	}
21568 
21569 	/*
21570 	 * If there's another ill already with the requested name, ensure
21571 	 * that it's of the same type.	Otherwise, ill_phyint_reinit() will
21572 	 * fuse together two unrelated ills, which will cause chaos.
21573 	 */
21574 	ipst = ill->ill_ipst;
21575 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
21576 	    lifr->lifr_name, NULL);
21577 	if (phyi != NULL) {
21578 		ill_t *ill_mate = phyi->phyint_illv4;
21579 
21580 		if (ill_mate == NULL)
21581 			ill_mate = phyi->phyint_illv6;
21582 		ASSERT(ill_mate != NULL);
21583 
21584 		if (ill_mate->ill_media->ip_m_mac_type !=
21585 		    ill->ill_media->ip_m_mac_type) {
21586 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
21587 			    "use the same ill name on differing media\n"));
21588 			return (EINVAL);
21589 		}
21590 	}
21591 
21592 	/*
21593 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21594 	 * ill_bcast_addr_length info.
21595 	 */
21596 	if (!ill->ill_needs_attach &&
21597 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21598 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21599 	    ill->ill_bcast_addr_length == 0)) {
21600 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21601 		ip1dbg(("ip_setname: EINVAL 2\n"));
21602 		return (EINVAL);
21603 	}
21604 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21605 	    ((lifr->lifr_flags & IFF_IPV6) ||
21606 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21607 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21608 		ip1dbg(("ip_setname: EINVAL 3\n"));
21609 		return (EINVAL);
21610 	}
21611 	if (lifr->lifr_flags & IFF_UP) {
21612 		/* Can only be set with SIOCSLIFFLAGS */
21613 		ip1dbg(("ip_setname: EINVAL 4\n"));
21614 		return (EINVAL);
21615 	}
21616 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21617 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21618 		ip1dbg(("ip_setname: EINVAL 5\n"));
21619 		return (EINVAL);
21620 	}
21621 	/*
21622 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21623 	 */
21624 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21625 	    !(lifr->lifr_flags & IFF_IPV6) &&
21626 	    !(ipif->ipif_isv6)) {
21627 		ip1dbg(("ip_setname: EINVAL 6\n"));
21628 		return (EINVAL);
21629 	}
21630 
21631 	/*
21632 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21633 	 * we have all the flags here. So, we assign rather than we OR.
21634 	 * We can't OR the flags here because we don't want to set
21635 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21636 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21637 	 * on lifr_flags value here.
21638 	 */
21639 	/*
21640 	 * This ill has not been inserted into the global list.
21641 	 * So we are still single threaded and don't need any lock
21642 	 */
21643 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
21644 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21645 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21646 
21647 	/* We started off as V4. */
21648 	if (ill->ill_flags & ILLF_IPV6) {
21649 		ill->ill_phyint->phyint_illv6 = ill;
21650 		ill->ill_phyint->phyint_illv4 = NULL;
21651 	}
21652 
21653 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
21654 }
21655 
21656 /* ARGSUSED */
21657 int
21658 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21659     ip_ioctl_cmd_t *ipip, void *if_req)
21660 {
21661 	/*
21662 	 * ill_phyint_reinit merged the v4 and v6 into a single
21663 	 * ipsq. Could also have become part of a ipmp group in the
21664 	 * process, and we might not have been able to complete the
21665 	 * slifname in ipif_set_values, if we could not become
21666 	 * exclusive.  If so restart it here
21667 	 */
21668 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21669 }
21670 
21671 /*
21672  * Return a pointer to the ipif which matches the index, IP version type and
21673  * zoneid.
21674  */
21675 ipif_t *
21676 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21677     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21678 {
21679 	ill_t	*ill;
21680 	ipif_t	*ipif = NULL;
21681 
21682 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21683 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21684 
21685 	if (err != NULL)
21686 		*err = 0;
21687 
21688 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
21689 	if (ill != NULL) {
21690 		mutex_enter(&ill->ill_lock);
21691 		for (ipif = ill->ill_ipif; ipif != NULL;
21692 		    ipif = ipif->ipif_next) {
21693 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
21694 			    zoneid == ipif->ipif_zoneid ||
21695 			    ipif->ipif_zoneid == ALL_ZONES)) {
21696 				ipif_refhold_locked(ipif);
21697 				break;
21698 			}
21699 		}
21700 		mutex_exit(&ill->ill_lock);
21701 		ill_refrele(ill);
21702 		if (ipif == NULL && err != NULL)
21703 			*err = ENXIO;
21704 	}
21705 	return (ipif);
21706 }
21707 
21708 typedef struct conn_change_s {
21709 	uint_t cc_old_ifindex;
21710 	uint_t cc_new_ifindex;
21711 } conn_change_t;
21712 
21713 /*
21714  * ipcl_walk function for changing interface index.
21715  */
21716 static void
21717 conn_change_ifindex(conn_t *connp, caddr_t arg)
21718 {
21719 	conn_change_t *connc;
21720 	uint_t old_ifindex;
21721 	uint_t new_ifindex;
21722 	int i;
21723 	ilg_t *ilg;
21724 
21725 	connc = (conn_change_t *)arg;
21726 	old_ifindex = connc->cc_old_ifindex;
21727 	new_ifindex = connc->cc_new_ifindex;
21728 
21729 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21730 		connp->conn_orig_bound_ifindex = new_ifindex;
21731 
21732 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21733 		connp->conn_orig_multicast_ifindex = new_ifindex;
21734 
21735 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21736 		ilg = &connp->conn_ilg[i];
21737 		if (ilg->ilg_orig_ifindex == old_ifindex)
21738 			ilg->ilg_orig_ifindex = new_ifindex;
21739 	}
21740 }
21741 
21742 /*
21743  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21744  * to new_index if it matches the old_index.
21745  *
21746  * Failovers typically happen within a group of ills. But somebody
21747  * can remove an ill from the group after a failover happened. If
21748  * we are setting the ifindex after this, we potentially need to
21749  * look at all the ills rather than just the ones in the group.
21750  * We cut down the work by looking at matching ill_net_types
21751  * and ill_types as we could not possibly grouped them together.
21752  */
21753 static void
21754 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21755 {
21756 	ill_t *ill;
21757 	ipif_t *ipif;
21758 	uint_t old_ifindex;
21759 	uint_t new_ifindex;
21760 	ilm_t *ilm;
21761 	ill_walk_context_t ctx;
21762 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21763 
21764 	old_ifindex = connc->cc_old_ifindex;
21765 	new_ifindex = connc->cc_new_ifindex;
21766 
21767 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21768 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21769 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21770 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21771 		    (ill_orig->ill_type != ill->ill_type)) {
21772 			continue;
21773 		}
21774 		for (ipif = ill->ill_ipif; ipif != NULL;
21775 		    ipif = ipif->ipif_next) {
21776 			if (ipif->ipif_orig_ifindex == old_ifindex)
21777 				ipif->ipif_orig_ifindex = new_ifindex;
21778 		}
21779 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21780 			if (ilm->ilm_orig_ifindex == old_ifindex)
21781 				ilm->ilm_orig_ifindex = new_ifindex;
21782 		}
21783 	}
21784 	rw_exit(&ipst->ips_ill_g_lock);
21785 }
21786 
21787 /*
21788  * We first need to ensure that the new index is unique, and
21789  * then carry the change across both v4 and v6 ill representation
21790  * of the physical interface.
21791  */
21792 /* ARGSUSED */
21793 int
21794 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21795     ip_ioctl_cmd_t *ipip, void *ifreq)
21796 {
21797 	ill_t		*ill;
21798 	ill_t		*ill_other;
21799 	phyint_t	*phyi;
21800 	int		old_index;
21801 	conn_change_t	connc;
21802 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21803 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21804 	uint_t	index;
21805 	ill_t	*ill_v4;
21806 	ill_t	*ill_v6;
21807 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21808 
21809 	if (ipip->ipi_cmd_type == IF_CMD)
21810 		index = ifr->ifr_index;
21811 	else
21812 		index = lifr->lifr_index;
21813 
21814 	/*
21815 	 * Only allow on physical interface. Also, index zero is illegal.
21816 	 *
21817 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21818 	 *
21819 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21820 	 *    implies a possible failback might have to happen. As failback
21821 	 *    depends on the old index, we should fail setting the index.
21822 	 *
21823 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21824 	 *    any addresses or multicast memberships are failed over to
21825 	 *    a non-STANDBY interface. As failback depends on the old
21826 	 *    index, we should fail setting the index for this case also.
21827 	 *
21828 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21829 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21830 	 */
21831 	ill = ipif->ipif_ill;
21832 	phyi = ill->ill_phyint;
21833 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21834 	    ipif->ipif_id != 0 || index == 0) {
21835 		return (EINVAL);
21836 	}
21837 	old_index = phyi->phyint_ifindex;
21838 
21839 	/* If the index is not changing, no work to do */
21840 	if (old_index == index)
21841 		return (0);
21842 
21843 	/*
21844 	 * Use ill_lookup_on_ifindex to determine if the
21845 	 * new index is unused and if so allow the change.
21846 	 */
21847 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21848 	    ipst);
21849 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21850 	    ipst);
21851 	if (ill_v6 != NULL || ill_v4 != NULL) {
21852 		if (ill_v4 != NULL)
21853 			ill_refrele(ill_v4);
21854 		if (ill_v6 != NULL)
21855 			ill_refrele(ill_v6);
21856 		return (EBUSY);
21857 	}
21858 
21859 	/*
21860 	 * The new index is unused. Set it in the phyint.
21861 	 * Locate the other ill so that we can send a routing
21862 	 * sockets message.
21863 	 */
21864 	if (ill->ill_isv6) {
21865 		ill_other = phyi->phyint_illv4;
21866 	} else {
21867 		ill_other = phyi->phyint_illv6;
21868 	}
21869 
21870 	phyi->phyint_ifindex = index;
21871 
21872 	/* Update SCTP's ILL list */
21873 	sctp_ill_reindex(ill, old_index);
21874 
21875 	connc.cc_old_ifindex = old_index;
21876 	connc.cc_new_ifindex = index;
21877 	ip_change_ifindex(ill, &connc);
21878 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21879 
21880 	/* Send the routing sockets message */
21881 	ip_rts_ifmsg(ipif);
21882 	if (ill_other != NULL)
21883 		ip_rts_ifmsg(ill_other->ill_ipif);
21884 
21885 	return (0);
21886 }
21887 
21888 /* ARGSUSED */
21889 int
21890 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21891     ip_ioctl_cmd_t *ipip, void *ifreq)
21892 {
21893 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21894 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21895 
21896 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21897 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21898 	/* Get the interface index */
21899 	if (ipip->ipi_cmd_type == IF_CMD) {
21900 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21901 	} else {
21902 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21903 	}
21904 	return (0);
21905 }
21906 
21907 /* ARGSUSED */
21908 int
21909 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21910     ip_ioctl_cmd_t *ipip, void *ifreq)
21911 {
21912 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21913 
21914 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21915 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21916 	/* Get the interface zone */
21917 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21918 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21919 	return (0);
21920 }
21921 
21922 /*
21923  * Set the zoneid of an interface.
21924  */
21925 /* ARGSUSED */
21926 int
21927 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21928     ip_ioctl_cmd_t *ipip, void *ifreq)
21929 {
21930 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21931 	int err = 0;
21932 	boolean_t need_up = B_FALSE;
21933 	zone_t *zptr;
21934 	zone_status_t status;
21935 	zoneid_t zoneid;
21936 
21937 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21938 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21939 		if (!is_system_labeled())
21940 			return (ENOTSUP);
21941 		zoneid = GLOBAL_ZONEID;
21942 	}
21943 
21944 	/* cannot assign instance zero to a non-global zone */
21945 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
21946 		return (ENOTSUP);
21947 
21948 	/*
21949 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21950 	 * the event of a race with the zone shutdown processing, since IP
21951 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
21952 	 * interface will be cleaned up even if the zone is shut down
21953 	 * immediately after the status check. If the interface can't be brought
21954 	 * down right away, and the zone is shut down before the restart
21955 	 * function is called, we resolve the possible races by rechecking the
21956 	 * zone status in the restart function.
21957 	 */
21958 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
21959 		return (EINVAL);
21960 	status = zone_status_get(zptr);
21961 	zone_rele(zptr);
21962 
21963 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
21964 		return (EINVAL);
21965 
21966 	if (ipif->ipif_flags & IPIF_UP) {
21967 		/*
21968 		 * If the interface is already marked up,
21969 		 * we call ipif_down which will take care
21970 		 * of ditching any IREs that have been set
21971 		 * up based on the old interface address.
21972 		 */
21973 		err = ipif_logical_down(ipif, q, mp);
21974 		if (err == EINPROGRESS)
21975 			return (err);
21976 		ipif_down_tail(ipif);
21977 		need_up = B_TRUE;
21978 	}
21979 
21980 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
21981 	return (err);
21982 }
21983 
21984 static int
21985 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
21986     queue_t *q, mblk_t *mp, boolean_t need_up)
21987 {
21988 	int	err = 0;
21989 	ip_stack_t	*ipst;
21990 
21991 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
21992 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21993 
21994 	if (CONN_Q(q))
21995 		ipst = CONNQ_TO_IPST(q);
21996 	else
21997 		ipst = ILLQ_TO_IPST(q);
21998 
21999 	/*
22000 	 * For exclusive stacks we don't allow a different zoneid than
22001 	 * global.
22002 	 */
22003 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22004 	    zoneid != GLOBAL_ZONEID)
22005 		return (EINVAL);
22006 
22007 	/* Set the new zone id. */
22008 	ipif->ipif_zoneid = zoneid;
22009 
22010 	/* Update sctp list */
22011 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22012 
22013 	if (need_up) {
22014 		/*
22015 		 * Now bring the interface back up.  If this
22016 		 * is the only IPIF for the ILL, ipif_up
22017 		 * will have to re-bind to the device, so
22018 		 * we may get back EINPROGRESS, in which
22019 		 * case, this IOCTL will get completed in
22020 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22021 		 */
22022 		err = ipif_up(ipif, q, mp);
22023 	}
22024 	return (err);
22025 }
22026 
22027 /* ARGSUSED */
22028 int
22029 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22030     ip_ioctl_cmd_t *ipip, void *if_req)
22031 {
22032 	struct lifreq *lifr = (struct lifreq *)if_req;
22033 	zoneid_t zoneid;
22034 	zone_t *zptr;
22035 	zone_status_t status;
22036 
22037 	ASSERT(ipif->ipif_id != 0);
22038 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22039 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22040 		zoneid = GLOBAL_ZONEID;
22041 
22042 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22043 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22044 
22045 	/*
22046 	 * We recheck the zone status to resolve the following race condition:
22047 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22048 	 * 2) hme0:1 is up and can't be brought down right away;
22049 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22050 	 * 3) zone "myzone" is halted; the zone status switches to
22051 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22052 	 * the interfaces to remove - hme0:1 is not returned because it's not
22053 	 * yet in "myzone", so it won't be removed;
22054 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22055 	 * status check here, we would have hme0:1 in "myzone" after it's been
22056 	 * destroyed.
22057 	 * Note that if the status check fails, we need to bring the interface
22058 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22059 	 * ipif_up_done[_v6]().
22060 	 */
22061 	status = ZONE_IS_UNINITIALIZED;
22062 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22063 		status = zone_status_get(zptr);
22064 		zone_rele(zptr);
22065 	}
22066 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22067 		if (ipif->ipif_isv6) {
22068 			(void) ipif_up_done_v6(ipif);
22069 		} else {
22070 			(void) ipif_up_done(ipif);
22071 		}
22072 		return (EINVAL);
22073 	}
22074 
22075 	ipif_down_tail(ipif);
22076 
22077 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22078 	    B_TRUE));
22079 }
22080 
22081 /* ARGSUSED */
22082 int
22083 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22084 	ip_ioctl_cmd_t *ipip, void *ifreq)
22085 {
22086 	struct lifreq	*lifr = ifreq;
22087 
22088 	ASSERT(q->q_next == NULL);
22089 	ASSERT(CONN_Q(q));
22090 
22091 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22092 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22093 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22094 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22095 
22096 	return (0);
22097 }
22098 
22099 /* Find the previous ILL in this usesrc group */
22100 static ill_t *
22101 ill_prev_usesrc(ill_t *uill)
22102 {
22103 	ill_t *ill;
22104 
22105 	for (ill = uill->ill_usesrc_grp_next;
22106 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22107 	    ill = ill->ill_usesrc_grp_next)
22108 		/* do nothing */;
22109 	return (ill);
22110 }
22111 
22112 /*
22113  * Release all members of the usesrc group. This routine is called
22114  * from ill_delete when the interface being unplumbed is the
22115  * group head.
22116  */
22117 static void
22118 ill_disband_usesrc_group(ill_t *uill)
22119 {
22120 	ill_t *next_ill, *tmp_ill;
22121 	ip_stack_t	*ipst = uill->ill_ipst;
22122 
22123 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22124 	next_ill = uill->ill_usesrc_grp_next;
22125 
22126 	do {
22127 		ASSERT(next_ill != NULL);
22128 		tmp_ill = next_ill->ill_usesrc_grp_next;
22129 		ASSERT(tmp_ill != NULL);
22130 		next_ill->ill_usesrc_grp_next = NULL;
22131 		next_ill->ill_usesrc_ifindex = 0;
22132 		next_ill = tmp_ill;
22133 	} while (next_ill->ill_usesrc_ifindex != 0);
22134 	uill->ill_usesrc_grp_next = NULL;
22135 }
22136 
22137 /*
22138  * Remove the client usesrc ILL from the list and relink to a new list
22139  */
22140 int
22141 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22142 {
22143 	ill_t *ill, *tmp_ill;
22144 	ip_stack_t	*ipst = ucill->ill_ipst;
22145 
22146 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22147 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22148 
22149 	/*
22150 	 * Check if the usesrc client ILL passed in is not already
22151 	 * in use as a usesrc ILL i.e one whose source address is
22152 	 * in use OR a usesrc ILL is not already in use as a usesrc
22153 	 * client ILL
22154 	 */
22155 	if ((ucill->ill_usesrc_ifindex == 0) ||
22156 	    (uill->ill_usesrc_ifindex != 0)) {
22157 		return (-1);
22158 	}
22159 
22160 	ill = ill_prev_usesrc(ucill);
22161 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22162 
22163 	/* Remove from the current list */
22164 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22165 		/* Only two elements in the list */
22166 		ASSERT(ill->ill_usesrc_ifindex == 0);
22167 		ill->ill_usesrc_grp_next = NULL;
22168 	} else {
22169 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22170 	}
22171 
22172 	if (ifindex == 0) {
22173 		ucill->ill_usesrc_ifindex = 0;
22174 		ucill->ill_usesrc_grp_next = NULL;
22175 		return (0);
22176 	}
22177 
22178 	ucill->ill_usesrc_ifindex = ifindex;
22179 	tmp_ill = uill->ill_usesrc_grp_next;
22180 	uill->ill_usesrc_grp_next = ucill;
22181 	ucill->ill_usesrc_grp_next =
22182 	    (tmp_ill != NULL) ? tmp_ill : uill;
22183 	return (0);
22184 }
22185 
22186 /*
22187  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22188  * ip.c for locking details.
22189  */
22190 /* ARGSUSED */
22191 int
22192 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22193     ip_ioctl_cmd_t *ipip, void *ifreq)
22194 {
22195 	struct lifreq *lifr = (struct lifreq *)ifreq;
22196 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22197 	    ill_flag_changed = B_FALSE;
22198 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22199 	int err = 0, ret;
22200 	uint_t ifindex;
22201 	phyint_t *us_phyint, *us_cli_phyint;
22202 	ipsq_t *ipsq = NULL;
22203 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22204 
22205 	ASSERT(IAM_WRITER_IPIF(ipif));
22206 	ASSERT(q->q_next == NULL);
22207 	ASSERT(CONN_Q(q));
22208 
22209 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22210 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22211 
22212 	ASSERT(us_cli_phyint != NULL);
22213 
22214 	/*
22215 	 * If the client ILL is being used for IPMP, abort.
22216 	 * Note, this can be done before ipsq_try_enter since we are already
22217 	 * exclusive on this ILL
22218 	 */
22219 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22220 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22221 		return (EINVAL);
22222 	}
22223 
22224 	ifindex = lifr->lifr_index;
22225 	if (ifindex == 0) {
22226 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22227 			/* non usesrc group interface, nothing to reset */
22228 			return (0);
22229 		}
22230 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22231 		/* valid reset request */
22232 		reset_flg = B_TRUE;
22233 	}
22234 
22235 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22236 	    ip_process_ioctl, &err, ipst);
22237 
22238 	if (usesrc_ill == NULL) {
22239 		return (err);
22240 	}
22241 
22242 	/*
22243 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22244 	 * group nor can either of the interfaces be used for standy. So
22245 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22246 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22247 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22248 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22249 	 * the usesrc_cli_ill
22250 	 */
22251 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22252 	    NEW_OP, B_TRUE);
22253 	if (ipsq == NULL) {
22254 		err = EINPROGRESS;
22255 		/* Operation enqueued on the ipsq of the usesrc ILL */
22256 		goto done;
22257 	}
22258 
22259 	/* Check if the usesrc_ill is used for IPMP */
22260 	us_phyint = usesrc_ill->ill_phyint;
22261 	if ((us_phyint->phyint_groupname != NULL) ||
22262 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22263 		err = EINVAL;
22264 		goto done;
22265 	}
22266 
22267 	/*
22268 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22269 	 * already a client then return EINVAL
22270 	 */
22271 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22272 		err = EINVAL;
22273 		goto done;
22274 	}
22275 
22276 	/*
22277 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22278 	 * be then this is a duplicate operation.
22279 	 */
22280 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22281 		err = 0;
22282 		goto done;
22283 	}
22284 
22285 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22286 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22287 	    usesrc_ill->ill_isv6));
22288 
22289 	/*
22290 	 * The next step ensures that no new ires will be created referencing
22291 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22292 	 * we go through an ire walk deleting all ire caches that reference
22293 	 * the client ill. New ires referencing the client ill that are added
22294 	 * to the ire table before the ILL_CHANGING flag is set, will be
22295 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22296 	 * the client ill while the ILL_CHANGING flag is set will be failed
22297 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22298 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22299 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22300 	 * belong to the same usesrc group.
22301 	 */
22302 	mutex_enter(&usesrc_cli_ill->ill_lock);
22303 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22304 	mutex_exit(&usesrc_cli_ill->ill_lock);
22305 	ill_flag_changed = B_TRUE;
22306 
22307 	if (ipif->ipif_isv6)
22308 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22309 		    ALL_ZONES, ipst);
22310 	else
22311 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22312 		    ALL_ZONES, ipst);
22313 
22314 	/*
22315 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22316 	 * and the ill_usesrc_ifindex fields
22317 	 */
22318 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22319 
22320 	if (reset_flg) {
22321 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22322 		if (ret != 0) {
22323 			err = EINVAL;
22324 		}
22325 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22326 		goto done;
22327 	}
22328 
22329 	/*
22330 	 * Four possibilities to consider:
22331 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22332 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22333 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22334 	 * 4. Both are part of their respective usesrc groups
22335 	 */
22336 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22337 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22338 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22339 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22340 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22341 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22342 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22343 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22344 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22345 		/* Insert at head of list */
22346 		usesrc_cli_ill->ill_usesrc_grp_next =
22347 		    usesrc_ill->ill_usesrc_grp_next;
22348 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22349 	} else {
22350 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22351 		    ifindex);
22352 		if (ret != 0)
22353 			err = EINVAL;
22354 	}
22355 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22356 
22357 done:
22358 	if (ill_flag_changed) {
22359 		mutex_enter(&usesrc_cli_ill->ill_lock);
22360 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22361 		mutex_exit(&usesrc_cli_ill->ill_lock);
22362 	}
22363 	if (ipsq != NULL)
22364 		ipsq_exit(ipsq);
22365 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22366 	ill_refrele(usesrc_ill);
22367 	return (err);
22368 }
22369 
22370 /*
22371  * comparison function used by avl.
22372  */
22373 static int
22374 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22375 {
22376 
22377 	uint_t index;
22378 
22379 	ASSERT(phyip != NULL && index_ptr != NULL);
22380 
22381 	index = *((uint_t *)index_ptr);
22382 	/*
22383 	 * let the phyint with the lowest index be on top.
22384 	 */
22385 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22386 		return (1);
22387 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22388 		return (-1);
22389 	return (0);
22390 }
22391 
22392 /*
22393  * comparison function used by avl.
22394  */
22395 static int
22396 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22397 {
22398 	ill_t *ill;
22399 	int res = 0;
22400 
22401 	ASSERT(phyip != NULL && name_ptr != NULL);
22402 
22403 	if (((phyint_t *)phyip)->phyint_illv4)
22404 		ill = ((phyint_t *)phyip)->phyint_illv4;
22405 	else
22406 		ill = ((phyint_t *)phyip)->phyint_illv6;
22407 	ASSERT(ill != NULL);
22408 
22409 	res = strcmp(ill->ill_name, (char *)name_ptr);
22410 	if (res > 0)
22411 		return (1);
22412 	else if (res < 0)
22413 		return (-1);
22414 	return (0);
22415 }
22416 /*
22417  * This function is called from ill_delete when the ill is being
22418  * unplumbed. We remove the reference from the phyint and we also
22419  * free the phyint when there are no more references to it.
22420  */
22421 static void
22422 ill_phyint_free(ill_t *ill)
22423 {
22424 	phyint_t *phyi;
22425 	phyint_t *next_phyint;
22426 	ipsq_t *cur_ipsq;
22427 	ip_stack_t	*ipst = ill->ill_ipst;
22428 
22429 	ASSERT(ill->ill_phyint != NULL);
22430 
22431 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22432 	phyi = ill->ill_phyint;
22433 	ill->ill_phyint = NULL;
22434 	/*
22435 	 * ill_init allocates a phyint always to store the copy
22436 	 * of flags relevant to phyint. At that point in time, we could
22437 	 * not assign the name and hence phyint_illv4/v6 could not be
22438 	 * initialized. Later in ipif_set_values, we assign the name to
22439 	 * the ill, at which point in time we assign phyint_illv4/v6.
22440 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22441 	 */
22442 	if (ill->ill_flags & ILLF_IPV6) {
22443 		phyi->phyint_illv6 = NULL;
22444 	} else {
22445 		phyi->phyint_illv4 = NULL;
22446 	}
22447 	/*
22448 	 * ipif_down removes it from the group when the last ipif goes
22449 	 * down.
22450 	 */
22451 	ASSERT(ill->ill_group == NULL);
22452 
22453 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22454 		return;
22455 
22456 	/*
22457 	 * Make sure this phyint was put in the list.
22458 	 */
22459 	if (phyi->phyint_ifindex > 0) {
22460 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22461 		    phyi);
22462 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22463 		    phyi);
22464 	}
22465 	/*
22466 	 * remove phyint from the ipsq list.
22467 	 */
22468 	cur_ipsq = phyi->phyint_ipsq;
22469 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22470 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22471 	} else {
22472 		next_phyint = cur_ipsq->ipsq_phyint_list;
22473 		while (next_phyint != NULL) {
22474 			if (next_phyint->phyint_ipsq_next == phyi) {
22475 				next_phyint->phyint_ipsq_next =
22476 				    phyi->phyint_ipsq_next;
22477 				break;
22478 			}
22479 			next_phyint = next_phyint->phyint_ipsq_next;
22480 		}
22481 		ASSERT(next_phyint != NULL);
22482 	}
22483 	IPSQ_DEC_REF(cur_ipsq, ipst);
22484 
22485 	if (phyi->phyint_groupname_len != 0) {
22486 		ASSERT(phyi->phyint_groupname != NULL);
22487 		mi_free(phyi->phyint_groupname);
22488 	}
22489 	mi_free(phyi);
22490 }
22491 
22492 /*
22493  * Attach the ill to the phyint structure which can be shared by both
22494  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22495  * function is called from ipif_set_values and ill_lookup_on_name (for
22496  * loopback) where we know the name of the ill. We lookup the ill and if
22497  * there is one present already with the name use that phyint. Otherwise
22498  * reuse the one allocated by ill_init.
22499  */
22500 static void
22501 ill_phyint_reinit(ill_t *ill)
22502 {
22503 	boolean_t isv6 = ill->ill_isv6;
22504 	phyint_t *phyi_old;
22505 	phyint_t *phyi;
22506 	avl_index_t where = 0;
22507 	ill_t	*ill_other = NULL;
22508 	ipsq_t	*ipsq;
22509 	ip_stack_t	*ipst = ill->ill_ipst;
22510 
22511 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22512 
22513 	phyi_old = ill->ill_phyint;
22514 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22515 	    phyi_old->phyint_illv6 == NULL));
22516 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22517 	    phyi_old->phyint_illv4 == NULL));
22518 	ASSERT(phyi_old->phyint_ifindex == 0);
22519 
22520 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22521 	    ill->ill_name, &where);
22522 
22523 	/*
22524 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22525 	 *    the global list of ills. So no other thread could have located
22526 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22527 	 * 2. Now locate the other protocol instance of this ill.
22528 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22529 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22530 	 *    of neither ill can change.
22531 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22532 	 *    other ill.
22533 	 * 5. Release all locks.
22534 	 */
22535 
22536 	/*
22537 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22538 	 * we are initializing IPv4.
22539 	 */
22540 	if (phyi != NULL) {
22541 		ill_other = (isv6) ? phyi->phyint_illv4 :
22542 		    phyi->phyint_illv6;
22543 		ASSERT(ill_other->ill_phyint != NULL);
22544 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22545 		    (!isv6 && ill_other->ill_isv6));
22546 		GRAB_ILL_LOCKS(ill, ill_other);
22547 		/*
22548 		 * We are potentially throwing away phyint_flags which
22549 		 * could be different from the one that we obtain from
22550 		 * ill_other->ill_phyint. But it is okay as we are assuming
22551 		 * that the state maintained within IP is correct.
22552 		 */
22553 		mutex_enter(&phyi->phyint_lock);
22554 		if (isv6) {
22555 			ASSERT(phyi->phyint_illv6 == NULL);
22556 			phyi->phyint_illv6 = ill;
22557 		} else {
22558 			ASSERT(phyi->phyint_illv4 == NULL);
22559 			phyi->phyint_illv4 = ill;
22560 		}
22561 		/*
22562 		 * This is a new ill, currently undergoing SLIFNAME
22563 		 * So we could not have joined an IPMP group until now.
22564 		 */
22565 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22566 		    phyi_old->phyint_groupname == NULL);
22567 
22568 		/*
22569 		 * This phyi_old is going away. Decref ipsq_refs and
22570 		 * assert it is zero. The ipsq itself will be freed in
22571 		 * ipsq_exit
22572 		 */
22573 		ipsq = phyi_old->phyint_ipsq;
22574 		IPSQ_DEC_REF(ipsq, ipst);
22575 		ASSERT(ipsq->ipsq_refs == 0);
22576 		/* Get the singleton phyint out of the ipsq list */
22577 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22578 		ipsq->ipsq_phyint_list = NULL;
22579 		phyi_old->phyint_illv4 = NULL;
22580 		phyi_old->phyint_illv6 = NULL;
22581 		mi_free(phyi_old);
22582 	} else {
22583 		mutex_enter(&ill->ill_lock);
22584 		/*
22585 		 * We don't need to acquire any lock, since
22586 		 * the ill is not yet visible globally  and we
22587 		 * have not yet released the ill_g_lock.
22588 		 */
22589 		phyi = phyi_old;
22590 		mutex_enter(&phyi->phyint_lock);
22591 		/* XXX We need a recovery strategy here. */
22592 		if (!phyint_assign_ifindex(phyi, ipst))
22593 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22594 
22595 		/* No IPMP group yet, thus the hook uses the ifindex */
22596 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22597 
22598 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22599 		    (void *)phyi, where);
22600 
22601 		(void) avl_find(&ipst->ips_phyint_g_list->
22602 		    phyint_list_avl_by_index,
22603 		    &phyi->phyint_ifindex, &where);
22604 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22605 		    (void *)phyi, where);
22606 	}
22607 
22608 	/*
22609 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22610 	 * pending mp is not affected because that is per ill basis.
22611 	 */
22612 	ill->ill_phyint = phyi;
22613 
22614 	/*
22615 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22616 	 * We do this here as when the first ipif was allocated,
22617 	 * ipif_allocate does not know the right interface index.
22618 	 */
22619 
22620 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22621 	/*
22622 	 * Now that the phyint's ifindex has been assigned, complete the
22623 	 * remaining
22624 	 */
22625 
22626 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22627 	if (ill->ill_isv6) {
22628 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22629 		    ill->ill_phyint->phyint_ifindex;
22630 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22631 	} else {
22632 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22633 	}
22634 
22635 	/*
22636 	 * Generate an event within the hooks framework to indicate that
22637 	 * a new interface has just been added to IP.  For this event to
22638 	 * be generated, the network interface must, at least, have an
22639 	 * ifindex assigned to it.
22640 	 *
22641 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22642 	 * that the ordering of delivered events to listeners matches the
22643 	 * order of them in the kernel.
22644 	 *
22645 	 * This function could be called from ill_lookup_on_name. In that case
22646 	 * the interface is loopback "lo", which will not generate a NIC event.
22647 	 */
22648 	if (ill->ill_name_length <= 2 ||
22649 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22650 		/*
22651 		 * Generate nic plumb event for ill_name even if
22652 		 * ipmp_hook_emulation is set. That avoids generating events
22653 		 * for the ill_names should ipmp_hook_emulation be turned on
22654 		 * later.
22655 		 */
22656 		ill_nic_info_plumb(ill, B_FALSE);
22657 	}
22658 	RELEASE_ILL_LOCKS(ill, ill_other);
22659 	mutex_exit(&phyi->phyint_lock);
22660 }
22661 
22662 /*
22663  * Allocate a NE_PLUMB nic info event and store in the ill.
22664  * If 'group' is set we do it for the group name, otherwise the ill name.
22665  * It will be sent when we leave the ipsq.
22666  */
22667 void
22668 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22669 {
22670 	phyint_t	*phyi = ill->ill_phyint;
22671 	char		*name;
22672 	int		namelen;
22673 
22674 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22675 
22676 	if (group) {
22677 		ASSERT(phyi->phyint_groupname_len != 0);
22678 		namelen = phyi->phyint_groupname_len;
22679 		name = phyi->phyint_groupname;
22680 	} else {
22681 		namelen = ill->ill_name_length;
22682 		name = ill->ill_name;
22683 	}
22684 
22685 	(void) ill_hook_event_create(ill, 0, NE_PLUMB, name, namelen);
22686 }
22687 
22688 /*
22689  * Unhook the nic event message from the ill and enqueue it
22690  * into the nic event taskq.
22691  */
22692 void
22693 ill_nic_info_dispatch(ill_t *ill)
22694 {
22695 	hook_nic_event_t *info;
22696 
22697 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22698 
22699 	if ((info = ill->ill_nic_event_info) != NULL) {
22700 		if (ddi_taskq_dispatch(eventq_queue_nic,
22701 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22702 			ip2dbg(("ill_nic_info_dispatch: "
22703 			    "ddi_taskq_dispatch failed\n"));
22704 			if (info->hne_data != NULL)
22705 				kmem_free(info->hne_data, info->hne_datalen);
22706 			kmem_free(info, sizeof (hook_nic_event_t));
22707 		}
22708 		ill->ill_nic_event_info = NULL;
22709 	}
22710 }
22711 
22712 /*
22713  * Notify any downstream modules of the name of this interface.
22714  * An M_IOCTL is used even though we don't expect a successful reply.
22715  * Any reply message from the driver (presumably an M_IOCNAK) will
22716  * eventually get discarded somewhere upstream.  The message format is
22717  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22718  * to IP.
22719  */
22720 static void
22721 ip_ifname_notify(ill_t *ill, queue_t *q)
22722 {
22723 	mblk_t *mp1, *mp2;
22724 	struct iocblk *iocp;
22725 	struct lifreq *lifr;
22726 
22727 	mp1 = mkiocb(SIOCSLIFNAME);
22728 	if (mp1 == NULL)
22729 		return;
22730 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22731 	if (mp2 == NULL) {
22732 		freeb(mp1);
22733 		return;
22734 	}
22735 
22736 	mp1->b_cont = mp2;
22737 	iocp = (struct iocblk *)mp1->b_rptr;
22738 	iocp->ioc_count = sizeof (struct lifreq);
22739 
22740 	lifr = (struct lifreq *)mp2->b_rptr;
22741 	mp2->b_wptr += sizeof (struct lifreq);
22742 	bzero(lifr, sizeof (struct lifreq));
22743 
22744 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22745 	lifr->lifr_ppa = ill->ill_ppa;
22746 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22747 
22748 	putnext(q, mp1);
22749 }
22750 
22751 static int
22752 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22753 {
22754 	int err;
22755 	ip_stack_t	*ipst = ill->ill_ipst;
22756 
22757 	/* Set the obsolete NDD per-interface forwarding name. */
22758 	err = ill_set_ndd_name(ill);
22759 	if (err != 0) {
22760 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22761 		    err);
22762 	}
22763 
22764 	/* Tell downstream modules where they are. */
22765 	ip_ifname_notify(ill, q);
22766 
22767 	/*
22768 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22769 	 * Error cases are ENOMEM ...
22770 	 */
22771 	err = ill_dl_phys(ill, ipif, mp, q);
22772 
22773 	/*
22774 	 * If there is no IRE expiration timer running, get one started.
22775 	 * igmp and mld timers will be triggered by the first multicast
22776 	 */
22777 	if (ipst->ips_ip_ire_expire_id == 0) {
22778 		/*
22779 		 * acquire the lock and check again.
22780 		 */
22781 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22782 		if (ipst->ips_ip_ire_expire_id == 0) {
22783 			ipst->ips_ip_ire_expire_id = timeout(
22784 			    ip_trash_timer_expire, ipst,
22785 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22786 		}
22787 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22788 	}
22789 
22790 	if (ill->ill_isv6) {
22791 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22792 		if (ipst->ips_mld_slowtimeout_id == 0) {
22793 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22794 			    (void *)ipst,
22795 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22796 		}
22797 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22798 	} else {
22799 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22800 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22801 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22802 			    (void *)ipst,
22803 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22804 		}
22805 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22806 	}
22807 
22808 	return (err);
22809 }
22810 
22811 /*
22812  * Common routine for ppa and ifname setting. Should be called exclusive.
22813  *
22814  * Returns EINPROGRESS when mp has been consumed by queueing it on
22815  * ill_pending_mp and the ioctl will complete in ip_rput.
22816  *
22817  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22818  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22819  * For SLIFNAME, we pass these values back to the userland.
22820  */
22821 static int
22822 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22823 {
22824 	ill_t	*ill;
22825 	ipif_t	*ipif;
22826 	ipsq_t	*ipsq;
22827 	char	*ppa_ptr;
22828 	char	*old_ptr;
22829 	char	old_char;
22830 	int	error;
22831 	ip_stack_t	*ipst;
22832 
22833 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22834 	ASSERT(q->q_next != NULL);
22835 	ASSERT(interf_name != NULL);
22836 
22837 	ill = (ill_t *)q->q_ptr;
22838 	ipst = ill->ill_ipst;
22839 
22840 	ASSERT(ill->ill_ipst != NULL);
22841 	ASSERT(ill->ill_name[0] == '\0');
22842 	ASSERT(IAM_WRITER_ILL(ill));
22843 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22844 	ASSERT(ill->ill_ppa == UINT_MAX);
22845 
22846 	/* The ppa is sent down by ifconfig or is chosen */
22847 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22848 		return (EINVAL);
22849 	}
22850 
22851 	/*
22852 	 * make sure ppa passed in is same as ppa in the name.
22853 	 * This check is not made when ppa == UINT_MAX in that case ppa
22854 	 * in the name could be anything. System will choose a ppa and
22855 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22856 	 */
22857 	if (*new_ppa_ptr != UINT_MAX) {
22858 		/* stoi changes the pointer */
22859 		old_ptr = ppa_ptr;
22860 		/*
22861 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22862 		 * (they don't have an externally visible ppa).  We assign one
22863 		 * here so that we can manage the interface.  Note that in
22864 		 * the past this value was always 0 for DLPI 1 drivers.
22865 		 */
22866 		if (*new_ppa_ptr == 0)
22867 			*new_ppa_ptr = stoi(&old_ptr);
22868 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22869 			return (EINVAL);
22870 	}
22871 	/*
22872 	 * terminate string before ppa
22873 	 * save char at that location.
22874 	 */
22875 	old_char = ppa_ptr[0];
22876 	ppa_ptr[0] = '\0';
22877 
22878 	ill->ill_ppa = *new_ppa_ptr;
22879 	/*
22880 	 * Finish as much work now as possible before calling ill_glist_insert
22881 	 * which makes the ill globally visible and also merges it with the
22882 	 * other protocol instance of this phyint. The remaining work is
22883 	 * done after entering the ipsq which may happen sometime later.
22884 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22885 	 */
22886 	ipif = ill->ill_ipif;
22887 
22888 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22889 	ipif_assign_seqid(ipif);
22890 
22891 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22892 		ill->ill_flags |= ILLF_IPV4;
22893 
22894 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22895 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22896 
22897 	if (ill->ill_flags & ILLF_IPV6) {
22898 
22899 		ill->ill_isv6 = B_TRUE;
22900 		if (ill->ill_rq != NULL) {
22901 			ill->ill_rq->q_qinfo = &iprinitv6;
22902 			ill->ill_wq->q_qinfo = &ipwinitv6;
22903 		}
22904 
22905 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22906 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22907 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22908 		ipif->ipif_v6subnet = ipv6_all_zeros;
22909 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22910 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22911 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22912 		/*
22913 		 * point-to-point or Non-mulicast capable
22914 		 * interfaces won't do NUD unless explicitly
22915 		 * configured to do so.
22916 		 */
22917 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22918 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22919 			ill->ill_flags |= ILLF_NONUD;
22920 		}
22921 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22922 		if (ill->ill_flags & ILLF_NOARP) {
22923 			/*
22924 			 * Note: xresolv interfaces will eventually need
22925 			 * NOARP set here as well, but that will require
22926 			 * those external resolvers to have some
22927 			 * knowledge of that flag and act appropriately.
22928 			 * Not to be changed at present.
22929 			 */
22930 			ill->ill_flags &= ~ILLF_NOARP;
22931 		}
22932 		/*
22933 		 * Set the ILLF_ROUTER flag according to the global
22934 		 * IPv6 forwarding policy.
22935 		 */
22936 		if (ipst->ips_ipv6_forward != 0)
22937 			ill->ill_flags |= ILLF_ROUTER;
22938 	} else if (ill->ill_flags & ILLF_IPV4) {
22939 		ill->ill_isv6 = B_FALSE;
22940 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
22941 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
22942 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
22943 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
22944 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
22945 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
22946 		/*
22947 		 * Set the ILLF_ROUTER flag according to the global
22948 		 * IPv4 forwarding policy.
22949 		 */
22950 		if (ipst->ips_ip_g_forward != 0)
22951 			ill->ill_flags |= ILLF_ROUTER;
22952 	}
22953 
22954 	ASSERT(ill->ill_phyint != NULL);
22955 
22956 	/*
22957 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
22958 	 * be completed in ill_glist_insert -> ill_phyint_reinit
22959 	 */
22960 	if (!ill_allocate_mibs(ill))
22961 		return (ENOMEM);
22962 
22963 	/*
22964 	 * Pick a default sap until we get the DL_INFO_ACK back from
22965 	 * the driver.
22966 	 */
22967 	if (ill->ill_sap == 0) {
22968 		if (ill->ill_isv6)
22969 			ill->ill_sap  = IP6_DL_SAP;
22970 		else
22971 			ill->ill_sap  = IP_DL_SAP;
22972 	}
22973 
22974 	ill->ill_ifname_pending = 1;
22975 	ill->ill_ifname_pending_err = 0;
22976 
22977 	ill_refhold(ill);
22978 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
22979 	if ((error = ill_glist_insert(ill, interf_name,
22980 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
22981 		ill->ill_ppa = UINT_MAX;
22982 		ill->ill_name[0] = '\0';
22983 		/*
22984 		 * undo null termination done above.
22985 		 */
22986 		ppa_ptr[0] = old_char;
22987 		rw_exit(&ipst->ips_ill_g_lock);
22988 		ill_refrele(ill);
22989 		return (error);
22990 	}
22991 
22992 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
22993 
22994 	/*
22995 	 * When we return the buffer pointed to by interf_name should contain
22996 	 * the same name as in ill_name.
22997 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
22998 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
22999 	 * so copy full name and update the ppa ptr.
23000 	 * When ppa passed in != UINT_MAX all values are correct just undo
23001 	 * null termination, this saves a bcopy.
23002 	 */
23003 	if (*new_ppa_ptr == UINT_MAX) {
23004 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23005 		*new_ppa_ptr = ill->ill_ppa;
23006 	} else {
23007 		/*
23008 		 * undo null termination done above.
23009 		 */
23010 		ppa_ptr[0] = old_char;
23011 	}
23012 
23013 	/* Let SCTP know about this ILL */
23014 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23015 
23016 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23017 	    B_TRUE);
23018 
23019 	rw_exit(&ipst->ips_ill_g_lock);
23020 	ill_refrele(ill);
23021 	if (ipsq == NULL)
23022 		return (EINPROGRESS);
23023 
23024 	/*
23025 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23026 	 */
23027 	if (ipsq->ipsq_current_ipif == NULL)
23028 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23029 	else
23030 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23031 
23032 	error = ipif_set_values_tail(ill, ipif, mp, q);
23033 	ipsq_exit(ipsq);
23034 	if (error != 0 && error != EINPROGRESS) {
23035 		/*
23036 		 * restore previous values
23037 		 */
23038 		ill->ill_isv6 = B_FALSE;
23039 	}
23040 	return (error);
23041 }
23042 
23043 
23044 void
23045 ipif_init(ip_stack_t *ipst)
23046 {
23047 	hrtime_t hrt;
23048 	int i;
23049 
23050 	/*
23051 	 * Can't call drv_getparm here as it is too early in the boot.
23052 	 * As we use ipif_src_random just for picking a different
23053 	 * source address everytime, this need not be really random.
23054 	 */
23055 	hrt = gethrtime();
23056 	ipst->ips_ipif_src_random =
23057 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23058 
23059 	for (i = 0; i < MAX_G_HEADS; i++) {
23060 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23061 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23062 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23063 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23064 	}
23065 
23066 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23067 	    ill_phyint_compare_index,
23068 	    sizeof (phyint_t),
23069 	    offsetof(struct phyint, phyint_avl_by_index));
23070 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23071 	    ill_phyint_compare_name,
23072 	    sizeof (phyint_t),
23073 	    offsetof(struct phyint, phyint_avl_by_name));
23074 }
23075 
23076 /*
23077  * Lookup the ipif corresponding to the onlink destination address. For
23078  * point-to-point interfaces, it matches with remote endpoint destination
23079  * address. For point-to-multipoint interfaces it only tries to match the
23080  * destination with the interface's subnet address. The longest, most specific
23081  * match is found to take care of such rare network configurations like -
23082  * le0: 129.146.1.1/16
23083  * le1: 129.146.2.2/24
23084  * It is used only by SO_DONTROUTE at the moment.
23085  */
23086 ipif_t *
23087 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23088 {
23089 	ipif_t	*ipif, *best_ipif;
23090 	ill_t	*ill;
23091 	ill_walk_context_t ctx;
23092 
23093 	ASSERT(zoneid != ALL_ZONES);
23094 	best_ipif = NULL;
23095 
23096 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23097 	ill = ILL_START_WALK_V4(&ctx, ipst);
23098 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23099 		mutex_enter(&ill->ill_lock);
23100 		for (ipif = ill->ill_ipif; ipif != NULL;
23101 		    ipif = ipif->ipif_next) {
23102 			if (!IPIF_CAN_LOOKUP(ipif))
23103 				continue;
23104 			if (ipif->ipif_zoneid != zoneid &&
23105 			    ipif->ipif_zoneid != ALL_ZONES)
23106 				continue;
23107 			/*
23108 			 * Point-to-point case. Look for exact match with
23109 			 * destination address.
23110 			 */
23111 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23112 				if (ipif->ipif_pp_dst_addr == addr) {
23113 					ipif_refhold_locked(ipif);
23114 					mutex_exit(&ill->ill_lock);
23115 					rw_exit(&ipst->ips_ill_g_lock);
23116 					if (best_ipif != NULL)
23117 						ipif_refrele(best_ipif);
23118 					return (ipif);
23119 				}
23120 			} else if (ipif->ipif_subnet == (addr &
23121 			    ipif->ipif_net_mask)) {
23122 				/*
23123 				 * Point-to-multipoint case. Looping through to
23124 				 * find the most specific match. If there are
23125 				 * multiple best match ipif's then prefer ipif's
23126 				 * that are UP. If there is only one best match
23127 				 * ipif and it is DOWN we must still return it.
23128 				 */
23129 				if ((best_ipif == NULL) ||
23130 				    (ipif->ipif_net_mask >
23131 				    best_ipif->ipif_net_mask) ||
23132 				    ((ipif->ipif_net_mask ==
23133 				    best_ipif->ipif_net_mask) &&
23134 				    ((ipif->ipif_flags & IPIF_UP) &&
23135 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23136 					ipif_refhold_locked(ipif);
23137 					mutex_exit(&ill->ill_lock);
23138 					rw_exit(&ipst->ips_ill_g_lock);
23139 					if (best_ipif != NULL)
23140 						ipif_refrele(best_ipif);
23141 					best_ipif = ipif;
23142 					rw_enter(&ipst->ips_ill_g_lock,
23143 					    RW_READER);
23144 					mutex_enter(&ill->ill_lock);
23145 				}
23146 			}
23147 		}
23148 		mutex_exit(&ill->ill_lock);
23149 	}
23150 	rw_exit(&ipst->ips_ill_g_lock);
23151 	return (best_ipif);
23152 }
23153 
23154 /*
23155  * Save enough information so that we can recreate the IRE if
23156  * the interface goes down and then up.
23157  */
23158 static void
23159 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23160 {
23161 	mblk_t	*save_mp;
23162 
23163 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23164 	if (save_mp != NULL) {
23165 		ifrt_t	*ifrt;
23166 
23167 		save_mp->b_wptr += sizeof (ifrt_t);
23168 		ifrt = (ifrt_t *)save_mp->b_rptr;
23169 		bzero(ifrt, sizeof (ifrt_t));
23170 		ifrt->ifrt_type = ire->ire_type;
23171 		ifrt->ifrt_addr = ire->ire_addr;
23172 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23173 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23174 		ifrt->ifrt_mask = ire->ire_mask;
23175 		ifrt->ifrt_flags = ire->ire_flags;
23176 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23177 		mutex_enter(&ipif->ipif_saved_ire_lock);
23178 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23179 		ipif->ipif_saved_ire_mp = save_mp;
23180 		ipif->ipif_saved_ire_cnt++;
23181 		mutex_exit(&ipif->ipif_saved_ire_lock);
23182 	}
23183 }
23184 
23185 static void
23186 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23187 {
23188 	mblk_t	**mpp;
23189 	mblk_t	*mp;
23190 	ifrt_t	*ifrt;
23191 
23192 	/* Remove from ipif_saved_ire_mp list if it is there */
23193 	mutex_enter(&ipif->ipif_saved_ire_lock);
23194 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23195 	    mpp = &(*mpp)->b_cont) {
23196 		/*
23197 		 * On a given ipif, the triple of address, gateway and
23198 		 * mask is unique for each saved IRE (in the case of
23199 		 * ordinary interface routes, the gateway address is
23200 		 * all-zeroes).
23201 		 */
23202 		mp = *mpp;
23203 		ifrt = (ifrt_t *)mp->b_rptr;
23204 		if (ifrt->ifrt_addr == ire->ire_addr &&
23205 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23206 		    ifrt->ifrt_mask == ire->ire_mask) {
23207 			*mpp = mp->b_cont;
23208 			ipif->ipif_saved_ire_cnt--;
23209 			freeb(mp);
23210 			break;
23211 		}
23212 	}
23213 	mutex_exit(&ipif->ipif_saved_ire_lock);
23214 }
23215 
23216 /*
23217  * IP multirouting broadcast routes handling
23218  * Append CGTP broadcast IREs to regular ones created
23219  * at ifconfig time.
23220  */
23221 static void
23222 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23223 {
23224 	ire_t *ire_prim;
23225 
23226 	ASSERT(ire != NULL);
23227 	ASSERT(ire_dst != NULL);
23228 
23229 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23230 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23231 	if (ire_prim != NULL) {
23232 		/*
23233 		 * We are in the special case of broadcasts for
23234 		 * CGTP. We add an IRE_BROADCAST that holds
23235 		 * the RTF_MULTIRT flag, the destination
23236 		 * address of ire_dst and the low level
23237 		 * info of ire_prim. In other words, CGTP
23238 		 * broadcast is added to the redundant ipif.
23239 		 */
23240 		ipif_t *ipif_prim;
23241 		ire_t  *bcast_ire;
23242 
23243 		ipif_prim = ire_prim->ire_ipif;
23244 
23245 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23246 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23247 		    (void *)ire_dst, (void *)ire_prim,
23248 		    (void *)ipif_prim));
23249 
23250 		bcast_ire = ire_create(
23251 		    (uchar_t *)&ire->ire_addr,
23252 		    (uchar_t *)&ip_g_all_ones,
23253 		    (uchar_t *)&ire_dst->ire_src_addr,
23254 		    (uchar_t *)&ire->ire_gateway_addr,
23255 		    &ipif_prim->ipif_mtu,
23256 		    NULL,
23257 		    ipif_prim->ipif_rq,
23258 		    ipif_prim->ipif_wq,
23259 		    IRE_BROADCAST,
23260 		    ipif_prim,
23261 		    0,
23262 		    0,
23263 		    0,
23264 		    ire->ire_flags,
23265 		    &ire_uinfo_null,
23266 		    NULL,
23267 		    NULL,
23268 		    ipst);
23269 
23270 		if (bcast_ire != NULL) {
23271 
23272 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23273 			    B_FALSE) == 0) {
23274 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23275 				    "added bcast_ire %p\n",
23276 				    (void *)bcast_ire));
23277 
23278 				ipif_save_ire(bcast_ire->ire_ipif,
23279 				    bcast_ire);
23280 				ire_refrele(bcast_ire);
23281 			}
23282 		}
23283 		ire_refrele(ire_prim);
23284 	}
23285 }
23286 
23287 
23288 /*
23289  * IP multirouting broadcast routes handling
23290  * Remove the broadcast ire
23291  */
23292 static void
23293 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23294 {
23295 	ire_t *ire_dst;
23296 
23297 	ASSERT(ire != NULL);
23298 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23299 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23300 	if (ire_dst != NULL) {
23301 		ire_t *ire_prim;
23302 
23303 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23304 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23305 		if (ire_prim != NULL) {
23306 			ipif_t *ipif_prim;
23307 			ire_t  *bcast_ire;
23308 
23309 			ipif_prim = ire_prim->ire_ipif;
23310 
23311 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23312 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23313 			    (void *)ire_dst, (void *)ire_prim,
23314 			    (void *)ipif_prim));
23315 
23316 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23317 			    ire->ire_gateway_addr,
23318 			    IRE_BROADCAST,
23319 			    ipif_prim, ALL_ZONES,
23320 			    NULL,
23321 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23322 			    MATCH_IRE_MASK, ipst);
23323 
23324 			if (bcast_ire != NULL) {
23325 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23326 				    "looked up bcast_ire %p\n",
23327 				    (void *)bcast_ire));
23328 				ipif_remove_ire(bcast_ire->ire_ipif,
23329 				    bcast_ire);
23330 				ire_delete(bcast_ire);
23331 				ire_refrele(bcast_ire);
23332 			}
23333 			ire_refrele(ire_prim);
23334 		}
23335 		ire_refrele(ire_dst);
23336 	}
23337 }
23338 
23339 /*
23340  * IPsec hardware acceleration capabilities related functions.
23341  */
23342 
23343 /*
23344  * Free a per-ill IPsec capabilities structure.
23345  */
23346 static void
23347 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23348 {
23349 	if (capab->auth_hw_algs != NULL)
23350 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23351 	if (capab->encr_hw_algs != NULL)
23352 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23353 	if (capab->encr_algparm != NULL)
23354 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23355 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23356 }
23357 
23358 /*
23359  * Allocate a new per-ill IPsec capabilities structure. This structure
23360  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23361  * an array which specifies, for each algorithm, whether this algorithm
23362  * is supported by the ill or not.
23363  */
23364 static ill_ipsec_capab_t *
23365 ill_ipsec_capab_alloc(void)
23366 {
23367 	ill_ipsec_capab_t *capab;
23368 	uint_t nelems;
23369 
23370 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23371 	if (capab == NULL)
23372 		return (NULL);
23373 
23374 	/* we need one bit per algorithm */
23375 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23376 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23377 
23378 	/* allocate memory to store algorithm flags */
23379 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23380 	if (capab->encr_hw_algs == NULL)
23381 		goto nomem;
23382 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23383 	if (capab->auth_hw_algs == NULL)
23384 		goto nomem;
23385 	/*
23386 	 * Leave encr_algparm NULL for now since we won't need it half
23387 	 * the time
23388 	 */
23389 	return (capab);
23390 
23391 nomem:
23392 	ill_ipsec_capab_free(capab);
23393 	return (NULL);
23394 }
23395 
23396 /*
23397  * Resize capability array.  Since we're exclusive, this is OK.
23398  */
23399 static boolean_t
23400 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23401 {
23402 	ipsec_capab_algparm_t *nalp, *oalp;
23403 	uint32_t olen, nlen;
23404 
23405 	oalp = capab->encr_algparm;
23406 	olen = capab->encr_algparm_size;
23407 
23408 	if (oalp != NULL) {
23409 		if (algid < capab->encr_algparm_end)
23410 			return (B_TRUE);
23411 	}
23412 
23413 	nlen = (algid + 1) * sizeof (*nalp);
23414 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23415 	if (nalp == NULL)
23416 		return (B_FALSE);
23417 
23418 	if (oalp != NULL) {
23419 		bcopy(oalp, nalp, olen);
23420 		kmem_free(oalp, olen);
23421 	}
23422 	capab->encr_algparm = nalp;
23423 	capab->encr_algparm_size = nlen;
23424 	capab->encr_algparm_end = algid + 1;
23425 
23426 	return (B_TRUE);
23427 }
23428 
23429 /*
23430  * Compare the capabilities of the specified ill with the protocol
23431  * and algorithms specified by the SA passed as argument.
23432  * If they match, returns B_TRUE, B_FALSE if they do not match.
23433  *
23434  * The ill can be passed as a pointer to it, or by specifying its index
23435  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23436  *
23437  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23438  * packet is eligible for hardware acceleration, and by
23439  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23440  * to a particular ill.
23441  */
23442 boolean_t
23443 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23444     ipsa_t *sa, netstack_t *ns)
23445 {
23446 	boolean_t sa_isv6;
23447 	uint_t algid;
23448 	struct ill_ipsec_capab_s *cpp;
23449 	boolean_t need_refrele = B_FALSE;
23450 	ip_stack_t	*ipst = ns->netstack_ip;
23451 
23452 	if (ill == NULL) {
23453 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23454 		    NULL, NULL, NULL, ipst);
23455 		if (ill == NULL) {
23456 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23457 			return (B_FALSE);
23458 		}
23459 		need_refrele = B_TRUE;
23460 	}
23461 
23462 	/*
23463 	 * Use the address length specified by the SA to determine
23464 	 * if it corresponds to a IPv6 address, and fail the matching
23465 	 * if the isv6 flag passed as argument does not match.
23466 	 * Note: this check is used for SADB capability checking before
23467 	 * sending SA information to an ill.
23468 	 */
23469 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23470 	if (sa_isv6 != ill_isv6)
23471 		/* protocol mismatch */
23472 		goto done;
23473 
23474 	/*
23475 	 * Check if the ill supports the protocol, algorithm(s) and
23476 	 * key size(s) specified by the SA, and get the pointers to
23477 	 * the algorithms supported by the ill.
23478 	 */
23479 	switch (sa->ipsa_type) {
23480 
23481 	case SADB_SATYPE_ESP:
23482 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23483 			/* ill does not support ESP acceleration */
23484 			goto done;
23485 		cpp = ill->ill_ipsec_capab_esp;
23486 		algid = sa->ipsa_auth_alg;
23487 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23488 			goto done;
23489 		algid = sa->ipsa_encr_alg;
23490 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23491 			goto done;
23492 		if (algid < cpp->encr_algparm_end) {
23493 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23494 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23495 				goto done;
23496 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23497 				goto done;
23498 		}
23499 		break;
23500 
23501 	case SADB_SATYPE_AH:
23502 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23503 			/* ill does not support AH acceleration */
23504 			goto done;
23505 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23506 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23507 			goto done;
23508 		break;
23509 	}
23510 
23511 	if (need_refrele)
23512 		ill_refrele(ill);
23513 	return (B_TRUE);
23514 done:
23515 	if (need_refrele)
23516 		ill_refrele(ill);
23517 	return (B_FALSE);
23518 }
23519 
23520 /*
23521  * Add a new ill to the list of IPsec capable ills.
23522  * Called from ill_capability_ipsec_ack() when an ACK was received
23523  * indicating that IPsec hardware processing was enabled for an ill.
23524  *
23525  * ill must point to the ill for which acceleration was enabled.
23526  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23527  */
23528 static void
23529 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23530 {
23531 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23532 	uint_t sa_type;
23533 	uint_t ipproto;
23534 	ip_stack_t	*ipst = ill->ill_ipst;
23535 
23536 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23537 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23538 
23539 	switch (dl_cap) {
23540 	case DL_CAPAB_IPSEC_AH:
23541 		sa_type = SADB_SATYPE_AH;
23542 		ills = &ipst->ips_ipsec_capab_ills_ah;
23543 		ipproto = IPPROTO_AH;
23544 		break;
23545 	case DL_CAPAB_IPSEC_ESP:
23546 		sa_type = SADB_SATYPE_ESP;
23547 		ills = &ipst->ips_ipsec_capab_ills_esp;
23548 		ipproto = IPPROTO_ESP;
23549 		break;
23550 	}
23551 
23552 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23553 
23554 	/*
23555 	 * Add ill index to list of hardware accelerators. If
23556 	 * already in list, do nothing.
23557 	 */
23558 	for (cur_ill = *ills; cur_ill != NULL &&
23559 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23560 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23561 		;
23562 
23563 	if (cur_ill == NULL) {
23564 		/* if this is a new entry for this ill */
23565 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23566 		if (new_ill == NULL) {
23567 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23568 			return;
23569 		}
23570 
23571 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23572 		new_ill->ill_isv6 = ill->ill_isv6;
23573 		new_ill->next = *ills;
23574 		*ills = new_ill;
23575 	} else if (!sadb_resync) {
23576 		/* not resync'ing SADB and an entry exists for this ill */
23577 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23578 		return;
23579 	}
23580 
23581 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23582 
23583 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23584 		/*
23585 		 * IPsec module for protocol loaded, initiate dump
23586 		 * of the SADB to this ill.
23587 		 */
23588 		sadb_ill_download(ill, sa_type);
23589 }
23590 
23591 /*
23592  * Remove an ill from the list of IPsec capable ills.
23593  */
23594 static void
23595 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23596 {
23597 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23598 	ip_stack_t	*ipst = ill->ill_ipst;
23599 
23600 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23601 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23602 
23603 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23604 	    &ipst->ips_ipsec_capab_ills_esp;
23605 
23606 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23607 
23608 	prev_ill = NULL;
23609 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23610 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23611 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23612 		;
23613 	if (cur_ill == NULL) {
23614 		/* entry not found */
23615 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23616 		return;
23617 	}
23618 	if (prev_ill == NULL) {
23619 		/* entry at front of list */
23620 		*ills = NULL;
23621 	} else {
23622 		prev_ill->next = cur_ill->next;
23623 	}
23624 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23625 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23626 }
23627 
23628 /*
23629  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23630  * supporting the specified IPsec protocol acceleration.
23631  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23632  * We free the mblk and, if sa is non-null, release the held referece.
23633  */
23634 void
23635 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23636     netstack_t *ns)
23637 {
23638 	ipsec_capab_ill_t *ici, *cur_ici;
23639 	ill_t *ill;
23640 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23641 	ip_stack_t	*ipst = ns->netstack_ip;
23642 
23643 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23644 	    ipst->ips_ipsec_capab_ills_esp;
23645 
23646 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23647 
23648 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23649 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23650 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23651 
23652 		/*
23653 		 * Handle the case where the ill goes away while the SADB is
23654 		 * attempting to send messages.  If it's going away, it's
23655 		 * nuking its shadow SADB, so we don't care..
23656 		 */
23657 
23658 		if (ill == NULL)
23659 			continue;
23660 
23661 		if (sa != NULL) {
23662 			/*
23663 			 * Make sure capabilities match before
23664 			 * sending SA to ill.
23665 			 */
23666 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23667 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23668 				ill_refrele(ill);
23669 				continue;
23670 			}
23671 
23672 			mutex_enter(&sa->ipsa_lock);
23673 			sa->ipsa_flags |= IPSA_F_HW;
23674 			mutex_exit(&sa->ipsa_lock);
23675 		}
23676 
23677 		/*
23678 		 * Copy template message, and add it to the front
23679 		 * of the mblk ship list. We want to avoid holding
23680 		 * the ipsec_capab_ills_lock while sending the
23681 		 * message to the ills.
23682 		 *
23683 		 * The b_next and b_prev are temporarily used
23684 		 * to build a list of mblks to be sent down, and to
23685 		 * save the ill to which they must be sent.
23686 		 */
23687 		nmp = copymsg(mp);
23688 		if (nmp == NULL) {
23689 			ill_refrele(ill);
23690 			continue;
23691 		}
23692 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23693 		nmp->b_next = mp_ship_list;
23694 		mp_ship_list = nmp;
23695 		nmp->b_prev = (mblk_t *)ill;
23696 	}
23697 
23698 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23699 
23700 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23701 		/* restore the mblk to a sane state */
23702 		next_mp = nmp->b_next;
23703 		nmp->b_next = NULL;
23704 		ill = (ill_t *)nmp->b_prev;
23705 		nmp->b_prev = NULL;
23706 
23707 		ill_dlpi_send(ill, nmp);
23708 		ill_refrele(ill);
23709 	}
23710 
23711 	if (sa != NULL)
23712 		IPSA_REFRELE(sa);
23713 	freemsg(mp);
23714 }
23715 
23716 /*
23717  * Derive an interface id from the link layer address.
23718  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23719  */
23720 static boolean_t
23721 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23722 {
23723 	char		*addr;
23724 
23725 	if (phys_length != ETHERADDRL)
23726 		return (B_FALSE);
23727 
23728 	/* Form EUI-64 like address */
23729 	addr = (char *)&v6addr->s6_addr32[2];
23730 	bcopy((char *)phys_addr, addr, 3);
23731 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23732 	addr[3] = (char)0xff;
23733 	addr[4] = (char)0xfe;
23734 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23735 	return (B_TRUE);
23736 }
23737 
23738 /* ARGSUSED */
23739 static boolean_t
23740 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23741 {
23742 	return (B_FALSE);
23743 }
23744 
23745 /* ARGSUSED */
23746 static boolean_t
23747 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23748     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23749 {
23750 	/*
23751 	 * Multicast address mappings used over Ethernet/802.X.
23752 	 * This address is used as a base for mappings.
23753 	 */
23754 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23755 	    0x00, 0x00, 0x00};
23756 
23757 	/*
23758 	 * Extract low order 32 bits from IPv6 multicast address.
23759 	 * Or that into the link layer address, starting from the
23760 	 * second byte.
23761 	 */
23762 	*hw_start = 2;
23763 	v6_extract_mask->s6_addr32[0] = 0;
23764 	v6_extract_mask->s6_addr32[1] = 0;
23765 	v6_extract_mask->s6_addr32[2] = 0;
23766 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23767 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23768 	return (B_TRUE);
23769 }
23770 
23771 /*
23772  * Indicate by return value whether multicast is supported. If not,
23773  * this code should not touch/change any parameters.
23774  */
23775 /* ARGSUSED */
23776 static boolean_t
23777 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23778     uint32_t *hw_start, ipaddr_t *extract_mask)
23779 {
23780 	/*
23781 	 * Multicast address mappings used over Ethernet/802.X.
23782 	 * This address is used as a base for mappings.
23783 	 */
23784 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23785 	    0x00, 0x00, 0x00 };
23786 
23787 	if (phys_length != ETHERADDRL)
23788 		return (B_FALSE);
23789 
23790 	*extract_mask = htonl(0x007fffff);
23791 	*hw_start = 2;
23792 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23793 	return (B_TRUE);
23794 }
23795 
23796 /*
23797  * Derive IPoIB interface id from the link layer address.
23798  */
23799 static boolean_t
23800 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23801 {
23802 	char		*addr;
23803 
23804 	if (phys_length != 20)
23805 		return (B_FALSE);
23806 	addr = (char *)&v6addr->s6_addr32[2];
23807 	bcopy(phys_addr + 12, addr, 8);
23808 	/*
23809 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23810 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23811 	 * rules. In these cases, the IBA considers these GUIDs to be in
23812 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23813 	 * required; vendors are required not to assign global EUI-64's
23814 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23815 	 * of the interface identifier. Whether the GUID is in modified
23816 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23817 	 * bit set to 1.
23818 	 */
23819 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23820 	return (B_TRUE);
23821 }
23822 
23823 /*
23824  * Note on mapping from multicast IP addresses to IPoIB multicast link
23825  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23826  * The format of an IPoIB multicast address is:
23827  *
23828  *  4 byte QPN      Scope Sign.  Pkey
23829  * +--------------------------------------------+
23830  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23831  * +--------------------------------------------+
23832  *
23833  * The Scope and Pkey components are properties of the IBA port and
23834  * network interface. They can be ascertained from the broadcast address.
23835  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23836  */
23837 
23838 static boolean_t
23839 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23840     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23841 {
23842 	/*
23843 	 * Base IPoIB IPv6 multicast address used for mappings.
23844 	 * Does not contain the IBA scope/Pkey values.
23845 	 */
23846 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23847 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23848 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23849 
23850 	/*
23851 	 * Extract low order 80 bits from IPv6 multicast address.
23852 	 * Or that into the link layer address, starting from the
23853 	 * sixth byte.
23854 	 */
23855 	*hw_start = 6;
23856 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23857 
23858 	/*
23859 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23860 	 */
23861 	*(maddr + 5) = *(bphys_addr + 5);
23862 	*(maddr + 8) = *(bphys_addr + 8);
23863 	*(maddr + 9) = *(bphys_addr + 9);
23864 
23865 	v6_extract_mask->s6_addr32[0] = 0;
23866 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23867 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23868 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23869 	return (B_TRUE);
23870 }
23871 
23872 static boolean_t
23873 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23874     uint32_t *hw_start, ipaddr_t *extract_mask)
23875 {
23876 	/*
23877 	 * Base IPoIB IPv4 multicast address used for mappings.
23878 	 * Does not contain the IBA scope/Pkey values.
23879 	 */
23880 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23881 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23882 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23883 
23884 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23885 		return (B_FALSE);
23886 
23887 	/*
23888 	 * Extract low order 28 bits from IPv4 multicast address.
23889 	 * Or that into the link layer address, starting from the
23890 	 * sixteenth byte.
23891 	 */
23892 	*extract_mask = htonl(0x0fffffff);
23893 	*hw_start = 16;
23894 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23895 
23896 	/*
23897 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23898 	 */
23899 	*(maddr + 5) = *(bphys_addr + 5);
23900 	*(maddr + 8) = *(bphys_addr + 8);
23901 	*(maddr + 9) = *(bphys_addr + 9);
23902 	return (B_TRUE);
23903 }
23904 
23905 /*
23906  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23907  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23908  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23909  * the link-local address is preferred.
23910  */
23911 boolean_t
23912 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23913 {
23914 	ipif_t	*ipif;
23915 	ipif_t	*maybe_ipif = NULL;
23916 
23917 	mutex_enter(&ill->ill_lock);
23918 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23919 		mutex_exit(&ill->ill_lock);
23920 		if (ipifp != NULL)
23921 			*ipifp = NULL;
23922 		return (B_FALSE);
23923 	}
23924 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23925 		if (!IPIF_CAN_LOOKUP(ipif))
23926 			continue;
23927 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23928 		    ipif->ipif_zoneid != ALL_ZONES)
23929 			continue;
23930 		if ((ipif->ipif_flags & flags) != flags)
23931 			continue;
23932 
23933 		if (ipifp == NULL) {
23934 			mutex_exit(&ill->ill_lock);
23935 			ASSERT(maybe_ipif == NULL);
23936 			return (B_TRUE);
23937 		}
23938 		if (!ill->ill_isv6 ||
23939 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23940 			ipif_refhold_locked(ipif);
23941 			mutex_exit(&ill->ill_lock);
23942 			*ipifp = ipif;
23943 			return (B_TRUE);
23944 		}
23945 		if (maybe_ipif == NULL)
23946 			maybe_ipif = ipif;
23947 	}
23948 	if (ipifp != NULL) {
23949 		if (maybe_ipif != NULL)
23950 			ipif_refhold_locked(maybe_ipif);
23951 		*ipifp = maybe_ipif;
23952 	}
23953 	mutex_exit(&ill->ill_lock);
23954 	return (maybe_ipif != NULL);
23955 }
23956 
23957 /*
23958  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
23959  */
23960 boolean_t
23961 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23962 {
23963 	ill_t *illg;
23964 	ip_stack_t	*ipst = ill->ill_ipst;
23965 
23966 	/*
23967 	 * We look at the passed-in ill first without grabbing ill_g_lock.
23968 	 */
23969 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
23970 		return (B_TRUE);
23971 	}
23972 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23973 	if (ill->ill_group == NULL) {
23974 		/* ill not in a group */
23975 		rw_exit(&ipst->ips_ill_g_lock);
23976 		return (B_FALSE);
23977 	}
23978 
23979 	/*
23980 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
23981 	 * group. We need to look for an ipif in the zone on all the ills in the
23982 	 * group.
23983 	 */
23984 	illg = ill->ill_group->illgrp_ill;
23985 	do {
23986 		/*
23987 		 * We don't call ipif_lookup_zoneid() on ill as we already know
23988 		 * that it's not there.
23989 		 */
23990 		if (illg != ill &&
23991 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
23992 			break;
23993 		}
23994 	} while ((illg = illg->ill_group_next) != NULL);
23995 	rw_exit(&ipst->ips_ill_g_lock);
23996 	return (illg != NULL);
23997 }
23998 
23999 /*
24000  * Check if this ill is only being used to send ICMP probes for IPMP
24001  */
24002 boolean_t
24003 ill_is_probeonly(ill_t *ill)
24004 {
24005 	/*
24006 	 * Check if the interface is FAILED, or INACTIVE
24007 	 */
24008 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24009 		return (B_TRUE);
24010 
24011 	return (B_FALSE);
24012 }
24013 
24014 /*
24015  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24016  * If a pointer to an ipif_t is returned then the caller will need to do
24017  * an ill_refrele().
24018  *
24019  * If there is no real interface which matches the ifindex, then it looks
24020  * for a group that has a matching index. In the case of a group match the
24021  * lifidx must be zero. We don't need emulate the logical interfaces
24022  * since IP Filter's use of netinfo doesn't use that.
24023  */
24024 ipif_t *
24025 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24026     ip_stack_t *ipst)
24027 {
24028 	ipif_t *ipif;
24029 	ill_t *ill;
24030 
24031 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24032 	    ipst);
24033 
24034 	if (ill == NULL) {
24035 		/* Fallback to group names only if hook_emulation set */
24036 		if (!ipst->ips_ipmp_hook_emulation)
24037 			return (NULL);
24038 
24039 		if (lifidx != 0)
24040 			return (NULL);
24041 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24042 		if (ill == NULL)
24043 			return (NULL);
24044 	}
24045 
24046 	mutex_enter(&ill->ill_lock);
24047 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24048 		mutex_exit(&ill->ill_lock);
24049 		ill_refrele(ill);
24050 		return (NULL);
24051 	}
24052 
24053 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24054 		if (!IPIF_CAN_LOOKUP(ipif))
24055 			continue;
24056 		if (lifidx == ipif->ipif_id) {
24057 			ipif_refhold_locked(ipif);
24058 			break;
24059 		}
24060 	}
24061 
24062 	mutex_exit(&ill->ill_lock);
24063 	ill_refrele(ill);
24064 	return (ipif);
24065 }
24066 
24067 /*
24068  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24069  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24070  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24071  * for details.
24072  */
24073 void
24074 ill_fastpath_flush(ill_t *ill)
24075 {
24076 	ip_stack_t *ipst = ill->ill_ipst;
24077 
24078 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24079 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24080 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24081 }
24082 
24083 /*
24084  * Set the physical address information for `ill' to the contents of the
24085  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24086  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24087  * EINPROGRESS will be returned.
24088  */
24089 int
24090 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24091 {
24092 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24093 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24094 
24095 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24096 
24097 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24098 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24099 		/* Changing DL_IPV6_TOKEN is not yet supported */
24100 		return (0);
24101 	}
24102 
24103 	/*
24104 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24105 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24106 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24107 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24108 	 */
24109 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24110 		freemsg(mp);
24111 		return (ENOMEM);
24112 	}
24113 
24114 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24115 
24116 	/*
24117 	 * If we can quiesce the ill, then set the address.  If not, then
24118 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24119 	 */
24120 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24121 	mutex_enter(&ill->ill_lock);
24122 	if (!ill_is_quiescent(ill)) {
24123 		/* call cannot fail since `conn_t *' argument is NULL */
24124 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24125 		    mp, ILL_DOWN);
24126 		mutex_exit(&ill->ill_lock);
24127 		return (EINPROGRESS);
24128 	}
24129 	mutex_exit(&ill->ill_lock);
24130 
24131 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24132 	return (0);
24133 }
24134 
24135 /*
24136  * Once the ill associated with `q' has quiesced, set its physical address
24137  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24138  * are passed (linked by b_cont), since we sometimes need to save two distinct
24139  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24140  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24141  * is quiesced, we know any stale IREs with the old address information have
24142  * already been removed, so we don't need to call ill_fastpath_flush().
24143  */
24144 /* ARGSUSED */
24145 static void
24146 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24147 {
24148 	ill_t		*ill = q->q_ptr;
24149 	mblk_t		*addrmp2 = unlinkb(addrmp);
24150 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24151 	uint_t		addrlen, addroff;
24152 
24153 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24154 
24155 	addroff	= dlindp->dl_addr_offset;
24156 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24157 
24158 	switch (dlindp->dl_data) {
24159 	case DL_IPV6_LINK_LAYER_ADDR:
24160 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24161 		freemsg(addrmp2);
24162 		break;
24163 
24164 	case DL_CURR_PHYS_ADDR:
24165 		freemsg(ill->ill_phys_addr_mp);
24166 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24167 		ill->ill_phys_addr_mp = addrmp;
24168 		ill->ill_phys_addr_length = addrlen;
24169 
24170 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24171 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24172 		else
24173 			freemsg(addrmp2);
24174 		break;
24175 	default:
24176 		ASSERT(0);
24177 	}
24178 
24179 	/*
24180 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24181 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24182 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24183 	 * brought up.
24184 	 */
24185 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24186 		ipsq_current_finish(ipsq);
24187 }
24188 
24189 /*
24190  * Helper routine for setting the ill_nd_lla fields.
24191  */
24192 void
24193 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24194 {
24195 	freemsg(ill->ill_nd_lla_mp);
24196 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24197 	ill->ill_nd_lla_mp = ndmp;
24198 	ill->ill_nd_lla_len = addrlen;
24199 }
24200 
24201 major_t IP_MAJ;
24202 #define	IP	"ip"
24203 
24204 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24205 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24206 
24207 /*
24208  * Issue REMOVEIF ioctls to have the loopback interfaces
24209  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24210  * the former going away when the user-level processes in the zone
24211  * are killed  * and the latter are cleaned up by the stream head
24212  * str_stack_shutdown callback that undoes all I_PLINKs.
24213  */
24214 void
24215 ip_loopback_cleanup(ip_stack_t *ipst)
24216 {
24217 	int error;
24218 	ldi_handle_t	lh = NULL;
24219 	ldi_ident_t	li = NULL;
24220 	int		rval;
24221 	cred_t		*cr;
24222 	struct strioctl iocb;
24223 	struct lifreq	lifreq;
24224 
24225 	IP_MAJ = ddi_name_to_major(IP);
24226 
24227 #ifdef NS_DEBUG
24228 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24229 	    ipst->ips_netstack->netstack_stackid);
24230 #endif
24231 
24232 	bzero(&lifreq, sizeof (lifreq));
24233 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24234 
24235 	error = ldi_ident_from_major(IP_MAJ, &li);
24236 	if (error) {
24237 #ifdef DEBUG
24238 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24239 		    error);
24240 #endif
24241 		return;
24242 	}
24243 
24244 	cr = zone_get_kcred(netstackid_to_zoneid(
24245 	    ipst->ips_netstack->netstack_stackid));
24246 	ASSERT(cr != NULL);
24247 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24248 	if (error) {
24249 #ifdef DEBUG
24250 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24251 		    error);
24252 #endif
24253 		goto out;
24254 	}
24255 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24256 	iocb.ic_timout = 15;
24257 	iocb.ic_len = sizeof (lifreq);
24258 	iocb.ic_dp = (char *)&lifreq;
24259 
24260 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24261 	/* LINTED - statement has no consequent */
24262 	if (error) {
24263 #ifdef NS_DEBUG
24264 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24265 		    "UDP6 error %d\n", error);
24266 #endif
24267 	}
24268 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24269 	lh = NULL;
24270 
24271 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24272 	if (error) {
24273 #ifdef NS_DEBUG
24274 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24275 		    error);
24276 #endif
24277 		goto out;
24278 	}
24279 
24280 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24281 	iocb.ic_timout = 15;
24282 	iocb.ic_len = sizeof (lifreq);
24283 	iocb.ic_dp = (char *)&lifreq;
24284 
24285 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24286 	/* LINTED - statement has no consequent */
24287 	if (error) {
24288 #ifdef NS_DEBUG
24289 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24290 		    "UDP error %d\n", error);
24291 #endif
24292 	}
24293 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24294 	lh = NULL;
24295 
24296 out:
24297 	/* Close layered handles */
24298 	if (lh)
24299 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24300 	if (li)
24301 		ldi_ident_release(li);
24302 
24303 	crfree(cr);
24304 }
24305 
24306 /*
24307  * This needs to be in-sync with nic_event_t definition
24308  */
24309 static const char *
24310 ill_hook_event2str(nic_event_t event)
24311 {
24312 	switch (event) {
24313 	case NE_PLUMB:
24314 		return ("PLUMB");
24315 	case NE_UNPLUMB:
24316 		return ("UNPLUMB");
24317 	case NE_UP:
24318 		return ("UP");
24319 	case NE_DOWN:
24320 		return ("DOWN");
24321 	case NE_ADDRESS_CHANGE:
24322 		return ("ADDRESS_CHANGE");
24323 	default:
24324 		return ("UNKNOWN");
24325 	}
24326 }
24327 
24328 static void
24329 ill_hook_event_destroy(ill_t *ill)
24330 {
24331 	hook_nic_event_t	*info;
24332 
24333 	if ((info = ill->ill_nic_event_info) != NULL) {
24334 		if (info->hne_data != NULL)
24335 			kmem_free(info->hne_data, info->hne_datalen);
24336 		kmem_free(info, sizeof (hook_nic_event_t));
24337 
24338 		ill->ill_nic_event_info = NULL;
24339 	}
24340 
24341 }
24342 
24343 boolean_t
24344 ill_hook_event_create(ill_t *ill, lif_if_t lif, nic_event_t event,
24345     nic_event_data_t data, size_t datalen)
24346 {
24347 	ip_stack_t		*ipst = ill->ill_ipst;
24348 	hook_nic_event_t	*info;
24349 	const char		*str = NULL;
24350 
24351 	/* destroy nic event info if it exists */
24352 	if ((info = ill->ill_nic_event_info) != NULL) {
24353 		str = ill_hook_event2str(info->hne_event);
24354 		ip2dbg(("ill_hook_event_create: unexpected nic event %s "
24355 		    "attached for %s\n", str, ill->ill_name));
24356 		ill_hook_event_destroy(ill);
24357 	}
24358 
24359 	/* create a new nic event info */
24360 	if ((info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP)) == NULL)
24361 		goto fail;
24362 
24363 	ill->ill_nic_event_info = info;
24364 
24365 	if (event == NE_UNPLUMB)
24366 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
24367 	else
24368 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
24369 	info->hne_lif = lif;
24370 	info->hne_event = event;
24371 	info->hne_family = ill->ill_isv6 ?
24372 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
24373 	info->hne_data = NULL;
24374 	info->hne_datalen = 0;
24375 
24376 	if (data != NULL && datalen != 0) {
24377 		info->hne_data = kmem_alloc(datalen, KM_NOSLEEP);
24378 		if (info->hne_data != NULL) {
24379 			bcopy(data, info->hne_data, datalen);
24380 			info->hne_datalen = datalen;
24381 		} else {
24382 			ill_hook_event_destroy(ill);
24383 			goto fail;
24384 		}
24385 	}
24386 
24387 	return (B_TRUE);
24388 fail:
24389 	str = ill_hook_event2str(event);
24390 	ip2dbg(("ill_hook_event_create: could not attach %s nic event "
24391 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
24392 	return (B_FALSE);
24393 }
24394